forked from fastify/avvio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
283 lines (231 loc) · 6.4 KB
/
plugin.js
File metadata and controls
283 lines (231 loc) · 6.4 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'use strict'
const fastq = require('fastq')
const EE = require('events').EventEmitter
const inherits = require('util').inherits
const debug = require('debug')('avvio')
const { AVV_ERR_READY_TIMEOUT } = require('./lib/errors')
// this symbol is assigned by fastify-plugin
const kPluginMeta = Symbol.for('plugin-meta')
function getName (func, optsOrFunc) {
// use explicit function metadata if set
if (func[kPluginMeta] && func[kPluginMeta].name) {
return func[kPluginMeta].name
}
if (typeof optsOrFunc !== 'undefined' && typeof optsOrFunc !== 'function' && optsOrFunc.name) {
return optsOrFunc.name
}
// use the function name if it exists
if (func.name) {
return func.name
}
// takes the first two lines of the function if nothing else works
return func.toString().split('\n').slice(0, 2).map(s => s.trim()).join(' -- ')
}
function promise () {
const obj = {}
obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve
obj.reject = reject
})
return obj
}
function Plugin (parent, func, optsOrFunc, isAfter, timeout) {
this.started = false
this.func = func
this.opts = optsOrFunc
this.onFinish = null
this.parent = parent
this.timeout = timeout === undefined ? parent._timeout : timeout
this.name = getName(func, optsOrFunc)
this.isAfter = isAfter
this.q = fastq(parent, loadPluginNextTick, 1)
this.q.pause()
this._error = null
this.loaded = false
this._promise = null
// always start the queue in the next tick
// because we try to attach subsequent call to use()
// to the right plugin. we need to defer them,
// or they will end up at the top of _current
}
inherits(Plugin, EE)
Plugin.prototype.exec = function (server, cb) {
const func = this.func
let completed = false
const name = this.name
if (this.parent._error && !this.isAfter) {
debug('skipping loading of plugin as parent errored and it is not an after', name)
process.nextTick(cb)
return
}
if (!this.isAfter) {
// Skip override for after
try {
this.server = this.parent.override(server, func, this.opts)
} catch (err) {
debug('override errored', name)
return cb(err)
}
} else {
this.server = server
}
this.opts = typeof this.opts === 'function' ? this.opts(this.server) : this.opts
debug('exec', name)
let timer
const done = (err) => {
if (completed) {
debug('loading complete', name)
return
}
this._error = err
if (err) {
debug('exec errored', name)
} else {
debug('exec completed', name)
}
completed = true
if (timer) {
clearTimeout(timer)
}
cb(err)
}
if (this.timeout > 0) {
debug('setting up timeout', name, this.timeout)
timer = setTimeout(function () {
debug('timed out', name)
timer = null
const err = new AVV_ERR_READY_TIMEOUT(name)
err.fn = func
done(err)
}, this.timeout)
}
this.started = true
this.emit('start', this.server ? this.server.name : null, this.name, Date.now())
const promise = func(this.server, this.opts, done)
if (promise && typeof promise.then === 'function') {
debug('exec: resolving promise', name)
promise.then(
() => process.nextTick(done),
(e) => process.nextTick(done, e))
}
}
Plugin.prototype.loadedSoFar = function () {
if (this.loaded) {
return Promise.resolve()
}
const setup = () => {
this.server.after((err, cb) => {
this._error = err
this.q.pause()
if (err) {
debug('rejecting promise', this.name, err)
this._promise.reject(err)
} else {
debug('resolving promise', this.name)
this._promise.resolve()
}
this._promise = null
process.nextTick(cb, err)
})
this.q.resume()
}
let res
if (!this._promise) {
this._promise = promise()
res = this._promise.promise
if (!this.server) {
this.on('start', setup)
} else {
setup()
}
} else {
res = Promise.resolve()
}
return res
}
Plugin.prototype.enqueue = function (obj, cb) {
debug('enqueue', this.name, obj.name)
this.emit('enqueue', this.server ? this.server.name : null, this.name, Date.now())
this.q.push(obj, cb)
}
Plugin.prototype.finish = function (err, cb) {
debug('finish', this.name, err)
const done = () => {
if (this.loaded) {
return
}
debug('loaded', this.name)
this.emit('loaded', this.server ? this.server.name : null, this.name, Date.now())
this.loaded = true
cb(err)
}
if (err) {
if (this._promise) {
this._promise.reject(err)
this._promise = null
}
done()
return
}
const check = () => {
debug('check', this.name, this.q.length(), this.q.running(), this._promise)
if (this.q.length() === 0 && this.q.running() === 0) {
if (this._promise) {
const wrap = () => {
debug('wrap')
queueMicrotask(check)
}
this._promise.resolve()
this._promise.promise.then(wrap, wrap)
this._promise = null
} else {
done()
}
} else {
debug('delayed', this.name)
// finish when the queue of nested plugins to load is empty
this.q.drain = () => {
debug('drain', this.name)
this.q.drain = noop
// we defer the check, as a safety net for things
// that might be scheduled in the loading callback
queueMicrotask(check)
}
}
}
queueMicrotask(check)
// we start loading the dependents plugins only once
// the current level is finished
this.q.resume()
}
// delays plugin loading until the next tick to ensure any bound `_after` callbacks have a chance
// to run prior to executing the next plugin
function loadPluginNextTick (toLoad, cb) {
const parent = this
process.nextTick(loadPlugin.bind(parent), toLoad, cb)
}
// loads a plugin
function loadPlugin (toLoad, cb) {
if (typeof toLoad.func.then === 'function') {
toLoad.func.then((fn) => {
if (typeof fn.default === 'function') {
fn = fn.default
}
toLoad.func = fn
loadPlugin.call(this, toLoad, cb)
}, cb)
return
}
const last = this._current[0]
// place the plugin at the top of _current
this._current.unshift(toLoad)
toLoad.exec((last && last.server) || this._server, (err) => {
toLoad.finish(err, (err) => {
this._current.shift()
cb(err)
})
})
}
function noop () {}
module.exports = Plugin
module.exports.loadPlugin = loadPlugin