npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@art-of-coding/wormhole

v2.1.2

Published

Simple but versatile IPC interface

Downloads

37

Readme

Wormhole

This module can be used to provide handy functionality on top of remote connections such as Inter-Process Communication (IPC). It's designed to be small and easy to use.

It has the following features:

  • Send and receive events with arbitrary arguments
  • RPC-style remote command calling
  • Can be used on both sides of a communication channel

Wormhole is developed with process and ChildProcess (over IPC) in mind.

Installing

npm i @art-of-coding/wormhole

Example

This is an attempt to write a complete example. In this example we fork a process and then we want to communicate with it. By using wormhole on both ends, we can send and receive events and commands in both directions.

The master process:

import { fork } from 'child_process'
import Wormhole from '@art-of-coding/wormhole'

const child = fork('./example-child.js')
const wormhole = new Wormhole(child)

// Register a `startup` event handler
wormhole.events.on('startup', () => {
  console.log('received startup event!')
})

// Register an `add` command
wormhole.define('add', function (a: number, b: number) {
  return a + b
})

// Send the `quit` event to the child
setTimeout(() => wormhole.event('quit'), 5000)

The child process:

import Wormhole from '@art-of-coding/wormhole'

const wormhole = new Wormhole(process)

// Register a `quit` event handler
wormhole.events.once('quit', () => {
  process.exit(-1)
})

// Send an event
wormhole.event('startup')

// Call a remote command
wormhole.command<number>('add', 5, 6).then(result => {
  console.log(`5 + 6 = ${result}`)
})

API

new Wormhole (channel)

Instantiates a new wormhole instance.

  • channel: The channel to use (must either be process or an instance of ChildProcess)

Events

A Wormhole instance is an EventEmitter with the following events:

  • error: There was an error
  • message: A received message that isn't handled by Wormhole
  • disconnect: The channel is disconnected

wormhole.connected

Returns true if the channel is connected.

wormhole.pendingCallbacks

Returns the amount of pending command callbacks.

wormhole.events

Getter to get the events EventEmitter.

wormhole.events.on('some-event', () => {
  console.log('some-event emitted')
})

wormhole.define (name, fn, context)

Define a command.

If the command is not a promise, the result will be cast into one. Throwing an Error (or subclassed) results in the rejection of the promise.

  • name: The name for this command
  • fn: The function for this command
  • context: The command context (optional)
wormhole.define('add', function (a, b) {
  if (isNaN(a) || isNaN(b)) {
    throw new TypeError('arguments must be numbers')
  }

  return a + b
})

wormhole.event (event, ...args)

Emit an event named event.

  • event: The name of the event
  • ...args: The event arguments

wormhole.command<TResult> (name, ...args): Promise<TResult>

Call a remote command named name.

  • name: The name of the command to call
  • ...args: The command arguments
wormhole.command<number>('add', 5, 6).then(result => {
  console.log(`5 + 6 = ${result}`)
})

wormhole.write (message)

Writes a message over the channel.

  • message: The message primitive or object

wormhole.disconnect ()

Disconnects the channel and releases all resources. After calling this, the instance is no longer usable.

License

Copyright 2017-2021 Art of Coding.

This software is licensed under the MIT License.