-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-runner.js
More file actions
139 lines (112 loc) · 4.15 KB
/
node-runner.js
File metadata and controls
139 lines (112 loc) · 4.15 KB
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
"use strict";
var util = require("util");
var c9util = require("../cloud9.core/util");
var ShellRunner = require("../cloud9.run.shell/shell").Runner;
/**
* Run node scripts with restricted user rights
*/
var exports = module.exports = function(url, listenHint, vfs, pm, sandbox, usePort, nodePath, callback) {
sandbox.getProjectDir(function(err, projectDir) {
if (err) return callback(err);
init(projectDir, url);
});
function init(projectDir, url) {
pm.addRunner("node", exports.factory(vfs, sandbox, projectDir, url, listenHint, nodePath, usePort));
callback();
}
};
exports.factory = function(vfs, sandbox, root, url, listenHint, nodePath, usePort) {
return function(args, eventEmitter, eventName, callback) {
var options = {};
c9util.extend(options, args);
options.root = root;
options.file = args.file;
options.args = args.args;
options.cwd = args.cwd;
options.env = args.env;
options.nodePath = args.nodePath || nodePath || process.execPath;
options.nodeVersion = args.nodeVersion;
options.encoding = args.encoding;
options.eventEmitter = eventEmitter;
options.eventName = eventName;
options.url = url;
options.usePort = usePort;
options.listenHint = listenHint;
options.sandbox = sandbox;
new Runner(vfs, options, callback);
};
};
var Runner = exports.Runner = function(vfs, options, callback) {
var self = this;
if (!options.sandbox) {
return callback("No sandbox specified");
}
self.vfs = vfs;
self.root = options.root;
self.nodeVersion = options.nodeVersion || "auto";
self.file = options.file || "";
options.env = options.env || {};
self.scriptArgs = options.args || [];
self.nodeArgs = [];
if (options.uid) {
self.nodeArgs.push("--setuid=" + options.uid);
}
// first we need to get an open port
options.sandbox.getPort(function (err, port) {
if (err) {
return console.error("getPort failed");
}
// the port flag is only present in the precompiled binaries that we provide
// in the hosted version, so only add it then
if (options.usePort) {
self.nodeArgs.push("--ports=" + port);
}
// then create a url.
// this can be passed in as an option, or we can construct it
// based on the host and the port
if (!options.url) {
options.sandbox.getHost(function(err, host) {
if (err) return console.error(err);
var url = "http://" + host + ":" + port;
startProcess(url, port);
});
}
else {
startProcess(options.url, port);
}
});
function startProcess (url, port) {
self.port = port;
if (self.port) {
options.env.PORT = self.port;
}
if (options.usePort) {
self.nodeArgs.push("--ports=" + self.port);
}
// a nice debug message for our users when we fire up the process
var debugMessageListener = function (msg) {
// process dies? then we die as well
if (msg.type === "node-exit") {
return options.eventEmitter.removeListener(options.eventName, debugMessageListener);
}
if (msg.type === "node-start") {
var info = [
"Tip: you can access long running processes, like a server, at '" + url + "'.",
options.listenHint
];
options.eventEmitter.emit(options.eventName, {
type: "node-debug-data",
stream: "stdout",
data: info.join("\n"),
extra: null,
pid: msg.pid
});
}
};
options.eventEmitter.on(options.eventName, debugMessageListener);
options.cwd = options.cwd ? options.cwd : options.root;
options.command = options.nodePath || process.execPath;
ShellRunner.call(self, vfs, options, callback);
}
};
util.inherits(Runner, ShellRunner);