@fastify/express 4.0.5 → 4.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -60,7 +60,14 @@ function fastifyExpress (fastify, options, next) {
60
60
  req.raw.ips = req.ips
61
61
  req.raw.log = req.log
62
62
  reply.raw.log = req.log
63
+
64
+ let replySent = false
63
65
  reply.raw.send = function send (...args) {
66
+ if (replySent) {
67
+ return
68
+ }
69
+
70
+ replySent = true
64
71
  // Restore req.raw.url to its original value https://github.com/fastify/fastify-express/issues/11
65
72
  req.raw.url = normalizedUrl
66
73
  return reply.send.apply(reply, args)
@@ -76,13 +83,19 @@ function fastifyExpress (fastify, options, next) {
76
83
  req.raw.cookies = req.cookies
77
84
  }
78
85
 
79
- // Make it lazy as it does a bit of work
86
+ // ** Request **
80
87
  Object.defineProperty(req.raw, 'protocol', {
81
88
  get () {
82
89
  return req.protocol
83
90
  }
84
91
  })
85
92
 
93
+ // ** Response **
94
+ Object.defineProperty(reply.raw, 'headersSent', {
95
+ get () { return replySent },
96
+ configurable: true
97
+ })
98
+
86
99
  next()
87
100
  }
88
101
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "4.0.5",
3
+ "version": "4.0.6",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -10,7 +10,7 @@
10
10
  "lint:fix": "eslint --fix",
11
11
  "test": "npm run test:unit && npm run test:typescript",
12
12
  "test:unit": "c8 --100 --reporter html node --test",
13
- "test:typescript": "tsd"
13
+ "test:typescript": "tstyche"
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
@@ -72,15 +72,12 @@
72
72
  "passport": "^0.7.0",
73
73
  "passport-http-bearer": "^1.0.1",
74
74
  "serve-static": "^2.2.0",
75
- "tsd": "^0.33.0"
75
+ "tstyche": "^7.0.0"
76
76
  },
77
77
  "dependencies": {
78
78
  "express": "^5.2.1",
79
79
  "fastify-plugin": "^5.0.0"
80
80
  },
81
- "tsd": {
82
- "directory": "test/types"
83
- },
84
81
  "publishConfig": {
85
82
  "access": "public"
86
83
  }
@@ -5,6 +5,72 @@ const Fastify = require('fastify')
5
5
  const express = require('express')
6
6
  const expressPlugin = require('../index')
7
7
 
8
+ test('express error middlewares chained via next(err) must not crash the process when the first middleware already sent a response', async t => {
9
+ t.plan(2)
10
+ const fastify = Fastify()
11
+ t.after(() => fastify.close())
12
+
13
+ await fastify.register(expressPlugin)
14
+
15
+ const app = express()
16
+
17
+ app.get('/foo', (_req, _res, next) => {
18
+ next(new Error('route error'))
19
+ })
20
+
21
+ // First error middleware: send a response, then pass the error along.
22
+ app.use((err, _req, res, next) => {
23
+ res.status(500).json({ error: err.message })
24
+ next(err)
25
+ })
26
+
27
+ // Second error middleware: should skip sending because headersSent is true.
28
+ app.use((_err, _req, res, _next) => {
29
+ if (!res.headersSent) {
30
+ res.status(500).json({ error: 'fallback' })
31
+ }
32
+ })
33
+
34
+ fastify.use(app)
35
+
36
+ const address = await fastify.listen({ port: 0 })
37
+ const result = await fetch(`${address}/foo`)
38
+ t.assert.deepStrictEqual(result.status, 500)
39
+ t.assert.deepStrictEqual(await result.json(), { error: 'route error' })
40
+ })
41
+
42
+ test('express error middleware that sends unconditionally must not crash the process after the first send', async t => {
43
+ t.plan(2)
44
+ const fastify = Fastify()
45
+ t.after(() => fastify.close())
46
+
47
+ await fastify.register(expressPlugin)
48
+
49
+ const app = express()
50
+
51
+ app.get('/bar', (_req, _res, next) => {
52
+ next(new Error('route error'))
53
+ })
54
+
55
+ // First error middleware sends and forwards the error.
56
+ app.use((err, _req, res, next) => {
57
+ res.status(500).json({ error: err.message })
58
+ next(err)
59
+ })
60
+
61
+ // Second error middleware sends unconditionally — exercises the replySent guard.
62
+ app.use((_err, _req, res, _next) => {
63
+ res.status(500).json({ error: 'should be blocked by replySent guard' })
64
+ })
65
+
66
+ fastify.use(app)
67
+
68
+ const address = await fastify.listen({ port: 0 })
69
+ const result = await fetch(`${address}/bar`)
70
+ t.assert.deepStrictEqual(result.status, 500)
71
+ t.assert.deepStrictEqual(await result.json(), { error: 'route error' })
72
+ })
73
+
8
74
  test('onSend hook should receive valid request and reply objects if middleware fails', async t => {
9
75
  t.plan(3)
10
76
  const fastify = Fastify()
@@ -1,6 +1,6 @@
1
1
  import Fastify, { FastifyRequest } from 'fastify'
2
2
  import fastifyExpress from '..'
3
- import { expectType } from 'tsd'
3
+ import { expect } from 'tstyche'
4
4
  import { Application } from 'express'
5
5
 
6
6
  const app = Fastify()
@@ -10,13 +10,13 @@ app.register(fastifyExpress, {
10
10
  expressHook: 'onRequest',
11
11
  createProxyHandler: (fastifyReq) => ({
12
12
  set (target, prop, value) {
13
- expectType<FastifyRequest>(fastifyReq)
13
+ expect(fastifyReq).type.toBe<FastifyRequest>()
14
14
  return Reflect.set(target, prop, value)
15
15
  }
16
16
  })
17
17
  })
18
18
 
19
- expectType<Application>(app.express)
19
+ expect(app.express).type.toBe<Application>()
20
20
 
21
21
  app.express.disable('x-powered-by')
22
22