Browse Source

The return of remote module loading

v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
8fd472b9a1
  1. 26
      src/node.js
  2. 17
      test/mjsunit/disabled/test-remote-module-loading.js
  3. 38
      test/mjsunit/test-remote-module-loading.js

26
src/node.js

@ -288,6 +288,11 @@ if (process.ENV["NODE_PATH"]) {
function findModulePath (id, dirs, callback) {
process.assert(dirs.constructor == Array);
if (/^https?:\/\//.exec(id)) {
callback(id);
return;
}
if (/.(js|node)$/.exec(id)) {
throw new Error("No longer accepting filename extension in module names");
}
@ -402,7 +407,24 @@ Module.prototype.loadObject = function (filename, loadPromise) {
Module.prototype.loadScript = function (filename, loadPromise) {
var self = this;
var catPromise = process.cat(filename);
if (filename.match(/^http:\/\//)) {
var catPromise = new process.Promise();
loadModule('http', this)
.addCallback(function(http) {
http.cat(filename)
.addCallback(function(content) {
catPromise.emitSuccess(content);
})
.addErrback(function() {
catPromise.emitError.apply(null, arguments);
});
})
.addErrback(function() {
loadPromise.emitError(new Error("could not load core module \"http\""));
});
} else {
var catPromise = process.cat(filename);
}
catPromise.addErrback(function () {
loadPromise.emitError(new Error("Error reading " + filename));
@ -468,7 +490,7 @@ if (process.ARGV[0].charAt(0) != "/") {
process.ARGV[0] = path.join(cwd, process.ARGV[0]);
}
if (process.ARGV[1].charAt(0) != "/") {
if (process.ARGV[1].charAt(0) != "/" && !/^http:\/\//.exec(process.ARGV[1])) {
process.ARGV[1] = path.join(cwd, process.ARGV[1]);
}

17
test/mjsunit/disabled/test-remote-module-loading.js

@ -1,17 +0,0 @@
var s = process.http.createServer(function (req, res) {
var body = "exports.A = function() { return 'A';}";
res.sendHeader(200, [
["Content-Length", body.length],
["Content-Type", "text/plain"]
]);
res.sendBody(body);
res.finish();
});
s.listen(8000);
process.mixin(require("../common.js"));
var a = require("http://localhost:8000/")
assertInstanceof(a.A, Function);
assertEquals("A", a.A());
s.close();

38
test/mjsunit/test-remote-module-loading.js

@ -0,0 +1,38 @@
process.mixin(require("./common"));
var PORT = 8889;
var http = require('http');
var sys = require('sys');
var modulesLoaded = 0;
var server = http.createServer(function(req, res) {
var body = 'exports.httpPath = function() {'+
'return '+JSON.stringify(req.uri.path)+';'+
'};';
res.sendHeader(200, {'Content-Type': 'text/javascript'});
res.sendBody(body);
res.finish();
});
server.listen(PORT);
var httpModule = require('http://localhost:'+PORT+'/moduleA.js');
assertEquals('/moduleA.js', httpModule.httpPath());
modulesLoaded++;
var nodeBinary = process.ARGV[0];
var cmd = nodeBinary+' http://localhost:'+PORT+'/moduleB.js';
sys
.exec(cmd)
.addCallback(function() {
modulesLoaded++;
server.close();
})
.addErrback(function() {
assertUnreachable('node binary could not load module from url');
});
process.addListener('exit', function() {
assertEquals(2, modulesLoaded);
});
Loading…
Cancel
Save