-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
246 lines (211 loc) · 8.11 KB
/
index.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
// import events from "events";
const events = require("events");
const EventEmitter = events.EventEmitter;
function objHas(obj, name) { return Object.prototype.hasOwnProperty.call(obj, name); }
// Check a plugin config list for bad dependencies and throw on error
function checkConfig(config) {
// Check for the required fields in each plugin.
config.forEach(function (plugin) {
if (plugin.checked) { return; }
if (!objHas(plugin, "setup") && typeof plugin == "function")
plugin.setup = plugin;
if (!objHas(plugin, "setup") && typeof plugin.default == "function")
plugin.setup = plugin.default;
if (!objHas(plugin, "setup")) {
throw new Error("Plugin is missing the setup function " + JSON.stringify(plugin));
}
if (!objHas(plugin, "provides")) {
if (objHas(plugin.setup, "provides")) {
plugin.provides = plugin.setup.provides;
} else
throw new Error("Plugin is missing the provides array " + JSON.stringify(plugin));
}
if (!objHas(plugin, "consumes")) {
if (objHas(plugin.setup, "consumes")) {
plugin.consumes = plugin.setup.consumes;
} else
throw new Error("Plugin is missing the consumes array " + JSON.stringify(plugin));
}
});
return checkCycles(config);
}
function checkCycles(config) {
var plugins = [];
config.forEach(function (pluginConfig, index) {
plugins.push({
packagePath: pluginConfig.packagePath,
provides: pluginConfig.provides.concat(),
consumes: pluginConfig.consumes.concat(),
i: index
});
});
var resolved = {
app: true
};
var changed = true;
var sorted = [];
while (plugins.length && changed) {
changed = false;
plugins.concat().forEach(function (plugin) {
var consumes = plugin.consumes.concat();
var resolvedAll = true;
for (var i = 0; i < consumes.length; i++) {
var service = consumes[i];
if (!resolved[service]) {
resolvedAll = false;
} else {
plugin.consumes.splice(plugin.consumes.indexOf(service), 1);
}
}
if (!resolvedAll)
return;
plugins.splice(plugins.indexOf(plugin), 1);
plugin.provides.forEach(function (service) {
resolved[service] = true;
});
sorted.push(config[plugin.i]);
changed = true;
});
}
if (plugins.length) {
var unresolved = {};
plugins.forEach(function (plugin) {
delete plugin.config;
plugin.consumes.forEach(function (name) {
if (unresolved[name] == false)
return;
if (!unresolved[name])
unresolved[name] = [];
unresolved[name].push(plugin.packagePath);
});
plugin.provides.forEach(function (name) {
unresolved[name] = false;
});
});
Object.keys(unresolved).forEach(function (name) {
if (unresolved[name] == false)
delete unresolved[name];
});
console.error("Could not resolve dependencies of these plugins:", plugins);
console.error("Resolved services:", Object.keys(resolved));
console.error("Missing services:", unresolved);
throw new Error("Could not resolve dependencies");
}
return sorted;
}
class Rectify extends EventEmitter {
constructor(config, appArg) {
super();//setup emitter
var app = this;
app.config = config;
var services = app.services = {
app: {
EventEmitter: EventEmitter,
isNode: Rectify.isNode,
isFork: Rectify.isFork,
isNWJS: Rectify.isNWJS,
isWorker: Rectify.isWorker,
window: typeof window == "undefined" ? global : window,
on: function (name, callback) {
if (typeof (callback) == "function") callback = callback.bind(app);
app.on.apply(app, [name, callback]);
},
once: function (name, callback) {
if (typeof (callback) == "function") callback = callback.bind(app);
app.once.apply(app, [name, callback]);
},
emit: function () {
app.emit.apply(app, arguments);
}
}
};
if (appArg && typeof appArg == "object")
for (var i in appArg) {
services.app[i] = appArg[i];
}
else
services.app.arg = appArg;
// Check the config
var sortedPlugins = checkConfig(config);
var destructors = [];
app.start = async function (callback) {
if (callback) app.on("ready", callback);
var plugin = sortedPlugins.shift();
if (!plugin)
return app.emit("ready", app);
var imports = {};
if (plugin.consumes) {
plugin.consumes.forEach(function (name) {
imports[name] = services[name];
});
}
var $config = {};
plugin.provides.forEach(function (name) {
if (plugin.config && plugin.config[name]) {
$config[name] = plugin.config[name];
} else $config[name] = {};
var $c = config.config && config.config[name] ? config.config[name] : {};
for (var i in $c) {
$config[name][i] = $c[i];
}
});
try {
await plugin.setup(imports, register, $config);
return app;
} catch (e) {
return app.emit("error", e);
}
async function register(err, provided) {
if (err) { return app.emit("error", err); }
plugin.provides.forEach(function (name) {
if (!objHas(provided, name)) {
var err = new Error("Plugin failed to provide " + name + " service. " + JSON.stringify(plugin));
return app.emit("error", err);
}
services[name] = provided[name];
// if (typeof provided[name] != "function")
// provided[name].name = name;
app.emit("service", name, services[name]);
});
if (provided && objHas(provided, "onDestroy"))
destructors.push(provided.onDestroy);
app.emit("plugin", plugin);
await app.start();
}
};
// Give createApp some time to subscribe to our "ready" event
// (typeof process === "object" ? process.nextTick : setTimeout)(app.start);
}
}
Rectify.isNode = (typeof process != "undefined" && !process.__nwjs ? 1 : 0);
Rectify.isFork = (typeof process != "undefined" && process.send ? 1 : 0);
Rectify.isNWJS = (typeof process != "undefined" && process.__nwjs ? 1 : 0);
Rectify.isWorker = (typeof WorkerGlobalScope != "undefined" && globalThis instanceof WorkerGlobalScope) ? 1 : 0;
Rectify.build = function (config, callback) {
var app;
try {
app = new Rectify(config, callback && typeof callback == "object" ? callback : null);
} catch (err) {
if (!callback || !(typeof callback == "function")) throw err;
return callback(err, app);
}
if (callback) {
app.on("error", done);
app.on("ready", onReady);
}
return app;
function onReady() {
done();
}
function done(err) {
if (err) {
console.error(err);
app.emit('destroy');
}
app.removeListener("error", done);
app.removeListener("ready", onReady);
if (callback && (typeof callback == "function"))
callback(err, app);
}
};
module.exports = Rectify;