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,99 @@
1
+
2
+ var SYSTEM = require("system");
3
+ var FILE = require("file");
4
+ var PACKAGES = require("packages");
5
+ var UTIL = require("util");
6
+ var TERM = require("term");
7
+ var Bundler = require("./server").Bundler;
8
+
9
+ exports.ids = function (system, packages) {
10
+ if (!packages)
11
+ packages = PACKAGES;
12
+ if (!system)
13
+ system = SYSTEM;
14
+
15
+ // scan the package order for all possibly relevant
16
+ // javascript files for the given system
17
+
18
+ var files = [];
19
+ packages.order.forEach(function (info) {
20
+ var enginePaths = info.engines || 'engines';
21
+ if (typeof enginePaths == "string")
22
+ enginePaths = [enginePaths];
23
+ system.engines.forEach(function (engine) {
24
+ enginePaths.forEach(function (enginePath) {
25
+ enginePath = enginePath + '/' + engine;
26
+ var enginePath = info[enginePath] || enginePath;
27
+ if (typeof enginePath == "string")
28
+ enginePath = [enginePath];
29
+ enginePath.forEach(function (enginePaths) {
30
+ enginePaths = enginePath + '/lib';
31
+ var enginePath = info[enginePaths] || enginePaths;
32
+ if (typeof enginePath == "string")
33
+ enginePath = [enginePath];
34
+ enginePath.forEach(function (enginePath) {
35
+ enginePath = info.directory.join(enginePath);
36
+ enginePath.globPaths("**.js").forEach(function (path) {
37
+ files.push([path, enginePath]);
38
+ });
39
+ });
40
+ });
41
+ });
42
+ });
43
+ info.lib.forEach(function (lib) {
44
+ lib.globPaths("**.js").forEach(function (path) {
45
+ files.push([path, lib]);
46
+ });
47
+ });
48
+ });
49
+
50
+ // transform paths into identifiers
51
+ var ids = UTIL.mapApply(files, function (path, base) {
52
+ var relative = path.from(base.join(''));
53
+ var parts = relative.split();
54
+ parts.pop();
55
+ parts.push(relative.basename('.js'));
56
+ var id = parts.join('/');
57
+ //print(id + ' ' + path + ' ' + base);
58
+ return id;
59
+ });
60
+
61
+ ids = UTIL.unique(ids);
62
+
63
+ return ids;
64
+ };
65
+
66
+ exports.build = function (location, system, packages) {
67
+
68
+ if (!system)
69
+ system = SYSTEM;
70
+ system = Object.create(system);
71
+ system.engine = "browser";
72
+ system.engines = UTIL.copy(system.engines);
73
+ system.engines.unshift("browser");
74
+
75
+ location = FILE.path(location);
76
+ var bundler = Bundler(system);
77
+ exports.ids(system, packages).forEach(function (id) {
78
+ try {
79
+ var parts = id.split('/');
80
+ var basename = parts.pop();
81
+ var dirname = location.join.apply(location, parts);
82
+ dirname.mkdirs();
83
+ var path = dirname.join(basename + '.js');
84
+ path.write(bundler.bundle([id]));
85
+ print(path);
86
+ } catch (exception) {
87
+ TERM.stream.print("\0red(" + exception + "\0)");
88
+ }
89
+ });
90
+
91
+ };
92
+
93
+ exports.main = function (system, packages) {
94
+ exports.build("build", system, packages);
95
+ };
96
+
97
+ if (require.main == module.id)
98
+ exports.main();
99
+
@@ -0,0 +1,140 @@
1
+
2
+ var file = require("file");
3
+ var system = require("system");
4
+ var packages = require("packages");
5
+ var util = require("util");
6
+ var args = require("args");
7
+
8
+ var parser = exports.parser = new args.Parser();
9
+
10
+ parser.help(
11
+ "Constructs an 'eval'-able shell script by augmenting\n" +
12
+ "current PATH and PATH-like environment variables with\n" +
13
+ "directories from installed packages in their precedence\n" +
14
+ "order. Precedence is determined by a topological sort\n" +
15
+ "of package dependencies. Topo-sort guarantees that the\n" +
16
+ "most dependent packages come first.\n" +
17
+ "\n" +
18
+ " eval $(narwhal -m narwhal/env --path --ldpath)\n" +
19
+ " echo $PATH\n"
20
+ );
21
+
22
+
23
+ parser.option('--path', 'path')
24
+ .help("prints a new PATH= based on package/bins")
25
+ .set(true);
26
+ parser.option('--ldpath', 'ldpath')
27
+ .help("prints a new LDPATH= based on package/libs")
28
+ .set(true);
29
+ parser.option('--classpath', 'classpath')
30
+ .help("prints a new CLASSPATH= based on package/classes and package/jars/*")
31
+ .set(true);
32
+ parser.option('--custom')
33
+ .help("prints a new $NAME= based on package/$name")
34
+ .action(function (options, name, envName, pathName) {
35
+ util.getset(options, 'custom', []).push([
36
+ envName,
37
+ pathName
38
+ ]);
39
+ });
40
+ parser.option('--extra')
41
+ .help("prints a new $NAME= based on package/$name and package/$extra/*")
42
+ .action(function (options, name, envName, pathName, extraName) {
43
+ util.getset(options, 'custom', []).push([
44
+ envName,
45
+ pathName,
46
+ extraName
47
+ ]);
48
+ });
49
+
50
+ parser.helpful();
51
+
52
+ var paths = function (envName, pathName, extraName) {
53
+
54
+ // construct a list of paths for each installed package
55
+ // based on the default name, or the overridden name
56
+ // or names from the package configuration.
57
+ var paths = Array.prototype.concat.apply(
58
+ [],
59
+ packages.order.map(function (info) {
60
+ var paths = info[pathName] || pathName;
61
+ if (typeof paths == "string")
62
+ paths = [paths];
63
+ return paths.map(function (path) {
64
+ return info.directory.resolve(path);
65
+ });
66
+ })
67
+ // and add the list of existing values from the
68
+ // corresponding environment value (in lower
69
+ // precedence, packages will override previous
70
+ // values)
71
+ ).concat(
72
+ (system.env[envName] || '')
73
+ .split(":")
74
+ // filter blank components, or empty env var
75
+ .filter(function (path) {
76
+ return path;
77
+ }).map(function (path) {
78
+ return new file.Path(path);
79
+ })
80
+ // grab only the ones that exist
81
+ ).filter(function (path) {
82
+ return path.isDirectory();
83
+ });
84
+
85
+ // grab individual files for the extra directory name
86
+ if (extraName) {
87
+ paths = Array.prototype.concat.apply(
88
+ paths,
89
+ Array.prototype.concat.apply(
90
+ [],
91
+ // as before, find the directories in
92
+ // package precedence order, accepting
93
+ // overrides from the package configuration
94
+ packages.order.map(function (info) {
95
+ var paths = info[extraName] || extraName;
96
+ if (typeof paths == "string")
97
+ paths = [paths];
98
+ return paths.map(function (path) {
99
+ return info.directory.resolve(path);
100
+ }).filter(function (path) {
101
+ return path.isDirectory();
102
+ });
103
+ })
104
+ // each individual file within that directory,
105
+ // as with *.jar in jars/
106
+ ).map(function (path) {
107
+ return path.listPaths();
108
+ })
109
+ );
110
+ }
111
+
112
+ paths = paths.map(function (path) {
113
+ return path.canonical().toString();
114
+ });
115
+
116
+ return util.unique(paths).join(":");
117
+ };
118
+
119
+ exports.main = function (system) {
120
+
121
+ var options = parser.parse(system.args);
122
+
123
+ if (options.path)
124
+ system.print("PATH=" + paths("PATH", "bin"));
125
+ if (options.ldpath)
126
+ system.print("LDPATH=" + paths("LDPATH", "lib"));
127
+ if (options.classpath)
128
+ system.print("CLASSPATH=" + paths(
129
+ "CLASSPATH", "classes",
130
+ "jars"
131
+ ));
132
+ util.forEachApply(options.custom, function (pathName, envName) {
133
+ system.print(envName + "=" + paths(envName, pathName));
134
+ });
135
+
136
+ };
137
+
138
+ if (require.main == module.id)
139
+ exports.main(system);
140
+
@@ -0,0 +1,106 @@
1
+ // this is not a module. it is a snippet of a script that will be sent by the
2
+ // client/browser module server defined in the narwhal/server module, or some
3
+ // similar mechanism. this file is simply an anonymous function declaration.
4
+ // the server constructs an inline script to inject anywhere in an HTML page,
5
+ // along with its "path" argument (the base URL from which to download
6
+ // modules). The function returns a module loader API function/object called
7
+ // "require" that can then be chained with .preload(ids), .when(id),
8
+ // .async(id), or other calls, depending on what services are needed to load
9
+ // this particular page.
10
+ (function (path) {
11
+
12
+ // ultimately, we're exporting a "require" API to global scope, and
13
+ // returning that same object.
14
+ var require = this.require = function (id) {
15
+ if (!require.require)
16
+ throw new Error("require is not yet available");
17
+ return require.require(id);
18
+ };
19
+
20
+ // the catalog is a lookup table of top level module identifiers to catalog
21
+ // entries. catalog entries include a module factory function and an array
22
+ // of top level identifiers of each dependency. this is populated by the
23
+ // register function that is called by injected module and bundle scripts.
24
+ require.catalog = {};
25
+ require.requests = [];
26
+ require.preloads = {};
27
+
28
+ // this is an event handler for factory arrivals that will be replaced when
29
+ // narwhal/client arrives.
30
+ require.arrive = function (id) {
31
+ // there's only one module we're particularly interested in at the
32
+ // moment. as soon as it comes down, load the client. this will take
33
+ // us to the next stage, getting the promise graph and sandbox set up.
34
+ if (id == "narwhal/client")
35
+ require.catalog[id].factory(require);
36
+ };
37
+
38
+ // this function gets called by injected scripts with a portion of the
39
+ // module catalog, which includes factories and dependency lists.
40
+ require.register = function (entries) {
41
+ for (var id in entries) {
42
+ if (Object.prototype.hasOwnProperty.call(entries, id)) {
43
+ require.catalog[id] = entries[id];
44
+ // make a note not to preload this module, even if it
45
+ // was not preloaded in the first place.
46
+ require.preloads[id] = true;
47
+ // this function gets redefined at various stages to notice the
48
+ // arrival of important modules for the bootstrapping process.
49
+ // 1.) narwhal/client
50
+ // 2.) ref-send and reactor
51
+ // 3.) all identifiers
52
+ require.arrive(id);
53
+ }
54
+ }
55
+ };
56
+
57
+ // this function injects script tags for modules or module bundles to
58
+ // asynchronously load modules that have been wrapped with their metadata
59
+ // for transport either by a build system or by a server-side component
60
+ // like the narwhal/server module.
61
+ require.preload = function (ids) {
62
+ setTimeout(function () {
63
+ var length = ids.length;
64
+ for (var i = 0; i < length; i++) {
65
+ // don't double preload; this check makes preload idempotent
66
+ if (require.preloads[ids[i]])
67
+ continue;
68
+ // make a note not to preload this module ever again.
69
+ require.preloads[ids[i]] = true;
70
+
71
+ // prepend instead of append to avoid KB917927
72
+ // - Kean Tan <http://www.karmagination.com/>
73
+ var script = document.createElement('script');
74
+ script.src = path + ids[i] + '.js';
75
+ document.documentElement.insertBefore(
76
+ script,
77
+ document.documentElement.firstChild
78
+ );
79
+
80
+ };
81
+ }, 0);
82
+ return require;
83
+ };
84
+
85
+ // this is the public API for requesting that a module be loaded as soon as
86
+ // it's ready (as soon as it and its transitive dependencies have arrived
87
+ // and been entered in the catalog). unfortunately, we're not ready to
88
+ // process requests at this stage, so we add them to a queue that will be
89
+ // processed later, at which point this function will also be replaced.
90
+ // all of these later steps occur toward the end of client.js
91
+ require.async = function (id) {
92
+ require.requests.push(id);
93
+ return require;
94
+ };
95
+
96
+ // for convenience, if ever we opt to abolish the global require
97
+ // variable, being able to call an arbitrary function while
98
+ // maintaining the require object along the call chain.
99
+ require.bridge = function (block) {
100
+ block(require);
101
+ return require;
102
+ };
103
+
104
+ // return the require object for chaining
105
+ return require;
106
+ })
@@ -0,0 +1,324 @@
1
+
2
+ var os = require("os");
3
+ var args = require("args");
4
+ var assert = require("test/assert");
5
+ var util = require("util");
6
+ var system = require("system");
7
+ var fs = require("file");
8
+
9
+ var parser = new args.Parser();
10
+
11
+ // syntax highlighting for option help
12
+ parser.Option = function () {
13
+ args.Option.apply(this, arguments);
14
+ return this;
15
+ };
16
+ parser.Option.prototype = Object.create(args.Option.prototype);
17
+ parser.Option.prototype.help = function (help) {
18
+ if (/->/.test(help)) {
19
+ help = help.replace(/\b[a-z]+\b/g, function (all) {
20
+ return '\0cyan(' + all + '\0)';
21
+ }).replace(/\b[A-Z]+\b/g, function (all) {
22
+ return '\0green(' + all + '\0)';
23
+ }).replace(/"[^"]+"/g, function (all) {
24
+ return '\0red(' + all + '\0)';
25
+ }).replace(/_/g, function (all) {
26
+ return '\0green(\0bold(' + all + '\0)\0)';
27
+ }).replace(/->|;/g, function (all) {
28
+ return '\0red(' + all + '\0)';
29
+ })
30
+ }
31
+ return args.Option.prototype.help.call(this, help);
32
+ };
33
+
34
+ parser.help(
35
+ 'A tool for working with JSON and other data.\n' +
36
+ 'Options get executed in order, on a register\n' +
37
+ 'or on each value in the register iteratively.'
38
+ );
39
+
40
+ parser.option('-i', '--file-in', 'name')
41
+ .help('open(NAME) -> _')
42
+ .action(function (options, name, fileName) {
43
+ options._ = fs.open(fileName);
44
+ });
45
+
46
+ parser.option('-o', '--file-out', 'name', 'fileOut')
47
+ .help('write(NAME, _); _ -> _')
48
+ .action(function (options, name, fileName) {
49
+ options.out = fs.open(fileName, 'w');
50
+ });
51
+
52
+ parser.option('-n', '--read-lines', 'mode')
53
+ .help('switches to line register mode; _ is each value in the register')
54
+ .set('lines');
55
+
56
+ parser.option('-N', '--read-all', 'mode')
57
+ .help('switches to whole register mode; _ is the whole value')
58
+ .action(function (options, name) {
59
+ options.mode = 'all';
60
+ /*if (options._.read && !Object.hasOwnProperty.call(options._, 'read'))
61
+ options._ = options._.read();
62
+ */
63
+ });
64
+
65
+ parser.option('-j', '-}', '--json-in', 'jsonIn')
66
+ .help('json.decode(_) -> _')
67
+ .action(function (options) {
68
+ options._ = block(options._, options.mode, function (_) {
69
+ if (!typeof _ == "string") {
70
+ system.stderr.print("JSON can only decode strings.");
71
+ os.exit(-1);
72
+ }
73
+ // consume strems before reading
74
+ _ = read(_);
75
+ return JSON.decode(_);
76
+ });
77
+ });
78
+
79
+ parser.option('-J', '-{', '--json-out', 'jsonOut')
80
+ .help('json.encode(_) -> _')
81
+ .action(function (options) {
82
+ options._ = collect(options._);
83
+ options._ = block(options._, options.mode, function (_) {
84
+ _ = collect(_);
85
+ return JSON.encode(_, null, options.tabWidth);
86
+ });
87
+ });
88
+
89
+ parser.option('-t', '--tab-width', 'tab-width', 'tabWidth')
90
+ .help('use pretty print with a given tab width')
91
+ .set();
92
+
93
+ parser.option('-T', '--pretty', 'tab-width', 'tabWidth')
94
+ .help('use pretty print with default tab width of 4')
95
+ .set(4);
96
+
97
+ parser.option('-$', '--json-path', 'path')
98
+ .help('jsonpath.resolve(_, PATH) -> _')
99
+ .action(function (options, name, path) {
100
+ options._ = require('jsonpath').resolve(options._, path);
101
+ });
102
+
103
+ parser.option('-c', '--array-collect')
104
+ .help('array(_) -> _')
105
+ .action(function (options) {
106
+ options._ = collect(options._);
107
+ });
108
+
109
+ parser.option('-]', '-a', '--array-wrap')
110
+ .help('[array(_)] -> _')
111
+ .action(function (options) {
112
+ // TODO convert strams to arrays
113
+ options._ = [options._];
114
+ });
115
+
116
+ parser.option('-[', '-A', '--array-unwrap')
117
+ .help('concat(_, _, _, ...) -> _')
118
+ .action(function (options) {
119
+ options._ = Array.prototype.concat.apply([], options._);
120
+ });
121
+
122
+ parser.option('-O', '--array2object', '--a2o')
123
+ .help('converts [name,value] pairs to {name:value} object')
124
+ .action(function (options) {
125
+ options._ = collect(options._)
126
+ options._ = block(options._, options.mode, function (_) {
127
+ return util.object(_);
128
+ });
129
+ });
130
+
131
+ parser.option('-X', '--object2array', '--o2a')
132
+ .help('converts {name:value} object to [name,value] pairs')
133
+ .action(function (options) {
134
+ options._ = block(options._, options.mode, function (_) {
135
+ return util.array(_);
136
+ });
137
+ });
138
+
139
+ parser.option('-d', '--delimiter-in', 'delimiter')
140
+ .help('split(_, DELIMITER) -> _')
141
+ .action(function (options, name, delimiter) {
142
+ // todo switch between regex and literal mode with -E
143
+ var regex = new RegExp(RegExp.escape(delimiter), 'g');
144
+ options._ = block(options._, options.mode, function (_) {
145
+ return ('' + _).split(regex);
146
+ });
147
+ });
148
+
149
+ parser.option('-D', '--delimiter-out', 'delimiter', 'delimiterOut')
150
+ .help('join(_, DELIMITER) -> _')
151
+ .action(function (options, name, delimiter) {
152
+ options._ = block(options._, options.mode, function (_) {
153
+ return collect(_).join(delimiter);
154
+ });
155
+ });
156
+
157
+ parser.option('-f', '-.', '--array-fields', 'names', 'fields')
158
+ .help('selects fields from objects or arrays (first is -f 1)')
159
+ .action(function (options, name, fields) {
160
+ fields = fields.split(",");
161
+ options._ = block(options._, options.mode, function (_) {
162
+ return fields.map(function (field) {
163
+ if (field == "_")
164
+ return _;
165
+ if ('' + (field >>> 0) === field)
166
+ return _[(field >>> 0) - 1];
167
+ return _[field];
168
+ });
169
+ });
170
+ });
171
+
172
+ parser.option('-F', '--object-fields', 'names')
173
+ .help('converts array indexes to object keys')
174
+ .action(function (options, name, fields) {
175
+ var fields = fields.split(",");
176
+ options._ = block(options._, options.mode, function (_) {
177
+ return util.object(util.zip(fields, _));
178
+ });
179
+ });
180
+
181
+ parser.option('-l', '--lambda', 'names', 'expression')
182
+ .help('(function (NAMES) EXPRESSION)(_) -> _')
183
+ .action(function (options, name, names, expression) {
184
+ var lambda = Function.constructor.apply(
185
+ null,
186
+ ['_'].concat(names.split(',')).concat(['return (' + expression + ')'])
187
+ );
188
+ options._ = block(options._, options.mode, function (_) {
189
+ return lambda.apply(null, [_].concat(_));
190
+ });
191
+ });
192
+
193
+ parser.option('-e', '--evaluate', 'expression')
194
+ .help('eval(EXPRESSION)) -> _')
195
+ .action(function (options, name, expression) {
196
+ options._ = block(options._, options.mode, function (_) {
197
+ global._ = _;
198
+ return system.evalGlobal(expression);
199
+ });
200
+ });
201
+
202
+ parser.option('-v', '--value', 'expression')
203
+ .help('eval("("+EXPRESSION+")") -> _')
204
+ .action(function (options, name, expression) {
205
+ options._ = block(options._, options.mode, function (_) {
206
+ global._ = _;
207
+ return system.evalGlobal('(' + expression + ')');
208
+ });
209
+ });
210
+
211
+ parser.option('-x', '--execute', 'statement')
212
+ .help('eval(STATEMENT); _ -> _')
213
+ .action(function (options, name, statement) {
214
+ options._ = block(options._, options.mode, function (_) {
215
+ global._ = _;
216
+ system.evalGlobal(statement);
217
+ return global._;
218
+ });
219
+ });
220
+
221
+ parser.option('-w', '--where', 'expression')
222
+ .help('if (EXPRESSION) _ -> _')
223
+ .action(function (options, name, expression) {
224
+ var results = [];
225
+ options._.forEach(function (_) {
226
+ global._ = _;
227
+ if (system.evalGlobal(expression))
228
+ results.push(_);
229
+ });
230
+ options._ = results;
231
+ })
232
+
233
+ parser.option('-p', '--print-lines', 'print')
234
+ .help('_.forEach(print); _ -> _')
235
+ .action(function (options) {
236
+ block(options._, options.mode, function (_) {
237
+ if (options.print0)
238
+ options.out.write('' + _ + '\0');
239
+ else
240
+ options.out.print('' + _);
241
+ });
242
+ if (options.print0)
243
+ options.out.flush();
244
+ });
245
+
246
+ parser.option('-P', '--print-all', 'print')
247
+ .help('print(_); _ -> _')
248
+ .action(function (options) {
249
+ options._ = collect(options._);
250
+ if (options.print0)
251
+ options.out.write('' + options._ + '\0');
252
+ else
253
+ options.out.print('' + options._);
254
+ });
255
+
256
+ parser.option('-0', '--read0', 'read0')
257
+ .help('read null terminated strings from streams with -N')
258
+ .action(function (options) {
259
+ // trim a final end of line if there is one
260
+ // (find -print0 writes one)
261
+ var content = options._.read().replace(/\0(\r\n?|\n)$/, '');
262
+ options._ = require("io").StringIO(content, "\0");
263
+ });
264
+
265
+ parser.option('-z', '--print0', 'print0')
266
+ .help('print null terminated strings to streams with -p')
267
+ .set(true);
268
+
269
+ parser.helpful();
270
+
271
+ var read = function (_) {
272
+ if (_.read && !Object.prototype.hasOwnProperty.call(_, 'read')) {
273
+ return _.read();
274
+ } else {
275
+ return _;
276
+ }
277
+ };
278
+
279
+ var collect = function (_) {
280
+ if (
281
+ !util.no(_) &&
282
+ _.forEach &&
283
+ !Object.prototype.hasOwnProperty.call(_, 'forEach') &&
284
+ typeof _ != "string" // deprecated
285
+ ) {
286
+ var result = [];
287
+ _.forEach(function (_) {
288
+ result.push(_);
289
+ });
290
+ return result;
291
+ } else {
292
+ return _;
293
+ }
294
+ };
295
+
296
+ var block = function (_, mode, block) {
297
+ if (mode == "lines") {
298
+ if (typeof _ == "string") {
299
+ system.stderr.print("Cannot operate on strings in line mode.");
300
+ os.exit(-1);
301
+ }
302
+ var result = [];
303
+ _.forEach(function (_) {
304
+ result.push(block(_));
305
+ });
306
+ return result;
307
+ } else {
308
+ return block(_);
309
+ }
310
+ };
311
+
312
+ exports.main = function (system) {
313
+ var options = {};
314
+ options._ = system.stdin;
315
+ options.mode = "all";
316
+ options.out = system.stdout;
317
+ parser.parse(system.args, options);
318
+ if (typeof options._ == "number")
319
+ os.exit(options._);
320
+ };
321
+
322
+ if (require.main == module.id)
323
+ exports.main(system);
324
+