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

mprocess

v1.7.1

Published

child_process simple wrapper using Promise

Downloads

17

Readme

mprocess.js

Simple ChildProcess module wrapper

NodeJS has great module that handle creating, manipulating, and destroying child processes using simple module called Child Process (https://nodejs.org/api/child_process.html). Personally, I think Child Process module compensates javascript single-threaded nature when an application needs to consume more CPU power explicitly. MProcess was developed when I need to send commands to child processes and retrieve the responses. Although this functionality is already managed by Child Process .send(), but it lacks state management when a parent process send several commands / messages to several child processes. MProcess works well on maintaining the state of a message ("tell which message is responding which command"). Currently, MProcess only wraps spawn() and fork() method.

Installation

$ npm install mprocess

API

Constructor

MProcess(command[,args,][, type][, options])

Parameters

  • command : The command to run
  • args : Array of arguments for command. Default to empty array
  • type : Type of runner. Currently support MProcess.FORK to invoke fork or MProcess.SPAWN to invoke spawn. Default to MProcess.SPAWN
  • options : Object containing parameter that will control the behaviour of invoked process. Accept all parameter defined in https://nodejs.org/api/child_process.html. Default to {}

Return

MProcess Object

Methods

  • run()
    After creating new MProcess, the process itself is not automatically created. To create the process, invoke run()
  • kill([code])
    Kill the underlying process. Custom signal can ben passed to child process via code
  • done(fn)
    Receive callback as parameter that is invoked when the child process is exited.
    • fn(ret)
      ret could be an integer with 0 value, or an object containing code and signal if the child process not cleanly exited
  • clone()
    Create new MProcess object based on current instance of MProcess
  • send(messsage)
    Send message to child process. Note that send() can only be used if MProcess instance is initiated using MProcess.FORK type. Return Promise and will resolve if child process successfully response, and reject if child process can't response in timely fashion.
  • getProcess()
    Return ChildProcess object of the current instance of MProcess or null if run() has not been called previously.
  • onStdOutFinish(fn)
    Receive callback as parameter that is invoked when the child process finish streaming its standard output. Callback not fired if you supply options during instantiation with {stdio : 'inherit'}
    • fn(str)
      str contains the output string
  • onStdOutError(fn)
    Receive callback as parameter that is invoked when the child process experience errors and writing to its std err. Callback not fired if you supply options during instantiation with {stdio : 'inherit'}
    • fn(str)
      str contains the error string

How to use

SPAWN

var MProcess = require("mprocess")
var processPath = "/path/to/some/javascript/file"
var instance = new MProcess("node",[processPath],MProcess.SPAWN);
instance.run()
instance.done(function(code){
    //code == 0
}) 

FORK

main.js

var MProcess = require("mprocess")
var processPath = "/path/to/some/worker/file"
var instance = new MProcess("node",[processPath],MProcess.FORK);
instance.run()
instance.send("here my message").then(function(resp){
    // resp contains valid javascript object
}).catch(console.error)
instance.done(function(code){
    //code == 0
}) 

To be able answer on parent process send(), the worker script must able to do this :

process.on("message",function(message){
    var id = message.id; // message id, internally created by MProcess
    var data = message.data; // will contains 'here my message'
    // some process
    var response = {valid javascript object}
    process.send({
        id : id,
        data : response
    })
})

GitHub

https://github.com/aerios/mprocess