'use strict'
const VERSION = '6.0.0-alpha.4'
const Avvio = require('avvio')
const http = require('node:http')
const diagnostics = require('node:diagnostics_channel')
let lightMyRequest
const {
kAvvioBoot,
kChildren,
kServerBindings,
kBodyLimit,
kSupportedHTTPMethods,
kRoutePrefix,
kLogLevel,
kLogSerializers,
kHooks,
kSchemaController,
kRequestAcceptVersion,
kReplySerializerDefault,
kContentTypeParser,
kReply,
kRequest,
kFourOhFour,
kState,
kOptions,
kPluginNameChain,
kSchemaErrorFormatter,
kErrorHandler,
kKeepAliveConnections,
kChildLoggerFactory,
kGenReqId,
kErrorHandlerAlreadySet,
kHandlerTimeout,
kLogController
} = require('./lib/symbols.js')
const { createServer } = require('./lib/server')
const Reply = require('./lib/reply')
const Request = require('./lib/request')
const Context = require('./lib/context.js')
const decorator = require('./lib/decorate')
const ContentTypeParser = require('./lib/content-type-parser.js')
const SchemaController = require('./lib/schema-controller')
const { Hooks, hookRunnerApplication, supportedHooks } = require('./lib/hooks')
const { createChildLogger, defaultChildLoggerFactory, createLogger, createLogController, LogController } = require('./lib/logger-factory')
const pluginUtils = require('./lib/plugin-utils.js')
const { getGenReqId, reqIdGenFactory } = require('./lib/req-id-gen-factory.js')
const { buildRouting, validateBodyLimitOption, buildRouterOptions } = require('./lib/route')
const build404 = require('./lib/four-oh-four')
const getSecuredInitialConfig = require('./lib/initial-config-validation.js')
const override = require('./lib/plugin-override')
const {
appendStackTrace,
AVVIO_ERRORS_MAP,
...errorCodes
} = require('./lib/errors')
const { defaultInitOptions } = getSecuredInitialConfig
const {
FST_ERR_ASYNC_CONSTRAINT,
FST_ERR_BAD_URL,
FST_ERR_MAX_PARAM_LENGTH,
FST_ERR_OPTIONS_NOT_OBJ,
FST_ERR_QSP_NOT_FN,
FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN,
FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ,
FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR,
FST_ERR_INSTANCE_ALREADY_STARTED,
FST_ERR_REOPENED_CLOSE_SERVER,
FST_ERR_ROUTE_REWRITE_NOT_STR,
FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN,
FST_ERR_ERROR_HANDLER_NOT_FN,
FST_ERR_ERROR_HANDLER_ALREADY_SET,
FST_ERR_ROUTE_METHOD_INVALID,
FST_ERR_ROUTE_METHOD_ALREADY_SUPPORTED
} = errorCodes
const { buildErrorHandler } = require('./lib/error-handler.js')
const initChannel = diagnostics.channel('fastify.initialization')
* @param {import('./fastify.js').FastifyServerOptions} serverOptions
*/
function fastify (serverOptions) {
const {
options,
genReqId,
logController,
hasLogger,
initialConfig
} = processOptions(serverOptions, defaultRoute, onBadUrl, onMaxParamLength)
const router = buildRouting(options.routerOptions)
const fourOhFour = build404(options)
const httpHandler = wrapRouting(router, options)
const {
server,
listen,
forceCloseConnections,
serverHasCloseAllConnections,
serverHasCloseHttp2Sessions,
keepAliveConnections
} = createServer(options, httpHandler)
const setupResponseListeners = Reply.setupResponseListeners
const schemaController = SchemaController.buildSchemaController(null, options.schemaController)
const fastify = {
[kState]: {
listening: false,
closing: false,
started: false,
ready: false,
booting: false,
aborted: false,
readyResolver: null
},
[kKeepAliveConnections]: keepAliveConnections,
[kSupportedHTTPMethods]: {
bodyless: new Set([
'GET',
'HEAD',
'TRACE'
]),
bodywith: new Set([
'DELETE',
'OPTIONS',
'PATCH',
'PUT',
'POST',
'QUERY'
])
},
[kOptions]: options,
[kChildren]: [],
[kServerBindings]: [],
[kBodyLimit]: options.bodyLimit,
[kHandlerTimeout]: options.handlerTimeout,
[kRoutePrefix]: '',
[kLogLevel]: '',
[kLogSerializers]: null,
[kHooks]: new Hooks(),
[kSchemaController]: schemaController,
[kSchemaErrorFormatter]: null,
[kErrorHandler]: buildErrorHandler(),
[kErrorHandlerAlreadySet]: false,
[kChildLoggerFactory]: options.childLoggerFactory || defaultChildLoggerFactory,
[kReplySerializerDefault]: null,
[kContentTypeParser]: new ContentTypeParser(
options.bodyLimit,
(options.onProtoPoisoning || defaultInitOptions.onProtoPoisoning),
(options.onConstructorPoisoning || defaultInitOptions.onConstructorPoisoning)
),
[kReply]: Reply.buildReply(Reply),
[kRequest]: Request.buildRequest(Request, options.trustProxy),
[kFourOhFour]: fourOhFour,
[pluginUtils.kRegisteredPlugins]: [],
[kPluginNameChain]: ['fastify'],
[kAvvioBoot]: null,
[kGenReqId]: genReqId,
routing: httpHandler,
delete: function _delete (url, options, handler) {
return router.prepareRoute.call(this, { method: 'DELETE', url, options, handler })
},
get: function _get (url, options, handler) {
return router.prepareRoute.call(this, { method: 'GET', url, options, handler })
},
head: function _head (url, options, handler) {
return router.prepareRoute.call(this, { method: 'HEAD', url, options, handler })
},
trace: function _trace (url, options, handler) {
return router.prepareRoute.call(this, { method: 'TRACE', url, options, handler })
},
patch: function _patch (url, options, handler) {
return router.prepareRoute.call(this, { method: 'PATCH', url, options, handler })
},
post: function _post (url, options, handler) {
return router.prepareRoute.call(this, { method: 'POST', url, options, handler })
},
put: function _put (url, options, handler) {
return router.prepareRoute.call(this, { method: 'PUT', url, options, handler })
},
options: function _options (url, options, handler) {
return router.prepareRoute.call(this, { method: 'OPTIONS', url, options, handler })
},
query: function _query (url, options, handler) {
return router.prepareRoute.call(this, { method: 'QUERY', url, options, handler })
},
all: function _all (url, options, handler) {
return router.prepareRoute.call(this, { method: this.supportedMethods, url, options, handler })
},
route: function _route (options) {
return router.route.call(this, { options })
},
hasRoute: function _route (options) {
return router.hasRoute.call(this, { options })
},
findRoute: function _findRoute (options) {
return router.findRoute(options)
},
log: options.logger,
[kLogController]: logController,
withTypeProvider,
addHook,
addSchema,
getSchema: schemaController.getSchema.bind(schemaController),
getSchemas: schemaController.getSchemas.bind(schemaController),
setValidatorCompiler,
setSerializerCompiler,
setSchemaController,
setReplySerializer,
setSchemaErrorFormatter,
setGenReqId,
addContentTypeParser: ContentTypeParser.helpers.addContentTypeParser,
hasContentTypeParser: ContentTypeParser.helpers.hasContentTypeParser,
getDefaultJsonParser: ContentTypeParser.defaultParsers.getDefaultJsonParser,
defaultTextParser: ContentTypeParser.defaultParsers.defaultTextParser,
removeContentTypeParser: ContentTypeParser.helpers.removeContentTypeParser,
removeAllContentTypeParsers: ContentTypeParser.helpers.removeAllContentTypeParsers,
register: null,
after: null,
ready: null,
onClose: null,
close: null,
printPlugins: null,
hasPlugin: function (name) {
return this[pluginUtils.kRegisteredPlugins].includes(name) || this[kPluginNameChain].includes(name)
},
listen,
server,
addresses: function () {
const binded = this[kServerBindings].map(b => b.address())
binded.push(this.server.address())
return binded.filter(adr => adr)
},
decorate: decorator.add,
hasDecorator: decorator.exist,
decorateReply: decorator.decorateReply,
decorateRequest: decorator.decorateRequest,
hasRequestDecorator: decorator.existRequest,
hasReplyDecorator: decorator.existReply,
getDecorator: decorator.getInstanceDecorator,
addHttpMethod,
inject,
printRoutes,
setNotFoundHandler,
setErrorHandler,
setChildLoggerFactory,
initialConfig,
addConstraintStrategy: router.addConstraintStrategy.bind(router),
hasConstraintStrategy: router.hasConstraintStrategy.bind(router)
}
Object.defineProperties(fastify, {
listeningOrigin: {
get () {
const address = this.addresses().slice(-1).pop()
if (typeof address === 'string') {
return address
}
const host = address.family === 'IPv6' ? `[${address.address}]` : address.address
return `${this[kOptions].https ? 'https' : 'http'}://${host}:${address.port}`
}
},
pluginName: {
configurable: true,
get () {
if (this[kPluginNameChain].length > 1) {
return this[kPluginNameChain].join(' -> ')
}
return this[kPluginNameChain][0]
}
},
prefix: {
configurable: true,
get () { return this[kRoutePrefix] }
},
validatorCompiler: {
configurable: true,
get () { return this[kSchemaController].getValidatorCompiler() }
},
serializerCompiler: {
configurable: true,
get () { return this[kSchemaController].getSerializerCompiler() }
},
childLoggerFactory: {
configurable: true,
get () { return this[kChildLoggerFactory] }
},
version: {
configurable: true,
get () { return VERSION }
},
errorHandler: {
configurable: true,
get () {
return this[kErrorHandler].func
}
},
genReqId: {
configurable: true,
get () { return this[kGenReqId] }
},
supportedMethods: {
configurable: false,
get () {
return [
...this[kSupportedHTTPMethods].bodyless,
...this[kSupportedHTTPMethods].bodywith
]
}
}
})
if (options.schemaErrorFormatter) {
validateSchemaErrorFormatter(options.schemaErrorFormatter)
fastify[kSchemaErrorFormatter] = options.schemaErrorFormatter.bind(fastify)
}
const avvioPluginTimeout = Number(options.pluginTimeout)
const avvio = Avvio(fastify, {
autostart: false,
timeout: isNaN(avvioPluginTimeout) === false ? avvioPluginTimeout : defaultInitOptions.pluginTimeout,
expose: {
use: 'register'
}
})
avvio.override = override
avvio.on('start', () => (fastify[kState].started = true))
fastify[kAvvioBoot] = fastify.ready
fastify.ready = ready
fastify.printPlugins = avvio.prettyPrint.bind(avvio)
avvio.once('preReady', () => {
fastify.onClose((instance, done) => {
fastify[kState].closing = true
router.closeRoutes()
hookRunnerApplication('preClose', fastify[kAvvioBoot], fastify, function () {
if (fastify[kState].listening) {
if (forceCloseConnections === 'idle' && options.serverFactory) {
instance.server.closeIdleConnections()
} else if (serverHasCloseAllConnections && forceCloseConnections === true) {
instance.server.closeAllConnections()
} else if (forceCloseConnections === true) {
for (const conn of fastify[kKeepAliveConnections]) {
conn.destroy()
fastify[kKeepAliveConnections].delete(conn)
}
}
}
if (serverHasCloseHttp2Sessions) {
instance.server.closeHttp2Sessions()
}
if (!options.serverFactory || fastify[kState].listening) {
instance.server.close(function (err) {
if (err && err.code !== 'ERR_SERVER_NOT_RUNNING') {
done(null)
} else {
done()
}
})
} else {
process.nextTick(done, null)
}
})
})
})
const routeEventContext = new Context({
server: fastify,
config: {}
})
fastify.setNotFoundHandler()
fourOhFour.arrange404(fastify)
router.setup(options, {
avvio,
fourOhFour,
hasLogger,
setupResponseListeners,
throwIfAlreadyStarted,
keepAliveConnections
})
server.on('clientError', options.clientErrorHandler.bind(fastify))
if (initChannel.hasSubscribers) {
initChannel.publish({ fastify })
}
if ('asyncDispose' in Symbol) {
fastify[Symbol.asyncDispose] = function dispose () {
return fastify.close()
}
}
return fastify
function throwIfAlreadyStarted (msg) {
if (fastify[kState].started) throw new FST_ERR_INSTANCE_ALREADY_STARTED(msg)
}
function inject (opts, cb) {
if (lightMyRequest === undefined) {
lightMyRequest = require('light-my-request')
}
if (fastify[kState].started) {
if (fastify[kState].closing) {
const error = new FST_ERR_REOPENED_CLOSE_SERVER()
if (cb) {
cb(error)
return
} else {
return Promise.reject(error)
}
}
return lightMyRequest(httpHandler, opts, cb)
}
if (cb) {
this.ready(err => {
if (err) cb(err, null)
else lightMyRequest(httpHandler, opts, cb)
})
} else {
return lightMyRequest((req, res) => {
this.ready(function (err) {
if (err) {
res.emit('error', err)
return
}
httpHandler(req, res)
})
}, opts)
}
}
function ready (cb) {
if (this[kState].readyResolver !== null) {
if (cb != null) {
this[kState].readyResolver.promise.then(() => cb(null, fastify), cb)
return
}
return this[kState].readyResolver.promise
}
process.nextTick(runHooks)
this[kState].readyResolver = Promise.withResolvers()
if (!cb) {
return this[kState].readyResolver.promise
} else {
this[kState].readyResolver.promise.then(() => cb(null, fastify), cb)
}
function runHooks () {
fastify[kAvvioBoot]((err, done) => {
if (err || fastify[kState].started || fastify[kState].ready || fastify[kState].booting) {
manageErr(err)
} else {
fastify[kState].booting = true
hookRunnerApplication('onReady', fastify[kAvvioBoot], fastify, manageErr)
}
done()
})
}
function manageErr (err) {
err = err != null && AVVIO_ERRORS_MAP[err.code] != null
? appendStackTrace(err, new AVVIO_ERRORS_MAP[err.code](err.message))
: err
if (err) {
return fastify[kState].readyResolver.reject(err)
}
fastify[kState].readyResolver.resolve(fastify)
fastify[kState].booting = false
fastify[kState].ready = true
fastify[kState].readyResolver = null
}
}
function withTypeProvider () {
return this
}
function addHook (name, fn) {
throwIfAlreadyStarted('Cannot call "addHook"!')
if (fn == null) {
throw new errorCodes.FST_ERR_HOOK_INVALID_HANDLER(name, fn)
}
if (name === 'onSend' || name === 'preSerialization' || name === 'onError' || name === 'preParsing') {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 4) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else if (name === 'onReady' || name === 'onListen') {
if (fn.constructor.name === 'AsyncFunction' && fn.length !== 0) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else if (name === 'onRequestAbort') {
if (fn.constructor.name === 'AsyncFunction' && fn.length !== 1) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
}
if (name === 'onClose') {
this.onClose(fn.bind(this))
} else if (name === 'onReady' || name === 'onListen' || name === 'onRoute' || name === 'preClose') {
this[kHooks].add(name, fn)
} else {
this.after((err, done) => {
try {
_addHook.call(this, name, fn)
done(err)
} catch (err) {
done(err)
}
})
}
return this
function _addHook (name, fn) {
this[kHooks].add(name, fn)
this[kChildren].forEach(child => _addHook.call(child, name, fn))
}
}
function addSchema (schema) {
throwIfAlreadyStarted('Cannot call "addSchema"!')
this[kSchemaController].add(schema)
this[kChildren].forEach(child => child.addSchema(schema))
return this
}
function defaultRoute (req, res) {
if (req.headers['accept-version'] !== undefined) {
req.headers[kRequestAcceptVersion] = req.headers['accept-version']
req.headers['accept-version'] = undefined
}
fourOhFour.router.lookup(req, res)
}
function onBadUrl (path, req, res) {
if (options.frameworkErrors) {
const id = getGenReqId(routeEventContext.server, req)
const childLogger = createChildLogger(routeEventContext, options.logger, req, id)
const request = new Request(id, null, req, null, childLogger, routeEventContext)
const reply = new Reply(res, request, childLogger)
routeEventContext.server[kLogController].incomingRequest(request, reply)
return options.frameworkErrors(new FST_ERR_BAD_URL(path), request, reply)
}
const body = JSON.stringify({
error: 'Bad Request',
code: 'FST_ERR_BAD_URL',
message: `'${path}' is not a valid url component`,
statusCode: 400
})
res.writeHead(400, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body)
})
res.end(body)
}
function onMaxParamLength (path, req, res) {
if (options.frameworkErrors) {
const id = getGenReqId(routeEventContext.server, req)
const childLogger = createChildLogger(routeEventContext, options.logger, req, id)
const request = new Request(id, null, req, null, childLogger, routeEventContext)
const reply = new Reply(res, request, childLogger)
routeEventContext.server[kLogController].incomingRequest(request, reply)
return options.frameworkErrors(new FST_ERR_MAX_PARAM_LENGTH(path), request, reply)
}
const body = JSON.stringify({
error: 'Bad Request',
code: 'FST_ERR_MAX_PARAM_LENGTH',
message: `'${path}' is exceeding the max param length`,
statusCode: 414
})
res.writeHead(414, {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body)
})
res.end(body)
}
function buildAsyncConstraintCallback (isAsync, req, res) {
if (isAsync === false) return undefined
return function onAsyncConstraintError (err) {
if (err) {
if (options.frameworkErrors) {
const id = getGenReqId(routeEventContext.server, req)
const childLogger = createChildLogger(routeEventContext, options.logger, req, id)
const request = new Request(id, null, req, null, childLogger, routeEventContext)
const reply = new Reply(res, request, childLogger)
routeEventContext.server[kLogController].incomingRequest(request, reply)
return options.frameworkErrors(new FST_ERR_ASYNC_CONSTRAINT(), request, reply)
}
const body = '{"error":"Internal Server Error","message":"Unexpected error from async constraint","statusCode":500}'
res.writeHead(500, {
'Content-Type': 'application/json',
'Content-Length': body.length
})
res.end(body)
}
}
}
function setNotFoundHandler (opts, handler) {
throwIfAlreadyStarted('Cannot call "setNotFoundHandler"!')
fourOhFour.setNotFoundHandler.call(this, opts, handler, avvio, router.routeHandler)
return this
}
function setValidatorCompiler (validatorCompiler) {
throwIfAlreadyStarted('Cannot call "setValidatorCompiler"!')
this[kSchemaController].setValidatorCompiler(validatorCompiler)
return this
}
function setSchemaErrorFormatter (errorFormatter) {
throwIfAlreadyStarted('Cannot call "setSchemaErrorFormatter"!')
validateSchemaErrorFormatter(errorFormatter)
this[kSchemaErrorFormatter] = errorFormatter.bind(this)
return this
}
function setSerializerCompiler (serializerCompiler) {
throwIfAlreadyStarted('Cannot call "setSerializerCompiler"!')
this[kSchemaController].setSerializerCompiler(serializerCompiler)
return this
}
function setSchemaController (schemaControllerOpts) {
throwIfAlreadyStarted('Cannot call "setSchemaController"!')
const old = this[kSchemaController]
const schemaController = SchemaController.buildSchemaController(
old,
Object.assign({}, old.opts, schemaControllerOpts)
)
this[kSchemaController] = schemaController
this.getSchema = schemaController.getSchema.bind(schemaController)
this.getSchemas = schemaController.getSchemas.bind(schemaController)
return this
}
function setReplySerializer (replySerializer) {
throwIfAlreadyStarted('Cannot call "setReplySerializer"!')
this[kReplySerializerDefault] = replySerializer
return this
}
function setErrorHandler (func) {
throwIfAlreadyStarted('Cannot call "setErrorHandler"!')
if (typeof func !== 'function') {
throw new FST_ERR_ERROR_HANDLER_NOT_FN()
}
if (this[kErrorHandlerAlreadySet] && !options.allowErrorHandlerOverride) {
throw new FST_ERR_ERROR_HANDLER_ALREADY_SET()
}
this[kErrorHandlerAlreadySet] = true
this[kErrorHandler] = buildErrorHandler(this[kErrorHandler], func.bind(this))
return this
}
function setChildLoggerFactory (factory) {
throwIfAlreadyStarted('Cannot call "setChildLoggerFactory"!')
this[kChildLoggerFactory] = factory
return this
}
function printRoutes (opts = {}) {
opts.includeMeta = opts.includeHooks
? opts.includeMeta ? supportedHooks.concat(opts.includeMeta) : supportedHooks
: opts.includeMeta
return router.printRoutes(opts)
}
function wrapRouting (router, { rewriteUrl, logger }) {
let isAsync
return function preRouting (req, res) {
if (isAsync === undefined) isAsync = router.isAsyncConstraint()
if (rewriteUrl) {
req.originalUrl = req.url
const url = rewriteUrl.call(fastify, req)
if (typeof url === 'string') {
req.url = url
} else {
const err = new FST_ERR_ROUTE_REWRITE_NOT_STR(req.url, typeof url)
req.destroy(err)
}
}
router.routing(req, res, buildAsyncConstraintCallback(isAsync, req, res))
}
}
function setGenReqId (func) {
throwIfAlreadyStarted('Cannot call "setGenReqId"!')
this[kGenReqId] = reqIdGenFactory(this[kOptions].requestIdHeader, func)
return this
}
function addHttpMethod (method, { hasBody = false, overrideExisting = false } = {}) {
if (typeof method !== 'string' || http.METHODS.indexOf(method) === -1) {
throw new FST_ERR_ROUTE_METHOD_INVALID()
}
const alreadyExists = this[kSupportedHTTPMethods].bodyless.has(method) ||
this[kSupportedHTTPMethods].bodywith.has(method)
if (alreadyExists && !overrideExisting) {
throw new FST_ERR_ROUTE_METHOD_ALREADY_SUPPORTED(method)
}
if (hasBody === true) {
this[kSupportedHTTPMethods].bodywith.add(method)
this[kSupportedHTTPMethods].bodyless.delete(method)
} else {
this[kSupportedHTTPMethods].bodywith.delete(method)
this[kSupportedHTTPMethods].bodyless.add(method)
}
const _method = method.toLowerCase()
if (!this.hasDecorator(_method)) {
this.decorate(_method, function (url, options, handler) {
return router.prepareRoute.call(this, { method, url, options, handler })
})
}
return this
}
}
function processOptions (options, defaultRoute, onBadUrl, onMaxParamLength) {
if (options && typeof options !== 'object') {
throw new FST_ERR_OPTIONS_NOT_OBJ()
} else {
options = Object.assign({}, options)
}
if (
options.routerOptions?.querystringParser &&
typeof options.routerOptions.querystringParser !== 'function'
) {
throw new FST_ERR_QSP_NOT_FN(typeof options.routerOptions.querystringParser)
}
if (options.schemaController && options.schemaController.bucket && typeof options.schemaController.bucket !== 'function') {
throw new FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN(typeof options.schemaController.bucket)
}
validateBodyLimitOption(options.bodyLimit)
const requestIdHeader = typeof options.requestIdHeader === 'string' && options.requestIdHeader.length !== 0 ? options.requestIdHeader.toLowerCase() : (options.requestIdHeader === true && 'request-id')
const genReqId = reqIdGenFactory(requestIdHeader, options.genReqId)
options.bodyLimit = options.bodyLimit || defaultInitOptions.bodyLimit
const ajvOptions = Object.assign({
customOptions: {},
plugins: []
}, options.ajv)
if (!ajvOptions.customOptions || Object.prototype.toString.call(ajvOptions.customOptions) !== '[object Object]') {
throw new FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ(typeof ajvOptions.customOptions)
}
if (!ajvOptions.plugins || !Array.isArray(ajvOptions.plugins)) {
throw new FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR(typeof ajvOptions.plugins)
}
const { logger, hasLogger } = createLogger(options)
const logController = createLogController(options)
options.connectionTimeout = options.connectionTimeout || defaultInitOptions.connectionTimeout
options.keepAliveTimeout = options.keepAliveTimeout || defaultInitOptions.keepAliveTimeout
options.maxRequestsPerSocket = options.maxRequestsPerSocket || defaultInitOptions.maxRequestsPerSocket
options.requestTimeout = options.requestTimeout || defaultInitOptions.requestTimeout
options.logger = logger
options.requestIdHeader = requestIdHeader
options.ajv = ajvOptions
options.clientErrorHandler = options.clientErrorHandler || defaultClientErrorHandler
options.allowErrorHandlerOverride = options.allowErrorHandlerOverride ?? defaultInitOptions.allowErrorHandlerOverride
options.routerOptions = buildRouterOptions(options, {
buildPrettyMeta: defaultBuildPrettyMeta,
defaultRoute,
onBadUrl,
onMaxParamLength
})
const initialConfig = getSecuredInitialConfig(options)
options.exposeHeadRoutes = initialConfig.exposeHeadRoutes
options.http2SessionTimeout = initialConfig.http2SessionTimeout
return {
options,
genReqId,
logController,
hasLogger,
initialConfig
}
}
function defaultBuildPrettyMeta (route) {
const cleanKeys = {}
const allowedProps = ['errorHandler', 'logLevel', 'logSerializers']
allowedProps.concat(supportedHooks).forEach(k => {
cleanKeys[k] = route.store[k]
})
return Object.assign({}, cleanKeys)
}
function defaultClientErrorHandler (err, socket) {
if (err.code === 'ECONNRESET' || socket.destroyed) {
return
}
let body, errorCode, errorStatus, errorLabel
if (err.code === 'ERR_HTTP_REQUEST_TIMEOUT') {
errorCode = '408'
errorStatus = http.STATUS_CODES[errorCode]
body = `{"error":"${errorStatus}","message":"Client Timeout","statusCode":408}`
errorLabel = 'timeout'
} else if (err.code === 'HPE_HEADER_OVERFLOW') {
errorCode = '431'
errorStatus = http.STATUS_CODES[errorCode]
body = `{"error":"${errorStatus}","message":"Exceeded maximum allowed HTTP header size","statusCode":431}`
errorLabel = 'header_overflow'
} else {
errorCode = '400'
errorStatus = http.STATUS_CODES[errorCode]
body = `{"error":"${errorStatus}","message":"Client Error","statusCode":400}`
errorLabel = 'error'
}
this.log.trace({ err }, `client ${errorLabel}`)
if (socket.writable) {
socket.write(`HTTP/1.1 ${errorCode} ${errorStatus}\r\nContent-Length: ${body.length}\r\nContent-Type: application/json\r\n\r\n${body}`)
}
socket.destroy(err)
}
function validateSchemaErrorFormatter (schemaErrorFormatter) {
if (typeof schemaErrorFormatter !== 'function') {
throw new FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN(typeof schemaErrorFormatter)
} else if (schemaErrorFormatter.constructor.name === 'AsyncFunction') {
throw new FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN('AsyncFunction')
}
}
* These export configurations enable JS and TS developers
* to consume fastify in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const fastify = require('fastify')`
* - `const { fastify } = require('fastify')`
* - `import * as Fastify from 'fastify'`
* - `import { fastify, TSC_definition } from 'fastify'`
* - `import fastify from 'fastify'`
* - `import fastify, { TSC_definition } from 'fastify'`
*/
module.exports = fastify
module.exports.errorCodes = errorCodes
module.exports.LogController = LogController
module.exports.fastify = fastify
module.exports.default = fastify