-
Notifications
You must be signed in to change notification settings - Fork 15
/
carapace.js
executable file
·243 lines (208 loc) · 5.86 KB
/
carapace.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* carapace.js: Top-level include for the haibu-carapace module.
*
* (C) 2011 Nodejitsu Inc.
* MIT LICENCE
*
*/
var fs = require('fs'),
net = require('net'),
path = require('path'),
events = require('eventemitter2');
var carapace = module.exports = new events.EventEmitter2({
delimeter: '::',
wildcard: true
});
if (process.send) {
carapace.send = process.send;
delete process.send;
}
carapace.onAny(function (data) {
//
// Simple `EventEmitter` over `child_process.fork`.
//
if (carapace.send) {
carapace.send({ event: this.event, data: data });
}
});
//
// Cache the version of node running in case a user-space
// script attempts to overwrite it;
//
var nodeVersion = process.version;
//
// Require the `net` module for observing and overriding
// relevant events and functions in the core
// node.js `net` module
// Do the require prior to any chroot or chdir
// Only override once all plugins have been loaded
//
var overrideNet = require('./net').getOverride(nodeVersion);
//
// Expose the `cli` module for default options
// and liberal arguments parsing
//
carapace.cli = require('./cli');
carapace.bin = path.join(__dirname, '..', 'bin', 'carapace');
carapace.argv = [];
//
// Plugins list exposed through path names so that they
// can be later required by `carapace.plugin()` or `carapace:plugin` events.
//
carapace.plugins = {};
//
// Internal mapping of server instances to
// ports that have been mapped via `.listen()`.
//
carapace.servers = {};
//
// Internal state for managing various carapace operations:
// * carapace.running: Value indicating if the target script has started.
// * carapace.listening: Value indicating if the carapace `dnode` server has started.
//
carapace.running = false;
carapace.listening = false;
//
// Store a set of reserved ports for which the `net.js` implementation
// should actually throw an error, or not emit the `carapace::port` event.
//
carapace.ports = {
throw: [],
ignore: []
};
//
// ### function use (plugins, callback)
// #### @plugins {string|Array} List (or single) plugin add to the carapace.
// #### @callback {function} Continuation to respond to when complete.
// Enables the specified `plugins` in the carapace.
//
carapace.use = function (plugins, callback) {
if (!Array.isArray(plugins)) {
plugins = [plugins];
}
plugins.forEach(function (plugin) {
try {
//
// todo make this more flexible
// this requires absolute path or node_module
//
require(plugin)(carapace);
carapace.emit('plugin::loaded', {
id: carapace.id,
plugin: plugin
});
}
catch (ex) {
carapace.emit('plugin::error', {
id: carapace.id,
plugin: plugin,
error: ex
});
}
});
if (callback) {
callback();
}
return carapace;
};
//
// ### function run (script, argv, callback)
// #### @script {string} Path to the script to run inside the carapace.
// #### @argv {Array} Arguments to rewrite into process.argv
// #### @override {Boolean} When true remove current process.argv and replace
// with [script, argv]. If false remove all arguments that would be
// overwritten by the new [script, argv]
// #### @callback {function} Continuation to respond to when complete.
// Runs the script in `argv[0]` with the rest of the arguments specified
// in `argv` by transparently rewriting the current `process.argv`.
//
carapace.run = function (override, callback) {
var error;
Array.prototype.slice.call(arguments).forEach(function (a) {
switch (typeof(a)) {
case 'function': callback = a; break;
case 'boolean': override = a; break;
}
});
if (!carapace.script) {
error = new Error('Cannot spawn a script with no path.');
}
else if (carapace.running) {
error = new Error('Cannot spawn a new script, one is already running.');
}
if (error) {
return callback
? callback(error)
: carapace.emit('error', { id: carapace.id, error: error });
}
//
// Rewrite `process.argv` so that `Module.runMain()`
// will transparently locate and run the target script
// and it will be completely unaware of the carapace.
//
carapace.cli.rewrite(carapace.script, carapace.argv, override);
//
// Start up the hooks for the net module
//
overrideNet();
//
// Clear the module cache so anything required by `haibu-carapace`
// is reloaded as necessary.
//
require('module').Module._cache = {};
//
// Setup `carapace.wrapped` contain information about
// the wrapped script, then
//
carapace.wrapped = {
script: carapace.script,
argv: carapace.argv
};
process.nextTick(function () {
//
// Next tick to prevent a leak from arguments
//
require('module').Module.runMain();
carapace._module = process.mainModule;
carapace.running = true;
carapace.emit('running', {
id: carapace.id,
script: carapace.script,
argv: carapace.argv
});
if (callback) {
callback();
}
});
return carapace;
};
//
// ### function load (script, callback)
// #### @script {string} Path to the script to run inside the carapace
//
carapace.load = function (script) {
if (script[0] === '.') {
throw new Error('Cannot load relative paths into carapace. Provide an absolute path');
}
var name = path.basename(script, '.js');
if (carapace.plugins[name]) {
// if we already have the plugin,
return carapace.plugins[name];
}
if ('/' !== script[0]) {
//
// If it is not a relative or absolute path, make it absolute
// from the current `process.cwd()`.
//
script = path.join(process.cwd(), script);
}
carapace.plugins[name] = script;
carapace.emit('plugin::loaded', script);
return script;
};
//
// Setup the filenames for each of the default plugins
//
fs.readdirSync(path.join(__dirname, 'plugins')).forEach(function (name) {
carapace.load(path.join(__dirname, 'plugins', name));
});