felixge (owner)

Revisions

  • 824035 felixge Sun Nov 01 06:35:13 -0800 2009
  • 68dea8 felixge Sun Nov 01 06:27:45 -0800 2009
gist: 223540 Download_button fork
public
Public Clone URL: git://gist.github.com/223540.git
Embed All Files: show embed
0001-The-return-of-remote-module-loading.patch #
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
From 4ab67c1fb5cd60428e4469d34f3a308bd9228b14 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Felix=20Geisend=C3=B6rfer?= <felix@debuggable.com>
Date: Sun, 1 Nov 2009 15:34:46 +0100
Subject: [PATCH] The return of remote module loading
 
---
 src/node.js | 26 ++++++++++++-
 .../mjsunit/disabled/test-remote-module-loading.js | 17 ---------
 test/mjsunit/test-remote-module-loading.js | 38 ++++++++++++++++++++
 3 files changed, 62 insertions(+), 19 deletions(-)
 delete mode 100644 test/mjsunit/disabled/test-remote-module-loading.js
 create mode 100644 test/mjsunit/test-remote-module-loading.js
 
diff --git a/src/node.js b/src/node.js
index 90f03f8..79866b0 100644
--- a/src/node.js
+++ b/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]);
 }
 
diff --git a/test/mjsunit/disabled/test-remote-module-loading.js b/test/mjsunit/disabled/test-remote-module-loading.js
deleted file mode 100644
index 752ec8b..0000000
--- a/test/mjsunit/disabled/test-remote-module-loading.js
+++ /dev/null
@@ -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();
diff --git a/test/mjsunit/test-remote-module-loading.js b/test/mjsunit/test-remote-module-loading.js
new file mode 100644
index 0000000..7baecff
--- /dev/null
+++ b/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);
+});
\ No newline at end of file
--
1.6.3.3