Skip to content

Instantly share code, notes, and snippets.

@paolochiodi
Created October 24, 2022 14:25
Show Gist options
  • Save paolochiodi/7176009653df985ddcd33e51fecb4959 to your computer and use it in GitHub Desktop.
Save paolochiodi/7176009653df985ddcd33e51fecb4959 to your computer and use it in GitHub Desktop.
fastify-raw-body bodyLimit reduced test case

To replicate the issue:

  1. Install dependencies with npm install
  2. Run the server with node server.js
  3. Run the client with node client.js

We expect the request to complete successfully as the body reques is within the limit set on the specific route. You will see that the request does not return an error, it just hangs up.

Replacing BODY_KO with BODY_OK in client.js and re-lunching it will show that normal requests do work.

const { randomBytes } = require('crypto')
const fetch = require('node-fetch')
const BODY_OK = 1 * 1024
const BODY_KO = 2 * 1024 * 1024
async function run () {
console.log('before fetch')
const body = randomBytes(BODY_KO).toString('hex')
const res = await fetch(
`http://localhost:3000`, {
method: 'POST',
body
}
)
console.log('fetch response', res.ok)
}
run()
.then(() => console.log('done'))
.catch(err => console.error(err))
{
"name": "replicate-raw-body",
"version": "1.0.0",
"dependencies": {
"fastify": "^4.9.2",
"fastify-raw-body": "^4.1.0",
"node-fetch": "^2.6.7"
}
}
const Fastify = require('fastify')
async function start () {
const app = Fastify({
logger: true
})
await app.register(import('fastify-raw-body'), {
field: 'rawBody',
global: false,
runFirst: false,
})
app.post('/', {
config: {
rawBody: true,
},
bodyLimit: 1048576 * 50, // increase limit to 50 mb
handler (req, reply) {
reply.send('ok')
}
})
app.listen({ port: 3000 })
}
start().then(() => console.log('started'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment