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,178 @@
1
+
2
+ JA: normal JSON
3
+ [[1,2,3],[4,5,6]]
4
+
5
+ JA': abnormal multi-line JSON
6
+ [[1,2,3],
7
+ [4,5,6]]
8
+
9
+ JAR: normal reversed java array
10
+ [[3,2,1],[6,5,4]]
11
+
12
+ JOA: deep JSON config file
13
+ {
14
+ "values": [
15
+ [1,2,3],
16
+ [4,5,6]
17
+ ]
18
+ }
19
+
20
+ JAO: a deep JSON array of objects
21
+
22
+ [
23
+ {"a": 1, "b": 2, "c": 3},
24
+ {"a": 4, "b": 5, "c": 6}
25
+ ]
26
+
27
+ JAS: a JSON array of sums
28
+
29
+ [6, 15]
30
+
31
+ JP: pretty JSON, t = 4
32
+
33
+ [
34
+ [1,2,3],
35
+ [4,5,6]
36
+ ]
37
+
38
+ JL: lines of JSON
39
+ [1,2,3]
40
+ [4,5,6]
41
+
42
+ J0: null-terminated lines of JSON
43
+
44
+ [1,2,3]\0[4,5,6]\0
45
+
46
+ SL: lines of toString
47
+
48
+ 1,2,3
49
+ 4,5,6
50
+
51
+ S0: null terminated lines of toString
52
+
53
+ 1,2,3\04,5,6\0
54
+
55
+ flat lines of JSON values
56
+ 1
57
+ 2
58
+ 3
59
+ 4
60
+ 5
61
+ 6
62
+
63
+ flat lines of JSON
64
+ "2"
65
+ "3"
66
+ "4"
67
+ "5"
68
+ "6"
69
+
70
+ UAA: unix records
71
+
72
+ 1:2:3
73
+ 4:5:6
74
+
75
+ Cookbook
76
+ ========
77
+
78
+ JOA -> JA: extracting a JSON array form a JSON object with an array
79
+ -N}e{P
80
+
81
+ -N _.read()
82
+ -} JSON.decode(_)
83
+ -e statement(_) "_.values"
84
+ -{ JSON.encode(_)
85
+ -P print(_)
86
+
87
+ JA -> JOA: creating a JSON object with an array from a JSON array
88
+ -N}x{P
89
+
90
+ -N _.read()
91
+ -} JSON.decode(_)
92
+ -x expression(_) "{values: _}"
93
+ -{ JSON.encode(_)
94
+ -P print(_)
95
+
96
+ JAO -> JA: converting objects to arrays
97
+ -N}f{P
98
+
99
+ -N _.read()
100
+ -} JSON.decode(_)
101
+ -f _.map(select) "a,b,c"
102
+ -{ JSON.encode(_)
103
+ -P print(_)
104
+
105
+ JAR -> JA
106
+ -N}
107
+
108
+ -N _.read()
109
+ -} JSON.decode(_)
110
+ -f _.map(select) "3,2,1"
111
+ -{ JSON.encode(_)
112
+ -P print(_)
113
+
114
+ JA -> JAS: calculating the sum of each array
115
+ -N}l{P
116
+
117
+ -N _.read()
118
+ -} JSON.decode(_)
119
+ -l _.map(lambda) "a,b,c" "a+b+c"
120
+ -{ JSON.encode(_)
121
+ -P print(_)
122
+
123
+ JA' -> JA: normalizing an abnormal JSON file
124
+ -N}{P
125
+
126
+ -N _.read()
127
+ -} JSON.decode(_)
128
+ -{ JSON.encode(_)
129
+ -P print(_)
130
+
131
+ JL -> J
132
+ -}]{P
133
+
134
+ -} _.map(JSON.decode)
135
+ -] [array(_)]
136
+ -{ JSON.encode(_)
137
+ -P print(_)
138
+
139
+ J -> JL
140
+ -N}{p
141
+
142
+ -N _.read()
143
+ -} JSON.decode(_)
144
+ -{ _.map(JSON.encode)
145
+ -p _.forEach(print)
146
+
147
+ J -> SL
148
+ -N}p
149
+
150
+ -N _.read()
151
+ -} JSON.decode(_)
152
+ -p _.forEach(print)
153
+
154
+ SL -> JA
155
+ -ne]{P
156
+
157
+ -ne _.map(expression) "_.split(/,/)"
158
+ -] [array(_)]
159
+ -{ JSON.encode(_)
160
+ -P print(_)
161
+
162
+ UAA -> JA
163
+ -d]{P
164
+
165
+ -d _.map(split) ":"
166
+ -] [array(_)]
167
+ -{ JSON.encode(_)
168
+ -P print(_)
169
+
170
+ UAA -> JAR
171
+ -d]f{P
172
+
173
+ -d _.map(split) ":"
174
+ -] [array(_)]
175
+ -f _.map(select) "3,2,1"
176
+ -{ JSON.encode(_)
177
+ -P print(_)
178
+
@@ -0,0 +1,96 @@
1
+
2
+ var system = require("system");
3
+
4
+ try { var readline = require("readline").readline; } catch (e) {}
5
+ try { var Narcissus = require("narcissus/parse"); } catch (e) {}
6
+
7
+ var util = require('util');
8
+
9
+ var PROMPT_NORMAL = "js> ",
10
+ PROMPT_INCOMPLETE = " > "
11
+
12
+ var buffer = "";
13
+ function readln(input) {
14
+ while (!(/\n/).test(buffer))
15
+ buffer += input.read(100).toString();
16
+
17
+ var lines = buffer.split("\n"),
18
+ line = lines.shift();
19
+
20
+ buffer = lines.join("\n");
21
+
22
+ return line
23
+ }
24
+
25
+ exports.repl = function() {
26
+
27
+ var buffer = "",
28
+ bufferedLines = [],
29
+ pendingLines = [];
30
+
31
+ system.stdout.write(PROMPT_NORMAL);
32
+ system.stdout.flush();
33
+
34
+ while (true) {
35
+
36
+ var line = readline ? readline() : system.stdin.readLine();
37
+ if (!line) {
38
+ system.stdout.write("\n");
39
+ system.stdout.flush();
40
+ break;
41
+ }
42
+
43
+ line = util.trimEnd(line);
44
+ var text = pendingLines.join("\n") + "\n" + line;
45
+ if (line == "" || !incomplete(text)) {
46
+
47
+ pendingLines = [];
48
+
49
+ try {
50
+ var result = system.evalGlobal(text);
51
+ var repr = util.repr(result);
52
+ if (repr.length > 76 || /\n/.test(repr))
53
+ repr = String(result);
54
+ if (!util.no(result)) {
55
+ system.stdout.write(repr + '\n');
56
+ system.stdout.flush();
57
+ global._ = result;
58
+ }
59
+ } catch (e) {
60
+ system.stdout.write(" exception from uncaught JavaScript throw: " + e + "\n");
61
+ }
62
+
63
+ } else
64
+ pendingLines.push(line);
65
+
66
+ if (pendingLines.length > 0)
67
+ system.stdout.write(PROMPT_INCOMPLETE);
68
+ else
69
+ system.stdout.write(PROMPT_NORMAL);
70
+ system.stdout.flush();
71
+
72
+ }
73
+ }
74
+
75
+ function incomplete(text) {
76
+ if (!Narcissus)
77
+ return false;
78
+
79
+ var incomp = true;
80
+ try {
81
+ var t = new Narcissus.Tokenizer(text);
82
+ var x = new Narcissus.CompilerContext(false);
83
+ var n = Narcissus.Script(t, x);
84
+ incomp = !t.done;
85
+ } catch (e) {
86
+ if (!t.done) {
87
+ print(e);
88
+ return false;
89
+ }
90
+ }
91
+ return incomp;
92
+ }
93
+
94
+ if (module.id == require.main)
95
+ exports.repl();
96
+
@@ -0,0 +1,6 @@
1
+ var Q = require("ref-send");
2
+ Q.when(require.async("util"), function (util) {
3
+ print("Hello, World!");
4
+ print(util.keys({"a": 10, "b": 20}).join(', '));
5
+ alert(util.upper('hi'));
6
+ });
@@ -0,0 +1,270 @@
1
+
2
+ var SYSTEM = require("system");
3
+ var FILE = require("file");
4
+ var UTIL = require("util");
5
+ var SANDBOX = require("sandbox");
6
+ var JSMIN = require("jsmin");
7
+ var JACKUTILS = require("jack/utils");
8
+
9
+ var Loader = SANDBOX.Loader;
10
+ var AttenuatedLoader = SANDBOX.AttenuatedLoader;
11
+ var Sandbox = SANDBOX.Sandbox;
12
+
13
+ var contentType = "application/x-javascript";
14
+
15
+ exports.Bundler = function (system) {
16
+ var self = {};
17
+
18
+ if (!system)
19
+ system = SYSTEM;
20
+ system = Object.create(system);
21
+ system.engine = "browser";
22
+ system.engines = UTIL.copy(system.engines);
23
+ system.engines.unshift("browser");
24
+ var paths = require.paths.map(function (path) {
25
+ return String(path);
26
+ });
27
+ paths.unshift(FILE.join(system.prefix, "engines", "browser", "lib"));
28
+
29
+ var loader = AttenuatedLoader(Loader({"paths": paths}));
30
+ var sandbox = Sandbox({
31
+ "loader": loader,
32
+ "system": system,
33
+ "modules": {
34
+ "system": system
35
+ }
36
+ });
37
+
38
+ sandbox.force("system");
39
+ sandbox("global");
40
+ sandbox("packages").main();
41
+
42
+ self.depends = function (baseId) {
43
+ // special case for the sandbox observer
44
+ // in inline.js
45
+ if (baseId == "sandbox")
46
+ return ["global", "system"];
47
+ var depends = [];
48
+ loader.fetch(baseId).replace(
49
+ // TODO proper recursive descent parser
50
+ /(?:^|\s+|;)require\s*\(\s*(['"])([^'"]+)\1\s*\)/g,
51
+ function (all, quote, id) {
52
+ depends.push(loader.resolve(id, baseId));
53
+ return all;
54
+ }
55
+ );
56
+ return depends;
57
+ };
58
+
59
+ self.deepDepends = function (id, visited) {
60
+ var result = [];
61
+ if (!visited)
62
+ visited = {};
63
+ if (UTIL.has(visited, id))
64
+ return result;
65
+ visited[id] = true;
66
+ result.push(id);
67
+ self.depends(id).forEach(function (id) {
68
+ result.push.apply(
69
+ result,
70
+ self.deepDepends(id, visited)
71
+ );
72
+ });
73
+ return result;
74
+ };
75
+
76
+ self.module = function (id) {
77
+ return loader.fetch(id);
78
+ };
79
+
80
+ self.bundle = function (ids) {
81
+ return (
82
+ "require.register({" +
83
+ ids.map(function (id) {
84
+ var depends = JSON.encode(self.depends(id));
85
+ var factory = (
86
+ "function(require,exports,module,system,print){" +
87
+ loader.fetch(id) +
88
+ "/**/\n}"
89
+ );
90
+ return (
91
+ UTIL.enquote(id) + ':' +
92
+ "{" +
93
+ '"factory":' + factory + "," +
94
+ '"depends":' + depends +
95
+ "}"
96
+ );
97
+ }).join(",") +
98
+ "});"
99
+ );
100
+ };
101
+
102
+ return self;
103
+ };
104
+
105
+ exports.BundlerApp = function (bundler, options) {
106
+ options = options || {};
107
+ var debug = options.debug;
108
+ return function (env) {
109
+ var id = env.PATH_INFO.replace(/^\//, '').replace(/\.js$/, '');
110
+ var content = bundler.bundle([id]);
111
+ if (!debug)
112
+ content = JSMIN.encode(content);
113
+ return {
114
+ "status": 200,
115
+ "headers": {"Content-type": contentType},
116
+ "body": [content]
117
+ };
118
+ };
119
+ };
120
+
121
+ exports.App = function (nextApp, options) {
122
+ options = options || {};
123
+ var debug = options.debug;
124
+ var proxy = options.proxy;
125
+ var catalog = options.catalog;
126
+ var noHost = proxy || catalog;
127
+ if (noHost && options.path)
128
+ throw new Error("The path option is not applicable when using a proxy or catalog.");
129
+
130
+ var bundler = exports.Bundler();
131
+
132
+ var path = options.path || '.js/';
133
+ if (!/\/$/.test(path))
134
+ path += "/";
135
+ var re = new RegExp("^/" + RegExp.escape(path));
136
+ var jsApp = exports.BundlerApp(bundler, options);
137
+
138
+ return function (env) {
139
+ if (!proxy)
140
+ proxy = '/' + env.SCRIPT_NAME + path;
141
+ env.script = exports.ClientLoader(
142
+ env,
143
+ proxy,
144
+ bundler,
145
+ options
146
+ );
147
+ if (noHost) {
148
+ return nextApp(env);
149
+ } else if (re.test(env.PATH_INFO)) {
150
+ env = Object.create(env);
151
+ env.SCRIPT_NAME = env.SCRIPT_NAME + path;
152
+ env.PATH_INFO = env.PATH_INFO.substring(path.length);
153
+ return jsApp(env);
154
+ } else {
155
+ return nextApp(env);
156
+ }
157
+ };
158
+
159
+ };
160
+
161
+ var inline = FILE.path(module.path)
162
+ .resolve("inline.js")
163
+ .read()
164
+ .replace(/\s*$/, "");
165
+
166
+ exports.ClientLoader = function (env, path, bundler, options) {
167
+ options = options || {};
168
+ var debug = options.debug;
169
+ var bundle = options.bundle;
170
+ var proxy = options.proxy;
171
+ var catalog = options.catalog;
172
+
173
+ // returns a script that loads a program
174
+ var loader = {};
175
+
176
+ var bootstrap = [
177
+ "narwhal/client",
178
+ "sandbox",
179
+ "system",
180
+ "ref-send",
181
+ "reactor"
182
+ ].concat(
183
+ bundler.deepDepends("global")
184
+ );
185
+ var queue = Array.prototype.slice.call(bootstrap);
186
+
187
+ var clientSent = false;
188
+ var client = function () {
189
+ if (clientSent) {
190
+ return "require";
191
+ } else {
192
+ clientSent = true;
193
+ var content = inline +
194
+ ".call(this," + UTIL.repr(path) + ")";
195
+ if (!debug)
196
+ content = JSMIN.encode(content);
197
+ return content;
198
+ }
199
+ };
200
+
201
+ var flush = function () {
202
+ var ids = queue.splice(0, queue.length);
203
+ UTIL.sort(ids, UTIL.by(function (id) {
204
+ return -bundler.module(id).length;
205
+ }));
206
+ return client() +
207
+ ".preload(" + UTIL.repr(ids) + ")";
208
+ };
209
+
210
+ loader.loader = function () {
211
+ return flush();
212
+ };
213
+
214
+ // returns a script that loads a bunch of factories
215
+ loader.preload = function (ids) {
216
+ ids = Array.prototype.concat.apply([], ids.map(bundler.deepDepends));
217
+ // enqueue the given module's deep dependencies
218
+ queue.push.apply(queue, ids);
219
+ return flush();
220
+ };
221
+
222
+ // returns a script that loads and requires a module
223
+ loader.require = function (id) {
224
+ return loader.preload([id]) + ".async(" + UTIL.repr(id) + ")";
225
+ };
226
+
227
+ loader.require.async = function (id) {
228
+ return flush() + ".async(" + UTIL.repr(id) + ")";
229
+ };
230
+
231
+ // return a script that *is* a bunch of factories and a loader for them.
232
+ loader.embed = function (id) {
233
+ var content = loader.loader() +
234
+ ".bridge(function(require){" +
235
+ bundler.bundle(
236
+ queue.splice(0, queue.length)
237
+ .concat(bundler.deepDepends(id))
238
+ ) +
239
+ "}).async(" + UTIL.repr(id) + ");";
240
+ if (!debug)
241
+ content = JSMIN.encode(content);
242
+ return content;
243
+ };
244
+
245
+ return loader;
246
+ };
247
+
248
+ exports.main = function () {
249
+ var app = function (env) {
250
+ if (/^../.test(env.PATH_INFO))
251
+ return JACKUTILS.responseForStatus(404, env.PATH_INFO);
252
+ return {
253
+ "status": 200,
254
+ "headers": {"Content-type": "text/html"},
255
+ "body": [
256
+ "<html><head><script>" +
257
+ env.script.require("narwhal/server-test") +
258
+ "</script></head><body></body></html>"
259
+ ]
260
+ };
261
+ };
262
+ app = exports.App(app, {"debug": true});
263
+ app = require("jack").ContentLength(app);
264
+ exports.app = app;
265
+ require("jackup").main(["jackup", module.path]);
266
+ };
267
+
268
+ if (require.main == module.id)
269
+ exports.main();
270
+