← Back to Pull requests

fix: return injected payload when express handles the request #212

Open
unknown opened this pull request with 369 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 18, 2026 03:48 UTC
Author

Fixes #54

fastify.inject() returns an empty payload for any request that goes through the express stack. That covers express routes and routers, and also plain Fastify routes once any fastify.use() middleware is registered. The status code and headers are correct, but the body is ''.

Root cause: runConnect in index.js hands req.raw/reply.raw to express, and express's app.handle (express/lib/application.js) does Object.setPrototypeOf(res, this.response). For an injected request, reply.raw is a light-my-request Response. Its prototype overrides write, end, writeHead and a few more methods to collect the payload. Once the prototype is swapped, those overrides are lost and the plain http.ServerResponse methods write to the mock socket instead. The existing tests that use inject only check status codes, so this never showed up.

What changed: when req.raw.isLightMyRequest is set (a documented light-my-request marker), runConnect copies the own methods of the req/res prototype onto the instance before calling express. Express still swaps the prototype, but the instance properties shadow it, so light-my-request's methods keep working. Express's own methods (res.json, res.send, req.get, ...) are not affected because light-my-request doesn't define them. Requests from a real server never get this flag, so the normal HTTP path doesn't change.

Tests: added test/inject.test.js. It covers an express app route with res.json and a status/header, a router with res.send and res.end, a POST body parsed by express.json(), and a Fastify route (JSON and stream) behind an express middleware. All 5 fail without the change to index.js and pass with it. npm test passes: lint, 78 unit tests with 100% coverage, and tstyche.

Repro script (fastify 5.12.8, light-my-request 6.8.0, express 5.3.0): express routes mounted with fastify.use(), then fastify.inject():

Before:

/json 200 application/json; charset=utf-8 ""
/send 201 text/plain; charset=utf-8 ""
/end 200 undefined ""
/app 200 application/json; charset=utf-8 ""
/body 200 application/json; charset=utf-8 ""
/f 200 ""          <- fastify route behind fastify.use((req, res, next) => next())

After:

/json 200 application/json; charset=utf-8 "{\"hello\":\"world\"}"
/send 201 text/plain; charset=utf-8 "text body"
/end 200 undefined "raw end"
/app 200 application/json; charset=utf-8 "{\"from\":\"app\"}"
/body 200 application/json; charset=utf-8 "{\"a\":1}"
/f 200 "{\"fastify\":true}"

Left out: I only copy methods from the immediate prototype of the injected req/res. That prototype is the one light-my-request builds, and it also covers its custom Request option, because light-my-request copies its methods onto that constructor's prototype. Methods that a user-supplied Request class inherits from further up its own chain are not preserved. I didn't add a README note because inject should now just work.

Diff: 369 lines (+276 / -93) across 4 files Reviewer: mcollina Review duration: 3848m
All Vouch review confidence checks passed
Residual risk 0.38 = Change Risk (0.66) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.66 · 369 lines diff across 4 files
0.66
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
42%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.354
Shallow Review Depth shallow_review_depth
0.344
Sensitive Paths sensitive_paths
0.066
Real PR Conversation Timeline
1 comments
"Review active on #212"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: medium confidence: 43.0% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 66.0% (HIGH) · Higher = larger blast radius
66.0%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 43.0% · Lower = rushed or shallow review
43.0%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 37.6% · Moderate risk
37.6%