/
hooks.js
258 lines (221 loc) · 5.4 KB
/
hooks.js
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
'use strict'
const applicationHooks = [
'onRoute',
'onRegister',
'onReady',
'onClose'
]
const lifecycleHooks = [
'onTimeout',
'onRequest',
'preParsing',
'preValidation',
'preSerialization',
'preHandler',
'onSend',
'onResponse',
'onError'
]
const supportedHooks = lifecycleHooks.concat(applicationHooks)
const {
FST_ERR_HOOK_INVALID_TYPE,
FST_ERR_HOOK_INVALID_HANDLER,
FST_ERR_SEND_UNDEFINED_ERR,
FST_ERR_HOOK_TIMEOUT,
AVVIO_ERRORS_MAP,
appendStackTrace
} = require('./errors')
const {
kChildren,
kHooks
} = require('./symbols')
function Hooks () {
this.onRequest = []
this.preParsing = []
this.preValidation = []
this.preSerialization = []
this.preHandler = []
this.onResponse = []
this.onSend = []
this.onError = []
this.onRoute = []
this.onRegister = []
this.onReady = []
this.onTimeout = []
}
Hooks.prototype.validate = function (hook, fn) {
if (typeof hook !== 'string') throw new FST_ERR_HOOK_INVALID_TYPE()
if (supportedHooks.indexOf(hook) === -1) {
throw new Error(`${hook} hook not supported!`)
}
if (typeof fn !== 'function') throw new FST_ERR_HOOK_INVALID_HANDLER(hook, fn === null ? Object.prototype.toString.call(fn) : typeof fn)
}
Hooks.prototype.add = function (hook, fn) {
this.validate(hook, fn)
this[hook].push(fn)
}
function buildHooks (h) {
const hooks = new Hooks()
hooks.onRequest = h.onRequest.slice()
hooks.preParsing = h.preParsing.slice()
hooks.preValidation = h.preValidation.slice()
hooks.preSerialization = h.preSerialization.slice()
hooks.preHandler = h.preHandler.slice()
hooks.onSend = h.onSend.slice()
hooks.onResponse = h.onResponse.slice()
hooks.onError = h.onError.slice()
hooks.onRoute = h.onRoute.slice()
hooks.onRegister = h.onRegister.slice()
hooks.onTimeout = h.onTimeout.slice()
hooks.onReady = []
return hooks
}
function hookRunnerApplication (hookName, boot, server, cb) {
const hooks = server[kHooks][hookName]
let i = 0
let c = 0
next()
function exit (err) {
if (err) {
if (err.code === 'AVV_ERR_READY_TIMEOUT') {
err = appendStackTrace(err, new FST_ERR_HOOK_TIMEOUT(hookName))
} else {
err = AVVIO_ERRORS_MAP[err.code] != null
? appendStackTrace(err, new AVVIO_ERRORS_MAP[err.code](err.message))
: err
}
cb(err)
return
}
cb()
}
function next (err) {
if (err) {
exit(err)
return
}
if (i === hooks.length && c === server[kChildren].length) {
if (i === 0 && c === 0) { // speed up start
exit()
} else {
// This is the last function executed for every fastify instance
boot(function manageTimeout (err, done) {
// this callback is needed by fastify to provide an hook interface without the error
// as first parameter and managing it on behalf the user
exit(err)
// this callback is needed by avvio to continue the loading of the next `register` plugins
done(err)
})
}
return
}
if (i === hooks.length && c < server[kChildren].length) {
const child = server[kChildren][c++]
hookRunnerApplication(hookName, boot, child, next)
return
}
boot(wrap(hooks[i++], server))
next()
}
function wrap (fn, server) {
return function (err, done) {
if (err) {
done(err)
return
}
if (fn.length === 1) {
try {
fn.call(server, done)
} catch (error) {
done(error)
}
return
}
const ret = fn.call(server)
if (ret && typeof ret.then === 'function') {
ret.then(done, done)
return
}
done(err) // auto done
}
}
}
function hookRunner (functions, runner, request, reply, cb) {
let i = 0
function next (err) {
if (err || i === functions.length) {
cb(err, request, reply)
return
}
let result
try {
result = runner(functions[i++], request, reply, next)
} catch (error) {
next(error)
return
}
if (result && typeof result.then === 'function') {
result.then(handleResolve, handleReject)
}
}
function handleResolve () {
next()
}
function handleReject (err) {
if (!err) {
err = new FST_ERR_SEND_UNDEFINED_ERR()
}
cb(err, request, reply)
}
next()
}
function onSendHookRunner (functions, request, reply, payload, cb) {
let i = 0
function next (err, newPayload) {
if (err) {
cb(err, request, reply, payload)
return
}
if (newPayload !== undefined) {
payload = newPayload
}
if (i === functions.length) {
cb(null, request, reply, payload)
return
}
let result
try {
result = functions[i++](request, reply, payload, next)
} catch (error) {
next(error)
return
}
if (result && typeof result.then === 'function') {
result.then(handleResolve, handleReject)
}
}
function handleResolve (newPayload) {
next(null, newPayload)
}
function handleReject (err) {
if (!err) {
err = new FST_ERR_SEND_UNDEFINED_ERR()
}
cb(err, request, reply, payload)
}
next()
}
function hookIterator (fn, request, reply, next) {
if (reply.sent === true) return undefined
return fn(request, reply, next)
}
module.exports = {
Hooks,
buildHooks,
hookRunner,
onSendHookRunner,
hookIterator,
hookRunnerApplication,
lifecycleHooks,
supportedHooks
}