Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a separate hook runner for onSend hooks #795

Merged
merged 2 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Hooks = require('./lib/hooks')
const Schemas = require('./lib/schemas')
const loggerUtils = require('./lib/logger')
const pluginUtils = require('./lib/pluginUtils')
const runHooks = require('./lib/hookRunner')
const runHooks = require('./lib/hookRunner').hookRunner

const DEFAULT_BODY_LIMIT = 1024 * 1024 // 1 MiB
const childrenKey = Symbol('fastify.children')
Expand Down
2 changes: 1 addition & 1 deletion lib/handleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fastJsonStringify = require('fast-json-stringify')
const urlUtil = require('url')
const validation = require('./validation')
const validateSchema = validation.validate
const runHooks = require('./hookRunner')
const runHooks = require('./hookRunner').hookRunner

const schemas = require('./schemas.json')
const inputSchemaError = fastJsonStringify(schemas.inputSchemaError)
Expand Down
46 changes: 36 additions & 10 deletions lib/hookRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,62 @@
function hookRunner (functions, runner, state, cb) {
var i = 0

function next (err, value) {
if (err) {
function next (err) {
if (err || i === functions.length) {
cb(err, state)
return
}

if (value !== undefined) {
state = value
const result = runner(functions[i++], state, next)
if (result && typeof result.then === 'function') {
result.then(handleResolve, handleReject)
}
}

function handleResolve () {
next()
}

function handleReject (err) {
cb(err, state)
}

next()
}

function onSendHookRunner (functions, reply, payload, cb) {
var i = 0

function next (err, newPayload) {
if (err) {
cb(err, reply, payload)
return
}

if (newPayload !== undefined) {
payload = newPayload
}

if (i === functions.length) {
cb(null, state)
cb(null, reply, payload)
return
}

const result = runner(functions[i++], state, next)
const result = functions[i++](reply.request, reply, payload, next)
if (result && typeof result.then === 'function') {
result.then(handleResolve, handleReject)
}
}

function handleResolve (value) {
next(null, value)
function handleResolve (newPayload) {
next(null, newPayload)
}

function handleReject (err) {
cb(err, state)
cb(err, reply, payload)
}

next()
}

module.exports = hookRunner
module.exports = { hookRunner, onSendHookRunner }
16 changes: 6 additions & 10 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const serialize = validation.serialize
const statusCodes = require('http').STATUS_CODES
const flatstr = require('flatstr')
const FJS = require('fast-json-stringify')
const runHooks = require('./hookRunner')
const runHooks = require('./hookRunner').onSendHookRunner

const serializeError = FJS({
type: 'object',
Expand Down Expand Up @@ -122,24 +122,20 @@ function onSendHook (reply, payload) {
if (reply.context.onSend !== null) {
runHooks(
reply.context.onSend,
hookIterator.bind(reply),
reply,
payload,
wrapOnSendEnd.bind(reply)
wrapOnSendEnd
)
} else {
onSendEnd(reply, payload)
}
}

function hookIterator (fn, payload, next) {
return fn(this.request, this, payload, next)
}

function wrapOnSendEnd (err, payload) {
function wrapOnSendEnd (err, reply, payload) {
if (err) {
handleError(this, err)
handleError(reply, err)
} else {
onSendEnd(this, payload)
onSendEnd(reply, payload)
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,29 @@ test('onResponse hooks should run in the order in which they are defined', t =>
})
})

test('onRequest, preHandler, and onResponse hooks that resolve to a value do not cause an error', t => {
t.plan(3)
const fastify = Fastify()

fastify
.addHook('onRequest', () => Promise.resolve(1))
.addHook('onRequest', () => Promise.resolve(true))
.addHook('preHandler', () => Promise.resolve(null))
.addHook('preHandler', () => Promise.resolve('a'))
.addHook('onResponse', () => Promise.resolve({}))
.addHook('onResponse', () => Promise.resolve([]))

fastify.get('/', (request, reply) => {
reply.send('hello')
})

fastify.inject('/', (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 200)
t.strictEqual(res.payload, 'hello')
})
})

if (Number(process.versions.node[0]) >= 8) {
require('./hooks-async')(t)
} else {
Expand Down