This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
71 lines (57 loc) · 1.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var spawn = require('child_process').spawn,
ipc = require('./lib/server'),
debug = require('debug')('xpcshell'),
ControlServer = require('./lib/control_server'),
ImapStack = require('./lib/imap_stack');
var PORT_ENV = 'FAKESERVER_IPC_PORT';
function setupDebug(child) {
function watchProcess(type) {
child[type].on('data', function(buffer) {
debug(type, buffer.toString());
});
}
if (process.env.DEBUG) {
watchProcess('stderr');
watchProcess('stdout');
}
}
/**
* Spawns a xpcshell target instance.
*
* @private
* @param {Number} port of where server is running.
* @return {ChildProcess} child process instance of xpcshell.
*/
function spawnXpcshell(port) {
var spawnOpts = {};
// port over the env's of the parent process
var envs = spawnOpts.env = {};
for (var key in process.env) {
envs[key] = process.env[key];
}
// tell xpcshell where to look.
spawnOpts.env[PORT_ENV] = port;
var bin = __dirname + '/xpcom/bin/server.sh';
debug('spawn', bin);
return spawn(
bin,
[__dirname + '/xpcom/bin/server.js'],
spawnOpts
);
}
function create(callback) {
ipc.create(function(err, ipcInterface) {
if (err) return callback(err);
var xpcshell = spawnXpcshell(ipcInterface.port);
setupDebug(xpcshell);
var controlServer = new ControlServer(xpcshell, ipcInterface);
// wait until xpcshell connects
ipcInterface.once('ready', function() {
// don't fire the callback until we have the port
controlServer.setupControlPort(function(err) {
callback(err, controlServer);
});
});
});
}
module.exports.create = create;