typenative 0.4.0 → 0.5.0

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 (167) hide show
  1. package/.github/workflows/ci.yml +2 -0
  2. package/CHANGELOG.md +372 -0
  3. package/CONTRIBUTION.md +1 -1
  4. package/README.md +62 -22
  5. package/ROUND2-HANDOFF.md +200 -0
  6. package/bin/cli.js +2 -0
  7. package/bin/go/bridge.js +74 -14
  8. package/bin/go/convert.js +48 -0
  9. package/bin/go/metadata.js +13 -4
  10. package/bin/go/overrides.js +4 -6
  11. package/bin/go/stdlib.generated.js +2 -0
  12. package/bin/index.js +130 -2
  13. package/bin/main.js +2 -0
  14. package/bin/node/assert.js +2 -2
  15. package/bin/node/async_hooks.js +1 -1
  16. package/bin/node/buffer.js +16 -5
  17. package/bin/node/child_process.js +57 -9
  18. package/bin/node/cluster.js +44 -6
  19. package/bin/node/core.js +1 -1
  20. package/bin/node/crypto.js +205 -4
  21. package/bin/node/crypto_keys.js +21 -21
  22. package/bin/node/dgram.js +231 -7
  23. package/bin/node/diagnostics_channel.js +3 -3
  24. package/bin/node/dns.js +472 -9
  25. package/bin/node/errno_tables.js +445 -0
  26. package/bin/node/events.js +3 -3
  27. package/bin/node/fetch.js +2 -2
  28. package/bin/node/fs.js +24 -17
  29. package/bin/node/fs_watch.js +5 -5
  30. package/bin/node/http.js +131 -33
  31. package/bin/node/http2.js +7 -4
  32. package/bin/node/module_v8.js +142 -0
  33. package/bin/node/net.js +17 -14
  34. package/bin/node/path.js +17 -3
  35. package/bin/node/process.js +274 -9
  36. package/bin/node/readline.js +272 -1
  37. package/bin/node/registry.js +5 -1
  38. package/bin/node/sqlite.js +65 -17
  39. package/bin/node/stream.js +220 -22
  40. package/bin/node/stream_web.js +10 -10
  41. package/bin/node/system_extra.js +3 -3
  42. package/bin/node/test.js +834 -234
  43. package/bin/node/test_coverage.js +187 -0
  44. package/bin/node/test_report.js +1392 -0
  45. package/bin/node/test_run.js +575 -0
  46. package/bin/node/timers.js +4 -4
  47. package/bin/node/tls.js +5 -5
  48. package/bin/node/url.js +23 -5
  49. package/bin/node/util.js +49 -8
  50. package/bin/node/util_extra.js +13 -37
  51. package/bin/node/webview.js +654 -0
  52. package/bin/node/worker_threads.js +80 -20
  53. package/bin/node/zlib.js +3 -3
  54. package/bin/parse-node.js +4 -0
  55. package/bin/transpiler.js +14457 -1723
  56. package/bin/typecheck-node.js +1 -0
  57. package/bin/version.js +1 -1
  58. package/conformance.md +1153 -884
  59. package/examples/README.md +47 -10
  60. package/examples/desktop-notes/README.md +108 -0
  61. package/examples/desktop-notes/bench.mjs +125 -0
  62. package/examples/desktop-notes/main.ts +131 -0
  63. package/examples/desktop-notes/package-lock.json +726 -0
  64. package/examples/desktop-notes/package.json +9 -0
  65. package/examples/desktop-notes/ui.ts +176 -0
  66. package/examples/dupe-finder/README.md +76 -0
  67. package/examples/dupe-finder/bench.mjs +120 -0
  68. package/examples/dupe-finder/main.ts +87 -0
  69. package/examples/json-server/README.md +31 -29
  70. package/examples/json-server/bench.mjs +130 -38
  71. package/examples/mandelbrot-png/README.md +65 -0
  72. package/examples/mandelbrot-png/bench.mjs +95 -0
  73. package/examples/mandelbrot-png/main.ts +98 -0
  74. package/examples/mandelbrot-png/preview.png +0 -0
  75. package/examples/marked-cli/README.md +159 -0
  76. package/examples/marked-cli/bench.mjs +178 -0
  77. package/examples/marked-cli/main.ts +292 -0
  78. package/examples/marked-cli/parity.mjs +185 -0
  79. package/examples/marked-cli/vendor/marked/LICENSE +44 -0
  80. package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
  81. package/examples/todo-cli/README.md +69 -0
  82. package/examples/todo-cli/bench.mjs +89 -0
  83. package/examples/todo-cli/main.ts +92 -0
  84. package/examples/yaml-cli/README.md +136 -0
  85. package/examples/yaml-cli/bench.mjs +265 -0
  86. package/examples/yaml-cli/main.ts +29 -0
  87. package/examples/yaml-cli/parity.mjs +156 -0
  88. package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
  89. package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
  90. package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
  91. package/govendor/github.com/jchv/go-webview2/README.md +21 -0
  92. package/govendor/github.com/jchv/go-webview2/common.go +84 -0
  93. package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
  94. package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
  95. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
  96. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
  97. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
  98. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
  99. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
  100. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
  101. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
  102. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
  103. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
  104. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
  105. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
  106. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
  107. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
  108. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
  109. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
  110. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
  111. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
  112. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
  113. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
  114. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
  115. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
  116. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
  117. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
  118. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
  119. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
  120. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
  121. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
  122. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
  123. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
  124. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
  125. package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
  126. package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
  127. package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
  128. package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
  129. package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
  130. package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
  131. package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
  132. package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
  133. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
  134. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
  135. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
  136. package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
  137. package/govendor/github.com/jchv/go-winloader/README.md +124 -0
  138. package/govendor/github.com/jchv/go-winloader/common.go +11 -0
  139. package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
  140. package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
  141. package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
  142. package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
  143. package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
  144. package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
  145. package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
  146. package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
  147. package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
  148. package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
  149. package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
  150. package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
  151. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
  152. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
  153. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
  154. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
  155. package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
  156. package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
  157. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
  158. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
  159. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
  160. package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
  161. package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
  162. package/govendor/manifest.txt +3 -1
  163. package/package.json +1 -1
  164. package/tsparser/main.go +4 -0
  165. package/types/typenative-go.d.ts +23 -0
  166. package/types/typenative-npm.d.ts +359 -7
  167. package/types/typenative.d.ts +283 -42
@@ -0,0 +1,44 @@
1
+ # License information
2
+
3
+ ## Contribution License Agreement
4
+
5
+ If you contribute code to this project, you are implicitly allowing your code
6
+ to be distributed under the MIT license. You are also implicitly verifying that
7
+ all code is your original work. `</legalese>`
8
+
9
+ ## Marked
10
+
11
+ Copyright (c) 2018+, MarkedJS (https://github.com/markedjs/)
12
+ Copyright (c) 2011-2018, Christopher Jeffrey (https://github.com/chjj/)
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in
22
+ all copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
+ THE SOFTWARE.
31
+
32
+ ## Markdown
33
+
34
+ Copyright © 2004, John Gruber
35
+ http://daringfireball.net/
36
+ All rights reserved.
37
+
38
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
39
+
40
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
41
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
42
+ * Neither the name “Markdown” nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
43
+
44
+ This software is provided by the copyright holders and contributors “as is” and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
@@ -0,0 +1,26 @@
1
+ # Vendored: `marked`
2
+
3
+ | | |
4
+ | ---------- | ----------------------------------------------------------------------------------- |
5
+ | Upstream | [markedjs/marked](https://github.com/markedjs/marked) |
6
+ | Tag | `v18.1.2` |
7
+ | Commit | `5beecae42b0beb07eaccc3a732560f56867a8301` |
8
+ | License | MIT ([LICENSE](LICENSE)) |
9
+ | Copied | `src/` and `LICENSE`, byte for byte. Upstream's tests, docs, man page and `bin/` aren't vendored |
10
+
11
+ `bin/main.js` is ported to [../../main.ts](../../main.ts) instead of vendored.
12
+ It's a JavaScript file that imports the built package, and main.ts parses the
13
+ same options and makes the same `marked.parse` / `marked.lexer` calls from
14
+ TypeScript (its header comment lists the differences).
15
+
16
+ ## Patches
17
+
18
+ None. `src/` is upstream's tree unmodified. To check:
19
+
20
+ ```sh
21
+ git clone --depth 1 --branch v18.1.2 https://github.com/markedjs/marked.git /tmp/marked
22
+ diff -r /tmp/marked/src src && diff /tmp/marked/LICENSE LICENSE
23
+ ```
24
+
25
+ Anything in this tree that TypeNative couldn't compile was fixed in the
26
+ transpiler, never here. Each fix has a probe in [conformance/](../../../../conformance/).
@@ -0,0 +1,69 @@
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 5500U, Node v24.19.0, Go 1.26.0,
57
+ TypeNative 0.4.1, default (non-`--release`) build.
58
+
59
+ | Command | Runtime | min | median | mean | max |
60
+ |-------------|------------|-----------|-----------|-----------|-----------|
61
+ | `todo list` | Node.js | 104.98 ms | 111.37 ms | 112.74 ms | 130.05 ms |
62
+ | `todo list` | TypeNative | 14.45 ms | 16.26 ms | 16.44 ms | 20.40 ms |
63
+ | `todo add` | Node.js | 105.63 ms | 112.74 ms | 113.33 ms | 133.39 ms |
64
+ | `todo add` | TypeNative | 15.23 ms | 16.98 ms | 17.15 ms | 23.72 ms |
65
+
66
+ **TypeNative answers each command 6.6–6.8x faster** (median). At about 16 ms,
67
+ a call feels instant. At about 110 ms, there's a visible pause, 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 3.8 MB file, with no Node install and no `node_modules`.
@@ -0,0 +1,89 @@
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
+ }
@@ -0,0 +1,92 @@
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
+ }
@@ -0,0 +1,136 @@
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.
9
+
10
+ This is a test of TypeNative on real code nobody wrote for it: a lexer and
11
+ parser driven by sticky regexes, class hierarchies with overrides (an
12
+ `Array` subclass among them), structural object types passed between
13
+ modules, `Object.assign` and `Object.defineProperty` on class instances,
14
+ Maps keyed by nodes, and JSON output whose key order and number formatting
15
+ must match V8's.
16
+
17
+ ## Usage
18
+
19
+ ```sh
20
+ yaml < in.yaml # re-emit the YAML stream
21
+ yaml --json < in.yaml # print it as JSON
22
+ yaml valid < in.yaml # exit 1 on the first error
23
+ yaml lex < in.yaml # print the lexer's tokens
24
+ yaml cst < in.yaml # print the concrete syntax tree
25
+ yaml --help # all options (--indent, --single, --strict, …)
26
+ ```
27
+
28
+ ## Run it
29
+
30
+ With Node.js (24 or later runs the TypeScript directly):
31
+
32
+ ```sh
33
+ node main.ts --json < docker-compose.yml
34
+ ```
35
+
36
+ With TypeNative:
37
+
38
+ ```sh
39
+ npx typenative --source main.ts --output dist/yaml
40
+ ./dist/yaml --json < docker-compose.yml
41
+ ```
42
+
43
+ From a checkout of this repository:
44
+
45
+ ```sh
46
+ node bin/cli.js --source examples/yaml-cli/main.ts --output dist/yaml
47
+ ```
48
+
49
+ ## Parity with Node.js
50
+
51
+ Reproduce with `node parity.mjs [filter]`. The script fetches the
52
+ [yaml-test-suite](https://github.com/yaml/yaml-test-suite) at tag
53
+ `data-2022-01-17` into `.cache/` (it isn't committed) and compiles `main.ts`.
54
+ It then feeds each test's `in.yaml` to both runtimes in each of four modes:
55
+ default (re-emit), `--json`, `valid` and `lex`. stdout, stderr and the exit
56
+ code must match byte for byte. The only normalization is on stderr: Node's
57
+ ` at …` stack frames are dropped (a native binary has no JS frames to
58
+ print), and warnings' `(node:PID)` becomes `(node)`. Failures are written to
59
+ `.cache/failures/`.
60
+
61
+ ```
62
+ 1608/1608 cases match (100.00%)
63
+ ```
64
+
65
+ That's 402 inputs × 4 modes. They include the error cases: invalid YAML
66
+ reports the same `YAMLParseError`s (message, code, positions and the
67
+ source excerpt with its caret line) and exits with the same code. Warnings
68
+ such as `Keys with collection values will be stringified…` print the same
69
+ `process.emitWarning` lines.
70
+
71
+ ## Benchmark: Node.js vs TypeNative
72
+
73
+ Reproduce with `node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]`.
74
+ The script compiles `main.ts`, then times **complete process runs** (spawn to
75
+ exit) of `yaml --json` under both runtimes, alternating Node and native runs,
76
+ and checks that the two JSON outputs are identical:
77
+
78
+ - **(a) per-call latency:** a 1.4 KB docker-compose.yml (five services,
79
+ anchors, flow collections, a folded scalar), the kind of file a script or a
80
+ git hook converts once.
81
+ - **(b) throughput:** 10 MB of generated Kubernetes manifests (13,140
82
+ Deployments, Services and ConfigMaps in one stream, from a fixed-seed
83
+ generator, so every machine gets the same input).
84
+
85
+ **Environment:** Linux 6.18 container, 4 vCPUs of an Intel Xeon @ 2.80GHz,
86
+ Node v24.22.2, Go 1.26.0, TypeNative 0.4.2 (unreleased changes), default
87
+ (non-`--release`) build. The binary is 7.3 MB.
88
+
89
+ (a) `yaml --json < docker-compose.yml`, 30 runs each:
90
+
91
+ | Runtime | min | median | mean | max |
92
+ | ---------- | --------- | --------- | --------- | --------- |
93
+ | Node.js | 214.80 ms | 249.92 ms | 248.16 ms | 305.68 ms |
94
+ | TypeNative | 7.63 ms | 9.16 ms | 9.49 ms | 12.83 ms |
95
+
96
+ (b) `yaml --json < manifests.yaml` (10.0 MB), 5 runs each:
97
+
98
+ | Runtime | min | median | mean | max | MB/s (median) |
99
+ | ---------- | --------- | --------- | --------- | --------- | ------------- |
100
+ | Node.js | 3,916 ms | 4,226 ms | 4,137 ms | 4,301 ms | 2.37 |
101
+ | TypeNative | 14,955 ms | 15,170 ms | 15,184 ms | 15,547 ms | 0.66 |
102
+
103
+ **Per call, TypeNative is 27x faster** (9 ms vs 250 ms, median). Node's
104
+ cost here is mostly startup. It loads and type-strips the 78 TypeScript
105
+ files on every run before it reads a byte of input. That's what you feel
106
+ when a tool calls the converter once per file.
107
+
108
+ **On bulk throughput, TypeNative is 3.6x slower.** Once V8's JIT has warmed
109
+ up on a long input, it runs this code faster than the compiled Go does. The
110
+ library passes its values around as structural object types and unions
111
+ (`Scalar | YAMLMap | YAMLSeq | Pair`, CST tokens), which TypeNative holds as
112
+ `interface{}`. Property reads and calls on them go through reflection, and a
113
+ value handed to a parameter of a narrower object type is copied field by
114
+ field. Garbage collection takes about a fifth of the native run. Both costs
115
+ grow linearly with input size, while Node's head start is fixed, so the
116
+ native binary finishes first up to about 150–200 KB of YAML per process:
117
+
118
+ | Input | Node.js | TypeNative |
119
+ | ------ | ------- | ---------- |
120
+ | 16 KB | 284 ms | 38 ms |
121
+ | 64 KB | 311 ms | 107 ms |
122
+ | 128 KB | 345 ms | 253 ms |
123
+ | 1 MB | 862 ms | 1,599 ms |
124
+
125
+ (Medians of 5 full process runs each, same generator.)
126
+
127
+ Getting here fixed three performance bugs in TypeNative's runtime, all
128
+ general:
129
+
130
+ - Long strings' code-point indexes were evicted by the many short slices a
131
+ lexer takes, so every sticky-regex match rebuilt the index of the whole
132
+ input. That made lexing quadratic: about 23 s for 1 MB before (measured on
133
+ a loaded machine), about 1.5 s after.
134
+ - Struct field lookups by name are cached per type.
135
+ - `typeof f` types are func types, so the composer's callbacks are called
136
+ directly instead of through reflection.