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.
- package/.taprc +3 -0
- package/README.md +2 -3
- package/docs/Guides/Database.md +320 -0
- package/docs/Guides/Getting-Started.md +7 -7
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Serverless.md +3 -3
- package/docs/Guides/Testing.md +2 -2
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Encapsulation.md +2 -2
- package/docs/Reference/HTTP2.md +3 -3
- package/docs/Reference/Plugins.md +3 -3
- package/docs/Reference/Routes.md +5 -5
- package/docs/Reference/Server.md +27 -62
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +4 -4
- package/docs/Type-Providers.md +2 -2
- package/examples/asyncawait.js +1 -1
- package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
- package/examples/benchmark/hooks-benchmark.js +1 -1
- package/examples/benchmark/simple.js +1 -1
- package/examples/hooks.js +1 -1
- package/examples/http2.js +1 -1
- package/examples/https.js +1 -1
- package/examples/parser.js +1 -1
- package/examples/route-prefix.js +1 -1
- package/examples/shared-schema.js +1 -1
- package/examples/simple-stream.js +1 -1
- package/examples/simple.js +1 -1
- package/examples/simple.mjs +1 -1
- package/examples/typescript-server.ts +1 -1
- package/examples/use-plugin.js +1 -1
- package/fastify.js +4 -2
- package/lib/reply.js +1 -1
- package/lib/server.js +32 -10
- package/lib/warnings.js +2 -0
- package/package.json +13 -11
- package/test/404s.test.js +16 -16
- package/test/500s.test.js +1 -1
- package/test/als.test.js +1 -1
- package/test/async-await.test.js +7 -7
- package/test/bodyLimit.test.js +1 -1
- package/test/build-certificate.js +6 -7
- package/test/case-insensitive.test.js +4 -4
- package/test/close-pipelining.test.js +2 -2
- package/test/close.test.js +11 -11
- package/test/custom-http-server.test.js +1 -1
- package/test/custom-parser-async.test.js +1 -1
- package/test/custom-parser.test.js +38 -38
- package/test/custom-querystring-parser.test.js +3 -3
- package/test/decorator.test.js +10 -10
- package/test/delete.test.js +1 -1
- package/test/genReqId.test.js +1 -1
- package/test/get.test.js +1 -1
- package/test/handler-context.test.js +2 -2
- package/test/head.test.js +1 -1
- package/test/helper.js +1 -1
- package/test/hooks-async.test.js +1 -1
- package/test/hooks.on-ready.test.js +1 -1
- package/test/hooks.test.js +20 -20
- package/test/http2/closing.test.js +5 -5
- package/test/http2/constraint.test.js +1 -1
- package/test/http2/head.test.js +1 -1
- package/test/http2/plain.test.js +1 -1
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/http2/secure.test.js +1 -1
- package/test/http2/unknown-http-method.test.js +1 -1
- package/test/https/custom-https-server.test.js +1 -1
- package/test/https/https.test.js +1 -1
- package/test/input-validation.js +1 -1
- package/test/internals/handleRequest.test.js +3 -3
- package/test/internals/initialConfig.test.js +9 -1
- package/test/internals/logger.test.js +2 -2
- package/test/internals/reply.test.js +41 -42
- package/test/internals/server.test.js +5 -5
- package/test/listen.deprecated.test.js +202 -0
- package/test/listen.test.js +41 -156
- package/test/logger.test.js +15 -15
- package/test/maxRequestsPerSocket.test.js +2 -2
- package/test/nullable-validation.test.js +3 -3
- package/test/output-validation.test.js +1 -1
- package/test/plugin.test.js +16 -16
- package/test/promises.test.js +1 -1
- package/test/proto-poisoning.test.js +6 -6
- package/test/register.test.js +3 -3
- package/test/reply-error.test.js +3 -3
- package/test/request-error.test.js +1 -1
- package/test/route-hooks.test.js +1 -1
- package/test/route.test.js +3 -3
- package/test/router-options.test.js +1 -1
- package/test/schema-feature.test.js +1 -1
- package/test/schema-special-usage.test.js +0 -81
- package/test/skip-reply-send.test.js +1 -1
- package/test/stream.test.js +60 -12
- package/test/trust-proxy.test.js +6 -6
- package/test/types/instance.test-d.ts +33 -3
- package/test/types/reply.test-d.ts +2 -1
- package/test/url-rewriting.test.js +3 -3
- package/test/versioned-routes.test.js +3 -3
- package/types/.eslintrc.json +3 -1
- package/types/instance.d.ts +120 -14
- package/types/reply.d.ts +2 -1
package/docs/Reference/Server.md
CHANGED
|
@@ -353,7 +353,7 @@ fastify.get('/', (req, reply) => {
|
|
|
353
353
|
reply.send({ hello: 'world' })
|
|
354
354
|
})
|
|
355
355
|
|
|
356
|
-
fastify.listen(3000)
|
|
356
|
+
fastify.listen({ port: 3000 })
|
|
357
357
|
```
|
|
358
358
|
|
|
359
359
|
Internally Fastify uses the API of Node core HTTP server, so if you are using a
|
|
@@ -514,7 +514,8 @@ derive <code>request.hostname</code> and <code>request.protocol</code>**
|
|
|
514
514
|
|
|
515
515
|
The maximum amount of time in *milliseconds* in which a plugin can load. If not,
|
|
516
516
|
[`ready`](#ready) will complete with an `Error` with code
|
|
517
|
-
`'ERR_AVVIO_PLUGIN_TIMEOUT'`.
|
|
517
|
+
`'ERR_AVVIO_PLUGIN_TIMEOUT'`. When set to `0`, disables this check. This controls
|
|
518
|
+
[avvio](https://www.npmjs.com/package/avvio) 's `timeout` parameter.
|
|
518
519
|
|
|
519
520
|
+ Default: `10000`
|
|
520
521
|
|
|
@@ -820,12 +821,17 @@ fastify.ready().then(() => {
|
|
|
820
821
|
#### listen
|
|
821
822
|
<a id="listen"></a>
|
|
822
823
|
|
|
823
|
-
Starts the server
|
|
824
|
-
|
|
824
|
+
Starts the server and internally waits for the the `.ready()` event. The
|
|
825
|
+
signature is `.listen([options][, callback])`. Both the `options` object and the
|
|
826
|
+
`callback` parameters follow the [Node.js
|
|
827
|
+
core][https://nodejs.org/api/net.html#serverlistenoptions-callback] parameter
|
|
828
|
+
definitions.
|
|
825
829
|
|
|
826
830
|
By default, the server will listen on the address(es) resolved by `localhost` when no
|
|
827
|
-
specific
|
|
831
|
+
specific host is provided. If listening on any available interface is desired,
|
|
828
832
|
then specifying `0.0.0.0` for the address will listen on all IPv4 addresses.
|
|
833
|
+
The following table details the possible values for `host` when targeting
|
|
834
|
+
`localhost`, and what the result of those values for `host` will be.
|
|
829
835
|
|
|
830
836
|
Host | IPv4 | IPv6
|
|
831
837
|
--------------|------|-------
|
|
@@ -836,25 +842,19 @@ then specifying `0.0.0.0` for the address will listen on all IPv4 addresses.
|
|
|
836
842
|
`127.0.0.1` | ✅ | 🚫
|
|
837
843
|
`::1` | 🚫 | ✅
|
|
838
844
|
|
|
839
|
-
<sup>*</sup> Using `::` for the address will listen on all IPv6 addresses and,
|
|
840
|
-
may also listen on [all IPv4
|
|
845
|
+
<sup>*</sup> Using `::` for the address will listen on all IPv6 addresses and,
|
|
846
|
+
depending on OS, may also listen on [all IPv4
|
|
847
|
+
addresses](https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback).
|
|
841
848
|
|
|
842
|
-
Be careful when deciding to listen on all interfaces; it comes with inherent
|
|
849
|
+
Be careful when deciding to listen on all interfaces; it comes with inherent
|
|
850
|
+
[security
|
|
843
851
|
risks](https://web.archive.org/web/20170831174611/https://snyk.io/blog/mongodb-hack-and-secure-defaults/).
|
|
844
852
|
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
if (err) {
|
|
848
|
-
fastify.log.error(err)
|
|
849
|
-
process.exit(1)
|
|
850
|
-
}
|
|
851
|
-
})
|
|
852
|
-
```
|
|
853
|
-
|
|
854
|
-
Specifying an address is also supported:
|
|
853
|
+
The default is to listen on `port: 0` (which picks the first available open
|
|
854
|
+
port) and `host: 'localhost'`:
|
|
855
855
|
|
|
856
856
|
```js
|
|
857
|
-
fastify.listen(
|
|
857
|
+
fastify.listen((err, address) => {
|
|
858
858
|
if (err) {
|
|
859
859
|
fastify.log.error(err)
|
|
860
860
|
process.exit(1)
|
|
@@ -862,23 +862,10 @@ fastify.listen(3000, '127.0.0.1', (err, address) => {
|
|
|
862
862
|
})
|
|
863
863
|
```
|
|
864
864
|
|
|
865
|
-
Specifying
|
|
866
|
-
|
|
867
|
-
```js
|
|
868
|
-
fastify.listen(3000, '127.0.0.1', 511, (err, address) => {
|
|
869
|
-
if (err) {
|
|
870
|
-
fastify.log.error(err)
|
|
871
|
-
process.exit(1)
|
|
872
|
-
}
|
|
873
|
-
})
|
|
874
|
-
```
|
|
875
|
-
|
|
876
|
-
Specifying options is also supported; the object is same as
|
|
877
|
-
[options](https://nodejs.org/api/net.html#net_server_listen_options_callback) in
|
|
878
|
-
the Node.js server listen:
|
|
865
|
+
Specifying an address is also supported:
|
|
879
866
|
|
|
880
867
|
```js
|
|
881
|
-
fastify.listen({ port: 3000, host: '127.0.0.1'
|
|
868
|
+
fastify.listen({ port: 3000, host: '127.0.0.1' }, (err, address) => {
|
|
882
869
|
if (err) {
|
|
883
870
|
fastify.log.error(err)
|
|
884
871
|
process.exit(1)
|
|
@@ -889,29 +876,7 @@ fastify.listen({ port: 3000, host: '127.0.0.1', backlog: 511 }, (err) => {
|
|
|
889
876
|
If no callback is provided a Promise is returned:
|
|
890
877
|
|
|
891
878
|
```js
|
|
892
|
-
fastify.listen(3000)
|
|
893
|
-
.then((address) => console.log(`server listening on ${address}`))
|
|
894
|
-
.catch(err => {
|
|
895
|
-
console.log('Error starting server:', err)
|
|
896
|
-
process.exit(1)
|
|
897
|
-
})
|
|
898
|
-
```
|
|
899
|
-
|
|
900
|
-
Specifying an address without a callback is also supported:
|
|
901
|
-
|
|
902
|
-
```js
|
|
903
|
-
fastify.listen(3000, '127.0.0.1')
|
|
904
|
-
.then((address) => console.log(`server listening on ${address}`))
|
|
905
|
-
.catch(err => {
|
|
906
|
-
console.log('Error starting server:', err)
|
|
907
|
-
process.exit(1)
|
|
908
|
-
})
|
|
909
|
-
```
|
|
910
|
-
|
|
911
|
-
Specifying options without a callback is also supported:
|
|
912
|
-
|
|
913
|
-
```js
|
|
914
|
-
fastify.listen({ port: 3000, host: '127.0.0.1', backlog: 511 })
|
|
879
|
+
fastify.listen({ port: 3000 })
|
|
915
880
|
.then((address) => console.log(`server listening on ${address}`))
|
|
916
881
|
.catch(err => {
|
|
917
882
|
console.log('Error starting server:', err)
|
|
@@ -924,7 +889,7 @@ to listen on `0.0.0.0` because they do not default to exposing mapped ports to
|
|
|
924
889
|
`localhost`:
|
|
925
890
|
|
|
926
891
|
```js
|
|
927
|
-
fastify.listen(3000, '0.0.0.0', (err, address) => {
|
|
892
|
+
fastify.listen({ port: 3000, host: '0.0.0.0' }, (err, address) => {
|
|
928
893
|
if (err) {
|
|
929
894
|
fastify.log.error(err)
|
|
930
895
|
process.exit(1)
|
|
@@ -933,7 +898,7 @@ fastify.listen(3000, '0.0.0.0', (err, address) => {
|
|
|
933
898
|
```
|
|
934
899
|
|
|
935
900
|
If the `port` is omitted (or is set to zero), a random available port is
|
|
936
|
-
automatically chosen (
|
|
901
|
+
automatically chosen (available via `fastify.server.address().port`).
|
|
937
902
|
|
|
938
903
|
The default options of listen are:
|
|
939
904
|
|
|
@@ -956,7 +921,7 @@ If you call it before `listen()` is called or after the `close()` function,
|
|
|
956
921
|
it will return an empty array.
|
|
957
922
|
|
|
958
923
|
```js
|
|
959
|
-
await fastify.listen(8080)
|
|
924
|
+
await fastify.listen({ port: 8080 })
|
|
960
925
|
const addresses = fastify.addresses()
|
|
961
926
|
// [
|
|
962
927
|
// { port: 8080, family: 'IPv6', address: '::1' },
|
|
@@ -1470,7 +1435,7 @@ fastify.addContentTypeParser('text/json', { asString: true }, fastify.getDefault
|
|
|
1470
1435
|
#### hasContentTypeParser
|
|
1471
1436
|
<a id="hasContentTypeParser"></a>
|
|
1472
1437
|
|
|
1473
|
-
`fastify.hasContentTypeParser(contentType)` is used to check whether there is a content type parser in the current
|
|
1438
|
+
`fastify.hasContentTypeParser(contentType)` is used to check whether there is a content type parser in the current
|
|
1474
1439
|
context for the specified content type.
|
|
1475
1440
|
|
|
1476
1441
|
```js
|
|
@@ -1614,7 +1579,7 @@ fastify.register(async (instance, opts) => {
|
|
|
1614
1579
|
})
|
|
1615
1580
|
|
|
1616
1581
|
// Start listening.
|
|
1617
|
-
fastify.listen(3000, (err) => {
|
|
1582
|
+
fastify.listen({ port: 3000 }, (err) => {
|
|
1618
1583
|
if (err) {
|
|
1619
1584
|
fastify.log.error(err)
|
|
1620
1585
|
process.exit(1)
|
|
@@ -72,7 +72,7 @@ in a blank http Fastify server.
|
|
|
72
72
|
return 'pong\n'
|
|
73
73
|
})
|
|
74
74
|
|
|
75
|
-
server.listen(8080, (err, address) => {
|
|
75
|
+
server.listen({ port: 8080 }, (err, address) => {
|
|
76
76
|
if (err) {
|
|
77
77
|
console.error(err)
|
|
78
78
|
process.exit(1)
|
|
@@ -360,7 +360,7 @@ into TypeScript interfaces!
|
|
|
360
360
|
}
|
|
361
361
|
})
|
|
362
362
|
|
|
363
|
-
server.listen(8080, (err, address) => {
|
|
363
|
+
server.listen({ port: 8080 }, (err, address) => {
|
|
364
364
|
if (err) {
|
|
365
365
|
console.error(err)
|
|
366
366
|
process.exit(0)
|
|
@@ -681,21 +681,21 @@ There are a couple supported import methods with the Fastify type system.
|
|
|
681
681
|
import fastify from 'fastify'
|
|
682
682
|
|
|
683
683
|
const f = fastify()
|
|
684
|
-
f.listen(8080, () => { console.log('running') })
|
|
684
|
+
f.listen({ port: 8080 }, () => { console.log('running') })
|
|
685
685
|
```
|
|
686
686
|
- Gain access to types with destructuring:
|
|
687
687
|
```typescript
|
|
688
688
|
import fastify, { FastifyInstance } from 'fastify'
|
|
689
689
|
|
|
690
690
|
const f: FastifyInstance = fastify()
|
|
691
|
-
f.listen(8080, () => { console.log('running') })
|
|
691
|
+
f.listen({ port: 8080 }, () => { console.log('running') })
|
|
692
692
|
```
|
|
693
693
|
- Destructuring also works for the main API method:
|
|
694
694
|
```typescript
|
|
695
695
|
import { fastify, FastifyInstance } from 'fastify'
|
|
696
696
|
|
|
697
697
|
const f: FastifyInstance = fastify()
|
|
698
|
-
f.listen(8080, () => { console.log('running') })
|
|
698
|
+
f.listen({ port: 8080 }, () => { console.log('running') })
|
|
699
699
|
```
|
|
700
700
|
2. `import * as Fastify from 'fastify'`
|
|
701
701
|
- Types are resolved and accessible using dot notation
|
|
@@ -706,7 +706,7 @@ There are a couple supported import methods with the Fastify type system.
|
|
|
706
706
|
import * as Fastify from 'fastify'
|
|
707
707
|
|
|
708
708
|
const f: Fastify.FastifyInstance = Fastify.fastify()
|
|
709
|
-
f.listen(8080, () => { console.log('running') })
|
|
709
|
+
f.listen({ port: 8080 }, () => { console.log('running') })
|
|
710
710
|
```
|
|
711
711
|
3. `const fastify = require('fastify')`
|
|
712
712
|
- This syntax is valid and will import fastify as expected; however, types
|
|
@@ -716,14 +716,14 @@ There are a couple supported import methods with the Fastify type system.
|
|
|
716
716
|
const fastify = require('fastify')
|
|
717
717
|
|
|
718
718
|
const f = fastify()
|
|
719
|
-
f.listen(8080, () => { console.log('running') })
|
|
719
|
+
f.listen({ port: 8080 }, () => { console.log('running') })
|
|
720
720
|
```
|
|
721
721
|
- Destructuring is supported and will resolve types properly
|
|
722
722
|
```typescript
|
|
723
723
|
const { fastify } = require('fastify')
|
|
724
724
|
|
|
725
725
|
const f = fastify()
|
|
726
|
-
f.listen(8080, () => { console.log('running') })
|
|
726
|
+
f.listen({ port: 8080 }, () => { console.log('running') })
|
|
727
727
|
```
|
|
728
728
|
|
|
729
729
|
#### Generics
|
|
@@ -825,7 +825,7 @@ a more detailed http server walkthrough.
|
|
|
825
825
|
return { hello: 'world' }
|
|
826
826
|
})
|
|
827
827
|
|
|
828
|
-
server.listen(8080, (err, address) => {
|
|
828
|
+
server.listen({ port: 8080 }, (err, address) => {
|
|
829
829
|
if (err) {
|
|
830
830
|
console.error(err)
|
|
831
831
|
process.exit(0)
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
Fastify uses a schema-based approach, and even if it is not mandatory we
|
|
5
5
|
recommend using [JSON Schema](https://json-schema.org/) to validate your routes
|
|
6
6
|
and serialize your outputs. Internally, Fastify compiles the schema into a
|
|
7
|
-
highly performant function.
|
|
7
|
+
highly performant function.
|
|
8
8
|
|
|
9
9
|
Validation will only be attempted if the content type is `application-json`,
|
|
10
10
|
as described in the documentation for the [content type parser](./ContentTypeParser.md).
|
|
@@ -239,7 +239,7 @@ the types specified in your schema `type` keywords, both to pass the validation
|
|
|
239
239
|
and to use the correctly typed data afterwards.*
|
|
240
240
|
|
|
241
241
|
The Ajv default configuration in Fastify supports coercing array
|
|
242
|
-
parameters in `querystring`.
|
|
242
|
+
parameters in `querystring`.
|
|
243
243
|
Example:
|
|
244
244
|
|
|
245
245
|
```js
|
|
@@ -261,7 +261,7 @@ fastify.get('/', opts, (request, reply) => {
|
|
|
261
261
|
reply.send({ params: request.query }) // echo the querystring
|
|
262
262
|
})
|
|
263
263
|
|
|
264
|
-
fastify.listen(3000, (err) => {
|
|
264
|
+
fastify.listen({ port: 3000 }, (err) => {
|
|
265
265
|
if (err) throw err
|
|
266
266
|
})
|
|
267
267
|
```
|
|
@@ -319,7 +319,7 @@ For further information see [here](https://ajv.js.org/coercion.html)
|
|
|
319
319
|
#### Ajv Plugins
|
|
320
320
|
<a id="ajv-plugins"></a>
|
|
321
321
|
|
|
322
|
-
You can provide a list of plugins you want to use with the default `ajv` instance.
|
|
322
|
+
You can provide a list of plugins you want to use with the default `ajv` instance.
|
|
323
323
|
Note that the plugin must be **compatible with the Ajv version shipped within Fastify**.
|
|
324
324
|
|
|
325
325
|
> Refer to [`ajv options`](./Server.md#ajv) to check plugins format
|
package/docs/Type-Providers.md
CHANGED
|
@@ -6,7 +6,7 @@ Type Providers are a TypeScript only feature that enables Fastify to statically
|
|
|
6
6
|
|
|
7
7
|
### Providers
|
|
8
8
|
|
|
9
|
-
Type Providers are offered as additional packages you will need to install into your project. Each provider uses a different inference library under the hood; allowing you to select the library most appropriate for your needs. Type Provider packages follow a `fastify-type-provider-{provider-name}` naming convention.
|
|
9
|
+
Type Providers are offered as additional packages you will need to install into your project. Each provider uses a different inference library under the hood; allowing you to select the library most appropriate for your needs. Type Provider packages follow a `fastify-type-provider-{provider-name}` naming convention.
|
|
10
10
|
|
|
11
11
|
The following inference packages are supported:
|
|
12
12
|
|
|
@@ -217,7 +217,7 @@ const server = Fastify({
|
|
|
217
217
|
|
|
218
218
|
registerRoutes(server)
|
|
219
219
|
|
|
220
|
-
server.listen(3000)
|
|
220
|
+
server.listen({ port: 3000 })
|
|
221
221
|
```
|
|
222
222
|
|
|
223
223
|
```ts
|
package/examples/asyncawait.js
CHANGED
package/examples/hooks.js
CHANGED
package/examples/http2.js
CHANGED
package/examples/https.js
CHANGED
package/examples/parser.js
CHANGED
package/examples/route-prefix.js
CHANGED
package/examples/simple.js
CHANGED
package/examples/simple.mjs
CHANGED
package/examples/use-plugin.js
CHANGED
package/fastify.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const VERSION = '4.0.0-alpha.
|
|
3
|
+
const VERSION = '4.0.0-alpha.2'
|
|
4
4
|
|
|
5
5
|
const Avvio = require('avvio')
|
|
6
6
|
const http = require('http')
|
|
@@ -352,9 +352,11 @@ function fastify (options) {
|
|
|
352
352
|
// - ready
|
|
353
353
|
// - onClose
|
|
354
354
|
// - close
|
|
355
|
+
|
|
356
|
+
const avvioPluginTimeout = Number(options.pluginTimeout)
|
|
355
357
|
const avvio = Avvio(fastify, {
|
|
356
358
|
autostart: false,
|
|
357
|
-
timeout:
|
|
359
|
+
timeout: isNaN(avvioPluginTimeout) === false ? avvioPluginTimeout : defaultInitOptions.pluginTimeout,
|
|
358
360
|
expose: {
|
|
359
361
|
use: 'register'
|
|
360
362
|
}
|
package/lib/reply.js
CHANGED
|
@@ -457,7 +457,7 @@ function sendStream (payload, res, reply) {
|
|
|
457
457
|
eos(payload, { readable: true, writable: false }, function (err) {
|
|
458
458
|
sourceOpen = false
|
|
459
459
|
if (err != null) {
|
|
460
|
-
if (res.headersSent) {
|
|
460
|
+
if (res.headersSent || reply.request.raw.aborted === true) {
|
|
461
461
|
if (!errorLogged) {
|
|
462
462
|
errorLogged = true
|
|
463
463
|
logStreamError(reply.log, err, res)
|
package/lib/server.js
CHANGED
|
@@ -4,6 +4,7 @@ const http = require('http')
|
|
|
4
4
|
const https = require('https')
|
|
5
5
|
const dns = require('dns')
|
|
6
6
|
|
|
7
|
+
const warnings = require('./warnings')
|
|
7
8
|
const { kState, kOptions, kServerBindings } = require('./symbols')
|
|
8
9
|
const { FST_ERR_HTTP2_INVALID_VERSION, FST_ERR_REOPENED_CLOSE_SERVER, FST_ERR_REOPENED_SERVER } = require('./errors')
|
|
9
10
|
|
|
@@ -15,9 +16,34 @@ function createServer (options, httpHandler) {
|
|
|
15
16
|
return { server, listen }
|
|
16
17
|
|
|
17
18
|
// `this` is the Fastify object
|
|
18
|
-
function listen () {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
function listen (listenOptions, ...args) {
|
|
20
|
+
let cb = args.slice(-1).pop()
|
|
21
|
+
// When the variadic signature deprecation is complete, the function
|
|
22
|
+
// declaration should become:
|
|
23
|
+
// function listen (listenOptions = { port: 0, host: 'localhost' }, cb = undefined)
|
|
24
|
+
// Upon doing so, the `normalizeListenArgs` function is no longer needed,
|
|
25
|
+
// and all of this preamble to feed it correctly also no longer needed.
|
|
26
|
+
const firstArgType = Object.prototype.toString.call(arguments[0])
|
|
27
|
+
if (arguments.length === 0) {
|
|
28
|
+
listenOptions = normalizeListenArgs([])
|
|
29
|
+
} else if (arguments.length > 0 && (firstArgType !== '[object Object]' && firstArgType !== '[object Function]')) {
|
|
30
|
+
warnings.emit('FSTDEP011')
|
|
31
|
+
listenOptions = normalizeListenArgs(Array.from(arguments))
|
|
32
|
+
cb = listenOptions.cb
|
|
33
|
+
} else if (args.length > 1) {
|
|
34
|
+
// `.listen(obj, a, ..., n, callback )`
|
|
35
|
+
warnings.emit('FSTDEP011')
|
|
36
|
+
// Deal with `.listen(port, host, backlog, [cb])`
|
|
37
|
+
const hostPath = listenOptions.path ? [listenOptions.path] : [listenOptions.port ?? 0, listenOptions.host ?? 'localhost']
|
|
38
|
+
Object.assign(listenOptions, normalizeListenArgs([...hostPath, ...args]))
|
|
39
|
+
} else {
|
|
40
|
+
listenOptions.cb = cb
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const { host = 'localhost' } = listenOptions
|
|
44
|
+
if (Object.prototype.hasOwnProperty.call(listenOptions, 'host') === false) {
|
|
45
|
+
listenOptions.host = host
|
|
46
|
+
}
|
|
21
47
|
|
|
22
48
|
if (host === 'localhost') {
|
|
23
49
|
listenOptions.cb = (err, address) => {
|
|
@@ -41,6 +67,7 @@ function createServer (options, httpHandler) {
|
|
|
41
67
|
|
|
42
68
|
if (cb === undefined) {
|
|
43
69
|
const listening = listenPromise.call(this, server, listenOptions)
|
|
70
|
+
/* istanbul ignore else */
|
|
44
71
|
if (host === 'localhost') {
|
|
45
72
|
return listening.then(address => {
|
|
46
73
|
return new Promise((resolve, reject) => {
|
|
@@ -51,7 +78,6 @@ function createServer (options, httpHandler) {
|
|
|
51
78
|
})
|
|
52
79
|
})
|
|
53
80
|
}
|
|
54
|
-
return listening
|
|
55
81
|
}
|
|
56
82
|
|
|
57
83
|
this.ready(listenCallback.call(this, server, listenOptions))
|
|
@@ -226,17 +252,13 @@ function normalizeListenArgs (args) {
|
|
|
226
252
|
const firstArg = args[0]
|
|
227
253
|
const argsLength = args.length
|
|
228
254
|
const lastArg = args[argsLength - 1]
|
|
229
|
-
|
|
230
|
-
if (typeof firstArg === 'object' && firstArg !== null) {
|
|
231
|
-
options.backlog = argsLength > 1 ? lastArg : undefined
|
|
232
|
-
Object.assign(options, firstArg)
|
|
233
|
-
} else if (typeof firstArg === 'string' && isNaN(firstArg)) {
|
|
255
|
+
if (typeof firstArg === 'string' && isNaN(firstArg)) {
|
|
234
256
|
/* Deal with listen (pipe[, backlog]) */
|
|
235
257
|
options.path = firstArg
|
|
236
258
|
options.backlog = argsLength > 1 ? lastArg : undefined
|
|
237
259
|
} else {
|
|
238
260
|
/* Deal with listen ([port[, host[, backlog]]]) */
|
|
239
|
-
options.port = argsLength >= 1 && firstArg ? firstArg : 0
|
|
261
|
+
options.port = argsLength >= 1 && Number.isInteger(firstArg) ? firstArg : 0
|
|
240
262
|
// This will listen to what localhost is.
|
|
241
263
|
// It can be 127.0.0.1 or ::1, depending on the operating system.
|
|
242
264
|
// Fixes https://github.com/fastify/fastify/issues/1022.
|
package/lib/warnings.js
CHANGED
|
@@ -19,4 +19,6 @@ warning.create('FastifyDeprecation', 'FSTDEP009', 'You are using a custom route
|
|
|
19
19
|
|
|
20
20
|
warning.create('FastifyDeprecation', 'FSTDEP010', 'Modifying the "reply.sent" property is deprecated. Use the "reply.hijack()" method instead.')
|
|
21
21
|
|
|
22
|
+
warning.create('FastifyDeprecation', 'FSTDEP011', 'Variadic listen method is deprecated. Please use ".listen(optionsObject)" instead. The variadic signature will be removed in `fastify@5`.')
|
|
23
|
+
|
|
22
24
|
module.exports = warning
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.2",
|
|
4
4
|
"description": "Fast and low overhead web framework, for Node.js",
|
|
5
5
|
"main": "fastify.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
"bench": "branchcmp -r 2 -g -s \"npm run benchmark\"",
|
|
10
10
|
"benchmark": "npx concurrently -k -s first \"node ./examples/benchmark/simple.js\" \"npx autocannon -c 100 -d 30 -p 10 localhost:3000/\"",
|
|
11
11
|
"coverage": "npm run unit -- --cov --coverage-report=html",
|
|
12
|
+
"coverage:ci": "npm run unit -- --cov --coverage-report=html --no-browser --no-check-coverage -R terse",
|
|
13
|
+
"coverage:ci-check-coverage": "nyc check-coverage --branches 100 --functions 100 --lines 100 --statements 100",
|
|
12
14
|
"license-checker": "license-checker --production --onlyAllow=\"MIT;ISC;BSD-3-Clause;BSD-2-Clause\"",
|
|
13
15
|
"lint": "npm run lint:standard && npm run lint:typescript",
|
|
14
16
|
"lint:fix": "standard --fix",
|
|
@@ -19,9 +21,10 @@
|
|
|
19
21
|
"test:ci": "npm run unit -- -R terse --cov --coverage-report=lcovonly && npm run test:typescript",
|
|
20
22
|
"test:report": "npm run lint && npm run unit:report && npm run test:typescript",
|
|
21
23
|
"test:typescript": "tsc test/types/import.ts && tsd",
|
|
22
|
-
"
|
|
24
|
+
"test:watch": "npm run unit -- -w --no-coverage-report -R terse",
|
|
25
|
+
"unit": "tap",
|
|
23
26
|
"unit:junit": "tap-mocha-reporter xunit < out.tap > test/junit-testresults.xml",
|
|
24
|
-
"unit:report": "tap
|
|
27
|
+
"unit:report": "tap --cov --coverage-report=html --coverage-report=cobertura | tee out.tap"
|
|
25
28
|
},
|
|
26
29
|
"repository": {
|
|
27
30
|
"type": "git",
|
|
@@ -121,21 +124,19 @@
|
|
|
121
124
|
},
|
|
122
125
|
"homepage": "https://www.fastify.io/",
|
|
123
126
|
"devDependencies": {
|
|
124
|
-
"@fastify/ajv-compiler-6": "npm:@fastify/ajv-compiler@^1.0.0",
|
|
125
|
-
"@fastify/ajv-compiler-8": "npm:@fastify/ajv-compiler@^2.0.0",
|
|
126
127
|
"@fastify/pre-commit": "^2.0.1",
|
|
127
|
-
"@sinonjs/fake-timers": "^9.1.0",
|
|
128
|
-
"@types/node": "^16.0.0",
|
|
129
128
|
"@sinclair/typebox": "^0.23.1",
|
|
129
|
+
"@sinonjs/fake-timers": "^9.1.0",
|
|
130
|
+
"@types/node": "^17.0.18",
|
|
130
131
|
"@typescript-eslint/eslint-plugin": "^5.7.0",
|
|
131
132
|
"@typescript-eslint/parser": "^5.7.0",
|
|
133
|
+
"ajv": "^8.10.0",
|
|
132
134
|
"ajv-errors": "^3.0.0",
|
|
133
135
|
"ajv-formats": "^2.1.1",
|
|
134
136
|
"ajv-i18n": "^4.2.0",
|
|
135
137
|
"ajv-merge-patch": "^5.0.1",
|
|
136
138
|
"branch-comparer": "^1.1.0",
|
|
137
139
|
"cors": "^2.8.5",
|
|
138
|
-
"coveralls": "^3.1.1",
|
|
139
140
|
"dns-prefetch-control": "^0.3.0",
|
|
140
141
|
"eslint": "^8.0.1",
|
|
141
142
|
"eslint-config-standard": "^17.0.0-1",
|
|
@@ -152,20 +153,21 @@
|
|
|
152
153
|
"h2url": "^0.2.0",
|
|
153
154
|
"helmet": "^5.0.1",
|
|
154
155
|
"hide-powered-by": "^1.1.0",
|
|
156
|
+
"http-errors": "^2.0.0",
|
|
155
157
|
"joi": "^17.5.0",
|
|
156
158
|
"json-schema-to-ts": "^1.6.4",
|
|
157
159
|
"JSONStream": "^1.3.5",
|
|
158
160
|
"license-checker": "^25.0.1",
|
|
159
|
-
"pem": "^1.14.4",
|
|
160
161
|
"proxyquire": "^2.1.3",
|
|
161
162
|
"pump": "^3.0.0",
|
|
163
|
+
"self-cert": "^2.0.0",
|
|
162
164
|
"send": "^0.17.2",
|
|
163
165
|
"serve-static": "^1.14.1",
|
|
164
166
|
"simple-get": "^4.0.0",
|
|
165
167
|
"snazzy": "^9.0.0",
|
|
166
168
|
"split2": "^4.1.0",
|
|
167
169
|
"standard": "^17.0.0-2",
|
|
168
|
-
"tap": "^
|
|
170
|
+
"tap": "^16.0.0",
|
|
169
171
|
"tsd": "^0.19.1",
|
|
170
172
|
"typescript": "^4.5.4",
|
|
171
173
|
"undici": "^4.11.3",
|
|
@@ -177,7 +179,7 @@
|
|
|
177
179
|
"@fastify/fast-json-stringify-compiler": "^1.0.0",
|
|
178
180
|
"abstract-logging": "^2.0.1",
|
|
179
181
|
"avvio": "^8.1.0",
|
|
180
|
-
"fastify-error": "^0.
|
|
182
|
+
"fastify-error": "^1.0.0",
|
|
181
183
|
"find-my-way": "^5.1.0",
|
|
182
184
|
"light-my-request": "^4.7.0",
|
|
183
185
|
"pino": "^7.5.1",
|