
灵感来自AWSLABS AWS-Server-Server-Spack-Express Library为Fastify Web框架量身定制。
不使用内部插座,可以利用Fastify的注射功能。
(如名称所暗示的)似乎比AWS-Serverless-express和AWS-Serverless-Fastify更快
$ npm i @fastify/aws-lambda@fastify/aws-lambda可以通过以下方式通过: awsLambdaFastify(app, options)
| 财产 | 描述 | 默认值 |
|---|---|---|
| 二进制型 | 一系列二进制模拟型要处理 | [] |
| EnforceBase64 | 接收响应并返回布尔值的功能,指示响应内容是否是二进制的,应进行基础64编码 | undefined |
| Serializelambdaarguments | 激活HTTP标头x-apigateway-event x-apigateway-context中Lambda事件和上下文的序列化 | false (对于<v2.0.0是true ) |
| DecoratereQuest | 使用lambda事件和上下文request.awsLambda.event装饰快速的请求request.awsLambda.context | true |
| DecorationPropertyname | 请求装饰的默认属性名称 | awsLambda |
| callbackwaitsforemptyeventloop | 请参阅:官方文档 | undefined |
| 保留 | 保留API网关URL的舞台部分 | false |
| Pathparameteraspath | 使用定义的路径参数作为路径(即'proxy' ) | false |
| parsecommaseparatedSqueryparams | 用逗号分析成一个值的值。在使用有效载荷格式2.0时,用逗号影响Querystring解析器的行为 | true |
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) 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
}在您的lambda函数中执行时,我们不需要收听特定端口,因此在这种情况下,我们只是导出该app 。 lambda.js文件将使用此导出。
当您像往常一样执行快速应用程序时,IE node app.js (可能require.main === module ) ,您可以正常收听端口,因此您仍然可以在本地运行快速函数。
原始的lambda事件和上下文是通过快速请求传递的,可以这样使用:
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现在可以在Node.js 14 runtimes中使用Ecmascript(ES)模块,因此,由于顶级等待功能,您可以降低与配置同时发生时的冷启动潜伏期。
我们可以通过调用lambda处理程序功能之外的fastify.ready()函数来使用此功能,例如:
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/master/index.js#L9在这里,您可以找到讨论此功能的资助问题。
@fastify/aws-lambda(decoraterequest:false) x 56,892 ops/sec ±3.73%(79运行采样)
@fastify/aws-lambda x 56,571 ops/sec ±3.52%(82运行采样)
@fastify/aws-lambda(serializelambdaarguments:true) x 56,499 ops/sec/sec ±3.56%(76次运行)
无服务器-HTTP X 45,867 OPS/sec ±4.42%(83运行采样)
AWS-Serverless-Fastify x 17,937 OPS/sec ±1.83%(86行采样)
AWS-Server-Sexpress x 16,647 OPS/sec ±2.88%(87运行采样)
最快的是@fastify/aws-lambda(decoraterequest:false), @fastify/aws-lambda
此页面中显示的徽标是相应组织的属性,并且它们的分配与 @fastify/aws-lambda(MIT)相同的许可证。