fastify 4.11.0 → 4.12.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.
- package/LICENSE +1 -1
- package/docs/Guides/Ecosystem.md +5 -0
- package/docs/Guides/Migration-Guide-V4.md +5 -0
- package/fastify.d.ts +191 -176
- package/fastify.js +1 -1
- package/lib/contentTypeParser.js +3 -15
- package/lib/error-serializer.js +8 -0
- package/package.json +3 -3
- package/test/content-parser.test.js +43 -0
- package/test/types/instance.test-d.ts +2 -0
- package/types/instance.d.ts +2 -0
package/LICENSE
CHANGED
package/docs/Guides/Ecosystem.md
CHANGED
|
@@ -366,6 +366,9 @@ section.
|
|
|
366
366
|
- [`fastify-influxdb`](https://github.com/alex-ppg/fastify-influxdb) Fastify
|
|
367
367
|
InfluxDB plugin connecting to an InfluxDB instance via the Influx default
|
|
368
368
|
package.
|
|
369
|
+
- [`fastify-ip`](https://github.com/metcoder95/fastify-ip) A plugin
|
|
370
|
+
for Fastify that allows you to infer a request ID by a
|
|
371
|
+
given set of custom Request headers.
|
|
369
372
|
- [`fastify-jwt-authz`](https://github.com/Ethan-Arrowood/fastify-jwt-authz) JWT
|
|
370
373
|
user scope verifier.
|
|
371
374
|
- [`fastify-jwt-webapp`](https://github.com/charlesread/fastify-jwt-webapp) JWT
|
|
@@ -474,6 +477,8 @@ section.
|
|
|
474
477
|
- [`fastify-postgraphile`](https://github.com/alemagio/fastify-postgraphile)
|
|
475
478
|
Plugin to integrate [PostGraphile](https://www.graphile.org/postgraphile/) in
|
|
476
479
|
a Fastify project.
|
|
480
|
+
- [`fastify-postgres-dot-js`](https://github.com/kylerush/fastify-postgresjs) Fastify
|
|
481
|
+
PostgreSQL connection plugin that uses [Postgres.js](https://github.com/porsager/postgres).
|
|
477
482
|
- [`fastify-prettier`](https://github.com/hsynlms/fastify-prettier) A Fastify
|
|
478
483
|
plugin that uses [prettier](https://github.com/prettier/prettier) under the
|
|
479
484
|
hood to beautify outgoing responses and/or other things in the Fastify server.
|
|
@@ -39,6 +39,11 @@ const res = await fastify.inject('/encapsulated')
|
|
|
39
39
|
console.log(res.json().message) // 'wrapped'
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
+
>The root error handler is Fastify’s generic error handler.
|
|
43
|
+
>This error handler will use the headers and status code in the Error object,
|
|
44
|
+
>if they exist. **The headers and status code will not be automatically set if
|
|
45
|
+
>a custom error handler is provided**.
|
|
46
|
+
|
|
42
47
|
### Removed `app.use()` ([#3506](https://github.com/fastify/fastify/pull/3506))
|
|
43
48
|
|
|
44
49
|
With v4 of Fastify, `app.use()` has been removed and the use of middleware is
|
package/fastify.d.ts
CHANGED
|
@@ -1,26 +1,190 @@
|
|
|
1
1
|
import * as http from 'http'
|
|
2
2
|
import * as http2 from 'http2'
|
|
3
3
|
import * as https from 'https'
|
|
4
|
-
import {
|
|
4
|
+
import { Socket } from 'net'
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression } from './types/utils'
|
|
8
|
-
import { FastifyBaseLogger, FastifyLoggerInstance, FastifyLoggerOptions, PinoLoggerOptions } from './types/logger'
|
|
9
|
-
import { FastifyInstance } from './types/instance'
|
|
10
|
-
import { FastifyServerFactory } from './types/serverFactory'
|
|
11
|
-
import { Options as AjvOptions } from '@fastify/ajv-compiler'
|
|
12
|
-
import { Options as FJSOptions } from '@fastify/fast-json-stringify-compiler'
|
|
6
|
+
import { Options as AjvOptions, ValidatorCompiler } from '@fastify/ajv-compiler'
|
|
13
7
|
import { FastifyError } from '@fastify/error'
|
|
8
|
+
import { Options as FJSOptions, SerializerCompiler } from '@fastify/fast-json-stringify-compiler'
|
|
9
|
+
import { ConstraintStrategy, HTTPVersion } from 'find-my-way'
|
|
10
|
+
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
|
|
11
|
+
|
|
12
|
+
import { FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction } from './types/content-type-parser'
|
|
13
|
+
import { FastifyContext, FastifyContextConfig } from './types/context'
|
|
14
|
+
import { FastifyErrorCodes } from './types/errors'
|
|
15
|
+
import { DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, 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 './types/hooks'
|
|
16
|
+
import { FastifyListenOptions, FastifyInstance, PrintRoutesOptions } from './types/instance'
|
|
17
|
+
import { FastifyBaseLogger, FastifyLoggerInstance, FastifyLoggerOptions, PinoLoggerOptions, FastifyLogFn, LogLevel } from './types/logger'
|
|
18
|
+
import { FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin } from './types/plugin'
|
|
19
|
+
import { FastifyRegister, FastifyRegisterOptions, RegisterOptions } from './types/register'
|
|
14
20
|
import { FastifyReply } from './types/reply'
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import { SerializerCompiler } from '@fastify/fast-json-stringify-compiler'
|
|
20
|
-
import { FastifySchema } from './types/schema'
|
|
21
|
-
import { FastifyContextConfig } from './types/context'
|
|
21
|
+
import { FastifyRequest, RequestGenericInterface } from './types/request'
|
|
22
|
+
import { RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler, RouteGenericInterface } from './types/route'
|
|
23
|
+
import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError } from './types/schema'
|
|
24
|
+
import { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
|
|
22
25
|
import { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
|
|
23
|
-
import {
|
|
26
|
+
import { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
|
|
27
|
+
|
|
28
|
+
declare module '@fastify/error' {
|
|
29
|
+
interface FastifyError {
|
|
30
|
+
validation?: fastify.ValidationResult[];
|
|
31
|
+
validationContext?: 'body' | 'headers' | 'parameters' | 'querystring';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type Fastify = typeof fastify
|
|
36
|
+
|
|
37
|
+
declare namespace fastify {
|
|
38
|
+
export const errorCodes: FastifyErrorCodes;
|
|
39
|
+
|
|
40
|
+
export type FastifyHttp2SecureOptions<
|
|
41
|
+
Server extends http2.Http2SecureServer,
|
|
42
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger
|
|
43
|
+
> = FastifyServerOptions<Server, Logger> & {
|
|
44
|
+
http2: true,
|
|
45
|
+
https: http2.SecureServerOptions,
|
|
46
|
+
http2SessionTimeout?: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type FastifyHttp2Options<
|
|
50
|
+
Server extends http2.Http2Server,
|
|
51
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger
|
|
52
|
+
> = FastifyServerOptions<Server, Logger> & {
|
|
53
|
+
http2: true,
|
|
54
|
+
http2SessionTimeout?: number
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type FastifyHttpsOptions<
|
|
58
|
+
Server extends https.Server,
|
|
59
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger
|
|
60
|
+
> = FastifyServerOptions<Server, Logger> & {
|
|
61
|
+
https: https.ServerOptions | null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type FindMyWayVersion<RawServer extends RawServerBase> = RawServer extends http.Server ? HTTPVersion.V1 : HTTPVersion.V2
|
|
65
|
+
|
|
66
|
+
export interface ConnectionError extends Error {
|
|
67
|
+
code: string,
|
|
68
|
+
bytesParsed: number,
|
|
69
|
+
rawPacket: {
|
|
70
|
+
type: string,
|
|
71
|
+
data: number[]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type TrustProxyFunction = (address: string, hop: number) => boolean
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Options for a fastify server instance. Utilizes conditional logic on the generic server parameter to enforce certain https and http2
|
|
79
|
+
*/
|
|
80
|
+
export type FastifyServerOptions<
|
|
81
|
+
RawServer extends RawServerBase = RawServerDefault,
|
|
82
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger
|
|
83
|
+
> = {
|
|
84
|
+
ignoreTrailingSlash?: boolean,
|
|
85
|
+
ignoreDuplicateSlashes?: boolean,
|
|
86
|
+
connectionTimeout?: number,
|
|
87
|
+
keepAliveTimeout?: number,
|
|
88
|
+
maxRequestsPerSocket?: number,
|
|
89
|
+
forceCloseConnections?: boolean | 'idle',
|
|
90
|
+
requestTimeout?: number,
|
|
91
|
+
pluginTimeout?: number,
|
|
92
|
+
bodyLimit?: number,
|
|
93
|
+
maxParamLength?: number,
|
|
94
|
+
disableRequestLogging?: boolean,
|
|
95
|
+
exposeHeadRoutes?: boolean,
|
|
96
|
+
onProtoPoisoning?: ProtoAction,
|
|
97
|
+
onConstructorPoisoning?: ConstructorAction,
|
|
98
|
+
logger?: boolean | FastifyLoggerOptions<RawServer> & PinoLoggerOptions | Logger,
|
|
99
|
+
serializerOpts?: FJSOptions | Record<string, unknown>,
|
|
100
|
+
serverFactory?: FastifyServerFactory<RawServer>,
|
|
101
|
+
caseSensitive?: boolean,
|
|
102
|
+
requestIdHeader?: string | false,
|
|
103
|
+
requestIdLogLabel?: string;
|
|
104
|
+
jsonShorthand?: boolean;
|
|
105
|
+
genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>) => string,
|
|
106
|
+
trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
|
|
107
|
+
querystringParser?: (str: string) => { [key: string]: unknown },
|
|
108
|
+
/**
|
|
109
|
+
* @deprecated Prefer using the `constraints.version` property
|
|
110
|
+
*/
|
|
111
|
+
versioning?: {
|
|
112
|
+
storage(): {
|
|
113
|
+
get(version: string): string | null,
|
|
114
|
+
set(version: string, store: Function): void
|
|
115
|
+
del(version: string): void,
|
|
116
|
+
empty(): void
|
|
117
|
+
},
|
|
118
|
+
deriveVersion<Context>(req: Object, ctx?: Context): string // not a fan of using Object here. Also what is Context? Can either of these be better defined?
|
|
119
|
+
},
|
|
120
|
+
constraints?: {
|
|
121
|
+
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
|
|
122
|
+
},
|
|
123
|
+
schemaController?: {
|
|
124
|
+
bucket?: (parentSchemas?: unknown) => {
|
|
125
|
+
add(schema: unknown): FastifyInstance;
|
|
126
|
+
getSchema(schemaId: string): unknown;
|
|
127
|
+
getSchemas(): Record<string, unknown>;
|
|
128
|
+
};
|
|
129
|
+
compilersFactory?: {
|
|
130
|
+
buildValidator?: ValidatorCompiler;
|
|
131
|
+
buildSerializer?: SerializerCompiler;
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
return503OnClosing?: boolean,
|
|
135
|
+
ajv?: {
|
|
136
|
+
customOptions?: AjvOptions,
|
|
137
|
+
plugins?: (Function | [Function, unknown])[]
|
|
138
|
+
},
|
|
139
|
+
frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema>(
|
|
140
|
+
error: FastifyError,
|
|
141
|
+
req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>,
|
|
142
|
+
res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RequestGeneric, FastifyContextConfig, SchemaCompiler, TypeProvider>
|
|
143
|
+
) => void,
|
|
144
|
+
rewriteUrl?: (req: RawRequestDefaultExpression<RawServer>) => string,
|
|
145
|
+
schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error,
|
|
146
|
+
/**
|
|
147
|
+
* listener to error events emitted by client connections
|
|
148
|
+
*/
|
|
149
|
+
clientErrorHandler?: (error: ConnectionError, socket: Socket) => void
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface ValidationResult {
|
|
153
|
+
keyword: string;
|
|
154
|
+
instancePath: string;
|
|
155
|
+
schemaPath: string;
|
|
156
|
+
params: Record<string, string | string[]>;
|
|
157
|
+
message?: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/* Export additional types */
|
|
161
|
+
export type {
|
|
162
|
+
LightMyRequestChain, InjectOptions, LightMyRequestResponse, LightMyRequestCallback, // 'light-my-request'
|
|
163
|
+
FastifyRequest, RequestGenericInterface, // './types/request'
|
|
164
|
+
FastifyReply, // './types/reply'
|
|
165
|
+
FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin, // './types/plugin'
|
|
166
|
+
FastifyListenOptions, FastifyInstance, PrintRoutesOptions, // './types/instance'
|
|
167
|
+
FastifyLoggerOptions, FastifyBaseLogger, FastifyLoggerInstance, FastifyLogFn, LogLevel, // './types/logger'
|
|
168
|
+
FastifyContext, FastifyContextConfig, // './types/context'
|
|
169
|
+
RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler, RouteGenericInterface, // './types/route'
|
|
170
|
+
FastifyRegister, FastifyRegisterOptions, RegisterOptions, // './types/register'
|
|
171
|
+
FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, // './types/content-type-parser'
|
|
172
|
+
FastifyError, // '@fastify/error'
|
|
173
|
+
FastifySchema, FastifySchemaCompiler, // './types/schema'
|
|
174
|
+
HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault, // './types/utils'
|
|
175
|
+
DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, // './types/hooks'
|
|
176
|
+
FastifyServerFactory, FastifyServerFactoryHandler, // './types/serverFactory'
|
|
177
|
+
FastifyTypeProvider, FastifyTypeProviderDefault, // './types/type-provider'
|
|
178
|
+
FastifyErrorCodes, // './types/errors'
|
|
179
|
+
}
|
|
180
|
+
// named export
|
|
181
|
+
// import { plugin } from 'plugin'
|
|
182
|
+
// const { plugin } = require('plugin')
|
|
183
|
+
export const fastify: Fastify
|
|
184
|
+
// default export
|
|
185
|
+
// import plugin from 'plugin'
|
|
186
|
+
export { fastify as default }
|
|
187
|
+
}
|
|
24
188
|
|
|
25
189
|
/**
|
|
26
190
|
* Fastify factory function for the standard fastify http, https, or http2 server instance.
|
|
@@ -34,183 +198,34 @@ declare function fastify<
|
|
|
34
198
|
Server extends http2.Http2SecureServer,
|
|
35
199
|
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
|
|
36
200
|
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
|
|
37
|
-
Logger extends FastifyBaseLogger =
|
|
201
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
|
|
38
202
|
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
39
|
-
>(opts: FastifyHttp2SecureOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
203
|
+
>(opts: fastify.FastifyHttp2SecureOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
40
204
|
|
|
41
205
|
declare function fastify<
|
|
42
206
|
Server extends http2.Http2Server,
|
|
43
207
|
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
|
|
44
208
|
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
|
|
45
|
-
Logger extends FastifyBaseLogger =
|
|
209
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
|
|
46
210
|
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
47
|
-
>(opts: FastifyHttp2Options<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
211
|
+
>(opts: fastify.FastifyHttp2Options<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
48
212
|
|
|
49
213
|
declare function fastify<
|
|
50
214
|
Server extends https.Server,
|
|
51
215
|
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
|
|
52
216
|
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
|
|
53
|
-
Logger extends FastifyBaseLogger =
|
|
217
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
|
|
54
218
|
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
55
|
-
>(opts: FastifyHttpsOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
219
|
+
>(opts: fastify.FastifyHttpsOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
56
220
|
|
|
57
221
|
declare function fastify<
|
|
58
222
|
Server extends http.Server,
|
|
59
223
|
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
|
|
60
224
|
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
|
|
61
|
-
Logger extends FastifyBaseLogger =
|
|
225
|
+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
|
|
62
226
|
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
|
|
63
|
-
>(opts?: FastifyServerOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
64
|
-
|
|
65
|
-
declare namespace fastify {
|
|
66
|
-
export const errorCodes: FastifyErrorCodes;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export default fastify
|
|
70
|
-
|
|
71
|
-
export type FastifyHttp2SecureOptions<
|
|
72
|
-
Server extends http2.Http2SecureServer,
|
|
73
|
-
Logger extends FastifyBaseLogger = FastifyLoggerInstance
|
|
74
|
-
> = FastifyServerOptions<Server, Logger> & {
|
|
75
|
-
http2: true,
|
|
76
|
-
https: http2.SecureServerOptions,
|
|
77
|
-
http2SessionTimeout?: number
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export type FastifyHttp2Options<
|
|
81
|
-
Server extends http2.Http2Server,
|
|
82
|
-
Logger extends FastifyBaseLogger = FastifyLoggerInstance
|
|
83
|
-
> = FastifyServerOptions<Server, Logger> & {
|
|
84
|
-
http2: true,
|
|
85
|
-
http2SessionTimeout?: number
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export type FastifyHttpsOptions<
|
|
89
|
-
Server extends https.Server,
|
|
90
|
-
Logger extends FastifyBaseLogger = FastifyLoggerInstance
|
|
91
|
-
> = FastifyServerOptions<Server, Logger> & {
|
|
92
|
-
https: https.ServerOptions | null
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
type FindMyWayVersion<RawServer extends RawServerBase> = RawServer extends http.Server ? HTTPVersion.V1 : HTTPVersion.V2
|
|
96
|
-
|
|
97
|
-
export interface ConnectionError extends Error {
|
|
98
|
-
code: string,
|
|
99
|
-
bytesParsed: number,
|
|
100
|
-
rawPacket: {
|
|
101
|
-
type: string,
|
|
102
|
-
data: number[]
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Options for a fastify server instance. Utilizes conditional logic on the generic server parameter to enforce certain https and http2
|
|
108
|
-
*/
|
|
109
|
-
export type FastifyServerOptions<
|
|
110
|
-
RawServer extends RawServerBase = RawServerDefault,
|
|
111
|
-
Logger extends FastifyBaseLogger = FastifyLoggerInstance
|
|
112
|
-
> = {
|
|
113
|
-
ignoreTrailingSlash?: boolean,
|
|
114
|
-
ignoreDuplicateSlashes?: boolean,
|
|
115
|
-
connectionTimeout?: number,
|
|
116
|
-
keepAliveTimeout?: number,
|
|
117
|
-
maxRequestsPerSocket?: number,
|
|
118
|
-
forceCloseConnections?: boolean | 'idle',
|
|
119
|
-
requestTimeout?: number,
|
|
120
|
-
pluginTimeout?: number,
|
|
121
|
-
bodyLimit?: number,
|
|
122
|
-
maxParamLength?: number,
|
|
123
|
-
disableRequestLogging?: boolean,
|
|
124
|
-
exposeHeadRoutes?: boolean,
|
|
125
|
-
onProtoPoisoning?: ProtoAction,
|
|
126
|
-
onConstructorPoisoning?: ConstructorAction,
|
|
127
|
-
logger?: boolean | FastifyLoggerOptions<RawServer> & PinoLoggerOptions | Logger,
|
|
128
|
-
serializerOpts?: FJSOptions | Record<string, unknown>,
|
|
129
|
-
serverFactory?: FastifyServerFactory<RawServer>,
|
|
130
|
-
caseSensitive?: boolean,
|
|
131
|
-
requestIdHeader?: string | false,
|
|
132
|
-
requestIdLogLabel?: string;
|
|
133
|
-
jsonShorthand?: boolean;
|
|
134
|
-
genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>) => string,
|
|
135
|
-
trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
|
|
136
|
-
querystringParser?: (str: string) => { [key: string]: unknown },
|
|
137
|
-
/**
|
|
138
|
-
* @deprecated Prefer using the `constraints.version` property
|
|
139
|
-
*/
|
|
140
|
-
versioning?: {
|
|
141
|
-
storage(): {
|
|
142
|
-
get(version: string): string | null,
|
|
143
|
-
set(version: string, store: Function): void
|
|
144
|
-
del(version: string): void,
|
|
145
|
-
empty(): void
|
|
146
|
-
},
|
|
147
|
-
deriveVersion<Context>(req: Object, ctx?: Context): string // not a fan of using Object here. Also what is Context? Can either of these be better defined?
|
|
148
|
-
},
|
|
149
|
-
constraints?: {
|
|
150
|
-
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
|
|
151
|
-
},
|
|
152
|
-
schemaController?: {
|
|
153
|
-
bucket?: (parentSchemas?: unknown) => {
|
|
154
|
-
add(schema: unknown): FastifyInstance;
|
|
155
|
-
getSchema(schemaId: string): unknown;
|
|
156
|
-
getSchemas(): Record<string, unknown>;
|
|
157
|
-
};
|
|
158
|
-
compilersFactory?: {
|
|
159
|
-
buildValidator?: ValidatorCompiler;
|
|
160
|
-
buildSerializer?: SerializerCompiler;
|
|
161
|
-
};
|
|
162
|
-
};
|
|
163
|
-
return503OnClosing?: boolean,
|
|
164
|
-
ajv?: {
|
|
165
|
-
customOptions?: AjvOptions,
|
|
166
|
-
plugins?: (Function | [Function, unknown])[]
|
|
167
|
-
},
|
|
168
|
-
frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema>(
|
|
169
|
-
error: FastifyError,
|
|
170
|
-
req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>,
|
|
171
|
-
res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RequestGeneric, FastifyContextConfig, SchemaCompiler, TypeProvider>
|
|
172
|
-
) => void,
|
|
173
|
-
rewriteUrl?: (req: RawRequestDefaultExpression<RawServer>) => string,
|
|
174
|
-
schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error,
|
|
175
|
-
/**
|
|
176
|
-
* listener to error events emitted by client connections
|
|
177
|
-
*/
|
|
178
|
-
clientErrorHandler?: (error: ConnectionError, socket: Socket) => void
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
type TrustProxyFunction = (address: string, hop: number) => boolean
|
|
182
|
-
|
|
183
|
-
declare module '@fastify/error' {
|
|
184
|
-
interface FastifyError {
|
|
185
|
-
validation?: ValidationResult[];
|
|
186
|
-
validationContext?: 'body' | 'headers' | 'parameters' | 'querystring';
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export interface ValidationResult {
|
|
191
|
-
keyword: string;
|
|
192
|
-
instancePath: string;
|
|
193
|
-
schemaPath: string;
|
|
194
|
-
params: Record<string, string | string[]>;
|
|
195
|
-
message?: string;
|
|
196
|
-
}
|
|
227
|
+
>(opts?: fastify.FastifyServerOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & PromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
|
|
197
228
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
export
|
|
201
|
-
export { FastifyReply } from './types/reply'
|
|
202
|
-
export { FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin } from './types/plugin'
|
|
203
|
-
export { FastifyListenOptions, FastifyInstance, PrintRoutesOptions } from './types/instance'
|
|
204
|
-
export { FastifyLoggerOptions, FastifyBaseLogger, FastifyLoggerInstance, FastifyLogFn, LogLevel } from './types/logger'
|
|
205
|
-
export { FastifyContext, FastifyContextConfig } from './types/context'
|
|
206
|
-
export { RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler, RouteGenericInterface } from './types/route'
|
|
207
|
-
export * from './types/register'
|
|
208
|
-
export { FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction } from './types/content-type-parser'
|
|
209
|
-
export { FastifyError } from '@fastify/error'
|
|
210
|
-
export { FastifySchema, FastifySchemaCompiler } from './types/schema'
|
|
211
|
-
export { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
|
|
212
|
-
export * from './types/hooks'
|
|
213
|
-
export { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
|
|
214
|
-
export { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
|
|
215
|
-
export { FastifyErrorCodes } from './types/errors'
|
|
216
|
-
export { fastify }
|
|
229
|
+
// CJS export
|
|
230
|
+
// const fastify = require('fastify')
|
|
231
|
+
export = fastify
|
package/fastify.js
CHANGED
package/lib/contentTypeParser.js
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { AsyncResource } = require('async_hooks')
|
|
4
4
|
const lru = require('tiny-lru').lru
|
|
5
|
-
|
|
6
|
-
const { parse: parseContentType } = require('content-type')
|
|
7
|
-
|
|
5
|
+
const { safeParse: safeParseContentType, defaultContentType } = require('fast-content-type-parse')
|
|
8
6
|
const secureJson = require('secure-json-parse')
|
|
9
7
|
const {
|
|
10
8
|
kDefaultJsonParse,
|
|
@@ -104,7 +102,7 @@ ContentTypeParser.prototype.getParser = function (contentType) {
|
|
|
104
102
|
|
|
105
103
|
// dummyContentType always the same object
|
|
106
104
|
// we can use === for the comparsion and return early
|
|
107
|
-
if (parsed ===
|
|
105
|
+
if (parsed === defaultContentType) {
|
|
108
106
|
return this.customParsers.get('')
|
|
109
107
|
}
|
|
110
108
|
|
|
@@ -308,6 +306,7 @@ function buildContentTypeParser (c) {
|
|
|
308
306
|
contentTypeParser[kDefaultJsonParse] = c[kDefaultJsonParse]
|
|
309
307
|
contentTypeParser.customParsers = new Map(c.customParsers.entries())
|
|
310
308
|
contentTypeParser.parserList = c.parserList.slice()
|
|
309
|
+
contentTypeParser.parserRegExpList = c.parserRegExpList.slice()
|
|
311
310
|
return contentTypeParser
|
|
312
311
|
}
|
|
313
312
|
|
|
@@ -359,17 +358,6 @@ function removeAllContentTypeParsers () {
|
|
|
359
358
|
this[kContentTypeParser].removeAll()
|
|
360
359
|
}
|
|
361
360
|
|
|
362
|
-
// dummy here to prevent repeated object creation
|
|
363
|
-
const dummyContentType = { type: '', parameters: Object.create(null) }
|
|
364
|
-
|
|
365
|
-
function safeParseContentType (contentType) {
|
|
366
|
-
try {
|
|
367
|
-
return parseContentType(contentType)
|
|
368
|
-
} catch (err) {
|
|
369
|
-
return dummyContentType
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
361
|
function compareContentType (contentType, parserListItem) {
|
|
374
362
|
if (parserListItem.isEssence) {
|
|
375
363
|
// we do essence check
|
package/lib/error-serializer.js
CHANGED
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
+
// eslint-disable-next-line
|
|
9
|
+
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
|
|
10
|
+
|
|
8
11
|
class Serializer {
|
|
9
12
|
constructor (options = {}) {
|
|
10
13
|
switch (options.rounding) {
|
|
@@ -99,6 +102,11 @@ class Serializer {
|
|
|
99
102
|
str = str.toString()
|
|
100
103
|
}
|
|
101
104
|
|
|
105
|
+
// Fast escape chars check
|
|
106
|
+
if (!STR_ESCAPE.test(str)) {
|
|
107
|
+
return quotes + str + quotes
|
|
108
|
+
}
|
|
109
|
+
|
|
102
110
|
if (str.length < 42) {
|
|
103
111
|
return this.asStringSmall(str)
|
|
104
112
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.12.0",
|
|
4
4
|
"description": "Fast and low overhead web framework, for Node.js",
|
|
5
5
|
"main": "fastify.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -154,7 +154,7 @@
|
|
|
154
154
|
"json-schema-to-ts": "^2.5.5",
|
|
155
155
|
"JSONStream": "^1.3.5",
|
|
156
156
|
"license-checker": "^25.0.1",
|
|
157
|
-
"markdownlint-cli2": "^0.
|
|
157
|
+
"markdownlint-cli2": "^0.6.0",
|
|
158
158
|
"proxyquire": "^2.1.3",
|
|
159
159
|
"pump": "^3.0.0",
|
|
160
160
|
"self-cert": "^2.0.0",
|
|
@@ -176,7 +176,7 @@
|
|
|
176
176
|
"@fastify/fast-json-stringify-compiler": "^4.1.0",
|
|
177
177
|
"abstract-logging": "^2.0.1",
|
|
178
178
|
"avvio": "^8.2.0",
|
|
179
|
-
"content-type": "^1.0.
|
|
179
|
+
"fast-content-type-parse": "^1.0.0",
|
|
180
180
|
"find-my-way": "^7.3.0",
|
|
181
181
|
"light-my-request": "^5.6.1",
|
|
182
182
|
"pino": "^8.5.0",
|
|
@@ -609,3 +609,46 @@ test('content-type fail when parameters not match - regexp', async t => {
|
|
|
609
609
|
|
|
610
610
|
t.same(response.statusCode, 415)
|
|
611
611
|
})
|
|
612
|
+
|
|
613
|
+
// Refs: https://github.com/fastify/fastify/issues/4495
|
|
614
|
+
test('content-type regexp list should be cloned when plugin override', async t => {
|
|
615
|
+
t.plan(6)
|
|
616
|
+
|
|
617
|
+
const fastify = Fastify()
|
|
618
|
+
|
|
619
|
+
fastify.addContentTypeParser(/^image\/.*/, { parseAs: 'buffer' }, (req, payload, done) => {
|
|
620
|
+
done(null, payload)
|
|
621
|
+
})
|
|
622
|
+
|
|
623
|
+
fastify.register(function plugin (fastify, options, done) {
|
|
624
|
+
fastify.post('/', function (request, reply) {
|
|
625
|
+
reply.type(request.headers['content-type']).send(request.body)
|
|
626
|
+
})
|
|
627
|
+
|
|
628
|
+
done()
|
|
629
|
+
})
|
|
630
|
+
|
|
631
|
+
{
|
|
632
|
+
const { payload, headers, statusCode } = await fastify.inject({
|
|
633
|
+
method: 'POST',
|
|
634
|
+
path: '/',
|
|
635
|
+
payload: 'jpeg',
|
|
636
|
+
headers: { 'content-type': 'image/jpeg' }
|
|
637
|
+
})
|
|
638
|
+
t.same(statusCode, 200)
|
|
639
|
+
t.same(headers['content-type'], 'image/jpeg')
|
|
640
|
+
t.same(payload, 'jpeg')
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
{
|
|
644
|
+
const { payload, headers, statusCode } = await fastify.inject({
|
|
645
|
+
method: 'POST',
|
|
646
|
+
path: '/',
|
|
647
|
+
payload: 'png',
|
|
648
|
+
headers: { 'content-type': 'image/png' }
|
|
649
|
+
})
|
|
650
|
+
t.same(statusCode, 200)
|
|
651
|
+
t.same(headers['content-type'], 'image/png')
|
|
652
|
+
t.same(payload, 'png')
|
|
653
|
+
}
|
|
654
|
+
})
|
|
@@ -12,6 +12,7 @@ import { FastifyReply } from '../../types/reply'
|
|
|
12
12
|
import { FastifyRequest } from '../../types/request'
|
|
13
13
|
import { DefaultRoute } from '../../types/route'
|
|
14
14
|
import { FastifySchemaControllerOptions, FastifySchemaCompiler, FastifySerializerCompiler } from '../../types/schema'
|
|
15
|
+
import { AddressInfo } from 'net'
|
|
15
16
|
|
|
16
17
|
const server = fastify()
|
|
17
18
|
|
|
@@ -26,6 +27,7 @@ expectAssignable<FastifyInstance>(server.addSchema({
|
|
|
26
27
|
}))
|
|
27
28
|
|
|
28
29
|
expectType<Record<string, unknown>>(server.getSchemas())
|
|
30
|
+
expectType<AddressInfo[]>(server.addresses())
|
|
29
31
|
expectType<unknown>(server.getSchema('SchemaId'))
|
|
30
32
|
expectType<string>(server.printRoutes())
|
|
31
33
|
expectType<string>(server.printPlugins())
|
package/types/instance.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
FastifyTypeProviderDefault
|
|
22
22
|
} from './type-provider'
|
|
23
23
|
import { ContextConfigDefault, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
|
|
24
|
+
import { AddressInfo } from 'net'
|
|
24
25
|
|
|
25
26
|
export interface PrintRoutesOptions {
|
|
26
27
|
includeMeta?: boolean | (string | symbol)[]
|
|
@@ -92,6 +93,7 @@ export interface FastifyInstance<
|
|
|
92
93
|
version: string;
|
|
93
94
|
log: Logger;
|
|
94
95
|
|
|
96
|
+
addresses(): AddressInfo[]
|
|
95
97
|
withTypeProvider<Provider extends FastifyTypeProvider>(): FastifyInstance<RawServer, RawRequest, RawReply, Logger, Provider>;
|
|
96
98
|
|
|
97
99
|
addSchema(schema: unknown): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
|