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

worker-process

v0.0.0-rc2

Published

Dead simple worker child processes for Node.js with a modern Promise API and a binary IPC.

Downloads

6

Readme

worker-process

npm version javascript standard style travis build coveralls coverage david dependencies david dev dependencies

Dead simple worker child processes for Node.js with a modern Promise API and a binary IPC.

For people who don't want to use external servers for binary IPC, don't want to mess with child_process, and want to use something that "just works".


master.js

import {createWorker} from 'worker-process'
import msgpack from 'msgpack-lite'

async function main () {
  // Create a worker.
  const worker = await createWorker('./worker.js')

  let gotMessage = false
  await Promise.race([
    // Wait for the worker to complete (resolve) or fail (reject).
    worker.lifetime,

    // Do what you want to do with the worker.
    (async () => {
      // Handle messages from worker process
      worker.onmessage = message => {
        console.log('master:message', msgpack.decode(message))
        gotMessage = true
      }

      // Send a message to the worker
      const data = 'hello, worker!'
      await worker.send(msgpack.encode(data))

      // Wait until the worker stops
      await worker.lifetime
      // or optionally stop the worker yourself if you're no longer waiting
      // for more messages from it
      worker.kill()
    })()
  ])

  // If the worker exits successfully but you didn't finish what you wanted to
  // do, you can handle it
  if (!gotMessage) {
    throw new Error('Worker did not say hello :(')
  }
}

main().catch(err => {
  console.error('master:fail')
  console.error(err)
})

worker.js

import {connect} from 'worker-process'
import msgpack from 'msgpack-lite'
import exit from 'exit'

async function main () {
  // Connect to the IPC socket.
  // All nuances and errors from here on will be handled by worker-process
  // by logging the error and exiting the process with exit code 1 to
  // guarantee safety and consistency
  const workerConnection = await connect()

  // Handle messages from the parent process
  workerConnection.onmessage = message => {
    console.log('worker:message', msgpack.decode(message))
  }

  // Send messages to the parent process
  const data = ['hello', 'world!']
  await workerConnection.send(msgpack.encode(data))

  // Wait for the message to complete sending, and then close the connection
  await workerConnection.finish()
}

console.log('worker:start')
main().then(() => {
  exit(0)
}).catch(err => {
  console.error('worker:fail')
  console.error(err)
})

I recommend using MessagePack as the IPC message format.


How it works

The "worker" process is created via child_process.spawn(). At spawn, a fourth stdio is created (fd = 3), which is a node.js duplex stream because node.js creates a duplex socket, rather than a pipe for communication. When the child process is connected, connectivity is assured by sending the handshake message, 0x00, and waiting for the worker process to send 0x01 back. From there, data is passed back and forth using the binary protocol.