-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathBroadcastProxyRegistry.js
More file actions
55 lines (44 loc) · 1.83 KB
/
Copy pathBroadcastProxyRegistry.js
File metadata and controls
55 lines (44 loc) · 1.83 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
'use strict';
module.exports = class BroadcastProxyRegistry extends Map {
load(requireFn, rootPath, config = {}) {
for (const [name, settings] of Object.entries(config)) {
if (!settings.hasOwnProperty('require')) {
throw new Error(`BroadcastProxyRegistry [${name}] does not specify a 'require'`);
}
if (typeof settings.require !== 'string') {
throw new TypeError(`BroadcastProxyRegistry [${name}] has an invalid 'require'`);
}
const sourceConfig = settings.config || {};
let proxy;
// relative path to require
if (settings.require[0] === '.') {
proxy = require(rootPath + '/' + settings.require);
} else if (!settings.require.includes('.')) {
proxy = require(settings.require);
} else {
const [moduleName, exportName] = settings.require.split('.');
proxy = requireFn(moduleName)[exportName];
}
const instance = new proxy();
if (typeof instance.configure !== 'function') {
throw new Error(`BroadcastProxyRegistry ${name} requires a 'proxy' method`);
}
this.set(name, instance);
}
}
attach(stream) {
const identifier = stream.identifier;
if (!stream.identifier) {
throw new Error("TransportStreams must define a 'identifier' getter.");
}
const proxyInstance = this.get(identifier);
if (!proxyInstance) {
throw new Error(`No BroadcastProxies are defined for identifier: ${identifier}`);
}
Object.assign(stream, {
broadcastProxy(target, message, options) {
proxyInstance.proxy(target, message, options);
}
});
}
};