-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathplugins.js
More file actions
83 lines (73 loc) · 2.45 KB
/
Copy pathplugins.js
File metadata and controls
83 lines (73 loc) · 2.45 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
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
'use strict';
/*
const fs = require('fs');
const path = require('path');
const debug = require('debug')('gateway:init');
const util = require('util')
*/
const _ = require('lodash');
const assert = require('assert')
const logging = require('./logging');
const configService = require('./config');
const stats = require('./stats');
const CONSOLE_LOG_TAG = 'microgateway-core plugins';
var Plugins = function () {
this.config = configService.get();
this.emgConfigs = null;
};
/**
* calls init and returns plugin loaded
* @param options {pluginName,plugin}
* @returns {*}
*/
Plugins.prototype.loadPlugin = function (options) {
var name;
var plugin;
// extract only emg level configs by removing plugin configs.
if (options.allPluginNames && !this.emgConfigs) {
this.emgConfigs = {}
let sequence = [];
if ( this.config.edgemicro.plugins && this.config.edgemicro.plugins.sequence ) {
sequence = this.config.edgemicro.plugins.sequence;
}
Object.keys(this.config).filter( key => sequence.indexOf(key) === -1 && key !== 'quotas'
&& options.allPluginNames.indexOf(key) === -1).forEach( emgKey => {
this.emgConfigs[emgKey] = this.config[emgKey];
});
}
if (_.isObject(options)) {
assert(options.plugin, "must have plugin loaded in memory");
if (_.isFunction(options.plugin)) {
plugin = options.plugin;
} else {
assert(_.isFunction(options.plugin.init), 'init must be a function');
plugin = _.bind(options.plugin.init, options.plugin);
}
assert(options.pluginName, "must have plugin name");
name = options.pluginName;
} else {
throw new Error('bad plugin')
}
const config = this.config;
const logger = logging.getLogger();
var middleware;
const subconfig = config[name] || {};
subconfig.emgConfigs = this.emgConfigs;
if (plugin) {
if(config.keys && config.keys.key && config.keys.secret){
subconfig.key = config.keys.key;
subconfig.secret = config.keys.secret;
}
middleware = plugin(subconfig, logger, stats);
assert(_.isObject(middleware), 'ignoring invalid plugin handlers ' + name);
middleware.id = name;
logger.info({}, 'installed plugin from ' + name);
logger.debug({}, 'installed plugin from ' + name + ' and subconfig=' + JSON.stringify(subconfig));
} else {
logger.consoleLog('error',{component: CONSOLE_LOG_TAG},'error loading plugin', name);
}
return middleware
};
exports = module.exports = function () {
return new Plugins();
};