Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $ npm i @fastify/aws-lambda
| decorateRequest | Decorates the fastify request with the lambda Event and Context `request.awsLambda.event` `request.awsLambda.context` | `true` |
| decorationPropertyName | The default property name for request decoration | `awsLambda` |
| callbackWaitsForEmptyEventLoop | See: [Official Documentation](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html#nodejs-prog-model-context-properties) | `undefined` |
| isBinary | Function that receives the response and returns a boolean indicating if the response content is binary or not | `undefined` |

## 📖Example

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ module.exports = (app, options) => {
})

const contentType = (res.headers['content-type'] || res.headers['Content-Type'] || '').split(';')[0]
const isBase64Encoded = options.binaryMimeTypes.indexOf(contentType) > -1
const customBinaryCheck = typeof options.isBinary === 'function' && !!options.isBinary(res)
const isBase64Encoded = options.binaryMimeTypes.indexOf(contentType) > -1 || customBinaryCheck

const ret = {
statusCode: res.statusCode,
Expand Down
41 changes: 41 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@ test('GET with base64 encoding response', async (t) => {
t.equal(ret.headers['set-cookie'], 'qwerty=one')
})

test('GET with custom binary check response', async (t) => {
t.plan(15)

const readFileAsync = promisify(fs.readFile)
const fileBuffer = await readFileAsync(__filename)
const app = fastify()
app.get('/test', async (request, reply) => {
t.equal(request.headers['x-my-header'], 'wuuusaaa')
t.equal(request.headers['x-apigateway-event'], '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%2C%22Content-Type%22%3A%22application%2Fjson%22%7D%7D')
t.equal(request.headers['user-agent'], 'lightMyRequest')
t.equal(request.headers.host, 'localhost:80')
t.equal(request.headers['content-length'], '0')
reply.header('Set-Cookie', 'qwerty=one')
reply.header('Content-Encoding', 'gzip')
reply.send(fileBuffer)
})
const proxy = awsLambdaFastify(app, {
binaryMimeTypes: [],
serializeLambdaArguments: true,
isBinary: (res) => !!(res.headers['Content-Encoding'] || res.headers['content-encoding'])
})
const ret = await proxy({
httpMethod: 'GET',
path: '/test',
headers: {
'X-My-Header': 'wuuusaaa',
'Content-Type': 'application/json'
}
})
t.equal(ret.statusCode, 200)
t.equal(ret.body, fileBuffer.toString('base64'))
t.equal(ret.isBase64Encoded, true)
t.ok(ret.headers)
t.equal(ret.headers['content-type'], 'application/octet-stream')
t.ok(ret.headers['content-length'])
t.ok(ret.headers.date)
t.equal(ret.headers.connection, 'keep-alive')
t.same(ret.multiValueHeaders, undefined)
t.equal(ret.headers['set-cookie'], 'qwerty=one')
})

test('GET with multi-value query params', async (t) => {
t.plan(2)

Expand Down