fastify 3.27.3 → 4.0.0-alpha.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 (164) hide show
  1. package/.taprc +3 -0
  2. package/README.md +7 -7
  3. package/build/build-error-serializer.js +27 -0
  4. package/build/build-validation.js +47 -35
  5. package/docs/Guides/Database.md +320 -0
  6. package/docs/Guides/Getting-Started.md +7 -7
  7. package/docs/Guides/Plugins-Guide.md +1 -1
  8. package/docs/Guides/Serverless.md +3 -3
  9. package/docs/Guides/Testing.md +2 -2
  10. package/docs/Migration-Guide-V4.md +12 -0
  11. package/docs/Reference/ContentTypeParser.md +4 -0
  12. package/docs/Reference/Decorators.md +2 -2
  13. package/docs/Reference/Encapsulation.md +2 -2
  14. package/docs/Reference/Errors.md +51 -6
  15. package/docs/Reference/HTTP2.md +3 -3
  16. package/docs/Reference/Hooks.md +4 -7
  17. package/docs/Reference/LTS.md +5 -4
  18. package/docs/Reference/Plugins.md +3 -3
  19. package/docs/Reference/Reply.md +23 -22
  20. package/docs/Reference/Request.md +1 -3
  21. package/docs/Reference/Routes.md +22 -15
  22. package/docs/Reference/Server.md +69 -119
  23. package/docs/Reference/TypeScript.md +20 -22
  24. package/docs/Reference/Validation-and-Serialization.md +30 -55
  25. package/docs/Type-Providers.md +257 -0
  26. package/examples/asyncawait.js +1 -1
  27. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  28. package/examples/benchmark/hooks-benchmark.js +1 -1
  29. package/examples/benchmark/simple.js +1 -1
  30. package/examples/hooks.js +2 -2
  31. package/examples/http2.js +1 -1
  32. package/examples/https.js +1 -1
  33. package/examples/parser.js +1 -1
  34. package/examples/route-prefix.js +1 -1
  35. package/examples/shared-schema.js +1 -1
  36. package/examples/simple-stream.js +18 -0
  37. package/examples/simple.js +1 -1
  38. package/examples/simple.mjs +1 -1
  39. package/examples/typescript-server.ts +1 -1
  40. package/examples/use-plugin.js +1 -1
  41. package/fastify.d.ts +34 -22
  42. package/fastify.js +40 -36
  43. package/lib/configValidator.js +902 -1023
  44. package/lib/contentTypeParser.js +6 -16
  45. package/lib/context.js +36 -10
  46. package/lib/decorate.js +3 -1
  47. package/lib/error-handler.js +158 -0
  48. package/lib/error-serializer.js +257 -0
  49. package/lib/errors.js +43 -9
  50. package/lib/fourOhFour.js +31 -20
  51. package/lib/handleRequest.js +10 -13
  52. package/lib/hooks.js +14 -9
  53. package/lib/pluginOverride.js +0 -3
  54. package/lib/pluginUtils.js +3 -2
  55. package/lib/reply.js +29 -158
  56. package/lib/request.js +13 -10
  57. package/lib/route.js +131 -138
  58. package/lib/schema-controller.js +2 -2
  59. package/lib/schemas.js +27 -1
  60. package/lib/server.js +241 -116
  61. package/lib/symbols.js +4 -3
  62. package/lib/validation.js +2 -1
  63. package/lib/warnings.js +4 -12
  64. package/lib/wrapThenable.js +4 -11
  65. package/package.json +37 -39
  66. package/test/404s.test.js +258 -125
  67. package/test/500s.test.js +3 -3
  68. package/test/als.test.js +1 -1
  69. package/test/async-await.test.js +20 -76
  70. package/test/bodyLimit.test.js +1 -1
  71. package/test/build-certificate.js +6 -7
  72. package/test/case-insensitive.test.js +4 -4
  73. package/test/close-pipelining.test.js +2 -2
  74. package/test/close.test.js +11 -11
  75. package/test/content-parser.test.js +32 -0
  76. package/test/context-config.test.js +52 -0
  77. package/test/custom-http-server.test.js +14 -7
  78. package/test/custom-parser-async.test.js +1 -66
  79. package/test/custom-parser.test.js +92 -159
  80. package/test/custom-querystring-parser.test.js +3 -3
  81. package/test/decorator.test.js +11 -13
  82. package/test/delete.test.js +6 -6
  83. package/test/encapsulated-error-handler.test.js +50 -0
  84. package/test/esm/index.test.js +0 -14
  85. package/test/fastify-instance.test.js +4 -4
  86. package/test/fluent-schema.test.js +4 -4
  87. package/test/genReqId.test.js +1 -1
  88. package/test/get.test.js +4 -4
  89. package/test/handler-context.test.js +2 -2
  90. package/test/head.test.js +1 -1
  91. package/test/helper.js +19 -4
  92. package/test/hooks-async.test.js +15 -48
  93. package/test/hooks.on-ready.test.js +10 -5
  94. package/test/hooks.test.js +78 -119
  95. package/test/http2/closing.test.js +10 -16
  96. package/test/http2/constraint.test.js +1 -1
  97. package/test/http2/head.test.js +1 -1
  98. package/test/http2/plain.test.js +1 -1
  99. package/test/http2/secure-with-fallback.test.js +1 -1
  100. package/test/http2/secure.test.js +1 -1
  101. package/test/http2/unknown-http-method.test.js +4 -10
  102. package/test/https/custom-https-server.test.js +12 -6
  103. package/test/https/https.test.js +1 -1
  104. package/test/input-validation.js +3 -3
  105. package/test/internals/handleRequest.test.js +6 -43
  106. package/test/internals/initialConfig.test.js +41 -12
  107. package/test/internals/logger.test.js +2 -2
  108. package/test/internals/reply.test.js +281 -40
  109. package/test/internals/request.test.js +13 -7
  110. package/test/internals/server.test.js +88 -0
  111. package/test/listen.deprecated.test.js +202 -0
  112. package/test/listen.test.js +118 -150
  113. package/test/logger.test.js +82 -42
  114. package/test/maxRequestsPerSocket.test.js +8 -6
  115. package/test/middleware.test.js +2 -25
  116. package/test/nullable-validation.test.js +53 -16
  117. package/test/output-validation.test.js +1 -1
  118. package/test/plugin.test.js +47 -21
  119. package/test/pretty-print.test.js +22 -10
  120. package/test/promises.test.js +1 -1
  121. package/test/proto-poisoning.test.js +6 -6
  122. package/test/register.test.js +3 -3
  123. package/test/reply-error.test.js +126 -15
  124. package/test/request-error.test.js +3 -6
  125. package/test/route-hooks.test.js +18 -18
  126. package/test/route-prefix.test.js +2 -1
  127. package/test/route.test.js +206 -22
  128. package/test/router-options.test.js +2 -2
  129. package/test/schema-examples.test.js +11 -5
  130. package/test/schema-feature.test.js +25 -20
  131. package/test/schema-serialization.test.js +9 -9
  132. package/test/schema-special-usage.test.js +5 -153
  133. package/test/schema-validation.test.js +9 -9
  134. package/test/skip-reply-send.test.js +2 -2
  135. package/test/stream.test.js +82 -23
  136. package/test/throw.test.js +8 -5
  137. package/test/trust-proxy.test.js +6 -6
  138. package/test/type-provider.test.js +20 -0
  139. package/test/types/fastify.test-d.ts +10 -18
  140. package/test/types/import.js +2 -0
  141. package/test/types/import.ts +1 -0
  142. package/test/types/instance.test-d.ts +68 -17
  143. package/test/types/logger.test-d.ts +44 -15
  144. package/test/types/reply.test-d.ts +2 -1
  145. package/test/types/route.test-d.ts +8 -2
  146. package/test/types/schema.test-d.ts +2 -39
  147. package/test/types/type-provider.test-d.ts +417 -0
  148. package/test/url-rewriting.test.js +3 -3
  149. package/test/validation-error-handling.test.js +8 -8
  150. package/test/versioned-routes.test.js +30 -18
  151. package/test/wrapThenable.test.js +7 -6
  152. package/types/content-type-parser.d.ts +17 -8
  153. package/types/hooks.d.ts +102 -59
  154. package/types/instance.d.ts +244 -118
  155. package/types/logger.d.ts +18 -104
  156. package/types/plugin.d.ts +10 -4
  157. package/types/reply.d.ts +18 -12
  158. package/types/request.d.ts +10 -5
  159. package/types/route.d.ts +42 -31
  160. package/types/schema.d.ts +1 -1
  161. package/types/type-provider.d.ts +99 -0
  162. package/types/utils.d.ts +1 -1
  163. package/lib/schema-compilers.js +0 -12
  164. package/test/emit-warning.test.js +0 -166
@@ -1,166 +0,0 @@
1
- 'use strict'
2
-
3
- const sget = require('simple-get').concat
4
- const { test } = require('tap')
5
- const Fastify = require('..')
6
- const semver = require('semver')
7
-
8
- process.removeAllListeners('warning')
9
-
10
- test('Should emit a warning when accessing request.req instead of request.raw', t => {
11
- t.plan(4)
12
-
13
- process.on('warning', onWarning)
14
- function onWarning (warning) {
15
- t.equal(warning.name, 'FastifyDeprecation')
16
- t.equal(warning.code, 'FSTDEP001')
17
- t.equal(warning.message, 'You are accessing the Node.js core request object via "request.req", Use "request.raw" instead.')
18
- }
19
-
20
- const fastify = Fastify()
21
-
22
- fastify.get('/', (request, reply) => {
23
- reply.send(request.req.method + request.req.method)
24
- })
25
-
26
- fastify.inject({
27
- method: 'GET',
28
- path: '/'
29
- }, (err, res) => {
30
- t.error(err)
31
- process.removeListener('warning', onWarning)
32
- })
33
- })
34
-
35
- test('Should emit a warning when accessing reply.res instead of reply.raw', t => {
36
- t.plan(4)
37
-
38
- process.on('warning', onWarning)
39
- function onWarning (warning) {
40
- t.equal(warning.name, 'FastifyDeprecation')
41
- t.equal(warning.code, 'FSTDEP002')
42
- t.equal(warning.message, 'You are accessing the Node.js core response object via "reply.res", Use "reply.raw" instead.')
43
- }
44
-
45
- const fastify = Fastify()
46
-
47
- fastify.get('/', (request, reply) => {
48
- reply.send(reply.res.statusCode + reply.res.statusCode)
49
- })
50
-
51
- fastify.inject({
52
- method: 'GET',
53
- path: '/'
54
- }, (err, res) => {
55
- t.error(err)
56
- process.removeListener('warning', onWarning)
57
- })
58
- })
59
-
60
- test('Should emit a warning when using two arguments Content Type Parser instead of three arguments', t => {
61
- t.plan(7)
62
-
63
- process.on('warning', onWarning)
64
- function onWarning (warning) {
65
- t.equal(warning.name, 'FastifyDeprecation')
66
- t.equal(warning.code, 'FSTDEP003')
67
- t.equal(warning.message, 'You are using the legacy Content Type Parser function signature. Use the one suggested in the documentation instead.')
68
- }
69
-
70
- const fastify = Fastify()
71
-
72
- fastify.addContentTypeParser('x/foo', function (req, done) {
73
- done(null, 'OK')
74
- })
75
-
76
- fastify.post('/', (request, reply) => {
77
- reply.send(request.body)
78
- })
79
-
80
- fastify.listen(0, err => {
81
- t.error(err)
82
-
83
- sget({
84
- method: 'POST',
85
- url: 'http://localhost:' + fastify.server.address().port,
86
- headers: { 'Content-Type': 'x/foo' },
87
- body: '{"hello":"world"}'
88
- }, (err, response, body) => {
89
- t.error(err)
90
- t.equal(response.statusCode, 200)
91
- t.equal(body.toString(), 'OK')
92
- process.removeListener('warning', onWarning)
93
- fastify.close()
94
- })
95
- })
96
- })
97
-
98
- test('Should emit a warning when using payload less preParsing hook', t => {
99
- t.plan(7)
100
-
101
- process.on('warning', onWarning)
102
- function onWarning (warning) {
103
- t.equal(warning.name, 'FastifyDeprecation')
104
- t.equal(warning.code, 'FSTDEP004')
105
- t.equal(warning.message, 'You are using the legacy preParsing hook signature. Use the one suggested in the documentation instead.')
106
- }
107
-
108
- const fastify = Fastify()
109
-
110
- fastify.addHook('preParsing', function (request, reply, done) {
111
- done()
112
- })
113
-
114
- fastify.get('/', (request, reply) => {
115
- reply.send('OK')
116
- })
117
-
118
- fastify.listen(0, err => {
119
- t.error(err)
120
-
121
- sget({
122
- method: 'GET',
123
- url: 'http://localhost:' + fastify.server.address().port
124
- }, (err, response, body) => {
125
- t.error(err)
126
- t.equal(response.statusCode, 200)
127
- t.equal(body.toString(), 'OK')
128
- process.removeListener('warning', onWarning)
129
- fastify.close()
130
- })
131
- })
132
- })
133
-
134
- test('Should emit a warning when accessing request.connection instead of request.socket on Node process greater than 13.0.0', t => {
135
- t.plan(4)
136
-
137
- process.on('warning', onWarning)
138
- function onWarning (warning) {
139
- if (semver.gte(process.versions.node, '13.0.0')) {
140
- t.equal(warning.name, 'FastifyDeprecation')
141
- t.equal(warning.code, 'FSTDEP005')
142
- t.equal(warning.message, 'You are accessing the deprecated "request.connection" property. Use "request.socket" instead.')
143
- } else {
144
- t.equal(warning.name, 'FastifyDeprecationLightMyRequest')
145
- t.equal(warning.code, 'FST_LIGHTMYREQUEST_DEP01')
146
- t.equal(warning.message, 'You are accessing "request.connection", use "request.socket" instead.')
147
- }
148
-
149
- // removed listener before light-my-request emit second warning
150
- process.removeListener('warning', onWarning)
151
- }
152
-
153
- const fastify = Fastify()
154
-
155
- fastify.get('/', (request, reply) => {
156
- reply.send(request.connection)
157
- })
158
-
159
- fastify.inject({
160
- method: 'GET',
161
- path: '/'
162
- }, (err, res) => {
163
- t.error(err)
164
- process.removeListener('warning', onWarning)
165
- })
166
- })