Skip to content

Commit 31101ab

Browse files
committed
style: clean up some overly abstracted code
1 parent 4017a04 commit 31101ab

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

index.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports.handler = async (event, context, callback) => {
1919
const request = event.Records[0].cf.request;
2020
const response = event.Records[0].cf.response;
2121

22-
if (shouldSkipProcessing(request, response)) {
22+
if ('200' !== response.status || isCloudFrontRequestValid(request)) {
2323
return callback(null, response);
2424
}
2525

@@ -34,7 +34,7 @@ exports.handler = async (event, context, callback) => {
3434
const key = decodeURIComponent(request.uri.substring(1));
3535
const objectResponse = await fetchOriginalImageFromS3(bucket, key);
3636

37-
if (shouldSkipImageProcessing(objectResponse, allowedContentTypes)) {
37+
if (!objectResponse.ContentType || !allowedContentTypes.includes(objectResponse.ContentType)) {
3838
return callback(null, response);
3939
}
4040

@@ -50,9 +50,8 @@ exports.handler = async (event, context, callback) => {
5050
return callback(null, response);
5151
}
5252

53-
const image = sharp(objectBody);
54-
5553
let contentType = null;
54+
const image = sharp(objectBody);
5655

5756
if (!preserveOriginalFormat) {
5857
contentType = await processImageFormat(image, objectResponse, request, params, formatParam);
@@ -83,20 +82,13 @@ exports.handler = async (event, context, callback) => {
8382
};
8483

8584
/**
86-
* Determines if the image processing should be skipped based on response status
87-
* and origin information.
85+
* Checks if the CloudFront request is valid for processing.
8886
*
8987
* @param {Object} request - CloudFront request object
90-
* @param {Object} response - CloudFront response object
9188
* @returns {boolean} True if processing should be skipped, false otherwise
9289
*/
93-
function shouldSkipProcessing(request, response) {
94-
return (
95-
'200' !== response.status ||
96-
!request.origin ||
97-
!request.origin.s3 ||
98-
!request.origin.s3.domainName
99-
);
90+
function isCloudFrontRequestValid(request) {
91+
return !request.origin || !request.origin.s3 || !request.origin.s3.domainName;
10092
}
10193

10294
/**
@@ -127,17 +119,6 @@ function fetchOriginalImageFromS3(bucket, key) {
127119
return s3Client.send(getObjectCommand);
128120
}
129121

130-
/**
131-
* Determines if image processing should be skipped based on content type.
132-
*
133-
* @param {Object} objectResponse - S3 object response
134-
* @param {string[]} allowedContentTypes - Array of allowed content types
135-
* @returns {boolean} True if processing should be skipped, false otherwise
136-
*/
137-
function shouldSkipImageProcessing(objectResponse, allowedContentTypes) {
138-
return !objectResponse.ContentType || !allowedContentTypes.includes(objectResponse.ContentType);
139-
}
140-
141122
/**
142123
* Processes the image format based on settings and request headers.
143124
*

0 commit comments

Comments
 (0)