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

observable-process

v8.0.0-pre4

Published

High-level support for running, observing, and interacting with child processes in Node.js

Downloads

217

Readme

ObservableProcess

CircleCI Coverage Status install size Language grade: JavaScript

ObservableProcess enhances the slightly too low-level Node.JS ChildProcess model with functionality to observe the behavior of processes more conveniently. In particular:

  • easy access to the accumulated content of stdout and stderr
  • await content in stdout and stderr
  • combines stdout and stderr into a new output stream
  • await the process end
  • easier access to the process exit code
  • signals whether the process ended naturally or was manually terminated

ObservableProcess is for short-lived processes, for example when testing the terminal output of applications. Since ObservableProcess stores all output from the child process in memory, executing long-running processes that produce lots of output through ObservableProcess will cause high memory consumption.

Setup

Add this library to your code base:

Load this library into your JavaScript code:

const observableProcess = require("observable-process")

or

import * as observableProcess from "observable-process"

Starting processes

The best way to provide the command to start is in the form of an argv array:

const observable = observableProcess.start(["node", "server.js"])

You can also provide the full command line to run as a string:

const observable = observableProcess.start("node server.js")

By default, the process runs in the current directory. To set a different working directory for the subprocess:

const observable = observableProcess.start("node server.js", { cwd: "~/tmp" })

You can provide custom environment variables for the process:

const observable = observableProcess.start("node server.js", {
  env: {
    foo: "bar",
    PATH: process.env.PATH,
  },
})

Without an env parameter, ObservableProcess uses the environment variables from the parent process.

Reading output

The stdout and stderr variables of an ObservableProcess behave like normal readable streams:

// normal consumption of data from STDOUT via the event stream
observable.stdout.on("data", function () {
  // ...
})

They also provide extra functionality to access and search their aggregated content. To get all content from STDOUT as a string:

const text = observable.stdout.fullText()

To wait for text to appear in STDOUT:

const match = await observable.stdout.waitForText("server is online")
// => "server is online"

To wait for a regular expression on STDOUT:

const match = await observable.stdout.waitForRegex(/running at port \d+/)
// => "running at port 3000"

Comparable functionality is available for stderr. ObservableProcess also provides a new output stream with the combined content of STDOUT and STDERR:

observable.output.on("data", function (data) {
  // ...
})
const text = observable.output.fullText()
await observable.output.waitForText("server is online")
const port = await observable.output.waitForRegex(/running at port \d+./)

You also get a copy of the process output after it ended (see below).

Sending input to the process

ObservableProcess exposes the stdin stream of its underlying ChildProcess:

observable.stdin.write("my input\n")
observable.stdin.end()

Get the process id

observable.pid()

Stop the process

Wait until the process ends naturally:

const result = await observable.waitForEnd()
assert.equal(result, {
  status: "finished",
  exitCode: 0,
  stdText: "... content from STDOUT ...",
  errText: "... content from STDERR ...",
  combinedText: "... content from both STDOUT and STDERR ...",
})

Manually stop the process:

const result = await observable.kill()
assert.equal(result, {
  status: "killed",
  stdText: "... content from STDOUT ...",
  errText: "... content from STDERR ...",
  combinedText: "... content from both STDOUT and STDERR ...",
})

Related libraries

  • nexpect: Allows to define expectations on command output, and send it input, but doesn't allow to add more listeners to existing long-running processes, which makes declarative testing hard.

Development

If you want to hack on ObservableProcess:

  • run all tests: make test
  • run automated code repair: make fix
  • see all make commands: make help

To deploy a new version:

  • update the version in package.json and commit to master
  • run npm publish