Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

plug-and-play

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

plug-and-play

Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.

  • 2.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3K
decreased by-19.98%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

Node.js Plugable package

Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.

Main features

  • Extention points definition
    Simple to declare new extention points, yet a lot of flexibility to the plugin authors.
  • Hook definition
    Plugin writer can intercept calls to a function by placing their own logicl before, after and even switching the default implementation.
  • Dependency management
    Plugins can require other plugins as required dependencies as well as choose the order of execution of each hook.
  • Promise support
    Hook can be synchronous and asynchronous when returning a promise.
  • Nested/hierachical
    Instanciate plugin instances with a parent reference and parent hooks will also be available inside the children.

Learning

We encourage your to read the detailed tutorial on how to create a plugin architected with Plug and Play published by Adaltas.

Quick example

Library and application authors must 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
<<<<<<<<<<<

Keywords

FAQs

Package last updated on 22 Jan 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc