/
node_apps.js
261 lines (250 loc) · 7.16 KB
/
node_apps.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*******************************************************************************
* Copyright (c) 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*global exports module process require*/
/*jslint devel:true forin:true*/
var child_process = require('child_process');
var events = require('events'), EventEmitter = events.EventEmitter;
var path = require('path');
var util = require('util');
var url = require('url');
var api = require('./api');
var fileUtil = require('./fileUtil');
var PATH_TO_NODE = process.execPath;
var PATH_TO_NODE_INSPECTOR = 'node-inspector/bin/inspector';
var INSPECT_PORT = 8900;
/**
* @name orionode.App
* @class
*/
/**
* @name orionode.App#exit
* @event
*/
/**
* @name orionode.App#stdout
* @event
*/
/**
* @name orionode.App#stderr
* @event
*/
var App = function(pid, proc, hidden) {
EventEmitter.call(this);
this.pid = pid;
this.process = proc;
this.hidden = typeof hidden === 'undefined' ? false : !!hidden;
proc.on('exit', this.emit.bind(this, 'exit'));
proc.stdout.on('data', this.emit.bind(this, 'stdout'));
proc.stderr.on('data', this.emit.bind(this, 'stderr'));
};
util.inherits(App, EventEmitter);
/** @returns {Object} */
App.prototype.toJson = function() {
var json = {
Id: this.pid
};
var app = this;
this.decorators.forEach(function(decorator) {
decorator.call(null, app, json);
});
return json;
};
App.prototype.decorators = [
function debugDecorator(app, json) {
if (app.debug) {
json.DebugURL = app.debug;
}
}
];
/**
* @name orionode.AppTable
* @class
*/
var AppTable = function() {
this.map = Object.create(null);
};
AppTable.prototype = /** @lends orionode.AppTable.prototype */ {
/** @returns {orionode.App} */
get: function(/**String*/ pid) {
return this.map[pid] || null;
},
put: function(/**String*/ pid, /**orionode.App*/ app) {
if (this.map[pid]) {
throw new Error('pid in use: ' + pid);
}
this.map[pid] = app;
},
/** @returns {orionode.App} */
remove: function(/**String*/ pid) {
if (!Object.prototype.hasOwnProperty.call(this.map, pid)) {
return false;
}
var app = this.map[pid];
delete this.map[pid];
return app;
},
/** @returns {orionode.App[]} */
apps: function(includeHidden) {
var map = this.map;
var values = [];
for (var prop in map) {
var app = map[prop];
if (!app.hidden || includeHidden) {
values.push(map[prop]);
}
}
return values;
}
};
function spawnNode(args, options) {
args = Array.isArray(args) ? args : null;
options = options || {};
var child = child_process.spawn(PATH_TO_NODE, args, options);
child.on('error', function(e) {
console.log('PID ' + child.pid + ' error: ' + e);
});
return child;
}
/**
* @name orionode.AppContext
* @class
*/
var AppContext = function(options) {
// function checkArgs(args) {
// if (args && !Array.isArray(args)) {
// throw new Error('Parameter "args" must be an array, or omitted');
// }
// return true;
// }
/** @throws {Error} If modulePath is unsafe */
function resolveModulePath(fileRoot, workspaceDir, modulePath) {
var filePath = api.rest(fileRoot, modulePath);
return fileUtil.safeFilePath(workspaceDir, filePath);
}
var fileRoot = options.fileRoot, workspaceDir = options.workspaceDir;
if (!fileRoot || !workspaceDir) {
throw new Error('Missing fileRoot or workspaceDir');
}
var inspector = null;
/**
* @name orionode.AppContext#appTable
* @type orionode.AppTable
*/
var appTable = new AppTable();
Object.defineProperty(this, 'appTable', {
get: function() { return appTable; }
});
/**
* @param {String[]} [args]
* @param {String} cwd
* @param {Boolean} [hidden=false]
*/
var _startApp = function(args, cwd, hidden) {
hidden = typeof hidden === 'boolean' ? hidden : false;
var app;
try {
var proc = spawnNode(args, {cwd: cwd});
app = new App(proc.pid, proc, hidden);
appTable.put(proc.pid, app);
app.on('exit', function() {
appTable.remove(proc.pid);
});
return app;
} catch (e) {
console.log(e.stack || e);
this.stopApp(app);
throw e;
}
};
/**
* @function
* @private
*/
var _startInspectorApp = function(args, cwd, headers, requestUrl, restartOnExit){
var app;
try {
app = _startApp(args, cwd, true /*hidden*/);
var parsedRequestUrl = url.parse(requestUrl);
app.inspect = url.format({
protocol: parsedRequestUrl.protocol,
host: headers.host,
pathname: parsedRequestUrl.pathname + 'node-inspector/' + app.pid
});
app.on('exit', function() {
console.log('Exit inspector, PID: ' + app.pid);
if(restartOnExit){
_startInspectorApp(args, cwd, headers, requestUrl, restartOnExit);
}
});
console.log('Started inspector, PID: ' + app.pid);
return app;
} catch (e) {
console.log(e.stack || e);
this.stopApp(app);
return null;
}
}.bind(this);
/**
* @name orionode.AppContext#startApp
* @function
* @param {String} modulePath The Orionode-filesystem path (ie. starting with '/file') of the module to execute
* @param {Array} [args]
* @param {Boolean} [hidden]
*/
this.startApp = function(modulePath, args, hidden) {
modulePath = resolveModulePath(fileRoot, workspaceDir, modulePath);
// TODO cwd should be passed in, not assumed to be the module's parent folder.
var app = _startApp([modulePath].concat(args || []), path.dirname(modulePath), hidden);
app.on('exit', function(code) {
console.log('App # ' + app.pid + ' exited, code=' + code);
});
return app;
};
/**
* @name orion.AppContext#debugApp
* @param {String} modulePath
* @param {Number} port
*/
this.debugApp = function(modulePath, port, headers, requestUrl) {
var resolvedPath = resolveModulePath(fileRoot, workspaceDir, modulePath);
// TODO cwd should be passed in, not assumed to be the module's parent folder.
var app = _startApp(["--debug-brk=" + port, resolvedPath], path.dirname(resolvedPath));
var parsedRequestUrl = url.parse(requestUrl);
app.debug = url.format({
protocol: parsedRequestUrl.protocol,
// TODO this is bizarre. Can we host node-inspector on the same port as Orionode?
hostname: url.parse('http://' + headers.host).hostname,
port: INSPECT_PORT,
pathname: url.resolve(parsedRequestUrl.pathname, '../../debug?port=' + port)
});
//Lazy spawn the node inspector procees for the first time when user wants to debug an app.
if(app && !inspector) {
var inspectorPath = require.resolve(PATH_TO_NODE_INSPECTOR);
inspector = _startInspectorApp([inspectorPath, "--web-port=" + INSPECT_PORT], path.dirname(inspectorPath), headers, requestUrl, true);
}
return app;
}.bind(this);
/**
* @name orionode.AppContext#stopApp
* @function
* @param {orionode.App} app
*/
this.stopApp = function(app) {
if (app) {
appTable.remove(app.pid);
app.process.kill();
return app;
}
return null;
};
};
exports.AppContext = AppContext;
exports.App = App;