Spaces:
Runtime error
Runtime error
File size: 13,361 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
import * as http from 'node:http'
import * as http2 from 'node:http2'
import * as https from 'node:https'
import { Socket } from 'node:net'
import { Options as AjvOptions, ValidatorFactory } from '@fastify/ajv-compiler'
import { FastifyError } from '@fastify/error'
import { Options as FJSOptions, SerializerFactory } from '@fastify/fast-json-stringify-compiler'
import { ConstraintStrategy, HTTPVersion } from 'find-my-way'
import { InjectOptions, CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, Response as LightMyRequestResponse } from 'light-my-request'
import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, FastifyContentTypeParser, getDefaultJsonParser, hasContentTypeParser, ProtoAction } from './types/content-type-parser'
import { FastifyContextConfig, FastifyReplyContext, FastifyRequestContext } from './types/context'
import { FastifyErrorCodes } from './types/errors'
import { DoneFuncWithErrOrRes, HookHandlerDoneFunction, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onListenAsyncHookHandler, onListenHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAbortAsyncHookHandler, onRequestAbortHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preCloseAsyncHookHandler, preCloseHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, RequestPayload } from './types/hooks'
import { FastifyInstance, FastifyListenOptions, PrintRoutesOptions } from './types/instance'
import {
FastifyBaseLogger,
FastifyChildLoggerFactory,
FastifyLogFn,
FastifyLoggerInstance,
FastifyLoggerOptions,
LogLevel,
PinoLoggerOptions
} from './types/logger'
import { FastifyPlugin, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions } from './types/plugin'
import { FastifyRegister, FastifyRegisterOptions, RegisterOptions } from './types/register'
import { FastifyReply } from './types/reply'
import { FastifyRequest, RequestGenericInterface } from './types/request'
import { RouteGenericInterface, RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler } from './types/route'
import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, SchemaErrorDataVar, SchemaErrorFormatter } from './types/schema'
import { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
import { FastifyTypeProvider, FastifyTypeProviderDefault, SafePromiseLike } from './types/type-provider'
import { ContextConfigDefault, HTTPMethods, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from './types/utils'
declare module '@fastify/error' {
interface FastifyError {
validationContext?: SchemaErrorDataVar;
validation?: FastifySchemaValidationError[];
}
}
type Fastify = typeof fastify
declare namespace fastify {
export const errorCodes: FastifyErrorCodes
export type FastifyHttp2SecureOptions<
Server extends http2.Http2SecureServer,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyServerOptions<Server, Logger> & {
http2: true,
https: http2.SecureServerOptions,
http2SessionTimeout?: number
}
export type FastifyHttp2Options<
Server extends http2.Http2Server,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyServerOptions<Server, Logger> & {
http2: true,
http2SessionTimeout?: number
}
export type FastifyHttpsOptions<
Server extends https.Server,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyServerOptions<Server, Logger> & {
https: https.ServerOptions | null
}
export type FastifyHttpOptions<
Server extends http.Server,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = FastifyServerOptions<Server, Logger> & {
http?: http.ServerOptions | null
}
type FindMyWayVersion<RawServer extends RawServerBase> = RawServer extends http.Server ? HTTPVersion.V1 : HTTPVersion.V2
export interface ConnectionError extends Error {
code: string,
bytesParsed: number,
rawPacket: {
type: string,
data: number[]
}
}
type TrustProxyFunction = (address: string, hop: number) => boolean
/**
* Options for a fastify server instance. Utilizes conditional logic on the generic server parameter to enforce certain https and http2
*/
export type FastifyServerOptions<
RawServer extends RawServerBase = RawServerDefault,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> = {
ignoreTrailingSlash?: boolean,
ignoreDuplicateSlashes?: boolean,
connectionTimeout?: number,
keepAliveTimeout?: number,
maxRequestsPerSocket?: number,
forceCloseConnections?: boolean | 'idle',
requestTimeout?: number,
pluginTimeout?: number,
bodyLimit?: number,
maxParamLength?: number,
disableRequestLogging?: boolean,
exposeHeadRoutes?: boolean,
onProtoPoisoning?: ProtoAction,
onConstructorPoisoning?: ConstructorAction,
logger?: boolean | FastifyLoggerOptions<RawServer> & PinoLoggerOptions,
loggerInstance?: Logger
serializerOpts?: FJSOptions | Record<string, unknown>,
serverFactory?: FastifyServerFactory<RawServer>,
caseSensitive?: boolean,
allowUnsafeRegex?: boolean,
requestIdHeader?: string | false,
requestIdLogLabel?: string;
useSemicolonDelimiter?: boolean,
genReqId?: (req: RawRequestDefaultExpression<RawServer>) => string,
trustProxy?: boolean | string | string[] | number | TrustProxyFunction,
querystringParser?: (str: string) => { [key: string]: unknown },
constraints?: {
[name: string]: ConstraintStrategy<FindMyWayVersion<RawServer>, unknown>,
},
schemaController?: {
bucket?: (parentSchemas?: unknown) => {
add(schema: unknown): FastifyInstance;
getSchema(schemaId: string): unknown;
getSchemas(): Record<string, unknown>;
};
compilersFactory?: {
buildValidator?: ValidatorFactory;
buildSerializer?: SerializerFactory;
};
};
return503OnClosing?: boolean,
ajv?: {
customOptions?: AjvOptions,
plugins?: (Function | [Function, unknown])[]
},
frameworkErrors?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, SchemaCompiler extends FastifySchema = FastifySchema>(
error: FastifyError,
req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>,
res: FastifyReply<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, FastifyContextConfig, SchemaCompiler, TypeProvider>
) => void,
rewriteUrl?: (
// The RawRequestDefaultExpression, RawReplyDefaultExpression, and FastifyTypeProviderDefault parameters
// should be narrowed further but those generic parameters are not passed to this FastifyServerOptions type
this: FastifyInstance<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, Logger, FastifyTypeProviderDefault>,
req: RawRequestDefaultExpression<RawServer>
) => string,
schemaErrorFormatter?: SchemaErrorFormatter,
/**
* listener to error events emitted by client connections
*/
clientErrorHandler?: (error: ConnectionError, socket: Socket) => void,
childLoggerFactory?: FastifyChildLoggerFactory
}
/**
* @deprecated use {@link FastifySchemaValidationError}
*/
export type ValidationResult = FastifySchemaValidationError
/* Export additional types */
export type {
LightMyRequestChain, InjectOptions, LightMyRequestResponse, LightMyRequestCallback, // 'light-my-request'
FastifyRequest, RequestGenericInterface, // './types/request'
FastifyReply, // './types/reply'
FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin, // './types/plugin'
FastifyListenOptions, FastifyInstance, PrintRoutesOptions, // './types/instance'
FastifyLoggerOptions, FastifyBaseLogger, FastifyLoggerInstance, FastifyLogFn, LogLevel, // './types/logger'
FastifyRequestContext, FastifyContextConfig, FastifyReplyContext, // './types/context'
RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler, RouteGenericInterface, // './types/route'
FastifyRegister, FastifyRegisterOptions, RegisterOptions, // './types/register'
FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, // './types/content-type-parser'
FastifyError, // '@fastify/error'
FastifySchema, FastifySchemaCompiler, // './types/schema'
HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault, // './types/utils'
DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onListenAsyncHookHandler, onListenHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, onRequestAbortHookHandler, onRequestAbortAsyncHookHandler, preCloseAsyncHookHandler, preCloseHookHandler, // './types/hooks'
FastifyServerFactory, FastifyServerFactoryHandler, // './types/serverFactory'
FastifyTypeProvider, FastifyTypeProviderDefault, SafePromiseLike, // './types/type-provider'
FastifyErrorCodes // './types/errors'
}
// named export
// import { plugin } from 'plugin'
// const { plugin } = require('plugin')
export const fastify: Fastify
// default export
// import plugin from 'plugin'
export { fastify as default }
}
/**
* Fastify factory function for the standard fastify http, https, or http2 server instance.
*
* The default function utilizes http
*
* @param opts Fastify server options
* @returns Fastify server instance
*/
declare function fastify<
Server extends http2.Http2SecureServer,
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
> (opts: fastify.FastifyHttp2SecureOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & SafePromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
declare function fastify<
Server extends http2.Http2Server,
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
> (opts: fastify.FastifyHttp2Options<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & SafePromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
declare function fastify<
Server extends https.Server,
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
> (opts: fastify.FastifyHttpsOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & SafePromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
declare function fastify<
Server extends http.Server,
Request extends RawRequestDefaultExpression<Server> = RawRequestDefaultExpression<Server>,
Reply extends RawReplyDefaultExpression<Server> = RawReplyDefaultExpression<Server>,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
> (opts?: fastify.FastifyHttpOptions<Server, Logger>): FastifyInstance<Server, Request, Reply, Logger, TypeProvider> & SafePromiseLike<FastifyInstance<Server, Request, Reply, Logger, TypeProvider>>
// CJS export
// const fastify = require('fastify')
export = fastify
|