fastify 5.3.3 → 5.4.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 (70) hide show
  1. package/README.md +2 -0
  2. package/build/build-validation.js +2 -1
  3. package/docs/Guides/Delay-Accepting-Requests.md +3 -3
  4. package/docs/Guides/Ecosystem.md +9 -5
  5. package/docs/Reference/ContentTypeParser.md +1 -1
  6. package/docs/Reference/Errors.md +2 -2
  7. package/docs/Reference/Hooks.md +14 -14
  8. package/docs/Reference/Logging.md +3 -3
  9. package/docs/Reference/Middleware.md +1 -1
  10. package/docs/Reference/Reply.md +8 -8
  11. package/docs/Reference/Request.md +1 -1
  12. package/docs/Reference/Routes.md +3 -3
  13. package/docs/Reference/Server.md +35 -21
  14. package/docs/Reference/Validation-and-Serialization.md +1 -1
  15. package/fastify.d.ts +2 -1
  16. package/fastify.js +14 -2
  17. package/lib/configValidator.js +1 -1
  18. package/lib/errors.js +6 -0
  19. package/lib/pluginOverride.js +3 -1
  20. package/lib/reply.js +7 -11
  21. package/lib/request.js +3 -10
  22. package/lib/symbols.js +1 -0
  23. package/lib/warnings.js +8 -0
  24. package/package.json +8 -4
  25. package/test/404s.test.js +226 -325
  26. package/test/allow-unsafe-regex.test.js +19 -48
  27. package/test/als.test.js +28 -40
  28. package/test/async-await.test.js +11 -2
  29. package/test/body-limit.test.js +41 -65
  30. package/test/build-certificate.js +1 -1
  31. package/test/custom-parser-async.test.js +17 -22
  32. package/test/decorator-namespace.test._js_ +3 -4
  33. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  34. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  35. package/test/helper.js +1 -1
  36. package/test/hooks-async.test.js +248 -218
  37. package/test/hooks.test.js +910 -769
  38. package/test/http-methods/lock.test.js +31 -31
  39. package/test/http-methods/mkcol.test.js +5 -9
  40. package/test/http-methods/proppatch.test.js +23 -29
  41. package/test/http-methods/report.test.js +44 -69
  42. package/test/http-methods/search.test.js +67 -82
  43. package/test/http2/closing.test.js +38 -20
  44. package/test/http2/secure-with-fallback.test.js +28 -27
  45. package/test/https/https.test.js +56 -53
  46. package/test/internals/errors.test.js +1 -1
  47. package/test/internals/handle-request.test.js +49 -66
  48. package/test/issue-4959.test.js +12 -3
  49. package/test/listen.4.test.js +31 -43
  50. package/test/nullable-validation.test.js +33 -46
  51. package/test/output-validation.test.js +24 -26
  52. package/test/plugin.2.test.js +104 -86
  53. package/test/plugin.3.test.js +56 -35
  54. package/test/plugin.4.test.js +124 -119
  55. package/test/proto-poisoning.test.js +78 -97
  56. package/test/request-error.test.js +0 -46
  57. package/test/route-hooks.test.js +112 -92
  58. package/test/route-prefix.test.js +194 -133
  59. package/test/schema-serialization.test.js +177 -154
  60. package/test/schema-special-usage.test.js +165 -132
  61. package/test/schema-validation.test.js +242 -205
  62. package/test/set-error-handler.test.js +58 -1
  63. package/test/skip-reply-send.test.js +64 -69
  64. package/test/trust-proxy.test.js +32 -58
  65. package/test/types/fastify.test-d.ts +3 -0
  66. package/test/types/request.test-d.ts +1 -0
  67. package/test/url-rewriting.test.js +45 -62
  68. package/types/request.d.ts +1 -0
  69. package/.taprc +0 -7
  70. package/.vscode/settings.json +0 -22
@@ -2,10 +2,9 @@
2
2
 
3
3
  const { test } = require('node:test')
4
4
  const Fastify = require('..')
5
- const sget = require('simple-get').concat
6
5
 
7
- test('Should rewrite url', (t, done) => {
8
- t.plan(5)
6
+ test('Should rewrite url', async t => {
7
+ t.plan(4)
9
8
  const fastify = Fastify({
10
9
  rewriteUrl (req) {
11
10
  t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
@@ -22,24 +21,19 @@ test('Should rewrite url', (t, done) => {
22
21
  }
23
22
  })
24
23
 
25
- fastify.listen({ port: 0 }, function (err) {
26
- t.assert.ifError(err)
27
-
28
- t.after(() => fastify.close())
29
- sget({
30
- method: 'GET',
31
- url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
32
- }, (err, response, body) => {
33
- t.assert.ifError(err)
34
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
35
- t.assert.strictEqual(response.statusCode, 200)
36
- done()
37
- })
38
- })
24
+ const fastifyServer = await fastify.listen({ port: 0 })
25
+
26
+ t.after(() => fastify.close())
27
+
28
+ const result = await fetch(`${fastifyServer}/this-would-404-without-url-rewrite`)
29
+
30
+ t.assert.ok(result.ok)
31
+ t.assert.strictEqual(result.status, 200)
32
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
39
33
  })
40
34
 
41
- test('Should not rewrite if the url is the same', (t, done) => {
42
- t.plan(4)
35
+ test('Should not rewrite if the url is the same', async t => {
36
+ t.plan(3)
43
37
  const fastify = Fastify({
44
38
  rewriteUrl (req) {
45
39
  t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
@@ -56,22 +50,18 @@ test('Should not rewrite if the url is the same', (t, done) => {
56
50
  }
57
51
  })
58
52
 
59
- fastify.listen({ port: 0 }, function (err) {
60
- t.assert.ifError(err)
61
- t.after(() => fastify.close())
62
- sget({
63
- method: 'GET',
64
- url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
65
- }, (err, response, body) => {
66
- t.assert.ifError(err)
67
- t.assert.strictEqual(response.statusCode, 404)
68
- done()
69
- })
70
- })
53
+ const fastifyServer = await fastify.listen({ port: 0 })
54
+
55
+ t.after(() => fastify.close())
56
+
57
+ const result = await fetch(`${fastifyServer}/this-would-404-without-url-rewrite`)
58
+
59
+ t.assert.ok(!result.ok)
60
+ t.assert.strictEqual(result.status, 404)
71
61
  })
72
62
 
73
- test('Should throw an error', (t, done) => {
74
- t.plan(5)
63
+ test('Should throw an error', async t => {
64
+ t.plan(2)
75
65
  const fastify = Fastify({
76
66
  rewriteUrl (req) {
77
67
  t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
@@ -88,23 +78,20 @@ test('Should throw an error', (t, done) => {
88
78
  }
89
79
  })
90
80
 
91
- fastify.listen({ port: 0 }, function (err) {
92
- t.assert.ifError(err)
93
- t.after(() => fastify.close())
94
- sget({
95
- method: 'GET',
96
- url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
97
- }, (err, response, body) => {
98
- t.assert.strictEqual(err.code, 'ECONNRESET')
99
- t.assert.strictEqual(response, undefined)
100
- t.assert.strictEqual(body, undefined)
101
- done()
102
- })
103
- })
81
+ const fastifyServer = await fastify.listen({ port: 0 })
82
+
83
+ t.after(() => fastify.close())
84
+
85
+ try {
86
+ await fetch(`${fastifyServer}/this-would-404-without-url-rewrite`)
87
+ t.assert.fail('Expected fetch to throw an error')
88
+ } catch (err) {
89
+ t.assert.ok(err instanceof Error)
90
+ }
104
91
  })
105
92
 
106
- test('Should rewrite url but keep originalUrl unchanged', (t, done) => {
107
- t.plan(7)
93
+ test('Should rewrite url but keep originalUrl unchanged', async t => {
94
+ t.plan(6)
108
95
  const fastify = Fastify({
109
96
  rewriteUrl (req) {
110
97
  t.assert.strictEqual(req.url, '/this-would-404-without-url-rewrite')
@@ -122,18 +109,14 @@ test('Should rewrite url but keep originalUrl unchanged', (t, done) => {
122
109
  }
123
110
  })
124
111
 
125
- fastify.listen({ port: 0 }, function (err) {
126
- t.assert.ifError(err)
127
- t.after(() => fastify.close())
128
- sget({
129
- method: 'GET',
130
- url: 'http://localhost:' + fastify.server.address().port + '/this-would-404-without-url-rewrite'
131
- }, (err, response, body) => {
132
- t.assert.ifError(err)
133
- const parsedBody = JSON.parse(body)
134
- t.assert.deepStrictEqual(parsedBody, { hello: 'world', hostname: 'localhost', port: fastify.server.address().port })
135
- t.assert.strictEqual(response.statusCode, 200)
136
- done()
137
- })
138
- })
112
+ await fastify.listen({ port: 0 })
113
+ const port = fastify.server.address().port
114
+
115
+ t.after(() => fastify.close())
116
+
117
+ const result = await fetch(`http://localhost:${port}/this-would-404-without-url-rewrite`)
118
+
119
+ t.assert.ok(result.ok)
120
+ t.assert.strictEqual(result.status, 200)
121
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world', hostname: 'localhost', port })
139
122
  })
@@ -32,6 +32,7 @@ export interface RequestRouteOptions<ContextConfig = ContextConfigDefault, Schem
32
32
  config: FastifyContextConfig & FastifyRouteConfig & ContextConfig;
33
33
  schema?: SchemaCompiler; // it is empty for 404 requests
34
34
  handler: RouteHandlerMethod;
35
+ version?: string;
35
36
  }
36
37
 
37
38
  /**
package/.taprc DELETED
@@ -1,7 +0,0 @@
1
- # vim: set filetype=yaml :
2
- node-arg:
3
- - '--allow-natives-syntax'
4
-
5
- include:
6
- - 'test/**/*.test.js'
7
- - 'test/**/*.test.mjs'
@@ -1,22 +0,0 @@
1
- {
2
- "workbench.colorCustomizations": {
3
- "[GitHub Dark]": {
4
- "tab.activeBackground": "#0d0d0d",
5
- "tab.activeBorder": "#ffff00"
6
- },
7
- "activityBar.background": "#7AC6C8",
8
- "activityBar.foreground": "#0B2A34",
9
- "activityBar.inactiveForeground": "#1e2021",
10
- "activityBar.activeBorder": "#853937",
11
- "activityBar.activeBackground": "#3F7F81",
12
- "activityBar.border": "#3F7F81",
13
- "titleBar.activeBackground": "#549B9D",
14
- "titleBar.activeForeground": "#0B2A34",
15
- "titleBar.inactiveBackground": "#727f7f",
16
- "titleBar.inactiveForeground": "#1e2021",
17
- "titleBar.border": "#3F7F81",
18
- "statusBar.background": "#71ACAD",
19
- "statusBar.foreground": "#0B2A34",
20
- "statusBar.border": "#3F7F81"
21
- }
22
- }