Skip to content

Commit 4d438db

Browse files
fix(fastify): use Fastify content-type parsers instead of raw stream reading
Fastify's default body parser consumes the request stream before route handlers run. The old code tried to read req.raw manually via collectBody/ collectRawBody, but the stream was already consumed — causing the Promise to never resolve and the validation to hang indefinitely. Fix: - Register content-type parsers for text/plain, application/octet-stream, and a catch-all so req.body is always populated - Use req.body directly in POST /baseline11 and /upload handlers - Remove unused collectBody/collectRawBody helpers - Bump bodyLimit to 50MB for upload test (default 1MB is too small)
1 parent 744656c commit 4d438db

1 file changed

Lines changed: 10 additions & 24 deletions

File tree

frameworks/fastify/server.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ function startWorker() {
7474
loadDatabase();
7575

7676
const Fastify = require('fastify');
77-
const app = Fastify({ logger: false });
77+
const app = Fastify({ logger: false, bodyLimit: 50 * 1024 * 1024 });
78+
79+
// Register raw body parsers so req.body is available without manual stream reading
80+
app.addContentTypeParser('text/plain', { parseAs: 'string' }, (req, body, done) => done(null, body));
81+
app.addContentTypeParser('application/octet-stream', { parseAs: 'buffer' }, (req, body, done) => done(null, body));
82+
app.addContentTypeParser('*', { parseAs: 'buffer' }, (req, body, done) => done(null, body));
7883

7984
// --- /pipeline ---
8085
app.get('/pipeline', (req, reply) => {
@@ -87,10 +92,9 @@ function startWorker() {
8792
reply.header('server', SERVER_NAME).type('text/plain').send(String(s));
8893
});
8994

90-
app.post('/baseline11', async (req, reply) => {
95+
app.post('/baseline11', (req, reply) => {
9196
const querySum = sumQuery(req.query);
92-
// Fastify parses body based on content-type; for raw/text we collect manually
93-
const body = await collectBody(req.raw);
97+
const body = typeof req.body === 'string' ? req.body : (req.body ? req.body.toString() : '');
9498
let total = querySum;
9599
const n = parseInt(body.trim(), 10);
96100
if (n === n) total += n;
@@ -163,29 +167,11 @@ function startWorker() {
163167
});
164168

165169
// --- /upload ---
166-
app.post('/upload', async (req, reply) => {
167-
const body = await collectRawBody(req.raw);
170+
app.post('/upload', (req, reply) => {
171+
const body = Buffer.isBuffer(req.body) ? req.body : Buffer.from(req.body || '');
168172
reply.header('server', SERVER_NAME).type('text/plain').send(String(body.length));
169173
});
170174

171-
// Helper: collect raw body as string
172-
function collectBody(raw) {
173-
return new Promise((resolve) => {
174-
let body = '';
175-
raw.on('data', chunk => body += chunk);
176-
raw.on('end', () => resolve(body));
177-
});
178-
}
179-
180-
// Helper: collect raw body as buffer
181-
function collectRawBody(raw) {
182-
return new Promise((resolve) => {
183-
const chunks = [];
184-
raw.on('data', chunk => chunks.push(chunk));
185-
raw.on('end', () => resolve(Buffer.concat(chunks)));
186-
});
187-
}
188-
189175
// Start HTTP/1.1 server
190176
app.listen({ port: 8080, host: '0.0.0.0' }).then(() => {
191177
// Also start HTTP/2 server on 8443

0 commit comments

Comments
 (0)