forked from hapijs/hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext.js
executable file
·71 lines (46 loc) · 1.44 KB
/
ext.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
'use strict';
// Load modules
const Topo = require('topo');
// Declare internals
const internals = {};
exports = module.exports = internals.Ext = function (type, server) {
this._topo = new Topo();
this._server = server;
this._routes = [];
this.type = type;
this.nodes = null;
};
internals.Ext.prototype.add = function (event) {
const methods = [].concat(event.method);
const options = event.options;
for (let i = 0; i < methods.length; ++i) {
const settings = {
before: options.before,
after: options.after,
group: event.plugin.realm.plugin,
sort: this._server._extensionsSeq++
};
const node = {
func: methods[i], // Connection: function (request, next), Server: function (server, next)
bind: options.bind,
plugin: event.plugin
};
this._topo.add(node, settings);
}
this.nodes = this._topo.nodes;
// Notify routes
for (let i = 0; i < this._routes.length; ++i) {
this._routes[i].rebuild(event);
}
};
internals.Ext.prototype.merge = function (others) {
const merge = [];
for (let i = 0; i < others.length; ++i) {
merge.push(others[i]._topo);
}
this._topo.merge(merge);
this.nodes = (this._topo.nodes.length ? this._topo.nodes : null);
};
internals.Ext.prototype.subscribe = function (route) {
this._routes.push(route);
};