forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.js
More file actions
340 lines (280 loc) · 8.98 KB
/
node.js
File metadata and controls
340 lines (280 loc) · 8.98 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
node.tcp.createServer = function () {
throw new Error("node.tcp.createServer() has moved. Use require('/tcp.js') to access it.");
};
node.createProcess = function () {
throw "node.createProcess() has been changed to node.createChildProcess() update your code";
};
node.createChildProcess = function (file, args, env) {
var child = new node.ChildProcess();
args = args || [];
env = env || process.ENV;
var envPairs = [];
for (var key in env) {
if (env.hasOwnProperty(key)) {
envPairs.push(key + "=" + env[key]);
}
}
// TODO Note envPairs is not currently used in child_process.cc. The PATH
// needs to be searched for the 'file' command if 'file' does not contain
// a '/' character.
child.spawn(file, args, envPairs);
return child;
};
node.exec = function () {
throw new Error("node.exec() has moved. Use require('/utils.js') to bring it back.");
}
node.http.createServer = function () {
throw new Error("node.http.createServer() has moved. Use require('/http.js') to access it.");
}
node.http.createClient = function () {
throw new Error("node.http.createClient() has moved. Use require('/http.js') to access it.");
}
node.tcp.createConnection = function (port, host) {
throw new Error("node.tcp.createConnection() has moved. Use require('/tcp.js') to access it.");
};
include = function () {
throw new Error("include() has been removed. Use node.mixin(process, require(file)) to get the same effect.");
}
/* From jQuery.extend in the jQuery JavaScript Library v1.3.2
* Copyright (c) 2009 John Resig
* Dual licensed under the MIT and GPL licenses.
* http://docs.jquery.com/License
*/
node.mixin = function() {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !node.isFunction(target) )
target = {};
// mixin process itself if only one argument is passed
if ( length == i ) {
target = process;
--i;
}
for ( ; i < length; i++ )
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null )
// Extend the base object
for ( var name in options ) {
var src = target[ name ], copy = options[ name ];
// Prevent never-ending loop
if ( target === copy )
continue;
// Recurse if we're merging object values
if ( deep && copy && typeof copy === "object" && !copy.nodeType )
target[ name ] = node.mixin( deep,
// Never move original objects, clone them
src || ( copy.length != null ? [ ] : { } )
, copy );
// Don't bring in undefined values
else if ( copy !== undefined )
target[ name ] = copy;
}
// Return the modified object
return target;
};
// 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();
}
clearInterval = clearTimeout;
// Module
node.libraryPaths = [ node.path.join(ENV["HOME"], ".node_libraries")
, node.path.join(node.installPrefix, "lib/node/libraries")
, "/"
];
if (ENV["NODE_LIBRARY_PATHS"]) {
node.libraryPaths =
ENV["NODE_LIBRARY_PATHS"].split(":").concat(node.libraryPaths);
}
node.Module = function (filename, parent) {
node.assert(filename.charAt(0) == "/");
this.filename = filename;
this.exports = {};
this.parent = parent;
this.loaded = false;
this.loadPromise = null;
this.exited = false;
this.children = [];
};
node.Module.cache = {};
(function () {
function retrieveFromCache (loadPromise, fullPath, parent) {
var module;
if (fullPath in node.Module.cache) {
module = node.Module.cache[fullPath];
setTimeout(function () {
loadPromise.emitSuccess(module.exports);
}, 0);
} else {
module = new node.Module(fullPath, parent);
node.Module.cache[fullPath] = module;
module.load(loadPromise);
}
}
function findPath (path, dirs, callback) {
node.assert(path.charAt(0) == "/");
node.assert(dirs.constructor == Array);
if (dirs.length == 0) {
callback();
} else {
var dir = dirs[0];
var rest = dirs.slice(1, dirs.length);
var fullPath = node.path.join(dir, path);
node.fs.exists(fullPath, function (doesExist) {
if (doesExist) {
callback(fullPath);
} else {
findPath(path, rest, callback);
}
});
}
}
node.loadModule = function (requestedPath, exports, parent) {
var loadPromise = new node.Promise();
// On success copy the loaded properties into the exports
loadPromise.addCallback(function (t) {
for (var prop in t) {
if (t.hasOwnProperty(prop)) exports[prop] = t[prop];
}
});
loadPromise.addErrback(function (e) {
node.stdio.writeError(e.message + "\n");
process.exit(1);
});
if (!parent) {
// root module
node.assert(requestedPath.charAt(0) == "/");
retrieveFromCache(loadPromise, requestedPath);
} else {
if (requestedPath.charAt(0) == "/") {
// Need to find the module in node.libraryPaths
findPath(requestedPath, node.libraryPaths, function (fullPath) {
if (fullPath) {
retrieveFromCache(loadPromise, fullPath, parent);
} else {
loadPromise.emitError(new Error("Cannot find module '" + requestedPath + "'"));
}
});
} else {
// Relative file load
var fullPath = node.path.join(node.path.dirname(parent.filename),
requestedPath);
retrieveFromCache(loadPromise, fullPath, parent);
}
}
return loadPromise;
};
}());
node.Module.prototype.load = function (loadPromise) {
if (this.loaded) {
loadPromise.emitError(new Error("Module '" + self.filename + "' is already loaded."));
return;
}
node.assert(!node.loadPromise);
this.loadPromise = loadPromise;
if (this.filename.match(/\.node$/)) {
this.loadObject(loadPromise);
} else {
this.loadScript(loadPromise);
}
};
node.Module.prototype.loadObject = function (loadPromise) {
var self = this;
// 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.exports); // FIXME synchronus
loadPromise.emitSuccess(self.exports);
} else {
loadPromise.emitError(new Error("Error reading " + self.filename));
}
});
};
node.Module.prototype.loadScript = function (loadPromise) {
var self = this;
var catPromise = node.cat(self.filename);
catPromise.addErrback(function () {
loadPromise.emitError(new Error("Error reading " + self.filename));
});
catPromise.addCallback(function (content) {
// remove shebang
content = content.replace(/^\#\!.*/, '');
function requireAsync (url) {
return self.newChild(url);
}
function require (url) {
return requireAsync(url).wait();
}
require.async = requireAsync;
// create wrapper function
var wrapper = "var __wrap__ = function (__module, __filename, exports, require) { "
+ content
+ "\n}; __wrap__;";
var compiled_wrapper = node.compile(wrapper, self.filename);
compiled_wrapper.apply(self.exports, [self, self.filename, self.exports, require]);
self.waitChildrenLoad(function () {
self.loaded = true;
loadPromise.emitSuccess(self.exports);
});
});
};
node.Module.prototype.newChild = function (path) {
return node.loadModule(path, {}, this);
};
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();
};
process.exit = function (code) {
process.emit("exit");
node.reallyExit(code);
};
node.exit = function (code) {
throw new Error("node.exit() has been renamed to process.exit().");
};
(function () {
var cwd = node.cwd();
// Make ARGV[0] and ARGV[1] into full paths.
if (ARGV[0].charAt(0) != "/") {
ARGV[0] = node.path.join(cwd, ARGV[0]);
}
if (ARGV[1].charAt(0) != "/") {
ARGV[1] = node.path.join(cwd, ARGV[1]);
}
// Load the root module--the command line argument.
node.loadModule(ARGV[1], process);
}());