sproutit-narwhal 0.1.106
Sign up to get free protection for your applications and to get access to all the features.
- data/DISTRIBUTION.yml +15 -0
- data/README.md +86 -0
- data/Rakefile +349 -0
- data/VERSION.yml +7 -0
- data/bin/activate +50 -0
- data/bin/activate.bash +50 -0
- data/bin/activate.cmd +3 -0
- data/bin/js +67 -0
- data/bin/json +2 -0
- data/bin/narwhal +67 -0
- data/bin/narwhal.cmd +29 -0
- data/bin/sea +45 -0
- data/bin/sea.cmd +25 -0
- data/bin/tusk +2 -0
- data/bin/tusk.cmd +5 -0
- data/catalog.json +902 -0
- data/docs/available-packages.md +32 -0
- data/docs/browser-api-plan.md +290 -0
- data/docs/browser-api.md +153 -0
- data/docs/download.md +25 -0
- data/docs/engines.md +32 -0
- data/docs/json-tool.md +121 -0
- data/docs/lib/binary.wiki +242 -0
- data/docs/lib/file.wiki +325 -0
- data/docs/lib/os/popen.md +70 -0
- data/docs/modules.md +38 -0
- data/docs/narwhal.md +487 -0
- data/docs/packages-howto.md +32 -0
- data/docs/packages.md +30 -0
- data/docs/posts/2009-07-29-hello-0.1.md +19 -0
- data/docs/quick-start.md +69 -0
- data/docs/sea.md +49 -0
- data/engines/browser/lib/binary.js +2 -0
- data/engines/browser/lib/reactor.js +21 -0
- data/engines/browser/lib/system.js +3 -0
- data/engines/default/lib/array.js +164 -0
- data/engines/default/lib/binary-engine.js +53 -0
- data/engines/default/lib/binary.js +755 -0
- data/engines/default/lib/date.js +8 -0
- data/engines/default/lib/file-engine.js +119 -0
- data/engines/default/lib/function.js +119 -0
- data/engines/default/lib/global.js +11 -0
- data/engines/default/lib/io-engine.js +26 -0
- data/engines/default/lib/json.js +488 -0
- data/engines/default/lib/object.js +69 -0
- data/engines/default/lib/os-engine.js +3 -0
- data/engines/default/lib/reactor.js +12 -0
- data/engines/default/lib/string.js +84 -0
- data/engines/default/lib/system.js +20 -0
- data/engines/default/lib/worker.js +133 -0
- data/engines/jsc/README.md +18 -0
- data/engines/jsc/bootstrap.js +53 -0
- data/engines/jsc/deps/http-parser/LICENSE +77 -0
- data/engines/jsc/deps/http-parser/README.md +145 -0
- data/engines/jsc/deps/http-parser/http_parser.c +6087 -0
- data/engines/jsc/deps/http-parser/http_parser.h +141 -0
- data/engines/jsc/deps/http-parser/http_parser.rl +500 -0
- data/engines/jsc/deps/http-parser/test.c +858 -0
- data/engines/jsc/include/binary-engine.h +11 -0
- data/engines/jsc/include/io-engine.h +23 -0
- data/engines/jsc/include/narwhal.h +427 -0
- data/engines/jsc/lib/file-engine.js +31 -0
- data/engines/jsc/lib/http.js +1 -0
- data/engines/jsc/lib/io-engine.js +202 -0
- data/engines/jsc/lib/os-engine.js +25 -0
- data/engines/jsc/lib/system.js +18 -0
- data/engines/jsc/lib/zip.js +1 -0
- data/engines/jsc/narwhal-jsc.c +273 -0
- data/engines/jsc/narwhal.c +29 -0
- data/engines/jsc/package.json +8 -0
- data/engines/jsc/src/binary-engine.cc +290 -0
- data/engines/jsc/src/file-engine.cc +405 -0
- data/engines/jsc/src/io-engine.cc +423 -0
- data/engines/jsc/src/jack/handler/jill.cc +710 -0
- data/engines/jsc/src/os-engine.cc +210 -0
- data/engines/rhino/bin/narwhal-rhino +68 -0
- data/engines/rhino/bin/narwhal-rhino.cmd +34 -0
- data/engines/rhino/bootstrap.js +119 -0
- data/engines/rhino/jars/jline.jar +0 -0
- data/engines/rhino/jars/jna.jar +0 -0
- data/engines/rhino/jars/js.jar +0 -0
- data/engines/rhino/lib/binary-engine.js +83 -0
- data/engines/rhino/lib/concurrency.js +6 -0
- data/engines/rhino/lib/event-queue.js +18 -0
- data/engines/rhino/lib/file-engine.js +216 -0
- data/engines/rhino/lib/http-client-engine.js +90 -0
- data/engines/rhino/lib/http-engine.js +10 -0
- data/engines/rhino/lib/io-engine.js +347 -0
- data/engines/rhino/lib/md5-engine.js +40 -0
- data/engines/rhino/lib/os-engine.js +150 -0
- data/engines/rhino/lib/packages-engine.js +71 -0
- data/engines/rhino/lib/sandbox-engine.js +70 -0
- data/engines/rhino/lib/system.js +38 -0
- data/engines/rhino/lib/worker-engine.js +23 -0
- data/engines/rhino/lib/zip.js +78 -0
- data/engines/rhino/package.json +4 -0
- data/engines/secure/lib/file.js +6 -0
- data/engines/secure/lib/system.js +6 -0
- data/engines/template/bin/narwhal-engine-name +32 -0
- data/engines/template/bootstrap.js +40 -0
- data/engines/template/lib/file-engine.js +118 -0
- data/engines/template/lib/system.js +17 -0
- data/examples/browser-deployment-jackconfig.js +35 -0
- data/examples/fibonacci-worker.js +35 -0
- data/examples/fibonacci.js +19 -0
- data/examples/hello +2 -0
- data/examples/narwhal +3 -0
- data/examples/not-quite-a-quine.js +1 -0
- data/extconf.rb +44 -0
- data/gem_bin/narwhal +5 -0
- data/gem_bin/sea +4 -0
- data/gem_bin/tusk +4 -0
- data/lib/args.js +849 -0
- data/lib/base16.js +16 -0
- data/lib/base64.js +120 -0
- data/lib/codec/base64.js +8 -0
- data/lib/crc32.js +60 -0
- data/lib/file-bootstrap.js +187 -0
- data/lib/file.js +659 -0
- data/lib/hash.js +28 -0
- data/lib/hashp.js +65 -0
- data/lib/html.js +16 -0
- data/lib/http-client.js +134 -0
- data/lib/http.js +17 -0
- data/lib/io.js +98 -0
- data/lib/jsmin.js +315 -0
- data/lib/jsonpath.js +89 -0
- data/lib/logger.js +55 -0
- data/lib/md4.js +146 -0
- data/lib/md5.js +164 -0
- data/lib/mime.js +166 -0
- data/lib/narwhal.js +102 -0
- data/lib/narwhal/client.js +261 -0
- data/lib/narwhal/compile.js +99 -0
- data/lib/narwhal/env.js +140 -0
- data/lib/narwhal/inline.js +106 -0
- data/lib/narwhal/json.js +324 -0
- data/lib/narwhal/json.md +178 -0
- data/lib/narwhal/repl.js +96 -0
- data/lib/narwhal/server-test.js +6 -0
- data/lib/narwhal/server.js +270 -0
- data/lib/narwhal/tusk.js +170 -0
- data/lib/narwhal/tusk/bin.js +13 -0
- data/lib/narwhal/tusk/bundle.js +0 -0
- data/lib/narwhal/tusk/catalog.js +22 -0
- data/lib/narwhal/tusk/clone.js +66 -0
- data/lib/narwhal/tusk/consolidate.js +25 -0
- data/lib/narwhal/tusk/create-catalog.js +80 -0
- data/lib/narwhal/tusk/engine.js +42 -0
- data/lib/narwhal/tusk/freeze.js +0 -0
- data/lib/narwhal/tusk/init.js +56 -0
- data/lib/narwhal/tusk/install.js +288 -0
- data/lib/narwhal/tusk/list.js +20 -0
- data/lib/narwhal/tusk/orphans.js +0 -0
- data/lib/narwhal/tusk/reheat.js +15 -0
- data/lib/narwhal/tusk/remove.js +15 -0
- data/lib/narwhal/tusk/search.js +145 -0
- data/lib/narwhal/tusk/update.js +21 -0
- data/lib/narwhal/tusk/upgrade.js +0 -0
- data/lib/os.js +33 -0
- data/lib/packages.js +423 -0
- data/lib/printf.js +169 -0
- data/lib/promise.js +352 -0
- data/lib/querystring.js +176 -0
- data/lib/ref-send.js +257 -0
- data/lib/regexp.js +12 -0
- data/lib/sandbox.js +422 -0
- data/lib/sha.js +112 -0
- data/lib/sha256.js +102 -0
- data/lib/struct.js +228 -0
- data/lib/term.js +179 -0
- data/lib/test/assert.js +95 -0
- data/lib/test/equiv.js +188 -0
- data/lib/test/jsdump.js +165 -0
- data/lib/test/runner.js +129 -0
- data/lib/unload.js +13 -0
- data/lib/uri.js +378 -0
- data/lib/url.js +5 -0
- data/lib/utf8.js +64 -0
- data/lib/util.js +985 -0
- data/lib/uuid.js +89 -0
- data/lib/xregexp.js +521 -0
- data/local.json.template +1 -0
- data/narwhal.gemspec +105 -0
- data/narwhal.js +213 -0
- data/package.json +26 -0
- data/packages/readline/engines/default/lib/readline.js +4 -0
- data/packages/readline/engines/rhino/lib/readline.js +6 -0
- data/packages/readline/package.json +5 -0
- data/sources.json +207 -0
- data/tests/all-tests.js +17 -0
- data/tests/args.js +31 -0
- data/tests/args/domain.js +215 -0
- data/tests/args/options.js +36 -0
- data/tests/args/shifting.js +92 -0
- data/tests/args/validation.js +31 -0
- data/tests/base64.js +23 -0
- data/tests/commonjs.js +3 -0
- data/tests/commonjs/all-tests.js +12 -0
- data/tests/commonjs/bytearray-encodings-tests.js +69 -0
- data/tests/commonjs/bytearray-tests.js +465 -0
- data/tests/commonjs/bytestring-encodings-tests.js +89 -0
- data/tests/commonjs/bytestring-tests.js +263 -0
- data/tests/commonjs/es5/all-tests.js +3 -0
- data/tests/commonjs/es5/bind.js +29 -0
- data/tests/commonjs/file-tests.js +315 -0
- data/tests/commonjs/file/dirname.js +31 -0
- data/tests/commonjs/file/extension.js +45 -0
- data/tests/commonjs/file/is-absolute.js +11 -0
- data/tests/commonjs/file/iterator.js +101 -0
- data/tests/commonjs/file/normal.js +27 -0
- data/tests/commonjs/file/path.js +17 -0
- data/tests/commonjs/file/relative.js +42 -0
- data/tests/commonjs/file/resolve.js +44 -0
- data/tests/commonjs/module-tests.js +9 -0
- data/tests/commonjs/modules/absolute/b.js +1 -0
- data/tests/commonjs/modules/absolute/program.js +5 -0
- data/tests/commonjs/modules/absolute/submodule/a.js +3 -0
- data/tests/commonjs/modules/absolute/test.js +9 -0
- data/tests/commonjs/modules/all-tests.js +47 -0
- data/tests/commonjs/modules/config.js +11 -0
- data/tests/commonjs/modules/cyclic/a.js +4 -0
- data/tests/commonjs/modules/cyclic/b.js +4 -0
- data/tests/commonjs/modules/cyclic/program.js +10 -0
- data/tests/commonjs/modules/cyclic/test.js +9 -0
- data/tests/commonjs/modules/determinism/program.js +3 -0
- data/tests/commonjs/modules/determinism/submodule/a.js +8 -0
- data/tests/commonjs/modules/determinism/submodule/b.js +2 -0
- data/tests/commonjs/modules/determinism/test.js +9 -0
- data/tests/commonjs/modules/exactExports/a.js +3 -0
- data/tests/commonjs/modules/exactExports/program.js +4 -0
- data/tests/commonjs/modules/exactExports/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/hasOwnProperty.js +0 -0
- data/tests/commonjs/modules/hasOwnProperty/program.js +3 -0
- data/tests/commonjs/modules/hasOwnProperty/test.js +9 -0
- data/tests/commonjs/modules/hasOwnProperty/toString.js +0 -0
- data/tests/commonjs/modules/method/a.js +12 -0
- data/tests/commonjs/modules/method/program.js +8 -0
- data/tests/commonjs/modules/method/test.js +9 -0
- data/tests/commonjs/modules/missing/program.js +8 -0
- data/tests/commonjs/modules/missing/test.js +9 -0
- data/tests/commonjs/modules/monkeys/a.js +1 -0
- data/tests/commonjs/modules/monkeys/program.js +4 -0
- data/tests/commonjs/modules/monkeys/test.js +9 -0
- data/tests/commonjs/modules/nested/a/b/c/d.js +3 -0
- data/tests/commonjs/modules/nested/program.js +3 -0
- data/tests/commonjs/modules/nested/test.js +9 -0
- data/tests/commonjs/modules/relative/program.js +5 -0
- data/tests/commonjs/modules/relative/submodule/a.js +1 -0
- data/tests/commonjs/modules/relative/submodule/b.js +2 -0
- data/tests/commonjs/modules/relative/test.js +9 -0
- data/tests/commonjs/modules/transitive/a.js +1 -0
- data/tests/commonjs/modules/transitive/b.js +1 -0
- data/tests/commonjs/modules/transitive/c.js +3 -0
- data/tests/commonjs/modules/transitive/program.js +3 -0
- data/tests/commonjs/modules/transitive/test.js +9 -0
- data/tests/file/all-tests.js +61 -0
- data/tests/file/fnmatch.js +102 -0
- data/tests/file/glob.js +466 -0
- data/tests/file/match.js +102 -0
- data/tests/global.js +6 -0
- data/tests/global/array.js +19 -0
- data/tests/hashes.js +94 -0
- data/tests/html.js +13 -0
- data/tests/io/stringio.js +21 -0
- data/tests/os/all-tests.js +4 -0
- data/tests/os/popen.js +41 -0
- data/tests/os/system.js +22 -0
- data/tests/printf.js +123 -0
- data/tests/query-string.js +87 -0
- data/tests/sandbox/byte-io.js +20 -0
- data/tests/sandbox/fileName.js +3 -0
- data/tests/sandbox/foo.js +0 -0
- data/tests/sandbox/reload.js +79 -0
- data/tests/string.js +35 -0
- data/tests/uri.js +41 -0
- data/tests/util/all-tests.js +79 -0
- data/tests/util/array.js +207 -0
- data/tests/util/array/is-arguments.js +29 -0
- data/tests/util/array/is-array-like.js +29 -0
- data/tests/util/case.js +9 -0
- data/tests/util/collection.js +104 -0
- data/tests/util/eq.js +57 -0
- data/tests/util/expand.js +45 -0
- data/tests/util/object.js +125 -0
- data/tests/util/operator.js +25 -0
- data/tests/util/range.js +19 -0
- data/tests/util/repr.js +26 -0
- data/tests/util/string.js +34 -0
- data/tests/util/unique.js +12 -0
- metadata +434 -0
data/lib/test/runner.js
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
var system = require('system');
|
2
|
+
var file = require('file');
|
3
|
+
var util = require('util');
|
4
|
+
var assert = require("./assert");
|
5
|
+
|
6
|
+
var stream = require('term').stream;
|
7
|
+
|
8
|
+
var args = require('args');
|
9
|
+
var parser = exports.parser = new args.Parser();
|
10
|
+
parser.option('--no-color', 'color').def(true).set(false);
|
11
|
+
parser.option('--loop', 'loop').def(false).set(true);
|
12
|
+
parser.option('--stacktrace', 'showStacktraces').def(false).set(true);
|
13
|
+
|
14
|
+
function getBacktrace(e) {
|
15
|
+
if (!e) {
|
16
|
+
return "";
|
17
|
+
}
|
18
|
+
else if (e.rhinoException) {
|
19
|
+
var s = new Packages.java.io.StringWriter();
|
20
|
+
e.rhinoException.printStackTrace(new Packages.java.io.PrintWriter(s));
|
21
|
+
return String(s.toString());
|
22
|
+
}
|
23
|
+
else if (e.javaException) {
|
24
|
+
var s = new Packages.java.io.StringWriter();
|
25
|
+
e.javaException.printStackTrace(new Packages.java.io.PrintWriter(s));
|
26
|
+
return String(s.toString());
|
27
|
+
}
|
28
|
+
return "";
|
29
|
+
}
|
30
|
+
|
31
|
+
exports.run = function(objectOrModule) {
|
32
|
+
var options = parser.parse([module.path].concat(system.args));
|
33
|
+
if (options.color == false)
|
34
|
+
stream.disable();
|
35
|
+
if (!objectOrModule) {
|
36
|
+
var id = file.canonical(options.args.shift());
|
37
|
+
objectOrModule = require(id);
|
38
|
+
}
|
39
|
+
|
40
|
+
do {
|
41
|
+
var result = _run(objectOrModule, options);
|
42
|
+
} while (options.loop);
|
43
|
+
|
44
|
+
return result;
|
45
|
+
}
|
46
|
+
|
47
|
+
var _run = function(objectOrModule, options, context) {
|
48
|
+
|
49
|
+
if (typeof objectOrModule === "string")
|
50
|
+
objectOrModule = require(objectOrModule);
|
51
|
+
|
52
|
+
if (!objectOrModule)
|
53
|
+
throw "Nothing to run";
|
54
|
+
|
55
|
+
var localContext = context || { passed : 0, failed : 0, error : 0, depth : 0 };
|
56
|
+
localContext.depth++;
|
57
|
+
|
58
|
+
for (var spaces=""; spaces.length < localContext.depth * 2; spaces += " ");
|
59
|
+
|
60
|
+
for (var property in objectOrModule) {
|
61
|
+
if (property.match(/^test/)) {
|
62
|
+
print(spaces + "+ Running "+property);
|
63
|
+
if (typeof objectOrModule[property] == "function") {
|
64
|
+
if (typeof objectOrModule.setup === "function")
|
65
|
+
objectOrModule.setup();
|
66
|
+
|
67
|
+
var globals = {};
|
68
|
+
for (var name in system.global) {
|
69
|
+
globals[name] = true;
|
70
|
+
}
|
71
|
+
|
72
|
+
try {
|
73
|
+
try {
|
74
|
+
objectOrModule[property]();
|
75
|
+
} finally {
|
76
|
+
if (!objectOrModule.addsGlobals) {
|
77
|
+
for (var name in system.global) {
|
78
|
+
if (!globals[name]) {
|
79
|
+
delete system.global[name];
|
80
|
+
throw new assert.AssertionError("New global introduced: " + util.enquote(name));
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
localContext.passed++;
|
87
|
+
} catch (e) {
|
88
|
+
if (e.name === "AssertionError") {
|
89
|
+
var backtrace = getBacktrace(e);
|
90
|
+
var message = "Assertion failed in "+property+":";
|
91
|
+
|
92
|
+
stream.print("\0violet("+message+"\0)");
|
93
|
+
stream.print("\0yellow("+e+"\0)");
|
94
|
+
if (options.showStacktraces && backtrace)
|
95
|
+
stream.print("\0blue("+backtrace+"\0)");
|
96
|
+
|
97
|
+
localContext.failed++;
|
98
|
+
} else {
|
99
|
+
var backtrace = getBacktrace(e);
|
100
|
+
var message = "Exception in "+property+":";
|
101
|
+
|
102
|
+
stream.print("\0violet("+message+"\0)");
|
103
|
+
stream.print("\0red("+e+"\0)");
|
104
|
+
if (backtrace)
|
105
|
+
stream.print("\0blue("+backtrace+"\0)");
|
106
|
+
|
107
|
+
localContext.error++;
|
108
|
+
}
|
109
|
+
} finally {
|
110
|
+
if (typeof objectOrModule.teardown === "function")
|
111
|
+
objectOrModule.teardown();
|
112
|
+
}
|
113
|
+
} else {
|
114
|
+
_run(objectOrModule[property], options, localContext);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
localContext.depth--;
|
120
|
+
|
121
|
+
if (context === undefined)
|
122
|
+
print("Passed "+localContext.passed+"; Failed "+localContext.failed+"; Error "+localContext.error+";");
|
123
|
+
|
124
|
+
return localContext.failed + localContext.error;
|
125
|
+
};
|
126
|
+
|
127
|
+
if (require.main == module.id)
|
128
|
+
exports.run();
|
129
|
+
|
data/lib/unload.js
ADDED
data/lib/uri.js
ADDED
@@ -0,0 +1,378 @@
|
|
1
|
+
// original code: http://code.google.com/p/js-uri/
|
2
|
+
|
3
|
+
// Based on the regex in RFC2396 Appendix B.
|
4
|
+
var URI_RE = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Uniform Resource Identifier (URI) - RFC3986
|
8
|
+
*/
|
9
|
+
var URI = exports.URI = function(str) {
|
10
|
+
if (!str) str = "";
|
11
|
+
var result = str.match(URI_RE);
|
12
|
+
this.scheme = result[1] || null;
|
13
|
+
this.authority = result[2] || null;
|
14
|
+
this.path = result[3] || null;
|
15
|
+
this.query = result[4] || null;
|
16
|
+
this.fragment = result[5] || null;
|
17
|
+
}
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Convert the URI to a String.
|
21
|
+
*/
|
22
|
+
URI.prototype.toString = function () {
|
23
|
+
var str = "";
|
24
|
+
|
25
|
+
if (this.scheme)
|
26
|
+
str += this.scheme + ":";
|
27
|
+
|
28
|
+
if (this.authority)
|
29
|
+
str += "//" + this.authority;
|
30
|
+
|
31
|
+
if (this.path)
|
32
|
+
str += this.path;
|
33
|
+
|
34
|
+
if (this.query)
|
35
|
+
str += "?" + this.query;
|
36
|
+
|
37
|
+
if (this.fragment)
|
38
|
+
str += "#" + this.fragment;
|
39
|
+
|
40
|
+
return str;
|
41
|
+
}
|
42
|
+
|
43
|
+
exports.parse = URI.parse = function(uri) {
|
44
|
+
return new URI(uri);
|
45
|
+
}
|
46
|
+
|
47
|
+
exports.unescape = URI.unescape = function(str, plus) {
|
48
|
+
return decodeURI(str).replace(/\+/g, " ");
|
49
|
+
}
|
50
|
+
|
51
|
+
exports.unescapeComponent = URI.unescapeComponent = function(str, plus) {
|
52
|
+
return decodeURIComponent(str).replace(/\+/g, " ");
|
53
|
+
}
|
54
|
+
|
55
|
+
// from Chiron's HTTP module:
|
56
|
+
|
57
|
+
/**** keys
|
58
|
+
members of a parsed URI object.
|
59
|
+
*/
|
60
|
+
exports.keys = [
|
61
|
+
"url",
|
62
|
+
"protocol",
|
63
|
+
"authorityRoot",
|
64
|
+
"authority",
|
65
|
+
"userInfo",
|
66
|
+
"user",
|
67
|
+
"password",
|
68
|
+
"domain",
|
69
|
+
"domains",
|
70
|
+
"port",
|
71
|
+
"path",
|
72
|
+
"root",
|
73
|
+
"directory",
|
74
|
+
"directories",
|
75
|
+
"file",
|
76
|
+
"query",
|
77
|
+
"anchor"
|
78
|
+
];
|
79
|
+
|
80
|
+
/**** expressionKeys
|
81
|
+
members of a parsed URI object that you get
|
82
|
+
from evaluting the strict regular expression.
|
83
|
+
*/
|
84
|
+
exports.expressionKeys = [
|
85
|
+
"url",
|
86
|
+
"protocol",
|
87
|
+
"authorityRoot",
|
88
|
+
"authority",
|
89
|
+
"userInfo",
|
90
|
+
"user",
|
91
|
+
"password",
|
92
|
+
"domain",
|
93
|
+
"port",
|
94
|
+
"path",
|
95
|
+
"root",
|
96
|
+
"directory",
|
97
|
+
"file",
|
98
|
+
"query",
|
99
|
+
"anchor"
|
100
|
+
];
|
101
|
+
|
102
|
+
/**** strictExpression
|
103
|
+
*/
|
104
|
+
exports.strictExpression = new RegExp( /* url */
|
105
|
+
"^" +
|
106
|
+
"(?:" +
|
107
|
+
"([^:/?#]+):" + /* protocol */
|
108
|
+
")?" +
|
109
|
+
"(?:" +
|
110
|
+
"(//)" + /* authorityRoot */
|
111
|
+
"(" + /* authority */
|
112
|
+
"(?:" +
|
113
|
+
"(" + /* userInfo */
|
114
|
+
"([^:@]*)" + /* user */
|
115
|
+
":?" +
|
116
|
+
"([^:@]*)" + /* password */
|
117
|
+
")?" +
|
118
|
+
"@" +
|
119
|
+
")?" +
|
120
|
+
"([^:/?#]*)" + /* domain */
|
121
|
+
"(?::(\\d*))?" + /* port */
|
122
|
+
")" +
|
123
|
+
")?" +
|
124
|
+
"(" + /* path */
|
125
|
+
"(/?)" + /* root */
|
126
|
+
"((?:[^?#/]*/)*)" +
|
127
|
+
"([^?#]*)" + /* file */
|
128
|
+
")" +
|
129
|
+
"(?:\\?([^#]*))?" + /* query */
|
130
|
+
"(?:#(.*))?" /*anchor */
|
131
|
+
);
|
132
|
+
|
133
|
+
/**** Parser
|
134
|
+
returns a URI parser function given
|
135
|
+
a regular expression that renders
|
136
|
+
`expressionKeys` and returns an `Object`
|
137
|
+
mapping all `keys` to values.
|
138
|
+
*/
|
139
|
+
exports.Parser = function (expression) {
|
140
|
+
return function (url) {
|
141
|
+
if (typeof url == "undefined")
|
142
|
+
throw new Error("HttpError: URL is undefined");
|
143
|
+
if (typeof url != "string") return new Object(url);
|
144
|
+
|
145
|
+
var items = {};
|
146
|
+
var parts = expression.exec(url);
|
147
|
+
|
148
|
+
for (var i = 0; i < parts.length; i++) {
|
149
|
+
items[exports.expressionKeys[i]] = parts[i] ? parts[i] : "";
|
150
|
+
}
|
151
|
+
|
152
|
+
items.root = (items.root || items.authorityRoot) ? '/' : '';
|
153
|
+
|
154
|
+
items.directories = items.directory.split("/");
|
155
|
+
if (items.directories[items.directories.length - 1] == "") {
|
156
|
+
items.directories.pop();
|
157
|
+
}
|
158
|
+
|
159
|
+
/* normalize */
|
160
|
+
var directories = [];
|
161
|
+
for (var i = 0; i < items.directories.length; i++) {
|
162
|
+
var directory = items.directories[i];
|
163
|
+
if (directory == '.') {
|
164
|
+
} else if (directory == '..') {
|
165
|
+
if (directories.length && directories[directories.length - 1] != '..')
|
166
|
+
directories.pop();
|
167
|
+
else
|
168
|
+
directories.push('..');
|
169
|
+
} else {
|
170
|
+
directories.push(directory);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
items.directories = directories;
|
174
|
+
|
175
|
+
items.domains = items.domain.split(".");
|
176
|
+
|
177
|
+
return items;
|
178
|
+
};
|
179
|
+
};
|
180
|
+
|
181
|
+
/**** parse
|
182
|
+
a strict URI parser.
|
183
|
+
*/
|
184
|
+
exports.parse = exports.Parser(exports.strictExpression);
|
185
|
+
|
186
|
+
/**** format
|
187
|
+
accepts a parsed URI object and returns
|
188
|
+
the corresponding string.
|
189
|
+
*/
|
190
|
+
exports.format = function (object) {
|
191
|
+
if (typeof(object) == 'undefined')
|
192
|
+
throw new Error("UrlError: URL undefined for urls#format");
|
193
|
+
if (object instanceof String || typeof(object) == 'string')
|
194
|
+
return object;
|
195
|
+
var domain =
|
196
|
+
object.domains ?
|
197
|
+
object.domains.join(".") :
|
198
|
+
object.domain;
|
199
|
+
var userInfo = (
|
200
|
+
object.user ||
|
201
|
+
object.password
|
202
|
+
) ?
|
203
|
+
(
|
204
|
+
(object.user || "") +
|
205
|
+
(object.password ? ":" + object.password : "")
|
206
|
+
) :
|
207
|
+
object.userInfo;
|
208
|
+
var authority = (
|
209
|
+
userInfo ||
|
210
|
+
domain ||
|
211
|
+
object.port
|
212
|
+
) ? (
|
213
|
+
(userInfo ? userInfo + "@" : "") +
|
214
|
+
(domain || "") +
|
215
|
+
(object.port ? ":" + object.port : "")
|
216
|
+
) :
|
217
|
+
object.authority;
|
218
|
+
var directory =
|
219
|
+
object.directories ?
|
220
|
+
object.directories.join("/") :
|
221
|
+
object.directory;
|
222
|
+
var path =
|
223
|
+
directory || object.file ?
|
224
|
+
(
|
225
|
+
(directory ? directory + "/" : "") +
|
226
|
+
(object.file || "")
|
227
|
+
) :
|
228
|
+
object.path;
|
229
|
+
return (
|
230
|
+
(object.protocol ? object.protocol + ":" : "") +
|
231
|
+
(authority ? "//" + authority : "") +
|
232
|
+
(object.root || (authority && path) ? "/" : "") +
|
233
|
+
(path ? path : "") +
|
234
|
+
(object.query ? "?" + object.query : "") +
|
235
|
+
(object.anchor ? "#" + object.anchor : "")
|
236
|
+
) || object.url || "";
|
237
|
+
};
|
238
|
+
|
239
|
+
/**** resolveObject
|
240
|
+
returns an object representing a URL resolved from
|
241
|
+
a relative location and a source location.
|
242
|
+
*/
|
243
|
+
exports.resolveObject = function (source, relative) {
|
244
|
+
if (!source)
|
245
|
+
return relative;
|
246
|
+
|
247
|
+
source = exports.parse(source);
|
248
|
+
relative = exports.parse(relative);
|
249
|
+
|
250
|
+
if (relative.url == "")
|
251
|
+
return source;
|
252
|
+
|
253
|
+
delete source.url;
|
254
|
+
delete source.authority;
|
255
|
+
delete source.domain;
|
256
|
+
delete source.userInfo;
|
257
|
+
delete source.path;
|
258
|
+
delete source.directory;
|
259
|
+
|
260
|
+
if (
|
261
|
+
relative.protocol && relative.protocol != source.protocol ||
|
262
|
+
relative.authority && relative.authority != source.authority
|
263
|
+
) {
|
264
|
+
source = relative;
|
265
|
+
} else {
|
266
|
+
if (relative.root) {
|
267
|
+
source.directories = relative.directories;
|
268
|
+
} else {
|
269
|
+
|
270
|
+
var directories = relative.directories;
|
271
|
+
for (var i = 0; i < directories.length; i++) {
|
272
|
+
var directory = directories[i];
|
273
|
+
if (directory == ".") {
|
274
|
+
} else if (directory == "..") {
|
275
|
+
if (source.directories.length) {
|
276
|
+
source.directories.pop();
|
277
|
+
} else {
|
278
|
+
source.directories.push('..');
|
279
|
+
}
|
280
|
+
} else {
|
281
|
+
source.directories.push(directory);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
285
|
+
if (relative.file == ".") {
|
286
|
+
relative.file = "";
|
287
|
+
} else if (relative.file == "..") {
|
288
|
+
source.directories.pop();
|
289
|
+
relative.file = "";
|
290
|
+
}
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
if (relative.root)
|
295
|
+
source.root = relative.root;
|
296
|
+
if (relative.protcol)
|
297
|
+
source.protocol = relative.protocol;
|
298
|
+
if (!(!relative.path && relative.anchor))
|
299
|
+
source.file = relative.file;
|
300
|
+
source.query = relative.query;
|
301
|
+
source.anchor = relative.anchor;
|
302
|
+
|
303
|
+
return source;
|
304
|
+
};
|
305
|
+
|
306
|
+
/**** relativeObject
|
307
|
+
returns an object representing a relative URL to
|
308
|
+
a given target URL from a source URL.
|
309
|
+
*/
|
310
|
+
exports.relativeObject = function (source, target) {
|
311
|
+
target = exports.parse(target);
|
312
|
+
source = exports.parse(source);
|
313
|
+
|
314
|
+
delete target.url;
|
315
|
+
|
316
|
+
if (
|
317
|
+
target.protocol == source.protocol &&
|
318
|
+
target.authority == source.authority
|
319
|
+
) {
|
320
|
+
delete target.protocol;
|
321
|
+
delete target.authority;
|
322
|
+
delete target.userInfo;
|
323
|
+
delete target.user;
|
324
|
+
delete target.password;
|
325
|
+
delete target.domain;
|
326
|
+
delete target.domains;
|
327
|
+
delete target.port;
|
328
|
+
if (
|
329
|
+
!!target.root == !!source.root && !(
|
330
|
+
target.root &&
|
331
|
+
target.directories[0] != source.directories[0]
|
332
|
+
)
|
333
|
+
) {
|
334
|
+
delete target.path;
|
335
|
+
delete target.root;
|
336
|
+
delete target.directory;
|
337
|
+
while (
|
338
|
+
source.directories.length &&
|
339
|
+
target.directories.length &&
|
340
|
+
target.directories[0] == source.directories[0]
|
341
|
+
) {
|
342
|
+
target.directories.shift();
|
343
|
+
source.directories.shift();
|
344
|
+
}
|
345
|
+
while (source.directories.length) {
|
346
|
+
source.directories.shift();
|
347
|
+
target.directories.unshift('..');
|
348
|
+
}
|
349
|
+
|
350
|
+
if (!target.root && !target.directories.length && !target.file && source.file)
|
351
|
+
target.directories.push('.');
|
352
|
+
|
353
|
+
if (source.file == target.file)
|
354
|
+
delete target.file;
|
355
|
+
if (source.query == target.query)
|
356
|
+
delete target.query;
|
357
|
+
if (source.anchor == target.anchor)
|
358
|
+
delete target.anchor;
|
359
|
+
}
|
360
|
+
}
|
361
|
+
|
362
|
+
return target;
|
363
|
+
};
|
364
|
+
|
365
|
+
/**** resolve
|
366
|
+
returns a URL resovled to a relative URL from a source URL.
|
367
|
+
*/
|
368
|
+
exports.resolve = function (source, relative) {
|
369
|
+
return exports.format(exports.resolveObject(source, relative));
|
370
|
+
};
|
371
|
+
|
372
|
+
/**** relative
|
373
|
+
returns a relative URL to a target from a source.
|
374
|
+
*/
|
375
|
+
exports.relative = function (source, target) {
|
376
|
+
return exports.format(exports.relativeObject(source, target));
|
377
|
+
};
|
378
|
+
|