@innovatorved/code-lab 0.3.2 → 0.4.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 (155) hide show
  1. package/ARCHITECTURE.md +5 -3
  2. package/CHANGELOG.md +20 -0
  3. package/README.md +70 -168
  4. package/dist/apps/web/src/index.d.ts +34 -0
  5. package/dist/code-lab-sql.css +482 -0
  6. package/dist/code-lab-sql.d.ts +179 -0
  7. package/dist/code-lab-sql.esm.js +542 -0
  8. package/dist/code-lab-sql.umd.js +531 -0
  9. package/dist/code-lab.css +246 -0
  10. package/dist/code-lab.d.ts +110 -15
  11. package/dist/code-lab.esm.js +34265 -18104
  12. package/dist/code-lab.js +33186 -17053
  13. package/dist/code-lab.umd.js +65502 -0
  14. package/dist/extensions/cpp.esm.js +19478 -3760
  15. package/dist/extensions/cpp.js +19475 -3757
  16. package/dist/extensions/guided-lab.esm.js +19401 -3725
  17. package/dist/extensions/guided-lab.js +19400 -3724
  18. package/dist/extensions/python.d.ts +1 -0
  19. package/dist/extensions/python.esm.js +19576 -3766
  20. package/dist/extensions/python.js +19576 -3766
  21. package/dist/extensions/umbrella.d.ts +5 -0
  22. package/dist/index.d.ts +1 -1
  23. package/dist/modular-kernel.d.ts +11 -0
  24. package/dist/packages/commands/src/index.d.ts +96 -0
  25. package/dist/packages/core/src/di.d.ts +36 -0
  26. package/dist/packages/core/src/disposable.d.ts +27 -0
  27. package/dist/packages/core/src/errors.d.ts +69 -0
  28. package/dist/packages/core/src/events.d.ts +38 -0
  29. package/dist/packages/core/src/index.d.ts +6 -0
  30. package/dist/packages/core/src/path.d.ts +14 -0
  31. package/dist/packages/core/src/registry.d.ts +23 -0
  32. package/dist/packages/editor/src/index.d.ts +118 -0
  33. package/dist/packages/fs/src/backends.d.ts +37 -0
  34. package/dist/packages/fs/src/index.d.ts +3 -0
  35. package/dist/packages/fs/src/types.d.ts +40 -0
  36. package/dist/packages/fs/src/vfs-service.d.ts +33 -0
  37. package/dist/packages/preview/src/index.d.ts +34 -0
  38. package/dist/packages/runtime/src/index.d.ts +47 -0
  39. package/dist/packages/settings/src/builtin-schemas.d.ts +15 -0
  40. package/dist/packages/settings/src/configuration-service.d.ts +100 -0
  41. package/dist/packages/settings/src/index.d.ts +4 -0
  42. package/dist/packages/settings/src/jsonc.d.ts +20 -0
  43. package/dist/packages/settings/src/schema.d.ts +28 -0
  44. package/dist/packages/sql/src/completions.d.ts +8 -0
  45. package/dist/packages/sql/src/diagnostics.d.ts +2 -0
  46. package/dist/packages/sql/src/formatter.d.ts +2 -0
  47. package/dist/packages/sql/src/index.d.ts +7 -0
  48. package/dist/packages/sql/src/sql-editor.d.ts +2 -0
  49. package/dist/packages/sql/src/styles.d.ts +2 -0
  50. package/dist/packages/sql/src/tokenizer.d.ts +13 -0
  51. package/dist/packages/sql/src/types.d.ts +130 -0
  52. package/dist/packages/terminal/src/index.d.ts +54 -0
  53. package/dist/packages/ui/src/index.d.ts +4 -0
  54. package/dist/packages/ui/src/observability.d.ts +22 -0
  55. package/dist/packages/ui/src/plugin-registry.d.ts +89 -0
  56. package/dist/packages/ui/src/settings-dialog.d.ts +25 -0
  57. package/dist/packages/ui/src/themes.d.ts +35 -0
  58. package/dist/preview-sw.js +53 -12
  59. package/dist/runtime.d.ts +3 -3
  60. package/dist/runtime.esm.js +467 -44
  61. package/dist/runtime.js +465 -44
  62. package/dist/schemas.d.ts +234 -814
  63. package/dist/schemas.esm.js +19401 -3725
  64. package/dist/schemas.js +19404 -3728
  65. package/dist/shell.d.ts +1 -1
  66. package/dist/shell.esm.js +364 -39
  67. package/dist/shell.js +364 -39
  68. package/dist/terminal.esm.js +62 -3
  69. package/dist/terminal.js +62 -3
  70. package/dist/workbench/diff-editor.d.ts +1 -0
  71. package/package.json +37 -8
  72. package/packages/commands/README.md +7 -0
  73. package/packages/commands/package.json +16 -0
  74. package/packages/commands/src/index.ts +467 -0
  75. package/packages/commands/tsconfig.json +22 -0
  76. package/packages/core/README.md +12 -0
  77. package/packages/core/package.json +13 -0
  78. package/packages/core/src/di.ts +147 -0
  79. package/packages/core/src/disposable.ts +112 -0
  80. package/packages/core/src/errors.ts +205 -0
  81. package/packages/core/src/events.ts +157 -0
  82. package/packages/core/src/index.ts +49 -0
  83. package/packages/core/src/path.ts +118 -0
  84. package/packages/core/src/registry.ts +61 -0
  85. package/packages/core/tsconfig.json +19 -0
  86. package/packages/editor/README.md +9 -0
  87. package/packages/editor/package.json +18 -0
  88. package/packages/editor/src/index.ts +344 -0
  89. package/packages/editor/tsconfig.json +24 -0
  90. package/packages/fs/README.md +7 -0
  91. package/packages/fs/package.json +16 -0
  92. package/packages/fs/src/backends.ts +301 -0
  93. package/packages/fs/src/index.ts +18 -0
  94. package/packages/fs/src/types.ts +44 -0
  95. package/packages/fs/src/vfs-service.ts +257 -0
  96. package/packages/fs/tsconfig.json +22 -0
  97. package/packages/preview/README.md +3 -0
  98. package/packages/preview/package.json +16 -0
  99. package/packages/preview/src/index.ts +125 -0
  100. package/packages/preview/tsconfig.json +22 -0
  101. package/packages/runtime/README.md +3 -0
  102. package/packages/runtime/package.json +17 -0
  103. package/packages/runtime/src/index.ts +134 -0
  104. package/packages/runtime/tsconfig.json +23 -0
  105. package/packages/settings/README.md +7 -0
  106. package/packages/settings/package.json +16 -0
  107. package/packages/settings/src/builtin-schemas.ts +235 -0
  108. package/packages/settings/src/configuration-service.ts +543 -0
  109. package/packages/settings/src/index.ts +37 -0
  110. package/packages/settings/src/jsonc.ts +189 -0
  111. package/packages/settings/src/schema.ts +164 -0
  112. package/packages/settings/tsconfig.json +22 -0
  113. package/packages/sql/README.md +12 -0
  114. package/packages/sql/package.json +13 -0
  115. package/packages/sql/src/completions.ts +359 -0
  116. package/packages/sql/src/diagnostics.ts +200 -0
  117. package/packages/sql/src/formatter.ts +271 -0
  118. package/packages/sql/src/index.ts +54 -0
  119. package/packages/sql/src/sql-editor.ts +802 -0
  120. package/packages/sql/src/styles.ts +501 -0
  121. package/packages/sql/src/tokenizer.ts +457 -0
  122. package/packages/sql/src/types.ts +148 -0
  123. package/packages/sql/tsconfig.json +19 -0
  124. package/packages/terminal/README.md +3 -0
  125. package/packages/terminal/package.json +16 -0
  126. package/packages/terminal/src/index.ts +183 -0
  127. package/packages/terminal/tsconfig.json +22 -0
  128. package/packages/ui/README.md +8 -0
  129. package/packages/ui/package.json +18 -0
  130. package/packages/ui/src/index.ts +36 -0
  131. package/packages/ui/src/observability.ts +175 -0
  132. package/packages/ui/src/plugin-registry.ts +274 -0
  133. package/packages/ui/src/settings-dialog.ts +293 -0
  134. package/packages/ui/src/themes.ts +174 -0
  135. package/packages/ui/tsconfig.json +24 -0
  136. package/src/code-lab.css +246 -0
  137. package/src/extensions/cpp.ts +58 -15
  138. package/src/extensions/python.ts +156 -24
  139. package/src/extensions/umbrella.ts +5 -0
  140. package/src/index.ts +1 -1
  141. package/src/modular-kernel.ts +11 -0
  142. package/src/preview-sw.js +52 -11
  143. package/src/runtime.ts +466 -46
  144. package/src/schemas.ts +62 -62
  145. package/src/shell.ts +363 -39
  146. package/src/terminal.ts +60 -2
  147. package/src/workbench/diff-editor.ts +66 -6
  148. package/src/workbench/icons.ts +5 -2
  149. package/src/workbench.ts +2410 -220
  150. package/dist/runtime/index.d.ts +0 -4
  151. package/dist/shell/coreutils/compilers-cli.d.ts +0 -24
  152. package/dist/shell/index.d.ts +0 -4
  153. package/src/runtime/index.ts +0 -5
  154. package/src/shell/coreutils/compilers-cli.ts +0 -103
  155. package/src/shell/index.ts +0 -5
package/src/shell.ts CHANGED
@@ -1593,9 +1593,11 @@
1593
1593
  function createFallbackVfs() {
1594
1594
  const files = new Map();
1595
1595
  const dirs = new Set(['/', '/tmp', '/workspace', '/etc', '/home', '/home/user']);
1596
+ let _activeStdin = '';
1596
1597
 
1597
1598
  function norm(p) {
1598
- return PosixPath.normalize(p.startsWith('/') ? p : '/workspace/' + p);
1599
+ const s = String(p);
1600
+ return PosixPath.normalize(s.startsWith('/') ? s : '/workspace/' + s);
1599
1601
  }
1600
1602
 
1601
1603
  const emitter = {
@@ -1683,6 +1685,12 @@
1683
1685
  return Array.from(children).sort();
1684
1686
  },
1685
1687
  readFileSync(p, encoding) {
1688
+ if (p === 0 || p === '/dev/stdin') {
1689
+ if (!encoding || encoding === 'buffer') {
1690
+ return new TextEncoder().encode(_activeStdin);
1691
+ }
1692
+ return _activeStdin;
1693
+ }
1686
1694
  const np = norm(p);
1687
1695
  const f = files.get(np);
1688
1696
  if (!f) {
@@ -1695,6 +1703,9 @@
1695
1703
  }
1696
1704
  return f.data;
1697
1705
  },
1706
+ _setActiveStdin(text) {
1707
+ _activeStdin = text === null || text === undefined ? '' : String(text);
1708
+ },
1698
1709
  writeFileSync(p, data) {
1699
1710
  const np = norm(p);
1700
1711
  const parent = PosixPath.dirname(np);
@@ -2953,9 +2964,31 @@
2953
2964
  '.gif': 'image/gif',
2954
2965
  '.webp': 'image/webp',
2955
2966
  '.ico': 'image/x-icon',
2967
+ '.woff': 'font/woff',
2968
+ '.woff2': 'font/woff2',
2969
+ '.ttf': 'font/ttf',
2970
+ '.otf': 'font/otf',
2971
+ '.wasm': 'application/wasm',
2972
+ '.pdf': 'application/pdf',
2973
+ '.mp3': 'audio/mpeg',
2974
+ '.mp4': 'video/mp4',
2975
+ '.webm': 'video/webm',
2956
2976
  '.txt': 'text/plain; charset=utf-8'
2957
2977
  };
2958
2978
 
2979
+ function isBinaryPreviewMime(mime) {
2980
+ const clean = String(mime || '').toLowerCase().split(';')[0].trim();
2981
+ if (!clean) return false;
2982
+ return !(
2983
+ clean.startsWith('text/') ||
2984
+ clean === 'application/javascript' ||
2985
+ clean === 'application/json' ||
2986
+ clean === 'application/xml' ||
2987
+ clean === 'application/xhtml+xml' ||
2988
+ clean === 'image/svg+xml'
2989
+ );
2990
+ }
2991
+
2959
2992
  function previewMimeFor(p) {
2960
2993
  const lower = String(p).toLowerCase();
2961
2994
  const dot = lower.lastIndexOf('.');
@@ -3139,7 +3172,8 @@
3139
3172
 
3140
3173
  function injectPreviewExtras(html, importMap) {
3141
3174
  let out = html;
3142
- const importMapTag = `<script type="importmap">\n${JSON.stringify({ imports: importMap }, null, 2)}\n</script>`;
3175
+ const safeImportMapJson = JSON.stringify({ imports: importMap }, null, 2).replace(/</g, '\\u003c');
3176
+ const importMapTag = `<script type="importmap">\n${safeImportMapJson}\n</script>`;
3143
3177
  if (/<head[^>]*>/i.test(out)) {
3144
3178
  out = out.replace(/<head[^>]*>/i, (m) => `${m}\n ${importMapTag}`);
3145
3179
  } else {
@@ -3395,6 +3429,20 @@
3395
3429
  );
3396
3430
  }
3397
3431
 
3432
+ const mimeType = previewMimeFor(target);
3433
+ if (isBinaryPreviewMime(mimeType)) {
3434
+ let rawBytes = null;
3435
+ try {
3436
+ rawBytes = ctx.vfs.readFileSync(target);
3437
+ } catch (err) {
3438
+ _shellLogger.debug('Binary preview asset read failed.', target, err);
3439
+ }
3440
+ const binaryBody = rawBytes instanceof Uint8Array
3441
+ ? rawBytes
3442
+ : (typeof rawBytes === 'string' ? new TextEncoder().encode(rawBytes) : new Uint8Array(0));
3443
+ return new Response(binaryBody, { status: 200, headers: { 'content-type': mimeType } });
3444
+ }
3445
+
3398
3446
  let body = readText(target);
3399
3447
  if (body === null) body = '';
3400
3448
  if (/\.html?$/i.test(target)) {
@@ -3410,7 +3458,7 @@
3410
3458
  }
3411
3459
  }
3412
3460
 
3413
- return new Response(body, { status: 200, headers: { 'content-type': previewMimeFor(target) } });
3461
+ return new Response(body, { status: 200, headers: { 'content-type': mimeType } });
3414
3462
  };
3415
3463
 
3416
3464
  const pid = (ctx.shell && typeof ctx.shell.nextPid === 'number') ? ctx.shell.nextPid++ : (ctx.pid || 1000);
@@ -5992,6 +6040,9 @@
5992
6040
  mode: 'repl',
5993
6041
  replCode: code,
5994
6042
  printReplResult: false,
6043
+ stdin: ctx.stdin || '',
6044
+ env: ctx.env,
6045
+ cwd: ctx.cwd,
5995
6046
  vfs: ctx.vfs,
5996
6047
  activePortTable: ctx.shell.portTable,
5997
6048
  pid: ctx.pid,
@@ -6009,6 +6060,9 @@
6009
6060
  mode: 'repl',
6010
6061
  replCode: code,
6011
6062
  printReplResult: true,
6063
+ stdin: ctx.stdin || '',
6064
+ env: ctx.env,
6065
+ cwd: ctx.cwd,
6012
6066
  vfs: ctx.vfs,
6013
6067
  activePortTable: ctx.shell.portTable,
6014
6068
  pid: ctx.pid,
@@ -6048,6 +6102,9 @@
6048
6102
  const res = await executeInVirtualRuntime({
6049
6103
  mode: 'test',
6050
6104
  entry: f,
6105
+ stdin: ctx.stdin || '',
6106
+ env: ctx.env,
6107
+ cwd: ctx.cwd,
6051
6108
  vfs: ctx.vfs,
6052
6109
  activePortTable: ctx.shell.portTable,
6053
6110
  pid: ctx.pid,
@@ -6073,6 +6130,9 @@
6073
6130
  mode: 'run',
6074
6131
  entry: scriptFile,
6075
6132
  argv: ['node', scriptFile, ...scriptArgs],
6133
+ stdin: ctx.stdin || '',
6134
+ env: ctx.env,
6135
+ cwd: ctx.cwd,
6076
6136
  vfs: ctx.vfs,
6077
6137
  activePortTable: ctx.shell.portTable,
6078
6138
  pid: ctx.pid,
@@ -6240,6 +6300,9 @@
6240
6300
  const res = await pyCompiler({
6241
6301
  entry: entryFile,
6242
6302
  source: codeToRun,
6303
+ stdin: ctx.stdin || '',
6304
+ env: ctx.env,
6305
+ cwd: ctx.cwd,
6243
6306
  vfs: ctx.vfs,
6244
6307
  onStdout: (t) => {
6245
6308
  outText += t;
@@ -6306,9 +6369,9 @@
6306
6369
  };
6307
6370
  }
6308
6371
  return {
6309
- code: 0,
6310
- stdout: `Successfully installed ${pkgs.join(', ') || 'packages'}\n`,
6311
- stderr: ''
6372
+ code: 1,
6373
+ stdout: '',
6374
+ stderr: 'pip: Python WebAssembly toolchain is not enabled. Enable pythonExtension from "@innovatorved/code-lab/extensions/python".\n'
6312
6375
  };
6313
6376
  }
6314
6377
  return { code: 0, stdout: 'pip 24.0 (Pyodide micropip)\n', stderr: '' };
@@ -6426,6 +6489,9 @@
6426
6489
  mode: 'repl',
6427
6490
  replCode: code,
6428
6491
  printReplResult: false,
6492
+ stdin: ctx.stdin || '',
6493
+ env: ctx.env,
6494
+ cwd: ctx.cwd,
6429
6495
  vfs: ctx.vfs,
6430
6496
  activePortTable: ctx.shell.portTable,
6431
6497
  pid: ctx.pid,
@@ -6465,6 +6531,9 @@
6465
6531
  const res = await executeInVirtualRuntime({
6466
6532
  mode: 'test',
6467
6533
  entry: f,
6534
+ stdin: ctx.stdin || '',
6535
+ env: ctx.env,
6536
+ cwd: ctx.cwd,
6468
6537
  vfs: ctx.vfs,
6469
6538
  activePortTable: ctx.shell.portTable,
6470
6539
  pid: ctx.pid,
@@ -6492,6 +6561,9 @@
6492
6561
  mode: 'run',
6493
6562
  entry: resolved,
6494
6563
  argv: ['bun', resolved, ...scriptArgs],
6564
+ stdin: ctx.stdin || '',
6565
+ env: ctx.env,
6566
+ cwd: ctx.cwd,
6495
6567
  vfs: ctx.vfs,
6496
6568
  activePortTable: ctx.shell.portTable,
6497
6569
  pid: ctx.pid,
@@ -6516,6 +6588,9 @@
6516
6588
  mode: 'run',
6517
6589
  entry: resolved,
6518
6590
  argv: ['bun', resolved, ...scriptArgs],
6591
+ stdin: ctx.stdin || '',
6592
+ env: ctx.env,
6593
+ cwd: ctx.cwd,
6519
6594
  vfs: ctx.vfs,
6520
6595
  activePortTable: ctx.shell.portTable,
6521
6596
  pid: ctx.pid,
@@ -6797,12 +6872,24 @@
6797
6872
  if (!method) method = 'GET';
6798
6873
 
6799
6874
  const portTable = (ctx.shell && ctx.shell.portTable) || null;
6875
+ const fetchImpl = (ctx.shell && ctx.shell.options && typeof (ctx.shell.options.fetch || ctx.shell.options.fetchImpl) === 'function')
6876
+ ? (ctx.shell.options.fetch || ctx.shell.options.fetchImpl)
6877
+ : fetch;
6878
+ let isLoopback = false;
6879
+ try {
6880
+ const parsedUrl = new URL(url);
6881
+ const host = parsedUrl.hostname.toLowerCase();
6882
+ isLoopback = host === 'localhost' || host === '127.0.0.1' || host === '0.0.0.0' || host === '::1' || host === '[::1]';
6883
+ } catch (err) {
6884
+ _shellLogger.debug('curl URL parse fallback', url, err);
6885
+ }
6800
6886
  let resp = null;
6801
6887
  let lastErr = null;
6888
+ const maxAttempts = portTable && isLoopback ? 25 : 1;
6802
6889
 
6803
- for (let attempt = 0; attempt < 25; attempt++) {
6890
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
6804
6891
  try {
6805
- if (portTable) {
6892
+ if (portTable && isLoopback) {
6806
6893
  resp = await portTable.dispatchRequest(url, {
6807
6894
  method,
6808
6895
  headers,
@@ -6811,7 +6898,7 @@
6811
6898
  lastErr = null;
6812
6899
  break;
6813
6900
  } else {
6814
- resp = await fetch(url, {
6901
+ resp = await fetchImpl(url, {
6815
6902
  method,
6816
6903
  headers,
6817
6904
  body: data
@@ -6822,7 +6909,9 @@
6822
6909
  } catch (err) {
6823
6910
  lastErr = err;
6824
6911
  if (err && err.chaosEffect) break;
6825
- await new Promise(r => setTimeout(r, 15));
6912
+ if (attempt + 1 < maxAttempts) {
6913
+ await new Promise(r => setTimeout(r, 15));
6914
+ }
6826
6915
  }
6827
6916
  }
6828
6917
 
@@ -6901,6 +6990,17 @@
6901
6990
  concurrency = Math.min(concurrency, totalRequests);
6902
6991
 
6903
6992
  const portTable = (ctx.shell && ctx.shell.portTable) || null;
6993
+ const fetchImpl = (ctx.shell && ctx.shell.options && typeof (ctx.shell.options.fetch || ctx.shell.options.fetchImpl) === 'function')
6994
+ ? (ctx.shell.options.fetch || ctx.shell.options.fetchImpl)
6995
+ : fetch;
6996
+ let isLoopback = false;
6997
+ try {
6998
+ const parsedUrl = new URL(url);
6999
+ const host = parsedUrl.hostname.toLowerCase();
7000
+ isLoopback = host === 'localhost' || host === '127.0.0.1' || host === '0.0.0.0' || host === '::1' || host === '[::1]';
7001
+ } catch (err) {
7002
+ _shellLogger.debug('hey URL parse fallback', url, err);
7003
+ }
6904
7004
  const latencies = [];
6905
7005
  const statusCounts = {};
6906
7006
  const errorCounts = {};
@@ -6916,10 +7016,10 @@
6916
7016
  const reqStart = (typeof performance !== 'undefined' && performance.now) ? performance.now() : Date.now();
6917
7017
  try {
6918
7018
  let resp;
6919
- if (portTable) {
7019
+ if (portTable && isLoopback) {
6920
7020
  resp = await portTable.dispatchRequest(url, { method, headers: { ...headers }, body });
6921
7021
  } else {
6922
- resp = await fetch(url, { method, headers: { ...headers }, body });
7022
+ resp = await fetchImpl(url, { method, headers: { ...headers }, body });
6923
7023
  }
6924
7024
  const reqEnd = (typeof performance !== 'undefined' && performance.now) ? performance.now() : Date.now();
6925
7025
  const dur = Math.max(0.05, reqEnd - reqStart);
@@ -7852,6 +7952,15 @@
7852
7952
  }
7853
7953
 
7854
7954
  this.history.push(trimmed);
7955
+ if (this.history.length > 1000) {
7956
+ this.history.splice(0, this.history.length - 1000);
7957
+ }
7958
+ if (this.jobs.size > 256) {
7959
+ for (const [jid, j] of this.jobs.entries()) {
7960
+ if (this.jobs.size <= 256) break;
7961
+ if (j && j.status !== 'Running') this.jobs.delete(jid);
7962
+ }
7963
+ }
7855
7964
 
7856
7965
  const cwd = execOptions.cwd ? this.resolvePath(execOptions.cwd) : this.cwd;
7857
7966
  this.cwd = cwd;
@@ -7921,6 +8030,10 @@
7921
8030
  if (res.aborted || (execOptions.signal && execOptions.signal.aborted)) {
7922
8031
  return { code: 130, exitCode: 130, stdout: combinedStdout, stderr: combinedStderr, aborted: true };
7923
8032
  }
8033
+ if (res.controlFlow && res.controlFlow.type === 'exit') {
8034
+ lastCode = res.controlFlow.code !== undefined ? res.controlFlow.code : lastCode;
8035
+ break;
8036
+ }
7924
8037
  }
7925
8038
  }
7926
8039
 
@@ -7931,6 +8044,218 @@
7931
8044
  return this.execute(commandLine, execOptions);
7932
8045
  }
7933
8046
 
8047
+ async _expandHeredocBody(rawBody, cmdEnv) {
8048
+ const src = String(rawBody || '');
8049
+ let out = '';
8050
+ let i = 0;
8051
+ while (i < src.length) {
8052
+ const ch = src[i];
8053
+ if (ch === '\\' && i + 1 < src.length && '$`\\\n'.includes(src[i + 1])) {
8054
+ if (src[i + 1] === '\n') {
8055
+ i += 2;
8056
+ continue;
8057
+ }
8058
+ out += src[i + 1];
8059
+ i += 2;
8060
+ continue;
8061
+ }
8062
+ if (ch === '$' && src[i + 1] === '(' && src[i + 2] === '(') {
8063
+ i += 3;
8064
+ let depth = 2;
8065
+ let expr = '';
8066
+ while (i < src.length && depth > 0) {
8067
+ if (src[i] === '(') { depth++; expr += '('; i++; }
8068
+ else if (src[i] === ')') {
8069
+ if (depth === 2 && src[i + 1] === ')') { depth = 0; i += 2; break; }
8070
+ depth--;
8071
+ if (depth > 0) expr += ')';
8072
+ i++;
8073
+ } else {
8074
+ expr += src[i++];
8075
+ }
8076
+ }
8077
+ out += evaluateArithmetic(expr, cmdEnv, this.env);
8078
+ continue;
8079
+ }
8080
+ if (ch === '$' && src[i + 1] === '(') {
8081
+ i += 2;
8082
+ let depth = 1;
8083
+ let sub = '';
8084
+ while (i < src.length && depth > 0) {
8085
+ if (src[i] === '(') depth++;
8086
+ else if (src[i] === ')') depth--;
8087
+ if (depth > 0) sub += src[i];
8088
+ i++;
8089
+ }
8090
+ out += await this._executeSubshell(sub, cmdEnv);
8091
+ continue;
8092
+ }
8093
+ if (ch === '`') {
8094
+ i++;
8095
+ let sub = '';
8096
+ while (i < src.length && src[i] !== '`') sub += src[i++];
8097
+ if (i < src.length && src[i] === '`') i++;
8098
+ out += await this._executeSubshell(sub, cmdEnv);
8099
+ continue;
8100
+ }
8101
+ if (ch === '$') {
8102
+ i++;
8103
+ if (src[i] === '{') {
8104
+ i++;
8105
+ let varName = '';
8106
+ let bDepth = 1;
8107
+ while (i < src.length && bDepth > 0) {
8108
+ if (src[i] === '{') bDepth++;
8109
+ else if (src[i] === '}') bDepth--;
8110
+ if (bDepth > 0) varName += src[i];
8111
+ i++;
8112
+ }
8113
+ out += this._expandVariable(varName, cmdEnv);
8114
+ } else if (i < src.length && /[0-9?!$#@*]/.test(src[i])) {
8115
+ out += this._expandVariable(src[i++], cmdEnv);
8116
+ } else if (i < src.length && /[a-zA-Z_]/.test(src[i])) {
8117
+ let varName = '';
8118
+ while (i < src.length && /[a-zA-Z0-9_]/.test(src[i])) varName += src[i++];
8119
+ out += this._expandVariable(varName, cmdEnv);
8120
+ } else {
8121
+ out += '$';
8122
+ }
8123
+ continue;
8124
+ }
8125
+ out += ch;
8126
+ i++;
8127
+ }
8128
+ return out;
8129
+ }
8130
+
8131
+ async _resolveInputRedirect(red, cmdEnv) {
8132
+ if (red.type === '<') {
8133
+ const target = await this.evaluateWord(red.target, cmdEnv);
8134
+ const absTarget = this.resolvePath(target);
8135
+ if (!this.vfs.existsSync(absTarget)) {
8136
+ return { ok: false, error: { code: 1, stdout: '', stderr: `shell: ${target}: No such file or directory\n` } };
8137
+ }
8138
+ try {
8139
+ return { ok: true, stdin: this.vfs.readFileSync(absTarget, 'utf8') };
8140
+ } catch (err) {
8141
+ return { ok: false, error: { code: 1, stdout: '', stderr: `shell: ${target}: ${err.message}\n` } };
8142
+ }
8143
+ }
8144
+ if (red.type === '<<<') {
8145
+ const val = await this.evaluateWord(red.target, cmdEnv);
8146
+ return { ok: true, stdin: val + '\n' };
8147
+ }
8148
+ if (red.type === '<<') {
8149
+ let body = String(red.body || '');
8150
+ if (red.stripTabs) {
8151
+ body = body.replace(/^\t+/gm, '');
8152
+ }
8153
+ if (!red.quoted) {
8154
+ body = await this._expandHeredocBody(body, cmdEnv);
8155
+ }
8156
+ return { ok: true, stdin: body };
8157
+ }
8158
+ return null;
8159
+ }
8160
+
8161
+ _evaluateDoubleBracket(rawArgs) {
8162
+ const closeIdx = rawArgs.lastIndexOf(']]');
8163
+ if (closeIdx === -1) {
8164
+ return { code: 2, stdout: '', stderr: 'shell: [[: missing `]]`\n' };
8165
+ }
8166
+ const tokens = rawArgs.slice(0, closeIdx);
8167
+ let idx = 0;
8168
+
8169
+ const globToRegExp = (pat) => {
8170
+ let re = '^';
8171
+ for (let i = 0; i < pat.length; i++) {
8172
+ const c = pat[i];
8173
+ if (c === '*') re += '.*';
8174
+ else if (c === '?') re += '.';
8175
+ else re += c.replace(/[.+^${}()|[\]\\]/g, '\\$&');
8176
+ }
8177
+ return new RegExp(re + '$');
8178
+ };
8179
+
8180
+ const parseOr = () => {
8181
+ let left = parseAnd();
8182
+ while (tokens[idx] === '||') {
8183
+ idx++;
8184
+ const right = parseAnd();
8185
+ left = left || right;
8186
+ }
8187
+ return left;
8188
+ };
8189
+
8190
+ const parseAnd = () => {
8191
+ let left = parseNot();
8192
+ while (tokens[idx] === '&&') {
8193
+ idx++;
8194
+ const right = parseNot();
8195
+ left = left && right;
8196
+ }
8197
+ return left;
8198
+ };
8199
+
8200
+ const parseNot = () => {
8201
+ if (tokens[idx] === '!') {
8202
+ idx++;
8203
+ return !parseNot();
8204
+ }
8205
+ return parsePrimary();
8206
+ };
8207
+
8208
+ const parsePrimary = () => {
8209
+ if (idx >= tokens.length) return false;
8210
+ if (tokens[idx] === '(') {
8211
+ idx++;
8212
+ const val = parseOr();
8213
+ if (tokens[idx] === ')') idx++;
8214
+ return val;
8215
+ }
8216
+ const unary = tokens[idx];
8217
+ if (/^-(?:z|n|e|f|d|s)$/.test(unary) && idx + 1 < tokens.length) {
8218
+ idx++;
8219
+ const operand = String(tokens[idx++] ?? '');
8220
+ if (unary === '-z') return operand.length === 0;
8221
+ if (unary === '-n') return operand.length > 0;
8222
+ const abs = this.resolvePath(operand);
8223
+ const exists = this.vfs.existsSync(abs);
8224
+ if (unary === '-e') return exists;
8225
+ if (!exists) return false;
8226
+ const st = this.vfs.statSync(abs);
8227
+ if (unary === '-d') return Boolean(st && st.isDirectory());
8228
+ if (unary === '-f') return Boolean(st && !st.isDirectory());
8229
+ if (unary === '-s') return Boolean(st && !st.isDirectory() && st.size > 0);
8230
+ }
8231
+ const left = String(tokens[idx++] ?? '');
8232
+ const op = tokens[idx];
8233
+ if (op && /^(?:==|=|!=|=~|<|>|-eq|-ne|-lt|-le|-gt|-ge)$/.test(op) && idx + 1 < tokens.length) {
8234
+ idx++;
8235
+ const right = String(tokens[idx++] ?? '');
8236
+ if (op === '==' || op === '=') return globToRegExp(right).test(left);
8237
+ if (op === '!=') return !globToRegExp(right).test(left);
8238
+ if (op === '=~') {
8239
+ try { return new RegExp(right).test(left); } catch (err) { _shellLogger.debug('[[ =~ invalid regex', right, err); return false; }
8240
+ }
8241
+ if (op === '<') return left < right;
8242
+ if (op === '>') return left > right;
8243
+ const ln = Number(left) || 0;
8244
+ const rn = Number(right) || 0;
8245
+ if (op === '-eq') return ln === rn;
8246
+ if (op === '-ne') return ln !== rn;
8247
+ if (op === '-lt') return ln < rn;
8248
+ if (op === '-le') return ln <= rn;
8249
+ if (op === '-gt') return ln > rn;
8250
+ if (op === '-ge') return ln >= rn;
8251
+ }
8252
+ return left.length > 0;
8253
+ };
8254
+
8255
+ const ok = parseOr();
8256
+ return { code: ok ? 0 : 1, stdout: '', stderr: '' };
8257
+ }
8258
+
7934
8259
  async _executeWithRedirects(redirects, context, execFn) {
7935
8260
  if (!redirects || redirects.length === 0) {
7936
8261
  return execFn(context);
@@ -7942,22 +8267,15 @@
7942
8267
  let effectiveStdin = context.stdin || '';
7943
8268
 
7944
8269
  for (const red of redirects) {
7945
- if (red.type === '<') {
7946
- const target = await this.evaluateWord(red.target, cmdEnv);
7947
- const absTarget = this.resolvePath(target);
7948
- if (!this.vfs.existsSync(absTarget)) {
7949
- return { code: 1, stdout: '', stderr: `shell: ${target}: No such file or directory\n` };
7950
- }
7951
- try {
7952
- effectiveStdin = this.vfs.readFileSync(absTarget, 'utf8');
7953
- } catch (err) {
7954
- return { code: 1, stdout: '', stderr: `shell: ${target}: ${err.message}\n` };
7955
- }
8270
+ if (red.type === '<' || red.type === '<<' || red.type === '<<<') {
8271
+ const inRes = await this._resolveInputRedirect(red, cmdEnv);
8272
+ if (inRes && !inRes.ok) return inRes.error;
8273
+ if (inRes && inRes.ok) effectiveStdin = inRes.stdin;
7956
8274
  }
7957
8275
  }
7958
8276
 
7959
8277
  for (const red of redirects) {
7960
- if (red.type === '<') continue;
8278
+ if (red.type === '<' || red.type === '<<' || red.type === '<<<') continue;
7961
8279
  if (red.type === '>' || red.type === '>>' || red.type === '&>') {
7962
8280
  const target = await this.evaluateWord(red.target, cmdEnv);
7963
8281
  const abs = this.resolvePath(target);
@@ -8466,7 +8784,20 @@
8466
8784
  args.push(...expanded);
8467
8785
  }
8468
8786
 
8469
- // 3. Built-in flow control
8787
+ // 3. Built-in flow control & POSIX special builtins
8788
+ if (cmdName === ':') {
8789
+ Object.assign(this.env, cmdEnv);
8790
+ Object.assign(context.env, cmdEnv);
8791
+ return { code: 0, stdout: '', stderr: '' };
8792
+ }
8793
+ if (cmdName === 'exit') {
8794
+ const rawCode = args.length > 0 ? parseInt(args[0], 10) : parseInt(this.env['?'] || '0', 10);
8795
+ const code = Number.isNaN(rawCode) ? 2 : ((rawCode % 256) + 256) % 256;
8796
+ return { code, stdout: '', stderr: '', controlFlow: { type: 'exit', code } };
8797
+ }
8798
+ if (cmdName === '[[') {
8799
+ return this._evaluateDoubleBracket(args);
8800
+ }
8470
8801
  if (cmdName === 'break') {
8471
8802
  const count = parseInt(args[0], 10) || 1;
8472
8803
  return { code: 0, stdout: '', stderr: '', controlFlow: { type: 'break', count } };
@@ -8484,20 +8815,13 @@
8484
8815
  return { code: 130, stdout: '', stderr: '', aborted: true };
8485
8816
  }
8486
8817
 
8487
- // 4. Process input redirection (<, 0<)
8818
+ // 4. Process input redirection (<, 0<, <<, <<-, <<<)
8488
8819
  let effectiveStdin = context.stdin || '';
8489
8820
  for (const red of cmdNode.redirects) {
8490
- if (red.type === '<') {
8491
- const target = await this.evaluateWord(red.target, cmdEnv);
8492
- const absTarget = this.resolvePath(target);
8493
- if (!this.vfs.existsSync(absTarget)) {
8494
- return { code: 1, stdout: '', stderr: `shell: ${target}: No such file or directory\n` };
8495
- }
8496
- try {
8497
- effectiveStdin = this.vfs.readFileSync(absTarget, 'utf8');
8498
- } catch (err) {
8499
- return { code: 1, stdout: '', stderr: `shell: ${target}: ${err.message}\n` };
8500
- }
8821
+ if (red.type === '<' || red.type === '<<' || red.type === '<<<') {
8822
+ const inRes = await this._resolveInputRedirect(red, cmdEnv);
8823
+ if (inRes && !inRes.ok) return inRes.error;
8824
+ if (inRes && inRes.ok) effectiveStdin = inRes.stdin;
8501
8825
  }
8502
8826
  }
8503
8827
 
@@ -8506,7 +8830,7 @@
8506
8830
  let fd2 = { type: 'stderr' };
8507
8831
 
8508
8832
  for (const red of cmdNode.redirects) {
8509
- if (red.type === '<') continue;
8833
+ if (red.type === '<' || red.type === '<<' || red.type === '<<<') continue;
8510
8834
  if (red.type === '>' || red.type === '>>' || red.type === '&>') {
8511
8835
  const target = await this.evaluateWord(red.target, cmdEnv);
8512
8836
  const abs = this.resolvePath(target);
@@ -8758,7 +9082,7 @@ export const tokenizeShell: (input: string) => any[] = _hostShell.tokenizeShell;
8758
9082
  export const PosixShell: any = _hostShell.PosixShell;
8759
9083
  export const COREUTILS: Record<string, any> = _hostShell.COREUTILS;
8760
9084
  export const PosixPath: any = _hostShell.PosixPath;
8761
- export const expandGlob: (pattern: string, vfs: any, cwd?: string) => string[] = _hostShell.expandGlob;
9085
+ export const expandGlob: (pattern: string, cwd: string, vfs: any) => string[] = _hostShell.expandGlob;
8762
9086
  export const createVirtualPortTable: () => any = _hostShell.createVirtualPortTable;
8763
9087
  export const parseGitHubRepoUrl: (input: string) => any = _hostShell.parseGitHubRepoUrl;
8764
9088
  export const cloneGitHubRepository: (repoInput: string, vfs: any, options?: any) => Promise<any> = _hostShell.cloneGitHubRepository;