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,95 +0,0 @@
1
- // Render benchmark: Node.js vs TypeNative for the Mandelbrot PNG renderer.
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 both runtimes rendering the same image, and checks that the two
6
- // PNGs decode to identical pixels. (The compressed bytes differ: Node uses
7
- // zlib's deflate and TypeNative Go's compress/flate.)
8
- //
9
- // Usage: node bench.mjs [width=1920] [height=1080] [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 zlib from 'node:zlib';
16
- import { fileURLToPath } from 'node:url';
17
-
18
- const here = path.dirname(fileURLToPath(import.meta.url));
19
- const repoRoot = path.resolve(here, '..', '..');
20
- const cli = path.join(repoRoot, 'bin', 'cli.js');
21
- const width = process.argv[2] ?? '1920';
22
- const height = process.argv[3] ?? '1080';
23
- const runs = Number(process.argv[4] ?? '10') || 10;
24
- const exeName = process.platform === 'win32' ? 'mandelbrot.exe' : 'mandelbrot';
25
- const exePath = path.join(here, 'dist', exeName);
26
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mandelbrot-bench-'));
27
-
28
- function stats(samples) {
29
- const sorted = [...samples].sort((a, b) => a - b);
30
- const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
31
- return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
32
- }
33
-
34
- function fmt(ms) {
35
- return `${ms.toFixed(2)} ms`.padStart(11);
36
- }
37
-
38
- // Inflated IDAT data (the raw scanlines), after checking every chunk's CRC
39
- function decodePixels(file) {
40
- const png = fs.readFileSync(file);
41
- const idat = [];
42
- for (let offset = 8; offset < png.length; ) {
43
- const length = png.readUInt32BE(offset);
44
- const typeAndData = png.subarray(offset + 4, offset + 8 + length);
45
- if (zlib.crc32(typeAndData) !== png.readUInt32BE(offset + 8 + length)) throw new Error(`bad CRC in ${file}`);
46
- if (typeAndData.toString('latin1', 0, 4) === 'IDAT') idat.push(typeAndData.subarray(4));
47
- offset += 12 + length;
48
- }
49
- return zlib.inflateSync(Buffer.concat(idat));
50
- }
51
-
52
- function timeRuns(command, args) {
53
- const samples = [];
54
- for (let i = 0; i < runs; i++) {
55
- const t0 = performance.now();
56
- const result = spawnSync(command, args, { cwd: here, encoding: 'utf8' });
57
- const elapsed = performance.now() - t0;
58
- if (result.status !== 0) {
59
- console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
60
- process.exit(1);
61
- }
62
- samples.push(elapsed);
63
- }
64
- return stats(samples);
65
- }
66
-
67
- // 1. Compile the renderer to a native binary.
68
- fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
69
- console.log('Compiling main.ts to a native binary with TypeNative...');
70
- const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
71
- cwd: here,
72
- stdio: 'inherit',
73
- });
74
- if (build.status !== 0) process.exit(build.status ?? 1);
75
-
76
- // 2. Time full renders under both runtimes.
77
- const nodePng = path.join(tmpDir, 'node.png');
78
- const nativePng = path.join(tmpDir, 'native.png');
79
- console.log(`\nTiming ${runs} full process runs of each runtime at ${width}x${height}...\n`);
80
- const nodeStats = timeRuns(process.execPath, ['main.ts', width, height, nodePng]);
81
- const nativeStats = timeRuns(exePath, [width, height, nativePng]);
82
-
83
- const nodePixels = decodePixels(nodePng);
84
- const same = nodePixels.equals(decodePixels(nativePng));
85
- const sizes = [fs.statSync(nodePng).size, fs.statSync(nativePng).size];
86
- fs.rmSync(tmpDir, { recursive: true, force: true });
87
-
88
- console.log(`Render + PNG encode + write, ${runs} runs (full process: spawn -> exit):\n`);
89
- console.log('| Runtime | min | median | mean | max |');
90
- console.log('|------------|-------------|-------------|-------------|-------------|');
91
- console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
92
- console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
93
- console.log(`\nPNG sizes: Node ${sizes[0]} B, TypeNative ${sizes[1]} B; decoded pixels identical: ${same}`);
94
- console.log(`TypeNative is ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster (median).`);
95
- if (!same) process.exit(1);
@@ -1,98 +0,0 @@
1
- // Renders the Mandelbrot set to a PNG file. The PNG encoder is written here
2
- // by hand on top of node:zlib (deflate + CRC-32), so the program needs no
3
- // image library under Node.js or TypeNative.
4
- //
5
- // node main.ts [width=1920] [height=1080] [out=mandelbrot.png]
6
- // ./mandelbrot [width] [height] [out]
7
- import fs from 'node:fs';
8
- import zlib from 'node:zlib';
9
-
10
- const width = Number(process.argv[2] ?? '1920');
11
- const height = Number(process.argv[3] ?? '1080');
12
- const out = process.argv[4] ?? 'mandelbrot.png';
13
- const maxIter = 500;
14
-
15
- // Smooth escape-time colouring: a cosine palette indexed by the
16
- // continuous iteration count, black inside the set
17
- function colorAt(cx: number, cy: number): [number, number, number] {
18
- let x = 0;
19
- let y = 0;
20
- let i = 0;
21
- while (x * x + y * y <= 256 && i < maxIter) {
22
- const xt = x * x - y * y + cx;
23
- y = 2 * x * y + cy;
24
- x = xt;
25
- i++;
26
- }
27
- if (i === maxIter) return [0, 0, 0];
28
- const smooth = i + 1 - Math.log2(Math.log2(x * x + y * y) / 2);
29
- const t = 0.55 + Math.sqrt(smooth) * 0.11;
30
- const r = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.0))));
31
- const g = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.1))));
32
- const b = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.2))));
33
- return [r, g, b];
34
- }
35
-
36
- // Raw PNG scanlines: each row is a filter byte (0 = none) then RGB triples
37
- function render(): Uint8Array {
38
- const stride = width * 3 + 1;
39
- const pixels = new Uint8Array(stride * height);
40
- const scale = 3 / height;
41
- for (let py = 0; py < height; py++) {
42
- const cy = (py - height / 2) * scale;
43
- let offset = py * stride + 1;
44
- for (let px = 0; px < width; px++) {
45
- const cx = (px - width * 0.65) * scale;
46
- const [r, g, b] = colorAt(cx, cy);
47
- pixels[offset] = r;
48
- pixels[offset + 1] = g;
49
- pixels[offset + 2] = b;
50
- offset += 3;
51
- }
52
- }
53
- return pixels;
54
- }
55
-
56
- // One PNG chunk: length, type, data, CRC-32 of type + data
57
- function chunk(type: string, data: Uint8Array): Uint8Array {
58
- const typed = new Uint8Array(4 + data.length);
59
- for (let i = 0; i < 4; i++) typed[i] = type.charCodeAt(i);
60
- typed.set(data, 4);
61
- const result = new Uint8Array(12 + data.length);
62
- const view = new DataView(result.buffer);
63
- view.setUint32(0, data.length);
64
- result.set(typed, 4);
65
- view.setUint32(8 + data.length, zlib.crc32(typed));
66
- return result;
67
- }
68
-
69
- function encodePng(pixels: Uint8Array): Uint8Array {
70
- const header = new Uint8Array(13);
71
- const view = new DataView(header.buffer);
72
- view.setUint32(0, width);
73
- view.setUint32(4, height);
74
- header[8] = 8; // bit depth
75
- header[9] = 2; // colour type: truecolour RGB
76
- const parts = [
77
- new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
78
- chunk('IHDR', header),
79
- chunk('IDAT', new Uint8Array(zlib.deflateSync(pixels))),
80
- chunk('IEND', new Uint8Array(0))
81
- ];
82
- const total = parts.reduce((sum, p) => sum + p.length, 0);
83
- const png = new Uint8Array(total);
84
- let offset = 0;
85
- for (const part of parts) {
86
- png.set(part, offset);
87
- offset += part.length;
88
- }
89
- return png;
90
- }
91
-
92
- const t0 = performance.now();
93
- const pixels = render();
94
- const t1 = performance.now();
95
- const png = encodePng(pixels);
96
- fs.writeFileSync(out, png);
97
- const t2 = performance.now();
98
- console.log(`Rendered ${width}x${height} in ${Math.round(t1 - t0)} ms, encoded ${png.length} bytes in ${Math.round(t2 - t1)} ms -> ${out}`);
Binary file
@@ -1,174 +0,0 @@
1
- # marked CLI
2
-
3
- The [`marked`](https://github.com/markedjs/marked) package's own command-line
4
- tool, compiled from its **unmodified** upstream TypeScript source: about 4,000
5
- lines across 13 files, vendored as is in [vendor/marked/](vendor/marked/) (see
6
- [VENDOR.md](vendor/marked/VENDOR.md): no patches). [main.ts](main.ts) ports
7
- upstream's `bin/main.js` to TypeScript. It parses the same options, reads the
8
- markdown from a string, a file or stdin, and prints the HTML or the lexer's
9
- token list.
10
-
11
- This is the second test of TypeNative on real code nobody wrote for it. marked
12
- is a markdown parser built on regexes: 66 definitions in `rules.ts`, with GFM
13
- and pedantic variants, many composed at run time by a fluent
14
- `edit(…).replace(…)` builder (44 `edit` calls). Many use lookarounds or backreferences, which
15
- Go's RE2 engine doesn't support, so they run on TypeNative's backtracking
16
- engine. Tokens are structural object types, arrays of tokens are
17
- filled in later through a queue that holds the same arrays, and the `--tokens`
18
- JSON must match V8's key order and leave out `undefined` fields, such as the
19
- title of a link definition that has none.
20
-
21
- ## Usage
22
-
23
- ```sh
24
- marked < in.md # render markdown to HTML
25
- marked -s '*hi*' # render a string
26
- marked -i in.md -o out.html # read a file, write a file
27
- marked --tokens < in.md # print the token list as JSON
28
- marked --no-gfm --pedantic # any boolean option of marked.defaults
29
- marked --help # all options
30
- ```
31
-
32
- Differences from `bin/main.js` (listed in main.ts's header): `--help` prints an
33
- option summary instead of opening the man page, `--version` prints the vendored
34
- version, and a config file (`-c`, `~/.marked.json`) must be JSON. A JavaScript
35
- config is a module Node would import and run, which a native binary can't. The
36
- HTML is also flushed before the process exits, under Node too: `bin/main.js`
37
- exits right after writing, which cuts output to a pipe off at 64 KB.
38
-
39
- ## Run it
40
-
41
- With Node.js (24 or later runs the TypeScript directly):
42
-
43
- ```sh
44
- node main.ts < README.md
45
- ```
46
-
47
- With TypeNative:
48
-
49
- ```sh
50
- npx typenative --source main.ts --output dist/marked
51
- ./dist/marked < README.md
52
- ```
53
-
54
- From a checkout of this repository:
55
-
56
- ```sh
57
- node bin/cli.js --source examples/marked-cli/main.ts --output dist/marked
58
- ```
59
-
60
- ## Parity with Node.js
61
-
62
- Reproduce with `node parity.mjs [filter]`. The script fetches marked's own spec
63
- suites (`test/specs` at tag `v18.1.2`) into `.cache/` (it isn't committed) and
64
- compiles `main.ts`. It then feeds each case's markdown on stdin to both
65
- runtimes in three modes: default (GFM), `--no-gfm` and `--tokens`. A case's
66
- front matter (`gfm`, `pedantic`, `breaks`) becomes CLI flags, as marked's test
67
- runner turns it into options. stdout, stderr and the exit code must match byte
68
- for byte. The only normalization is on stderr: Node's ` at …` stack frames
69
- are dropped (a native binary has no JS frames to print). Each run has a 10 s
70
- timeout. Failures are written to `.cache/failures/`.
71
-
72
- ```
73
- 2859/2859 cases match (100.00%)
74
- ```
75
-
76
- That's 953 inputs × 3 modes: CommonMark 0.31.2 (652 examples), GFM 0.29 (28),
77
- marked's own `new` (223) and `original` (22) tests, and its 28 ReDoS
78
- regression inputs. The ReDoS inputs are pathological on purpose, such as 10,000
79
- underscores in a row. Every one of them finishes within the timeout natively
80
- too (see the benchmark notes below for how long they take).
81
-
82
- ## Benchmark: Node.js vs TypeNative
83
-
84
- Reproduce with `node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]`.
85
- The script compiles `main.ts`, then times **complete process runs** (spawn to
86
- exit) of `marked` under both runtimes, alternating Node and native runs, and
87
- checks that the two HTML outputs are identical:
88
-
89
- - **(a) per-call latency:** marked's own README.md (3.5 KB), the kind of file
90
- a docs build or a git hook renders once per file.
91
- - **(b) throughput:** 10 MB of generated markdown (headings, paragraphs with
92
- emphasis, links and code spans, lists, tables, code fences and block quotes,
93
- from a fixed-seed generator, so every machine gets the same input).
94
-
95
- **Environment:** Windows 11 Pro, AMD Ryzen 5 7600 (6 cores, 12 threads),
96
- Node v26.13.0, Go 1.27.4, TypeNative 0.7.0, default
97
- (non-`--release`) build. The binary is 9.8 MB.
98
-
99
- (a) `marked < README.md` (3.5 KB), 30 runs each:
100
-
101
- | Runtime | min | median | mean | max |
102
- | ---------- | --------- | --------- | --------- | --------- |
103
- | Node.js | 104.86 ms | 106.91 ms | 108.76 ms | 122.06 ms |
104
- | TypeNative | 12.05 ms | 13.10 ms | 20.19 ms | 228.66 ms |
105
-
106
- (b) `marked < generated.md` (10.0 MB, 114,893 lines), 5 runs each:
107
-
108
- | Runtime | min | median | mean | max | MB/s (median) |
109
- | ---------- | -------- | -------- | -------- | -------- | ------------- |
110
- | Node.js | 604.66 ms | 607.99 ms | 609.90 ms | 620.93 ms | 16.45 |
111
- | TypeNative | 521.38 ms | 534.45 ms | 532.89 ms | 539.79 ms | 18.71 |
112
-
113
- **Per call, TypeNative is 8.2x faster** (13.1 ms vs 106.9 ms, median). Node's
114
- cost here is mostly startup: it loads and type-strips the 13 TypeScript files
115
- and compiles marked's regexes on every run. That's what you feel when a docs
116
- build or a hook renders one file per process.
117
-
118
- **On bulk throughput, TypeNative is 1.1x faster** (534 ms vs 608 ms). A
119
- loaded document is cut before top-level ATX headings that follow a blank line
120
- and are outside a code fence; up to six worker processes each parse one
121
- contiguous run of sections, and their HTML is concatenated. Parsing the
122
- sections separately is byte-for-byte identical to parsing the whole document
123
- here (bench.mjs checks it), because marked renders top-level blocks
124
- independently. `main.ts` falls back to one process for inputs under 512 KB, for
125
- documents with reference link definitions, raw HTML blocks or CRLF line
126
- endings, and for `--tokens`; `parity.mjs`'s spec cases all take that path, so
127
- their behavior is unchanged. The workers pass their assignment in `workerData`
128
- and return gzipped HTML through one temporary file: the worker IPC carries a
129
- single compact message, not several megabytes.
130
-
131
- **A single process is still about 1.6x slower than Node on 10 MB** (1.92 s vs
132
- 1.22 s). The
133
- parallel path is what closes the gap; per section the remaining cost is
134
- TypeNative's regex VM, which interprets marked's patterns where V8's Irregexp
135
- compiles them to machine code, plus the object and match-array allocation each
136
- token costs.
137
-
138
- The ReDoS inputs now favor the native binary: all of them together take 0.8 s
139
- natively and 3.3 s under Node, though most of Node's time there is starting up
140
- once per input. The slowest native case, `backticks_alternating_in_link`, takes
141
- 0.22 s against Node's 0.11 s.
142
-
143
- Getting here fixed performance bugs in TypeNative's runtime and compiler, all
144
- general:
145
-
146
- - Every regex now runs on one engine, a backtracking VM with JS semantics, in
147
- place of Go's RE2 for simple patterns and a closure-based matcher for the
148
- rest. A match allocates only its result. First-byte sets, literal prefixes
149
- and anchors reject most calls without running anything, which matters here
150
- because 83% of marked's 1.5 million regex calls per MB miss. RE2 remains as a
151
- fallback: a pattern it can run that exhausts the VM's step budget switches to
152
- it, so those patterns keep their linear-time guarantee.
153
- - Regex literals are compiled once, however many times they're evaluated.
154
- - `a += b` on a long string held as any is amortized: the string is extended
155
- in place when it ends where its buffer's written part ends. It used to copy
156
- on every append, which made the worst ReDoS input take over 10 s.
157
- - Strings built with `+=` in a loop are `strings.Builder`s, so rendering a
158
- long document is linear.
159
- - Short ASCII strings need no code-point index, and slices of a long non-ASCII
160
- string share its index instead of rebuilding it.
161
- - Type inference is memoized per query. marked's
162
- `edit(re).replace(…).replace(…)…` chains took exponential time to compile.
163
- - `[...s].length`, `[...s][i]` and `[...s].at(i)` are the code point count, the
164
- i-th code point and the i-th code point (from the end), without building an
165
- array of every rune; marked uses those for every emphasis delimiter.
166
- - `re.exec(s)` where only its presence is tested (`!!re.exec(s)`, an `if`
167
- condition) runs `re.test(s)`: no match array is built.
168
- - A nullable regex result is boxed only when it matched (`TnSlicePtr`), not by
169
- a closure and a heap box on every miss.
170
- - Greedy runs of "everything but one ASCII byte" (`[^\\n]+`, `[^*]+`) use
171
- `IndexByte` instead of testing each byte.
172
- - The ASCII cache for long strings is a mutex-guarded entry, not a new atomic
173
- entry per inline token.
174
- - Regex replacement builders are grown once to the input's length.
@@ -1,178 +0,0 @@
1
- // Benchmark: Node.js vs TypeNative for the marked CLI, both rendering
2
- // markdown to HTML (`marked < in.md`).
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 marked's
7
- // own README.md, the kind of file a docs build or a hook renders once;
8
- // (b) throughput: one run on about 10 MB of generated markdown (headings,
9
- // paragraphs with emphasis, links and code spans, lists, tables, code
10
- // fences and block quotes).
11
- // Runs alternate Node and native, so drift on the machine hits both alike.
12
- // Both runtimes' HTML must be identical, or the benchmark stops.
13
- //
14
- // Usage: node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]
15
-
16
- import { spawnSync } from 'node:child_process';
17
- import fs from 'node:fs';
18
- import path from 'node:path';
19
- import { fileURLToPath } from 'node:url';
20
-
21
- const here = path.dirname(fileURLToPath(import.meta.url));
22
- const repoRoot = path.resolve(here, '..', '..');
23
- const cli = path.join(repoRoot, 'bin', 'cli.js');
24
- const cache = path.join(here, '.cache');
25
- const upstream = path.join(cache, 'marked-src');
26
- const exePath = path.join(cache, process.platform === 'win32' ? 'marked.exe' : 'marked');
27
- const runs = Number(process.argv[2] ?? '30') || 30;
28
- const throughputRuns = Number(process.argv[3] ?? '5') || 5;
29
- const megabytes = Number(process.argv[4] ?? '10') || 10;
30
-
31
- // marked's README.md at the vendored tag (parity.mjs fetches the same checkout)
32
- function readme() {
33
- const file = path.join(upstream, 'README.md');
34
- if (!fs.existsSync(file)) {
35
- fs.mkdirSync(cache, { recursive: true });
36
- console.log('Fetching marked v18.1.2 ...');
37
- const clone = spawnSync('git', ['clone', '--quiet', '--depth', '1', '--branch', 'v18.1.2', '--filter=blob:none', '--sparse', 'https://github.com/markedjs/marked.git', upstream], { stdio: 'inherit' });
38
- if (clone.status !== 0) process.exit(1);
39
- spawnSync('git', ['-C', upstream, 'sparse-checkout', 'set', 'test/specs'], { stdio: 'inherit' });
40
- }
41
- return fs.readFileSync(file);
42
- }
43
-
44
- // Deterministic pseudo-random numbers (a fixed-seed LCG): the same document
45
- // on every run and every machine
46
- let seed = 42;
47
- function rand(n) {
48
- seed = (seed * 1103515245 + 12345) % 2147483648;
49
- return Math.floor(seed / 65536) % n;
50
- }
51
-
52
- const WORDS = ['markdown', 'parser', 'native', 'binary', 'token', 'lexer', 'render', 'inline', 'block', 'heading',
53
- 'paragraph', 'emphasis', 'table', 'fence', 'quote', 'list', 'link', 'image', 'escape', 'entity', 'spec', 'html'];
54
-
55
- function word() {
56
- return WORDS[rand(WORDS.length)];
57
- }
58
-
59
- function sentence() {
60
- const parts = [];
61
- const n = 6 + rand(14);
62
- for (let i = 0; i < n; i++) {
63
- const w = word();
64
- switch (rand(12)) {
65
- case 0: parts.push(`*${w}*`); break;
66
- case 1: parts.push(`**${w}**`); break;
67
- case 2: parts.push(`\`${w}()\``); break;
68
- case 3: parts.push(`[${w}](https://example.com/${w}/${rand(1000)} "${w}")`); break;
69
- case 4: parts.push(`_${w}_`); break;
70
- default: parts.push(w);
71
- }
72
- }
73
- return parts.join(' ') + '.';
74
- }
75
-
76
- function section(i) {
77
- const lines = [`${'#'.repeat(1 + rand(3))} ${word()} ${word()} ${i}`, ''];
78
- for (let p = 0; p < 2 + rand(3); p++) lines.push(`${sentence()} ${sentence()}\n${sentence()}`, '');
79
- switch (rand(5)) {
80
- case 0:
81
- for (let k = 0; k < 3 + rand(5); k++) lines.push(`- ${sentence()}`);
82
- lines.push('');
83
- break;
84
- case 1:
85
- for (let k = 1; k <= 3 + rand(4); k++) lines.push(`${k}. ${sentence()}`);
86
- lines.push('');
87
- break;
88
- case 2:
89
- lines.push(`| ${word()} | ${word()} | ${word()} |`, '| --- | :---: | ---: |');
90
- for (let k = 0; k < 3 + rand(6); k++) lines.push(`| ${word()} | ${rand(1000)} | *${word()}* |`);
91
- lines.push('');
92
- break;
93
- case 3:
94
- lines.push('```js', `function ${word()}(${word()}) {`, ` return ${rand(100)} < 3 && "${word()}";`, '}', '```', '');
95
- break;
96
- default:
97
- lines.push(`> ${sentence()}`, `> ${sentence()}`, '');
98
- }
99
- return lines.join('\n');
100
- }
101
-
102
- function document(bytes) {
103
- const out = [];
104
- let size = 0;
105
- for (let i = 0; size < bytes; i++) {
106
- const s = section(i);
107
- out.push(s);
108
- size += s.length + 1;
109
- }
110
- return out.join('\n');
111
- }
112
-
113
- function stats(samples) {
114
- const sorted = [...samples].sort((a, b) => a - b);
115
- const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
116
- return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
117
- }
118
-
119
- function fmt(ms) {
120
- return `${ms.toFixed(2)} ms`.padStart(13);
121
- }
122
-
123
- function runOnce(command, args, input) {
124
- const t0 = performance.now();
125
- const result = spawnSync(command, args, { cwd: here, input, maxBuffer: 1 << 30 });
126
- const elapsed = performance.now() - t0;
127
- if (result.status !== 0) {
128
- console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
129
- process.exit(1);
130
- }
131
- return { elapsed, stdout: result.stdout };
132
- }
133
-
134
- // Alternating Node and native runs; the two outputs must match
135
- function timeBoth(input, count) {
136
- const node = [];
137
- const native = [];
138
- for (let i = 0; i < count; i++) {
139
- const a = runOnce(process.execPath, ['main.ts'], input);
140
- const b = runOnce(exePath, [], input);
141
- if (!a.stdout.equals(b.stdout)) {
142
- console.error('Node and native HTML differ');
143
- process.exit(1);
144
- }
145
- node.push(a.elapsed);
146
- native.push(b.elapsed);
147
- }
148
- return [stats(node), stats(native)];
149
- }
150
-
151
- function table(nodeStats, nativeStats, extra = () => '') {
152
- console.log(`| Runtime | min | median | mean | max |${extra('head')}`);
153
- console.log(`|------------|--------------|--------------|--------------|--------------|${extra('rule')}`);
154
- console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |${extra(nodeStats)}`);
155
- console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |${extra(nativeStats)}`);
156
- }
157
-
158
- // 1. Compile the CLI to a native binary.
159
- fs.mkdirSync(cache, { recursive: true });
160
- const small = readme();
161
- console.log('Compiling main.ts to a native binary with TypeNative...');
162
- const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', exePath], { cwd: here, stdio: 'inherit' });
163
- if (build.status !== 0) process.exit(build.status ?? 1);
164
-
165
- // 2. Per-call latency on marked's README.md.
166
- console.log(`\n(a) \`marked < README.md\` (${small.length} bytes), ${runs} runs each (full process: spawn -> exit):\n`);
167
- const [nodeCall, nativeCall] = timeBoth(small, runs);
168
- table(nodeCall, nativeCall);
169
- console.log(`\nTypeNative is ${(nodeCall.median / nativeCall.median).toFixed(1)}x faster per call (median).`);
170
-
171
- // 3. Throughput on generated markdown.
172
- const big = Buffer.from(document(megabytes * 1024 * 1024));
173
- const mb = big.length / (1024 * 1024);
174
- console.log(`\n(b) \`marked < generated.md\` (${mb.toFixed(1)} MB, ${big.toString().split('\n').length} lines), ${throughputRuns} runs each:\n`);
175
- const [nodeBig, nativeBig] = timeBoth(big, throughputRuns);
176
- table(nodeBig, nativeBig, (s) => (s === 'head' ? ' MB/s (median) |' : s === 'rule' ? '---------------|' : ` ${(mb / (s.median / 1000)).toFixed(2).padStart(13)} |`));
177
- const ratio = nodeBig.median / nativeBig.median;
178
- console.log(`\nTypeNative is ${ratio >= 1 ? `${ratio.toFixed(1)}x faster` : `${(1 / ratio).toFixed(1)}x slower`} on throughput (median).`);