New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

child-service

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

child-service

Start services as child-process, wait until they are ready and stop them.

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-28.57%
Maintainers
1
Weekly downloads
 
Created
Source

child-service

NPM version

Start services as child-process, wait until they are ready and stop them.

Installation

npm install child-service

Usage

Let's assume, we have a service that does not detach, but it is not ready immediately after start. We want to start this service call some endpoints and stop it again (for example in tests). Assume that our service is implemented in service.js (for portability, we use a Node.js program in this example, but I actually wrote this tool for use with chisel.

We can now do the following.

const { ChildService } = require("child-service");
const got = require("got");

// We call nodejs in this example, but in reality, it
// is probably some binary executable.
const childService = new ChildService({
  command: process.execPath,
  args: ["service.js"],
  readyRegex: /Listening on port 3000/,
  spawnOptions: {
    cwd: __dirname
  }
});

(async function () {
  await childService.start();
  console.log("Started!");

  const response = await got("http://127.0.0.1:3000");
  console.log(response.body);

  await childService.stop();
  console.log("Stopped!");
})();

This will start the service, wait until the stdout matches pattern, then call an endpoint and stop it again. The output of this program is:

Started!
hello

Stopped!

The package also makes sure that processes are stopped when the parent-process dies unexpectedly. It takes into account quitting by process.exit() as well as SIGTERM, SIGINT and SIGKILL.

API reference

ChildService

The class for starting and stopping services.

Kind: global class
Access: public

new ChildService(userOptions)

Create a new child-service

ParamTypeDescription
userOptionsobjectparameters
userOptions.commandstringthe command to execute
userOptions.argsArray.<string>arguments to the command
userOptions.readyRegexRegExpprocess is assumed to be ready, when this regex matches the output.
userOptions.outputLimitnumberonly look for readyRegex in the first "outputLimit" number of bytes of the output.
userOptions.spawnOptionsobjectoptions to pass to child_process.spawn
userOptions.timeoutAfterSignalnumberhow long (in milliseconds) to wait after stopping the child with SIGTERM, before using SIGKILL and after that before giving up.

childService.start() ⇒ Promise.<void>

Starts the service.

Kind: instance method of ChildService
Returns: Promise.<void> - resolves when the "readyRegex" has been found.

childService.stop() ⇒ Promise.<void>

Stop the service.

Kind: instance method of ChildService
Returns: Promise.<void> - resolves, when the executable has exited.

License

child-service is published under the MIT-license.

See LICENSE.md for details.

Release-Notes

For release notes, see CHANGELOG.md

Contributing guidelines

See CONTRIBUTING.md.

FAQs

Package last updated on 06 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc