Permalink
Browse files

The return of remote module loading

  • Loading branch information...
1 parent 9bd2717 commit 8fd472b9a1199b5da1f038d9c30f3952698b99bc @felixge felixge committed with ry Nov 1, 2009
Showing with 62 additions and 19 deletions.
  1. +24 −2 src/node.js
  2. +0 −17 test/mjsunit/disabled/test-remote-module-loading.js
  3. +38 −0 test/mjsunit/test-remote-module-loading.js
View
@@ -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]);
}
@@ -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();
@@ -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);
+});

0 comments on commit 8fd472b

Please sign in to comment.