@@ -4,12 +4,16 @@ import init from "./index.js";
44import warmer from "lambda-warmer" ;
55import { type APIGatewayEvent , type Context } from "aws-lambda" ;
66import { InternalServerError , ValidationError } from "common/errors/index.js" ;
7+ import { promisify } from "node:util" ;
8+ import stream from "node:stream" ;
79
10+ const pipeline = promisify ( stream . pipeline ) ;
811const app = await init ( ) ;
912const realHandler = awsLambdaFastify ( app , {
1013 decorateRequest : false ,
1114 serializeLambdaArguments : true ,
1215 callbackWaitsForEmptyEventLoop : false ,
16+ payloadAsStream : true ,
1317} ) ;
1418const handler = async ( event : APIGatewayEvent , context : Context ) => {
1519 // if a warming event
@@ -36,21 +40,13 @@ const handler = async (event: APIGatewayEvent, context: Context) => {
3640 } ;
3741 }
3842 }
39- // else proceed with handler logic
40- return await realHandler ( event , context ) . catch ( ( e ) => {
41- console . error ( e ) ;
42- const newError = new InternalServerError ( {
43- message : "Failed to initialize application." ,
44- } ) ;
45- const json = JSON . stringify ( newError . toJson ( ) ) ;
46- return {
47- statusCode : newError . httpStatusCode ,
48- body : json ,
49- headers : {
50- "Content-Type" : "application/json" ,
51- } ,
52- isBase64Encoded : false ,
53- } ;
43+ awslambda . streamifyResponse ( async ( event , responseStream , context ) => {
44+ const { meta, stream } = await realHandler ( event , context ) ;
45+ responseStream = awslambda . HttpResponseStream . from (
46+ responseStream ,
47+ meta as unknown as Record < string , unknown > ,
48+ ) ; // weird typing bug
49+ await pipeline ( stream , responseStream ) ;
5450 } ) ;
5551} ;
5652
0 commit comments