child-io

0.4.3 • Public • Published

child-io

Rank
Version Build status

Create child process for safe execution of dangerous(e.g. user-made) function.

Install

To install the most recent release from npm, run:

	npm i child-io

Usage

	const io = require('child-io').IO([forkArgs, [options]]);
	io.exec(func | file, [execOpts]);
  • forkArgs <Array>
    process arguments used when child_process is .fork() ed.

  • options <Object>

    • wkdir: [<String>]
    • modules: [<Array>]
  • func <Function>|<String> | file <String>
    executing original function.
    eval() or require() should be success to extract item.

  • execOpts <Object>

    • timeout: [<Number>] (alias: limit, Default = 5_000ms) max time to finish executing (millisecond).

    • reuse : [<Function>|<Any>]
      Judge whether kill or not after execute the process.

    • replace: [<Object>] (Default = { }) String replacement for using variable in executing function.

    • escape : [<Function>|] (Default = true) Escape the replaced string not to fail eval(). when truely value is given, the function below is used.

      function defaultEscaper(s) { return s.replace(/['"]/g, '\"').replace(/[\r\n]/g, '\n'); }

    • args : [<Array>] (Default = [ ])
      given arguments when the function is executed.
      the data send function "sender" will be put to.

  • Event

    • data
    • end
    • timeout
    • error

simple return

	const io = require('child-io').IO();
	io.on('data', function(ret){
	  console.log('Result: ' + ret); // "Result: true"
	}).on('end', function(msec){
	  console.log('Killed child_process(sync). Execute time is: ' + msec + ' ms');
	}).exec(function(){ return true; });

use asynchronous callback

	const io = require('child-io').IO();
	io.on('data', function(ret){
	  console.log('Result: ' + ret); // "Result: false"
	}).on('end', function(msec){
	  console.log('Killed child_process(async). Execute time is: ' + msec + ' ms');
	}).exec(function(sender){ process.nextTick(function(){ sender(false) }); });

See child-io_test.js for a sample to recognize more contract usage. You could run the code by npm run sample.

Note

FakeChild class is used automatically when v8debug is tied up. In that case, message Currently, typeof v8debug is NOT undefined. is shown.

Change Log

  • 2024/1/26

    • 0.4.3 release
    • add option "wkdir" and "modules"
  • 2024/1/26

    • 0.4.1 release
    • emit "error" event on timeout
    • new option: reuse
  • 2014/6/26

    • 0.3.3 release
    • option "escape" is included and the default is true
  • 2013/5/24

    • 0.3.1 release
    • add FakeChild class for --debug environment
  • 2013/4/16

    • 0.2.0 release
    • add option key 'args'
    • add "timeout" event
    • add wrap function for asynchronous use
    • add more explanation for each arguments,
    • add list of Event
  • 2013/4/15

    • 0.1.3 release
    • npm release

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i child-io

    Weekly Downloads

    15

    Version

    0.4.3

    License

    MIT

    Unpacked Size

    28.3 kB

    Total Files

    11

    Last publish

    Collaborators

    • ystskm