@fastify/express 4.0.2 → 4.0.3

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.
@@ -9,5 +9,5 @@ updates:
9
9
  - package-ecosystem: "npm"
10
10
  directory: "/"
11
11
  schedule:
12
- interval: "weekly"
12
+ interval: "monthly"
13
13
  open-pull-requests-limit: 10
@@ -4,7 +4,6 @@ on:
4
4
  push:
5
5
  branches:
6
6
  - main
7
- - master
8
7
  - next
9
8
  - 'v*'
10
9
  paths-ignore:
@@ -15,9 +14,20 @@ on:
15
14
  - 'docs/**'
16
15
  - '*.md'
17
16
 
17
+ # This allows a subsequently queued workflow run to interrupt previous runs
18
+ concurrency:
19
+ group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
20
+ cancel-in-progress: true
21
+
22
+ permissions:
23
+ contents: read
24
+
18
25
  jobs:
19
26
  test:
27
+ permissions:
28
+ contents: write
29
+ pull-requests: write
20
30
  uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
21
31
  with:
22
- lint: true
23
32
  license-check: true
33
+ lint: true
package/LICENSE CHANGED
@@ -1,6 +1,8 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 Fastify
3
+ Copyright (c) 2019-present The Fastify team
4
+
5
+ The Fastify team members are listed at https://github.com/fastify/fastify#team.
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
8
  of this software and associated documentation files (the "Software"), to deal
@@ -19,3 +21,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
21
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
22
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
23
  SOFTWARE.
24
+
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @fastify/express
2
2
 
3
- [![CI](https://github.com/fastify/fastify-express/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-express/actions/workflows/ci.yml)
3
+ [![CI](https://github.com/fastify/fastify-express/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-express/actions/workflows/ci.yml)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/express.svg?style=flat)](https://www.npmjs.com/package/@fastify/express)
5
5
  [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
6
6
 
@@ -26,8 +26,8 @@ npm i @fastify/express
26
26
  ### Compatibility
27
27
  | Plugin version | Fastify version |
28
28
  | ---------------|-----------------|
29
- | `^4.x` | `^5.x` |
30
- | `^2.x` | `^4.x` |
29
+ | `>=4.x` | `^5.x` |
30
+ | `>=2.x <4.x` | `^4.x` |
31
31
  | `^1.x` | `^3.x` |
32
32
  | `^0.x` | `^2.x` |
33
33
 
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const fp = require('fastify-plugin')
4
4
  const Express = require('express')
5
+ const FindMyWay = require('find-my-way')
5
6
  const kMiddlewares = Symbol('fastify-express-middlewares')
6
7
 
7
8
  function fastifyExpress (fastify, options, next) {
@@ -41,6 +42,11 @@ function fastifyExpress (fastify, options, next) {
41
42
  }
42
43
 
43
44
  const { url } = req.raw
45
+
46
+ const decodedUrl = FindMyWay.sanitizeUrlPath(url)
47
+ // Decode URL before Express matches middleware to prevent encoded path bypass
48
+ // e.g., /%61dmin should match middleware registered on /admin
49
+ req.raw.url = decodedUrl
44
50
  req.raw.originalUrl = url
45
51
  req.raw.id = req.id
46
52
  req.raw.hostname = req.hostname
@@ -50,7 +56,7 @@ function fastifyExpress (fastify, options, next) {
50
56
  reply.raw.log = req.log
51
57
  reply.raw.send = function send (...args) {
52
58
  // Restore req.raw.url to its original value https://github.com/fastify/fastify-express/issues/11
53
- req.raw.url = url
59
+ req.raw.url = decodedUrl
54
60
  return reply.send.apply(reply, args)
55
61
  }
56
62
 
@@ -59,6 +65,7 @@ function fastifyExpress (fastify, options, next) {
59
65
  req.raw.body = req.body
60
66
  }
61
67
  // backward compatibility for cookie-parser
68
+ /* c8 ignore next 3 */
62
69
  if (req.cookies) {
63
70
  req.raw.cookies = req.cookies
64
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -9,7 +9,7 @@
9
9
  "lint": "eslint",
10
10
  "lint:fix": "eslint --fix",
11
11
  "test": "npm run test:unit && npm run test:typescript",
12
- "test:unit": "tap",
12
+ "test:unit": "c8 --100 --reporter html node --test",
13
13
  "test:typescript": "tsd"
14
14
  },
15
15
  "repository": {
@@ -63,6 +63,7 @@
63
63
  "@types/express": "^5.0.0",
64
64
  "@types/node": "*",
65
65
  "body-parser": "^1.20.2",
66
+ "c8": "^10.1.3",
66
67
  "cors": "^2.8.5",
67
68
  "eslint": "^9.17.0",
68
69
  "fastify": "^5.0.0",
@@ -70,22 +71,18 @@
70
71
  "neostandard": "^0.12.0",
71
72
  "passport": "^0.7.0",
72
73
  "passport-http-bearer": "^1.0.1",
73
- "serve-static": "^1.15.0",
74
- "simple-get": "^4.0.1",
75
- "tap": "^21.0.0",
76
- "tsd": "^0.31.0"
74
+ "serve-static": "^2.2.0",
75
+ "tsd": "^0.33.0"
77
76
  },
78
77
  "dependencies": {
79
78
  "express": "^4.18.3",
80
- "fastify-plugin": "^5.0.0"
79
+ "fastify-plugin": "^5.0.0",
80
+ "find-my-way": "^9.4.0"
81
81
  },
82
82
  "tsd": {
83
83
  "directory": "test/types"
84
84
  },
85
85
  "publishConfig": {
86
86
  "access": "public"
87
- },
88
- "tap": {
89
- "disable-coverage": true
90
87
  }
91
88
  }
package/test/404s.test.js CHANGED
@@ -1,13 +1,12 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const fp = require('fastify-plugin')
5
5
  const Fastify = require('fastify')
6
- const sget = require('simple-get').concat
7
6
  const expressPlugin = require('../index')
8
7
 
9
- test('run hooks and middleware on default 404', t => {
10
- t.plan(8)
8
+ test('run hooks and middleware on default 404', async t => {
9
+ t.plan(6)
11
10
 
12
11
  const fastify = Fastify()
13
12
 
@@ -15,28 +14,28 @@ test('run hooks and middleware on default 404', t => {
15
14
  .register(expressPlugin)
16
15
  .after(() => {
17
16
  fastify.use(function (_req, _res, next) {
18
- t.pass('middleware called')
17
+ t.assert.ok('middleware called')
19
18
  next()
20
19
  })
21
20
  })
22
21
 
23
22
  fastify.addHook('onRequest', function (_req, _res, next) {
24
- t.pass('onRequest called')
23
+ t.assert.ok('onRequest called')
25
24
  next()
26
25
  })
27
26
 
28
27
  fastify.addHook('preHandler', function (_request, _reply, next) {
29
- t.pass('preHandler called')
28
+ t.assert.ok('preHandler called')
30
29
  next()
31
30
  })
32
31
 
33
32
  fastify.addHook('onSend', function (_request, _reply, _payload, next) {
34
- t.pass('onSend called')
33
+ t.assert.ok('onSend called')
35
34
  next()
36
35
  })
37
36
 
38
37
  fastify.addHook('onResponse', function (_request, _reply, next) {
39
- t.pass('onResponse called')
38
+ t.assert.ok('onResponse called')
40
39
  next()
41
40
  })
42
41
 
@@ -44,53 +43,48 @@ test('run hooks and middleware on default 404', t => {
44
43
  reply.send({ hello: 'world' })
45
44
  })
46
45
 
47
- t.teardown(fastify.close.bind(fastify))
46
+ t.after(() => fastify.close())
48
47
 
49
- fastify.listen({ port: 0 }, err => {
50
- t.error(err)
48
+ const address = await fastify.listen({ port: 0 })
51
49
 
52
- sget({
53
- method: 'PUT',
54
- url: 'http://localhost:' + fastify.server.address().port,
55
- body: JSON.stringify({ hello: 'world' }),
56
- headers: { 'Content-Type': 'application/json' }
57
- }, (err, response) => {
58
- t.error(err)
59
- t.equal(response.statusCode, 404)
60
- })
50
+ const result = await fetch(address, {
51
+ method: 'PUT',
52
+ body: JSON.stringify({ hello: 'world' }),
61
53
  })
54
+
55
+ t.assert.deepStrictEqual(result.status, 404)
62
56
  })
63
57
 
64
- test('run non-encapsulated plugin hooks and middleware on default 404', t => {
65
- t.plan(8)
58
+ test('run non-encapsulated plugin hooks and middleware on default 404', async t => {
59
+ t.plan(6)
66
60
 
67
61
  const fastify = Fastify()
68
- t.teardown(fastify.close)
62
+ t.after(() => fastify.close())
69
63
  fastify.register(expressPlugin)
70
64
 
71
65
  fastify.register(fp(function (instance, _options, next) {
72
66
  instance.addHook('onRequest', function (_req, _res, next) {
73
- t.pass('onRequest called')
67
+ t.assert.ok('onRequest called')
74
68
  next()
75
69
  })
76
70
 
77
71
  instance.use(function (_req, _res, next) {
78
- t.pass('middleware called')
72
+ t.assert.ok('middleware called')
79
73
  next()
80
74
  })
81
75
 
82
76
  instance.addHook('preHandler', function (_request, _reply, next) {
83
- t.pass('preHandler called')
77
+ t.assert.ok('preHandler called')
84
78
  next()
85
79
  })
86
80
 
87
81
  instance.addHook('onSend', function (_request, _reply, _payload, next) {
88
- t.pass('onSend called')
82
+ t.assert.ok('onSend called')
89
83
  next()
90
84
  })
91
85
 
92
86
  instance.addHook('onResponse', function (_request, _reply, next) {
93
- t.pass('onResponse called')
87
+ t.assert.ok('onResponse called')
94
88
  next()
95
89
  })
96
90
 
@@ -101,50 +95,45 @@ test('run non-encapsulated plugin hooks and middleware on default 404', t => {
101
95
  reply.send({ hello: 'world' })
102
96
  })
103
97
 
104
- fastify.listen({ port: 0 }, (err, address) => {
105
- t.error(err)
106
- sget({
107
- method: 'POST',
108
- url: address,
109
- body: JSON.stringify({ hello: 'world' }),
110
- headers: { 'Content-Type': 'application/json' }
111
- }, (err, response) => {
112
- t.error(err)
113
- t.equal(response.statusCode, 404)
114
- })
98
+ const address = await fastify.listen({ port: 0 })
99
+
100
+ const result = await fetch(address, {
101
+ method: 'POST',
102
+ body: JSON.stringify({ hello: 'world' }),
115
103
  })
104
+ t.assert.deepStrictEqual(result.status, 404)
116
105
  })
117
106
 
118
- test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
119
- t.plan(14)
107
+ test('run non-encapsulated plugin hooks and middleware on custom 404', async t => {
108
+ t.plan(12)
120
109
 
121
110
  const fastify = Fastify()
122
- t.teardown(fastify.close)
111
+ t.after(() => fastify.close())
123
112
  fastify.register(expressPlugin)
124
113
 
125
114
  const plugin = fp((instance, _opts, next) => {
126
115
  instance.addHook('onRequest', function (_req, _res, next) {
127
- t.pass('onRequest called')
116
+ t.assert.ok('onRequest called')
128
117
  next()
129
118
  })
130
119
 
131
120
  instance.use(function (_req, _res, next) {
132
- t.pass('middleware called')
121
+ t.assert.ok('middleware called')
133
122
  next()
134
123
  })
135
124
 
136
125
  instance.addHook('preHandler', function (_request, _reply, next) {
137
- t.pass('preHandler called')
126
+ t.assert.ok('preHandler called')
138
127
  next()
139
128
  })
140
129
 
141
130
  instance.addHook('onSend', function (_request, _reply, _payload, next) {
142
- t.pass('onSend called')
131
+ t.assert.ok('onSend called')
143
132
  next()
144
133
  })
145
134
 
146
135
  instance.addHook('onResponse', function (_request, _reply, next) {
147
- t.pass('onResponse called')
136
+ t.assert.ok('onResponse called')
148
137
  next()
149
138
  })
150
139
 
@@ -163,21 +152,16 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
163
152
 
164
153
  fastify.register(plugin) // Registering plugin after handler also works
165
154
 
166
- fastify.listen({ port: 0 }, (err, address) => {
167
- t.error(err)
168
- sget({
169
- method: 'GET',
170
- url: address + '/not-found'
171
- }, (err, response, body) => {
172
- t.error(err)
173
- t.equal(body.toString(), 'this was not found')
174
- t.equal(response.statusCode, 404)
175
- })
176
- })
155
+ const address = await fastify.listen({ port: 0 })
156
+
157
+ const result = await fetch(address + '/not-found')
158
+
159
+ t.assert.deepStrictEqual(await result.text(), 'this was not found')
160
+ t.assert.deepStrictEqual(result.status, 404)
177
161
  })
178
162
 
179
- test('run hooks and middleware with encapsulated 404', t => {
180
- t.plan(13)
163
+ test('run hooks and middleware with encapsulated 404', async t => {
164
+ t.plan(11)
181
165
 
182
166
  const fastify = Fastify()
183
167
 
@@ -185,28 +169,28 @@ test('run hooks and middleware with encapsulated 404', t => {
185
169
  .register(expressPlugin)
186
170
  .after(() => {
187
171
  fastify.use(function (_req, _res, next) {
188
- t.pass('middleware called')
172
+ t.assert.ok('middleware called')
189
173
  next()
190
174
  })
191
175
  })
192
176
 
193
177
  fastify.addHook('onRequest', function (_req, _res, next) {
194
- t.pass('onRequest called')
178
+ t.assert.ok('onRequest called')
195
179
  next()
196
180
  })
197
181
 
198
182
  fastify.addHook('preHandler', function (_request, _reply, next) {
199
- t.pass('preHandler called')
183
+ t.assert.ok('preHandler called')
200
184
  next()
201
185
  })
202
186
 
203
187
  fastify.addHook('onSend', function (_request, _reply, _payload, next) {
204
- t.pass('onSend called')
188
+ t.assert.ok('onSend called')
205
189
  next()
206
190
  })
207
191
 
208
192
  fastify.addHook('onResponse', function (_request, _reply, next) {
209
- t.pass('onResponse called')
193
+ t.assert.ok('onResponse called')
210
194
  next()
211
195
  })
212
196
 
@@ -216,59 +200,54 @@ test('run hooks and middleware with encapsulated 404', t => {
216
200
  })
217
201
 
218
202
  f.addHook('onRequest', function (_req, _res, next) {
219
- t.pass('onRequest 2 called')
203
+ t.assert.ok('onRequest 2 called')
220
204
  next()
221
205
  })
222
206
 
223
207
  f.use(function (_req, _res, next) {
224
- t.pass('middleware 2 called')
208
+ t.assert.ok('middleware 2 called')
225
209
  next()
226
210
  })
227
211
 
228
212
  f.addHook('preHandler', function (_request, _reply, next) {
229
- t.pass('preHandler 2 called')
213
+ t.assert.ok('preHandler 2 called')
230
214
  next()
231
215
  })
232
216
 
233
217
  f.addHook('onSend', function (_request, _reply, _payload, next) {
234
- t.pass('onSend 2 called')
218
+ t.assert.ok('onSend 2 called')
235
219
  next()
236
220
  })
237
221
 
238
222
  f.addHook('onResponse', function (_request, _reply, next) {
239
- t.pass('onResponse 2 called')
223
+ t.assert.ok('onResponse 2 called')
240
224
  next()
241
225
  })
242
226
 
243
227
  next()
244
228
  }, { prefix: '/test' })
245
229
 
246
- t.teardown(fastify.close.bind(fastify))
230
+ t.after(() => fastify.close())
247
231
 
248
- fastify.listen({ port: 0 }, err => {
249
- t.error(err)
232
+ const address = await fastify.listen({ port: 0 })
250
233
 
251
- sget({
252
- method: 'PUT',
253
- url: 'http://localhost:' + fastify.server.address().port + '/test',
254
- body: JSON.stringify({ hello: 'world' }),
255
- headers: { 'Content-Type': 'application/json' }
256
- }, (err, response) => {
257
- t.error(err)
258
- t.equal(response.statusCode, 404)
259
- })
234
+ const result = await fetch(address + '/test', {
235
+ method: 'PUT',
236
+ body: JSON.stringify({ hello: 'world' }),
260
237
  })
238
+
239
+ t.assert.deepStrictEqual(result.status, 404)
261
240
  })
262
241
 
263
- test('run middlewares on default 404', t => {
264
- t.plan(4)
242
+ test('run middlewares on default 404', async t => {
243
+ t.plan(2)
265
244
 
266
245
  const fastify = Fastify()
267
246
  fastify
268
247
  .register(expressPlugin)
269
248
  .after(() => {
270
249
  fastify.use(function (_req, _res, next) {
271
- t.pass('middleware called')
250
+ t.assert.ok('middleware called')
272
251
  next()
273
252
  })
274
253
  })
@@ -277,32 +256,27 @@ test('run middlewares on default 404', t => {
277
256
  reply.send({ hello: 'world' })
278
257
  })
279
258
 
280
- t.teardown(fastify.close.bind(fastify))
259
+ t.after(() => fastify.close())
281
260
 
282
- fastify.listen({ port: 0 }, err => {
283
- t.error(err)
261
+ const address = await fastify.listen({ port: 0 })
284
262
 
285
- sget({
286
- method: 'PUT',
287
- url: 'http://localhost:' + fastify.server.address().port,
288
- body: JSON.stringify({ hello: 'world' }),
289
- headers: { 'Content-Type': 'application/json' }
290
- }, (err, response) => {
291
- t.error(err)
292
- t.equal(response.statusCode, 404)
293
- })
263
+ const result = await fetch(address, {
264
+ method: 'PUT',
265
+ body: JSON.stringify({ hello: 'world' }),
294
266
  })
267
+
268
+ t.assert.deepStrictEqual(result.status, 404)
295
269
  })
296
270
 
297
- test('run middlewares with encapsulated 404', t => {
298
- t.plan(5)
271
+ test('run middlewares with encapsulated 404', async t => {
272
+ t.plan(3)
299
273
 
300
274
  const fastify = Fastify()
301
275
  fastify
302
276
  .register(expressPlugin)
303
277
  .after(() => {
304
278
  fastify.use(function (_req, _res, next) {
305
- t.pass('middleware called')
279
+ t.assert.ok('middleware called')
306
280
  next()
307
281
  })
308
282
  })
@@ -313,26 +287,21 @@ test('run middlewares with encapsulated 404', t => {
313
287
  })
314
288
 
315
289
  f.use(function (_req, _res, next) {
316
- t.pass('middleware 2 called')
290
+ t.assert.ok('middleware 2 called')
317
291
  next()
318
292
  })
319
293
 
320
294
  next()
321
295
  }, { prefix: '/test' })
322
296
 
323
- t.teardown(fastify.close.bind(fastify))
297
+ t.after(() => fastify.close())
324
298
 
325
- fastify.listen({ port: 0 }, err => {
326
- t.error(err)
299
+ const address = await fastify.listen({ port: 0 })
327
300
 
328
- sget({
329
- method: 'PUT',
330
- url: 'http://localhost:' + fastify.server.address().port + '/test',
331
- body: JSON.stringify({ hello: 'world' }),
332
- headers: { 'Content-Type': 'application/json' }
333
- }, (err, response) => {
334
- t.error(err)
335
- t.equal(response.statusCode, 404)
336
- })
301
+ const result = await fetch(address + '/test', {
302
+ method: 'PUT',
303
+ body: JSON.stringify({ hello: 'world' }),
337
304
  })
305
+
306
+ t.assert.deepStrictEqual(result.status, 404)
338
307
  })