This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
/
node.js
251 lines (210 loc) · 6.23 KB
/
node.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
node.tcp.createServer = function (on_connection, options) {
var server = new node.tcp.Server();
server.addListener("connection", on_connection);
//server.setOptions(options);
return server;
};
node.createProcess = function () {
throw "node.createProcess() has been changed to node.createChildProcess() update your code";
};
node.createChildProcess = function (command) {
var child = new node.ChildProcess();
child.spawn(command);
return child;
};
node.exec = function (command) {
var child = node.createChildProcess(command);
var stdout = "";
var stderr = "";
var promise = new node.Promise();
child.addListener("output", function (chunk) {
if (chunk) stdout += chunk;
});
child.addListener("error", function (chunk) {
if (chunk) stderr += chunk;
});
child.addListener("exit", function (code) {
if (code == 0) {
promise.emitSuccess(stdout, stderr);
} else {
promise.emitError(code, stdout, stderr);
}
});
return promise;
};
node.tcp.createConnection = function (port, host) {
var connection = new node.tcp.Connection();
connection.connect(port, host);
return connection;
};
// Timers
function setTimeout (callback, after) {
var timer = new node.Timer();
timer.addListener("timeout", callback);
timer.start(after, 0);
return timer;
}
function setInterval (callback, repeat) {
var timer = new node.Timer();
timer.addListener("timeout", callback);
timer.start(repeat, repeat);
return timer;
}
function clearTimeout (timer) {
timer.stop();
delete timer;
}
clearInterval = clearTimeout;
// Module
node.loadingModules = [];
function require_async (url) {
var currentModule = node.loadingModules[0];
return currentModule.newChild(url, {});
}
function require (url) {
return require_async(url).wait();
}
function include_async (url) {
var currentModule = node.loadingModules[0];
return currentModule.newChild(url, currentModule.target);
}
function include (url) {
include_async(url).wait();
}
node.Module = function (o) {
this.parent = o.parent;
this.target = o.target || {};
if (!o.path) throw "path argument required";
if (o.path.charAt(0) == "/") {
throw "Absolute module paths are not yet supported by Node";
}
if (o.path.match(/:\/\//)) {
this.filename = o.path;
} else {
var dir = o.base_directory || ".";
this.filename = node.path.join(dir, o.path);
}
this.loaded = false;
this.loadPromise = null;
this.exited = false;
this.children = [];
};
node.Module.prototype.load = function (callback) {
if (this.filename.match(/\.node$/)) {
return this.loadObject(callback);
} else {
return this.loadScript(callback);
}
};
node.Module.prototype.loadObject = function (callback) {
var self = this;
var loadPromise = new node.Promise();
self.loadPromise = loadPromise;
// XXX Not yet supporting loading from HTTP. would need to download the
// file, store it to tmp then run dlopen on it.
node.fs.exists(self.filename, function (does_exist) {
if (does_exist) {
self.loaded = true;
node.dlopen(self.filename, self.target); // FIXME synchronus
loadPromise.emitSuccess(self.target);
} else {
node.stdio.writeError("Error reading " + self.filename + "\n");
loadPromise.emitError();
node.exit(1);
}
});
return loadPromise;
};
node.Module.prototype.loadScript = function (callback) {
var self = this;
if (self.loaded) {
throw "Module '" + self.filename + "' is already loaded.";
}
var loadPromise = new node.Promise();
node.assert(self.loadPromise === null);
self.loadPromise = loadPromise;
var cat_promise = node.cat(self.filename);
cat_promise.addErrback(function () {
node.stdio.writeError("Error reading " + self.filename + "\n");
loadPromise.emitError();
node.exit(1);
});
cat_promise.addCallback(function (content) {
// remove shebang
content = content.replace(/^\#\!.*/, '');
// create wrapper function
var wrapper = "function (__filename) { "+
" var onLoad; "+
" var onExit; "+
" var exports = this; "+
content+
"\n"+
" this.__onLoad = onLoad;\n"+
" this.__onExit = onExit;\n"+
"};\n";
var compiled_wrapper = node.compile(wrapper, self.filename);
node.loadingModules.unshift(self);
compiled_wrapper.apply(self.target, [self.filename]);
node.loadingModules.shift();
self.onLoad = self.target.__onLoad;
self.onExit = self.target.__onExit;
if (self.onLoad || self.onExit) {
node.stdio.writeError( "(node) onLoad and onExit have been removed. "
+ " module load is synchronous so onLoad is unnecessary"
+ " Use process.addListener('exit') for onExit. "
);
node.exit(1);
}
self.waitChildrenLoad(function () {
self.loaded = true;
loadPromise.emitSuccess(self.target);
});
});
};
node.Module.prototype.newChild = function (path, target) {
var child = new node.Module({
target: target,
path: path,
base_directory: node.path.dirname(this.filename),
parent: this
});
this.children.push(child);
child.load();
return child.loadPromise;
};
node.Module.prototype.waitChildrenLoad = function (callback) {
var nloaded = 0;
var children = this.children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.loaded) {
nloaded++;
} else {
child.loadPromise.addCallback(function () {
nloaded++;
if (children.length == nloaded && callback) callback();
});
}
}
if (children.length == nloaded && callback) callback();
};
(function () {
// Make ARGV[0] and ARGV[1] into full paths.
if (ARGV[0].charAt(0) != "/") {
ARGV[0] = node.path.join(node.cwd(), ARGV[0]);
}
if (ARGV[1].charAt(0) != "/") {
ARGV[1] = node.path.join(node.cwd(), ARGV[1]);
}
// Load the root module--the command line argument.
var root_module = new node.Module({
path: node.path.filename(ARGV[1]),
base_directory: node.path.dirname(ARGV[1]),
target: this
});
root_module.load();
node.exit = function (code) {
process.emit("exit");
node.reallyExit(code);
};
}());