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,9 @@
1
+ {
2
+ "name": "desktop-notes-example",
3
+ "private": true,
4
+ "type": "module",
5
+ "description": "The Node.js side of the example: compiled builds don't use this package",
6
+ "dependencies": {
7
+ "@webviewjs/webview": "1.0.0-rc.4"
8
+ }
9
+ }
@@ -0,0 +1,176 @@
1
+ // The app's whole user interface: one HTML page with inline CSS and JS. It is
2
+ // a TypeScript string, so it is compiled into the binary (no files to ship
3
+ // next to it) and works unchanged under Node.js.
4
+ export const page = `<!doctype html>
5
+ <html lang="en" translate="no">
6
+ <head>
7
+ <meta charset="utf-8">
8
+ <meta name="viewport" content="width=device-width, initial-scale=1">
9
+ <title>Notes</title>
10
+ <style>
11
+ :root {
12
+ color-scheme: light dark;
13
+ --bg: #f6f7f9; --side: #eceef2; --text: #1d2330; --muted: #677085;
14
+ --line: #d9dde5; --accent: #3d7be0; --active: #dde6f7; --danger: #c4402e;
15
+ }
16
+ @media (prefers-color-scheme: dark) {
17
+ :root { --bg: #16191f; --side: #1d212a; --text: #e8ebf2; --muted: #8b93a7;
18
+ --line: #2b313d; --active: #26324a; }
19
+ }
20
+ * { box-sizing: border-box; }
21
+ html, body { height: 100%; }
22
+ body { margin: 0; display: flex; background: var(--bg); color: var(--text);
23
+ font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
24
+ aside { width: 240px; flex-shrink: 0; background: var(--side); display: flex; flex-direction: column;
25
+ border-right: 1px solid var(--line); }
26
+ aside header { display: flex; gap: 6px; padding: 12px; }
27
+ #search { flex: 1; min-width: 0; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px;
28
+ background: var(--bg); color: inherit; font: inherit; }
29
+ button { border: 0; border-radius: 6px; padding: 6px 10px; font: inherit; cursor: pointer;
30
+ background: var(--accent); color: #fff; }
31
+ button.ghost { background: transparent; color: var(--danger); }
32
+ ul { list-style: none; margin: 0; padding: 0 6px; overflow-y: auto; flex: 1; }
33
+ li { padding: 8px 10px; border-radius: 6px; cursor: pointer; }
34
+ li.active { background: var(--active); }
35
+ li small { display: block; color: var(--muted); font-size: 12px; }
36
+ aside footer { padding: 10px 12px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }
37
+ main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
38
+ main header { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
39
+ #title { flex: 1; font-weight: 600; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
40
+ #status { color: var(--muted); font-size: 12px; }
41
+ textarea { flex: 1; border: 0; padding: 16px; resize: none; outline: none; background: transparent; color: inherit;
42
+ font: 15px/1.6 ui-monospace, "Cascadia Code", Menlo, monospace; }
43
+ .empty { margin: auto; color: var(--muted); }
44
+ @media (max-width: 560px) { aside { width: 150px; } }
45
+ </style>
46
+ </head>
47
+ <body>
48
+ <aside>
49
+ <header>
50
+ <input id="search" placeholder="Search" autocomplete="off">
51
+ <button id="new" title="New note">+</button>
52
+ </header>
53
+ <ul id="list"></ul>
54
+ <footer id="info"></footer>
55
+ </aside>
56
+ <main>
57
+ <header>
58
+ <div id="title">No note selected</div>
59
+ <span id="status"></span>
60
+ <button id="delete" class="ghost" hidden>Delete</button>
61
+ </header>
62
+ <textarea id="editor" spellcheck="false" hidden></textarea>
63
+ <div class="empty" id="empty">Create a note with +</div>
64
+ </main>
65
+ <script>
66
+ const $ = (id) => document.getElementById(id);
67
+ let notes = [];
68
+ let current = null;
69
+ let saveTimer = 0;
70
+
71
+ const ago = (ms) => {
72
+ const s = Math.round((Date.now() - ms) / 1000);
73
+ if (s < 60) return 'just now';
74
+ if (s < 3600) return Math.floor(s / 60) + ' min ago';
75
+ if (s < 86400) return Math.floor(s / 3600) + ' h ago';
76
+ return new Date(ms).toLocaleDateString();
77
+ };
78
+
79
+ async function loadList() {
80
+ notes = await (await fetch('/api/notes')).json();
81
+ render();
82
+ }
83
+
84
+ function render() {
85
+ const q = $('search').value.toLowerCase();
86
+ $('list').innerHTML = '';
87
+ for (const n of notes.filter((n) => n.name.toLowerCase().includes(q))) {
88
+ const li = document.createElement('li');
89
+ li.className = n.name === current ? 'active' : '';
90
+ li.textContent = n.name;
91
+ const meta = document.createElement('small');
92
+ meta.textContent = ago(n.modified) + ' · ' + n.size + ' B';
93
+ li.append(meta);
94
+ li.onclick = () => open(n.name);
95
+ $('list').append(li);
96
+ }
97
+ }
98
+
99
+ async function open(name) {
100
+ await flush();
101
+ const res = await fetch('/api/notes/' + encodeURIComponent(name));
102
+ current = name;
103
+ $('editor').value = res.ok ? await res.text() : '';
104
+ $('title').textContent = name;
105
+ $('editor').hidden = false;
106
+ $('delete').hidden = false;
107
+ $('empty').hidden = true;
108
+ $('status').textContent = '';
109
+ $('editor').focus();
110
+ render();
111
+ }
112
+
113
+ async function flush() {
114
+ if (!saveTimer) return;
115
+ clearTimeout(saveTimer);
116
+ saveTimer = 0;
117
+ await save();
118
+ }
119
+
120
+ async function save() {
121
+ saveTimer = 0;
122
+ await fetch('/api/notes/' + encodeURIComponent(current), { method: 'PUT', body: $('editor').value });
123
+ $('status').textContent = 'Saved';
124
+ await loadList();
125
+ }
126
+
127
+ $('editor').oninput = () => {
128
+ $('status').textContent = 'Editing…';
129
+ clearTimeout(saveTimer);
130
+ saveTimer = setTimeout(save, 400);
131
+ };
132
+
133
+ $('new').onclick = async () => {
134
+ let n = 1;
135
+ while (notes.some((x) => x.name === 'Untitled ' + n)) n++;
136
+ current = 'Untitled ' + n;
137
+ await fetch('/api/notes/' + encodeURIComponent(current), { method: 'PUT', body: '' });
138
+ await loadList();
139
+ open(current);
140
+ };
141
+
142
+ $('delete').onclick = async () => {
143
+ clearTimeout(saveTimer);
144
+ saveTimer = 0;
145
+ await fetch('/api/notes/' + encodeURIComponent(current), { method: 'DELETE' });
146
+ current = null;
147
+ $('editor').hidden = true;
148
+ $('delete').hidden = true;
149
+ $('empty').hidden = false;
150
+ $('title').textContent = 'No note selected';
151
+ await loadList();
152
+ };
153
+
154
+ $('search').oninput = render;
155
+
156
+ async function refreshInfo() {
157
+ try {
158
+ const i = await (await fetch('/api/info')).json();
159
+ $('info').textContent = i.dir + ' · ' + i.runtime + ' · ' + (i.rss / 1048576).toFixed(1) + ' MiB';
160
+ } catch {
161
+ $('info').textContent = 'disconnected';
162
+ }
163
+ }
164
+
165
+ loadList().then(() => notes.length && open(notes[0].name));
166
+ refreshInfo();
167
+ setInterval(refreshInfo, 5000);
168
+ // Closing the window saves what the autosave hasn't reached. Not pagehide:
169
+ // webviews can tear the page down without it.
170
+ addEventListener('beforeunload', () => {
171
+ if (saveTimer) navigator.sendBeacon('/api/notes/' + encodeURIComponent(current), $('editor').value);
172
+ });
173
+ </script>
174
+ </body>
175
+ </html>
176
+ `;
@@ -0,0 +1,76 @@
1
+ # Duplicate Finder
2
+
3
+ Scans a directory tree, finds files with identical content and writes a JSON
4
+ report of the duplicates and the space they waste. It's a disk-bound and
5
+ hash-bound tool you can hand to anyone as one binary, with no runtime to
6
+ install.
7
+
8
+ Demonstrates: `fs.readdirSync` with `recursive` + `withFileTypes`,
9
+ `fs.statSync`, `fs.readFileSync` / `fs.writeFileSync`, SHA-256 via
10
+ `crypto.createHash`, `Map` grouping, sorting with a compound comparator,
11
+ `reduce`, `JSON.stringify` with indentation and `performance.now()`.
12
+
13
+ ## How it works
14
+
15
+ 1. Walk the tree and group every non-empty file by size.
16
+ 2. Hash (SHA-256) only the files whose size matches at least one other file.
17
+ A file with a unique size can't have a duplicate, so it's never read.
18
+ 3. Files with the same hash form a duplicate group. Groups are sorted by
19
+ wasted bytes (`size × (copies − 1)`) and written to the report.
20
+
21
+ ```sh
22
+ node main.ts ~/Downloads report.json
23
+ # Scanned 2000 files (243.5 MiB), hashed 1494
24
+ # Found 449 duplicate groups wasting 81.9 MiB
25
+ # <copies> x <size> B <first path> (the 5 biggest groups)
26
+ # Report written to report.json in 412 ms
27
+ ```
28
+
29
+ `report.json` lists every group with its hash, size and full file paths.
30
+
31
+ ## Run it
32
+
33
+ With Node.js:
34
+
35
+ ```sh
36
+ node main.ts <dir> [report.json]
37
+ ```
38
+
39
+ With TypeNative:
40
+
41
+ ```sh
42
+ npx typenative --source main.ts --output dist/dupes.exe
43
+ ./dist/dupes.exe <dir> [report.json]
44
+ ```
45
+
46
+ From a checkout of this repository:
47
+
48
+ ```sh
49
+ node bin/cli.js --source examples/dupe-finder/main.ts --output dist/dupes.exe
50
+ ```
51
+
52
+ ## Benchmark: Node.js vs TypeNative
53
+
54
+ Reproduce with `node bench.mjs [runs=10]`. The script compiles `main.ts` and
55
+ generates a deterministic fixture tree in a temp directory: 2,000 files
56
+ (1 KiB–256 KiB, 243.5 MiB) across 280 folders. A quarter of the files are
57
+ exact copies of earlier ones, and another quarter have the same size as an
58
+ earlier file but different bytes, so they get hashed without being
59
+ duplicates. It runs each runtime once to warm the OS file cache, then times
60
+ **complete process runs** of both and checks that the two reports are
61
+ byte-identical.
62
+
63
+ **Environment:** Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0,
64
+ TypeNative 0.4.1, default (non-`--release`) build.
65
+
66
+ | Runtime | min | median | mean | max |
67
+ |------------|-----------|-----------|-----------|-----------|
68
+ | Node.js | 659.14 ms | 674.33 ms | 675.70 ms | 700.33 ms |
69
+ | TypeNative | 398.94 ms | 412.47 ms | 411.52 ms | 423.42 ms |
70
+
71
+ Both runtimes found the same 449 duplicate groups (1,494 files hashed,
72
+ 81.9 MiB wasted). **TypeNative finishes the scan 1.6x faster** (median). The
73
+ work is reading and hashing about 180 MiB, so the gap comes from Go's
74
+ SHA-256 and file I/O, and from skipping the ~100 ms Node startup. The OS
75
+ file cache is warm in both cases. On a cold cache, disk speed dominates and
76
+ the gap narrows.
@@ -0,0 +1,120 @@
1
+ // Disk-scan benchmark: Node.js vs TypeNative for the duplicate finder.
2
+ //
3
+ // Compiles main.ts to a native binary with the TypeNative CLI from this repo
4
+ // checkout (../../bin/cli.js), generates a fixture tree in a temp directory
5
+ // (2,000 files in 40 folders, 1 KiB - 256 KiB, a quarter of them duplicates,
6
+ // plus same-size files with different content), then times complete process
7
+ // runs of both runtimes over it and checks that the two reports match.
8
+ //
9
+ // Usage: node bench.mjs [runs=10]
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] ?? '10') || 10;
21
+ const exeName = process.platform === 'win32' ? 'dupes.exe' : 'dupes';
22
+ const exePath = path.join(here, 'dist', exeName);
23
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dupes-bench-'));
24
+ const fixture = path.join(tmpDir, 'tree');
25
+
26
+ function stats(samples) {
27
+ const sorted = [...samples].sort((a, b) => a - b);
28
+ const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
29
+ return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
30
+ }
31
+
32
+ function fmt(ms) {
33
+ return `${ms.toFixed(2)} ms`.padStart(11);
34
+ }
35
+
36
+ // Deterministic pseudo-random bytes, so every run scans the same tree
37
+ function makeFixture() {
38
+ let seed = 42;
39
+ const rand = () => {
40
+ seed ^= seed << 13;
41
+ seed ^= seed >>> 17;
42
+ seed ^= seed << 5;
43
+ return (seed >>> 0) / 0x100000000;
44
+ };
45
+ const originals = [];
46
+ let bytes = 0;
47
+ for (let i = 0; i < 2000; i++) {
48
+ const dir = path.join(fixture, `dir-${i % 40}`, `sub-${i % 7}`);
49
+ fs.mkdirSync(dir, { recursive: true });
50
+ let content;
51
+ if (i % 4 === 3 && originals.length > 0) {
52
+ // An exact copy of an earlier file
53
+ content = originals[Math.floor(rand() * originals.length)];
54
+ } else if (i % 4 === 2 && originals.length > 0) {
55
+ // Same size as an earlier file, different bytes: hashed, but not a duplicate
56
+ const twin = originals[Math.floor(rand() * originals.length)];
57
+ content = Buffer.from(twin);
58
+ content[content.length - 1] ^= 0xff;
59
+ } else {
60
+ content = Buffer.alloc(1024 + 4 * Math.floor(rand() * 255 * 256));
61
+ for (let j = 0; j < content.length; j += 4) content.writeUInt32LE(Math.floor(rand() * 0xffffffff), j);
62
+ originals.push(content);
63
+ }
64
+ fs.writeFileSync(path.join(dir, `file-${i}.bin`), content);
65
+ bytes += content.length;
66
+ }
67
+ return bytes;
68
+ }
69
+
70
+ function timeRuns(command, args) {
71
+ const samples = [];
72
+ for (let i = 0; i < runs; i++) {
73
+ const t0 = performance.now();
74
+ const result = spawnSync(command, args, { cwd: here, encoding: 'utf8' });
75
+ const elapsed = performance.now() - t0;
76
+ if (result.status !== 0) {
77
+ console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
78
+ process.exit(1);
79
+ }
80
+ samples.push(elapsed);
81
+ }
82
+ return stats(samples);
83
+ }
84
+
85
+ // 1. Compile the finder to a native binary.
86
+ fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
87
+ console.log('Compiling main.ts to a native binary with TypeNative...');
88
+ const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
89
+ cwd: here,
90
+ stdio: 'inherit',
91
+ });
92
+ if (build.status !== 0) process.exit(build.status ?? 1);
93
+
94
+ // 2. Build the fixture tree.
95
+ const fixtureBytes = makeFixture();
96
+ console.log(`\nFixture: 2000 files, ${(fixtureBytes / 1024 / 1024).toFixed(1)} MiB in ${fixture}`);
97
+
98
+ // 3. Time full runs of both runtimes (the first run of each also warms the OS file cache).
99
+ const nodeReport = path.join(tmpDir, 'node.json');
100
+ const nativeReport = path.join(tmpDir, 'native.json');
101
+ timeRuns(process.execPath, ['main.ts', fixture, nodeReport]);
102
+ console.log(`\nTiming ${runs} full process runs of each runtime...\n`);
103
+ const nodeStats = timeRuns(process.execPath, ['main.ts', fixture, nodeReport]);
104
+ const nativeStats = timeRuns(exePath, [fixture, nativeReport]);
105
+
106
+ const same = fs.readFileSync(nodeReport, 'utf8') === fs.readFileSync(nativeReport, 'utf8');
107
+ const report = JSON.parse(fs.readFileSync(nativeReport, 'utf8'));
108
+ fs.rmSync(tmpDir, { recursive: true, force: true });
109
+
110
+ console.log(`Scan + hash + report, ${runs} runs (full process: spawn -> exit):\n`);
111
+ console.log('| Runtime | min | median | mean | max |');
112
+ console.log('|------------|-------------|-------------|-------------|-------------|');
113
+ console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
114
+ console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
115
+ console.log(
116
+ `\nBoth found ${report.groups.length} duplicate groups (${report.hashedFiles} files hashed, ` +
117
+ `${(report.wastedBytes / 1024 / 1024).toFixed(1)} MiB wasted); reports identical: ${same}`
118
+ );
119
+ console.log(`TypeNative is ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster (median).`);
120
+ if (!same) process.exit(1);
@@ -0,0 +1,87 @@
1
+ // Finds duplicate files under a directory and writes a JSON report.
2
+ // Files are grouped by size first, so only files that share a size are read
3
+ // and hashed (SHA-256). Duplicates are files whose hashes match.
4
+ //
5
+ // node main.ts <dir> [report.json]
6
+ // ./dupes <dir> [report.json] (default report: dupes.json)
7
+ import crypto from 'node:crypto';
8
+ import fs from 'node:fs';
9
+ import path from 'node:path';
10
+
11
+ interface DuplicateGroup {
12
+ hash: string;
13
+ size: number;
14
+ files: string[];
15
+ }
16
+
17
+ interface Report {
18
+ root: string;
19
+ scannedFiles: number;
20
+ scannedBytes: number;
21
+ hashedFiles: number;
22
+ groups: DuplicateGroup[];
23
+ wastedBytes: number;
24
+ }
25
+
26
+ const root = path.resolve(process.argv[2] ?? '.');
27
+ const reportPath = process.argv[3] ?? 'dupes.json';
28
+ const started = performance.now();
29
+
30
+ // 1. Walk the tree and group every regular file by size.
31
+ const bySize = new Map<number, string[]>();
32
+ let scannedFiles = 0;
33
+ let scannedBytes = 0;
34
+ for (const entry of fs.readdirSync(root, { recursive: true, withFileTypes: true })) {
35
+ if (!entry.isFile()) continue;
36
+ const file = path.join(entry.parentPath, entry.name);
37
+ const size = fs.statSync(file).size;
38
+ scannedFiles++;
39
+ scannedBytes += size;
40
+ if (size === 0) continue;
41
+ const sameSize = bySize.get(size) ?? [];
42
+ sameSize.push(file);
43
+ bySize.set(size, sameSize);
44
+ }
45
+
46
+ // 2. Hash only the files that share a size with another file.
47
+ const byHash = new Map<string, DuplicateGroup>();
48
+ let hashedFiles = 0;
49
+ for (const [size, files] of bySize) {
50
+ if (files.length < 2) continue;
51
+ for (const file of files) {
52
+ const hash = crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
53
+ hashedFiles++;
54
+ const group = byHash.get(hash);
55
+ if (group) group.files.push(file);
56
+ else byHash.set(hash, { hash: hash, size: size, files: [file] });
57
+ }
58
+ }
59
+
60
+ // 3. Keep the groups with more than one file, biggest savings first.
61
+ const groups: DuplicateGroup[] = [];
62
+ for (const group of byHash.values()) {
63
+ if (group.files.length > 1) {
64
+ group.files.sort();
65
+ groups.push(group);
66
+ }
67
+ }
68
+ groups.sort((a, b) => b.size * (b.files.length - 1) - a.size * (a.files.length - 1) || (a.hash < b.hash ? -1 : 1));
69
+ const wastedBytes = groups.reduce((sum, g) => sum + g.size * (g.files.length - 1), 0);
70
+
71
+ const report: Report = {
72
+ root: root,
73
+ scannedFiles: scannedFiles,
74
+ scannedBytes: scannedBytes,
75
+ hashedFiles: hashedFiles,
76
+ groups: groups,
77
+ wastedBytes: wastedBytes
78
+ };
79
+ fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
80
+
81
+ const mib = (n: number): string => (n / 1024 / 1024).toFixed(1);
82
+ console.log(`Scanned ${scannedFiles} files (${mib(scannedBytes)} MiB), hashed ${hashedFiles}`);
83
+ console.log(`Found ${groups.length} duplicate groups wasting ${mib(wastedBytes)} MiB`);
84
+ for (const group of groups.slice(0, 5)) {
85
+ console.log(` ${group.files.length} x ${group.size} B ${path.relative(root, group.files[0])}`);
86
+ }
87
+ console.log(`Report written to ${reportPath} in ${Math.round(performance.now() - started)} ms`);
@@ -51,7 +51,9 @@ server.listen(3000, '127.0.0.1');
51
51
  | `/todos` | POST | Create a todo: `{ "title": "..." }` → 201 |
52
52
  | anything else | * | `404` with a JSON error |
53
53
 
54
- Invalid JSON or a missing `title` returns `400` with a JSON error.
54
+ A missing `title` returns `400` with a JSON error. (The example keeps
55
+ `JSON.parse` unguarded, so invalid JSON throws — under Node and the compiled
56
+ binary alike, an uncaught throw ends the process.)
55
57
 
56
58
  ## Run it
57
59
 
@@ -91,35 +93,35 @@ curl -s -w "%{http_code}" -X POST -d '{}' http://127.0.0.1:3000/todos # 400
91
93
  Reproduce with `node bench.mjs [gets=20000] [posts=10000]`. The script
92
94
  compiles `main.ts` with the TypeNative CLI, starts the server under each
93
95
  runtime, seeds 50 todos, and drives load over keep-alive connections at
94
- concurrency 16 with a Node.js `node:http` client (identical client for both).
95
- It measures requests/second plus p50/p99 latency per request for
96
- `GET /todos` (20,000 requests, ~50-item JSON response) and `POST /todos`
97
- (10,000 requests).
96
+ concurrency 16 (identical client for both). It measures requests/second plus
97
+ p50/p99 latency per request for `GET /todos` (20,000 requests, ~50-item JSON
98
+ response) and `POST /todos` (10,000 requests).
99
+
100
+ The load generator is built to stay out of the way, like `wrk` or
101
+ `autocannon`: 8 worker threads × 2 raw keep-alive sockets send prebuilt
102
+ request bytes and read each response just far enough to find its end. A
103
+ plain `node:http` client tops out around 8–11k req/s on this machine, so
104
+ with it both servers would simply tie at the client's limit.
98
105
 
99
106
  **Environment:** Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0,
100
- TypeNative 0.4.0, default (non-`--release`) build. Re-run variation is about
101
- ±5%.
107
+ TypeNative 0.4.0, default (non-`--release`) build, client and server on the
108
+ same machine. Re-run variation is about ±10%; across runs TypeNative was
109
+ 1.2–1.3x Node on GET and 1.2–1.4x on POST.
102
110
 
103
- | Runtime | Endpoint | req/s | p50 (ms) | p99 (ms) |
111
+ | Runtime | Endpoint | req/s | p50 (ms) | p99 (ms) |
104
112
  |------------|-------------|-------:|---------:|---------:|
105
- | Node.js | GET /todos | 7709 | 1.87 | 6.05 |
106
- | TypeNative | GET /todos | 4834 | 3.08 | 6.60 |
107
- | Node.js | POST /todos | 9244 | 1.57 | 3.97 |
108
- | TypeNative | POST /todos | 6141 | 2.46 | 4.55 |
109
-
110
- **Node.js serves ~1.5x more requests per second** on this workload. That is
111
- the honest result, and it is worth understanding: V8 and Node's `node:http`
112
- have been tuned for exactly this I/O-bound pattern for over a decade, while
113
- TypeNative re-implements the request lifecycle (event emission, stream
114
- callbacks, per-request closures) on top of Go's `net/http`, and that
115
- translation still has overhead. Tail latency (p99) is comparable for both.
116
-
117
- Where the compiled binary wins is everything *around* the steady-state loop:
118
- it starts in ~15 ms instead of ~100 ms, it is a single 9.7 MB executable with
119
- zero dependencies — `scp` it to a server and run it, no Node install, no
120
- `node_modules` — and CPU-heavy work inside a handler runs ~2x faster (see the
121
- [fibonacci example](../fibonacci/README.md)).
122
-
123
- Takeaway: for I/O-bound HTTP services Node remains the throughput king today;
124
- choose TypeNative when startup time, deployment simplicity, or CPU-bound
125
- handler work dominates.
113
+ | Node.js | GET /todos | 13379 | 1.02 | 3.03 |
114
+ | TypeNative | GET /todos | 17794 | 0.66 | 1.87 |
115
+ | Node.js | POST /todos | 15405 | 0.94 | 2.17 |
116
+ | TypeNative | POST /todos | 19581 | 0.60 | 2.32 |
117
+
118
+ **TypeNative serves ~1.3x more requests per second**, with lower median
119
+ latency on both routes. JavaScript still runs one thing at a time, as in
120
+ Node, but the compiled server spreads everything around it — socket reads
121
+ and writes, HTTP parsing — across Go's `net/http` goroutines, and runs the
122
+ handler right on the request's goroutine whenever the event loop is free.
123
+
124
+ On top of that, the binary starts in ~15 ms instead of ~100 ms and is a
125
+ single 9.7 MB executable with zero dependencies — `scp` it to a server and
126
+ run it, no Node install, no `node_modules` — and CPU-heavy work inside a
127
+ handler runs ~2x faster (see the [fibonacci example](../fibonacci/README.md)).