导言
不使用内部套接字,使用 Fastify 的 注入 函数.
看来 快点 (如名称所暗示) 比起来 无服务器表达式 财务报告和审定财务报表 aws- server- fastify (英语) 😉
安装
$ npm i @fastify/aws-lambda
选项
@ fastify/aws-lambda (法语) 可通过下列方式选择: awsLambdaFastify(app, options)
| 属性 | 说明 | 默认值 |
|---|---|---|
| 二进制MimeTypes | 矩阵 Content-Type 要作为二进制处理的值( API Gateway 的基准64- 编码)。 见 二进制回复。 。 。 . | [] |
| 执行Base64 | 函数 (res) => boolean 决定反应机构是否应该采用基础64编码。 省略时,内置默认基数64-encode 以非识别值响应 Content-Encoding (即说. gzip, (中文(简体) ), br, (中文(简体) ), deflate, (中文(简体) ), zstd见 二进制回复。 。 。 . | undefined (跌回一个) Content-Encoding-意识到默认) |
| 禁用Base64 编码 | 禁用响应的基础64编码并省略 isBase64Encoded 属性。 何时 undefined,当 payloadAsStream 实值 true 而请求并非来自ALB. | undefined |
| 序列化 Lambda 参数 | 在 http 页眉中激活 Lambda 事件和背景的序列 x-apigateway-event x-apigateway-context | false (曾经是) true 用于 <v2.0.0) |
| 装饰请求 | 用 Lambda 事件和上下文装饰封条请求 request.awsLambda.event request.awsLambda.context | true |
| 装饰 属性Name | 请求装饰的默认属性名称 | awsLambda |
| 调用后等待 ForEmpty EventLoop | 见: 正式文件 | undefined |
| 保留 | 保留 API 网关 URL 的舞台部分 | false |
| 路径 | 使用定义的路径Paremeter作为路径(即:. 'proxy'(中文(简体) ) | false |
| 解析Comma分离的QueryParams | 用逗号将查询字符串分析成一系列值。 使用逗号时影响查询字符串解析器的行为 有效载荷格式版本 2.0 | true |
| 有效载荷AsStream | 如果设定为真实, 响应是流并可用 awslambda.streamifyResponse | false |
| 复数数值 | 如果使用带有多值标题属性的 ALB 设置为真 | false |
二进制回复
API Gateway需要知道一个响应机构何时是二进制,这样它就可以被基数64-编码在线上(isBase64Encoded: true 在Lambda返回有效载荷. @ fastify/aws-lambda (法语) 通过两个互补路径来决定这一点——至少一个匹配触发基础64编码:
binaryMimeTypes- 与答复相符Content-Type面对一个允许入境者。 当二进制-性是内容类型本身的属性(例如.application/octet-stream, (中文(简体) ),image/png, (中文(简体) ),application/zip) (中文(简体) ).enforceBase64——这个功能随响应而来. 当二进制 - 性质取决于其它事物时使用此功能Content-Type- 最常见的是当身体是 压缩 (二进制字节)但内容类型本身类似文本(例如.application/json与Content-Encoding: br) (中文(简体) ).
何时 enforceBase64 被省略,该基数64-encode中的内置默认踢出任何非识别响应 Content-Encoding。 。 。 . 因此压缩的响应( A)gzip, (中文(简体) ), br, (中文(简体) ), deflate, (中文(简体) ), zstd,.)在盒子里工作——你不需要自己为普通案件写回调.
示例——让压缩后的响应自动通过
const fastify = require('fastify')
const compress = require('@fastify/compress')
const awsLambdaFastify = require('@fastify/aws-lambda')
const app = fastify()
app.register(compress, { global: true, encodings: ['br', 'gzip'] })
app.get('/', (req, reply) => reply.send({ hello: 'world' }))
// Responses gain Content-Encoding: br|gzip, and the default behaviour
// auto-base64-encodes them for API Gateway. No extra option needed.
exports.handler = awsLambdaFastify(app)
示例 — 在默认值上添加自定义规则
exports.handler = awsLambdaFastify(app, {
binaryMimeTypes: ['application/octet-stream', 'image/png'],
// Custom rule for binary detection. Note: when you pass enforceBase64,
// it REPLACES the built-in Content-Encoding default — it doesn't compose.
// Re-implement the compression check yourself if you still want it.
enforceBase64: (res) => {
if (res.headers['content-type'] === 'application/x-protobuf') return true
const enc = res.headers['content-encoding']
return !!enc && enc !== 'identity'
}
})
示例
羊肉酱
const awsLambdaFastify = require('@fastify/aws-lambda')
const app = require('./app')
const proxy = awsLambdaFastify(app)
// or
// const proxy = awsLambdaFastify(app, { binaryMimeTypes: ['application/octet-stream'], serializeLambdaArguments: false /* default is true */ })
exports.handler = proxy
// or
// exports.handler = (event, context, callback) => proxy(event, context, callback)
// or
// exports.handler = (event, context) => proxy(event, context)
// or
// exports.handler = async (event, context) => proxy(event, context)
应用程序.js
const fastify = require('fastify')
const app = fastify()
app.get('/', (request, reply) => reply.send({ hello: 'world' }))
if (require.main === module) {
// called directly i.e. "node app"
app.listen({ port: 3000 }, (err) => {
if (err) console.error(err)
console.log('server listening on 3000')
})
} else {
// required as a module => executed on aws lambda
module.exports = app
}
当执行你的羊肉达功能时 我们不需要听一个特定的端口,
所以,我们只是输出 app 在这种情况下.
这个 lambda.js 文件将使用此导出 .
当你像往常一样执行快速化应用程序时,
即说. node app.js (检测结果可能是 require.main === module(中文(简体) ), (中文(简体) ),
您通常可以听取您的端口,所以您仍然可以在本地运行您的快感功能.
#
Lambda 论点
原本的Lambda事件和上下文通过Fastify请求传递,可以像这样被使用:
app.get('/', (request, reply) => {
const event = request.awsLambda.event
const context = request.awsLambda.context
// ...
})
如果您不喜欢, 您可以设置 : decorateRequest 选项到 false。 。 。 .
或者,原始的lambda事件和上下文通过信头传递,并可以像这样使用,如果设置 serializeLambdaArguments 选项到 true数字 :
app.get('/', (request, reply) => {
const event = JSON.parse(decodeURIComponent(request.headers['x-apigateway-event']))
const context = JSON.parse(decodeURIComponent(request.headers['x-apigateway-context']))
// ...
})
低温起步时间
自兹 AWS Lambda现在能够使用ECMAScript模块 在节点.js 14 运行时,您可以在使用时降低寒冷的起步间隔 备付货币 感谢顶级等待功能.
我们可以用这个叫 fastify.ready() 在 Lambda 处理器功能外的函数,像这样:
import awsLambdaFastify from '@fastify/aws-lambda'
import app from './app.js'
export const handler = awsLambdaFastify(app)
await app.ready() // needs to be placed after awsLambdaFastify call because of the decoration: https://github.com/fastify/aws-lambda-fastify/blob/main/index.js#L9
给 您可以找到讨论此特性的近似问题.
支持反应流payloadAsStream(中文(简体) )
import awsLambdaFastify from '@fastify/aws-lambda'
import { promisify } from 'node:util'
import stream from 'node:stream'
import app from './app.js'
const pipeline = promisify(stream.pipeline)
const proxy = awsLambdaFastify(app, { payloadAsStream: true })
export const handler = awslambda.streamifyResponse(async (event, responseStream, context) => {
const { meta, stream } = await proxy(event, context)
responseStream = awslambda.HttpResponseStream.from(responseStream, meta)
await pipeline(stream, responseStream)
})
await app.ready() // https://github.com/fastify/aws-lambda-fastify/issues/89
给 您可以找到讨论此特性的近似问题.
*一些基本业绩指标
@fastify/aws-lambda(装饰请求:虚假) 页:1 56 892项行动/秒 ±3.73%(79次抽样)
@ fastify/aws-lambda (法语) 页:1 56 571项行动/sec ±3.52%(82次抽样)
@fastify/aws-lambda(序列化Lambda Arguments: true) 页:1 56 499项行动/sec ±3.56%(76次抽样)
无服务器 - http 页:1 45 867起行动/sec ±4.42%(83次抽样)
aws- server- fastify (英语) 页:1 17 937起行动/事件 ±1.83%(86次抽样)
无服务器表达式 页:1 16 647项行动/sec ±2.88%(87次抽样)
最快的是,我们 @fastify/aws-lambda(装饰请求:虚假), @fastify/aws-lambda
考虑
- 对于一次可能看不到几分钟流量的应用程序,你可以看到 开始冷
- 只有无国籍人
- API Gateway有29秒的超时,而Lambda最长的执行时间为15分钟. (使用应用程序负载平衡器没有超时限制,所以Lambda最大执行时间是相关的)
- 如果您在Fastify(即连接、快递、Koa、Restana、Sails、Hapi、Restify)之外使用另一个网络框架,或者想要使用一个更通用的无服务器代理框架,请查看: 无服务器 - http 或者说 无服务器适应器
谁在用?
本页面所显示的标志是各自组织的财产,属于 与@fastify/aws-lambda(麻省理工学院)相同.