Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created December 19, 2009 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/260278 to your computer and use it in GitHub Desktop.
Save isaacs/260278 to your computer and use it in GitHub Desktop.
diff --git a/lib/uri.js b/lib/uri.js
index 45187e0..e44dd40 100644
--- a/lib/uri.js
+++ b/lib/uri.js
@@ -96,20 +96,7 @@ function uri_parse (url) {
}
/* normalize */
- var directories = [];
- for (var i = 0; i < items.directories.length; i++) {
- var directory = items.directories[i];
- if (directory == '.') {
- } else if (directory == '..') {
- if (directories.length && directories[directories.length - 1] != '..')
- directories.pop();
- else
- directories.push('..');
- } else {
- directories.push(directory);
- }
- }
- items.directories = directories;
+ items.directories = require("path").normalizeArray(items.directories);
items.domains = items.domain.split(".");
diff --git a/src/node.js b/src/node.js
index 500d7f4..da4640e 100644
--- a/src/node.js
+++ b/src/node.js
@@ -667,31 +667,29 @@ var posix = posixModule.exports;
var pathModule = createInternalModule("path", function (exports) {
exports.join = function () {
- var joined = "",
- dotre = /^\.\//,
- dotreplace = "",
- dotdotre = /(^|(\/)([^\/]+\/)?)\.\.\//g,
- dotdotreplace = ""
- for (var i = 0; i < arguments.length; i++) {
- var part = arguments[i].toString();
-
- /* Some logic to shorten paths */
- if (part === ".") continue;
- while (dotre.exec(part)) part = part.replace(dotre, dotreplace);
-
- if (i === 0) {
- part = part.replace(/\/*$/, "/");
- } else if (i === arguments.length - 1) {
- part = part.replace(/^\/*/, "");
+ return exports.normalize(Array.prototype.join.call(arguments, "/"));
+ };
+ exports.normalizeArray = function (parts) {
+ var directories = [];
+ for (var i = 0, l = parts.length; i < l; i++) {
+ var directory = parts[i];
+ if (directory === "." || (directory === "" && directories.length)) {
+ continue;
+ }
+ if (
+ directory === ".."
+ && directories.length
+ && directories[directories.length - 1] != '..'
+ ) {
+ directories.pop();
} else {
- part = part.replace(/^\/*/, "").replace(/\/*$/, "/");
+ directories.push(directory);
}
- joined += part;
}
- // replace /foo/../bar/baz with /bar/baz
- while (dotdotre.exec(joined)) joined = joined.replace(dotdotre, dotdotreplace);
- return joined;
-
+ return directories;
+ };
+ exports.normalize = function (path) {
+ return exports.normalizeArray(path.split("/")).join("/");
};
exports.dirname = function (path) {
@@ -898,7 +896,6 @@ Module.prototype.loadScript = function (filename, loadPromise) {
require.paths = process.paths;
require.async = requireAsync;
require.main = process.mainModule;
-
// create wrapper function
var wrapper = "var __wrap__ = function (exports, require, module, __filename) { "
+ content
@@ -958,6 +955,10 @@ process.mainModule = createModule(".");
var loadPromise = new process.Promise();
process.mainModule.load(process.ARGV[1], loadPromise);
+loadPromise.addErrback(function(e) {
+ throw e;
+});
+
// All our arguments are loaded. We've evaluated all of the scripts. We
// might even have created TCP servers. Now we enter the main eventloop. If
// there are no watchers on the loop (except for the ones that were
diff --git a/test/mjsunit/fixtures/cycles/circ-1.js b/test/mjsunit/fixtures/cycles/circ-1.js
new file mode 100644
index 0000000..9de9a83
--- /dev/null
+++ b/test/mjsunit/fixtures/cycles/circ-1.js
@@ -0,0 +1,8 @@
+var c2 = require("./circ-2");
+
+c2.foo = "bar";
+
+var c3 = require("./circ-3");
+
+require("sys").debug( c2.foo === c3.c2.foo );
+require("sys").debug( c2 === c3.c2 );
\ No newline at end of file
diff --git a/test/mjsunit/fixtures/cycles/circ-2.js b/test/mjsunit/fixtures/cycles/circ-2.js
new file mode 100644
index 0000000..0e4b42e
--- /dev/null
+++ b/test/mjsunit/fixtures/cycles/circ-2.js
@@ -0,0 +1,3 @@
+
+module.setExports({ foo : "baz" })
+
diff --git a/test/mjsunit/fixtures/cycles/circ-3.js b/test/mjsunit/fixtures/cycles/circ-3.js
new file mode 100644
index 0000000..c468cd5
--- /dev/null
+++ b/test/mjsunit/fixtures/cycles/circ-3.js
@@ -0,0 +1,5 @@
+function Foo () {};
+
+var c3 = exports = module.exports = new Foo();
+
+c3.c2 = require("./circ-2");
diff --git a/test/mjsunit/fixtures/cycles/folder/foo.js b/test/mjsunit/fixtures/cycles/folder/foo.js
new file mode 100644
index 0000000..3763a11
--- /dev/null
+++ b/test/mjsunit/fixtures/cycles/folder/foo.js
@@ -0,0 +1,6 @@
+
+var root = require("./../root");
+
+exports.hello = function () {
+ return root.calledFromFoo();
+};
\ No newline at end of file
diff --git a/test/mjsunit/fixtures/cycles/root.js b/test/mjsunit/fixtures/cycles/root.js
new file mode 100644
index 0000000..b515639
--- /dev/null
+++ b/test/mjsunit/fixtures/cycles/root.js
@@ -0,0 +1,10 @@
+
+var foo = exports.foo = require("./folder/foo");
+
+exports.hello = "hello";
+exports.sayHello = function () {
+ return foo.hello();
+};
+exports.calledFromFoo = function () {
+ return exports.hello;
+};
\ No newline at end of file
diff --git a/test/mjsunit/fixtures/nested-index/one/hello.js b/test/mjsunit/fixtures/nested-index/one/hello.js
new file mode 100644
index 0000000..e287275
--- /dev/null
+++ b/test/mjsunit/fixtures/nested-index/one/hello.js
@@ -0,0 +1,2 @@
+exports.hello = "hello from one!";
+
diff --git a/test/mjsunit/fixtures/nested-index/one/index.js b/test/mjsunit/fixtures/nested-index/one/index.js
new file mode 100644
index 0000000..d959a28
--- /dev/null
+++ b/test/mjsunit/fixtures/nested-index/one/index.js
@@ -0,0 +1 @@
+exports.hello = require('./hello').hello;
diff --git a/test/mjsunit/fixtures/nested-index/two/hello.js b/test/mjsunit/fixtures/nested-index/two/hello.js
new file mode 100644
index 0000000..fead2bd
--- /dev/null
+++ b/test/mjsunit/fixtures/nested-index/two/hello.js
@@ -0,0 +1,2 @@
+exports.hello = "hello from two!";
+
diff --git a/test/mjsunit/fixtures/nested-index/two/index.js b/test/mjsunit/fixtures/nested-index/two/index.js
new file mode 100644
index 0000000..d959a28
--- /dev/null
+++ b/test/mjsunit/fixtures/nested-index/two/index.js
@@ -0,0 +1 @@
+exports.hello = require('./hello').hello;
diff --git a/test/mjsunit/test-module-loading.js b/test/mjsunit/test-module-loading.js
index c6aea25..09b5807 100644
--- a/test/mjsunit/test-module-loading.js
+++ b/test/mjsunit/test-module-loading.js
@@ -33,6 +33,17 @@ assert.equal("D", d3.D());
assert.equal(true, d4.D instanceof Function);
assert.equal("D", d4.D());
+debug("test index.js modules ids and relative loading")
+var one = require("./fixtures/nested-index/one"),
+ two = require("./fixtures/nested-index/two");
+assert.notEqual(one.hello, two.hello);
+
+debug("test cycles containing a .. path");
+var root = require("./fixtures/cycles/root"),
+ foo = require("./fixtures/cycles/folder/foo");
+assert.equal(root.foo, foo);
+assert.equal(root.sayHello(), root.hello);
+
var errorThrown = false;
try {
require("./fixtures/throws_error");
diff --git a/test/mjsunit/test-readdir.js b/test/mjsunit/test-readdir.js
index 1b98f0f..16c46a2 100644
--- a/test/mjsunit/test-readdir.js
+++ b/test/mjsunit/test-readdir.js
@@ -7,7 +7,8 @@ puts("readdir " + fixturesDir);
promise.addCallback(function (files) {
p(files);
- assert.deepEqual(["a.js", "b", "multipart.js", "test_ca.pem",
+ assert.deepEqual(["a.js", "b","cycles", "multipart.js",
+ "nested-index","test_ca.pem",
"test_cert.pem", "test_key.pem", "throws_error.js", "x.txt"], files.sort());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment