TopGit theo dõi fastify/aws-lambda-fastify trên GitHub trong nhóm Backend, đã đạt 560 sao. Insipired by aws-serverless-express to work with Fastify with inject functionality
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Inspired by the Code Genie serverless-express library tailor made for the Fastify web framework.
No use of internal sockets, makes use of Fastify's inject function.
Seems faster(as the name implies)than serverless-express and aws-serverless-fastify 😉
👨🏻💻Installation
$ npm i @fastify/aws-lambda
Options
@fastify/aws-lambda can take options by passing them with : awsLambdaFastify(app, options)
property
description
default value
binaryMimeTypes
Array of Content-Type values to treat as binary (base64-encoded for API Gateway). See Binary responses.
[]
enforceBase64
Function (res) => boolean that decides whether the response body should be base64-encoded. When omitted, a built-in default base64-encodes any response with a non-identity Content-Encoding (i.e. gzip, br, deflate, zstd, …). See Binary responses.
undefined(falls back to a Content-Encoding-aware default)
disableBase64Encoding
Disable base64 encoding of responses and omit the isBase64Encoded property. When undefined, it is automatically enabled when payloadAsStream is true and the request does not come from an ALB.
undefined
serializeLambdaArguments
Activate the serialization of lambda Event and Context in http header x-apigateway-eventx-apigateway-context
false(was true for <v2.0.0)
decorateRequest
Decorates the fastify request with the lambda Event and Context request.awsLambda.eventrequest.awsLambda.context
true
decorationPropertyName
The default property name for request decoration
awsLambda
callbackWaitsForEmptyEventLoop
See: Official Documentation
undefined
retainStage
Retain the stage part of the API Gateway URL
false
pathParameterUsedAsPath
Use a defined pathParameter as path (i.e. 'proxy')
false
parseCommaSeparatedQueryParams
Parse querystring with commas into an array of values. Affects the behavior of the querystring parser with commas while using Payload Format Version 2.0
true
payloadAsStream
If set to true the response is a stream and can be used with awslambda.streamifyResponse
false
albMultiValueHeaders
Set to true if using ALB with multi value headers attribute
false
Binary responses
API Gateway needs to know when a response body is binary so it can be base64-encoded over the wire (isBase64Encoded: true in the Lambda return payload). @fastify/aws-lambda decides this via two complementary paths — at least one match triggers base64 encoding:
binaryMimeTypes — matches the response Content-Type against an allowlist. Use this when binary-ness is a property of the content type itself (e.g. application/octet-stream, image/png, application/zip).
enforceBase64 — a function called with the response. Use this when binary-ness depends on something other than Content-Type — most commonly when the body is compressed (binary bytes) but the content type itself is text-like (e.g. application/json with Content-Encoding: br).
When enforceBase64 is omitted, a built-in default kicks in that base64-encodes any response with a non-identity Content-Encoding. So compressed responses (gzip, br, deflate, zstd, …) work out of the box — you don't need to write your own callback for the common case.
Example — let compressed responses pass through automatically
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)
Example — add a custom rule on top of the default
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 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
}
When executed in your lambda function we don't need to listen to a specific port,
so we just export the app in this case.
The lambda.js file will use this export.
When you execute your Fastify application like always,
i.e. node app.js(the detection for this could be require.main === module),
you can normally listen to your port, so you can still run your Fastify function locally.
📣Hint
Lambda arguments
The original lambda event and context are passed via Fastify request and can be used like this:
If you do not like it, you can disable this by setting the decorateRequest option to false.
Alternatively the original lambda event and context are passed via headers and can be used like this, if setting the serializeLambdaArguments option to true:
Since AWS Lambda now enables the use of ECMAScript (ES) modules in Node.js 14 runtimes, you could lower the cold start latency when used with Provisioned Concurrency thanks to the top-level await functionality.
We can use this by calling the fastify.ready() function outside of the Lambda handler function, like this:
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
Here you can find the approriate issue discussing this feature.
serverless-http x 45,867 ops/sec ±4.42% (83 runs sampled)
aws-serverless-fastify x 17,937 ops/sec ±1.83% (86 runs sampled)
serverless-express x 16,647 ops/sec ±2.88% (87 runs sampled)
Fastest is @fastify/aws-lambda (decorateRequest : false), @fastify/aws-lambda
⚠️Considerations
For apps that may not see traffic for several minutes at a time, you could see cold starts
Stateless only
API Gateway has a timeout of 29 seconds, and Lambda has a maximum execution time of 15 minutes. (Using Application Load Balancer has no timeout limit, so the lambda maximum execution time is relevant)
If you are using another web framework beside Fastify (i.e. Connect, Express, Koa, Restana, Sails, Hapi, Restify) or want to use a more generic serverless proxy framework, have a look at: serverless-http or serverless-adapter
🎖Who is using it?
The logos displayed in this page are property of the respective organisations and they are
not distributed under the same license as @fastify/aws-lambda (MIT).
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/fastify/aws-lambda-fastify là nguồn chính thức.
fastify/aws-lambda-fastify có phải mã nguồn mở không?
Có — fastify/aws-lambda-fastify phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/fastify/aws-lambda-fastify.
fastify/aws-lambda-fastify có trang demo không?
Dự án có trang chủ ở https://npmjs.com/package/@fastify/aws-lambda. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
fastify/aws-lambda-fastify còn đang phát triển không?
Commit gần nhất trên fastify/aws-lambda-fastify là 15 ngày trước (theo timestamp GitHub). Repo có 37 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
fastify/aws-lambda-fastify dùng license gì?
fastify/aws-lambda-fastify phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về aws-lambda-fastify?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về aws-lambda-fastify.