Skip to content

Commit 895e71a

Browse files
authored
Merge pull request #98 from timkelty/fix/preparse-autovals
Fix/preparse autovals
2 parents b938cfe + 1d270e5 commit 895e71a

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/ImageHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class ImageHandler {
164164
}
165165
image.avif(options)
166166
} else {
167-
image.toFormat(edits.fm.processedValue)
167+
image.toFormat(fm)
168168
}
169169

170170
return image

src/ImageRequest.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,40 @@ class ImageRequest {
3131

3232
this.sharpObject = sharp(this.originalImageBody)
3333
this.originalMetadata = await this.sharpObject.metadata()
34-
35-
const qp = this._parseQueryParams()
36-
3734
this.headers = this.event.headers
3835

39-
if (qp.auto !== undefined) {
40-
qp.fm = this.inferAutoFormat()
41-
}
42-
this.schema = schemaParser.getSchemaForQueryParams(qp)
43-
this.edits = schemaParser.normalizeAndValidateSchema(this.schema, qp)
36+
const queryParams = this.normalizeQueryParams(this.event.queryStringParameters)
37+
38+
this.schema = schemaParser.getSchemaForQueryParams(queryParams)
39+
this.edits = schemaParser.normalizeAndValidateSchema(this.schema, queryParams)
4440
}
4541

46-
inferAutoFormat() {
47-
const specialOutputFormats = eventParser.getAcceptedImageFormatsFromHeaders(this.headers)
42+
getAutoFormat() {
4843
const coercibleFormats = ['jpg', 'png', 'webp', 'avif', 'jpeg', 'tiff']
44+
const autoParam = this.event.multiValueQueryStringParameters.auto
45+
const specialOutputFormats = eventParser.getAcceptedImageFormatsFromHeaders(this.headers)
4946

50-
if (!coercibleFormats.includes(this.originalMetadata.format)) {
51-
return this.originalMetadata.format
47+
if (
48+
!autoParam ||
49+
!autoParam.includes('format') ||
50+
!coercibleFormats.includes(this.originalMetadata.format)
51+
) {
52+
return null
5253
}
5354

54-
// TODO: Ensure image is at least 16x16px for avif compatibility - this needs to happen down the line after schema parsing and validation
5555
if (specialOutputFormats.includes('avif')) {
5656
return 'avif'
5757
}
5858
// If avif isn't available, try to use webp
59-
if (specialOutputFormats.includes('webp')) {
59+
else if (specialOutputFormats.includes('webp')) {
6060
return 'webp'
6161
}
6262
// Coerce pngs and tiffs without alpha channels to jpg
63-
if (!this.originalMetadata.hasAlpha && (['png', 'tiff'].contains(this.originalMetadata.format))) {
63+
else if (!this.originalMetadata.hasAlpha && (['png', 'tiff'].includes(this.originalMetadata.format))) {
6464
return 'jpeg'
6565
}
66-
// There's no other coercion worth doing, use original format
67-
return this.originalMetadata.format
66+
67+
return null
6868
}
6969

7070
/**
@@ -107,17 +107,12 @@ class ImageRequest {
107107
return true
108108
}
109109

110-
/**
111-
* Decodes the image request path associated with default
112-
* image requests. Provides error handling for invalid or undefined path values.
113-
* @param {Object} event - The proxied request object.
114-
*/
115-
_parseQueryParams () {
116-
let qp = this.event.queryStringParameters
117-
if (!qp) {
118-
qp = {}
119-
}
120-
return schemaParser.replaceAliases(qp)
110+
normalizeQueryParams (params = {}) {
111+
let normalizedParams = schemaParser.replaceAliases(params)
112+
113+
normalizedParams.fm = this.getAutoFormat() || normalizedParams.fm || this.originalMetadata.format
114+
115+
return normalizedParams
121116
}
122117
}
123118

0 commit comments

Comments
 (0)