fastify 4.0.0-alpha.1 → 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 (101) hide show
  1. package/.taprc +3 -0
  2. package/README.md +2 -3
  3. package/docs/Guides/Database.md +320 -0
  4. package/docs/Guides/Getting-Started.md +7 -7
  5. package/docs/Guides/Plugins-Guide.md +1 -1
  6. package/docs/Guides/Serverless.md +3 -3
  7. package/docs/Guides/Testing.md +2 -2
  8. package/docs/Reference/Decorators.md +2 -2
  9. package/docs/Reference/Encapsulation.md +2 -2
  10. package/docs/Reference/HTTP2.md +3 -3
  11. package/docs/Reference/Plugins.md +3 -3
  12. package/docs/Reference/Routes.md +5 -5
  13. package/docs/Reference/Server.md +27 -62
  14. package/docs/Reference/TypeScript.md +9 -9
  15. package/docs/Reference/Validation-and-Serialization.md +4 -4
  16. package/docs/Type-Providers.md +2 -2
  17. package/examples/asyncawait.js +1 -1
  18. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  19. package/examples/benchmark/hooks-benchmark.js +1 -1
  20. package/examples/benchmark/simple.js +1 -1
  21. package/examples/hooks.js +1 -1
  22. package/examples/http2.js +1 -1
  23. package/examples/https.js +1 -1
  24. package/examples/parser.js +1 -1
  25. package/examples/route-prefix.js +1 -1
  26. package/examples/shared-schema.js +1 -1
  27. package/examples/simple-stream.js +1 -1
  28. package/examples/simple.js +1 -1
  29. package/examples/simple.mjs +1 -1
  30. package/examples/typescript-server.ts +1 -1
  31. package/examples/use-plugin.js +1 -1
  32. package/fastify.js +4 -2
  33. package/lib/reply.js +1 -1
  34. package/lib/server.js +32 -10
  35. package/lib/warnings.js +2 -0
  36. package/package.json +13 -11
  37. package/test/404s.test.js +16 -16
  38. package/test/500s.test.js +1 -1
  39. package/test/als.test.js +1 -1
  40. package/test/async-await.test.js +7 -7
  41. package/test/bodyLimit.test.js +1 -1
  42. package/test/build-certificate.js +6 -7
  43. package/test/case-insensitive.test.js +4 -4
  44. package/test/close-pipelining.test.js +2 -2
  45. package/test/close.test.js +11 -11
  46. package/test/custom-http-server.test.js +1 -1
  47. package/test/custom-parser-async.test.js +1 -1
  48. package/test/custom-parser.test.js +38 -38
  49. package/test/custom-querystring-parser.test.js +3 -3
  50. package/test/decorator.test.js +10 -10
  51. package/test/delete.test.js +1 -1
  52. package/test/genReqId.test.js +1 -1
  53. package/test/get.test.js +1 -1
  54. package/test/handler-context.test.js +2 -2
  55. package/test/head.test.js +1 -1
  56. package/test/helper.js +1 -1
  57. package/test/hooks-async.test.js +1 -1
  58. package/test/hooks.on-ready.test.js +1 -1
  59. package/test/hooks.test.js +20 -20
  60. package/test/http2/closing.test.js +5 -5
  61. package/test/http2/constraint.test.js +1 -1
  62. package/test/http2/head.test.js +1 -1
  63. package/test/http2/plain.test.js +1 -1
  64. package/test/http2/secure-with-fallback.test.js +1 -1
  65. package/test/http2/secure.test.js +1 -1
  66. package/test/http2/unknown-http-method.test.js +1 -1
  67. package/test/https/custom-https-server.test.js +1 -1
  68. package/test/https/https.test.js +1 -1
  69. package/test/input-validation.js +1 -1
  70. package/test/internals/handleRequest.test.js +3 -3
  71. package/test/internals/initialConfig.test.js +9 -1
  72. package/test/internals/logger.test.js +2 -2
  73. package/test/internals/reply.test.js +41 -42
  74. package/test/internals/server.test.js +5 -5
  75. package/test/listen.deprecated.test.js +202 -0
  76. package/test/listen.test.js +41 -156
  77. package/test/logger.test.js +15 -15
  78. package/test/maxRequestsPerSocket.test.js +2 -2
  79. package/test/nullable-validation.test.js +3 -3
  80. package/test/output-validation.test.js +1 -1
  81. package/test/plugin.test.js +16 -16
  82. package/test/promises.test.js +1 -1
  83. package/test/proto-poisoning.test.js +6 -6
  84. package/test/register.test.js +3 -3
  85. package/test/reply-error.test.js +3 -3
  86. package/test/request-error.test.js +1 -1
  87. package/test/route-hooks.test.js +1 -1
  88. package/test/route.test.js +3 -3
  89. package/test/router-options.test.js +1 -1
  90. package/test/schema-feature.test.js +1 -1
  91. package/test/schema-special-usage.test.js +0 -81
  92. package/test/skip-reply-send.test.js +1 -1
  93. package/test/stream.test.js +60 -12
  94. package/test/trust-proxy.test.js +6 -6
  95. package/test/types/instance.test-d.ts +33 -3
  96. package/test/types/reply.test-d.ts +2 -1
  97. package/test/url-rewriting.test.js +3 -3
  98. package/test/versioned-routes.test.js +3 -3
  99. package/types/.eslintrc.json +3 -1
  100. package/types/instance.d.ts +120 -14
  101. package/types/reply.d.ts +2 -1
@@ -35,7 +35,7 @@ test('should respond with a stream', t => {
35
35
  reply.code(200).send(stream)
36
36
  })
37
37
 
38
- fastify.listen(0, err => {
38
+ fastify.listen({ port: 0 }, err => {
39
39
  t.error(err)
40
40
  fastify.server.unref()
41
41
 
@@ -61,7 +61,7 @@ test('should respond with a stream (error)', t => {
61
61
  reply.code(200).send(stream)
62
62
  })
63
63
 
64
- fastify.listen(0, err => {
64
+ fastify.listen({ port: 0 }, err => {
65
65
  t.error(err)
66
66
  fastify.server.unref()
67
67
 
@@ -116,7 +116,7 @@ test('should trigger the onSend hook only twice if pumping the stream fails, fir
116
116
  done()
117
117
  })
118
118
 
119
- fastify.listen(0, err => {
119
+ fastify.listen({ port: 0 }, err => {
120
120
  t.error(err)
121
121
 
122
122
  fastify.server.unref()
@@ -279,7 +279,7 @@ test('Destroying streams prematurely', t => {
279
279
  reply.send(reallyLongStream)
280
280
  })
281
281
 
282
- fastify.listen(0, err => {
282
+ fastify.listen({ port: 0 }, err => {
283
283
  t.error(err)
284
284
  fastify.server.unref()
285
285
 
@@ -342,7 +342,7 @@ test('Destroying streams prematurely should call close method', t => {
342
342
  reply.send(reallyLongStream)
343
343
  })
344
344
 
345
- fastify.listen(0, err => {
345
+ fastify.listen({ port: 0 }, err => {
346
346
  t.error(err)
347
347
  fastify.server.unref()
348
348
 
@@ -404,7 +404,7 @@ test('Destroying streams prematurely should call close method when destroy is no
404
404
  reply.send(reallyLongStream)
405
405
  })
406
406
 
407
- fastify.listen(0, err => {
407
+ fastify.listen({ port: 0 }, err => {
408
408
  t.error(err)
409
409
  fastify.server.unref()
410
410
 
@@ -467,7 +467,7 @@ test('Destroying streams prematurely should call abort method', t => {
467
467
  reply.send(reallyLongStream)
468
468
  })
469
469
 
470
- fastify.listen(0, err => {
470
+ fastify.listen({ port: 0 }, err => {
471
471
  t.error(err)
472
472
  fastify.server.unref()
473
473
 
@@ -517,7 +517,7 @@ test('Destroying streams prematurely, log is disabled', t => {
517
517
  reply.send(reallyLongStream)
518
518
  })
519
519
 
520
- fastify.listen(0, err => {
520
+ fastify.listen({ port: 0 }, err => {
521
521
  t.error(err)
522
522
  fastify.server.unref()
523
523
 
@@ -547,7 +547,7 @@ test('should respond with a stream1', t => {
547
547
  stream.end({ a: 42 })
548
548
  })
549
549
 
550
- fastify.listen(0, err => {
550
+ fastify.listen({ port: 0 }, err => {
551
551
  t.error(err)
552
552
  fastify.server.unref()
553
553
 
@@ -579,7 +579,7 @@ test('return a 404 if the stream emits a 404 error', t => {
579
579
  reply.send(reallyLongStream)
580
580
  })
581
581
 
582
- fastify.listen(0, err => {
582
+ fastify.listen({ port: 0 }, err => {
583
583
  t.error(err)
584
584
  fastify.server.unref()
585
585
 
@@ -607,7 +607,7 @@ test('should support send module 200 and 404', { skip: semver.gte(process.versio
607
607
  reply.code(200).send(stream)
608
608
  })
609
609
 
610
- fastify.listen(0, err => {
610
+ fastify.listen({ port: 0 }, err => {
611
611
  t.error(err)
612
612
  fastify.server.unref()
613
613
 
@@ -648,7 +648,7 @@ test('should destroy stream when response is ended', t => {
648
648
  reply.raw.end(Buffer.from('hello\n'))
649
649
  })
650
650
 
651
- fastify.listen(0, err => {
651
+ fastify.listen({ port: 0 }, err => {
652
652
  t.error(err)
653
653
  fastify.server.unref()
654
654
 
@@ -694,3 +694,51 @@ test('should mark reply as sent before pumping the payload stream into response
694
694
  fastify.close()
695
695
  })
696
696
  })
697
+
698
+ test('reply.send handles aborted requests', t => {
699
+ t.plan(2)
700
+
701
+ const spyLogger = {
702
+ level: 'error',
703
+ fatal: () => { },
704
+ error: () => {
705
+ t.fail('should not log an error')
706
+ },
707
+ warn: () => { },
708
+ info: () => { },
709
+ debug: () => { },
710
+ trace: () => { },
711
+ child: () => { return spyLogger }
712
+ }
713
+ const fastify = Fastify({
714
+ logger: spyLogger
715
+ })
716
+
717
+ fastify.get('/', (req, reply) => {
718
+ setTimeout(() => {
719
+ const stream = new Readable({
720
+ read: function () {
721
+ this.push(null)
722
+ }
723
+ })
724
+ reply.send(stream)
725
+ }, 6)
726
+ })
727
+
728
+ fastify.listen({ port: 0 }, err => {
729
+ t.error(err)
730
+ fastify.server.unref()
731
+
732
+ const port = fastify.server.address().port
733
+ const http = require('http')
734
+ const req = http.get(`http://localhost:${port}`)
735
+ .on('error', (err) => {
736
+ t.equal(err.code, 'ECONNRESET')
737
+ fastify.close()
738
+ })
739
+
740
+ setTimeout(() => {
741
+ req.abort()
742
+ }, 1)
743
+ })
744
+ })
@@ -63,7 +63,7 @@ test('trust proxy, not add properties to node req', (t) => {
63
63
 
64
64
  t.teardown(app.close.bind(app))
65
65
 
66
- app.listen(0, (err) => {
66
+ app.listen({ port: 0 }, (err) => {
67
67
  app.server.unref()
68
68
  t.error(err)
69
69
  sgetForwardedRequest(app, '1.1.1.1', '/trustproxy')
@@ -84,7 +84,7 @@ test('trust proxy chain', (t) => {
84
84
 
85
85
  t.teardown(app.close.bind(app))
86
86
 
87
- app.listen(0, (err) => {
87
+ app.listen({ port: 0 }, (err) => {
88
88
  app.server.unref()
89
89
  t.error(err)
90
90
  sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain')
@@ -103,7 +103,7 @@ test('trust proxy function', (t) => {
103
103
 
104
104
  t.teardown(app.close.bind(app))
105
105
 
106
- app.listen(0, (err) => {
106
+ app.listen({ port: 0 }, (err) => {
107
107
  app.server.unref()
108
108
  t.error(err)
109
109
  sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc')
@@ -122,7 +122,7 @@ test('trust proxy number', (t) => {
122
122
 
123
123
  t.teardown(app.close.bind(app))
124
124
 
125
- app.listen(0, (err) => {
125
+ app.listen({ port: 0 }, (err) => {
126
126
  app.server.unref()
127
127
  t.error(err)
128
128
  sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber')
@@ -141,7 +141,7 @@ test('trust proxy IP addresses', (t) => {
141
141
 
142
142
  t.teardown(app.close.bind(app))
143
143
 
144
- app.listen(0, (err) => {
144
+ app.listen({ port: 0 }, (err) => {
145
145
  app.server.unref()
146
146
  t.error(err)
147
147
  sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs')
@@ -168,7 +168,7 @@ test('trust proxy protocol', (t) => {
168
168
 
169
169
  t.teardown(app.close.bind(app))
170
170
 
171
- app.listen(0, (err) => {
171
+ app.listen({ port: 0 }, (err) => {
172
172
  app.server.unref()
173
173
  t.error(err)
174
174
  sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocol', 'lorem')
@@ -1,3 +1,4 @@
1
+ import { expectAssignable, expectDeprecated, expectError, expectNotDeprecated, expectType } from 'tsd'
1
2
  import fastify, {
2
3
  FastifyBodyParser,
3
4
  FastifyError,
@@ -6,10 +7,9 @@ import fastify, {
6
7
  RawRequestDefaultExpression,
7
8
  RawServerDefault
8
9
  } from '../../fastify'
9
- import { expectAssignable, expectError, expectType } from 'tsd'
10
- import { FastifyRequest } from '../../types/request'
11
- import { FastifyReply } from '../../types/reply'
12
10
  import { HookHandlerDoneFunction } from '../../types/hooks'
11
+ import { FastifyReply } from '../../types/reply'
12
+ import { FastifyRequest } from '../../types/request'
13
13
  import { FastifySchemaControllerOptions } from '../../types/schema'
14
14
 
15
15
  const server = fastify()
@@ -194,13 +194,43 @@ expectAssignable<PromiseLike<string>>(server.listen('3000', '', 0))
194
194
  expectAssignable<PromiseLike<string>>(server.listen(3000, ''))
195
195
  expectAssignable<PromiseLike<string>>(server.listen('3000', ''))
196
196
 
197
+ // Test variadic listen signatures Typescript deprecation
198
+ expectDeprecated(server.listen(3000))
199
+ expectDeprecated(server.listen('3000'))
200
+ expectDeprecated(server.listen(3000, '', 0))
201
+ expectDeprecated(server.listen('3000', '', 0))
202
+ expectDeprecated(server.listen(3000, ''))
203
+ expectDeprecated(server.listen('3000', ''))
204
+
197
205
  // test listen opts objects
206
+ expectAssignable<PromiseLike<string>>(server.listen())
198
207
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000 }))
199
208
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0' }))
200
209
  expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }))
210
+ expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }))
211
+ expectAssignable<PromiseLike<string>>(server.listen({ port: 3000, host: '::/0', ipv6Only: true }))
212
+
213
+ expectAssignable<void>(server.listen(() => {}))
201
214
  expectAssignable<void>(server.listen({ port: 3000 }, () => {}))
202
215
  expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0' }, () => {}))
203
216
  expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }, () => {}))
217
+ expectAssignable<void>(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }, () => {}))
218
+ expectAssignable<void>(server.listen({ port: 3000, host: '::/0', ipv6Only: true }, () => {}))
219
+
220
+ // test listen opts objects Typescript deprectation exclusion
221
+ expectNotDeprecated(server.listen())
222
+ expectNotDeprecated(server.listen({ port: 3000 }))
223
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0' }))
224
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }))
225
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }))
226
+ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true }))
227
+
228
+ expectNotDeprecated(server.listen(() => {}))
229
+ expectNotDeprecated(server.listen({ port: 3000 }, () => {}))
230
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0' }, () => {}))
231
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42 }, () => {}))
232
+ expectNotDeprecated(server.listen({ port: 3000, host: '0.0.0.0', backlog: 42, exclusive: true }, () => {}))
233
+ expectNotDeprecated(server.listen({ port: 3000, host: '::/0', ipv6Only: true }, () => {}))
204
234
 
205
235
  expectAssignable<void>(server.routing({} as RawRequestDefaultExpression, {} as RawReplyDefaultExpression))
206
236
 
@@ -4,6 +4,7 @@ import { RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } fro
4
4
  import { FastifyLoggerInstance } from '../../types/logger'
5
5
  import { RouteGenericInterface } from '../../types/route'
6
6
  import { FastifyInstance } from '../../types/instance'
7
+ import { Buffer } from 'buffer'
7
8
 
8
9
  const getHandler: RouteHandlerMethod = function (_request, reply) {
9
10
  expectType<RawReplyDefaultExpression>(reply.raw)
@@ -28,7 +29,7 @@ const getHandler: RouteHandlerMethod = function (_request, reply) {
28
29
  expectType<() => number>(reply.getResponseTime)
29
30
  expectType<(contentType: string) => FastifyReply>(reply.type)
30
31
  expectType<(fn: (payload: any) => string) => FastifyReply>(reply.serializer)
31
- expectType<(payload: any) => string>(reply.serialize)
32
+ expectType<(payload: any) => string | ArrayBuffer | Buffer>(reply.serialize)
32
33
  expectType<(fulfilled: () => void, rejected: (err: Error) => void) => void>(reply.then)
33
34
  expectType<FastifyInstance>(reply.server)
34
35
  }
@@ -22,7 +22,7 @@ test('Should rewrite url', t => {
22
22
  }
23
23
  })
24
24
 
25
- fastify.listen(0, function (err) {
25
+ fastify.listen({ port: 0 }, function (err) {
26
26
  t.error(err)
27
27
 
28
28
  sget({
@@ -55,7 +55,7 @@ test('Should not rewrite if the url is the same', t => {
55
55
  }
56
56
  })
57
57
 
58
- fastify.listen(0, function (err) {
58
+ fastify.listen({ port: 0 }, function (err) {
59
59
  t.error(err)
60
60
 
61
61
  sget({
@@ -86,7 +86,7 @@ test('Should throw an error', t => {
86
86
  }
87
87
  })
88
88
 
89
- fastify.listen(0, function (err) {
89
+ fastify.listen({ port: 0 }, function (err) {
90
90
  t.error(err)
91
91
 
92
92
  sget({
@@ -242,7 +242,7 @@ test('Should register a versioned route', t => {
242
242
  }
243
243
  })
244
244
 
245
- fastify.listen(0, err => {
245
+ fastify.listen({ port: 0 }, err => {
246
246
  t.error(err)
247
247
  fastify.server.unref()
248
248
 
@@ -407,7 +407,7 @@ test('Bas accept version (server)', t => {
407
407
  }
408
408
  })
409
409
 
410
- fastify.listen(0, err => {
410
+ fastify.listen({ port: 0 }, err => {
411
411
  t.error(err)
412
412
  fastify.server.unref()
413
413
 
@@ -449,7 +449,7 @@ test('test log stream', t => {
449
449
  reply.send(new Error('kaboom'))
450
450
  })
451
451
 
452
- fastify.listen(0, localhost, err => {
452
+ fastify.listen({ port: 0, host: localhost }, err => {
453
453
  t.error(err)
454
454
  fastify.server.unref()
455
455
 
@@ -36,7 +36,9 @@
36
36
  "@typescript-eslint/explicit-function-return-type": "off",
37
37
  "@typescript-eslint/no-unused-vars": "off",
38
38
  "@typescript-eslint/no-non-null-assertion": "off",
39
- "@typescript-eslint/no-misused-promises": ["error"]
39
+ "@typescript-eslint/no-misused-promises": ["error", {
40
+ "checksVoidReturn": false
41
+ }]
40
42
  },
41
43
  "globals": {
42
44
  "NodeJS": "readonly"
@@ -1,21 +1,21 @@
1
- import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
2
- import { RouteOptions, RouteShorthandMethod, RouteGenericInterface, DefaultRoute } from './route'
1
+ import { FastifyError } from 'fastify-error'
2
+ import { CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request'
3
+ import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, getDefaultJsonParser, hasContentTypeParser, ProtoAction, removeAllContentTypeParsers, removeContentTypeParser } from './content-type-parser'
4
+ import { onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler } from './hooks'
5
+ import { FastifyLoggerInstance } from './logger'
6
+ import { FastifyRegister } from './register'
7
+ import { FastifyReply } from './reply'
8
+ import { FastifyRequest } from './request'
9
+ import { DefaultRoute, RouteGenericInterface, RouteOptions, RouteShorthandMethod } from './route'
3
10
  import {
4
11
  FastifySchema,
5
12
  FastifySchemaCompiler,
13
+ FastifySchemaControllerOptions,
6
14
  FastifySchemaValidationError,
7
- FastifySerializerCompiler,
8
- FastifySchemaControllerOptions
15
+ FastifySerializerCompiler
9
16
  } from './schema'
10
- import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
11
- import { FastifyLoggerInstance } from './logger'
12
- import { FastifyRegister } from './register'
13
- import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onCloseAsyncHookHandler, onReadyHookHandler, onTimeoutHookHandler, preParsingAsyncHookHandler, preValidationAsyncHookHandler, preHandlerAsyncHookHandler, preSerializationAsyncHookHandler, onSendAsyncHookHandler, onResponseAsyncHookHandler, onTimeoutAsyncHookHandler, onErrorAsyncHookHandler, onReadyAsyncHookHandler, onRequestAsyncHookHandler } from './hooks'
14
- import { FastifyRequest } from './request'
15
- import { FastifyReply } from './reply'
16
- import { FastifyError } from 'fastify-error'
17
- import { AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, FastifyBodyParser, removeContentTypeParser, removeAllContentTypeParsers } from './content-type-parser'
18
17
  import { FastifyTypeProvider, FastifyTypeProviderDefault } from './type-provider'
18
+ import { ContextConfigDefault, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
19
19
 
20
20
  export interface PrintRoutesOptions {
21
21
  includeMeta?: boolean | (string | symbol)[]
@@ -82,12 +82,118 @@ export interface FastifyInstance<
82
82
  inject(opts: InjectOptions | string): Promise<LightMyRequestResponse>;
83
83
  inject(): LightMyRequestChain;
84
84
 
85
+ listen(opts: {
86
+ /**
87
+ * Default to `0` (picks the first available open port).
88
+ */
89
+ port?: number;
90
+ /**
91
+ * Default to `localhost`.
92
+ */
93
+ host?: string;
94
+ /**
95
+ * Will be ignored if `port` is specified.
96
+ * @see [Identifying paths for IPC connections](https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections).
97
+ */
98
+ path?: string;
99
+ /**
100
+ * Specify the maximum length of the queue of pending connections.
101
+ * The actual length will be determined by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn` on Linux.
102
+ * Default to `511`.
103
+ */
104
+ backlog?: number;
105
+ /**
106
+ * Default to `false`.
107
+ */
108
+ exclusive?: boolean;
109
+ /**
110
+ * For IPC servers makes the pipe readable for all users.
111
+ * Default to `false`.
112
+ */
113
+ readableAll?: boolean;
114
+ /**
115
+ * For IPC servers makes the pipe writable for all users.
116
+ * Default to `false`.
117
+ */
118
+ writableAll?: boolean;
119
+ /**
120
+ * For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound.
121
+ * Default to `false`.
122
+ */
123
+ ipv6Only?: boolean;
124
+ /**
125
+ * An AbortSignal that may be used to close a listening server.
126
+ * @since This option is available only in Node.js v15.6.0 and greater
127
+ */
128
+ signal?: AbortSignal;
129
+ }, callback: (err: Error|null, address: string) => void): void;
130
+ listen(opts?: {
131
+ /**
132
+ * Default to `0` (picks the first available open port).
133
+ */
134
+ port?: number;
135
+ /**
136
+ * Default to `localhost`.
137
+ */
138
+ host?: string;
139
+ /**
140
+ * Will be ignored if `port` is specified.
141
+ * @see [Identifying paths for IPC connections](https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections).
142
+ */
143
+ path?: string;
144
+ /**
145
+ * Specify the maximum length of the queue of pending connections.
146
+ * The actual length will be determined by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn` on Linux.
147
+ * Default to `511`.
148
+ */
149
+ backlog?: number;
150
+ /**
151
+ * Default to `false`.
152
+ */
153
+ exclusive?: boolean;
154
+ /**
155
+ * For IPC servers makes the pipe readable for all users.
156
+ * Default to `false`.
157
+ */
158
+ readableAll?: boolean;
159
+ /**
160
+ * For IPC servers makes the pipe writable for all users.
161
+ * Default to `false`.
162
+ */
163
+ writableAll?: boolean;
164
+ /**
165
+ * For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound.
166
+ * Default to `false`.
167
+ */
168
+ ipv6Only?: boolean;
169
+ /**
170
+ * An AbortSignal that may be used to close a listening server.
171
+ * @since This option is available only in Node.js v15.6.0 and greater
172
+ */
173
+ signal?: AbortSignal;
174
+ }): Promise<string>;
175
+ listen(callback: (err: Error | null, address: string) => void): void;
176
+
177
+ /**
178
+ * @deprecated Variadic listen method is deprecated. Please use `.listen(optionsObject, callback)` instead. The variadic signature will be removed in `fastify@5`
179
+ * @see https://github.com/fastify/fastify/pull/3712
180
+ */
85
181
  listen(port: number | string, address: string, backlog: number, callback: (err: Error|null, address: string) => void): void;
182
+ /**
183
+ * @deprecated Variadic listen method is deprecated. Please use `.listen(optionsObject, callback)` instead. The variadic signature will be removed in `fastify@5`
184
+ * @see https://github.com/fastify/fastify/pull/3712
185
+ */
86
186
  listen(port: number | string, address: string, callback: (err: Error|null, address: string) => void): void;
187
+ /**
188
+ * @deprecated Variadic listen method is deprecated. Please use `.listen(optionsObject, callback)` instead. The variadic signature will be removed in `fastify@5`
189
+ * @see https://github.com/fastify/fastify/pull/3712
190
+ */
87
191
  listen(port: number | string, callback: (err: Error|null, address: string) => void): void;
192
+ /**
193
+ * @deprecated Variadic listen method is deprecated. Please use `.listen(optionsObject)` instead. The variadic signature will be removed in `fastify@5`
194
+ * @see https://github.com/fastify/fastify/pull/3712
195
+ */
88
196
  listen(port: number | string, address?: string, backlog?: number): Promise<string>;
89
- listen(opts: { port: number; host?: string; backlog?: number }, callback: (err: Error|null, address: string) => void): void;
90
- listen(opts: { port: number; host?: string; backlog?: number }): Promise<string>;
91
197
 
92
198
  ready(): FastifyInstance<RawServer, RawRequest, RawReply, FastifyLoggerInstance, TypeProvider> & PromiseLike<undefined>;
93
199
  ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
package/types/reply.d.ts CHANGED
@@ -6,6 +6,7 @@ import { FastifyRequest } from './request'
6
6
  import { RouteGenericInterface } from './route'
7
7
  import { FastifyInstance } from './instance'
8
8
  import { FastifySchema } from './schema'
9
+ import { Buffer } from 'buffer'
9
10
 
10
11
  export interface ReplyGenericInterface {
11
12
  Reply?: ReplyDefault;
@@ -52,6 +53,6 @@ export interface FastifyReply<
52
53
  getResponseTime(): number;
53
54
  type(contentType: string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
54
55
  serializer(fn: (payload: any) => string): FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>;
55
- serialize(payload: any): string;
56
+ serialize(payload: any): string | ArrayBuffer | Buffer;
56
57
  then(fulfilled: () => void, rejected: (err: Error) => void): void;
57
58
  }