Browse Source

Finished remote module loading

v0.7.4-release
Urban Hafner 16 years ago
parent
commit
ea290e727d
  1. 2
      src/http.js
  2. 6
      src/node.js
  3. 7
      test/test-http-cat.js
  4. 3
      test/test-node-cat.js
  5. 9
      test/test-remote-module-loading.js

2
src/http.js

@ -524,7 +524,7 @@ node.http.cat = function(url, encoding, callback) {
var uri = node.http.parseUri(url) var uri = node.http.parseUri(url)
var req = new node.http.Client(uri.port || 80, uri.host).get(uri.path || "/") var req = new node.http.Client(uri.port || 80, uri.host).get(uri.path || "/")
req.finish(function(res) { req.finish(function(res) {
var status = res.statusCode; var status = res.statusCode == 200 ? 0 : -1;
res.setBodyEncoding(encoding) res.setBodyEncoding(encoding)
var content = "" var content = ""
res.onBody = function(chunk) { res.onBody = function(chunk) {

6
src/node.js

@ -80,8 +80,12 @@ node.Module = function (o) {
if (o.path.charAt(0) == "/") if (o.path.charAt(0) == "/")
throw "Absolute module paths are not yet supported in Node"; throw "Absolute module paths are not yet supported in Node";
if (o.path.match(/:\/\//)) {
this.filename = o.path;
} else {
var dir = o.base_directory || "."; var dir = o.base_directory || ".";
this.filename = node.path.join(dir, o.path); this.filename = node.path.join(dir, o.path);
}
this.loaded = false; this.loaded = false;
this.exited = false; this.exited = false;
@ -93,7 +97,7 @@ node.Module.prototype.load = function (callback) {
if (self.loaded) if (self.loaded)
throw "Module '" + self.filename + "' is already 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) { if (status != 0) {
stderr.puts("Error reading " + self.filename); stderr.puts("Error reading " + self.filename);
node.exit(1); node.exit(1);

7
test/test-http-cat.js

@ -15,7 +15,12 @@ server.listen(PORT);
function onLoad() { function onLoad() {
node.http.cat("http://localhost:"+PORT, "utf8", function(status, content) { node.http.cat("http://localhost:"+PORT, "utf8", function(status, content) {
assertEquals(body, content); assertEquals(body, content);
assertEquals(200, status) assertEquals(0, status)
server.close() server.close()
}) })
node.http.cat("http://localhost:"+PORT+1, "utf8", function(status, content) {
assertEquals(-1, status)
assertEquals(nil, content)
})
} }

3
test/test-node-cat.js

@ -15,7 +15,7 @@ server.listen(PORT);
function onLoad() { function onLoad() {
node.cat("http://localhost:"+PORT, "utf8", function(status, content) { node.cat("http://localhost:"+PORT, "utf8", function(status, content) {
assertEquals(body, content); assertEquals(body, content);
assertEquals(200, status) assertEquals(0, status)
server.close() server.close()
}) })
@ -23,6 +23,7 @@ function onLoad() {
var fixtures = node.path.join(dirname, "fixtures"); var fixtures = node.path.join(dirname, "fixtures");
var x = node.path.join(fixtures, "x.txt"); var x = node.path.join(fixtures, "x.txt");
node.cat(x, "utf8", function(status, content) { node.cat(x, "utf8", function(status, content) {
assertEquals(0, status)
assertEquals("xyz", content.replace(/[\r\n]/, '')) assertEquals("xyz", content.replace(/[\r\n]/, ''))
}) })
} }

9
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';}"; var body = "exports.A = function() { return 'A';}";
res.sendHeader(200, [ res.sendHeader(200, [
["Content-Length", body.length], ["Content-Length", body.length],
@ -6,7 +6,8 @@ new node.http.Server(function (req, res) {
]); ]);
res.sendBody(body); res.sendBody(body);
res.finish(); res.finish();
}).listen(8000); });
s.listen(8000);
include("mjsunit.js"); include("mjsunit.js");
var a = require("http://localhost:8000/") var a = require("http://localhost:8000/")
@ -14,7 +15,5 @@ var a = require("http://localhost:8000/")
function onLoad() { function onLoad() {
assertInstanceof(a.A, Function); assertInstanceof(a.A, Function);
assertEquals("A", a.A()); assertEquals("A", a.A());
s.close();
} }
// TODO: Add tests for remote require using a timeout
Loading…
Cancel
Save