Skip to content

Commit

Permalink
Merge pull request #182 from humanmade/fix-import
Browse files Browse the repository at this point in the history
Import with file extensions
  • Loading branch information
joehoyle committed Mar 5, 2024
2 parents 00b3e26 + 6dda3aa commit 2e73546
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
3 changes: 2 additions & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
Expand All @@ -13,7 +14,7 @@ declare var awslambda: {
HttpResponseStream: {
from( response: ResponseStream, metadata: {
headers?: Record<string, string>,
statusCode?: number,
statusCode: number,
cookies?: string[],
} ): ResponseStream
}
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ export default {
},
],
},
moduleNameMapper: {
"(.+)\\.js": "$1"
},
};
6 changes: 4 additions & 2 deletions src/lambda-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Args, getS3File, resizeBuffer, Config } from './lib';
import { Args, getS3File, resizeBuffer, Config } from './lib.js';
/**
*
* @param event
Expand Down Expand Up @@ -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();
};
Expand All @@ -96,6 +97,7 @@ if ( typeof awslambda === 'undefined' ) {
from( response: ResponseStream, metadata: {
headers?: Record<string, string>,
} ): ResponseStream {
response.metadata = metadata;
return response;
},
},
Expand Down
8 changes: 6 additions & 2 deletions tests/test-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +47,9 @@ class TestResponseStream {
}
}
end(): void {

if ( this.metadata.headers['Content-Type'] ) {
this.contentType = this.metadata.headers['Content-Type'];
}
}
}

Expand All @@ -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;
},
},
Expand Down
6 changes: 6 additions & 0 deletions tests/test-private-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
},
} );

Expand Down Expand Up @@ -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'];
}
},
} );

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"strict": true,
"preserveConstEnums": true,
"sourceMap": false,
"module": "ES2022",
"moduleResolution": "node",
"module": "Node16",
"moduleResolution": "node16",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"preserveConstEnums": true,
"noEmit": false,
"sourceMap": true,
"module": "ES2022",
"moduleResolution": "node",
"module": "Node16",
"moduleResolution": "Node16",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
Expand Down

0 comments on commit 2e73546

Please sign in to comment.