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
package/bin/index.js CHANGED
@@ -27,6 +27,9 @@ Options:
27
27
  --target <os/arch> Cross-compile, e.g. linux/amd64, linux/arm64, darwin/arm64,
28
28
  windows/amd64 ('go tool dist list' shows every pair)
29
29
  --release Smaller binary: drop symbols and file paths
30
+ --gui Windowed app: no console window on Windows (ignored elsewhere)
31
+ --cover Build with coverage counters, for go:runtime/coverage
32
+ (set GOCOVERDIR to also write a profile at exit)
30
33
  --emit-go <dir> Write the generated Go (and go.mod) to <dir> instead of building
31
34
  --no-typecheck Skip type-checking
32
35
  -h, --help Show this help
@@ -47,6 +50,8 @@ export function parseCliArgs(argv) {
47
50
  script: false,
48
51
  noTypeCheck: false,
49
52
  release: false,
53
+ gui: false,
54
+ cover: false,
50
55
  source: null,
51
56
  output: null,
52
57
  target: null,
@@ -73,6 +78,10 @@ export function parseCliArgs(argv) {
73
78
  args.noTypeCheck = true;
74
79
  else if (arg === '--release')
75
80
  args.release = true;
81
+ else if (arg === '--gui')
82
+ args.gui = true;
83
+ else if (arg === '--cover')
84
+ args.cover = true;
76
85
  else if (arg === '--source' || arg === '--output' || arg === '--target' || arg === '--emit-go') {
77
86
  if (value === null) {
78
87
  if (i + 1 >= argv.length || argv[i + 1].startsWith('--')) {
@@ -171,6 +180,22 @@ function vendorXText(outDir) {
171
180
  fs.writeFileSync(path.join(outDir, 'tnvendor', 'x-text', 'go.mod'), 'module golang.org/x/text\n\ngo 1.23\n');
172
181
  return { path: 'golang.org/x/text', version: 'v0.0.0', dir: './tnvendor/x-text' };
173
182
  }
183
+ // golang.org/x/net's dns/dnsmessage (node:dns record queries), copied from
184
+ // GOROOT/src/vendor
185
+ function vendorXNet(outDir) {
186
+ const goroot = runCommand('go', ['env', 'GOROOT']).stdout.trim();
187
+ const pkg = path.join('dns', 'dnsmessage');
188
+ const source = path.join(goroot, 'src', 'vendor', 'golang.org', 'x', 'net', pkg);
189
+ const target = path.join(outDir, 'tnvendor', 'x-net', pkg);
190
+ fs.mkdirSync(target, { recursive: true });
191
+ for (const file of fs.readdirSync(source)) {
192
+ if (file.endsWith('.go') && !file.endsWith('_test.go')) {
193
+ fs.writeFileSync(path.join(target, file), fs.readFileSync(path.join(source, file), 'utf-8'));
194
+ }
195
+ }
196
+ fs.writeFileSync(path.join(outDir, 'tnvendor', 'x-net', 'go.mod'), 'module golang.org/x/net\n\ngo 1.23\n');
197
+ return { path: 'golang.org/x/net', version: 'v0.0.0', dir: './tnvendor/x-net' };
198
+ }
174
199
  // node:sqlite's driver, the Brotli codec, fsnotify and their dependencies,
175
200
  // copied from the package's govendor/ (listed in govendor/manifest.txt); the
176
201
  // copy is skipped while dist/tnvendor already holds the same version. Returns
@@ -258,6 +283,92 @@ export function createProject(projectName, installDependencies) {
258
283
  console.log('Dependencies installed successfully!');
259
284
  }
260
285
  }
286
+ // go:embed files are copied next to the Go sources at their paths relative
287
+ // to the entry file; this manifest lists the copies for the next build to remove
288
+ const EMBED_MANIFEST = '.tnembed';
289
+ // A go:embed path from a source file's directory → the pattern relative to
290
+ // rootDir (the entry file's directory), or null when nothing matches or it
291
+ // leaves rootDir. An `all:` prefix (dotfiles too) is kept.
292
+ function resolveEmbedPattern(pattern, fromDir, rootDir) {
293
+ const all = pattern.startsWith('all:');
294
+ const spec = all ? pattern.slice(4) : pattern;
295
+ const rel = path.relative(rootDir, path.resolve(fromDir, spec)).split(path.sep).join('/');
296
+ if (!rel || rel === '..' || rel.startsWith('../') || path.isAbsolute(rel))
297
+ return null;
298
+ if (embedMatches(rootDir, rel).length === 0)
299
+ return null;
300
+ return all ? `all:${rel}` : rel;
301
+ }
302
+ // The paths under rootDir an embed pattern names: itself, or for a glob
303
+ // (* ? [...], in the last element) the matching entries of its directory
304
+ function embedMatches(rootDir, rel) {
305
+ const slash = rel.lastIndexOf('/');
306
+ const dir = slash < 0 ? '' : rel.slice(0, slash);
307
+ const base = rel.slice(slash + 1);
308
+ if (!/[*?[]/.test(base))
309
+ return fs.existsSync(path.join(rootDir, rel)) ? [rel] : [];
310
+ if (/[*?[]/.test(dir))
311
+ return [];
312
+ const glob = globRegExp(base);
313
+ let names = [];
314
+ try {
315
+ names = fs.readdirSync(path.join(rootDir, dir));
316
+ }
317
+ catch {
318
+ return [];
319
+ }
320
+ return names.filter((name) => glob.test(name)).map((name) => (dir ? `${dir}/${name}` : name));
321
+ }
322
+ // Go path.Match syntax for one path element: * ? and [...] ([^...] negates)
323
+ function globRegExp(glob) {
324
+ let source = '';
325
+ for (let i = 0; i < glob.length; i++) {
326
+ const c = glob[i];
327
+ const close = c === '[' ? glob.indexOf(']', i + 1) : -1;
328
+ if (c === '*')
329
+ source += '.*';
330
+ else if (c === '?')
331
+ source += '.';
332
+ else if (close > i) {
333
+ source += `[${glob.slice(i + 1, close)}]`;
334
+ i = close;
335
+ }
336
+ else
337
+ source += /[\\^$.|+(){}[\]]/.test(c) ? `\\${c}` : c;
338
+ }
339
+ return new RegExp(`^${source}$`);
340
+ }
341
+ // Removes the go:embed copies the previous build listed
342
+ function removeEmbedCopies(outDir) {
343
+ const manifest = path.join(outDir, EMBED_MANIFEST);
344
+ if (!fs.existsSync(manifest))
345
+ return;
346
+ for (const line of fs.readFileSync(manifest, 'utf-8').split('\n')) {
347
+ if (line.trim())
348
+ fs.rmSync(path.join(outDir, line.trim()), { recursive: true, force: true });
349
+ }
350
+ fs.rmSync(manifest);
351
+ }
352
+ // Copies the go:embed files into outDir, where go build resolves the
353
+ // directives (embed paths can't leave the package directory)
354
+ function copyEmbeds(rootDir, patterns, outDir) {
355
+ const copied = [];
356
+ for (const pattern of patterns) {
357
+ const rel = pattern.startsWith('all:') ? pattern.slice(4) : pattern;
358
+ for (const match of embedMatches(rootDir, rel)) {
359
+ const top = match.split('/')[0];
360
+ if ((top === match && match.endsWith('.go')) || ['go.mod', 'go.sum', 'tnvendor', EMBED_MANIFEST].includes(top)) {
361
+ console.error(`error: can't embed '${match}': the generated Go files use that name`);
362
+ process.exit(1);
363
+ }
364
+ fs.cpSync(path.join(rootDir, match), path.join(outDir, match), { recursive: true });
365
+ if (!copied.includes(top))
366
+ copied.push(top);
367
+ }
368
+ }
369
+ if (copied.length > 0)
370
+ fs.writeFileSync(path.join(outDir, EMBED_MANIFEST), `${copied.join('\n')}\n`);
371
+ }
261
372
  export function run(opts) {
262
373
  const goError = checkGo(opts.target);
263
374
  if (goError) {
@@ -297,7 +408,8 @@ export function run(opts) {
297
408
  tsx: !!sourcePath && sourcePath.endsWith('.tsx'),
298
409
  fileName: displayPath(entryPath),
299
410
  goFile: goFile,
300
- goos: targetOs
411
+ goos: targetOs,
412
+ cwd: process.cwd().split('\\').join('/')
301
413
  };
302
414
  if (sourceDir) {
303
415
  transpileOptions.readFile = (specifier, fromDir) => {
@@ -343,6 +455,7 @@ export function run(opts) {
343
455
  content = typed;
344
456
  return { content, dir, path: displayPath(resolved.path) };
345
457
  };
458
+ transpileOptions.resolveEmbed = (pattern, fromDir) => resolveEmbedPattern(pattern, fromDir ?? sourceDir, sourceDir);
346
459
  }
347
460
  const transpileResult = transpileToNative(tsCode, transpileOptions);
348
461
  if (transpileResult.diagnostics.length > 0) {
@@ -363,6 +476,7 @@ export function run(opts) {
363
476
  }
364
477
  // Clean up stale Go files from previous runs before writing new ones (in an
365
478
  // --emit-go directory, only files TypeNative generated)
479
+ removeEmbedCopies(outDir);
366
480
  for (const existing of fs.readdirSync(outDir)) {
367
481
  const file = path.join(outDir, existing);
368
482
  if (existing === 'go.mod' || existing === 'go.sum')
@@ -376,6 +490,8 @@ export function run(opts) {
376
490
  fs.writeFileSync(path.join(outDir, filename), content);
377
491
  goFiles.push(filename);
378
492
  }
493
+ if (sourceDir)
494
+ copyEmbeds(sourceDir, transpileResult.embeds, outDir);
379
495
  // Third-party Go modules the program imports (see vendorXText / vendorGoModules)
380
496
  const generated = [transpileResult.main];
381
497
  for (const content of transpileResult.files.values())
@@ -385,6 +501,9 @@ export function run(opts) {
385
501
  let goVersion = `${MIN_GO_MAJOR}.${MIN_GO_MINOR}`;
386
502
  if (generated.some((content) => xTextImport.test(content)))
387
503
  modules.push(vendorXText(outDir));
504
+ const xNetImport = /^import (\w+ )?"golang\.org\/x\/net\//m;
505
+ if (generated.some((content) => xNetImport.test(content)))
506
+ modules.push(vendorXNet(outDir));
388
507
  if (generated.some((content) => importsVendoredModule(content, opts.vendorDir))) {
389
508
  const vendored = vendorGoModules(opts.vendorDir, outDir);
390
509
  for (const mod of vendored.modules)
@@ -410,8 +529,17 @@ export function run(opts) {
410
529
  // is ~5 MB of Go) compile once, not again for every project directory
411
530
  if (opts.release || modules.length > 0)
412
531
  buildArgs.push('-trimpath');
532
+ // One value: go build keeps only the last -ldflags
533
+ const ldflags = [];
413
534
  if (opts.release)
414
- buildArgs.push('-ldflags=-s -w');
535
+ ldflags.push('-s', '-w');
536
+ if (opts.gui && targetOs === 'windows')
537
+ ldflags.push('-H=windowsgui');
538
+ if (ldflags.length > 0)
539
+ buildArgs.push(`-ldflags=${ldflags.join(' ')}`);
540
+ // atomic counters: coverage.ClearCounters refuses the default 'set' mode
541
+ if (opts.cover)
542
+ buildArgs.push('-cover', '-covermode=atomic');
415
543
  for (const file of goFiles)
416
544
  buildArgs.push(file);
417
545
  const buildResult = runCommand('go', buildArgs);
package/bin/main.js CHANGED
@@ -54,6 +54,8 @@ import { VERSION } from './version.js';
54
54
  vendorDir,
55
55
  target: args.target,
56
56
  release: args.release,
57
+ gui: args.gui,
58
+ cover: args.cover,
57
59
  emitGo: args.emitGo
58
60
  });
59
61
  })();
@@ -121,7 +121,7 @@ func TnErrorText(e interface{}) string {
121
121
  if s, ok := e.(string); ok {
122
122
  return s
123
123
  }
124
- if msg, ok := TnGet(e, "message").(string); ok {
124
+ if msg, ok := TnGetValue(e, "message").(string); ok {
125
125
  return "Error: " + msg
126
126
  }
127
127
  return fmt.Sprint(e)
@@ -133,7 +133,7 @@ func TnErrorMatches(e interface{}, expected map[string]interface{}) bool {
133
133
  got := TnGet(e, key)
134
134
  if key == "message" {
135
135
  if _, ok := e.(*TnError); !ok {
136
- if m, ok := TnGet(e, "message").(string); ok {
136
+ if m, ok := TnGetValue(e, "message").(string); ok {
137
137
  got = m
138
138
  }
139
139
  }
@@ -14,7 +14,7 @@ type TnAsyncLocalStorage struct {
14
14
  func TnNewAsyncLocalStorage(options ...interface{}) *TnAsyncLocalStorage {
15
15
  als := &TnAsyncLocalStorage{}
16
16
  if len(options) > 0 && options[0] != nil {
17
- if n, ok := TnGet(options[0], "name").(string); ok {
17
+ if n, ok := TnGetValue(options[0], "name").(string); ok {
18
18
  als.name = n
19
19
  }
20
20
  }
@@ -425,12 +425,16 @@ func TnBufferWriteFloat(b []byte, value float64, offset float64, size int, littl
425
425
  return float64(o + size)
426
426
  }
427
427
 
428
- func TnBufferToJSON(b []byte) map[string]interface{} {
428
+ // { type: 'Buffer', data: [...] }, in that key order
429
+ func TnBufferToJSON(b []byte) *TnMap[string, interface{}] {
429
430
  data := make([]interface{}, len(b))
430
431
  for i, c := range b {
431
432
  data[i] = float64(c)
432
433
  }
433
- return map[string]interface{}{"type": "Buffer", "data": data}
434
+ m := TnNewMap[string, interface{}]()
435
+ m.Set("type", "Buffer")
436
+ m.Set("data", data)
437
+ return m
434
438
  }
435
439
 
436
440
  // <Buffer 68 69>, as console.log / util.inspect show a Buffer
@@ -513,7 +517,7 @@ func (d *TnTextDecoder) decode(args ...interface{}) string {
513
517
  }
514
518
  stream := false
515
519
  if len(args) > 1 && args[1] != nil {
516
- stream, _ = TnGet(args[1], "stream").(bool)
520
+ stream, _ = TnGetValue(args[1], "stream").(bool)
517
521
  }
518
522
  data := b
519
523
  if len(d.pending) > 0 {
@@ -588,7 +592,7 @@ function boundsArgs(call, from) {
588
592
  }
589
593
  export function registerBuffer(r) {
590
594
  defineModule(r, 'buffer');
591
- defineHelper(r, 'buffer', ['bytes', 'encoding/base64', 'encoding/binary', 'encoding/hex', 'fmt', 'math', 'math/big', 'strconv', 'strings', 'unicode/utf16', 'unicode/utf8'], ['error', 'dynamic'], bufferHelper);
595
+ defineHelper(r, 'buffer', ['bytes', 'encoding/base64', 'encoding/binary', 'encoding/hex', 'fmt', 'math', 'math/big', 'strconv', 'strings', 'unicode/utf16', 'unicode/utf8'], ['error', 'dynamic', 'orderedMap'], bufferHelper);
592
596
  // Uint8Array first: Buffer owns the []byte instance methods
593
597
  defineClass(r, 'globals.Uint8Array', '[]byte', 'Buffer', '', '', '', '');
594
598
  defineClass(r, 'buffer.Buffer', '[]byte', 'Buffer', '', '', '', '');
@@ -646,6 +650,12 @@ export function registerBuffer(r) {
646
650
  };
647
651
  setResults(r, 'buffer.Buffer', 'from=[]byte; alloc=[]byte; allocUnsafe=[]byte; allocUnsafeSlow=[]byte; concat=[]byte; byteLength=float64; isBuffer=bool; isEncoding=bool; compare=float64');
648
652
  r.results['globals.Uint8Array.from'] = '[]byte';
653
+ // Uint8Array.fromBase64(s) / u8.toBase64() (ES2026)
654
+ r.functions['globals.Uint8Array.fromBase64'] = (call) => {
655
+ use();
656
+ return `TnBufferFromString(${call.args[0]}, "base64")`;
657
+ };
658
+ r.results['globals.Uint8Array.fromBase64'] = '[]byte';
649
659
  r.functions['buffer.atob'] = (call) => {
650
660
  use();
651
661
  return `TnAtob(${call.args[0]})`;
@@ -726,7 +736,8 @@ export function registerBuffer(r) {
726
736
  r.results[`buffer.Buffer#${name}`] = result;
727
737
  };
728
738
  method('toString', 'string', (c) => `TnBufferToString(${c.recv}, ${encArg(c, 0)}${boundsArgs(c, 1)})`);
729
- method('toJSON', 'map[string]interface{}', (c) => `TnBufferToJSON(${c.recv})`);
739
+ method('toBase64', 'string', (c) => `TnBufferToString(${c.recv}, "base64")`);
740
+ method('toJSON', '*TnMap[string, interface{}]', (c) => `TnBufferToJSON(${c.recv})`);
730
741
  method('equals', 'bool', (c) => `bytes.Equal(${c.recv}, ${c.args[0]})`);
731
742
  method('compare', 'float64', (c) => `float64(bytes.Compare(${c.recv}, ${c.args[0]}))`);
732
743
  method('subarray', '[]byte', (c) => `TnBufferSlice(${c.recv}${boundsArgs(c, 0)})`);
@@ -10,6 +10,7 @@ type tnSpawnOptions struct {
10
10
  input []byte
11
11
  stdio string
12
12
  encoding string
13
+ detached bool
13
14
  }
14
15
 
15
16
  // Environment entries from an env option (records, objects, process.env)
@@ -48,25 +49,25 @@ func tnSpawnOpts(options []interface{}) tnSpawnOptions {
48
49
  return o
49
50
  }
50
51
  opts := options[0]
51
- if s, ok := TnGet(opts, "cwd").(string); ok {
52
+ if s, ok := TnGetValue(opts, "cwd").(string); ok {
52
53
  o.cwd = s
53
54
  }
54
55
  if env := TnGet(opts, "env"); env != nil {
55
56
  o.env = tnEnvList(env)
56
57
  }
57
- switch sh := TnGet(opts, "shell").(type) {
58
+ switch sh := TnGetValue(opts, "shell").(type) {
58
59
  case bool:
59
60
  o.shell = sh
60
61
  case string:
61
62
  o.shell = sh != ""
62
63
  }
63
- switch in := TnGet(opts, "input").(type) {
64
+ switch in := TnGetValue(opts, "input").(type) {
64
65
  case string:
65
66
  o.input = []byte(in)
66
67
  case []byte:
67
68
  o.input = in
68
69
  }
69
- switch st := TnGet(opts, "stdio").(type) {
70
+ switch st := TnGetValue(opts, "stdio").(type) {
70
71
  case string:
71
72
  o.stdio = st
72
73
  case []interface{}:
@@ -80,9 +81,12 @@ func tnSpawnOpts(options []interface{}) tnSpawnOptions {
80
81
  o.stdio = st[1]
81
82
  }
82
83
  }
83
- if e, ok := TnGet(opts, "encoding").(string); ok {
84
+ if e, ok := TnGetValue(opts, "encoding").(string); ok {
84
85
  o.encoding = e
85
86
  }
87
+ if d, ok := TnGetValue(opts, "detached").(bool); ok {
88
+ o.detached = d
89
+ }
86
90
  return o
87
91
  }
88
92
 
@@ -102,6 +106,9 @@ func tnCommand(file string, args []string, o tnSpawnOptions) *exec.Cmd {
102
106
  if o.env != nil {
103
107
  cmd.Env = o.env
104
108
  }
109
+ if o.detached {
110
+ tnDetach(cmd)
111
+ }
105
112
  return cmd
106
113
  }
107
114
 
@@ -249,6 +256,8 @@ type TnChildProcess struct {
249
256
  spawnfile string
250
257
  // IPC state of forked children (node:cluster / fork)
251
258
  ipc interface{}
259
+ // whether the child keeps the event loop alive (unref() drops it)
260
+ refHeld bool
252
261
  }
253
262
 
254
263
  func (cp *TnChildProcess) Get_stdin() *TnStream { return cp.stdin }
@@ -260,6 +269,21 @@ func (cp *TnChildProcess) Get_signalCode() interface{} { return cp.signalCode }
260
269
  func (cp *TnChildProcess) Get_killed() bool { return cp.killed }
261
270
  func (cp *TnChildProcess) Get_spawnfile() string { return cp.spawnfile }
262
271
 
272
+ // unref(): the parent can exit while the child runs; ref() undoes it
273
+ func (cp *TnChildProcess) unref() {
274
+ if cp.refHeld {
275
+ cp.refHeld = false
276
+ TnLoopUnref()
277
+ }
278
+ }
279
+
280
+ func (cp *TnChildProcess) ref() {
281
+ if !cp.refHeld && cp.exitCode == nil && cp.cmd != nil && cp.cmd.Process != nil {
282
+ cp.refHeld = true
283
+ TnLoopRef()
284
+ }
285
+ }
286
+
263
287
  func (cp *TnChildProcess) kill(signal ...interface{}) bool {
264
288
  if cp.cmd == nil || cp.cmd.Process == nil || cp.exitCode != nil {
265
289
  return false
@@ -330,14 +354,18 @@ func TnSpawn(file string, args []string, options ...interface{}) *TnChildProcess
330
354
  return cp
331
355
  }
332
356
  cp.pid = float64(cmd.Process.Pid)
357
+ cp.refHeld = true
333
358
  TnLoopRef()
334
359
  TnLoopPost(func() { cp.emit("spawn") })
335
360
  go func() {
336
361
  streams.Wait()
337
362
  err := cmd.Wait()
338
363
  status, _ := tnExitStatus(err)
364
+ if ipc, ok := cp.ipc.(interface{ waitDrained() }); ok {
365
+ ipc.waitDrained()
366
+ }
339
367
  TnLoopPost(func() {
340
- defer TnLoopUnref()
368
+ defer cp.unref()
341
369
  var code interface{} = status
342
370
  var signal interface{}
343
371
  if cp.killed {
@@ -412,7 +440,7 @@ func tnExecCollect(cp *TnChildProcess, display string, encoding string, cb inter
412
440
 
413
441
  func tnExecEncoding(options []interface{}) string {
414
442
  if len(options) > 0 && options[0] != nil {
415
- if e, ok := TnGet(options[0], "encoding").(string); ok {
443
+ if e, ok := TnGetValue(options[0], "encoding").(string); ok {
416
444
  return e
417
445
  }
418
446
  }
@@ -484,10 +512,30 @@ function stringArray(arg) {
484
512
  function isFunctionArg(node, type) {
485
513
  return !!node && (node.kind === 'ArrowFunction' || node.kind === 'FunctionExpression' || type.startsWith('func('));
486
514
  }
515
+ // detached: a new session on Unix (setsid, as libuv does), a detached process
516
+ // in a new process group on Windows
517
+ const unixDetachHelper = `func tnDetach(cmd *exec.Cmd) {
518
+ if cmd.SysProcAttr == nil {
519
+ cmd.SysProcAttr = &syscall.SysProcAttr{}
520
+ }
521
+ cmd.SysProcAttr.Setsid = true
522
+ }`;
523
+ const windowsDetachHelper = `func tnDetach(cmd *exec.Cmd) {
524
+ if cmd.SysProcAttr == nil {
525
+ cmd.SysProcAttr = &syscall.SysProcAttr{}
526
+ }
527
+ // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP
528
+ cmd.SysProcAttr.CreationFlags |= 0x8 | 0x200
529
+ }`;
487
530
  export function registerChildProcess(r) {
488
531
  defineModule(r, 'child_process');
489
- defineHelper(r, 'nodeChild', ['bytes', 'errors', 'fmt', 'io', 'io/fs', 'os', 'os/exec', 'reflect', 'runtime', 'strings', 'sync', 'unsafe'], ['nodeStream', 'fsErrors', 'buffer', 'dynamic', 'nodeInvoke', 'eventLoop', 'promise'], childHelper);
490
- defineClass(r, 'child_process.ChildProcess', '*TnChildProcess', 'class', 'on=*TnEventEmitter; once=*TnEventEmitter; off=*TnEventEmitter; emit=bool; kill=bool', '', 'stdin=*TnStream; stdout=*TnStream; stderr=*TnStream; pid=float64; exitCode=interface{}; signalCode=interface{}; killed=bool; spawnfile=string', '');
532
+ for (const goos of ['linux', 'darwin', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'solaris', 'illumos', 'aix']) {
533
+ defineHelper(r, `nodeChildOS@${goos}`, ['os/exec', 'syscall'], [], unixDetachHelper);
534
+ }
535
+ defineHelper(r, 'nodeChildOS@windows', ['os/exec', 'syscall'], [], windowsDetachHelper);
536
+ defineHelper(r, 'nodeChildOS@other', ['os/exec'], [], 'func tnDetach(cmd *exec.Cmd) {}');
537
+ defineHelper(r, 'nodeChild', ['bytes', 'errors', 'fmt', 'io', 'io/fs', 'os', 'os/exec', 'reflect', 'runtime', 'strings', 'sync', 'unsafe'], ['nodeStream', 'fsErrors', 'buffer', 'dynamic', 'nodeInvoke', 'eventLoop', 'promise', 'nodeChildOS@os'], childHelper);
538
+ defineClass(r, 'child_process.ChildProcess', '*TnChildProcess', 'class', 'on=*TnEventEmitter; once=*TnEventEmitter; off=*TnEventEmitter; emit=bool; kill=bool; ref=; unref=', '', 'stdin=*TnStream; stdout=*TnStream; stderr=*TnStream; pid=float64; exitCode=interface{}; signalCode=interface{}; killed=bool; spawnfile=string', '');
491
539
  defineClass(r, 'child_process.SpawnSyncReturns', '*TnSpawnSyncResult', 'class', '', 'pid=float64; stdout=string; stderr=string; status=float64; signal=*string; output=[]interface{}; error=*TnError', '', '');
492
540
  defineClass(r, 'child_process.SpawnSyncBytes', '*TnSpawnSyncBytes', 'class', '', 'pid=float64; stdout=[]byte; stderr=[]byte; status=float64; signal=*string; output=[]interface{}; error=*TnError', '', '');
493
541
  defineClass(r, 'child_process.ExecOutput', '*TnExecOutput', 'class', '', 'stdout=string; stderr=string', '', '');
@@ -58,6 +58,7 @@ type tnIpcLink struct {
58
58
  onControl func(ctl map[string]interface{})
59
59
  directControl func(ctl map[string]interface{}) bool
60
60
  onClose func()
61
+ done chan struct{}
61
62
  }
62
63
 
63
64
  func (l *tnIpcLink) write(kind byte, data []byte) bool {
@@ -95,6 +96,9 @@ func (l *tnIpcLink) start() {
95
96
  l.onClose()
96
97
  }
97
98
  })
99
+ if l.done != nil {
100
+ close(l.done)
101
+ }
98
102
  return
99
103
  }
100
104
  switch kind {
@@ -138,6 +142,16 @@ type tnChildIpc struct {
138
142
  connected bool
139
143
  onConnect func()
140
144
  onControl func(ctl map[string]interface{})
145
+ drained chan struct{}
146
+ }
147
+
148
+ // Holds the child's 'exit' until everything it sent (and the channel's close)
149
+ // is delivered, so 'message' and 'disconnect' come first as in Node
150
+ func (st *tnChildIpc) waitDrained() {
151
+ select {
152
+ case <-st.drained:
153
+ case <-time.After(time.Second):
154
+ }
141
155
  }
142
156
 
143
157
  func tnCPIpc(cp *TnChildProcess) *tnChildIpc {
@@ -210,6 +224,7 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
210
224
  }
211
225
  env = append(env, extraEnv...)
212
226
  env = append(env, "TN_IPC_ADDR="+ln.Addr().String(), "TN_IPC_TOKEN="+token)
227
+ env = tnSpawnEntryEnv(env)
213
228
  envMap := map[string]interface{}{}
214
229
  for _, kv := range env {
215
230
  if k, v, ok := strings.Cut(kv, "="); ok {
@@ -217,15 +232,15 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
217
232
  }
218
233
  }
219
234
  stdio := "inherit"
220
- if silent, _ := TnGet(opts, "silent").(bool); silent {
235
+ if silent, _ := TnGetValue(opts, "silent").(bool); silent {
221
236
  stdio = "pipe"
222
237
  }
223
238
  spawnOpts := map[string]interface{}{"env": envMap, "stdio": stdio}
224
- if cwd, ok := TnGet(opts, "cwd").(string); ok && cwd != "" {
239
+ if cwd, ok := TnGetValue(opts, "cwd").(string); ok && cwd != "" {
225
240
  spawnOpts["cwd"] = cwd
226
241
  }
227
242
  cp := TnSpawn(exe, argv, spawnOpts)
228
- st := &tnChildIpc{connected: true}
243
+ st := &tnChildIpc{connected: true, drained: make(chan struct{})}
229
244
  cp.ipc = st
230
245
  go func() {
231
246
  ln.(*net.TCPListener).SetDeadline(time.Now().Add(30 * time.Second))
@@ -233,6 +248,7 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
233
248
  conn, err := ln.Accept()
234
249
  if err != nil {
235
250
  ln.Close()
251
+ close(st.drained)
236
252
  return
237
253
  }
238
254
  kind, payload, err := tnReadFrame(conn)
@@ -241,11 +257,12 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
241
257
  continue
242
258
  }
243
259
  ln.Close()
244
- link := &tnIpcLink{conn: conn}
260
+ link := &tnIpcLink{conn: conn, done: st.drained}
245
261
  TnLoopPost(func() {
246
262
  st.link = link
247
263
  if !st.connected {
248
264
  link.close()
265
+ close(st.drained)
249
266
  return
250
267
  }
251
268
  for _, data := range st.pending {
@@ -277,6 +294,13 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
277
294
 
278
295
  // fork(modulePath, args?, options?): this program, run again (a native binary
279
296
  // holds one program; branch on process.send / cluster.isPrimary)
297
+ // fork('./child.ts'): runs the entry point compiled from that file
298
+ func TnForkAt(entry string, modulePath interface{}, rest ...interface{}) *TnChildProcess {
299
+ tnNextEntry = entry
300
+ defer func() { tnNextEntry = "" }()
301
+ return TnFork(modulePath, rest...)
302
+ }
303
+
280
304
  func TnFork(modulePath interface{}, rest ...interface{}) *TnChildProcess {
281
305
  argv := []string{}
282
306
  var opts interface{}
@@ -505,7 +529,7 @@ func (c *TnCluster) setupPrimary(settings ...interface{}) {
505
529
  }
506
530
  }
507
531
  }
508
- if b, ok := TnGet(s, "silent").(bool); ok {
532
+ if b, ok := TnGetValue(s, "silent").(bool); ok {
509
533
  c.silent = b
510
534
  }
511
535
  for _, key := range []string{"args", "silent", "exec", "execArgv", "cwd"} {
@@ -898,11 +922,15 @@ func tnClusterListen(srv *TnNetServer, network string, address string) (net.List
898
922
  }`;
899
923
  const CLUSTER_EMITTER = 'on=*TnEventEmitter; once=*TnEventEmitter; addListener=*TnEventEmitter; off=*TnEventEmitter; removeListener=*TnEventEmitter; removeAllListeners=*TnEventEmitter; emit=bool; listenerCount=float64';
900
924
  export function registerCluster(r) {
901
- defineHelper(r, 'nodeIpc', ['crypto/rand', 'encoding/binary', 'encoding/hex', 'encoding/json', 'io', 'net', 'os', 'reflect', 'strings', 'sync', 'time'], ['nodeClone', 'nodeChild', 'nodeEvents', 'eventLoop', 'nodeInvoke', 'dynamic'], ipcHelper);
925
+ defineHelper(r, 'nodeIpc', ['crypto/rand', 'encoding/binary', 'encoding/hex', 'encoding/json', 'io', 'net', 'os', 'reflect', 'strings', 'sync', 'time'], ['nodeClone', 'nodeChild', 'nodeEvents', 'eventLoop', 'nodeInvoke', 'dynamic', 'entryPoints'], ipcHelper);
902
926
  const useIpc = () => r.helper('nodeIpc');
903
927
  // child_process.fork and the child's process.send / connected / disconnect
904
928
  r.functions['child_process.fork'] = (c) => {
905
929
  useIpc();
930
+ // fork('./child.ts'): that file is compiled in as a second entry point
931
+ const entry = r.entryPoint(c.nodes[0]);
932
+ if (entry !== '')
933
+ return `TnForkAt(${JSON.stringify(entry)}, ${c.args.join(', ')})`;
906
934
  return `TnFork(${c.args.join(', ')})`;
907
935
  };
908
936
  r.results['child_process.fork'] = '*TnChildProcess';
@@ -923,6 +951,16 @@ export function registerCluster(r) {
923
951
  return `TnProcessSend(${c.args.join(', ')})`;
924
952
  };
925
953
  r.results['process.send'] = 'bool';
954
+ // process.send?.(…) / process.disconnect?.(): they exist only in a process
955
+ // with an IPC channel (a forked child)
956
+ r.functions['process.send?.'] = (c) => {
957
+ useIpc();
958
+ return `func() { if tnIpcParent != nil { TnProcessSend(${c.args.join(', ')}) } }()`;
959
+ };
960
+ r.functions['process.disconnect?.'] = (_c) => {
961
+ useIpc();
962
+ return 'func() { if tnIpcParent != nil { TnProcessDisconnect() } }()';
963
+ };
926
964
  r.functions['process.disconnect'] = (_c) => {
927
965
  useIpc();
928
966
  return 'TnProcessDisconnect()';
package/bin/node/core.js CHANGED
@@ -85,7 +85,7 @@ func tnInvokeArg(a interface{}, t reflect.Type) reflect.Value {
85
85
  }`;
86
86
  export function registerCore(r) {
87
87
  defineHelper(r, 'nodeInvoke', ['reflect'], ['dynamic'], invokeHelper);
88
- defineClass(r, 'NodeJS.ErrnoException', '*TnError', 'class', '', 'message=string; code=string', 'name=string; stack=string; cause=interface{}; syscall=string; path=string; errno=float64; hostname=string; address=string; port=float64; actual=interface{}; expected=interface{}; operator=string; generatedMessage=bool', '');
88
+ defineClass(r, 'NodeJS.ErrnoException', '*TnError', 'class', '', 'message=string; code=string', 'name=string; stack=string; cause=interface{}; syscall=string; path=string; errno=float64; hostname=string; address=string; port=float64; actual=interface{}; expected=interface{}; operator=string; generatedMessage=bool; errors=[]interface{}', '');
89
89
  r.globals['NodeJS'] = 'NodeJS';
90
90
  // Callback types of Node's typings (TransformCallback …)
91
91
  for (const name of ['stream.TransformCallback', 'fs.NoParamCallback']) {