node: add node.get() for plugins.

This commit is contained in:
Christopher Jeffrey 2017-07-14 11:18:09 -07:00
parent 7530359b55
commit b52a5e3fd2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 29 additions and 12 deletions

View File

@ -113,7 +113,7 @@ Node.prototype.init = function init() {
}); });
this.workers.on('log', (text, child) => { this.workers.on('log', (text, child) => {
this.logger.debug(`Worker ${child.id} says:`); this.logger.debug('Worker %d says:', child.id);
this.logger.debug(text); this.logger.debug(text);
}); });
@ -291,6 +291,8 @@ Node.prototype.use = function use(plugin) {
case 'mempool': case 'mempool':
case 'miner': case 'miner':
case 'pool': case 'pool':
case 'rpc':
case 'http':
assert(false, `${plugin.id} is already added.`); assert(false, `${plugin.id} is already added.`);
break; break;
} }
@ -319,16 +321,17 @@ Node.prototype.has = function has(name) {
}; };
/** /**
* Require a plugin. * Get a plugin.
* @param {String} name * @param {String} name
* @returns {Object} * @returns {Object|null}
*/ */
Node.prototype.require = function require(name) { Node.prototype.get = function get(name) {
let plugin; let plugin;
assert(typeof name === 'string', 'Plugin name must be a string.'); assert(typeof name === 'string', 'Plugin name must be a string.');
// Reserved names.
switch (name) { switch (name) {
case 'chain': case 'chain':
assert(this.chain, 'chain is not loaded.'); assert(this.chain, 'chain is not loaded.');
@ -345,11 +348,27 @@ Node.prototype.require = function require(name) {
case 'pool': case 'pool':
assert(this.pool, 'pool is not loaded.'); assert(this.pool, 'pool is not loaded.');
return this.pool; return this.pool;
case 'rpc':
assert(this.rpc, 'rpc is not loaded.');
return this.rpc;
case 'http':
assert(this.http, 'http is not loaded.');
return this.http;
} }
plugin = this.plugins[name]; return this.plugins[name];
assert(plugin, `${name} is not loaded.`); };
/**
* Require a plugin.
* @param {String} name
* @returns {Object}
* @throws {Error} on onloaded plugin
*/
Node.prototype.require = function require(name) {
let plugin = this.get(name);
assert(plugin, `${name} is not loaded.`);
return plugin; return plugin;
}; };
@ -362,13 +381,11 @@ Node.prototype.loadPlugins = function loadPlugins() {
let plugins = this.config.array('plugins', []); let plugins = this.config.array('plugins', []);
let loader = this.config.func('loader'); let loader = this.config.func('loader');
if (!loader)
return;
for (let plugin of plugins) { for (let plugin of plugins) {
if (typeof plugin === 'string') if (typeof plugin === 'string') {
assert(loader, 'Must pass a loader function.');
plugin = loader(plugin); plugin = loader(plugin);
}
this.use(plugin); this.use(plugin);
} }
}; };

View File

@ -68,7 +68,7 @@ server.create = function create(options) {
}); });
workers.on('log', (text, child) => { workers.on('log', (text, child) => {
logger.debug(`Worker ${child.id} says:`); logger.debug('Worker %d says:', child.id);
logger.debug(text); logger.debug(text);
}); });