Skip to content

Commit

Permalink
Add support for streaming max age
Browse files Browse the repository at this point in the history
This was previously not implemented, it turns out there's an somewhat unknown API to do it on `awslambda`.
  • Loading branch information
joehoyle committed Nov 20, 2023
1 parent b3f9d61 commit b5da6c6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
16 changes: 14 additions & 2 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
declare type ResponseStream = {
setContentType( type: string ): void;
write( stream: string | Buffer ): void;
end(): void;
};

declare type StreamifyHandler = ( event: APIGatewayProxyEventV2, response: ResponseStream ) => Promise<any>;

declare var awslambda: {
streamifyResponse: (
handler: ( event: APIGatewayProxyEventV2, response: ResponseStream ) => Promise<any>
) => ( event: APIGatewayProxyEventV2, context: ResponseStream ) => void;
handler: StreamifyHandler
) => ( event: APIGatewayProxyEventV2, context: ResponseStream ) => void,
HttpResponseStream: {
from: ( responseStream: ResponseStream, metadata: { statusCode: number, headers: HeadersInit } ) => ResponseStream,
},
};

26 changes: 17 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
"preset": "ts-jest/presets/default-esm",
testEnvironment: 'node',
testMatch: ['**/tests/**/test-*.ts'],
globals: {
"ts-jest": {
useESM: true,
}
},
"extensionsToTreatAsEsm": [".ts"]
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
testMatch: ['**/tests/**/test-*.ts'],
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
};
24 changes: 12 additions & 12 deletions src/lambda-handler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { APIGatewayProxyEventV2 } from 'aws-lambda';

import { Args, getS3File, resizeBuffer, Config } from './lib.js';

type ResponseStream = {
setContentType( type: string ): void;
write( stream: string | Buffer ): void;
end(): void;
};

type StreamifyHandler = ( event: APIGatewayProxyEventV2, response: ResponseStream ) => Promise<any>;

/**
*
* @param event
Expand Down Expand Up @@ -47,8 +37,6 @@ const streamify_handler: StreamifyHandler = async ( event, response ) => {

let { info, data } = await resizeBuffer( buffer, args );
// If this is a signed URL, we need to calculate the max-age of the image.
// We don't currently have a way to actually set this header with response
// streaming. It doesn't appear streamifyResponse support sending headers.
let maxAge = 31536000;
if ( args['X-Amz-Expires'] ) {
// Date format of X-Amz-Date is YYYYMMDDTHHMMSSZ, which is not parsable by Date.
Expand All @@ -65,6 +53,12 @@ const streamify_handler: StreamifyHandler = async ( event, response ) => {
// Mage age is the date the URL expires minus the current time.
maxAge = Math.round( expires - new Date().getTime() / 1000 ); // eslint-disable-line no-unused-vars
}
response = awslambda.HttpResponseStream.from( response, {
statusCode: 200,
headers: {
'Cache-Control': `max-age=${maxAge}`,
},
} );
response.setContentType( 'image/' + info.format );
response.write( data );
response.end();
Expand All @@ -79,6 +73,12 @@ if ( typeof awslambda === 'undefined' ) {
streamifyResponse( handler: StreamifyHandler ): StreamifyHandler {
return handler;
},

HttpResponseStream: {
from( stream: ResponseStream, metadata: { statusCode: number, headers: HeadersInit } ): ResponseStream {
return stream;
},
},
};
}

Expand Down

0 comments on commit b5da6c6

Please sign in to comment.