Coder Social home page Coder Social logo

subprocess.jsm's Introduction

I needed to get a list of running processes on X11 systems (Linux, Mac, etc) and Windows and came across this after a search on GitHub: [ochameau :: adbhelper - adb.js #L192 (https://github.com/ochameau/adbhelper/blob/fd3073a0f5f51e1707a5b4cbace82b13fb5a1fe5/adb.js#L192).

Over at, ochameau :: adhelper - Issues - subprocess for bootstrap, I heard you the subprocess.jsm module ships with Firefox. I tried to import it but it would not import because it was redeclaring constants. See the issue topic linked earlier on this line for more details.

So I came across a a readme on MXR: MXR :: Release - child_processes/README.md, which led me to the ipcode jsm modules. I copied those over and it imported great into bootstrap.js.

Here is the readme from MXR:

subprocess

Created by Jan Gerber, with contributions from Patrick Brunschwig, subprocess.jsm is used as the underlying library, supporting the Addon-SDK's implementation of Node's child-process API.

subprocess.jsm is originally from http://hg.mozilla.org/ipccode/.

How to Use subprocess.jsm in your Add-on

  1. copy subprocess.jsm and subprocess_worker_*.js into the modules/ directory of your add-on.
  2. add this line to chrome.manifest: resource EXTENSION modules/
  3. import it where needed: Components.utils.import("resource://EXTENSION/subprocess.jsm");

This object allows to start a process, and read/write data to/from it using stdin/stdout/stderr streams.

Usage example:

var p = subprocess.call({ command: '/bin/foo', arguments: ['-v', 'foo'], environment: [ "XYZ=abc", "MYVAR=def" ], charset: 'UTF-8', workdir: '/home/foo', //stdin: "some value to write to stdin\nfoobar", stdin: function(stdin) { stdin.write("some value to write to stdin\nfoobar"); stdin.close(); }, stdout: function(data) { dump("got data on stdout:" + data + "\n"); }, stderr: function(data) { dump("got data on stderr:" + data + "\n"); }, done: function(result) { dump("process terminated with " + result.exitCode + "\n"); }, mergeStderr: false });

p.wait(); // wait for the subprocess to terminate, // this will block the main thread, // only do if you can wait that long

Description of subprocess.call(...) Parameters

Apart from , all arguments are optional.

command: either a |nsIFile| object pointing to an executable file or a String containing the platform-dependent path to an executable file.

arguments: optional string array containing the arguments to the command.

environment: optional string array containing environment variables to pass to the command. The array elements must have the form "VAR=data". Please note that if environment is defined, it replaces any existing environment variables for the subprocess.

charset: Output is decoded with given charset and a string is returned. If charset is undefined, "UTF-8" is used as default. To get binary data, set this to null and the returned string is not decoded in any way.

workdir: Optional; either a |nsIFile| object or string containing the platform-dependent path to a directory to become the current working directory of the subprocess.

stdin: Optional input data for the process to be passed on standard input. stdin can either be a string or a function. A |string| gets written to stdin and stdin gets closed; A |function| gets passed an object with write and close function. Please note that the write() function will return almost immediately; data is always written asynchronously on a separate thread.

stdout: An optional function that can receive output data from the process. The stdout-function is called asynchronously; it can be called mutliple times during the execution of a process. At a minimum at each occurance of \n or \r. Please note that null-characters might need to be escaped with something like 'data.replace(/\0/g, "\0");'.

stderr: An optional function that can receive stderr data from the process. The stderr-function is called asynchronously; it can be called mutliple times during the execution of a process. Please note that null-characters might need to be escaped with something like 'data.replace(/\0/g, "\0");'. (on windows it only gets called once right now)

done: Optional function that is called when the process has terminated. The exit code from the process available via result.exitCode. If stdout is not defined, then the output from stdout is available via result.stdout. stderr data is in result.stderr

mergeStderr: Optional boolean value. If true, stderr is merged with stdout; no data will be provided to stderr.

Description of object returned by subprocess.call(...)

The object returned by subprocess.call offers a few methods that can be executed:

wait(): waits for the subprocess to terminate. It is not required to use wait; done will be called in any case when the subprocess terminated.

kill(): kill the subprocess. Any open pipes will be closed and done will be called.

Other methods exported by subprocess

The following functions help debugging and provide logging facilities.

registerDebugHandler(functionRef): register a handler that is called to get debugging information registerLogHandler(functionRef): register a handler that is called to get error messages

example:

subprocess.registerLogHandler( function(s) { dump(s); } );

subprocess.jsm's People

Contributors

noitidart avatar

Watchers

 avatar  avatar

Forkers

yajd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.