
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
plug-and-play
Advanced tools
Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.
Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.
We encourage your to read the detailed tutorial on how to create a plugin architected with Plug and Play published by Adaltas.
Library and application authors define hooks, see ./sample/lib.js
:
const plugandplay = require('plug-and-play')
const plugins = plugandplay()
module.exports = {
// Create and export a new Plug and Play instance
plugins: plugins,
// Our core library function
print: function() {
// Wrap-up code
plugins.call({
// Identify this hook with a name
name: 'hooks:print',
// Expose arguments to plugins authors
args: {
data: { message: 'hello' }
},
// Default implementation
handler: ({data}) => {
// Original library
console.log(data.message)
}
})
}
}
Users and pluging authors can now register their own hooks, see ./sample/index.js
:
const mysuperlibrary = require('./lib')
mysuperlibrary.plugins.register({
hooks: {
'hooks:print': ({data}, handler) => {
// Alter the argument
data.message = 'Hello World'
// Print a message before the library code
console.log('>>>>>>>>>>>')
// Call the original handler
const result = handler.call(null, {data: data})
// Print a message after the library code
console.log('<<<<<<<<<<<')
return result
}
}
})
mysuperlibrary.print()
While the original print
function was only printing Hello
to stdout, the introduction of this new plugin prints:
>>>>>>>>>>>
Hello world
<<<<<<<<<<<
The package exports a function to create a new instance:
const plugandplay = require('plug-and-play')
const plugins = plugandplay()
It accepts an option object with the properties:
args
([string])chain
(any)register
function, default to the Plug-And-Play instance.parent
(plugandplay)plugins
([plugin])A plugin is an object literal with the properties:
hooks
({name: hook})name
(string)require
([string])Alternatively, a plugin can be defined as a function which return the same plugin object and receive the args
property from the Plug-And-Play function.
A hook is an object literal with the properties:
after
([string])name
(string)before
([string])Note, when referencing plugins with after
and before
, the plugins do not need to exists. If they do, they are required to export a hook of the same name.
The function signature is:
call({args = [], handler, hooks = [], name})
Execute a hander function and its associated hooks. The first argument is an object with the properties:
args
(any)
Argument pass to the handler function as well as all hook handlershandler
(function)args
property.name
(string)hooks
([hook])The function signature is:
get({name, hooks, sort})
The get
function return hooks. It expects a property option
which is a literal object with the properties:
name
(string)hooks
([hook])
List of completary hooks from the end user.sort
(boolean)after
and before
properties.The function signature is:
register(plugin)
Plugin can be provided when instantiating Plug-And-Play by passing the plugins
property or they can be provided later on by calling the register
function. The function only accept one argument, the plugin
property:
plugin
(plugin)The function signature is:
registered(name)
Check if a plugin is registered.
Worth considering: move the name
property of get
and call
into the first argument and move the other properties into a second argument named option.
To automatically generate a new version:
yarn run release
There is currently no CI, copy/paste the code after release
git push --follow-tags origin master && npm publish
FAQs
Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.
The npm package plug-and-play receives a total of 4,971 weekly downloads. As such, plug-and-play popularity was classified as popular.
We found that plug-and-play demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.