fastify 4.25.0 → 4.25.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/README.md +3 -3
- package/docs/Reference/Server.md +5 -3
- package/fastify.js +1 -1
- package/lib/pluginUtils.js +3 -3
- package/lib/reply.js +4 -0
- package/package.json +2 -2
- package/test/plugin.4.test.js +25 -6
- package/test/reply-code.test.js +24 -1
- package/test/types/route.test-d.ts +6 -13
- package/types/route.d.ts +8 -2
package/README.md
CHANGED
|
@@ -377,10 +377,10 @@ to join this group by Lead Maintainers.
|
|
|
377
377
|
|
|
378
378
|
[<img
|
|
379
379
|
src="https://github.com/openjs-foundation/artwork/blob/main/openjs_foundation/openjs_foundation-logo-horizontal-color.png?raw=true"
|
|
380
|
-
width="250px;"/>](https://openjsf.org/projects
|
|
380
|
+
width="250px;"/>](https://openjsf.org/projects)
|
|
381
381
|
|
|
382
|
-
We are a [
|
|
383
|
-
Project](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/PROJECT_PROGRESSION.md#
|
|
382
|
+
We are a [At-Large
|
|
383
|
+
Project](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/PROJECT_PROGRESSION.md#at-large-projects)
|
|
384
384
|
in the [OpenJS Foundation](https://openjsf.org/).
|
|
385
385
|
|
|
386
386
|
## Acknowledgements
|
package/docs/Reference/Server.md
CHANGED
|
@@ -1532,9 +1532,11 @@ handlers. *async-await* is supported as well.
|
|
|
1532
1532
|
If the error `statusCode` is less than 400, Fastify will automatically
|
|
1533
1533
|
set it to 500 before calling the error handler.
|
|
1534
1534
|
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1535
|
+
`setErrorHandler` will ***not*** catch:
|
|
1536
|
+
- errors thrown in an `onResponse` hook because the response has already been
|
|
1537
|
+
sent to the client. Use the `onSend` hook instead.
|
|
1538
|
+
- not found (404) errors. Use [`setNotFoundHandler`](#set-not-found-handler)
|
|
1539
|
+
instead.
|
|
1538
1540
|
|
|
1539
1541
|
```js
|
|
1540
1542
|
fastify.setErrorHandler(function (error, request, reply) {
|
package/fastify.js
CHANGED
package/lib/pluginUtils.js
CHANGED
|
@@ -136,14 +136,14 @@ function registerPluginName (fn) {
|
|
|
136
136
|
return name
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
function checkPluginHealthiness (fn, pluginName
|
|
139
|
+
function checkPluginHealthiness (fn, pluginName) {
|
|
140
140
|
if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
|
|
141
|
-
FSTWRN002(pluginName)
|
|
141
|
+
FSTWRN002(pluginName || 'anonymous')
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
function registerPlugin (fn) {
|
|
146
|
-
const pluginName = registerPluginName.call(this, fn)
|
|
146
|
+
const pluginName = registerPluginName.call(this, fn) || getPluginName(fn)
|
|
147
147
|
checkPluginHealthiness.call(this, fn, pluginName)
|
|
148
148
|
checkVersion.call(this, fn)
|
|
149
149
|
checkDecorators.call(this, fn)
|
package/lib/reply.js
CHANGED
|
@@ -605,6 +605,10 @@ function onSendEnd (reply, payload) {
|
|
|
605
605
|
reply.removeHeader('content-length')
|
|
606
606
|
safeWriteHead(reply, statusCode)
|
|
607
607
|
sendTrailer(undefined, res, reply)
|
|
608
|
+
if (typeof payload.resume === 'function') {
|
|
609
|
+
payload.on('error', noop)
|
|
610
|
+
payload.resume()
|
|
611
|
+
}
|
|
608
612
|
return
|
|
609
613
|
}
|
|
610
614
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify",
|
|
3
|
-
"version": "4.25.
|
|
3
|
+
"version": "4.25.2",
|
|
4
4
|
"description": "Fast and low overhead web framework, for Node.js",
|
|
5
5
|
"main": "fastify.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"test:report": "npm run lint && npm run unit:report && npm run test:typescript",
|
|
26
26
|
"test:validator:integrity": "npm run build:validation && git diff --quiet --ignore-all-space --ignore-blank-lines --ignore-cr-at-eol lib/error-serializer.js && git diff --quiet --ignore-all-space --ignore-blank-lines --ignore-cr-at-eol lib/configValidator.js",
|
|
27
27
|
"test:typescript": "tsc test/types/import.ts && tsd",
|
|
28
|
-
"test:watch": "npm run unit --
|
|
28
|
+
"test:watch": "npm run unit -- --watch --cov --no-coverage-report --reporter=terse",
|
|
29
29
|
"unit": "c8 tap",
|
|
30
30
|
"unit:junit": "tap-mocha-reporter xunit < out.tap > test/junit-testresults.xml",
|
|
31
31
|
"unit:report": "tap --cov --coverage-report=html --coverage-report=cobertura | tee out.tap",
|
package/test/plugin.4.test.js
CHANGED
|
@@ -416,20 +416,27 @@ test('hasPlugin returns true when using encapsulation', async t => {
|
|
|
416
416
|
})
|
|
417
417
|
|
|
418
418
|
test('registering plugins with mixed style should return a warning', async t => {
|
|
419
|
-
t.plan(
|
|
419
|
+
t.plan(12)
|
|
420
|
+
|
|
421
|
+
const pluginNames = ['error-plugin', 'anonymous', 'anotherPlugin', 'anotherPluginNamed']
|
|
420
422
|
|
|
423
|
+
const oldWarnings = process.listeners('warning')
|
|
424
|
+
process.removeAllListeners('warning')
|
|
421
425
|
process.on('warning', onWarning)
|
|
422
426
|
function onWarning (warning) {
|
|
427
|
+
t.match(warning.message, new RegExp(`.*${pluginNames.shift()} plugin being registered mixes async and callback styles.*`))
|
|
423
428
|
t.equal(warning.name, 'FastifyWarning')
|
|
424
429
|
t.equal(warning.code, 'FSTWRN002')
|
|
425
430
|
}
|
|
431
|
+
t.teardown(() => {
|
|
432
|
+
process.removeListener('warning', onWarning)
|
|
433
|
+
for (const warning of oldWarnings) {
|
|
434
|
+
process.on('warning', warning)
|
|
435
|
+
}
|
|
436
|
+
})
|
|
426
437
|
|
|
427
438
|
const fastify = Fastify()
|
|
428
439
|
|
|
429
|
-
const anonymousPlugin = async (app, opts, done) => {
|
|
430
|
-
done()
|
|
431
|
-
}
|
|
432
|
-
|
|
433
440
|
const pluginName = 'error-plugin'
|
|
434
441
|
const errorPlugin = async (app, opts, done) => {
|
|
435
442
|
done()
|
|
@@ -437,8 +444,20 @@ test('registering plugins with mixed style should return a warning', async t =>
|
|
|
437
444
|
|
|
438
445
|
const namedPlugin = fp(errorPlugin, { name: pluginName })
|
|
439
446
|
|
|
447
|
+
async function anotherPlugin (app, opts, done) {
|
|
448
|
+
done()
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const anotherPluginNamed = async function (app, opts, done) {
|
|
452
|
+
done()
|
|
453
|
+
}
|
|
454
|
+
|
|
440
455
|
fastify.register(namedPlugin)
|
|
441
|
-
fastify.register(
|
|
456
|
+
fastify.register(async (app, opts, done) => {
|
|
457
|
+
done()
|
|
458
|
+
})
|
|
459
|
+
fastify.register(anotherPlugin)
|
|
460
|
+
fastify.register(anotherPluginNamed)
|
|
442
461
|
|
|
443
462
|
await fastify.ready()
|
|
444
463
|
})
|
package/test/reply-code.test.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const t = require('tap')
|
|
4
|
+
const { Readable } = require('stream')
|
|
4
5
|
const test = t.test
|
|
5
6
|
const Fastify = require('..')
|
|
6
7
|
|
|
@@ -59,7 +60,7 @@ test('code should handle null/undefined/float', t => {
|
|
|
59
60
|
})
|
|
60
61
|
|
|
61
62
|
test('code should handle 204', t => {
|
|
62
|
-
t.plan(
|
|
63
|
+
t.plan(13)
|
|
63
64
|
|
|
64
65
|
const fastify = Fastify()
|
|
65
66
|
|
|
@@ -72,6 +73,18 @@ test('code should handle 204', t => {
|
|
|
72
73
|
reply.status(204).send({ message: 'hello' })
|
|
73
74
|
})
|
|
74
75
|
|
|
76
|
+
fastify.get('/stream/204', function (request, reply) {
|
|
77
|
+
const stream = new Readable({
|
|
78
|
+
read () {
|
|
79
|
+
this.push(null)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
stream.on('end', () => {
|
|
83
|
+
t.pass('stream ended')
|
|
84
|
+
})
|
|
85
|
+
reply.status(204).send(stream)
|
|
86
|
+
})
|
|
87
|
+
|
|
75
88
|
fastify.inject({
|
|
76
89
|
method: 'GET',
|
|
77
90
|
url: '/204'
|
|
@@ -91,6 +104,16 @@ test('code should handle 204', t => {
|
|
|
91
104
|
t.equal(res.payload, '')
|
|
92
105
|
t.equal(res.headers['content-length'], undefined)
|
|
93
106
|
})
|
|
107
|
+
|
|
108
|
+
fastify.inject({
|
|
109
|
+
method: 'GET',
|
|
110
|
+
url: '/stream/204'
|
|
111
|
+
}, (error, res) => {
|
|
112
|
+
t.error(error)
|
|
113
|
+
t.equal(res.statusCode, 204)
|
|
114
|
+
t.equal(res.payload, '')
|
|
115
|
+
t.equal(res.headers['content-length'], undefined)
|
|
116
|
+
})
|
|
94
117
|
})
|
|
95
118
|
|
|
96
119
|
test('code should handle onSend hook on 204', t => {
|
|
@@ -444,19 +444,12 @@ expectType<boolean>(fastify().hasRoute({
|
|
|
444
444
|
url: '/',
|
|
445
445
|
method: 'GET',
|
|
446
446
|
constraints: {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
},
|
|
454
|
-
set: (type, store) => {}
|
|
455
|
-
}
|
|
456
|
-
},
|
|
457
|
-
deriveConstraint: (req, ctx, done) => {},
|
|
458
|
-
mustMatchWhenDerived: true
|
|
459
|
-
}
|
|
447
|
+
// constraints value should accept any value
|
|
448
|
+
number: 12,
|
|
449
|
+
date: new Date(),
|
|
450
|
+
boolean: true,
|
|
451
|
+
function: () => {},
|
|
452
|
+
object: { foo: 'bar' }
|
|
460
453
|
}
|
|
461
454
|
}))
|
|
462
455
|
|
package/types/route.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FastifyError } from '@fastify/error'
|
|
2
|
+
import { ConstraintStrategy } from 'find-my-way'
|
|
2
3
|
import { FastifyRequestContext } from './context'
|
|
3
4
|
import { onErrorMetaHookHandler, onRequestAbortMetaHookHandler, onRequestMetaHookHandler, onResponseMetaHookHandler, onSendMetaHookHandler, onTimeoutMetaHookHandler, preHandlerMetaHookHandler, preParsingMetaHookHandler, preSerializationMetaHookHandler, preValidationMetaHookHandler } from './hooks'
|
|
4
5
|
import { FastifyInstance } from './instance'
|
|
@@ -12,7 +13,6 @@ import {
|
|
|
12
13
|
ResolveFastifyReplyReturnType
|
|
13
14
|
} from './type-provider'
|
|
14
15
|
import { ContextConfigDefault, HTTPMethods, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
|
|
15
|
-
import { ConstraintStrategy } from 'find-my-way'
|
|
16
16
|
|
|
17
17
|
export interface FastifyRouteConfig {
|
|
18
18
|
url: string;
|
|
@@ -25,6 +25,12 @@ export type RouteConstraintType = Omit<ConstraintStrategy<any>, 'deriveConstrain
|
|
|
25
25
|
deriveConstraint<Context>(req: RawRequestDefaultExpression<RawServerDefault>, ctx?: Context, done?: (err: Error, ...args: any) => any): any,
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
export interface RouteConstraint {
|
|
29
|
+
version?: string
|
|
30
|
+
host?: RegExp | string
|
|
31
|
+
[name: string]: unknown
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
/**
|
|
29
35
|
* Route shorthand options for the various shorthand methods
|
|
30
36
|
*/
|
|
@@ -48,7 +54,7 @@ export interface RouteShorthandOptions<
|
|
|
48
54
|
logLevel?: LogLevel;
|
|
49
55
|
config?: Omit<FastifyRequestContext<ContextConfig>['config'], 'url' | 'method'>;
|
|
50
56
|
version?: string;
|
|
51
|
-
constraints?:
|
|
57
|
+
constraints?: RouteConstraint,
|
|
52
58
|
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
|
|
53
59
|
errorHandler?: (
|
|
54
60
|
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
|