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

child-process-data

v0.7.0

Published

A helper to handle data output from child processes

Downloads

77

Readme

child-process-data

A helper to capture data streams from child processes

Simple usage

Child processes output messages through their stdout and stderr. They get interleaved with messages from other child processes and messages from the parent process. child-process-data is a Node.js module that helps disentangle and recover these messages.

import childProcessData from 'child-process-data';
import {spawn} from 'child_process';

const proc1 = spawn('echo', ['Lorem ipsum']); // Spawns a child process
const proc2 = spawn('echo', ['dolor sit amet']); // Spawns another child process

childProcessData(proc1).then(res => { // Recovers output from proc1
  res.all(); // Returns 'Lorem ipsum\n'
});
childProcessData(proc2).then(res => { // Recovers output from proc2
  res.all(); // Returns 'dolor sit amet\n'
});

Options

As a second argument, childProcessData can take a literal object with the following keys:

  • silent: if true, childProcessData will capture but not print the child process logging messages.

Accessing individual messages

To access the output of a child process that exited without errors, you use the then channel of the promise returned by childProcessData.

Messages output on stdout are contained in array property outMessages. Those on stderr are contained in array property errMessages. They all are contained in order in array property allMessages.

Using file normal-exit.js:

process.stdout.write('lorem\n');
process.stdout.write('ipsum\n');
process.stderr.write('dolor\n');
process.stdout.write('sit\n');
process.stderr.write('amet\n');

We can recover all outputs:

childProcessData(spawn('node', ['./test/examples/normal-exit.js'])).then(res => {
  res.outMessages[0] === 'lorem\n'; // true
  res.outMessages[1] === 'ipsum\n'; // true
  res.outMessages[2] === 'sit\n'; // true
  res.errMessages[0] === 'dolor\n'; // true
  res.errMessages[1] === 'amet\n'; // true
  res.allMessages[0] === 'lorem\n'; // true
  res.allMessages[1] === 'ipsum\n'; // true
  res.allMessages[2] === 'dolor\n'; // true
  res.allMessages[3] === 'sit\n'; // true
  res.allMessages[4] === 'amet\n'; // true
});

Accessing all messages

As a conveniency, you can recover messages of a child process that exited without errors as a whole. Methods out, err and all return a concatenation respectively of array properties outMessages, errMessages and allMessages.

childProcessData(spawn('node', ['./test/examples/normal-exit.js'])).then(res => {
  res.out() === 'lorem\nipsum\nsit\n'; // true
  res.err() === 'dolor\namet\n'; // true
  res.all() === 'lorem\nipsum\ndolor\nsit\namet\n'; // true
});

Accessing uncaught error messages

If the child process exits with a non-zero error code, then you recover the output messages using the catch channel of the promise returned by childProcessData. You can access all messages through the result property of the returned error object (it is the object that would have been returned via the then channel of the promise, had it not failed).

Using file error-exit.js:

childProcessData(spawn('node', ['./test/examples/error-exit.js'])).catch(err => {
  let res = err.result;

  res.outMessages[0] === 'lorem\n'; // true
  res.outMessages[1] === 'ipsum\n'; // true
  res.outMessages[2] === 'sit\n'; // true
  res.errMessages[0] === 'dolor\n'; // true
  res.errMessages[1]).match(/Error:.*amet/); // Non empty array
  res.allMessages[0] === 'lorem\n'; // true
  res.allMessages[1] === 'ipsum\n'; // true
  res.allMessages[2] === 'dolor\n'; // true
  res.allMessages[3] === 'sit\n'; // true
  res.allMessages[4]).match(/Error:.*amet/); // Non empty array

  res.out() === 'lorem\nipsum\nsit\n'; // true     
  res.err() === 'dolor\n' + res.errMessages[1]; // true
  res.all() === 'lorem\nipsum\ndolor\nsit\n' + res.allMessages[4]; // true
});

Dealing with long lived processes

childProcessData is tweaked for TDD with Gulp. It keeps track of Starting and Finished messages and resolves even if the process keeps running. Therefore even after resolving, it keeps buffering the Gulp TDD messages.

But if you use childProcessData on another long running processes, to obtain the same behavior, you need to pass {dontBlock: true} option or a {startDelay: numberInMs} option. Then, after 1 ms or startDelay ms, logs will be accessible.

interceptMessage and resolveMessage helpers

interceptMessage(proc, message, options) helps capture messages. It returns a promise that resolves right away or after {startDelay}. Thereafter resolveMessage(message) returns a function returning a promise that resolves on seeing message either on stdout or stderr.

A possible usage is when defining gulp tasks that launch servers. You can take advantage of their logs to start tasks that rely on their being ready without the need of maintaining some sockets open.

Test helpers

childProcessData's primary intent was to help test Gulp processes by catching their outputs, especially while developing Yeoman generator generator-wupjs.

As such, some tests are pretty recurring, and two helpers around childProcessData are provided to help build them fast: makeSingleTest and makeIOTest.

makeSingleTest

import {makeSingleTest} from 'child-process-data';
import {expect} from 'chai';

describe('One test suite', function () {
  it('One test', function () {
    const test = makeSingleTest({
      childProcess: ['echo', ['Hello world']],

      checkResults (results) {
        expect(results.out()).to.equal('Hello world\n');
      },
    });

    return test();
  });
});

makeIOTest

import {makeIOTest} from 'child-process-data';

describe('One test suite', function () {
  it('One test', function () {
    const test = makeSingleTest({
      childProcessFile: 'build/src/do-this.js',

      messages: [
        {io: ['Did you do this?', 'Yes\n']},
        {io: ['And that?', 'Not yet\n']},
      ]
    });

    return test();
  });
});

Special extensions

The results argument of the checkResults function you provide has helper extensions to help you do some common testing.

  • test(msg => fn(msg)): Returns true if passes for all messages.
  • test(arrayOfFns): Returns true if passes for all function and all messages.
  • testUpTo(msg => fn(msg), msg0, {included}): Returns true if passes for all messages up to msg0, included or not.
  • testUpTo(arrayOfFns, msg0, {included}): Returns true if passes for all functions and all messages up to msg0, included or not.
  • forget(): In long processes where results is continuously updated, forget all previous messages.
  • forgetUpTo(msg0, {included}): In long processes where results is continuously updated, forget all previous messages up to msg0, included or not.

License

child-process-data is MIT licensed.

© 2016-2018 Jason Lenoble