Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Plugin functionality for modular libraries.
For an implementation using this module see air.
npm i zephyr --save
var plug = require('zephyr')
// create the plugin system
, sys = plug({});
// load plugins
sys.plugin([require('plugin-file')]);
// create a component
var component = sys();
// do something with the plugin functionality
proto
: A reference to the prototype object.type
: A reference to the class to instantiate.main
: An alternative main function (factory).plugin
: Override the default plugin function.hooks
: Array of functions invoked as constructor hooks.field
: String name of field for plugin function.Plugins are functions invoked in the scope of a class prototype that typically decorate the prototype object (using this
) but may also add static methods or load other plugins.
To load plugin(s) call the plugin
function passing an array of plugin functions:
var plug = require('zephyr')
, sys = plug();
sys.plugin([require('plugin-file')]);
It is possible to pass a configuration object at runtime to a plugin by using an object with a plugin
function and a conf
object:
var plug = require('zephyr')
, sys = plug();
var plugins = [
{
plugin: function(conf) {
// do something with the runtime configuration
// initialize the plugin
},
conf: {foo: 'bar'}
}
];
sys.plugin(plugins);
The most common use case for plugins is to decorate the class prototype with functions that are available on instances returned by the main function, these are referred to as instance plugins. Plugins may also decorate the main function these are referred to as static plugins.
Plugin implementations may mix functionality, for clarity the examples show the distinct styles.
To create an instance plugin just assign a function to this
within the plugin function:
module.exports = function plugin() {
// decorate class prototype
this.chain = function() {
// return this to allow chaining on this function
return this;
}
}
Now load the plugin and invoke the instance method:
var comp
, plug = require('zephyr')
// create the plugin system
, sys = plug();
// load the plugin
sys.plugin([require('instance-plugin')]);
// get the instance from the main function
comp = sys();
// invoke the plugin method
comp.chain();
To decorate the main function with static functions assign to this.main
.
module.exports = function plugin() {
this.main.method = function() {
// implement method functionality
}
}
You can then invoke the function on the plugin system:
var plug = require('zephyr')
, sys = plug();
sys.plugin([require('static-plugin')]);
sys.method();
You can depend upon other plugins by calling this.plugin
within the plugin function. This allows plugins to composite other plugins in order to resolve plugin dependencies or provide plugin groups (related plugins).
module.exports = function plugin() {
this.plugin([require('plugin-dependency')]);
}
By convention plugins are singular and plugin groups are plural.
Typically a plugin will be a single module (file) and the plugin function is exported, however sometimes you may prefer to export a class or other function; in this case the plugin initialization function may be assigned to the exported object and referenced using the field
option.
Consider a module that exports a class but also wishes to expose a plugin function:
function Component(){}
Component.init = function() {
// implement plugin functionality
}
module.exports = Component;
We can then configure the plugin system by specifying the field
option with the name of the function, in this case init
:
var zephyr = require('zephyr')
, main = zephyr({field: 'init'});
module.exports = main;
Then we can require the file when loading the plugin and the init
function will be invoked for plugin initialization:
main.plugin([require('./component-module')]);
Plugins accept a single argument which is a configuration object optionally passed when loading the plugin. Useful when a plugin wishes to add functionality conditionally. For example:
module.exports = function plugin(conf) {
conf = conf || {};
// implement default logic
if(conf.ext) {
// implement extended logic
}
}
Then a consumer of the plugin system could enable the extended logic:
sys.plugin({plugin: require('conf-plugin-file'), conf: {ext: true}})
For some plugin systems it is useful to be able to add functionality in the scope of the component instance rather than the prototype. For example to add a default listener for an event, set properties on the instance or start running logic on component creation (or based on the plugin configuration).
Pass an array as the hooks
option:
var plug = require('zephyr')
, sys = plug({hooks: []});
And an additional register
method is available on plugin
:
function hook() {
// do something on component instantiation
}
module.exports = function plugin() {
// register the constructor hook
this.plugin.register(hook);
}
Note that hooks are only applied when the component is created with the main function:
var plug = require('zephyr')
, sys = plug({hooks: []});
sys.plugin([require('plugin-with-hook')]);
// constructor hooks are applied
var comp = sys();
// bypass constructor hooks, probably not desirable
comp = new sys.Type();
A plugin system is the result of invoking the zephyr
function:
var plug = require('zephyr')
, sys = plug();
module.exports = sys;
Which allows the ability to mix multiple components using plugins in the same code base. Typically you would export the main function returned as the plugin system.
Pass the proto
and type
options to extend the plugin system:
var plug = require('zephyr');
// custom constructor
function PluginSystem() {}
var proto = PluginSystem.prototype
// extend the prototype with base functionality
// available to all plugins
var sys = plug({proto: proto, type: PluginSystem});
module.exports = sys;
For an example implementation see air.js.
;(function() {
'use strict'
function plug(opts) {
opts = opts || {};
/**
* Default plugin class.
*/
function Component(){}
var main
, hooks = opts.hooks
, proto = opts.proto || Component.prototype;
/**
* Plugin method.
*
* @param plugins Array of plugin functions.
*/
function plugin(plugins) {
var z, method, conf;
for(z in plugins) {
if(typeof plugins[z] === 'function') {
method = plugins[z];
}else{
method = plugins[z].plugin;
conf = plugins[z].conf;
}
if(opts.field && typeof method[opts.field] === 'function') {
method = method[opts.field];
}
method.call(proto, conf);
}
return main;
}
/**
* Create an instance of the class represented by *Type* and proxy
* all arguments to the constructor.
*/
function construct() {
var args = Array.prototype.slice.call(arguments);
function Fn() {
return main.Type.apply(this, args);
}
Fn.prototype = main.Type.prototype;
return new Fn();
}
/**
* Invoke constructor hooks by proxying to the main construct
* function and invoking registered hook functions in the scope
* of the created component.
*/
function hook() {
var comp = hook.proxy.apply(null, arguments);
for(var i = 0;i < hooks.length;i++) {
hooks[i].apply(comp, arguments);
}
return comp;
}
/**
* Register a constructor hook function.
*
* @param fn The constructor hook.
*/
function register(fn) {
if(typeof fn === 'function' && !~hooks.indexOf(fn)) {
hooks.push(fn);
}
}
main = opts.main || construct;
// hooks enabled, wrap main function aop style
if(Array.isArray(hooks)) {
hook.proxy = main;
main = hook;
}
// class to construct
main.Type = opts.type || Component;
// static and instance plugin method
main.plugin = proto.plugin = opts.plugin || plugin;
// hooks enabled, decorate with register function
if(Array.isArray(hooks)) {
main.plugin.register = register;
}
// reference to the main function for static assignment
proto.main = main;
return main;
}
module.exports = plug;
})();
Developer workflow is via gulp but should be executed as npm
scripts to enable shell execution where necessary.
Run the headless test suite using phantomjs:
npm test
To run the tests in a browser context open test/index.html or use the server npm start
.
Serve the test files from a web server with:
npm start
Run the test suite and generate code coverage:
npm run cover
Run the source tree through eslint:
npm run lint
Remove generated files:
npm run clean
Compile the test specifications:
npm run spec
Generate instrumented code from lib
in instrument
:
npm run instrument
Generate the project readme file (requires mdp):
npm run readme
Everything is MIT. Read the license if you feel inclined.
Generated by mdp(1).
FAQs
Plugin facade
The npm package zephyr receives a total of 3,211 weekly downloads. As such, zephyr popularity was classified as popular.
We found that zephyr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.