sproutit-narwhal 0.1.106

Sign up to get free protection for your applications and to get access to all the features.
Files changed (291) hide show
  1. data/DISTRIBUTION.yml +15 -0
  2. data/README.md +86 -0
  3. data/Rakefile +349 -0
  4. data/VERSION.yml +7 -0
  5. data/bin/activate +50 -0
  6. data/bin/activate.bash +50 -0
  7. data/bin/activate.cmd +3 -0
  8. data/bin/js +67 -0
  9. data/bin/json +2 -0
  10. data/bin/narwhal +67 -0
  11. data/bin/narwhal.cmd +29 -0
  12. data/bin/sea +45 -0
  13. data/bin/sea.cmd +25 -0
  14. data/bin/tusk +2 -0
  15. data/bin/tusk.cmd +5 -0
  16. data/catalog.json +902 -0
  17. data/docs/available-packages.md +32 -0
  18. data/docs/browser-api-plan.md +290 -0
  19. data/docs/browser-api.md +153 -0
  20. data/docs/download.md +25 -0
  21. data/docs/engines.md +32 -0
  22. data/docs/json-tool.md +121 -0
  23. data/docs/lib/binary.wiki +242 -0
  24. data/docs/lib/file.wiki +325 -0
  25. data/docs/lib/os/popen.md +70 -0
  26. data/docs/modules.md +38 -0
  27. data/docs/narwhal.md +487 -0
  28. data/docs/packages-howto.md +32 -0
  29. data/docs/packages.md +30 -0
  30. data/docs/posts/2009-07-29-hello-0.1.md +19 -0
  31. data/docs/quick-start.md +69 -0
  32. data/docs/sea.md +49 -0
  33. data/engines/browser/lib/binary.js +2 -0
  34. data/engines/browser/lib/reactor.js +21 -0
  35. data/engines/browser/lib/system.js +3 -0
  36. data/engines/default/lib/array.js +164 -0
  37. data/engines/default/lib/binary-engine.js +53 -0
  38. data/engines/default/lib/binary.js +755 -0
  39. data/engines/default/lib/date.js +8 -0
  40. data/engines/default/lib/file-engine.js +119 -0
  41. data/engines/default/lib/function.js +119 -0
  42. data/engines/default/lib/global.js +11 -0
  43. data/engines/default/lib/io-engine.js +26 -0
  44. data/engines/default/lib/json.js +488 -0
  45. data/engines/default/lib/object.js +69 -0
  46. data/engines/default/lib/os-engine.js +3 -0
  47. data/engines/default/lib/reactor.js +12 -0
  48. data/engines/default/lib/string.js +84 -0
  49. data/engines/default/lib/system.js +20 -0
  50. data/engines/default/lib/worker.js +133 -0
  51. data/engines/jsc/README.md +18 -0
  52. data/engines/jsc/bootstrap.js +53 -0
  53. data/engines/jsc/deps/http-parser/LICENSE +77 -0
  54. data/engines/jsc/deps/http-parser/README.md +145 -0
  55. data/engines/jsc/deps/http-parser/http_parser.c +6087 -0
  56. data/engines/jsc/deps/http-parser/http_parser.h +141 -0
  57. data/engines/jsc/deps/http-parser/http_parser.rl +500 -0
  58. data/engines/jsc/deps/http-parser/test.c +858 -0
  59. data/engines/jsc/include/binary-engine.h +11 -0
  60. data/engines/jsc/include/io-engine.h +23 -0
  61. data/engines/jsc/include/narwhal.h +427 -0
  62. data/engines/jsc/lib/file-engine.js +31 -0
  63. data/engines/jsc/lib/http.js +1 -0
  64. data/engines/jsc/lib/io-engine.js +202 -0
  65. data/engines/jsc/lib/os-engine.js +25 -0
  66. data/engines/jsc/lib/system.js +18 -0
  67. data/engines/jsc/lib/zip.js +1 -0
  68. data/engines/jsc/narwhal-jsc.c +273 -0
  69. data/engines/jsc/narwhal.c +29 -0
  70. data/engines/jsc/package.json +8 -0
  71. data/engines/jsc/src/binary-engine.cc +290 -0
  72. data/engines/jsc/src/file-engine.cc +405 -0
  73. data/engines/jsc/src/io-engine.cc +423 -0
  74. data/engines/jsc/src/jack/handler/jill.cc +710 -0
  75. data/engines/jsc/src/os-engine.cc +210 -0
  76. data/engines/rhino/bin/narwhal-rhino +68 -0
  77. data/engines/rhino/bin/narwhal-rhino.cmd +34 -0
  78. data/engines/rhino/bootstrap.js +119 -0
  79. data/engines/rhino/jars/jline.jar +0 -0
  80. data/engines/rhino/jars/jna.jar +0 -0
  81. data/engines/rhino/jars/js.jar +0 -0
  82. data/engines/rhino/lib/binary-engine.js +83 -0
  83. data/engines/rhino/lib/concurrency.js +6 -0
  84. data/engines/rhino/lib/event-queue.js +18 -0
  85. data/engines/rhino/lib/file-engine.js +216 -0
  86. data/engines/rhino/lib/http-client-engine.js +90 -0
  87. data/engines/rhino/lib/http-engine.js +10 -0
  88. data/engines/rhino/lib/io-engine.js +347 -0
  89. data/engines/rhino/lib/md5-engine.js +40 -0
  90. data/engines/rhino/lib/os-engine.js +150 -0
  91. data/engines/rhino/lib/packages-engine.js +71 -0
  92. data/engines/rhino/lib/sandbox-engine.js +70 -0
  93. data/engines/rhino/lib/system.js +38 -0
  94. data/engines/rhino/lib/worker-engine.js +23 -0
  95. data/engines/rhino/lib/zip.js +78 -0
  96. data/engines/rhino/package.json +4 -0
  97. data/engines/secure/lib/file.js +6 -0
  98. data/engines/secure/lib/system.js +6 -0
  99. data/engines/template/bin/narwhal-engine-name +32 -0
  100. data/engines/template/bootstrap.js +40 -0
  101. data/engines/template/lib/file-engine.js +118 -0
  102. data/engines/template/lib/system.js +17 -0
  103. data/examples/browser-deployment-jackconfig.js +35 -0
  104. data/examples/fibonacci-worker.js +35 -0
  105. data/examples/fibonacci.js +19 -0
  106. data/examples/hello +2 -0
  107. data/examples/narwhal +3 -0
  108. data/examples/not-quite-a-quine.js +1 -0
  109. data/extconf.rb +44 -0
  110. data/gem_bin/narwhal +5 -0
  111. data/gem_bin/sea +4 -0
  112. data/gem_bin/tusk +4 -0
  113. data/lib/args.js +849 -0
  114. data/lib/base16.js +16 -0
  115. data/lib/base64.js +120 -0
  116. data/lib/codec/base64.js +8 -0
  117. data/lib/crc32.js +60 -0
  118. data/lib/file-bootstrap.js +187 -0
  119. data/lib/file.js +659 -0
  120. data/lib/hash.js +28 -0
  121. data/lib/hashp.js +65 -0
  122. data/lib/html.js +16 -0
  123. data/lib/http-client.js +134 -0
  124. data/lib/http.js +17 -0
  125. data/lib/io.js +98 -0
  126. data/lib/jsmin.js +315 -0
  127. data/lib/jsonpath.js +89 -0
  128. data/lib/logger.js +55 -0
  129. data/lib/md4.js +146 -0
  130. data/lib/md5.js +164 -0
  131. data/lib/mime.js +166 -0
  132. data/lib/narwhal.js +102 -0
  133. data/lib/narwhal/client.js +261 -0
  134. data/lib/narwhal/compile.js +99 -0
  135. data/lib/narwhal/env.js +140 -0
  136. data/lib/narwhal/inline.js +106 -0
  137. data/lib/narwhal/json.js +324 -0
  138. data/lib/narwhal/json.md +178 -0
  139. data/lib/narwhal/repl.js +96 -0
  140. data/lib/narwhal/server-test.js +6 -0
  141. data/lib/narwhal/server.js +270 -0
  142. data/lib/narwhal/tusk.js +170 -0
  143. data/lib/narwhal/tusk/bin.js +13 -0
  144. data/lib/narwhal/tusk/bundle.js +0 -0
  145. data/lib/narwhal/tusk/catalog.js +22 -0
  146. data/lib/narwhal/tusk/clone.js +66 -0
  147. data/lib/narwhal/tusk/consolidate.js +25 -0
  148. data/lib/narwhal/tusk/create-catalog.js +80 -0
  149. data/lib/narwhal/tusk/engine.js +42 -0
  150. data/lib/narwhal/tusk/freeze.js +0 -0
  151. data/lib/narwhal/tusk/init.js +56 -0
  152. data/lib/narwhal/tusk/install.js +288 -0
  153. data/lib/narwhal/tusk/list.js +20 -0
  154. data/lib/narwhal/tusk/orphans.js +0 -0
  155. data/lib/narwhal/tusk/reheat.js +15 -0
  156. data/lib/narwhal/tusk/remove.js +15 -0
  157. data/lib/narwhal/tusk/search.js +145 -0
  158. data/lib/narwhal/tusk/update.js +21 -0
  159. data/lib/narwhal/tusk/upgrade.js +0 -0
  160. data/lib/os.js +33 -0
  161. data/lib/packages.js +423 -0
  162. data/lib/printf.js +169 -0
  163. data/lib/promise.js +352 -0
  164. data/lib/querystring.js +176 -0
  165. data/lib/ref-send.js +257 -0
  166. data/lib/regexp.js +12 -0
  167. data/lib/sandbox.js +422 -0
  168. data/lib/sha.js +112 -0
  169. data/lib/sha256.js +102 -0
  170. data/lib/struct.js +228 -0
  171. data/lib/term.js +179 -0
  172. data/lib/test/assert.js +95 -0
  173. data/lib/test/equiv.js +188 -0
  174. data/lib/test/jsdump.js +165 -0
  175. data/lib/test/runner.js +129 -0
  176. data/lib/unload.js +13 -0
  177. data/lib/uri.js +378 -0
  178. data/lib/url.js +5 -0
  179. data/lib/utf8.js +64 -0
  180. data/lib/util.js +985 -0
  181. data/lib/uuid.js +89 -0
  182. data/lib/xregexp.js +521 -0
  183. data/local.json.template +1 -0
  184. data/narwhal.gemspec +105 -0
  185. data/narwhal.js +213 -0
  186. data/package.json +26 -0
  187. data/packages/readline/engines/default/lib/readline.js +4 -0
  188. data/packages/readline/engines/rhino/lib/readline.js +6 -0
  189. data/packages/readline/package.json +5 -0
  190. data/sources.json +207 -0
  191. data/tests/all-tests.js +17 -0
  192. data/tests/args.js +31 -0
  193. data/tests/args/domain.js +215 -0
  194. data/tests/args/options.js +36 -0
  195. data/tests/args/shifting.js +92 -0
  196. data/tests/args/validation.js +31 -0
  197. data/tests/base64.js +23 -0
  198. data/tests/commonjs.js +3 -0
  199. data/tests/commonjs/all-tests.js +12 -0
  200. data/tests/commonjs/bytearray-encodings-tests.js +69 -0
  201. data/tests/commonjs/bytearray-tests.js +465 -0
  202. data/tests/commonjs/bytestring-encodings-tests.js +89 -0
  203. data/tests/commonjs/bytestring-tests.js +263 -0
  204. data/tests/commonjs/es5/all-tests.js +3 -0
  205. data/tests/commonjs/es5/bind.js +29 -0
  206. data/tests/commonjs/file-tests.js +315 -0
  207. data/tests/commonjs/file/dirname.js +31 -0
  208. data/tests/commonjs/file/extension.js +45 -0
  209. data/tests/commonjs/file/is-absolute.js +11 -0
  210. data/tests/commonjs/file/iterator.js +101 -0
  211. data/tests/commonjs/file/normal.js +27 -0
  212. data/tests/commonjs/file/path.js +17 -0
  213. data/tests/commonjs/file/relative.js +42 -0
  214. data/tests/commonjs/file/resolve.js +44 -0
  215. data/tests/commonjs/module-tests.js +9 -0
  216. data/tests/commonjs/modules/absolute/b.js +1 -0
  217. data/tests/commonjs/modules/absolute/program.js +5 -0
  218. data/tests/commonjs/modules/absolute/submodule/a.js +3 -0
  219. data/tests/commonjs/modules/absolute/test.js +9 -0
  220. data/tests/commonjs/modules/all-tests.js +47 -0
  221. data/tests/commonjs/modules/config.js +11 -0
  222. data/tests/commonjs/modules/cyclic/a.js +4 -0
  223. data/tests/commonjs/modules/cyclic/b.js +4 -0
  224. data/tests/commonjs/modules/cyclic/program.js +10 -0
  225. data/tests/commonjs/modules/cyclic/test.js +9 -0
  226. data/tests/commonjs/modules/determinism/program.js +3 -0
  227. data/tests/commonjs/modules/determinism/submodule/a.js +8 -0
  228. data/tests/commonjs/modules/determinism/submodule/b.js +2 -0
  229. data/tests/commonjs/modules/determinism/test.js +9 -0
  230. data/tests/commonjs/modules/exactExports/a.js +3 -0
  231. data/tests/commonjs/modules/exactExports/program.js +4 -0
  232. data/tests/commonjs/modules/exactExports/test.js +9 -0
  233. data/tests/commonjs/modules/hasOwnProperty/hasOwnProperty.js +0 -0
  234. data/tests/commonjs/modules/hasOwnProperty/program.js +3 -0
  235. data/tests/commonjs/modules/hasOwnProperty/test.js +9 -0
  236. data/tests/commonjs/modules/hasOwnProperty/toString.js +0 -0
  237. data/tests/commonjs/modules/method/a.js +12 -0
  238. data/tests/commonjs/modules/method/program.js +8 -0
  239. data/tests/commonjs/modules/method/test.js +9 -0
  240. data/tests/commonjs/modules/missing/program.js +8 -0
  241. data/tests/commonjs/modules/missing/test.js +9 -0
  242. data/tests/commonjs/modules/monkeys/a.js +1 -0
  243. data/tests/commonjs/modules/monkeys/program.js +4 -0
  244. data/tests/commonjs/modules/monkeys/test.js +9 -0
  245. data/tests/commonjs/modules/nested/a/b/c/d.js +3 -0
  246. data/tests/commonjs/modules/nested/program.js +3 -0
  247. data/tests/commonjs/modules/nested/test.js +9 -0
  248. data/tests/commonjs/modules/relative/program.js +5 -0
  249. data/tests/commonjs/modules/relative/submodule/a.js +1 -0
  250. data/tests/commonjs/modules/relative/submodule/b.js +2 -0
  251. data/tests/commonjs/modules/relative/test.js +9 -0
  252. data/tests/commonjs/modules/transitive/a.js +1 -0
  253. data/tests/commonjs/modules/transitive/b.js +1 -0
  254. data/tests/commonjs/modules/transitive/c.js +3 -0
  255. data/tests/commonjs/modules/transitive/program.js +3 -0
  256. data/tests/commonjs/modules/transitive/test.js +9 -0
  257. data/tests/file/all-tests.js +61 -0
  258. data/tests/file/fnmatch.js +102 -0
  259. data/tests/file/glob.js +466 -0
  260. data/tests/file/match.js +102 -0
  261. data/tests/global.js +6 -0
  262. data/tests/global/array.js +19 -0
  263. data/tests/hashes.js +94 -0
  264. data/tests/html.js +13 -0
  265. data/tests/io/stringio.js +21 -0
  266. data/tests/os/all-tests.js +4 -0
  267. data/tests/os/popen.js +41 -0
  268. data/tests/os/system.js +22 -0
  269. data/tests/printf.js +123 -0
  270. data/tests/query-string.js +87 -0
  271. data/tests/sandbox/byte-io.js +20 -0
  272. data/tests/sandbox/fileName.js +3 -0
  273. data/tests/sandbox/foo.js +0 -0
  274. data/tests/sandbox/reload.js +79 -0
  275. data/tests/string.js +35 -0
  276. data/tests/uri.js +41 -0
  277. data/tests/util/all-tests.js +79 -0
  278. data/tests/util/array.js +207 -0
  279. data/tests/util/array/is-arguments.js +29 -0
  280. data/tests/util/array/is-array-like.js +29 -0
  281. data/tests/util/case.js +9 -0
  282. data/tests/util/collection.js +104 -0
  283. data/tests/util/eq.js +57 -0
  284. data/tests/util/expand.js +45 -0
  285. data/tests/util/object.js +125 -0
  286. data/tests/util/operator.js +25 -0
  287. data/tests/util/range.js +19 -0
  288. data/tests/util/repr.js +26 -0
  289. data/tests/util/string.js +34 -0
  290. data/tests/util/unique.js +12 -0
  291. metadata +434 -0
@@ -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
@@ -0,0 +1,13 @@
1
+
2
+ var observers = [];
3
+
4
+ exports.when = function (observer) {
5
+ observers.unshift(observer);
6
+ };
7
+
8
+ exports.send = function () {
9
+ observers.forEach(function (observer) {
10
+ observer();
11
+ });
12
+ };
13
+
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
+