Skip to content

Commit 8879cf3

Browse files
forabibrettstack
authored andcommitted
feat(binary): allow wildcards and extensions in binary types (#136)
1 parent eaf8735 commit 8879cf3

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

__tests__/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,42 @@ describe('forwardResponseToApiGateway: content-type encoding', () => {
200200
isBase64Encoded: false
201201
}))
202202
})
203+
204+
test('wildcards in binary types array', () => {
205+
const server = new MockServer(['image/*'])
206+
const headers = {'content-type': 'image/jpeg'}
207+
const body = 'hello world'
208+
const response = new MockResponse(200, headers, body)
209+
return new Promise(
210+
(resolve, reject) => {
211+
const context = new MockContext(resolve)
212+
awsServerlessExpress.forwardResponseToApiGateway(
213+
server, response, context)
214+
}
215+
).then(successResponse => expect(successResponse).toEqual({
216+
statusCode: 200,
217+
body: new Buffer(body).toString('base64'),
218+
headers: headers,
219+
isBase64Encoded: true
220+
}))
221+
})
222+
223+
test('extensions in binary types array', () => {
224+
const server = new MockServer(['.png'])
225+
const headers = {'content-type': 'image/png'}
226+
const body = 'hello world'
227+
const response = new MockResponse(200, headers, body)
228+
return new Promise(
229+
(resolve, reject) => {
230+
const context = new MockContext(resolve)
231+
awsServerlessExpress.forwardResponseToApiGateway(
232+
server, response, context)
233+
}
234+
).then(successResponse => expect(successResponse).toEqual({
235+
statusCode: 200,
236+
body: new Buffer(body).toString('base64'),
237+
headers: headers,
238+
isBase64Encoded: true
239+
}))
240+
})
203241
})

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
const http = require('http')
1717
const url = require('url')
1818
const binarycase = require('binary-case')
19+
const isType = require('type-is')
1920

2021
function getPathWithQueryStringParams(event) {
2122
return url.format({ pathname: event.path, query: event.queryStringParameters })
@@ -27,7 +28,7 @@ function getContentType(params) {
2728
}
2829

2930
function isContentTypeBinaryMimeType(params) {
30-
return params.binaryMimeTypes.indexOf(params.contentType) !== -1
31+
return params.binaryMimeTypes.length > 0 && !!isType.is(params.contentType, params.binaryMimeTypes)
3132
}
3233

3334
function mapApiGatewayEventToHttpRequest(event, context, socketPath) {

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
"security-scan": "nsp check"
9090
},
9191
"dependencies": {
92-
"binary-case": "^1.0.0"
92+
"binary-case": "^1.0.0",
93+
"type-is": "^1.6.16"
9394
},
9495
"config": {
9596
"commitizen": {

0 commit comments

Comments
 (0)