@fastify/express 4.0.10 → 4.0.11

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/README.md CHANGED
@@ -128,6 +128,8 @@ me@computer ~ % curl -X PATCH -H 'content-type:application/json' -d '{"foo2":"ba
128
128
 
129
129
  The encapsulation works as usual with Fastify, you can register the plugin in a subsystem and your express code will work only inside there, or you can declare the express plugin top level and register a middleware in a nested plugin, and the middleware will be executed only for the nested routes of the specific plugin.
130
130
 
131
+ As with Fastify hooks, a middleware also runs for the routes of the plugins already registered in the instance where `use()` is called, for example after `await fastify.register(plugin)`.
132
+
131
133
  *Register the plugin in its own subsystem:*
132
134
  ```js
133
135
  const fastify = require('fastify')()
package/index.js CHANGED
@@ -6,6 +6,7 @@ const parseUrl = require('parseurl')
6
6
  const { parse, pathToRegexp } = require('path-to-regexp')
7
7
  const kMiddlewares = Symbol('fastify-express-middlewares')
8
8
  const kDynamicMountMatchers = Symbol('fastify-express-dynamic-mount-matchers')
9
+ const kChildren = Symbol('fastify-express-children')
9
10
 
10
11
  function fastifyExpress (fastify, options, next) {
11
12
  const {
@@ -21,6 +22,7 @@ function fastifyExpress (fastify, options, next) {
21
22
  fastify.decorate('use', use)
22
23
  fastify[kMiddlewares] = []
23
24
  fastify[kDynamicMountMatchers] = []
25
+ fastify[kChildren] = []
24
26
  fastify.decorate('express', Express())
25
27
  fastify.express.disable('x-powered-by')
26
28
 
@@ -48,14 +50,23 @@ function fastifyExpress (fastify, options, next) {
48
50
  // match case-insensitively or it would never match an uppercase spelling.
49
51
  path = Array.isArray(path) ? path.map(foldRegExp) : foldRegExp(path)
50
52
  }
51
- this[kMiddlewares].push([path, fn])
52
- this[kDynamicMountMatchers].push(...compileDynamicMountMatchers(path))
53
+ addMiddleware(this, path, fn)
54
+ return this
55
+ }
56
+
57
+ // Like a Fastify hook, a middleware also runs for the plugins that were
58
+ // already registered in the instance, so it is added to them as well.
59
+ function addMiddleware (instance, path, fn) {
60
+ instance[kMiddlewares].push([path, fn])
61
+ instance[kDynamicMountMatchers].push(...compileDynamicMountMatchers(path))
53
62
  if (fn == null) {
54
- this.express.use(path)
63
+ instance.express.use(path)
55
64
  } else {
56
- this.express.use(path, fn)
65
+ instance.express.use(path, fn)
66
+ }
67
+ for (const child of instance[kChildren]) {
68
+ addMiddleware(child, path, fn)
57
69
  }
58
- return this
59
70
  }
60
71
 
61
72
  function enhanceRequest (req, reply, next) {
@@ -173,6 +184,10 @@ function fastifyExpress (fastify, options, next) {
173
184
  const dynamicMountMatchers = instance[kDynamicMountMatchers].slice()
174
185
  instance[kMiddlewares] = middlewares.slice()
175
186
  instance[kDynamicMountMatchers] = dynamicMountMatchers.slice()
187
+ // Until it is replaced, the list is the one of the instance the plugin is
188
+ // registered in, as is the list of middlewares.
189
+ instance[kChildren].push(instance)
190
+ instance[kChildren] = []
176
191
  instance.decorate('express', Express())
177
192
  instance.express.disable('x-powered-by')
178
193
  instance.decorate('use', use)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "4.0.10",
3
+ "version": "4.0.11",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -575,7 +575,7 @@ test('res.end should block middleware execution', async t => {
575
575
  })
576
576
 
577
577
  test('Use a middleware inside a plugin after an encapsulated plugin', async t => {
578
- t.plan(3)
578
+ t.plan(4)
579
579
  const f = fastify()
580
580
  t.after(() => f.close())
581
581
  f.register(expressPlugin)
@@ -0,0 +1,108 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('node:test')
4
+ const Fastify = require('fastify')
5
+
6
+ const expressPlugin = require('../index')
7
+
8
+ function recorder () {
9
+ const calls = []
10
+ return {
11
+ calls,
12
+ record (name) {
13
+ return function (req, res, next) {
14
+ calls.push(name)
15
+ next()
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+ test('use() after a plugin has loaded runs for the routes of that plugin', async t => {
22
+ const fastify = Fastify()
23
+ t.after(() => fastify.close())
24
+ const { calls, record } = recorder()
25
+
26
+ await fastify.register(expressPlugin)
27
+ await fastify.register(async function (child) {
28
+ child.get('/before', async () => 'before')
29
+ })
30
+ fastify.use(record('late'))
31
+ await fastify.register(async function (child) {
32
+ child.get('/after', async () => 'after')
33
+ })
34
+
35
+ for (const url of ['/before', '/after']) {
36
+ calls.length = 0
37
+ const response = await fastify.inject(url)
38
+ t.assert.strictEqual(response.statusCode, 200, url)
39
+ t.assert.deepStrictEqual(calls, ['late'], url)
40
+ }
41
+ })
42
+
43
+ test('middleware added after a plugin has loaded runs after its own, as Fastify hooks do', async t => {
44
+ const fastify = Fastify()
45
+ t.after(() => fastify.close())
46
+ const { calls, record } = recorder()
47
+ const hooks = []
48
+
49
+ await fastify.register(expressPlugin)
50
+ fastify.use(record('parent before'))
51
+ fastify.addHook('onRequest', async () => { hooks.push('parent before') })
52
+ await fastify.register(async function (child) {
53
+ child.use(record('child'))
54
+ child.addHook('onRequest', async () => { hooks.push('child') })
55
+ child.get('/', async () => 'ok')
56
+ })
57
+ fastify.use(record('parent after'))
58
+ fastify.addHook('onRequest', async () => { hooks.push('parent after') })
59
+
60
+ const response = await fastify.inject('/')
61
+ t.assert.strictEqual(response.statusCode, 200)
62
+ t.assert.deepStrictEqual(calls, ['parent before', 'child', 'parent after'])
63
+ t.assert.deepStrictEqual(calls, hooks)
64
+ })
65
+
66
+ test('middleware reaches nested plugins without their prefix being added again', async t => {
67
+ const fastify = Fastify()
68
+ t.after(() => fastify.close())
69
+ const { calls, record } = recorder()
70
+
71
+ await fastify.register(expressPlugin)
72
+ await fastify.register(async function (api) {
73
+ await api.register(async function (v1) {
74
+ v1.get('/users', async () => 'users')
75
+ }, { prefix: '/v1' })
76
+ api.use('/v1/users', record('api'))
77
+ }, { prefix: '/api' })
78
+ fastify.use('/api/v1', record('root'))
79
+
80
+ const response = await fastify.inject('/api/v1/users')
81
+ t.assert.strictEqual(response.statusCode, 200)
82
+ t.assert.deepStrictEqual(calls, ['api', 'root'])
83
+ })
84
+
85
+ test('middleware added after a plugin has loaded stays encapsulated', async t => {
86
+ const fastify = Fastify()
87
+ t.after(() => fastify.close())
88
+ const { calls, record } = recorder()
89
+
90
+ await fastify.register(expressPlugin)
91
+ await fastify.register(async function (a) {
92
+ await a.register(async function (child) {
93
+ child.get('/a', async () => 'a')
94
+ })
95
+ a.use(record('a'))
96
+ })
97
+ await fastify.register(async function (b) {
98
+ b.get('/b', async () => 'b')
99
+ })
100
+ fastify.get('/root', async () => 'root')
101
+
102
+ for (const [url, expected] of [['/a', ['a']], ['/b', []], ['/root', []]]) {
103
+ calls.length = 0
104
+ const response = await fastify.inject(url)
105
+ t.assert.strictEqual(response.statusCode, 200, url)
106
+ t.assert.deepStrictEqual(calls, expected, url)
107
+ }
108
+ })