From 5aafb2de306d6fd15f08acdf2c1658ea921f626d Mon Sep 17 00:00:00 2001 From: Jerico Aragon Date: Thu, 14 Mar 2024 19:42:17 +0800 Subject: [PATCH] Smaller scope for try/catch block to detect 404 --- src/lambda-handler.ts | 87 ++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/lambda-handler.ts b/src/lambda-handler.ts index 1cc88fa..6dca456 100644 --- a/src/lambda-handler.ts +++ b/src/lambda-handler.ts @@ -40,61 +40,64 @@ const streamify_handler: StreamifyHandler = async ( event, response ) => { delete args.presign; } - try { - let s3_response = await getS3File( config, key, args ); - if ( ! s3_response.Body ) { - throw new Error( 'No body in file.' ); - } - let buffer = Buffer.from( await s3_response.Body.transformToByteArray() ); - - let { info, data } = await resizeBuffer( buffer, args ); - // If this is a signed URL, we need to calculate the max-age of the image. - let maxAge = 31536000; - if ( args['X-Amz-Expires'] ) { - // Date format of X-Amz-Date is YYYYMMDDTHHMMSSZ, which is not parsable by Date. - const dateString = args['X-Amz-Date']!.replace( - /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/, - '$1-$2-$3T$4:$5:$6Z' - ); - const date = new Date( dateString ); - - // Calculate when the signed URL will expire, as we'll set the max-age - // cache control to this value. - const expires = date.getTime() / 1000 + Number( args['X-Amz-Expires'] ); - - // 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 - } - - // 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, - }, - } ); + let s3_response; - response.write( data ); - response.end(); + try { + s3_response = await getS3File( config, key, args ); } catch ( e: any ) { // An AccessDenied error means the file is either protected, or doesn't exist. if ( e.Code === 'AccessDenied' ) { const metadata = { statusCode: 404, headers: { - 'Content-Type': 'text/html', - 'Cache-Control': 'no-cache', + 'Content-Type': 'text/html' }, }; response = awslambda.HttpResponseStream.from( response, metadata ); response.write( 'File not found.' ); response.end(); - } else { - throw e; - } + return; + } + throw e; } + + if ( ! s3_response.Body ) { + throw new Error( 'No body in file.' ); + } + + let buffer = Buffer.from( await s3_response.Body.transformToByteArray() ); + + let { info, data } = await resizeBuffer( buffer, args ); + // If this is a signed URL, we need to calculate the max-age of the image. + let maxAge = 31536000; + if ( args['X-Amz-Expires'] ) { + // Date format of X-Amz-Date is YYYYMMDDTHHMMSSZ, which is not parsable by Date. + const dateString = args['X-Amz-Date']!.replace( + /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/, + '$1-$2-$3T$4:$5:$6Z' + ); + const date = new Date( dateString ); + + // Calculate when the signed URL will expire, as we'll set the max-age + // cache control to this value. + const expires = date.getTime() / 1000 + Number( args['X-Amz-Expires'] ); + + // 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 + } + + // 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.write( data ); + response.end(); }; if ( typeof awslambda === 'undefined' ) {