From 8ec8a50e232f942a34d2d74639afbb09c693e853 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 5 Mar 2024 15:59:49 +0200 Subject: [PATCH 1/4] Import with file extensions Setting the tsconfig properly makes TSC also throw an error / warning for the extensionless imports too. --- src/lambda-handler.ts | 2 +- tsconfig.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lambda-handler.ts b/src/lambda-handler.ts index 6a5e243..b0053d1 100644 --- a/src/lambda-handler.ts +++ b/src/lambda-handler.ts @@ -1,4 +1,4 @@ -import { Args, getS3File, resizeBuffer, Config } from './lib'; +import { Args, getS3File, resizeBuffer, Config } from './lib.js'; /** * * @param event diff --git a/tsconfig.json b/tsconfig.json index 5527e68..52de306 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,8 +4,8 @@ "strict": true, "preserveConstEnums": true, "sourceMap": false, - "module": "ES2022", - "moduleResolution": "node", + "module": "Node16", + "moduleResolution": "node16", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, From 25ff598a2c6b79cef08c77bb5a68bf955052ad0d Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 5 Mar 2024 16:37:29 +0200 Subject: [PATCH 2/4] Fix imports - FIx imports to use file extensions per latest spec - Pass Content-Type as a header, as it's not possible to use response.contentType() with header streaming. - Fix jest with js extensions imports --- global.d.ts | 2 +- jest.config.js | 3 +++ src/lambda-handler.ts | 3 ++- tsconfig.test.json | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/global.d.ts b/global.d.ts index 4680475..8c76986 100644 --- a/global.d.ts +++ b/global.d.ts @@ -13,7 +13,7 @@ declare var awslambda: { HttpResponseStream: { from( response: ResponseStream, metadata: { headers?: Record, - statusCode?: number, + statusCode: number, cookies?: string[], } ): ResponseStream } diff --git a/jest.config.js b/jest.config.js index f813a19..a1214ee 100644 --- a/jest.config.js +++ b/jest.config.js @@ -13,4 +13,7 @@ export default { }, ], }, + moduleNameMapper: { + "(.+)\\.js": "$1" + }, }; diff --git a/src/lambda-handler.ts b/src/lambda-handler.ts index b0053d1..9a22222 100644 --- a/src/lambda-handler.ts +++ b/src/lambda-handler.ts @@ -67,13 +67,14 @@ const streamify_handler: StreamifyHandler = async ( event, response ) => { // Somewhat undocumented API on how to pass headers to a stream response. response = awslambda.HttpResponseStream.from( response, { + statusCode: 200, headers: { 'Cache-Control': `max-age=${ maxAge }`, 'Last-Modified': ( new Date() ).toUTCString(), + 'Content-Type': 'image/' + info.format, }, } ); - response.setContentType( 'image/' + info.format ); response.write( data ); response.end(); }; diff --git a/tsconfig.test.json b/tsconfig.test.json index 47e8a9a..0b73dc5 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -5,8 +5,8 @@ "preserveConstEnums": true, "noEmit": false, "sourceMap": true, - "module": "ES2022", - "moduleResolution": "node", + "module": "Node16", + "moduleResolution": "Node16", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, From 78e568db247f96b7627b6709bd5c9710e9f680ef Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 5 Mar 2024 16:50:46 +0200 Subject: [PATCH 3/4] Fix tests --- tests/test-lambda.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test-lambda.ts b/tests/test-lambda.ts index adaf7a3..73fa969 100644 --- a/tests/test-lambda.ts +++ b/tests/test-lambda.ts @@ -32,6 +32,7 @@ class TestResponseStream { contentType: string | undefined; body: string | Buffer | undefined; headers: { [key: string]: string } = {}; + metadata: any; setContentType( type: string ): void { this.contentType = type; @@ -46,7 +47,9 @@ class TestResponseStream { } } end(): void { - + if ( this.metadata.headers['Content-Type'] ) { + this.contentType = this.metadata.headers['Content-Type']; + } } } @@ -63,7 +66,8 @@ global.awslambda = { * @param stream The response stream. * @param metadata The metadata for the response. */ - from( stream: ResponseStream, metadata ) : ResponseStream { + from( stream: TestResponseStream, metadata ) : TestResponseStream { + stream.metadata = metadata; return stream; }, }, From 6dda3aacc68b9d1eeb2362c809144d5b7a5dd5b9 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 5 Mar 2024 16:55:29 +0200 Subject: [PATCH 4/4] Also mock the content type from headers --- global.d.ts | 1 + src/lambda-handler.ts | 1 + tests/test-private-upload.ts | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/global.d.ts b/global.d.ts index 8c76986..48855d9 100644 --- a/global.d.ts +++ b/global.d.ts @@ -2,6 +2,7 @@ declare type ResponseStream = { setContentType( type: string ): void; write( stream: string | Buffer ): void; end(): void; + metadata?: any; }; declare type StreamifyHandler = ( event: APIGatewayProxyEventV2, response: ResponseStream ) => Promise; diff --git a/src/lambda-handler.ts b/src/lambda-handler.ts index 9a22222..4325c41 100644 --- a/src/lambda-handler.ts +++ b/src/lambda-handler.ts @@ -97,6 +97,7 @@ if ( typeof awslambda === 'undefined' ) { from( response: ResponseStream, metadata: { headers?: Record, } ): ResponseStream { + response.metadata = metadata; return response; }, }, diff --git a/tests/test-private-upload.ts b/tests/test-private-upload.ts index b54cad1..e7e6bf1 100644 --- a/tests/test-private-upload.ts +++ b/tests/test-private-upload.ts @@ -74,6 +74,9 @@ test( 'Test get private upload', async () => { * End the response. */ end(): void { + if ( this.metadata.headers['Cache-Control'] ) { + contentType = this.metadata.headers['Content-Type']; + } }, } ); @@ -115,6 +118,9 @@ test( 'Test get private upload with presign params', async () => { * End the response. */ end(): void { + if ( this.metadata.headers['Cache-Control'] ) { + contentType = this.metadata.headers['Content-Type']; + } }, } );