Run npm in a separate worker process

This commit is contained in:
Marcel Klehr 2013-12-27 16:02:54 +01:00
parent 44f817da01
commit b8870a7586
4 changed files with 47 additions and 3 deletions

View file

@ -1,6 +1,6 @@
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm");
var npm = require("./npm-master");
var npmIsLoaded = false;
var withNpm = function (npmfn) {

View file

@ -0,0 +1,19 @@
/**
* This module exposes the same methods as installer.js, but runs them in a separate child process
*/
var child_process = require('child_process')
, rpc = require('rpc-stream')
var worker = child_process.fork(__dirname+'/npm-worker', {silent:true})
var client = rpc()
worker.stdout.pipe(client).pipe(worker.stdin)
worker.stderr.pipe(process.stdout)
module.exports = {
commands: client.wrap(['install', 'uninstall', 'search'])
, load: function(opts, cb) {cb()}
, on: function(){}
}

View file

@ -0,0 +1,24 @@
/**
* This is a worker that should be run using child_process.fork (see installer-master.js for reference)
* Stdin and stdout are connected to an rpc server exposing the methods of installer.js
*/
var RPC = require('rpc-stream')
, npm = require('npm')
npm.load({}, function (er) {
if(er) throw er
})
console.log = function() {process.stderr.write(Array.prototype.slice(arguments).join('')+'\n')}
var server = RPC({
search: function(args, cb) {
npm.commands.search.apply(npm, Array.prototype.concat(args, [cb]))
}
, install: function(args, cb) {
npm.commands.install.apply(npm, Array.prototype.concat(args, [cb]))
}
, uninstall: function(args, cb) {
npm.commands.uninstall.apply(npm, Array.prototype.concat(args, [cb]))
}
})
process.stdin.pipe(server).pipe(process.stdout)