GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: restore res.req for Node.js when createProxyHandler is set (#213) · fastify/fastify-express@6d3e9a2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • index.js
  • test
    • enhance-request.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎index.js‎

Copy file name to clipboardExpand all lines: index.js
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ function fastifyExpress (fastify, options, next) {
6161
function enhanceRequest (req, reply, next) {
6262
// Allow attaching custom Proxy handlers to Express req
6363
if (typeof createProxyHandler === 'function') {
64-
req.raw = new Proxy(req.raw, createProxyHandler(req))
64+
const rawRequest = req.raw
65+
req.raw = new Proxy(rawRequest, createProxyHandler(req))
66+
// Express sets `res.req` to the Proxy, but Node.js expects the request
67+
// it created there when the response finishes
68+
reply.raw.prependOnceListener('finish', () => {
69+
reply.raw.req = rawRequest
70+
})
6571
}
6672

6773
const { url } = req.raw
Collapse file

‎test/enhance-request.test.js‎

Copy file name to clipboardExpand all lines: test/enhance-request.test.js
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,47 @@ test('createProxyHandler has access to Fastify request object', async t => {
189189
t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
190190
t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
191191
})
192+
193+
test('createProxyHandler keeps the original request on the response', async t => {
194+
t.plan(5)
195+
196+
const fastify = Fastify()
197+
t.after(() => fastify.close())
198+
199+
let rawRequest
200+
fastify.register(expressPlugin, {
201+
createProxyHandler: fastifyReq => {
202+
rawRequest = fastifyReq.raw
203+
return {}
204+
}
205+
})
206+
.after(() => {
207+
fastify.use(function (req, res, next) {
208+
// Express still works with the Proxy while it handles the request
209+
t.assert.notStrictEqual(req, rawRequest)
210+
t.assert.strictEqual(res.req, req)
211+
next()
212+
})
213+
})
214+
215+
// Node.js reads `res.req` when the response finishes and expects the
216+
// request it created there, not the Proxy that Express assigned
217+
const finished = new Promise(resolve => {
218+
fastify.addHook('onResponse', function (_request, reply, done) {
219+
resolve(reply.raw.req)
220+
done()
221+
})
222+
})
223+
224+
fastify.get('/', function (_request, reply) {
225+
reply.send({ hello: 'world' })
226+
})
227+
228+
const address = await fastify.listen({ port: 0 })
229+
230+
const response = await fetch(address)
231+
232+
t.assert.deepStrictEqual(response.status, 200)
233+
t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
234+
t.assert.strictEqual(await finished, rawRequest)
235+
})

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.