fastify 3.25.0 → 3.26.0

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.
Files changed (51) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +25 -25
  3. package/docs/Guides/Ecosystem.md +5 -0
  4. package/docs/Guides/Fluent-Schema.md +2 -2
  5. package/docs/Guides/Index.md +2 -0
  6. package/docs/Guides/Prototype-Poisoning.md +391 -0
  7. package/docs/Guides/Recommendations.md +1 -1
  8. package/docs/Guides/Serverless.md +3 -3
  9. package/docs/Guides/Style-Guide.md +2 -2
  10. package/docs/Guides/Testing.md +1 -1
  11. package/docs/Reference/ContentTypeParser.md +1 -1
  12. package/docs/Reference/Decorators.md +3 -3
  13. package/docs/Reference/Errors.md +1 -1
  14. package/docs/Reference/Hooks.md +9 -1
  15. package/docs/Reference/Index.md +1 -1
  16. package/docs/Reference/Plugins.md +6 -6
  17. package/docs/Reference/Reply.md +2 -2
  18. package/docs/Reference/Request.md +3 -0
  19. package/docs/Reference/Routes.md +3 -3
  20. package/docs/Reference/Server.md +6 -6
  21. package/docs/Reference/TypeScript.md +7 -7
  22. package/docs/Reference/Validation-and-Serialization.md +1 -1
  23. package/docs/index.md +1 -1
  24. package/fastify.d.ts +1 -1
  25. package/fastify.js +6 -3
  26. package/lib/contentTypeParser.js +11 -6
  27. package/lib/decorate.js +1 -1
  28. package/lib/pluginUtils.js +5 -0
  29. package/lib/route.js +7 -0
  30. package/lib/symbols.js +1 -0
  31. package/lib/warnings.js +1 -1
  32. package/package.json +6 -6
  33. package/test/404s.test.js +1 -1
  34. package/test/als.test.js +74 -0
  35. package/test/bundler/esbuild/bundler-test.js +31 -0
  36. package/test/bundler/esbuild/package.json +10 -0
  37. package/test/bundler/esbuild/src/fail-plugin-version.js +12 -0
  38. package/test/bundler/esbuild/src/index.js +7 -0
  39. package/test/bundler/webpack/bundler-test.js +15 -4
  40. package/test/bundler/webpack/src/fail-plugin-version.js +1 -3
  41. package/test/bundler/webpack/src/index.js +1 -3
  42. package/test/custom-parser.test.js +30 -31
  43. package/test/decorator.test.js +20 -6
  44. package/test/http2/constraint.test.js +91 -0
  45. package/test/plugin.name.display.js +10 -0
  46. package/test/plugin.test.js +18 -0
  47. package/test/types/fastify.test-d.ts +16 -0
  48. package/test/types/hooks.test-d.ts +28 -4
  49. package/test/versioned-routes.test.js +27 -3
  50. package/types/hooks.d.ts +31 -20
  51. package/types/instance.d.ts +6 -1
@@ -44,7 +44,7 @@ This table of contents is in alphabetical order.
44
44
  standard set of errors Fastify generates.
45
45
  + [Hooks](./Hooks.md): Details the API by which Fastify plugins can inject
46
46
  themselves into Fastify's handling of the request lifecycle.
47
- + [HTTP2](/./HTTP2.md): Details Fastify's HTTP2 support.
47
+ + [HTTP2](./HTTP2.md): Details Fastify's HTTP2 support.
48
48
  + [Lifecycle](./Lifecycle.md): Explains the Fastify request lifecycle and
49
49
  illustrates where [Hooks](./Hooks.md) are available for integrating with it.
50
50
  + [Logging](./Logging.md): Details Fastify's included logging and how to
@@ -12,7 +12,7 @@ feature allows us to achieve plugin *encapsulation* and *inheritance*, in this
12
12
  way we create a *direct acyclic graph* (DAG) and we will not have issues caused
13
13
  by cross dependencies.
14
14
 
15
- You already see in the [getting started](../Guides/Getting-Started.md#register)
15
+ You already see in the [getting started](../Guides/Getting-Started.md#your-first-plugin)
16
16
  section how using this API is pretty straightforward.
17
17
  ```
18
18
  fastify.register(plugin, [options])
@@ -30,7 +30,7 @@ Fastify specific options is:
30
30
 
31
31
  + [`logLevel`](./Routes.md#custom-log-level)
32
32
  + [`logSerializers`](./Routes.md#custom-log-serializer)
33
- + [`prefix`](#route-prefixing-options)
33
+ + [`prefix`](#route-prefixing-option)
34
34
 
35
35
  **Note: Those options will be ignored when used with fastify-plugin**
36
36
 
@@ -181,7 +181,7 @@ callback.
181
181
  Example:
182
182
  ```js
183
183
  module.exports = function (fastify, opts, done) {
184
- fastify.decorate('utility', () => {})
184
+ fastify.decorate('utility', function () {})
185
185
 
186
186
  fastify.get('/', handler)
187
187
 
@@ -191,7 +191,7 @@ module.exports = function (fastify, opts, done) {
191
191
  You can also use `register` inside another `register`:
192
192
  ```js
193
193
  module.exports = function (fastify, opts, done) {
194
- fastify.decorate('utility', () => {})
194
+ fastify.decorate('utility', function () {})
195
195
 
196
196
  fastify.get('/', handler)
197
197
 
@@ -226,7 +226,7 @@ plugin will support.
226
226
  const fp = require('fastify-plugin')
227
227
 
228
228
  module.exports = fp(function (fastify, opts, done) {
229
- fastify.decorate('utility', () => {})
229
+ fastify.decorate('utility', function () {})
230
230
  done()
231
231
  }, '0.x')
232
232
  ```
@@ -239,7 +239,7 @@ changes it will be your responsibility to update the module, while if you use
239
239
  `fastify-plugin`, you can be sure about backward compatibility.
240
240
  ```js
241
241
  function yourPlugin (fastify, opts, done) {
242
- fastify.decorate('utility', () => {})
242
+ fastify.decorate('utility', function () {})
243
243
  done()
244
244
  }
245
245
  yourPlugin[Symbol.for('skip-override')] = true
@@ -67,7 +67,7 @@ object that exposes the following functions and properties:
67
67
  from Node core.
68
68
  - `.log` - The logger instance of the incoming request.
69
69
  - `.request` - The incoming request.
70
- - `.context` - Access the [Request's context](./Request.md#Request) property.
70
+ - `.context` - Access the [Request's context](./Request.md) property.
71
71
 
72
72
  ```js
73
73
  fastify.get('/', options, function (request, reply) {
@@ -510,7 +510,7 @@ fastify.setNotFoundHandler(function (request, reply) {
510
510
  <a id="payload-type"></a>
511
511
 
512
512
  The type of the sent payload (after serialization and going through any
513
- [`onSend` hooks](./Hooks.md#the-onsend-hook)) must be one of the following
513
+ [`onSend` hooks](./Hooks.md#onsend)) must be one of the following
514
514
  types, otherwise, an error will be thrown:
515
515
 
516
516
  - `string`
@@ -57,6 +57,9 @@ This operation will add to the request headers the new values that can be read
57
57
  calling `request.headers.bar`. Moreover, you can still access the standard
58
58
  request's headers with the `request.raw.headers` property.
59
59
 
60
+ > Note: For performance reason on `not found` route, you may see that we will add
61
+ an extra property `Symbol('fastify.RequestAcceptVersion')` on the headers.
62
+
60
63
  ```js
61
64
  fastify.post('/:params', options, function (request, reply) {
62
65
  console.log(request.body)
@@ -82,7 +82,7 @@ fastify.route(options)
82
82
  called. Note: using an arrow function will break the binding of `this`.
83
83
  * `errorHandler(error, request, reply)`: a custom error handler for the scope of
84
84
  the request. Overrides the default error global handler, and anything set by
85
- [`setErrorHandler`](./Server.md#setErrorHandler), for requests to the route.
85
+ [`setErrorHandler`](./Server.md#seterrorhandler), for requests to the route.
86
86
  To access the default handler, you can access `instance.errorHandler`. Note
87
87
  that this will point to fastify's default `errorHandler` only if a plugin
88
88
  hasn't overridden it already.
@@ -107,7 +107,7 @@ fastify.route(options)
107
107
  * `logSerializers`: set serializers to log for this route.
108
108
  * `config`: object used to store custom configuration.
109
109
  * `version`: a [semver](https://semver.org/) compatible string that defined the
110
- version of the endpoint. [Example](#version).
110
+ version of the endpoint. [Example](#version-constraints).
111
111
  * `prefixTrailingSlash`: string used to determine how to handle passing `/` as a
112
112
  route with a prefix.
113
113
  * `both` (default): Will register both `/prefix` and `/prefix/`.
@@ -437,7 +437,7 @@ the global Fastify Logger, accessible with `fastify.log`*
437
437
 
438
438
  In some context, you may need to log a large object but it could be a waste of
439
439
  resources for some routes. In this case, you can define some
440
- [`serializer`](https://github.com/pinojs/pino/blob/master/docs/api.md#bindingsserializers-object)
440
+ [`serializer`](https://github.com/pinojs/pino/blob/master/docs/api.md#serializers-object)
441
441
  and attach them in the right context!
442
442
 
443
443
  ```js
@@ -207,7 +207,7 @@ Defines the maximum payload, in bytes, the server is allowed to accept.
207
207
  Defines what action the framework must take when parsing a JSON object with
208
208
  `__proto__`. This functionality is provided by
209
209
  [secure-json-parse](https://github.com/fastify/secure-json-parse). See
210
- https://hueniverse.com/a-tale-of-prototype-poisoning-2610fa170061 for more
210
+ [Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more
211
211
  details about prototype poisoning attacks.
212
212
 
213
213
  Possible values are `'error'`, `'remove'` and `'ignore'`.
@@ -220,7 +220,7 @@ Possible values are `'error'`, `'remove'` and `'ignore'`.
220
220
  Defines what action the framework must take when parsing a JSON object with
221
221
  `constructor`. This functionality is provided by
222
222
  [secure-json-parse](https://github.com/fastify/secure-json-parse). See
223
- https://hueniverse.com/a-tale-of-prototype-poisoning-2610fa170061 for more
223
+ [Prototype Poisoning](../Guides/Prototype-Poisoning.md) for more
224
224
  details about prototype poisoning attacks.
225
225
 
226
226
  Possible values are `'error'`, `'remove'` and `'ignore'`.
@@ -1075,7 +1075,7 @@ used by plugins.
1075
1075
  #### inject
1076
1076
  <a id="inject"></a>
1077
1077
 
1078
- Fake HTTP injection (for testing purposes) [here](../Guides/Testing.md#inject).
1078
+ Fake HTTP injection (for testing purposes) [here](../Guides/Testing.md#benefits-of-using-fastifyinject).
1079
1079
 
1080
1080
  #### addSchema
1081
1081
  <a id="add-schema"></a>
@@ -1291,8 +1291,8 @@ handler so requests will go through the full [Fastify
1291
1291
  lifecycle](./Lifecycle.md#lifecycle).
1292
1292
 
1293
1293
  You can also register
1294
- [`preValidation`](https://www.fastify.io/docs/latest/Hooks/#route-hooks) and
1295
- [`preHandler`](https://www.fastify.io/docs/latest/Hooks/#route-hooks) hooks for
1294
+ [`preValidation`](./Hooks.md#route-hooks) and
1295
+ [`preHandler`](./Hooks.md#route-hooks) hooks for
1296
1296
  the 404 handler.
1297
1297
 
1298
1298
  _Note: The `preValidation` hook registered using this method will run for a
@@ -1402,7 +1402,7 @@ fastify.ready(() => {
1402
1402
  the `route.store` object for each displayed route. This can be an `array` of
1403
1403
  keys (e.g. `['onRequest', Symbol('key')]`), or `true` to display all properties.
1404
1404
  A shorthand option, `fastify.printRoutes({ includeHooks: true })` will include
1405
- all [hooks](https://www.fastify.io/docs/latest/Hooks/).
1405
+ all [hooks](./Hooks.md).
1406
1406
 
1407
1407
  ```js
1408
1408
  console.log(fastify.printRoutes({ includeHooks: true, includeMeta: ['metaProperty'] }))
@@ -51,9 +51,6 @@ in a blank http Fastify server.
51
51
  }
52
52
  ```
53
53
 
54
- *Note: Set `target` property in `tsconfig.json` to `es2017` or greater to avoid
55
- [FastifyDeprecation](https://github.com/fastify/fastify/issues/3284) warning.*
56
-
57
54
  3. Initialize a TypeScript configuration file:
58
55
  ```bash
59
56
  npx tsc --init
@@ -61,6 +58,9 @@ in a blank http Fastify server.
61
58
  or use one of the [recommended
62
59
  ones](https://github.com/tsconfig/bases#node-10-tsconfigjson).
63
60
 
61
+ *Note: Set `target` property in `tsconfig.json` to `es2017` or greater to avoid
62
+ [FastifyDeprecation](https://github.com/fastify/fastify/issues/3284) warning.*
63
+
64
64
  4. Create an `index.ts` file - this will contain the server code
65
65
  5. Add the following code block to your file:
66
66
  ```typescript
@@ -228,8 +228,8 @@ can do it as follows:
228
228
  },
229
229
  },
230
230
  },
231
- (req, rep) => {
232
- const { body: user } = req;
231
+ (request, reply) => {
232
+ const { body: user } = request;
233
233
  /* user has type
234
234
  * const user: StaticProperties<{
235
235
  * name: TString;
@@ -237,7 +237,7 @@ can do it as follows:
237
237
  * }>
238
238
  */
239
239
  //...
240
- rep.status(200).send(user);
240
+ reply.status(200).send(user);
241
241
  }
242
242
  );
243
243
  ```
@@ -1384,7 +1384,7 @@ body parsing happens before the `preHandler` hook.
1384
1384
 
1385
1385
  [src](https://github.com/fastify/fastify/blob/main/types/hooks.d.ts#L35)
1386
1386
 
1387
- preParsing` is the second hook to be executed in the request lifecycle. The
1387
+ `preParsing` is the second hook to be executed in the request lifecycle. The
1388
1388
  previous hook was `onRequest`, the next hook will be `preValidation`.
1389
1389
 
1390
1390
  Notice: in the `preParsing` hook, request.body will always be null, because the
@@ -713,7 +713,7 @@ fastify.setSchemaErrorFormatter(function (errors, dataVar) {
713
713
  ```
714
714
 
715
715
  You can also use
716
- [setErrorHandler](https://www.fastify.io/docs/latest/Server/#seterrorhandler) to
716
+ [setErrorHandler](./Server.md#seterrorhandler) to
717
717
  define a custom response for validation errors such as
718
718
 
719
719
  ```js
package/docs/index.md CHANGED
@@ -5,7 +5,7 @@ The documentation for Fastify is split into two categories:
5
5
  - [Reference documentation](./Reference/Index.md)
6
6
  - [Guides](./Guides/Index.md)
7
7
 
8
- The reference documenation utilizes a very formal style in an effort to document
8
+ The reference documentation utilizes a very formal style in an effort to document
9
9
  Fastify's API and implementation details thoroughly for the developer who needs
10
10
  such. The guides category utilizes an informal, educational, style as a means to
11
11
  introduce newcomers to core, and advanced, Fastify concepts.
package/fastify.d.ts CHANGED
@@ -144,7 +144,7 @@ export type FastifyServerOptions<
144
144
  return503OnClosing?: boolean,
145
145
  ajv?: {
146
146
  customOptions?: AjvOptions,
147
- plugins?: Function[]
147
+ plugins?: (Function | [Function, unknown])[]
148
148
  },
149
149
  frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface>(
150
150
  error: FastifyError,
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '3.25.0'
3
+ const VERSION = '3.26.0'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
@@ -16,6 +16,7 @@ const {
16
16
  kLogSerializers,
17
17
  kHooks,
18
18
  kSchemaController,
19
+ kRequestAcceptVersion,
19
20
  kReplySerializerDefault,
20
21
  kContentTypeParser,
21
22
  kReply,
@@ -520,10 +521,8 @@ function fastify (options) {
520
521
  }
521
522
 
522
523
  if (name === 'onClose') {
523
- this[kHooks].validate(name, fn)
524
524
  this.onClose(fn)
525
525
  } else if (name === 'onReady') {
526
- this[kHooks].validate(name, fn)
527
526
  this[kHooks].add(name, fn)
528
527
  } else {
529
528
  this.after((err, done) => {
@@ -578,6 +577,10 @@ function fastify (options) {
578
577
  // req and res are Node.js core objects
579
578
  function defaultRoute (req, res) {
580
579
  if (req.headers['accept-version'] !== undefined) {
580
+ // we remove the accept-version header for performance result
581
+ // because we do not want to go through the constraint checking
582
+ // the usage of symbol here to prevent any colision on custom header name
583
+ req.headers[kRequestAcceptVersion] = req.headers['accept-version']
581
584
  req.headers['accept-version'] = undefined
582
585
  }
583
586
  fourOhFour.router.lookup(req, res)
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { AsyncResource } = require('async_hooks')
3
4
  let lru = require('tiny-lru')
4
5
  // Needed to handle Webpack and faux modules
5
6
  // See https://github.com/fastify/fastify/issues/2356
@@ -137,6 +138,7 @@ ContentTypeParser.prototype.remove = function (contentType) {
137
138
 
138
139
  ContentTypeParser.prototype.run = function (contentType, handler, request, reply) {
139
140
  const parser = this.cache.get(contentType) || this.getParser(contentType)
141
+ const resource = new AsyncResource('content-type-parser:run', request)
140
142
 
141
143
  if (parser === undefined) {
142
144
  reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType))
@@ -163,12 +165,15 @@ ContentTypeParser.prototype.run = function (contentType, handler, request, reply
163
165
  }
164
166
 
165
167
  function done (error, body) {
166
- if (error) {
167
- reply.send(error)
168
- } else {
169
- request.body = body
170
- handler(request, reply)
171
- }
168
+ // We cannot use resource.bind() because it is broken in node v12.
169
+ resource.runInAsyncScope(() => {
170
+ if (error) {
171
+ reply.send(error)
172
+ } else {
173
+ request.body = body
174
+ handler(request, reply)
175
+ }
176
+ })
172
177
  }
173
178
  }
174
179
 
package/lib/decorate.js CHANGED
@@ -68,7 +68,7 @@ function decorateFastify (name, fn, dependencies) {
68
68
 
69
69
  function checkExistence (instance, name) {
70
70
  if (name) {
71
- return name in instance || hasKey(instance, name)
71
+ return name in instance || (instance.prototype && name in instance.prototype) || hasKey(instance, name)
72
72
  }
73
73
 
74
74
  return instance in this
@@ -14,6 +14,11 @@ function getMeta (fn) {
14
14
  }
15
15
 
16
16
  function getPluginName (func) {
17
+ const display = getDisplayName(func)
18
+ if (display) {
19
+ return display
20
+ }
21
+
17
22
  // let's see if this is a file, and in that case use that
18
23
  // this is common for plugins
19
24
  const cache = require.cache
package/lib/route.js CHANGED
@@ -8,6 +8,7 @@ const supportedMethods = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTI
8
8
  const { normalizeSchema } = require('./schemas')
9
9
  const { parseHeadOnSendHandlers } = require('./headRoute')
10
10
  const warning = require('./warnings')
11
+ const { kRequestAcceptVersion } = require('./symbols')
11
12
 
12
13
  const {
13
14
  compileSchemasForValidation,
@@ -344,6 +345,12 @@ function buildRouting (options) {
344
345
  }
345
346
  }
346
347
 
348
+ // we revert the changes in defaultRoute
349
+ if (req.headers[kRequestAcceptVersion] !== undefined) {
350
+ req.headers['accept-version'] = req.headers[kRequestAcceptVersion]
351
+ req.headers[kRequestAcceptVersion] = undefined
352
+ }
353
+
347
354
  const id = req.headers[requestIdHeader] || genReqId(req)
348
355
 
349
356
  const loggerBinding = {
package/lib/symbols.js CHANGED
@@ -21,6 +21,7 @@ const keys = {
21
21
  kReply: Symbol('fastify.Reply'),
22
22
  kRequest: Symbol('fastify.Request'),
23
23
  kRequestPayloadStream: Symbol('fastify.RequestPayloadStream'),
24
+ kRequestAcceptVersion: Symbol('fastify.RequestAcceptVersion'),
24
25
  kCanSetNotFoundHandler: Symbol('fastify.canSetNotFoundHandler'),
25
26
  kFourOhFour: Symbol('fastify.404'),
26
27
  kFourOhFourLevelInstance: Symbol('fastify.404LogLevelInstance'),
package/lib/warnings.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const warning = require('fastify-warning')()
3
+ const warning = require('process-warning')()
4
4
 
5
5
  /**
6
6
  * Deprecation codes:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "3.25.0",
3
+ "version": "3.26.0",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -16,7 +16,7 @@
16
16
  "lint:typescript": "eslint -c types/.eslintrc.json types/**/*.d.ts test/types/**/*.test-d.ts",
17
17
  "prepublishOnly": "tap --no-check-coverage test/internals/version.test.js",
18
18
  "test": "npm run lint && npm run unit && npm run test:typescript",
19
- "test:ci": "npm run lint && npm run unit -- --cov --coverage-report=lcovonly && npm run test:typescript",
19
+ "test:ci": "npm run unit -- --cov --coverage-report=lcovonly && npm run test:typescript",
20
20
  "test:report": "npm run lint && npm run unit:report && npm run test:typescript",
21
21
  "test:typescript": "tsd",
22
22
  "unit": "tap -J test/*.test.js test/*/*.test.js",
@@ -152,10 +152,10 @@
152
152
  "form-data": "^4.0.0",
153
153
  "frameguard": "^4.0.0",
154
154
  "h2url": "^0.2.0",
155
- "helmet": "^4.0.0",
155
+ "helmet": "^5.0.1",
156
156
  "hide-powered-by": "^1.1.0",
157
157
  "hsts": "^2.2.0",
158
- "http-errors": "^1.7.1",
158
+ "http-errors": "^2.0.0",
159
159
  "ienoopen": "^1.1.0",
160
160
  "JSONStream": "^1.3.5",
161
161
  "license-checker": "^25.0.1",
@@ -183,8 +183,8 @@
183
183
  "avvio": "^7.1.2",
184
184
  "fast-json-stringify": "^2.5.2",
185
185
  "fastify-error": "^0.3.0",
186
- "fastify-warning": "^0.2.0",
187
- "find-my-way": "^4.1.0",
186
+ "process-warning": "^1.0.0",
187
+ "find-my-way": "^4.5.0",
188
188
  "flatstr": "^1.0.12",
189
189
  "light-my-request": "^4.2.0",
190
190
  "pino": "^6.13.0",
package/test/404s.test.js CHANGED
@@ -1221,7 +1221,7 @@ test('preHandler option for setNotFoundHandler', t => {
1221
1221
  })
1222
1222
 
1223
1223
  // https://github.com/fastify/fastify/issues/2229
1224
- t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', t => {
1224
+ t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', { timeout: 40000 }, t => {
1225
1225
  t.plan(2)
1226
1226
  const fastify = Fastify()
1227
1227
 
@@ -0,0 +1,74 @@
1
+ 'use strict'
2
+
3
+ const { AsyncLocalStorage } = require('async_hooks')
4
+ const t = require('tap')
5
+ const Fastify = require('..')
6
+ const sget = require('simple-get').concat
7
+
8
+ if (!AsyncLocalStorage) {
9
+ t.skip('AsyncLocalStorage not available, skipping test')
10
+ process.exit(0)
11
+ }
12
+
13
+ const storage = new AsyncLocalStorage()
14
+ const app = Fastify({ logger: false })
15
+
16
+ let counter = 0
17
+ app.addHook('onRequest', (req, reply, next) => {
18
+ const id = counter++
19
+ storage.run({ id }, next)
20
+ })
21
+
22
+ app.get('/', function (request, reply) {
23
+ t.ok(storage.getStore())
24
+ const id = storage.getStore().id
25
+ reply.send({ id })
26
+ })
27
+
28
+ app.post('/', function (request, reply) {
29
+ t.ok(storage.getStore())
30
+ const id = storage.getStore().id
31
+ reply.send({ id })
32
+ })
33
+
34
+ app.listen(3000, function (err, address) {
35
+ t.error(err)
36
+
37
+ sget({
38
+ method: 'POST',
39
+ url: 'http://localhost:' + app.server.address().port,
40
+ body: {
41
+ hello: 'world'
42
+ },
43
+ json: true
44
+ }, (err, response, body) => {
45
+ t.error(err)
46
+ t.equal(response.statusCode, 200)
47
+ t.same(body, { id: 0 })
48
+
49
+ sget({
50
+ method: 'POST',
51
+ url: 'http://localhost:' + app.server.address().port,
52
+ body: {
53
+ hello: 'world'
54
+ },
55
+ json: true
56
+ }, (err, response, body) => {
57
+ t.error(err)
58
+ t.equal(response.statusCode, 200)
59
+ t.same(body, { id: 1 })
60
+
61
+ sget({
62
+ method: 'GET',
63
+ url: 'http://localhost:' + app.server.address().port,
64
+ json: true
65
+ }, (err, response, body) => {
66
+ t.error(err)
67
+ t.equal(response.statusCode, 200)
68
+ t.same(body, { id: 2 })
69
+ app.close()
70
+ t.end()
71
+ })
72
+ })
73
+ })
74
+ })
@@ -0,0 +1,31 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const test = t.test
5
+ const fastifySuccess = require('./dist/success')
6
+ const fastifyFailPlugin = require('./dist/failPlugin')
7
+
8
+ test('Bundled package should work', (t) => {
9
+ t.plan(4)
10
+ fastifySuccess.ready((err) => {
11
+ t.error(err)
12
+ fastifySuccess.inject(
13
+ {
14
+ method: 'GET',
15
+ url: '/'
16
+ },
17
+ (error, res) => {
18
+ t.error(error)
19
+ t.equal(res.statusCode, 200)
20
+ t.same(res.json(), { hello: 'world' })
21
+ }
22
+ )
23
+ })
24
+ })
25
+
26
+ test('Bundled package should not work with bad plugin version', (t) => {
27
+ t.plan(1)
28
+ fastifyFailPlugin.ready((err) => {
29
+ t.match(err.message, /expected '9.x' fastify version/i)
30
+ })
31
+ })
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": "0.0.1",
3
+ "scripts": {
4
+ "bundle": "esbuild success=src/index.js failPlugin=src/fail-plugin-version.js --bundle --outdir=dist --platform=node",
5
+ "test": "npm run bundle && node bundler-test.js"
6
+ },
7
+ "devDependencies": {
8
+ "esbuild": "^0.14.11"
9
+ }
10
+ }
@@ -0,0 +1,12 @@
1
+ const fp = require('fastify-plugin')
2
+ const fastify = require('../../../../')()
3
+
4
+ fastify.get('/', function (request, reply) {
5
+ reply.send({ hello: 'world' })
6
+ })
7
+
8
+ fastify.register(fp((instance, opts, done) => {
9
+ done()
10
+ }, { fastify: '9.x' }))
11
+
12
+ module.exports = fastify
@@ -0,0 +1,7 @@
1
+ const fastify = require('../../../../')()
2
+ // Declare a route
3
+ fastify.get('/', function (request, reply) {
4
+ reply.send({ hello: 'world' })
5
+ })
6
+
7
+ module.exports = fastify
@@ -5,16 +5,27 @@ const test = t.test
5
5
  const fastifySuccess = require('./dist/success')
6
6
  const fastifyFailPlugin = require('./dist/failPlugin')
7
7
 
8
- test('Bundled package should work', t => {
9
- t.plan(1)
8
+ test('Bundled package should work', (t) => {
9
+ t.plan(4)
10
10
  fastifySuccess.ready((err) => {
11
11
  t.error(err)
12
+ fastifySuccess.inject(
13
+ {
14
+ method: 'GET',
15
+ url: '/'
16
+ },
17
+ (error, res) => {
18
+ t.error(error)
19
+ t.equal(res.statusCode, 200)
20
+ t.same(res.json(), { hello: 'world' })
21
+ }
22
+ )
12
23
  })
13
24
  })
14
25
 
15
- test('Bundled package should not work with bad plugin version', t => {
26
+ test('Bundled package should not work with bad plugin version', (t) => {
16
27
  t.plan(1)
17
28
  fastifyFailPlugin.ready((err) => {
18
- t.ok(err)
29
+ t.match(err.message, /expected '9.x' fastify version/i)
19
30
  })
20
31
  })
@@ -1,7 +1,5 @@
1
1
  const fp = require('fastify-plugin')
2
- const fastify = require('../../../../')({
3
- logger: true
4
- })
2
+ const fastify = require('../../../../')()
5
3
 
6
4
  fastify.get('/', function (request, reply) {
7
5
  reply.send({ hello: 'world' })
@@ -1,6 +1,4 @@
1
- const fastify = require('../../../../')({
2
- logger: true
3
- })
1
+ const fastify = require('../../../../')()
4
2
  // Declare a route
5
3
  fastify.get('/', function (request, reply) {
6
4
  reply.send({ hello: 'world' })