Home

Awesome

core_d

Build Status SemVer License

Offload your heavy lifting to a daemon. Extracted from eslint_d.

Install

This will install the core_d as a dependency:

❯ npm install core_d

Usage

You need to create a main file that controls the daemon and a service.js file which will run in the background.

The main file should look something like this:

const cmd = process.argv[2];

process.env.CORE_D_TITLE = 'your_d';
process.env.CORE_D_DOTFILE = '.your_d';
process.env.CORE_D_SERVICE = require.resolve('./your-service');
// optional to get logging from the child process
process.env.CORE_D_DEBUG = 'true';

const core_d = require('core_d');

if (cmd === 'start'
  || cmd === 'stop'
  || cmd === 'restart'
  || cmd === 'status') {
  core_d[cmd]();
  return;
}

core_d.invoke(process.argv.slice(2));

The service.js file must expose an invoke function like this:

/*
 * The core_d service entry point.
 */
exports.invoke = function (cwd, args, text, callback) {
  callback(null, 'Your response');
};

How does this work?

The first time you call core_d.invoke(...), a little server is started in the background and bound to a random port. The port number is stored along with a security token in the configured dotfile. Your services invoke method is called with the same arguments. Later calls to invoke will be executed on the same instance. So if you have a large app that takes a long time to load, but otherwise responds quickly, and you're using it frequently, like linting a file, then core_d can give your tool a performance boost.

API

The core_d client exposes these functions:

Environment variables:

Your service must implement a function with the signature invoke(cwd, args, text, callback). The passed arguments are:

The service can optionally implement a getStatus() function to return additional status information when calling core_d.status().

Moar speed

If you're really into performance and want the lowest possible latency, talk to the core_d server with netcat. This will also eliminate the node.js startup time on the client side.

❯ PORT=`cat ~/.core_d | cut -d" " -f1`
❯ TOKEN=`cat ~/.core_d | cut -d" " -f2`
❯ echo "$TOKEN $PWD file.js" | nc localhost $PORT

Or if you want to work with stdin:

❯ echo "$TOKEN $PWD --stdin" | cat - file.js | nc localhost $PORT

Compatibility

License

MIT