c9-core/node_modules/vfs-child/parent.js

52 wiersze
1.6 KiB
JavaScript

var Consumer = require('vfs-socket/consumer').Consumer;
var inherits = require('util').inherits;
var spawn = require('child_process').spawn;
exports.Parent = Parent;
function Parent(fsOptions) {
Consumer.call(this);
var options = {};
if (fsOptions.hasOwnProperty("gid")) {
options.gid = fsOptions.gid;
delete fsOptions.gid;
}
if (fsOptions.hasOwnProperty("uid")) {
options.uid = fsOptions.uid;
delete fsOptions.uid;
}
options.customFds = [-1, -1, 2];
var args = [require.resolve('./child.js'), JSON.stringify(fsOptions)];
var executablePath = process.execPath;
var child;
// Override Consumer's connect since the transport logic is internal to this module
this.connect = connect.bind(this);
function connect(callback) {
child = spawn(executablePath, args, options);
child.stdin.readable = true;
Consumer.prototype.connect.call(this, [child.stdout, child.stdin], callback);
child.stdin.resume();
child.on("exit", disconnect);
}
// Override Consumer's disconnect to kill the child process afterwards
this.disconnect = disconnect.bind(this);
function disconnect() {
if (!this.transport) return;
Consumer.prototype.disconnect.apply(this, arguments);
child.kill();
}
if (fsOptions.inProcess) {
this.connect = function(callback) {
var vfs = require('vfs-local')(fsOptions);
setImmediate(function(){
callback(null, vfs);
});
};
}
}
inherits(Parent, Consumer);