typenative 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/bin/node/assert.js +2 -2
  2. package/bin/node/child_process.js +66 -8
  3. package/bin/node/fs.js +1012 -4
  4. package/bin/node/histogram.js +1991 -0
  5. package/bin/node/process.js +14 -2
  6. package/bin/node/sqlite.js +1488 -5
  7. package/bin/node/stream.js +2 -1
  8. package/bin/node/system_extra.js +3 -270
  9. package/bin/node/test.js +39 -1
  10. package/bin/node/test_report.js +47 -1
  11. package/bin/node/test_run.js +57 -2
  12. package/bin/node/util.js +8 -5
  13. package/bin/node/webassembly.js +20 -5
  14. package/bin/node/webview.js +3257 -244
  15. package/bin/node/zlib.js +118 -14
  16. package/bin/node/zlib_iter.js +585 -0
  17. package/bin/transpiler.js +1370 -258
  18. package/bin/version.js +1 -1
  19. package/govendor/software.sslmate.com/src/go-pkcs12/.gitattributes +10 -0
  20. package/govendor/software.sslmate.com/src/go-pkcs12/LICENSE +28 -0
  21. package/govendor/software.sslmate.com/src/go-pkcs12/README.md +31 -0
  22. package/govendor/software.sslmate.com/src/go-pkcs12/bmp-string.go +63 -0
  23. package/govendor/software.sslmate.com/src/go-pkcs12/crypto.go +331 -0
  24. package/govendor/software.sslmate.com/src/go-pkcs12/errors.go +23 -0
  25. package/govendor/software.sslmate.com/src/go-pkcs12/go.mod +3 -0
  26. package/govendor/software.sslmate.com/src/go-pkcs12/internal/rc2/rc2.go +271 -0
  27. package/govendor/software.sslmate.com/src/go-pkcs12/mac.go +202 -0
  28. package/govendor/software.sslmate.com/src/go-pkcs12/pbkdf.go +184 -0
  29. package/govendor/software.sslmate.com/src/go-pkcs12/pkcs12.go +1000 -0
  30. package/govendor/software.sslmate.com/src/go-pkcs12/safebags.go +107 -0
  31. package/package.json +10 -1
  32. package/types/tsconfig.json +17 -0
  33. package/types/typenative-npm.d.ts +492 -12
  34. package/types/typenative.d.ts +88 -2
  35. package/.gitattributes +0 -1
  36. package/.github/workflows/ci.yml +0 -68
  37. package/.github/workflows/npm-publish-github-packages.yml +0 -88
  38. package/CHANGELOG.md +0 -1746
  39. package/CONTRIBUTION.md +0 -133
  40. package/conformance.md +0 -1497
  41. package/examples/README.md +0 -114
  42. package/examples/angular-templates/README.md +0 -141
  43. package/examples/angular-templates/bench.mjs +0 -173
  44. package/examples/angular-templates/dom-type-globals.d.ts +0 -21
  45. package/examples/angular-templates/dom-type-stubs.ts +0 -21
  46. package/examples/angular-templates/main.ts +0 -135
  47. package/examples/angular-templates/parity.mjs +0 -157
  48. package/examples/angular-templates/run-node.mjs +0 -69
  49. package/examples/angular-templates/vendor/angular/compiler/LICENSE +0 -21
  50. package/examples/desktop-notes/README.md +0 -106
  51. package/examples/desktop-notes/bench.mjs +0 -125
  52. package/examples/desktop-notes/main.ts +0 -131
  53. package/examples/desktop-notes/package-lock.json +0 -726
  54. package/examples/desktop-notes/package.json +0 -9
  55. package/examples/desktop-notes/ui.ts +0 -176
  56. package/examples/dupe-finder/README.md +0 -78
  57. package/examples/dupe-finder/bench.mjs +0 -120
  58. package/examples/dupe-finder/main.ts +0 -87
  59. package/examples/fibonacci/README.md +0 -89
  60. package/examples/fibonacci/bench.mjs +0 -87
  61. package/examples/fibonacci/main.ts +0 -21
  62. package/examples/hello-world/README.md +0 -62
  63. package/examples/hello-world/bench.mjs +0 -66
  64. package/examples/hello-world/main.ts +0 -7
  65. package/examples/json-server/README.md +0 -132
  66. package/examples/json-server/bench.mjs +0 -263
  67. package/examples/json-server/main.ts +0 -67
  68. package/examples/mandelbrot-png/README.md +0 -63
  69. package/examples/mandelbrot-png/bench.mjs +0 -95
  70. package/examples/mandelbrot-png/main.ts +0 -98
  71. package/examples/mandelbrot-png/preview.png +0 -0
  72. package/examples/marked-cli/README.md +0 -174
  73. package/examples/marked-cli/bench.mjs +0 -178
  74. package/examples/marked-cli/main.ts +0 -410
  75. package/examples/marked-cli/parity.mjs +0 -185
  76. package/examples/marked-cli/vendor/marked/LICENSE +0 -44
  77. package/examples/marked-cli/vendor/marked/VENDOR.md +0 -26
  78. package/examples/openai-chat/README.md +0 -148
  79. package/examples/openai-chat/bench.mjs +0 -175
  80. package/examples/openai-chat/main.ts +0 -488
  81. package/examples/react-compiler/README.md +0 -185
  82. package/examples/react-compiler/bench.mjs +0 -181
  83. package/examples/react-compiler/main.ts +0 -324
  84. package/examples/react-compiler/parity.mjs +0 -362
  85. package/examples/react-compiler/run-node.mjs +0 -90
  86. package/examples/react-compiler/vendor/babel/LICENSE +0 -22
  87. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +0 -15
  88. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +0 -51
  89. package/examples/react-compiler/vendor/charcodes/package.json +0 -25
  90. package/examples/react-compiler/vendor/invariant/LICENSE +0 -21
  91. package/examples/react-compiler/vendor/invariant/invariant.cjs +0 -51
  92. package/examples/react-compiler/vendor/invariant/invariant.d.cts +0 -11
  93. package/examples/react-compiler/vendor/invariant/types/LICENSE +0 -21
  94. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +0 -19
  95. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +0 -19
  96. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +0 -19
  97. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +0 -19
  98. package/examples/react-compiler/vendor/js-tokens/LICENSE +0 -21
  99. package/examples/react-compiler/vendor/js-tokens/index.cjs +0 -23
  100. package/examples/react-compiler/vendor/js-tokens/index.d.cts +0 -13
  101. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +0 -20
  102. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +0 -337
  103. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +0 -384
  104. package/examples/react-compiler/vendor/jsesc/types/LICENSE +0 -21
  105. package/examples/react-compiler/vendor/picocolors/LICENSE +0 -15
  106. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +0 -75
  107. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +0 -5
  108. package/examples/react-compiler/vendor/picocolors/types.d.ts +0 -51
  109. package/examples/react-compiler/vendor/react-compiler/LICENSE +0 -21
  110. package/examples/react-compiler/vendor/stubs/babel-core.ts +0 -31
  111. package/examples/react-compiler/vendor/stubs/debug.ts +0 -18
  112. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +0 -13
  113. package/examples/react-compiler/vendor/stubs/pretty-format.ts +0 -18
  114. package/examples/react-compiler/vendor/zod/LICENSE +0 -21
  115. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +0 -9
  116. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +0 -5
  117. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +0 -49
  118. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +0 -48
  119. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +0 -50
  120. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +0 -6
  121. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +0 -126
  122. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +0 -39
  123. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +0 -12
  124. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +0 -78
  125. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +0 -2
  126. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +0 -12
  127. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +0 -12
  128. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +0 -131
  129. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +0 -54
  130. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +0 -12
  131. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +0 -40
  132. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +0 -12
  133. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +0 -76
  134. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +0 -76
  135. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +0 -27
  136. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +0 -27
  137. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +0 -13
  138. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +0 -61
  139. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +0 -38
  140. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +0 -20
  141. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +0 -5
  142. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +0 -8
  143. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +0 -11
  144. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +0 -20
  145. package/examples/react-compiler/vendor.mjs +0 -688
  146. package/examples/todo-cli/README.md +0 -69
  147. package/examples/todo-cli/bench.mjs +0 -89
  148. package/examples/todo-cli/main.ts +0 -92
  149. package/examples/yaml-cli/README.md +0 -152
  150. package/examples/yaml-cli/bench.mjs +0 -265
  151. package/examples/yaml-cli/main.ts +0 -251
  152. package/examples/yaml-cli/parity.mjs +0 -156
  153. package/examples/yaml-cli/vendor/yaml/LICENSE +0 -13
  154. package/examples/yaml-cli/vendor/yaml/VENDOR.md +0 -25
  155. package/performance/README.md +0 -248
  156. package/performance/any-dynamic.ts +0 -21
  157. package/performance/any-record-view.ts +0 -20
  158. package/performance/arr-for-of.ts +0 -29
  159. package/performance/arr-hof.ts +0 -21
  160. package/performance/arr-push-index.ts +0 -15
  161. package/performance/arr-push-local.ts +0 -39
  162. package/performance/arr-slice-concat.ts +0 -24
  163. package/performance/arr-sort.ts +0 -21
  164. package/performance/async-await.ts +0 -24
  165. package/performance/async-promise-all.ts +0 -38
  166. package/performance/bigint-arith.ts +0 -36
  167. package/performance/buf-encode.ts +0 -29
  168. package/performance/class-accessors.ts +0 -48
  169. package/performance/class-dispatch.ts +0 -52
  170. package/performance/date-format.ts +0 -18
  171. package/performance/events-emit.ts +0 -27
  172. package/performance/exc-throw-rare.ts +0 -25
  173. package/performance/exc-try-catch.ts +0 -26
  174. package/performance/fn-closures.ts +0 -29
  175. package/performance/fn-recursion-tree.ts +0 -31
  176. package/performance/fn-rest-spread.ts +0 -43
  177. package/performance/gen-for-of.ts +0 -46
  178. package/performance/gen-next.ts +0 -32
  179. package/performance/iter-helpers.ts +0 -38
  180. package/performance/json-any.ts +0 -30
  181. package/performance/json-roundtrip.ts +0 -27
  182. package/performance/map-delete.ts +0 -24
  183. package/performance/map-iterate.ts +0 -26
  184. package/performance/map-number-keys.ts +0 -37
  185. package/performance/map-string-keys.ts +0 -22
  186. package/performance/num-arith-loop.ts +0 -14
  187. package/performance/num-bitwise.ts +0 -21
  188. package/performance/num-hash-imul.ts +0 -36
  189. package/performance/num-mod-int.ts +0 -16
  190. package/performance/num-to-string.ts +0 -16
  191. package/performance/obj-alloc.ts +0 -32
  192. package/performance/obj-keys-entries.ts +0 -44
  193. package/performance/obj-optional-chain.ts +0 -42
  194. package/performance/obj-record-dict.ts +0 -24
  195. package/performance/obj-spread-destructure.ts +0 -40
  196. package/performance/regex-exec.ts +0 -29
  197. package/performance/regex-replace-fn.ts +0 -24
  198. package/performance/regex-site.ts +0 -28
  199. package/performance/set-ops.ts +0 -20
  200. package/performance/str-char-scan.ts +0 -35
  201. package/performance/str-concat-plus.ts +0 -18
  202. package/performance/str-concat.ts +0 -24
  203. package/performance/str-methods.ts +0 -24
  204. package/performance/str-short-scan.ts +0 -26
  205. package/performance/str-split-join.ts +0 -23
  206. package/performance/str-switch-compare.ts +0 -50
  207. package/performance/str-template.ts +0 -16
  208. package/performance/str-unicode.ts +0 -25
  209. package/performance/typed-array.ts +0 -30
  210. package/performance/union-switch.ts +0 -57
  211. package/performance/union-typeof.ts +0 -40
  212. package/performance/wasm-call.ts +0 -63
  213. package/tsparser/check.go +0 -158
  214. package/tsparser/go.mod +0 -14
  215. package/tsparser/go.sum +0 -22
  216. package/tsparser/main.go +0 -363
  217. package/tsparser/tsparser.exe +0 -4
@@ -1,69 +0,0 @@
1
- # Todo CLI
2
-
3
- A to-do list for the terminal that keeps its items in a JSON file. Tools like
4
- this run dozens of times a day, sometimes from shell prompts or git hooks.
5
- Each call does a little work, so most of the wait is **process startup**.
6
- That's where a native binary is a big improvement.
7
-
8
- Demonstrates: `process.argv` destructuring with a default and a rest element,
9
- `node:fs` reads and atomic writes (write a temp file, then `renameSync`),
10
- `os.homedir()`, `process.env`, `JSON.parse` / `JSON.stringify`, interfaces and
11
- `process.exit` codes.
12
-
13
- ## Usage
14
-
15
- ```sh
16
- todo add Buy milk # added #1: Buy milk
17
- todo add Write docs # added #2: Write docs
18
- todo done 1 # done #1: Buy milk
19
- todo # same as `todo list`
20
- # [x] 1 Buy milk
21
- # [ ] 2 Write docs
22
- #
23
- # 1 open, 1 done
24
- todo rm 2 # removed #2: Write docs
25
- ```
26
-
27
- Items are stored in `~/.todo.json`. Set `TODO_FILE` to use another file.
28
-
29
- ## Run it
30
-
31
- With Node.js:
32
-
33
- ```sh
34
- node main.ts add Buy milk
35
- ```
36
-
37
- With TypeNative, build a binary and put it on your `PATH`:
38
-
39
- ```sh
40
- npx typenative --source main.ts --output dist/todo.exe --release
41
- ./dist/todo.exe add Buy milk
42
- ```
43
-
44
- From a checkout of this repository:
45
-
46
- ```sh
47
- node bin/cli.js --source examples/todo-cli/main.ts --output dist/todo.exe
48
- ```
49
-
50
- ## Benchmark: Node.js vs TypeNative
51
-
52
- Reproduce with `node bench.mjs [runs=30]`. The script compiles `main.ts`, fills
53
- a temp file with 50 items for each runtime, and then times **complete process
54
- runs** (spawn to exit) of `todo list` and `todo add`.
55
-
56
- **Environment:** Windows 11 Pro, AMD Ryzen 5 7600, Node v26.13.0, Go 1.27.4,
57
- TypeNative 0.7.0, default (non-`--release`) build.
58
-
59
- | Command | Runtime | min | median | mean | max |
60
- |-------------|------------|-----------|-----------|-----------|-----------|
61
- | `todo list` | Node.js | 50.77 ms | 54.76 ms | 56.57 ms | 67.76 ms |
62
- | `todo list` | TypeNative | 5.12 ms | 5.37 ms | 9.80 ms | 137.30 ms |
63
- | `todo add` | Node.js | 51.89 ms | 53.94 ms | 56.75 ms | 69.28 ms |
64
- | `todo add` | TypeNative | 5.88 ms | 6.40 ms | 6.48 ms | 9.31 ms |
65
-
66
- **TypeNative answers each command 8.4–10.2x faster** (median). At about 6 ms,
67
- a call feels instant. At about 55 ms, there's a noticeable delay, and it adds up
68
- when the tool runs from a prompt or a hook on every keystroke or commit. The
69
- binary is a single 4.8 MB file, with no Node install and no `node_modules`.
@@ -1,89 +0,0 @@
1
- // Per-command latency benchmark: Node.js vs TypeNative for the todo CLI.
2
- //
3
- // Compiles main.ts to a native binary with the TypeNative CLI from this repo
4
- // checkout (../../bin/cli.js), then times complete process runs (spawn to
5
- // exit) of `todo list` and `todo add` against a 50-item list in a temp file.
6
- // Each command does a little file I/O and JSON work, so this measures what a
7
- // user waits for on every invocation: startup plus a small amount of real work.
8
- //
9
- // Usage: node bench.mjs [runs=30]
10
-
11
- import { spawnSync } from 'node:child_process';
12
- import fs from 'node:fs';
13
- import os from 'node:os';
14
- import path from 'node:path';
15
- import { fileURLToPath } from 'node:url';
16
-
17
- const here = path.dirname(fileURLToPath(import.meta.url));
18
- const repoRoot = path.resolve(here, '..', '..');
19
- const cli = path.join(repoRoot, 'bin', 'cli.js');
20
- const runs = Number(process.argv[2] ?? '30') || 30;
21
- const exeName = process.platform === 'win32' ? 'todo.exe' : 'todo';
22
- const exePath = path.join(here, 'dist', exeName);
23
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'todo-bench-'));
24
-
25
- function stats(samples) {
26
- const sorted = [...samples].sort((a, b) => a - b);
27
- const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
28
- return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
29
- }
30
-
31
- function fmt(ms) {
32
- return `${ms.toFixed(2)} ms`.padStart(11);
33
- }
34
-
35
- // A fresh 50-item list for each runtime, so `add` runs grow both the same way
36
- function seed(file) {
37
- const items = Array.from({ length: 50 }, (_, i) => ({
38
- id: i + 1,
39
- text: `Task number ${i + 1}`,
40
- done: i % 3 === 0,
41
- created: new Date(0).toISOString(),
42
- }));
43
- fs.writeFileSync(file, JSON.stringify({ nextId: 51, items }, null, 2));
44
- }
45
-
46
- function timeRuns(command, args, file) {
47
- const samples = [];
48
- for (let i = 0; i < runs; i++) {
49
- const t0 = performance.now();
50
- const result = spawnSync(command, args, { cwd: here, encoding: 'utf8', env: { ...process.env, TODO_FILE: file } });
51
- const elapsed = performance.now() - t0;
52
- if (result.status !== 0) {
53
- console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
54
- process.exit(1);
55
- }
56
- samples.push(elapsed);
57
- }
58
- return stats(samples);
59
- }
60
-
61
- // 1. Compile the CLI to a native binary.
62
- fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
63
- console.log('Compiling main.ts to a native binary with TypeNative...');
64
- const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
65
- cwd: here,
66
- stdio: 'inherit',
67
- });
68
- if (build.status !== 0) process.exit(build.status ?? 1);
69
-
70
- // 2. Time `list` and `add` under both runtimes, each on its own list file.
71
- const nodeFile = path.join(tmpDir, 'node.json');
72
- const nativeFile = path.join(tmpDir, 'native.json');
73
- seed(nodeFile);
74
- seed(nativeFile);
75
- console.log(`\nTiming ${runs} full process runs of each command...\n`);
76
- const results = [
77
- ['list', timeRuns(process.execPath, ['main.ts', 'list'], nodeFile), timeRuns(exePath, ['list'], nativeFile)],
78
- ['add', timeRuns(process.execPath, ['main.ts', 'add', 'Bench item'], nodeFile), timeRuns(exePath, ['add', 'Bench item'], nativeFile)],
79
- ];
80
- fs.rmSync(tmpDir, { recursive: true, force: true });
81
-
82
- for (const [command, nodeStats, nativeStats] of results) {
83
- console.log(`\`todo ${command}\`, ${runs} runs (full process: spawn -> exit):\n`);
84
- console.log('| Runtime | min | median | mean | max |');
85
- console.log('|------------|-------------|-------------|-------------|-------------|');
86
- console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
87
- console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
88
- console.log(`\nTypeNative is ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster per call (median).\n`);
89
- }
@@ -1,92 +0,0 @@
1
- // A to-do list for the terminal, kept in a JSON file. Commands are meant to be
2
- // run many times a day (or from shell hooks), so process startup dominates
3
- // every call: that is where a native binary pays off.
4
- //
5
- // todo add Buy milk add an item
6
- // todo list show open and done items
7
- // todo done 2 mark item 2 as done
8
- // todo rm 2 delete item 2
9
- //
10
- // Items live in ~/.todo.json; set TODO_FILE to use another file.
11
- import fs from 'node:fs';
12
- import os from 'node:os';
13
- import path from 'node:path';
14
-
15
- interface Item {
16
- id: number;
17
- text: string;
18
- done: boolean;
19
- created: string;
20
- }
21
-
22
- interface Store {
23
- nextId: number;
24
- items: Item[];
25
- }
26
-
27
- const file = process.env.TODO_FILE ?? path.join(os.homedir(), '.todo.json');
28
-
29
- function load(): Store {
30
- if (!fs.existsSync(file)) return { nextId: 1, items: [] };
31
- return JSON.parse(fs.readFileSync(file, 'utf8')) as Store;
32
- }
33
-
34
- // Write to a temp file, then rename over the old one: a crash mid-write never
35
- // leaves a half-written list behind
36
- function save(store: Store): void {
37
- const tmp = `${file}.tmp`;
38
- fs.writeFileSync(tmp, JSON.stringify(store, null, 2));
39
- fs.renameSync(tmp, file);
40
- }
41
-
42
- function findItem(store: Store, arg: string | undefined): Item {
43
- const id = Number(arg);
44
- const item = store.items.find((i) => i.id === id);
45
- if (!item) {
46
- console.error(`todo: no item with id ${arg ?? '(none)'}`);
47
- process.exit(1);
48
- }
49
- return item;
50
- }
51
-
52
- function usage(): void {
53
- console.log('usage: todo add <text> | list | done <id> | rm <id>');
54
- }
55
-
56
- const [command = 'list', ...rest] = process.argv.slice(2);
57
- const store = load();
58
-
59
- if (command === 'add') {
60
- const text = rest.join(' ').trim();
61
- if (text === '') {
62
- usage();
63
- process.exit(1);
64
- }
65
- const item: Item = { id: store.nextId, text: text, done: false, created: new Date().toISOString() };
66
- store.nextId += 1;
67
- store.items.push(item);
68
- save(store);
69
- console.log(`added #${item.id}: ${item.text}`);
70
- } else if (command === 'list') {
71
- if (store.items.length === 0) {
72
- console.log('nothing to do');
73
- }
74
- for (const item of store.items) {
75
- console.log(`${item.done ? '[x]' : '[ ]'} ${String(item.id).padStart(3)} ${item.text}`);
76
- }
77
- const open = store.items.filter((i) => !i.done).length;
78
- if (store.items.length > 0) console.log(`\n${open} open, ${store.items.length - open} done`);
79
- } else if (command === 'done') {
80
- const item = findItem(store, rest[0]);
81
- item.done = true;
82
- save(store);
83
- console.log(`done #${item.id}: ${item.text}`);
84
- } else if (command === 'rm') {
85
- const item = findItem(store, rest[0]);
86
- store.items = store.items.filter((i) => i.id !== item.id);
87
- save(store);
88
- console.log(`removed #${item.id}: ${item.text}`);
89
- } else {
90
- usage();
91
- process.exit(1);
92
- }
@@ -1,152 +0,0 @@
1
- # yaml CLI
2
-
3
- The [`yaml`](https://github.com/eemeli/yaml) package's own command-line tool,
4
- compiled from its **unmodified** upstream TypeScript source: about 11,000
5
- lines across 78 files, vendored as is in [vendor/yaml/](vendor/yaml/) (see
6
- [VENDOR.md](vendor/yaml/VENDOR.md): no patches). [main.ts](main.ts) ports
7
- upstream's `bin.js` to TypeScript. It hands stdin and the arguments to the
8
- library's `cli()` and turns the error it reports into an exit code. For
9
- `yaml --json` on a large, plain stream it splits the input before the `---`
10
- lines that start its documents and composes runs of them in parallel workers,
11
- then joins their JSON arrays; any directive, error, warning, CR line ending or
12
- non-scalar map key sends the whole input back through `cli()`.
13
-
14
- This is a test of TypeNative on real code nobody wrote for it: a lexer and
15
- parser driven by sticky regexes, class hierarchies with overrides (an
16
- `Array` subclass among them), structural object types passed between
17
- modules, `Object.assign` and `Object.defineProperty` on class instances,
18
- Maps keyed by nodes, and JSON output whose key order and number formatting
19
- must match V8's.
20
-
21
- ## Usage
22
-
23
- ```sh
24
- yaml < in.yaml # re-emit the YAML stream
25
- yaml --json < in.yaml # print it as JSON
26
- yaml valid < in.yaml # exit 1 on the first error
27
- yaml lex < in.yaml # print the lexer's tokens
28
- yaml cst < in.yaml # print the concrete syntax tree
29
- yaml --help # all options (--indent, --single, --strict, …)
30
- ```
31
-
32
- ## Run it
33
-
34
- With Node.js (24 or later runs the TypeScript directly):
35
-
36
- ```sh
37
- node main.ts --json < docker-compose.yml
38
- ```
39
-
40
- With TypeNative:
41
-
42
- ```sh
43
- npx typenative --source main.ts --output dist/yaml
44
- ./dist/yaml --json < docker-compose.yml
45
- ```
46
-
47
- From a checkout of this repository:
48
-
49
- ```sh
50
- node bin/cli.js --source examples/yaml-cli/main.ts --output dist/yaml
51
- ```
52
-
53
- ## Parity with Node.js
54
-
55
- Reproduce with `node parity.mjs [filter]`. The script fetches the
56
- [yaml-test-suite](https://github.com/yaml/yaml-test-suite) at tag
57
- `data-2022-01-17` into `.cache/` (it isn't committed) and compiles `main.ts`.
58
- It then feeds each test's `in.yaml` to both runtimes in each of four modes:
59
- default (re-emit), `--json`, `valid` and `lex`. stdout, stderr and the exit
60
- code must match byte for byte. The only normalization is on stderr: Node's
61
- ` at …` stack frames are dropped (a native binary has no JS frames to
62
- print), and warnings' `(node:PID)` becomes `(node)`. Failures are written to
63
- `.cache/failures/`.
64
-
65
- ```
66
- 1608/1608 cases match (100.00%)
67
- ```
68
-
69
- That's 402 inputs × 4 modes. They include the error cases: invalid YAML
70
- reports the same `YAMLParseError`s (message, code, positions and the
71
- source excerpt with its caret line) and exits with the same code. Warnings
72
- such as `Keys with collection values will be stringified…` print the same
73
- `process.emitWarning` lines.
74
-
75
- ## Benchmark: Node.js vs TypeNative
76
-
77
- Reproduce with `node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]`.
78
- The script compiles `main.ts`, then times **complete process runs** (spawn to
79
- exit) of `yaml --json` under both runtimes, alternating Node and native runs,
80
- and checks that the two JSON outputs are identical:
81
-
82
- - **(a) per-call latency:** a 1.4 KB docker-compose.yml (five services,
83
- anchors, flow collections, a folded scalar), the kind of file a script or a
84
- git hook converts once.
85
- - **(b) throughput:** 10 MB of generated Kubernetes manifests (13,140
86
- Deployments, Services and ConfigMaps in one stream, from a fixed-seed
87
- generator, so every machine gets the same input).
88
-
89
- **Environment:** Windows 11 Pro, AMD Ryzen 5 7600 (6 cores, 12 threads),
90
- Node v26.13.0, Go 1.27.4, TypeNative 0.7.0, default
91
- (non-`--release`) build.
92
-
93
- (a) `yaml --json < docker-compose.yml`, 30 runs each:
94
-
95
- | Runtime | min | median | mean | max |
96
- | ---------- | --------- | --------- | --------- | --------- |
97
- | Node.js | 104.46 ms | 106.84 ms | 108.99 ms | 121.29 ms |
98
- | TypeNative | 8.84 ms | 9.43 ms | 10.51 ms | 43.75 ms |
99
-
100
- (b) `yaml --json < manifests.yaml` (10.0 MB), 5 runs each:
101
-
102
- | Runtime | min | median | mean | max | MB/s (median) |
103
- | ---------- | -------- | -------- | -------- | -------- | ------------- |
104
- | Node.js | 852.59 ms | 862.44 ms | 859.11 ms | 864.16 ms | 11.60 |
105
- | TypeNative | 762.03 ms | 779.90 ms | 776.40 ms | 781.36 ms | 12.83 |
106
-
107
- **Per call, TypeNative is 11.3x faster** (9 ms vs 107 ms, median). Node's
108
- cost here is mostly startup. It loads and type-strips the 78 TypeScript
109
- files on every run before it reads a byte of input. That's what you feel
110
- when a tool calls the converter once per file.
111
-
112
- **On bulk throughput the two are close** (780 ms vs 862 ms, median; 12.8 vs
113
- 11.6 MB/s). Both runtimes take the split-and-workers path for this input, so
114
- Node pays six worker startups while the native binary pays almost none; that
115
- largely cancels the native single-process deficit in this benchmark. Without
116
- the workers, the native process takes about 3.1 s for the same 10 MB against
117
- Node's 1.5 s.
118
- Once V8's JIT has warmed up, it runs this dynamic object-heavy code faster:
119
- the library passes structural object types and unions (`Scalar | YAMLMap |
120
- YAMLSeq | Pair`, CST tokens) that TypeNative holds as `interface{}`, so the
121
- cost is spread across property reads, conversions and allocation. Garbage
122
- collection runs on other cores and hardly affects the wall clock.
123
-
124
- Getting here fixed performance bugs in TypeNative's runtime, all general:
125
-
126
- - Long strings' code-point indexes were evicted by the many short slices a
127
- lexer takes, so every sticky-regex match rebuilt the index of the whole
128
- input. That made lexing quadratic.
129
- - Properties of objects held as any are read and written through plans cached
130
- per type and key, with no lock, string hash or reflection on a hit. A read
131
- that's consumed on the spot, such as a comparison, doesn't box the value.
132
- - `obj.list.push(x)` on an object held as any appends in place. It used to
133
- convert the whole array on every push, which made building a list quadratic.
134
- - Single-value pushes grow the backing slice by 2x, not Go's 1.25x: building
135
- the lexer's 3.2 million tokens copies the array out about five times over
136
- otherwise. This alone cuts roughly 40 ms from lexing and 50 ms from compose.
137
- - Conversions between object types are compiled once per pair of types; a
138
- fresh local object converted once copies without an identity entry, and a
139
- conversion between two statically known struct types is a struct literal.
140
- Conversion identity holds one weak pointer per entry and sweeps dead ones
141
- in batches.
142
- - `Object.assign({ defaults }, typedSource)` sets fields directly instead of
143
- reflecting over them, `obj[key] = v` on a plain object skips reflection,
144
- and `s += chunk` for an untyped chunk appends in place.
145
- - An optional call or chain whose value is only tested returns its primitive
146
- directly instead of boxing it into a pointer first.
147
- - `switch` on a value held as any compiles to a string switch when its cases
148
- are string literals.
149
- - Regexes run on a VM that works on byte offsets, so a sticky match on a 10 MB
150
- input costs no more than one on a short string.
151
- - `typeof f` types are func types, so the composer's callbacks are called
152
- directly instead of through reflection.
@@ -1,265 +0,0 @@
1
- // Benchmark: Node.js vs TypeNative for the yaml CLI, both converting YAML to
2
- // JSON (`yaml --json`).
3
- //
4
- // Compiles main.ts to a native binary with the TypeNative CLI from this repo
5
- // checkout (../../bin/cli.js), then measures:
6
- // (a) per-call latency: complete process runs (spawn to exit) on a small
7
- // docker-compose.yml, the kind of file a script or hook converts once;
8
- // (b) throughput: one run on about 10 MB of generated Kubernetes-style
9
- // manifests (Deployments, Services and ConfigMaps in one stream).
10
- // Runs alternate Node and native, so drift on the machine hits both alike.
11
- // Both runtimes' JSON must be identical, or the benchmark stops.
12
- //
13
- // Usage: node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]
14
-
15
- import { spawnSync } from 'node:child_process';
16
- import fs from 'node:fs';
17
- import path from 'node:path';
18
- import { fileURLToPath } from 'node:url';
19
-
20
- const here = path.dirname(fileURLToPath(import.meta.url));
21
- const repoRoot = path.resolve(here, '..', '..');
22
- const cli = path.join(repoRoot, 'bin', 'cli.js');
23
- const cache = path.join(here, '.cache');
24
- const exePath = path.join(cache, process.platform === 'win32' ? 'yaml.exe' : 'yaml');
25
- const runs = Number(process.argv[2] ?? '30') || 30;
26
- const throughputRuns = Number(process.argv[3] ?? '5') || 5;
27
- const megabytes = Number(process.argv[4] ?? '10') || 10;
28
-
29
- const COMPOSE = `# A small web app: reverse proxy, API, worker, database and cache
30
- services:
31
- proxy:
32
- image: nginx:1.27-alpine
33
- ports: ["80:80", "443:443"]
34
- volumes:
35
- - ./nginx.conf:/etc/nginx/nginx.conf:ro
36
- - certs:/etc/letsencrypt
37
- depends_on: [api]
38
- restart: unless-stopped
39
- api:
40
- build:
41
- context: ./api
42
- args:
43
- NODE_ENV: production
44
- environment:
45
- DATABASE_URL: postgres://app:secret@db:5432/app
46
- REDIS_URL: redis://cache:6379/0
47
- LOG_LEVEL: info
48
- WORKERS: 4
49
- healthcheck:
50
- test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
51
- interval: 30s
52
- timeout: 5s
53
- retries: 3
54
- depends_on:
55
- db: { condition: service_healthy }
56
- cache: { condition: service_started }
57
- worker:
58
- build: ./api
59
- command: node dist/worker.js
60
- environment: &worker-env
61
- QUEUE: default
62
- CONCURRENCY: 8
63
- deploy:
64
- replicas: 2
65
- resources:
66
- limits: { cpus: "0.50", memory: 512M }
67
- db:
68
- image: postgres:16
69
- environment:
70
- POSTGRES_USER: app
71
- POSTGRES_PASSWORD: secret
72
- POSTGRES_DB: app
73
- volumes: [pgdata:/var/lib/postgresql/data]
74
- healthcheck:
75
- test: pg_isready -U app
76
- interval: 10s
77
- cache:
78
- image: redis:7-alpine
79
- command: >
80
- redis-server
81
- --maxmemory 256mb
82
- --maxmemory-policy allkeys-lru
83
- volumes:
84
- pgdata: {}
85
- certs: {}
86
- `;
87
-
88
- // Deterministic pseudo-random numbers (a fixed-seed LCG): the same manifests
89
- // on every run and every machine
90
- let seed = 42;
91
- function rand(n) {
92
- seed = (seed * 1103515245 + 12345) % 2147483648;
93
- return seed % n;
94
- }
95
-
96
- const APPS = ['checkout', 'catalog', 'payments', 'search', 'auth', 'billing', 'notifications', 'inventory'];
97
- const TEAMS = ['core', 'growth', 'platform', 'data'];
98
-
99
- function deployment(name, i) {
100
- const team = TEAMS[rand(TEAMS.length)];
101
- const env = Array.from({ length: 6 }, (_, k) => ` - name: FEATURE_${k}_ENABLED\n value: "${rand(2) === 1}"`).join('\n');
102
- return `apiVersion: apps/v1
103
- kind: Deployment
104
- metadata:
105
- name: ${name}
106
- namespace: ${team}
107
- labels:
108
- app.kubernetes.io/name: ${name}
109
- app.kubernetes.io/part-of: shop
110
- team: ${team}
111
- annotations:
112
- deployment.kubernetes.io/revision: "${1 + rand(40)}"
113
- spec:
114
- replicas: ${1 + rand(12)}
115
- selector:
116
- matchLabels:
117
- app.kubernetes.io/name: ${name}
118
- strategy:
119
- type: RollingUpdate
120
- rollingUpdate: { maxSurge: 25%, maxUnavailable: 0 }
121
- template:
122
- metadata:
123
- labels:
124
- app.kubernetes.io/name: ${name}
125
- version: v${1 + rand(5)}.${rand(20)}.${rand(100)}
126
- spec:
127
- serviceAccountName: ${name}
128
- containers:
129
- - name: app
130
- image: registry.example.com/shop/${name}:${(i * 7919).toString(16)}
131
- imagePullPolicy: IfNotPresent
132
- ports:
133
- - { name: http, containerPort: 8080, protocol: TCP }
134
- - { name: metrics, containerPort: 9090, protocol: TCP }
135
- args: ["--port=8080", "--log-format=json", "--shutdown-grace=${10 + rand(50)}s"]
136
- env:
137
- - name: POD_NAME
138
- valueFrom:
139
- fieldRef: { fieldPath: metadata.name }
140
- ${env}
141
- resources:
142
- requests: { cpu: ${50 + rand(450)}m, memory: ${64 + rand(448)}Mi }
143
- limits: { cpu: "${1 + rand(3)}", memory: ${512 + rand(1536)}Mi }
144
- readinessProbe:
145
- httpGet: { path: /ready, port: http }
146
- initialDelaySeconds: ${rand(15)}
147
- periodSeconds: 10
148
- livenessProbe:
149
- httpGet: { path: /healthz, port: http }
150
- failureThreshold: 3
151
- `;
152
- }
153
-
154
- function service(name, team) {
155
- return `apiVersion: v1
156
- kind: Service
157
- metadata:
158
- name: ${name}
159
- namespace: ${team}
160
- spec:
161
- type: ClusterIP
162
- selector:
163
- app.kubernetes.io/name: ${name}
164
- ports:
165
- - name: http
166
- port: 80
167
- targetPort: http
168
- `;
169
- }
170
-
171
- function configMap(name, team) {
172
- const lines = Array.from({ length: 8 }, (_, k) => ` key_${k} = ${rand(100000) / 100}`).join('\n');
173
- return `apiVersion: v1
174
- kind: ConfigMap
175
- metadata:
176
- name: ${name}-config
177
- namespace: ${team}
178
- data:
179
- LOG_LEVEL: ${['debug', 'info', 'warn'][rand(3)]}
180
- TIMEOUT_MS: "${100 * (1 + rand(50))}"
181
- app.properties: |
182
- ${lines}
183
- `;
184
- }
185
-
186
- function manifests(bytes) {
187
- const docs = [];
188
- let size = 0;
189
- for (let i = 0; size < bytes; i++) {
190
- const name = `${APPS[i % APPS.length]}-${i}`;
191
- const team = TEAMS[i % TEAMS.length];
192
- for (const doc of [deployment(name, i), service(name, team), configMap(name, team)]) {
193
- docs.push(doc);
194
- size += doc.length + 4;
195
- }
196
- }
197
- return docs.join('---\n');
198
- }
199
-
200
- function stats(samples) {
201
- const sorted = [...samples].sort((a, b) => a - b);
202
- const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
203
- return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
204
- }
205
-
206
- function fmt(ms) {
207
- return `${ms.toFixed(2)} ms`.padStart(13);
208
- }
209
-
210
- function runOnce(command, args, input) {
211
- const t0 = performance.now();
212
- const result = spawnSync(command, args, { cwd: here, input, maxBuffer: 1 << 30 });
213
- const elapsed = performance.now() - t0;
214
- if (result.status !== 0) {
215
- console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
216
- process.exit(1);
217
- }
218
- return { elapsed, stdout: result.stdout };
219
- }
220
-
221
- // Alternating Node and native runs; the two outputs must match
222
- function timeBoth(input, count) {
223
- const node = [];
224
- const native = [];
225
- for (let i = 0; i < count; i++) {
226
- const a = runOnce(process.execPath, ['main.ts', '--json'], input);
227
- const b = runOnce(exePath, ['--json'], input);
228
- if (!a.stdout.equals(b.stdout)) {
229
- console.error('Node and native JSON differ');
230
- process.exit(1);
231
- }
232
- node.push(a.elapsed);
233
- native.push(b.elapsed);
234
- }
235
- return [stats(node), stats(native)];
236
- }
237
-
238
- function table(nodeStats, nativeStats, extra = () => '') {
239
- console.log(`| Runtime | min | median | mean | max |${extra('head')}`);
240
- console.log(`|------------|--------------|--------------|--------------|--------------|${extra('rule')}`);
241
- console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |${extra(nodeStats)}`);
242
- console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |${extra(nativeStats)}`);
243
- }
244
-
245
- // 1. Compile the CLI to a native binary.
246
- fs.mkdirSync(cache, { recursive: true });
247
- console.log('Compiling main.ts to a native binary with TypeNative...');
248
- const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', exePath], { cwd: here, stdio: 'inherit' });
249
- if (build.status !== 0) process.exit(build.status ?? 1);
250
-
251
- // 2. Per-call latency on a small docker-compose.yml.
252
- const compose = Buffer.from(COMPOSE);
253
- console.log(`\n(a) \`yaml --json < docker-compose.yml\` (${compose.length} bytes), ${runs} runs each (full process: spawn -> exit):\n`);
254
- const [nodeCall, nativeCall] = timeBoth(compose, runs);
255
- table(nodeCall, nativeCall);
256
- console.log(`\nTypeNative is ${(nodeCall.median / nativeCall.median).toFixed(1)}x faster per call (median).`);
257
-
258
- // 3. Throughput on generated Kubernetes manifests.
259
- const big = Buffer.from(manifests(megabytes * 1024 * 1024));
260
- const mb = big.length / (1024 * 1024);
261
- console.log(`\n(b) \`yaml --json < manifests.yaml\` (${mb.toFixed(1)} MB, ${big.toString().split('\n---\n').length} documents), ${throughputRuns} runs each:\n`);
262
- const [nodeBig, nativeBig] = timeBoth(big, throughputRuns);
263
- table(nodeBig, nativeBig, (s) => (s === 'head' ? ' MB/s (median) |' : s === 'rule' ? '---------------|' : ` ${(mb / (s.median / 1000)).toFixed(2).padStart(13)} |`));
264
- const ratio = nodeBig.median / nativeBig.median;
265
- console.log(`\nTypeNative is ${ratio >= 1 ? `${ratio.toFixed(1)}x faster` : `${(1 / ratio).toFixed(1)}x slower`} on throughput (median).`);