Skip to content

Commit

Permalink
chore(prettier): add prettier config and run it (closes fastify#2837)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed Feb 5, 2021
1 parent fc33ae9 commit c537f15
Show file tree
Hide file tree
Showing 28 changed files with 1,091 additions and 1,071 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
@@ -1,3 +1,4 @@
{
"extends": "standard"
"extends": ["standard", "prettier"],
"plugins": ["prettier"]
}
2 changes: 1 addition & 1 deletion .prettierignore
@@ -1 +1 @@
*
package.json
7 changes: 7 additions & 0 deletions .prettierrc
@@ -0,0 +1,7 @@
{
"semi": false,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 120,
"arrowParens": "avoid"
}
127 changes: 64 additions & 63 deletions fastify.js
Expand Up @@ -47,34 +47,24 @@ const getSecuredInitialConfig = require('./lib/initialConfigValidation')
const override = require('./lib/pluginOverride')
const { defaultInitOptions } = getSecuredInitialConfig

const {
FST_ERR_BAD_URL,
FST_ERR_MISSING_MIDDLEWARE
} = require('./lib/errors')
const { FST_ERR_BAD_URL, FST_ERR_MISSING_MIDDLEWARE } = require('./lib/errors')

const onBadUrlContext = {
config: {
},
config: {},
onSend: [],
onError: []
}

function defaultErrorHandler (error, request, reply) {
function defaultErrorHandler(error, request, reply) {
if (reply.statusCode < 500) {
reply.log.info(
{ res: reply, err: error },
error && error.message
)
reply.log.info({ res: reply, err: error }, error && error.message)
} else {
reply.log.error(
{ req: request, res: reply, err: error },
error && error.message
)
reply.log.error({ req: request, res: reply, err: error }, error && error.message)
}
reply.send(error)
}

function fastify (options) {
function fastify(options) {
// Options validations
options = options || {}

Expand All @@ -96,10 +86,13 @@ function fastify (options) {
const disableRequestLogging = options.disableRequestLogging || false
const exposeHeadRoutes = options.exposeHeadRoutes != null ? options.exposeHeadRoutes : false

const ajvOptions = Object.assign({
customOptions: {},
plugins: []
}, options.ajv)
const ajvOptions = Object.assign(
{
customOptions: {},
plugins: []
},
options.ajv
)
const frameworkErrors = options.frameworkErrors

// Ajv options
Expand Down Expand Up @@ -180,8 +173,8 @@ function fastify (options) {
[kReplySerializerDefault]: null,
[kContentTypeParser]: new ContentTypeParser(
bodyLimit,
(options.onProtoPoisoning || defaultInitOptions.onProtoPoisoning),
(options.onConstructorPoisoning || defaultInitOptions.onConstructorPoisoning)
options.onProtoPoisoning || defaultInitOptions.onProtoPoisoning,
options.onConstructorPoisoning || defaultInitOptions.onConstructorPoisoning
),
[kReply]: Reply.buildReply(Reply),
[kRequest]: Request.buildRequest(Request, options.trustProxy),
Expand All @@ -194,32 +187,32 @@ function fastify (options) {
getDefaultRoute: router.getDefaultRoute.bind(router),
setDefaultRoute: router.setDefaultRoute.bind(router),
// routes shorthand methods
delete: function _delete (url, opts, handler) {
delete: function _delete(url, opts, handler) {
return router.prepareRoute.call(this, 'DELETE', url, opts, handler)
},
get: function _get (url, opts, handler) {
get: function _get(url, opts, handler) {
return router.prepareRoute.call(this, 'GET', url, opts, handler)
},
head: function _head (url, opts, handler) {
head: function _head(url, opts, handler) {
return router.prepareRoute.call(this, 'HEAD', url, opts, handler)
},
patch: function _patch (url, opts, handler) {
patch: function _patch(url, opts, handler) {
return router.prepareRoute.call(this, 'PATCH', url, opts, handler)
},
post: function _post (url, opts, handler) {
post: function _post(url, opts, handler) {
return router.prepareRoute.call(this, 'POST', url, opts, handler)
},
put: function _put (url, opts, handler) {
put: function _put(url, opts, handler) {
return router.prepareRoute.call(this, 'PUT', url, opts, handler)
},
options: function _options (url, opts, handler) {
options: function _options(url, opts, handler) {
return router.prepareRoute.call(this, 'OPTIONS', url, opts, handler)
},
all: function _all (url, opts, handler) {
all: function _all(url, opts, handler) {
return router.prepareRoute.call(this, supportedMethods, url, opts, handler)
},
// extended route
route: function _route (opts) {
route: function _route(opts) {
// we need the fastify object that we are producing so we apply a lazy loading of the function,
// otherwise we should bind it after the declaration
return router.route.call(this, opts)
Expand Down Expand Up @@ -270,32 +263,38 @@ function fastify (options) {

Object.defineProperties(fastify, {
pluginName: {
get () {
get() {
if (this[kPluginNameChain].length > 1) {
return this[kPluginNameChain].join(' -> ')
}
return this[kPluginNameChain][0]
}
},
prefix: {
get () { return this[kRoutePrefix] }
get() {
return this[kRoutePrefix]
}
},
validatorCompiler: {
get () { return this[kValidatorCompiler] }
get() {
return this[kValidatorCompiler]
}
},
serializerCompiler: {
get () { return this[kSerializerCompiler] }
get() {
return this[kSerializerCompiler]
}
},
version: {
get () {
get() {
if (versionLoaded === false) {
version = loadVersion()
}
return version
}
},
errorHandler: {
get () {
get() {
return this[kErrorHandler]
}
}
Expand Down Expand Up @@ -362,14 +361,14 @@ function fastify (options) {

return fastify

function throwIfAlreadyStarted (msg) {
function throwIfAlreadyStarted(msg) {
if (fastify[kState].started) throw new Error(msg)
}

// HTTP injection handling
// If the server is not ready yet, this
// utility will automatically force it.
function inject (opts, cb) {
function inject(opts, cb) {
// lightMyRequest is dynamically laoded as it seems very expensive
// because of Ajv
if (lightMyRequest === undefined) {
Expand Down Expand Up @@ -408,7 +407,7 @@ function fastify (options) {
}
}

function ready (cb) {
function ready(cb) {
let resolveReady
let rejectReady

Expand All @@ -422,7 +421,7 @@ function fastify (options) {
})
}

function runHooks () {
function runHooks() {
// start loading
fastify[kAvvioBoot]((err, done) => {
if (err || fastify[kState].started) {
Expand All @@ -434,7 +433,7 @@ function fastify (options) {
})
}

function manageErr (err) {
function manageErr(err) {
if (cb) {
if (err) {
cb(err)
Expand All @@ -450,25 +449,25 @@ function fastify (options) {
}
}

function use () {
function use() {
throw new FST_ERR_MISSING_MIDDLEWARE()
}

// wrapper that we expose to the user for hooks handling
function addHook (name, fn) {
function addHook(name, fn) {
throwIfAlreadyStarted('Cannot call "addHook" when fastify instance is already started!')

if (name === 'onSend' || name === 'preSerialization' || name === 'onError') {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 4) {
throw new Error('Async function has too many arguments. Async hooks should not use the \'done\' argument.')
throw new Error("Async function has too many arguments. Async hooks should not use the 'done' argument.")
}
} else if (name === 'onReady') {
if (fn.constructor.name === 'AsyncFunction' && fn.length !== 0) {
throw new Error('Async function has too many arguments. Async hooks should not use the \'done\' argument.')
throw new Error("Async function has too many arguments. Async hooks should not use the 'done' argument.")
}
} else if (name !== 'preParsing') {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
throw new Error('Async function has too many arguments. Async hooks should not use the \'done\' argument.')
throw new Error("Async function has too many arguments. Async hooks should not use the 'done' argument.")
}
}

Expand All @@ -486,21 +485,21 @@ function fastify (options) {
}
return this

function _addHook (name, fn) {
function _addHook(name, fn) {
this[kHooks].add(name, fn)
this[kChildren].forEach(child => _addHook.call(child, name, fn))
}
}

// wrapper that we expose to the user for schemas handling
function addSchema (schema) {
function addSchema(schema) {
throwIfAlreadyStarted('Cannot call "addSchema" when fastify instance is already started!')
this[kSchemas].add(schema)
this[kChildren].forEach(child => child.addSchema(schema))
return this
}

function defaultClientErrorHandler (err, socket) {
function defaultClientErrorHandler(err, socket) {
// In case of a connection reset, the socket has been destroyed and there is nothing that needs to be done.
// https://github.com/fastify/fastify/issues/2036
// https://github.com/nodejs/node/issues/33302
Expand All @@ -521,20 +520,22 @@ function fastify (options) {

// If the socket is not writable, there is no reason to try to send data.
if (socket.writable) {
socket.end(`HTTP/1.1 400 Bad Request\r\nContent-Length: ${body.length}\r\nContent-Type: application/json\r\n\r\n${body}`)
socket.end(
`HTTP/1.1 400 Bad Request\r\nContent-Length: ${body.length}\r\nContent-Type: application/json\r\n\r\n${body}`
)
}
}

// If the router does not match any route, every request will land here
// req and res are Node.js core objects
function defaultRoute (req, res) {
function defaultRoute(req, res) {
if (req.headers['accept-version'] !== undefined) {
req.headers['accept-version'] = undefined
}
fourOhFour.router.lookup(req, res)
}

function onBadUrl (path, req, res) {
function onBadUrl(path, req, res) {
if (frameworkErrors) {
const id = genReqId(req)
const childLogger = logger.child({ reqId: id })
Expand All @@ -553,61 +554,61 @@ function fastify (options) {
res.end(body)
}

function setNotFoundHandler (opts, handler) {
function setNotFoundHandler(opts, handler) {
throwIfAlreadyStarted('Cannot call "setNotFoundHandler" when fastify instance is already started!')

fourOhFour.setNotFoundHandler.call(this, opts, handler, avvio, router.routeHandler)
return this
}

function setValidatorCompiler (validatorCompiler) {
function setValidatorCompiler(validatorCompiler) {
throwIfAlreadyStarted('Cannot call "setValidatorCompiler" when fastify instance is already started!')
this[kValidatorCompiler] = validatorCompiler
return this
}

function setSchemaErrorFormatter (errorFormatter) {
function setSchemaErrorFormatter(errorFormatter) {
throwIfAlreadyStarted('Cannot call "setSchemaErrorFormatter" when fastify instance is already started!')
validateSchemaErrorFormatter(errorFormatter)
this[kSchemaErrorFormatter] = errorFormatter.bind(this)
return this
}

function setSerializerCompiler (serializerCompiler) {
function setSerializerCompiler(serializerCompiler) {
throwIfAlreadyStarted('Cannot call "setSerializerCompiler" when fastify instance is already started!')
this[kSerializerCompiler] = serializerCompiler
return this
}

function setReplySerializer (replySerializer) {
function setReplySerializer(replySerializer) {
throwIfAlreadyStarted('Cannot call "setReplySerializer" when fastify instance is already started!')

this[kReplySerializerDefault] = replySerializer
return this
}

// wrapper that we expose to the user for configure the custom error handler
function setErrorHandler (func) {
function setErrorHandler(func) {
throwIfAlreadyStarted('Cannot call "setErrorHandler" when fastify instance is already started!')

this[kErrorHandler] = func.bind(this)
return this
}
}

function validateSchemaErrorFormatter (schemaErrorFormatter) {
function validateSchemaErrorFormatter(schemaErrorFormatter) {
if (typeof schemaErrorFormatter !== 'function') {
throw new Error(`schemaErrorFormatter option should be a function, instead got ${typeof schemaErrorFormatter}`)
} else if (schemaErrorFormatter.constructor.name === 'AsyncFunction') {
throw new Error('schemaErrorFormatter option should not be an async function')
}
}

function wrapRouting (httpHandler, { rewriteUrl, logger }) {
function wrapRouting(httpHandler, { rewriteUrl, logger }) {
if (!rewriteUrl) {
return httpHandler
}
return function preRouting (req, res) {
return function preRouting(req, res) {
const originalUrl = req.url
const url = rewriteUrl(req)
if (originalUrl !== url) {
Expand All @@ -622,7 +623,7 @@ function wrapRouting (httpHandler, { rewriteUrl, logger }) {
}
}

function loadVersion () {
function loadVersion() {
versionLoaded = true
const fs = require('fs')
const path = require('path')
Expand Down

0 comments on commit c537f15

Please sign in to comment.