From ea290e727de256a51f0a4ef9e4ff701da88f975c Mon Sep 17 00:00:00 2001 From: Urban Hafner Date: Sun, 21 Jun 2009 16:59:11 +0200 Subject: [PATCH] Finished remote module loading --- src/http.js | 2 +- src/node.js | 10 +++++++--- test/test-http-cat.js | 7 ++++++- test/test-node-cat.js | 3 ++- test/test-remote-module-loading.js | 9 ++++----- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/http.js b/src/http.js index 1bb126e67c5..1f1fe2efc61 100644 --- a/src/http.js +++ b/src/http.js @@ -524,7 +524,7 @@ node.http.cat = function(url, encoding, callback) { var uri = node.http.parseUri(url) var req = new node.http.Client(uri.port || 80, uri.host).get(uri.path || "/") req.finish(function(res) { - var status = res.statusCode; + var status = res.statusCode == 200 ? 0 : -1; res.setBodyEncoding(encoding) var content = "" res.onBody = function(chunk) { diff --git a/src/node.js b/src/node.js index f2965783a33..904f334ecf2 100644 --- a/src/node.js +++ b/src/node.js @@ -80,8 +80,12 @@ node.Module = function (o) { if (o.path.charAt(0) == "/") throw "Absolute module paths are not yet supported in Node"; - var dir = o.base_directory || "."; - this.filename = node.path.join(dir, o.path); + 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.exited = false; @@ -93,7 +97,7 @@ node.Module.prototype.load = function (callback) { if (self.loaded) throw "Module '" + self.filename + "' is already loaded."; - node.fs.cat(self.filename, "utf8", function (status, content) { + node.cat(self.filename, "utf8", function (status, content) { if (status != 0) { stderr.puts("Error reading " + self.filename); node.exit(1); diff --git a/test/test-http-cat.js b/test/test-http-cat.js index 7632ac2692a..c2d439c21a1 100644 --- a/test/test-http-cat.js +++ b/test/test-http-cat.js @@ -15,7 +15,12 @@ server.listen(PORT); function onLoad() { node.http.cat("http://localhost:"+PORT, "utf8", function(status, content) { assertEquals(body, content); - assertEquals(200, status) + assertEquals(0, status) server.close() }) + + node.http.cat("http://localhost:"+PORT+1, "utf8", function(status, content) { + assertEquals(-1, status) + assertEquals(nil, content) + }) } diff --git a/test/test-node-cat.js b/test/test-node-cat.js index d00a366d957..207356ecde0 100644 --- a/test/test-node-cat.js +++ b/test/test-node-cat.js @@ -15,7 +15,7 @@ server.listen(PORT); function onLoad() { node.cat("http://localhost:"+PORT, "utf8", function(status, content) { assertEquals(body, content); - assertEquals(200, status) + assertEquals(0, status) server.close() }) @@ -23,6 +23,7 @@ function onLoad() { var fixtures = node.path.join(dirname, "fixtures"); var x = node.path.join(fixtures, "x.txt"); node.cat(x, "utf8", function(status, content) { + assertEquals(0, status) assertEquals("xyz", content.replace(/[\r\n]/, '')) }) } diff --git a/test/test-remote-module-loading.js b/test/test-remote-module-loading.js index b71a9389c04..f658c917f46 100644 --- a/test/test-remote-module-loading.js +++ b/test/test-remote-module-loading.js @@ -1,4 +1,4 @@ -new node.http.Server(function (req, res) { +var s = new node.http.Server(function (req, res) { var body = "exports.A = function() { return 'A';}"; res.sendHeader(200, [ ["Content-Length", body.length], @@ -6,7 +6,8 @@ new node.http.Server(function (req, res) { ]); res.sendBody(body); res.finish(); -}).listen(8000); +}); +s.listen(8000); include("mjsunit.js"); var a = require("http://localhost:8000/") @@ -14,7 +15,5 @@ var a = require("http://localhost:8000/") function onLoad() { assertInstanceof(a.A, Function); assertEquals("A", a.A()); - + s.close(); } - -// TODO: Add tests for remote require using a timeout \ No newline at end of file