-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ext.js
executable file
·96 lines (68 loc) · 2.25 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
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
'use strict';
const Hoek = require('@hapi/hoek');
const Topo = require('@hapi/topo');
const internals = {};
exports = module.exports = internals.Ext = class {
type = null;
nodes = null;
#core = null;
#routes = [];
#topo = new Topo.Sorter();
constructor(type, core) {
this.#core = core;
this.type = type;
}
add(event) {
const methods = [].concat(event.method);
for (const method of methods) {
const settings = {
before: event.options.before,
after: event.options.after,
group: event.realm.plugin,
sort: this.#core.extensionsSeq++
};
const node = {
func: method, // Request: function (request, h), Server: function (server)
bind: event.options.bind,
server: event.server, // Server event
realm: event.realm,
timeout: event.options.timeout
};
this.#topo.add(node, settings);
}
this.nodes = this.#topo.nodes;
// Notify routes
for (const route of this.#routes) {
route.rebuild(event);
}
}
merge(others) {
const merge = [];
for (const other of others) {
merge.push(other.#topo);
}
this.#topo.merge(merge);
this.nodes = this.#topo.nodes.length ? this.#topo.nodes : null;
}
subscribe(route) {
this.#routes.push(route);
}
static combine(route, type) {
const ext = new internals.Ext(type, route._core);
const events = route.settings.ext[type];
if (events) {
for (let event of events) {
event = Object.assign({}, event); // Shallow cloned
Hoek.assert(!event.options.sandbox, 'Cannot specify sandbox option for route extension');
event.realm = route.realm;
ext.add(event);
}
}
const server = route._core.extensions.route[type];
const realm = route.realm._extensions[type];
ext.merge([server, realm]);
server.subscribe(route);
realm.subscribe(route);
return ext;
}
};