@fastify/express 4.0.1 → 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.
@@ -2,160 +2,152 @@
2
2
 
3
3
  // Original Fastify test/middlewares.test.js file
4
4
 
5
- const t = require('tap')
6
- const test = t.test
7
- const sget = require('simple-get').concat
5
+ const { test } = require('node:test')
8
6
  const fastify = require('fastify')
9
7
  const fp = require('fastify-plugin')
10
8
  const cors = require('cors')
9
+ const express = require('express')
11
10
  const helmet = require('helmet')
12
11
 
13
12
  const expressPlugin = require('../index')
14
13
 
15
- test('use a middleware', t => {
16
- t.plan(7)
14
+ test('use a middleware', async t => {
15
+ t.plan(5)
17
16
 
18
17
  const instance = fastify()
18
+ t.after(() => instance.close())
19
19
  instance.register(expressPlugin)
20
20
  .after(() => {
21
- const useRes = instance.use(function (req, res, next) {
22
- t.pass('middleware called')
21
+ const useRes = instance.use(function (_req, _res, next) {
22
+ t.assert.ok('middleware called')
23
23
  next()
24
24
  })
25
25
 
26
- t.equal(useRes, instance)
26
+ t.assert.deepStrictEqual(useRes, instance)
27
27
  })
28
28
 
29
- instance.get('/', function (request, reply) {
29
+ instance.get('/', function (_request, reply) {
30
30
  reply.send({ hello: 'world' })
31
31
  })
32
32
 
33
- instance.listen({ port: 0 }, err => {
34
- t.error(err)
33
+ const address = await instance.listen({ port: 0 })
35
34
 
36
- t.teardown(instance.server.close.bind(instance.server))
35
+ const result = await fetch(address)
37
36
 
38
- sget({
39
- method: 'GET',
40
- url: 'http://localhost:' + instance.server.address().port
41
- }, (err, response, body) => {
42
- t.error(err)
43
- t.equal(response.statusCode, 200)
44
- t.equal(response.headers['content-length'], '' + body.length)
45
- t.same(JSON.parse(body), { hello: 'world' })
46
- })
47
- })
37
+ const responseText = await result.text()
38
+ t.assert.deepStrictEqual(result.status, 200)
39
+ t.assert.deepStrictEqual(result.headers.get('content-length'), '' + responseText.length)
40
+ t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
48
41
  })
49
42
 
50
- test('use cors', t => {
51
- t.plan(3)
43
+ test('use cors', async t => {
44
+ t.plan(1)
52
45
 
53
46
  const instance = fastify()
47
+ t.after(() => instance.close())
54
48
  instance.register(expressPlugin)
55
49
  .after(() => {
56
50
  instance.use(cors())
57
51
  })
58
52
 
59
- instance.get('/', function (request, reply) {
53
+ instance.get('/', function (_request, reply) {
60
54
  reply.send({ hello: 'world' })
61
55
  })
62
56
 
63
- instance.listen({ port: 0 }, err => {
64
- t.error(err)
65
-
66
- t.teardown(instance.server.close.bind(instance.server))
57
+ const address = await instance.listen({ port: 0 })
67
58
 
68
- sget({
69
- method: 'GET',
70
- url: 'http://localhost:' + instance.server.address().port
71
- }, (err, response, body) => {
72
- t.error(err)
73
- t.equal(response.headers['access-control-allow-origin'], '*')
74
- })
75
- })
59
+ const result = await fetch(address)
60
+ t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
76
61
  })
77
62
 
78
- test('use helmet', t => {
79
- t.plan(3)
63
+ test('use helmet', async t => {
64
+ t.plan(1)
80
65
 
81
66
  const instance = fastify()
67
+ t.after(() => instance.close())
82
68
  instance.register(expressPlugin)
83
69
  .after(() => {
84
70
  instance.use(helmet())
85
71
  })
86
72
 
87
- instance.get('/', function (request, reply) {
73
+ instance.get('/', function (_request, reply) {
88
74
  reply.send({ hello: 'world' })
89
75
  })
90
76
 
91
- instance.listen({ port: 0 }, err => {
92
- t.error(err)
93
-
94
- t.teardown(instance.server.close.bind(instance.server))
77
+ const address = await instance.listen({ port: 0 })
95
78
 
96
- sget({
97
- method: 'GET',
98
- url: 'http://localhost:' + instance.server.address().port
99
- }, (err, response, body) => {
100
- t.error(err)
101
- t.ok(response.headers['x-xss-protection'])
102
- })
103
- })
79
+ const result = await fetch(address)
80
+ t.assert.ok(result.headers.get('x-xss-protection'))
104
81
  })
105
82
 
106
- test('use helmet and cors', t => {
107
- t.plan(4)
83
+ test('use helmet and cors', async t => {
84
+ t.plan(2)
108
85
 
109
86
  const instance = fastify()
87
+ t.after(() => instance.close())
110
88
  instance.register(expressPlugin)
111
89
  .after(() => {
112
90
  instance.use(cors())
113
91
  instance.use(helmet())
114
92
  })
115
93
 
116
- instance.get('/', function (request, reply) {
94
+ instance.get('/', function (_request, reply) {
117
95
  reply.send({ hello: 'world' })
118
96
  })
119
97
 
120
- instance.listen({ port: 0 }, err => {
121
- t.error(err)
98
+ const address = await instance.listen({ port: 0 })
99
+
100
+ const result = await fetch(address)
101
+
102
+ t.assert.ok(result.headers.get('x-xss-protection'))
103
+ t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
104
+ })
122
105
 
123
- t.teardown(instance.server.close.bind(instance.server))
106
+ test('use cors only on prefix', async t => {
107
+ t.plan(1)
124
108
 
125
- sget({
126
- method: 'GET',
127
- url: 'http://localhost:' + instance.server.address().port
128
- }, (err, response, body) => {
129
- t.error(err)
130
- t.ok(response.headers['x-xss-protection'])
131
- t.equal(response.headers['access-control-allow-origin'], '*')
109
+ const instance = fastify()
110
+ t.after(() => instance.close())
111
+ instance.register((innerInstance) => {
112
+ innerInstance.register(expressPlugin).after(() => {
113
+ innerInstance.use('/', cors())
132
114
  })
133
- })
115
+ innerInstance.get('/', function (_request, reply) {
116
+ reply.send({ hello: 'world' })
117
+ })
118
+ }, { prefix: '/prefix' })
119
+
120
+ const address = await instance.listen({ port: 0 })
121
+
122
+ const result = await fetch(address + '/prefix')
123
+
124
+ t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
134
125
  })
135
126
 
136
- test('middlewares with prefix', t => {
137
- t.plan(5)
127
+ test('middlewares with prefix', async t => {
128
+ t.plan(4)
138
129
 
139
130
  const instance = fastify()
131
+ t.after(() => instance.close())
140
132
  instance.register(expressPlugin)
141
133
  .after(() => {
142
- instance.use(function (req, res, next) {
134
+ instance.use(function (req, _res, next) {
143
135
  req.global = true
144
136
  next()
145
137
  })
146
- instance.use('', function (req, res, next) {
138
+ instance.use('', function (req, _res, next) {
147
139
  req.global2 = true
148
140
  next()
149
141
  })
150
- instance.use('/', function (req, res, next) {
142
+ instance.use('/', function (req, _res, next) {
151
143
  req.root = true
152
144
  next()
153
145
  })
154
- instance.use('/prefix', function (req, res, next) {
146
+ instance.use('/prefix', function (req, _res, next) {
155
147
  req.prefixed = true
156
148
  next()
157
149
  })
158
- instance.use('/prefix/', function (req, res, next) {
150
+ instance.use('/prefix/', function (req, _res, next) {
159
151
  req.slashed = true
160
152
  next()
161
153
  })
@@ -176,199 +168,167 @@ test('middlewares with prefix', t => {
176
168
  instance.get('/prefix/', handler)
177
169
  instance.get('/prefix/inner', handler)
178
170
 
179
- instance.listen({ port: 0 }, err => {
180
- t.error(err)
181
- t.teardown(instance.server.close.bind(instance.server))
182
-
183
- t.test('/', t => {
184
- t.plan(2)
185
- sget({
186
- method: 'GET',
187
- url: 'http://localhost:' + instance.server.address().port + '/',
188
- json: true
189
- }, (err, response, body) => {
190
- t.error(err)
191
- t.same(body, {
192
- global: true,
193
- global2: true,
194
- root: true
195
- })
196
- })
171
+ const address = await instance.listen({ port: 0 })
172
+
173
+ await t.test('/', async t => {
174
+ t.plan(1)
175
+
176
+ const result = await fetch(address + '/')
177
+ t.assert.deepStrictEqual(await result.json(), {
178
+ global: true,
179
+ global2: true,
180
+ root: true
197
181
  })
182
+ })
198
183
 
199
- t.test('/prefix', t => {
200
- t.plan(2)
201
- sget({
202
- method: 'GET',
203
- url: 'http://localhost:' + instance.server.address().port + '/prefix',
204
- json: true
205
- }, (err, response, body) => {
206
- t.error(err)
207
- t.same(body, {
208
- prefixed: true,
209
- global: true,
210
- global2: true,
211
- root: true,
212
- slashed: true
213
- })
214
- })
184
+ await t.test('/prefix', async t => {
185
+ t.plan(1)
186
+
187
+ const result = await fetch(address + '/prefix')
188
+ t.assert.deepStrictEqual(await result.json(), {
189
+ prefixed: true,
190
+ global: true,
191
+ global2: true,
192
+ root: true,
193
+ slashed: true
215
194
  })
195
+ })
216
196
 
217
- t.test('/prefix/', t => {
218
- t.plan(2)
219
- sget({
220
- method: 'GET',
221
- url: 'http://localhost:' + instance.server.address().port + '/prefix/',
222
- json: true
223
- }, (err, response, body) => {
224
- t.error(err)
225
- t.same(body, {
226
- prefixed: true,
227
- slashed: true,
228
- global: true,
229
- global2: true,
230
- root: true
231
- })
232
- })
197
+ await t.test('/prefix/', async t => {
198
+ t.plan(1)
199
+
200
+ const result = await fetch(address + '/prefix/')
201
+ t.assert.deepStrictEqual(await result.json(), {
202
+ prefixed: true,
203
+ slashed: true,
204
+ global: true,
205
+ global2: true,
206
+ root: true
233
207
  })
208
+ })
234
209
 
235
- t.test('/prefix/inner', t => {
236
- t.plan(2)
237
- sget({
238
- method: 'GET',
239
- url: 'http://localhost:' + instance.server.address().port + '/prefix/inner',
240
- json: true
241
- }, (err, response, body) => {
242
- t.error(err)
243
- t.same(body, {
244
- prefixed: true,
245
- slashed: true,
246
- global: true,
247
- global2: true,
248
- root: true
249
- })
250
- })
210
+ await t.test('/prefix/inner', async t => {
211
+ t.plan(1)
212
+
213
+ const result = await fetch(address + '/prefix/inner')
214
+ t.assert.deepStrictEqual(await result.json(), {
215
+ prefixed: true,
216
+ slashed: true,
217
+ global: true,
218
+ global2: true,
219
+ root: true
251
220
  })
252
221
  })
253
222
  })
254
223
 
255
- test('res.end should block middleware execution', t => {
256
- t.plan(6)
224
+ test('res.end should block middleware execution', async t => {
225
+ t.plan(4)
257
226
 
258
227
  const instance = fastify()
259
- t.teardown(instance.close)
228
+ t.after(() => instance.close())
260
229
  instance.register(expressPlugin)
261
230
  .after(() => {
262
- instance.use(function (req, res, next) {
231
+ instance.use(function (_req, res) {
263
232
  res.send('hello')
264
233
  })
265
234
 
266
- instance.use(function (req, res, next) {
267
- t.fail('we should not be here')
235
+ instance.use(function () {
236
+ t.assert.fail('we should not be here')
268
237
  })
269
238
  })
270
239
 
271
- instance.addHook('onRequest', (req, res, next) => {
272
- t.ok('called')
240
+ instance.addHook('onRequest', (_req, _res, next) => {
241
+ t.assert.ok('called')
273
242
  next()
274
243
  })
275
244
 
276
- instance.addHook('preHandler', (req, reply, next) => {
277
- t.fail('this should not be called')
245
+ instance.addHook('preHandler', () => {
246
+ t.assert.fail('this should not be called')
278
247
  })
279
248
 
280
- instance.addHook('onSend', (req, reply, payload, next) => {
281
- t.ok('called')
249
+ instance.addHook('onSend', (_req, _reply, payload, next) => {
250
+ t.assert.ok('called')
282
251
  next(null, payload)
283
252
  })
284
253
 
285
- instance.addHook('onResponse', (request, reply, next) => {
286
- t.ok('called')
254
+ instance.addHook('onResponse', (_request, _reply, next) => {
255
+ t.assert.ok('called')
287
256
  next()
288
257
  })
289
258
 
290
- instance.get('/', function (request, reply) {
291
- t.fail('we should no be here')
259
+ instance.get('/', function () {
260
+ t.assert.fail('we should no be here')
292
261
  })
293
262
 
294
- instance.listen({ port: 0 }, (err, address) => {
295
- t.error(err)
296
- sget({
297
- method: 'GET',
298
- url: address
299
- }, (err, res, data) => {
300
- t.error(err)
301
- t.equal(res.statusCode, 200)
302
- t.equal(data.toString(), 'hello')
303
- })
304
- })
263
+ const address = await instance.listen({ port: 0 })
264
+
265
+ const result = await fetch(address)
266
+
267
+ t.assert.deepStrictEqual(result.status, 200)
268
+
269
+ t.assert.deepStrictEqual(await result.text(), 'hello')
305
270
  })
306
271
 
307
- test('Use a middleware inside a plugin after an encapsulated plugin', t => {
308
- t.plan(5)
272
+ test('Use a middleware inside a plugin after an encapsulated plugin', async t => {
273
+ t.plan(3)
309
274
  const f = fastify()
310
- t.teardown(f.close)
275
+ t.after(() => f.close())
311
276
  f.register(expressPlugin)
312
277
 
313
- f.register(function (instance, opts, next) {
314
- instance.use(function (req, res, next) {
315
- t.ok('first middleware called')
278
+ f.register(function (instance, _opts, next) {
279
+ instance.use(function (_req, _res, next) {
280
+ t.assert.ok('first middleware called')
316
281
  next()
317
282
  })
318
283
 
319
- instance.get('/', function (request, reply) {
284
+ instance.get('/', function (_request, reply) {
320
285
  reply.send({ hello: 'world' })
321
286
  })
322
287
 
323
288
  next()
324
289
  })
325
290
 
326
- f.register(fp(function (instance, opts, next) {
327
- instance.use(function (req, res, next) {
328
- t.ok('second middleware called')
291
+ f.register(fp(function (instance, _opts, next) {
292
+ instance.use(function (_req, _res, next) {
293
+ t.assert.ok('second middleware called')
329
294
  next()
330
295
  })
331
296
 
332
297
  next()
333
298
  }))
334
299
 
335
- f.listen({ port: 0 }, (err, address) => {
336
- t.error(err)
337
- sget({
338
- method: 'GET',
339
- url: address
340
- }, (err, res, data) => {
341
- t.error(err)
342
- t.equal(res.statusCode, 200)
343
- t.same(JSON.parse(data), { hello: 'world' })
344
- })
345
- })
300
+ const address = await f.listen({ port: 0 })
301
+
302
+ const result = await fetch(address)
303
+ t.assert.deepStrictEqual(result.status, 200)
304
+
305
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
346
306
  })
347
307
 
348
- test('middlewares should run in the order in which they are defined', t => {
349
- t.plan(10)
308
+ test('middlewares should run in the order in which they are defined', async t => {
309
+ t.plan(8)
350
310
  const f = fastify()
351
- t.teardown(f.close)
311
+ t.after(() => f.close())
352
312
  f.register(expressPlugin)
353
313
 
354
- f.register(fp(function (instance, opts, next) {
355
- instance.use(function (req, res, next) {
356
- t.equal(req.previous, undefined)
314
+ f.register(fp(function (instance, _opts, next) {
315
+ instance.use(function (req, _res, next) {
316
+ t.assert.deepStrictEqual(req.previous, undefined)
357
317
  req.previous = 1
358
318
  next()
359
319
  })
360
320
 
361
- instance.register(fp(function (i, opts, next) {
362
- i.use(function (req, res, next) {
363
- t.equal(req.previous, 2)
321
+ instance.register(fp(function (i, _opts, next) {
322
+ i.use(function (req, _res, next) {
323
+ t.assert.deepStrictEqual(req.previous, 2)
364
324
  req.previous = 3
365
325
  next()
366
326
  })
367
327
  next()
368
328
  }))
369
329
 
370
- instance.use(function (req, res, next) {
371
- t.equal(req.previous, 1)
330
+ instance.use(function (req, _res, next) {
331
+ t.assert.deepStrictEqual(req.previous, 1)
372
332
  req.previous = 2
373
333
  next()
374
334
  })
@@ -376,21 +336,21 @@ test('middlewares should run in the order in which they are defined', t => {
376
336
  next()
377
337
  }))
378
338
 
379
- f.register(function (instance, opts, next) {
380
- instance.use(function (req, res, next) {
381
- t.equal(req.previous, 3)
339
+ f.register(function (instance, _opts, next) {
340
+ instance.use(function (req, _res, next) {
341
+ t.assert.deepStrictEqual(req.previous, 3)
382
342
  req.previous = 4
383
343
  next()
384
344
  })
385
345
 
386
346
  instance.get('/', function (request, reply) {
387
- t.equal(request.raw.previous, 5)
347
+ t.assert.deepStrictEqual(request.raw.previous, 5)
388
348
  reply.send({ hello: 'world' })
389
349
  })
390
350
 
391
- instance.register(fp(function (i, opts, next) {
392
- i.use(function (req, res, next) {
393
- t.equal(req.previous, 4)
351
+ instance.register(fp(function (i, _opts, next) {
352
+ i.use(function (req, _res, next) {
353
+ t.assert.deepStrictEqual(req.previous, 4)
394
354
  req.previous = 5
395
355
  next()
396
356
  })
@@ -400,15 +360,100 @@ test('middlewares should run in the order in which they are defined', t => {
400
360
  next()
401
361
  })
402
362
 
403
- f.listen({ port: 0 }, (err, address) => {
404
- t.error(err)
405
- sget({
406
- method: 'GET',
407
- url: address
408
- }, (err, res, data) => {
409
- t.error(err)
410
- t.equal(res.statusCode, 200)
411
- t.same(JSON.parse(data), { hello: 'world' })
363
+ const address = await f.listen({ port: 0 })
364
+
365
+ const result = await fetch(address)
366
+ t.assert.deepStrictEqual(result.status, 200)
367
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
368
+ })
369
+
370
+ test('middlewares for encoded paths', async t => {
371
+ await t.test('decode the request url and run the middleware', async (t) => {
372
+ await checkEncodedPath('/encoded', '/%65ncoded', t)
373
+ })
374
+
375
+ await t.test('does not double decode the url', async (t) => {
376
+ await checkEncodedPath('/%65ncoded', '/%2565ncoded', t)
377
+ })
378
+
379
+ await t.test('handle the decoding for express handlers', async (t) => {
380
+ t.plan(6)
381
+
382
+ const routeUrl = '/express'
383
+ const requestUrl = '/%65xpress'
384
+
385
+ const instance = fastify()
386
+ t.after(() => instance.close())
387
+
388
+ instance.addHook('onSend', async function hook (request, reply, payload) {
389
+ t.assert.deepStrictEqual(request.raw.url, routeUrl)
390
+ t.assert.deepStrictEqual(request.raw.originalUrl, requestUrl)
391
+ return payload
412
392
  })
393
+
394
+ instance.addHook('onResponse', async function hook (request, reply) {
395
+ t.assert.deepStrictEqual(request.raw.url, routeUrl)
396
+ t.assert.deepStrictEqual(request.raw.originalUrl, requestUrl)
397
+ })
398
+
399
+ await instance.register(expressPlugin)
400
+
401
+ // Register the express-like middleware
402
+ instance.use(routeUrl, function (req, _res, next) {
403
+ req.slashedByExpress = true
404
+ next()
405
+ })
406
+
407
+ // Register an express Router with an express handler
408
+ const innerRouter = express.Router()
409
+ innerRouter.get(routeUrl, function (req, res) {
410
+ res.send({ slashedByExpress: req.slashedByExpress })
411
+ })
412
+ instance.use(innerRouter)
413
+
414
+ const address = await instance.listen({ port: 0 })
415
+
416
+ const response = await fetch(address + requestUrl)
417
+ const body = await response.json()
418
+ t.assert.ok(response.ok)
419
+ t.assert.deepStrictEqual(body, { slashedByExpress: true })
413
420
  })
414
421
  })
422
+
423
+ async function checkEncodedPath (routeUrl, requestUrl, t) {
424
+ t.plan(6)
425
+
426
+ const instance = fastify()
427
+ t.after(() => instance.close())
428
+
429
+ instance.addHook('onSend', async function hook (request, reply, payload) {
430
+ t.assert.deepStrictEqual(request.raw.url, routeUrl)
431
+ t.assert.deepStrictEqual(request.raw.originalUrl, requestUrl)
432
+ return payload
433
+ })
434
+
435
+ instance.addHook('onResponse', async function hook (request, reply) {
436
+ t.assert.deepStrictEqual(request.raw.url, routeUrl)
437
+ t.assert.deepStrictEqual(request.raw.originalUrl, requestUrl)
438
+ })
439
+
440
+ await instance.register(expressPlugin)
441
+
442
+ // Register the express-like middleware
443
+ instance.use(routeUrl, function (req, _res, next) {
444
+ req.slashed = true
445
+ next()
446
+ })
447
+
448
+ // ... with a Fastify route handler
449
+ instance.get(routeUrl, (request, reply) => {
450
+ reply.send({ slashed: request.raw.slashed, })
451
+ })
452
+
453
+ const address = await instance.listen({ port: 0 })
454
+
455
+ const response = await fetch(address + requestUrl)
456
+ const body = await response.json()
457
+ t.assert.ok(response.ok)
458
+ t.assert.deepStrictEqual(body, { slashed: true })
459
+ }
package/types/index.d.ts CHANGED
@@ -18,7 +18,7 @@ declare module 'fastify' {
18
18
  type FastifyExpress = FastifyPluginCallback<fastifyExpress.FastifyExpressOptions>
19
19
 
20
20
  declare namespace fastifyExpress {
21
-
21
+
22
22
  export interface FastifyExpressOptions {
23
23
  expressHook?: string;
24
24
  createProxyHandler?: (fastifyReq: FastifyRequest) => ProxyHandler<Request>
@@ -28,5 +28,5 @@ declare namespace fastifyExpress {
28
28
  export { fastifyExpress as default }
29
29
  }
30
30
 
31
- declare function fastifyExpress(...params: Parameters<FastifyExpress>): ReturnType<FastifyExpress>
31
+ declare function fastifyExpress (...params: Parameters<FastifyExpress>): ReturnType<FastifyExpress>
32
32
  export = fastifyExpress