Skip to content

Commit 12b71bf

Browse files
committed
fix: do not magically default body to ''
1 parent ae89512 commit 12b71bf

File tree

4 files changed

+38
-38
lines changed

4 files changed

+38
-38
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = (app, options) => {
3737
if (options.callbackWaitsForEmptyEventLoop !== undefined) {
3838
context.callbackWaitsForEmptyEventLoop = options.callbackWaitsForEmptyEventLoop
3939
}
40-
event.body = event.body || ''
40+
// event.body = event.body || '' // do not magically default body to ''
4141

4242
const method = event.httpMethod || (event.requestContext && event.requestContext.http ? event.requestContext.http.method : undefined)
4343
let url = (options.pathParameterUsedAsPath && event.pathParameters && event.pathParameters[options.pathParameterUsedAsPath] && `/${event.pathParameters[options.pathParameterUsedAsPath]}`) || event.path || event.rawPath || '/' // seen rawPath for HTTP-API
@@ -79,7 +79,7 @@ module.exports = (app, options) => {
7979
}
8080
})
8181
}
82-
const payload = Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
82+
const payload = event.body !== null && event.body !== undefined ? Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8') : event.body
8383
// NOTE: API Gateway is not setting Content-Length header on requests even when they have a body
8484
if (event.body && !headers['Content-Length'] && !headers['content-length']) headers['content-length'] = Buffer.byteLength(payload)
8585

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@
4141
},
4242
"devDependencies": {
4343
"@fastify/multipart": "8.1.0",
44-
"@fastify/pre-commit": "^2.0.2",
44+
"@fastify/pre-commit": "^2.1.0",
4545
"@types/aws-lambda": "8.10.131",
4646
"aws-lambda": "^1.0.7",
4747
"aws-serverless-express": "^3.4.0",
48-
"aws-serverless-fastify": "^3.0.4",
48+
"aws-serverless-fastify": "^3.0.6",
4949
"benchmark": "^2.1.4",
50-
"eslint": "^8.52.0",
50+
"eslint": "^8.56.0",
5151
"eslint-config-standard": "^17.1.0",
52-
"eslint-plugin-import": "^2.29.0",
53-
"eslint-plugin-n": "^16.2.0",
52+
"eslint-plugin-import": "^2.29.1",
53+
"eslint-plugin-n": "^16.6.2",
5454
"eslint-plugin-promise": "^6.1.1",
5555
"eslint-plugin-standard": "^5.0.0",
56-
"fastify": "^4.24.3",
56+
"fastify": "^4.25.2",
5757
"serverless-http": "^3.2.0",
5858
"tap": "^16.3.9",
59-
"tsd": "^0.30.0"
59+
"tsd": "^0.30.4"
6060
},
6161
"overrides": {
6262
"aws-serverless-fastify": {
63-
"fastify": "^4.24.3"
63+
"fastify": "^4.25.2"
6464
}
6565
},
6666
"publishConfig": {

test/alb.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const fastify = require('fastify')
55
const awsLambdaFastify = require('../index')
66

77
test('GET', async (t) => new Promise((resolve, reject) => {
8-
t.plan(14)
8+
t.plan(13)
99

1010
const app = fastify()
1111
const event = {
@@ -25,7 +25,7 @@ test('GET', async (t) => new Promise((resolve, reject) => {
2525
t.equal(request.awsLambda.event, event)
2626
t.equal(request.headers['user-agent'], 'lightMyRequest')
2727
t.equal(request.headers.host, 'localhost:80')
28-
t.equal(request.headers['content-length'], '0')
28+
// t.equal(request.headers['content-length'], '0')
2929
reply.header('Set-Cookie', 'qwerty=one')
3030
reply.header('Set-Cookie', 'qwerty=two')
3131
reply.send({ hello: 'world' })
@@ -52,7 +52,7 @@ test('GET', async (t) => new Promise((resolve, reject) => {
5252
}))
5353

5454
test('GET Broken', async (t) => new Promise((resolve, reject) => {
55-
t.plan(19)
55+
t.plan(18)
5656

5757
const event = {
5858
requestContext: { http: { method: 'GET' } },
@@ -83,7 +83,7 @@ test('GET Broken', async (t) => new Promise((resolve, reject) => {
8383
t.equal(request.awsLambda.context, context)
8484
t.equal(request.headers['user-agent'], 'lightMyRequest')
8585
t.equal(request.headers.host, 'localhost:80')
86-
t.equal(request.headers['content-length'], '0')
86+
// t.equal(request.headers['content-length'], '0')
8787
reply.header('Set-Cookie', 'qwerty=one')
8888
reply.header('Set-Cookie', 'qwerty=two')
8989
reply.header('x-Non-String', Date.now())

test/basic.test.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const fs = require('node:fs')
77
const awsLambdaFastify = require('../index')
88

99
test('GET', async (t) => {
10-
t.plan(16)
10+
t.plan(15)
1111

1212
const app = fastify()
1313
const evt = {
@@ -27,7 +27,7 @@ test('GET', async (t) => {
2727
t.equal(request.awsLambda.event, evt)
2828
t.equal(request.headers['user-agent'], 'lightMyRequest')
2929
t.equal(request.headers.host, 'localhost:80')
30-
t.equal(request.headers['content-length'], '0')
30+
// t.equal(request.headers['content-length'], '0')
3131
reply.header('Set-Cookie', 'qwerty=one')
3232
reply.header('Set-Cookie', 'qwerty=two')
3333
reply.send({ hello: 'world' })
@@ -47,7 +47,7 @@ test('GET', async (t) => {
4747
})
4848

4949
test('GET with base64 encoding response', async (t) => {
50-
t.plan(15)
50+
t.plan(14)
5151

5252
const readFileAsync = promisify(fs.readFile)
5353
const fileBuffer = await readFileAsync(__filename)
@@ -57,7 +57,7 @@ test('GET with base64 encoding response', async (t) => {
5757
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')
5858
t.equal(request.headers['user-agent'], 'lightMyRequest')
5959
t.equal(request.headers.host, 'localhost:80')
60-
t.equal(request.headers['content-length'], '0')
60+
// t.equal(request.headers['content-length'], '0')
6161
reply.header('Set-Cookie', 'qwerty=one')
6262
// reply.header('Set-Cookie', 'qwerty=two')
6363
reply.send(fileBuffer)
@@ -84,7 +84,7 @@ test('GET with base64 encoding response', async (t) => {
8484
})
8585

8686
test('GET with content-encoding response', async (t) => {
87-
t.plan(16)
87+
t.plan(15)
8888

8989
const readFileAsync = promisify(fs.readFile)
9090
const fileBuffer = await readFileAsync(__filename)
@@ -94,7 +94,7 @@ test('GET with content-encoding response', async (t) => {
9494
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')
9595
t.equal(request.headers['user-agent'], 'lightMyRequest')
9696
t.equal(request.headers.host, 'localhost:80')
97-
t.equal(request.headers['content-length'], '0')
97+
// t.equal(request.headers['content-length'], '0')
9898
reply.header('Set-Cookie', 'qwerty=one')
9999
reply.header('content-encoding', 'br')
100100
reply.send(fileBuffer)
@@ -122,7 +122,7 @@ test('GET with content-encoding response', async (t) => {
122122
})
123123

124124
test('GET with custom binary check response', async (t) => {
125-
t.plan(16)
125+
t.plan(15)
126126

127127
const readFileAsync = promisify(fs.readFile)
128128
const fileBuffer = await readFileAsync(__filename)
@@ -132,7 +132,7 @@ test('GET with custom binary check response', async (t) => {
132132
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')
133133
t.equal(request.headers['user-agent'], 'lightMyRequest')
134134
t.equal(request.headers.host, 'localhost:80')
135-
t.equal(request.headers['content-length'], '0')
135+
// t.equal(request.headers['content-length'], '0')
136136
reply.header('Set-Cookie', 'qwerty=one')
137137
reply.header('X-Base64-Encoded', '1')
138138
reply.send(fileBuffer)
@@ -315,15 +315,15 @@ test('POST with base64 encoding', async (t) => {
315315
})
316316

317317
test('subpath', async (t) => {
318-
t.plan(14)
318+
t.plan(13)
319319

320320
const app = fastify()
321321
app.get('/test', async (request, reply) => {
322322
t.equal(request.headers['x-my-header'], 'wuuusaaa')
323323
t.equal(request.headers['x-apigateway-event'], '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Fdev%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%2C%22requestContext%22%3A%7B%22resourcePath%22%3A%22%2Ftest%22%2C%22stage%22%3A%22dev%22%7D%7D')
324324
t.equal(request.headers['user-agent'], 'lightMyRequest')
325325
t.equal(request.headers.host, 'localhost:80')
326-
t.equal(request.headers['content-length'], '0')
326+
// t.equal(request.headers['content-length'], '0')
327327
reply.header('Set-Cookie', 'qwerty=one')
328328
reply.header('Set-Cookie', 'qwerty=two')
329329
reply.send({ hello: 'world' })
@@ -352,15 +352,15 @@ test('subpath', async (t) => {
352352
})
353353

354354
test('subpath retain stage', async (t) => {
355-
t.plan(7)
355+
t.plan(6)
356356

357357
const app = fastify()
358358
app.get('/dev/test', async (request, reply) => {
359359
t.equal(request.headers['x-my-header'], 'wuuusaaa')
360360
t.equal(request.headers['x-apigateway-event'], '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Fdev%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%2C%22requestContext%22%3A%7B%22resourcePath%22%3A%22%2Ftest%22%2C%22stage%22%3A%22dev%22%7D%7D')
361361
t.equal(request.headers['user-agent'], 'lightMyRequest')
362362
t.equal(request.headers.host, 'localhost:80')
363-
t.equal(request.headers['content-length'], '0')
363+
// t.equal(request.headers['content-length'], '0')
364364
reply.header('Set-Cookie', 'qwerty=one')
365365
reply.header('Set-Cookie', 'qwerty=two')
366366
reply.send({ hello: 'world' })
@@ -382,15 +382,15 @@ test('subpath retain stage', async (t) => {
382382
})
383383

384384
test('serializeLambdaArguments = false', async (t) => {
385-
t.plan(14)
385+
t.plan(13)
386386

387387
const app = fastify()
388388
app.get('/test', async (request, reply) => {
389389
t.equal(request.headers['x-my-header'], 'wuuusaaa')
390390
t.equal(request.headers['x-apigateway-event'], undefined)
391391
t.equal(request.headers['user-agent'], 'lightMyRequest')
392392
t.equal(request.headers.host, 'localhost:80')
393-
t.equal(request.headers['content-length'], '0')
393+
// t.equal(request.headers['content-length'], '0')
394394
reply.header('Set-Cookie', 'qwerty=one')
395395
reply.header('Set-Cookie', 'qwerty=two')
396396
reply.send({ hello: 'world' })
@@ -419,7 +419,7 @@ test('serializeLambdaArguments = false', async (t) => {
419419
})
420420

421421
test('with existing onRequest hook', async (t) => {
422-
t.plan(16)
422+
t.plan(15)
423423

424424
const app = fastify()
425425
const evt = {
@@ -441,7 +441,7 @@ test('with existing onRequest hook', async (t) => {
441441
t.equal(request.awsLambda.event, evt)
442442
t.equal(request.headers['user-agent'], 'lightMyRequest')
443443
t.equal(request.headers.host, 'localhost:80')
444-
t.equal(request.headers['content-length'], '0')
444+
// t.equal(request.headers['content-length'], '0')
445445
reply.header('Set-Cookie', 'qwerty=one')
446446
reply.header('Set-Cookie', 'qwerty=two')
447447
reply.send({ hello: 'world' })
@@ -461,7 +461,7 @@ test('with existing onRequest hook', async (t) => {
461461
})
462462

463463
test('proxy in pathParameters with http api', async (t) => {
464-
t.plan(13)
464+
t.plan(12)
465465

466466
const app = fastify()
467467
const evt = {
@@ -489,7 +489,7 @@ test('proxy in pathParameters with http api', async (t) => {
489489
t.equal(request.headers['x-my-header'], 'wuuusaaa')
490490
t.equal(request.headers['user-agent'], 'lightMyRequest')
491491
t.equal(request.headers.host, 'localhost:80')
492-
t.equal(request.headers['content-length'], '0')
492+
// t.equal(request.headers['content-length'], '0')
493493
t.equal(request.query.t, '1698604776681')
494494
reply.send({ hello: 'world' })
495495
})
@@ -506,7 +506,7 @@ test('proxy in pathParameters with http api', async (t) => {
506506
})
507507

508508
test('proxy in pathParameters with http api (pathParameterUsedAsPath = "proxy")', async (t) => {
509-
t.plan(13)
509+
t.plan(12)
510510

511511
const app = fastify()
512512
const evt = {
@@ -534,7 +534,7 @@ test('proxy in pathParameters with http api (pathParameterUsedAsPath = "proxy")'
534534
t.equal(request.headers['x-my-header'], 'wuuusaaa')
535535
t.equal(request.headers['user-agent'], 'lightMyRequest')
536536
t.equal(request.headers.host, 'localhost:80')
537-
t.equal(request.headers['content-length'], '0')
537+
// t.equal(request.headers['content-length'], '0')
538538
t.equal(request.query.t, '1698604776681')
539539
reply.send({ hello: 'world' })
540540
})
@@ -551,7 +551,7 @@ test('proxy in pathParameters with http api (pathParameterUsedAsPath = "proxy")'
551551
})
552552

553553
test('proxy in pathParameters with rest api', async (t) => {
554-
t.plan(13)
554+
t.plan(12)
555555

556556
const app = fastify()
557557
const evt = {
@@ -590,7 +590,7 @@ test('proxy in pathParameters with rest api', async (t) => {
590590
t.equal(request.headers['x-my-header'], 'wuuusaaa')
591591
t.equal(request.headers['user-agent'], 'lightMyRequest')
592592
t.equal(request.headers.host, 'localhost:80')
593-
t.equal(request.headers['content-length'], '0')
593+
// t.equal(request.headers['content-length'], '0')
594594
t.equal(request.query.t, '1698604776681')
595595
reply.send({ hello: 'world' })
596596
})
@@ -607,7 +607,7 @@ test('proxy in pathParameters with rest api', async (t) => {
607607
})
608608

609609
test('proxy in pathParameters with rest api (pathParameterUsedAsPath = "proxy")', async (t) => {
610-
t.plan(13)
610+
t.plan(12)
611611

612612
const app = fastify()
613613
const evt = {
@@ -646,7 +646,7 @@ test('proxy in pathParameters with rest api (pathParameterUsedAsPath = "proxy")'
646646
t.equal(request.headers['x-my-header'], 'wuuusaaa')
647647
t.equal(request.headers['user-agent'], 'lightMyRequest')
648648
t.equal(request.headers.host, 'localhost:80')
649-
t.equal(request.headers['content-length'], '0')
649+
// t.equal(request.headers['content-length'], '0')
650650
t.equal(request.query.t, '1698604776681')
651651
reply.send({ hello: 'world' })
652652
})

0 commit comments

Comments
 (0)