fastify 5.2.0 → 5.2.2

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 (83) hide show
  1. package/LICENSE +1 -1
  2. package/PROJECT_CHARTER.md +7 -7
  3. package/README.md +65 -67
  4. package/SPONSORS.md +2 -0
  5. package/build/build-validation.js +1 -1
  6. package/docs/Guides/Benchmarking.md +4 -4
  7. package/docs/Guides/Database.md +1 -1
  8. package/docs/Guides/Delay-Accepting-Requests.md +10 -10
  9. package/docs/Guides/Ecosystem.md +5 -1
  10. package/docs/Guides/Fluent-Schema.md +1 -1
  11. package/docs/Guides/Getting-Started.md +9 -5
  12. package/docs/Guides/Index.md +1 -1
  13. package/docs/Guides/Migration-Guide-V4.md +1 -1
  14. package/docs/Guides/Migration-Guide-V5.md +12 -2
  15. package/docs/Guides/Plugins-Guide.md +6 -6
  16. package/docs/Guides/Serverless.md +14 -48
  17. package/docs/Guides/Style-Guide.md +2 -2
  18. package/docs/Guides/Testing.md +2 -2
  19. package/docs/Guides/Write-Plugin.md +2 -3
  20. package/docs/Reference/ContentTypeParser.md +58 -78
  21. package/docs/Reference/Decorators.md +50 -60
  22. package/docs/Reference/Encapsulation.md +28 -33
  23. package/docs/Reference/Errors.md +52 -53
  24. package/docs/Reference/HTTP2.md +7 -7
  25. package/docs/Reference/Hooks.md +31 -30
  26. package/docs/Reference/LTS.md +10 -15
  27. package/docs/Reference/Lifecycle.md +19 -24
  28. package/docs/Reference/Logging.md +59 -56
  29. package/docs/Reference/Middleware.md +19 -19
  30. package/docs/Reference/Plugins.md +55 -71
  31. package/docs/Reference/Principles.md +25 -30
  32. package/docs/Reference/Reply.md +11 -10
  33. package/docs/Reference/Request.md +89 -99
  34. package/docs/Reference/Routes.md +108 -128
  35. package/docs/Reference/Server.md +19 -17
  36. package/docs/Reference/Type-Providers.md +19 -21
  37. package/docs/Reference/TypeScript.md +1 -18
  38. package/docs/Reference/Validation-and-Serialization.md +134 -159
  39. package/docs/Reference/Warnings.md +22 -25
  40. package/fastify.js +1 -1
  41. package/lib/contentTypeParser.js +7 -8
  42. package/lib/error-handler.js +14 -12
  43. package/lib/errors.js +4 -0
  44. package/lib/headRoute.js +4 -2
  45. package/lib/pluginUtils.js +4 -2
  46. package/lib/reply.js +4 -0
  47. package/lib/request.js +13 -9
  48. package/lib/server.js +5 -0
  49. package/lib/validation.js +1 -1
  50. package/lib/warnings.js +9 -0
  51. package/lib/wrapThenable.js +8 -1
  52. package/package.json +28 -17
  53. package/test/build/error-serializer.test.js +2 -1
  54. package/test/bundler/esbuild/package.json +1 -1
  55. package/test/close.test.js +125 -108
  56. package/test/custom-parser-async.test.js +34 -36
  57. package/test/custom-parser.2.test.js +19 -20
  58. package/test/custom-parser.3.test.js +56 -45
  59. package/test/delete.test.js +79 -67
  60. package/test/genReqId.test.js +125 -174
  61. package/test/has-route.test.js +1 -3
  62. package/test/internals/content-type-parser.test.js +1 -1
  63. package/test/internals/errors.test.js +19 -7
  64. package/test/issue-4959.test.js +84 -0
  65. package/test/listen.1.test.js +37 -34
  66. package/test/listen.2.test.js +47 -40
  67. package/test/listen.3.test.js +28 -32
  68. package/test/listen.4.test.js +61 -45
  69. package/test/listen.5.test.js +23 -0
  70. package/test/nullable-validation.test.js +30 -27
  71. package/test/register.test.js +55 -50
  72. package/test/request-error.test.js +114 -94
  73. package/test/route-shorthand.test.js +36 -32
  74. package/test/server.test.js +0 -175
  75. package/test/stream.5.test.js +35 -33
  76. package/test/throw.test.js +87 -91
  77. package/test/toolkit.js +32 -0
  78. package/test/trust-proxy.test.js +23 -23
  79. package/test/types/instance.test-d.ts +1 -0
  80. package/test/upgrade.test.js +32 -30
  81. package/test/web-api.test.js +44 -0
  82. package/types/instance.d.ts +4 -0
  83. package/test/test-reporter.mjs +0 -68
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const assert = require('node:assert')
4
+ const { test } = require('node:test')
5
5
  const sget = require('simple-get').concat
6
6
  const fastify = require('..')()
7
7
 
@@ -85,15 +85,17 @@ const bodySchema = {
85
85
  }
86
86
  }
87
87
 
88
- test('shorthand - delete', t => {
88
+ test('shorthand - delete', (t, done) => {
89
89
  t.plan(1)
90
90
  try {
91
91
  fastify.delete('/', schema, function (req, reply) {
92
92
  reply.code(200).send({ hello: 'world' })
93
93
  })
94
- t.pass()
94
+ t.assert.ok(true)
95
95
  } catch (e) {
96
- t.fail()
96
+ t.assert.fail()
97
+ } finally {
98
+ done()
97
99
  }
98
100
  })
99
101
 
@@ -103,9 +105,9 @@ test('shorthand - delete params', t => {
103
105
  fastify.delete('/params/:foo/:test', paramsSchema, function (req, reply) {
104
106
  reply.code(200).send(req.params)
105
107
  })
106
- t.pass()
108
+ t.assert.ok(true)
107
109
  } catch (e) {
108
- t.fail()
110
+ t.assert.fail()
109
111
  }
110
112
  })
111
113
 
@@ -115,9 +117,9 @@ test('shorthand - delete, querystring schema', t => {
115
117
  fastify.delete('/query', querySchema, function (req, reply) {
116
118
  reply.send(req.query)
117
119
  })
118
- t.pass()
120
+ t.assert.ok(true)
119
121
  } catch (e) {
120
- t.fail()
122
+ t.assert.fail()
121
123
  }
122
124
  })
123
125
 
@@ -127,9 +129,9 @@ test('shorthand - get, headers schema', t => {
127
129
  fastify.delete('/headers', headersSchema, function (req, reply) {
128
130
  reply.code(200).send(req.headers)
129
131
  })
130
- t.pass()
132
+ t.assert.ok(true)
131
133
  } catch (e) {
132
- t.fail()
134
+ t.assert.fail()
133
135
  }
134
136
  })
135
137
 
@@ -139,9 +141,9 @@ test('missing schema - delete', t => {
139
141
  fastify.delete('/missing', function (req, reply) {
140
142
  reply.code(200).send({ hello: 'world' })
141
143
  })
142
- t.pass()
144
+ t.assert.ok(true)
143
145
  } catch (e) {
144
- t.fail()
146
+ t.assert.fail()
145
147
  }
146
148
  })
147
149
 
@@ -151,60 +153,63 @@ test('body - delete', t => {
151
153
  fastify.delete('/body', bodySchema, function (req, reply) {
152
154
  reply.send(req.body)
153
155
  })
154
- t.pass()
156
+ t.assert.ok(true)
155
157
  } catch (e) {
156
- t.fail()
158
+ t.assert.fail()
157
159
  }
158
160
  })
159
161
 
160
162
  fastify.listen({ port: 0 }, err => {
161
- t.error(err)
162
- t.teardown(() => { fastify.close() })
163
+ assert.ifError(err)
164
+ test.after(() => { fastify.close() })
163
165
 
164
- test('shorthand - request delete', t => {
166
+ test('shorthand - request delete', (t, done) => {
165
167
  t.plan(4)
166
168
  sget({
167
169
  method: 'DELETE',
168
170
  url: 'http://localhost:' + fastify.server.address().port
169
171
  }, (err, response, body) => {
170
- t.error(err)
171
- t.equal(response.statusCode, 200)
172
- t.equal(response.headers['content-length'], '' + body.length)
173
- t.same(JSON.parse(body), { hello: 'world' })
172
+ t.assert.ifError(err)
173
+ t.assert.strictEqual(response.statusCode, 200)
174
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
175
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
176
+ done()
174
177
  })
175
178
  })
176
179
 
177
- test('shorthand - request delete params schema', t => {
180
+ test('shorthand - request delete params schema', (t, done) => {
178
181
  t.plan(4)
179
182
  sget({
180
183
  method: 'DELETE',
181
184
  url: 'http://localhost:' + fastify.server.address().port + '/params/world/123'
182
185
  }, (err, response, body) => {
183
- t.error(err)
184
- t.equal(response.statusCode, 200)
185
- t.equal(response.headers['content-length'], '' + body.length)
186
- t.same(JSON.parse(body), { foo: 'world', test: 123 })
186
+ t.assert.ifError(err)
187
+ t.assert.strictEqual(response.statusCode, 200)
188
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
189
+ t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
190
+ done()
187
191
  })
188
192
  })
189
193
 
190
- test('shorthand - request delete params schema error', t => {
194
+ test('shorthand - request delete params schema error', (t, done) => {
191
195
  t.plan(3)
192
196
  sget({
193
197
  method: 'DELETE',
194
198
  url: 'http://localhost:' + fastify.server.address().port + '/params/world/string'
195
199
  }, (err, response, body) => {
196
- t.error(err)
197
- t.equal(response.statusCode, 400)
198
- t.same(JSON.parse(body), {
200
+ t.assert.ifError(err)
201
+ t.assert.strictEqual(response.statusCode, 400)
202
+ t.assert.deepStrictEqual(JSON.parse(body), {
199
203
  error: 'Bad Request',
200
204
  code: 'FST_ERR_VALIDATION',
201
205
  message: 'params/test must be integer',
202
206
  statusCode: 400
203
207
  })
208
+ done()
204
209
  })
205
210
  })
206
211
 
207
- test('shorthand - request delete headers schema', t => {
212
+ test('shorthand - request delete headers schema', (t, done) => {
208
213
  t.plan(4)
209
214
  sget({
210
215
  method: 'DELETE',
@@ -213,14 +218,15 @@ fastify.listen({ port: 0 }, err => {
213
218
  },
214
219
  url: 'http://localhost:' + fastify.server.address().port + '/headers'
215
220
  }, (err, response, body) => {
216
- t.error(err)
217
- t.equal(response.statusCode, 200)
218
- t.equal(response.headers['content-length'], '' + body.length)
219
- t.equal(JSON.parse(body)['x-test'], 1)
221
+ t.assert.ifError(err)
222
+ t.assert.strictEqual(response.statusCode, 200)
223
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
224
+ t.assert.strictEqual(JSON.parse(body)['x-test'], 1)
225
+ done()
220
226
  })
221
227
  })
222
228
 
223
- test('shorthand - request delete headers schema error', t => {
229
+ test('shorthand - request delete headers schema error', (t, done) => {
224
230
  t.plan(3)
225
231
  sget({
226
232
  method: 'DELETE',
@@ -229,61 +235,65 @@ fastify.listen({ port: 0 }, err => {
229
235
  },
230
236
  url: 'http://localhost:' + fastify.server.address().port + '/headers'
231
237
  }, (err, response, body) => {
232
- t.error(err)
233
- t.equal(response.statusCode, 400)
234
- t.same(JSON.parse(body), {
238
+ t.assert.ifError(err)
239
+ t.assert.strictEqual(response.statusCode, 400)
240
+ t.assert.deepStrictEqual(JSON.parse(body), {
235
241
  error: 'Bad Request',
236
242
  code: 'FST_ERR_VALIDATION',
237
243
  message: 'headers/x-test must be number',
238
244
  statusCode: 400
239
245
  })
246
+ done()
240
247
  })
241
248
  })
242
249
 
243
- test('shorthand - request delete querystring schema', t => {
250
+ test('shorthand - request delete querystring schema', (t, done) => {
244
251
  t.plan(4)
245
252
  sget({
246
253
  method: 'DELETE',
247
254
  url: 'http://localhost:' + fastify.server.address().port + '/query?hello=123'
248
255
  }, (err, response, body) => {
249
- t.error(err)
250
- t.equal(response.statusCode, 200)
251
- t.equal(response.headers['content-length'], '' + body.length)
252
- t.same(JSON.parse(body), { hello: 123 })
256
+ t.assert.ifError(err)
257
+ t.assert.strictEqual(response.statusCode, 200)
258
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
259
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
260
+ done()
253
261
  })
254
262
  })
255
263
 
256
- test('shorthand - request delete querystring schema error', t => {
264
+ test('shorthand - request delete querystring schema error', (t, done) => {
257
265
  t.plan(3)
258
266
  sget({
259
267
  method: 'DELETE',
260
268
  url: 'http://localhost:' + fastify.server.address().port + '/query?hello=world'
261
269
  }, (err, response, body) => {
262
- t.error(err)
263
- t.equal(response.statusCode, 400)
264
- t.same(JSON.parse(body), {
270
+ t.assert.ifError(err)
271
+ t.assert.strictEqual(response.statusCode, 400)
272
+ t.assert.deepStrictEqual(JSON.parse(body), {
265
273
  error: 'Bad Request',
266
274
  code: 'FST_ERR_VALIDATION',
267
275
  message: 'querystring/hello must be integer',
268
276
  statusCode: 400
269
277
  })
278
+ done()
270
279
  })
271
280
  })
272
281
 
273
- test('shorthand - request delete missing schema', t => {
282
+ test('shorthand - request delete missing schema', (t, done) => {
274
283
  t.plan(4)
275
284
  sget({
276
285
  method: 'DELETE',
277
286
  url: 'http://localhost:' + fastify.server.address().port + '/missing'
278
287
  }, (err, response, body) => {
279
- t.error(err)
280
- t.equal(response.statusCode, 200)
281
- t.equal(response.headers['content-length'], '' + body.length)
282
- t.same(JSON.parse(body), { hello: 'world' })
288
+ t.assert.ifError(err)
289
+ t.assert.strictEqual(response.statusCode, 200)
290
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
291
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
292
+ done()
283
293
  })
284
294
  })
285
295
 
286
- test('shorthand - delete with body', t => {
296
+ test('shorthand - delete with body', (t, done) => {
287
297
  t.plan(3)
288
298
  sget({
289
299
  method: 'DELETE',
@@ -293,18 +303,19 @@ fastify.listen({ port: 0 }, err => {
293
303
  },
294
304
  json: true
295
305
  }, (err, response, body) => {
296
- t.error(err)
297
- t.equal(response.statusCode, 200)
298
- t.same(body, { hello: 'world' })
306
+ t.assert.ifError(err)
307
+ t.assert.strictEqual(response.statusCode, 200)
308
+ t.assert.deepStrictEqual(body, { hello: 'world' })
309
+ done()
299
310
  })
300
311
  })
301
312
  })
302
313
 
303
- test('shorthand - delete with application/json Content-Type header and null body', t => {
314
+ test('shorthand - delete with application/json Content-Type header and null body', (t, done) => {
304
315
  t.plan(4)
305
316
  const fastify = require('..')()
306
317
  fastify.delete('/', {}, (req, reply) => {
307
- t.equal(req.body, null)
318
+ t.assert.strictEqual(req.body, null)
308
319
  reply.send(req.body)
309
320
  })
310
321
  fastify.inject({
@@ -313,9 +324,10 @@ test('shorthand - delete with application/json Content-Type header and null body
313
324
  headers: { 'Content-Type': 'application/json' },
314
325
  body: 'null'
315
326
  }, (err, response) => {
316
- t.error(err)
317
- t.equal(response.statusCode, 200)
318
- t.same(response.payload.toString(), 'null')
327
+ t.assert.ifError(err)
328
+ t.assert.strictEqual(response.statusCode, 200)
329
+ t.assert.strictEqual(response.payload.toString(), 'null')
330
+ done()
319
331
  })
320
332
  })
321
333
 
@@ -325,7 +337,7 @@ test('shorthand - delete with application/json Content-Type header and without b
325
337
  t.plan(4)
326
338
  const fastify = require('..')()
327
339
  fastify.delete('/', {}, (req, reply) => {
328
- t.equal(req.body, undefined)
340
+ t.assert.strictEqual(req.body, undefined)
329
341
  reply.send(req.body)
330
342
  })
331
343
  fastify.inject({
@@ -334,8 +346,8 @@ test('shorthand - delete with application/json Content-Type header and without b
334
346
  headers: { 'Content-Type': 'application/json' },
335
347
  body: null
336
348
  }, (err, response) => {
337
- t.error(err)
338
- t.equal(response.statusCode, 200)
339
- t.same(response.payload.toString(), '')
349
+ t.assert.ifError(err)
350
+ t.assert.strictEqual(response.statusCode, 200)
351
+ t.assert.strictEqual(response.payload.toString(), '')
340
352
  })
341
353
  })