forked from coralproject/talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.js
358 lines (319 loc) · 8.69 KB
/
plugins.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const fs = require('fs');
const path = require('path');
const resolve = require('resolve');
const debug = require('debug')('talk:plugins');
const Joi = require('joi');
const amp = require('app-module-path');
const hjson = require('hjson');
const pkg = require('./package.json');
const PLUGINS_JSON = process.env.TALK_PLUGINS_JSON;
// Add the current path to the module root.
amp.addPath(__dirname);
let pluginsPath;
let plugins = {};
// Try to parse the plugins.json file, logging out an error if the plugins.json
// file isn't loaded, but continuing. Else, like a parsing error, throw it and
// crash the program.
try {
let envPlugins = path.join(__dirname, 'plugins.env.js');
let customPlugins = path.join(__dirname, 'plugins.json');
let defaultPlugins = path.join(__dirname, 'plugins.default.json');
if (PLUGINS_JSON && PLUGINS_JSON.length > 0) {
debug('Now using TALK_PLUGINS_JSON environment variable for plugins');
pluginsPath = envPlugins;
} else if (fs.existsSync(customPlugins)) {
debug(`Now using ${customPlugins} for plugins`);
pluginsPath = customPlugins;
} else {
debug(`Now using ${defaultPlugins} for plugins`);
pluginsPath = defaultPlugins;
}
// Load/parse the plugin content using hjson.
const pluginContent = fs.readFileSync(pluginsPath, 'utf8');
plugins = hjson.parse(pluginContent);
} catch (err) {
if (err.code === 'ENOENT') {
console.error(
'plugins.json and plugins.default.json not found, plugins will not be active'
);
} else {
throw err;
}
}
/**
* All the hooks from plugins must match the schema defined here.
*/
const hookSchemas = {
passport: Joi.func().arity(1),
router: Joi.func().arity(1),
context: Joi.object().pattern(/\w/, Joi.func().maxArity(1)),
hooks: Joi.object().pattern(
/\w/,
Joi.object().pattern(
/(?:__resolveType|\w+)/,
Joi.object({
pre: Joi.func(),
post: Joi.func(),
})
)
),
loaders: Joi.func().maxArity(1),
mutators: Joi.func().maxArity(1),
resolvers: Joi.object().pattern(
/\w/,
Joi.object().pattern(/(?:__resolveType|\w+)/, Joi.func())
),
typeDefs: Joi.string(),
schemaLevelResolveFunction: Joi.func(),
websockets: Joi.object({
onConnect: Joi.func(),
onDisconnect: Joi.func(),
}),
connect: Joi.func().maxArity(1),
};
/**
* PluginContext provides server context for the global application.
*/
class PluginContext {
constructor() {
this.pkg = pkg;
}
}
/**
* isInternal checks to see if a given plugin is internal, and returns true
* if it is.
*
* @param {String} name
* @returns {Boolean}
*/
function isInternal(name) {
const internalPluginPath = path.join(__dirname, 'plugins', name);
// Check to see if this plugin exists internally, because if it doesn't, it is
// external.
return fs.existsSync(internalPluginPath);
}
/**
* Returns the plugin path for the given plugin name.
*
* @param {any} name
* @returns
*/
function pluginPath(name) {
if (isInternal(name)) {
try {
return resolve.sync(name, {
moduleDirectory: 'plugins',
basedir: __dirname,
});
} catch (e) {
console.warn(e);
return undefined;
}
}
try {
return resolve.sync(name, { basedir: __dirname });
} catch (e) {
return undefined;
}
}
class Plugin {
constructor(entry) {
// This checks to see if the structure for this entry is an object:
//
// {"people": "^1.2.0"}
//
// otherwise it's checked whether it matches the local version:
//
// "people"
//
if (typeof entry === 'object') {
this.name = Object.keys(entry).find(name => name !== null);
this.version = entry[this.name];
} else if (typeof entry === 'string') {
this.name = entry;
this.version = `file:./plugins/${this.name}`;
} else {
throw new Error(
`plugins.json is malformed, refer to PLUGINS.md for formatting, expected a string or an object for a plugin entry, found a ${typeof entry}`
);
}
// Get the path for the plugin.
this.path = pluginPath(this.name);
}
require() {
if (typeof this.path === 'undefined') {
throw new Error(
`plugin '${
this.name
}' is not local and is not resolvable, plugin reconciliation may be required`
);
}
try {
this.module = require(this.path);
} catch (e) {
if (
e &&
e.code &&
e.code === 'MODULE_NOT_FOUND' &&
isInternal(this.name)
) {
console.error(
new Error(
`plugin '${
this.name
}' could not be loaded due to missing dependencies, plugin reconciliation may be required`
)
);
throw e;
}
console.error(
new Error(
`plugin '${this.name}' could not be required from '${this.path}': ${
e.message
}`
)
);
throw e;
}
}
}
/**
* Iterates over the plugins and gets the plugin path's, version, and name.
*
* @param {Array<Object|String>} plugins
* @returns {Array<Object>}
*/
const iteratePlugins = plugins => plugins.map(p => new Plugin(p));
// Add each plugin folder to the allowed import path so that they can import our
// internal dependencies.
Object.keys(plugins).forEach(type =>
iteratePlugins(plugins[type]).forEach(plugin => {
// The plugin may be remote, and therefore not installed. We check here if the
// plugin path is available before trying to monkey patch it's require path.
if (plugin.path) {
amp.enableForDir(path.dirname(plugin.path));
}
})
);
/**
* Stores a reference to a section for a section of Plugins.
*/
class PluginSection {
constructor(context, plugins) {
this.context = context;
this.required = false;
this.plugins = iteratePlugins(plugins);
}
require() {
if (this.required) {
return;
}
this.required = true;
this.plugins.forEach(plugin => {
// Load the plugin.
plugin.require();
if (isInternal(plugin.name)) {
debug(`loading internal plugin '${plugin.name}' from '${plugin.path}'`);
} else {
debug(`loading external plugin '${plugin.name}' from '${plugin.path}'`);
}
return plugin;
});
}
/**
* This iterates over the section to provide all plugin hooks that are
* available.
*/
hook(hookName) {
// Load the plugin source if we haven't already.
this.require();
return this.plugins
.filter(({ module }) => hookName in module)
.map(plugin => {
// Optionally bind the plugin context to a function if it's one.
const hook =
typeof plugin.module[hookName] === 'function'
? plugin.module[hookName].bind(this.context)
: plugin.module[hookName];
// Validate the hook.
if (hookName in hookSchemas) {
Joi.assert(
hook,
hookSchemas[hookName],
`Plugin '${
plugin.name
}' failed validation for the '${hookName}' hook`
);
}
return {
plugin,
[hookName]: hook,
};
});
}
}
const NullPluginSection = new PluginSection({}, []);
/**
* Stores references to all the plugins available on the application.
*/
class PluginManager {
constructor(plugins) {
this.context = new PluginContext();
this.sections = {};
for (let section in plugins) {
this.sections[section] = new PluginSection(
this.context,
plugins[section]
);
}
this.deferredHooks = [];
this.ranDeferredHooks = false;
}
/**
* Utility function which combines the Plugins.section and PluginSection.hook
* calls.
*/
get(sectionName, hookName) {
return this.section(sectionName).hook(hookName);
}
/**
* Utility function which combines the Plugins.section and PluginSection.hook
* calls and runs them when the `runDeferred` is called.
*/
defer(sectionName, hookName, callback) {
const plugins = this.section(sectionName).hook(hookName);
// If we've already ran the callbacks, then we should run it immediately.
if (this.ranDeferredHooks) {
plugins.forEach(callback);
} else {
this.deferredHooks.push({ plugins, callback });
}
}
/**
* Calls all deferred hooks.
*/
runDeferred() {
this.deferredHooks.forEach(({ plugins, callback }) =>
plugins.forEach(callback)
);
this.ranDeferredHooks = true;
}
/**
* Returns the named section if it exists, otherwise it returns an empty
* plugin section.
*/
section(section) {
if (section in this.sections) {
return this.sections[section];
}
return NullPluginSection;
}
}
module.exports = {
plugins,
pluginsPath,
PluginManager,
isInternal,
pluginPath,
iteratePlugins,
};