brsh 3.9.0 → 3.10.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 (49) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/Readme.md +22 -4
  3. package/bin/Readme.md +33 -3
  4. package/bin/exportFileSystem.js +127 -110
  5. package/bin/wbt.js +118 -0
  6. package/dist/shell.es.js +4074 -3717
  7. package/dist/shell.es.js.map +1 -1
  8. package/dist/shell.min.js +26 -25
  9. package/dist/shell.min.js.map +1 -1
  10. package/dist/terminal.cjs.js +3 -3
  11. package/dist/terminal.cjs.js.map +1 -1
  12. package/dist/terminal.es.js +55 -33
  13. package/dist/terminal.es.js.map +1 -1
  14. package/dist/terminal.min.js +3 -3
  15. package/dist/terminal.min.js.map +1 -1
  16. package/dist/wbt.es.js +3082 -2710
  17. package/dist/wbt.es.js.map +1 -1
  18. package/dist/wbt.min.js +16 -12
  19. package/dist/wbt.min.js.map +1 -1
  20. package/index.js +102 -80
  21. package/lib/BaseFileSystem.js +57 -2
  22. package/lib/BinaryContainer.js +1 -1
  23. package/lib/Command.js +1 -1
  24. package/lib/Context.js +4 -3
  25. package/lib/FileSystem.js +4 -2
  26. package/lib/InteractiveCommand.js +9 -8
  27. package/lib/JsCommand.js +113 -0
  28. package/lib/NodeFileSystem.js +164 -30
  29. package/lib/Readme.md +1 -0
  30. package/lib/ScriptCommand.js +148 -8
  31. package/lib/Terminal.js +60 -6
  32. package/lib/TtyStream.js +36 -2
  33. package/lib/WasmCommand.js +313 -66
  34. package/lib/buildFilesystemStructure.js +38 -24
  35. package/lib/isUtf8Text.js +47 -0
  36. package/lib/local/which.js +8 -3
  37. package/lib/wbt/index.js +473 -175
  38. package/lib/wbt/registry.js +21 -1
  39. package/package.json +3 -2
  40. package/test/devices-signals.test.js +112 -0
  41. package/test/doom.test.js +7 -4
  42. package/test/exportFileSystem.test.js +37 -15
  43. package/test/js-command.test.js +125 -0
  44. package/test/node-filesystem.test.js +7 -0
  45. package/test/scripts.test.js +30 -0
  46. package/test/terminal.test.js +148 -0
  47. package/test/wasm-cli.test.js +76 -0
  48. package/test/wbt.test.js +418 -10
  49. package/test/wbtCli.test.js +160 -0
@@ -2,6 +2,8 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
+ const { SymlinkRef } = require('./BaseFileSystem');
6
+ const isUtf8Text = require('./isUtf8Text');
5
7
 
6
8
  const IGNORE = ['.DS_Store'];
7
9
 
@@ -23,9 +25,13 @@ function _isWasmBuffer(buf) {
23
25
  /**
24
26
  * Recursively builds a filesystem structure object from a real directory.
25
27
  * Returns { filesystem, permissions } where:
26
- * - filesystem is the nested content tree (directories as objects, files as require() strings)
28
+ * - filesystem is the nested content tree (directories as objects, files as strings or BinaryFiles/SymlinkRefs)
27
29
  * - permissions maps virtual absolute paths to Unix mode bits (e.g. { '/bin/script.sh': 0o755 })
28
30
  *
31
+ * Real filenames and extensions (.js, .wasm, etc.) are preserved verbatim.
32
+ * OS symlinks are serialized as SymlinkRef instances.
33
+ * Files are checked for UTF-8/ASCII text content; anything that is not valid text defaults to binary.
34
+ *
29
35
  * Throws if `root` cannot be read.
30
36
  */
31
37
  function buildStructure(root) {
@@ -42,42 +48,50 @@ function _buildTree(root, virtualPrefix, permissions) {
42
48
  return structure;
43
49
  }
44
50
  try {
45
- const stat = fs.lstatSync(path.join(root, handle));
51
+ const filePath = path.join(root, handle);
52
+ const stat = fs.lstatSync(filePath);
53
+
54
+ if(stat.isSymbolicLink()) {
55
+ const target = fs.readlinkSync(filePath);
56
+ structure[handle] = new SymlinkRef(target);
57
+ permissions[virtualPrefix + '/' + handle] = stat.mode & 0o777;
58
+ return structure;
59
+ }
60
+
46
61
  if(stat.isDirectory()) {
47
62
  structure[handle] = _buildTree(
48
- path.join(root, handle),
63
+ filePath,
49
64
  virtualPrefix + '/' + handle,
50
65
  permissions
51
66
  );
52
67
  return structure;
53
68
  }
54
69
 
55
- const filePath = path.join(root, handle);
56
70
  const isJavaScript = handle.endsWith('.js');
57
71
  const ext = path.extname(handle).toLowerCase();
58
- const rawBuf = isJavaScript ? null : fs.readFileSync(filePath);
59
- const isWasmMagic = rawBuf !== null && _isWasmBuffer(rawBuf);
60
- const isBinary = (ext in BINARY_MIME) || isWasmMagic;
61
- const mime = BINARY_MIME[ext] || (isWasmMagic ? 'application/wasm' : 'application/octet-stream');
62
- const fileContents = isJavaScript
63
- ? null
64
- : isBinary
65
- ? `data:${mime};base64,${rawBuf.toString('base64')}`
66
- : rawBuf.toString('utf8');
72
+ const rawBuf = fs.readFileSync(filePath);
73
+ const isWasmMagic = _isWasmBuffer(rawBuf);
74
+ const isKnownBinary = (ext in BINARY_MIME) || isWasmMagic;
67
75
 
68
- const isWasmExt = ext === '.wasm';
69
- const key = isJavaScript
70
- ? handle.substring(0, handle.length - 3)
71
- : isWasmExt
72
- ? handle.substring(0, handle.length - 5)
73
- : handle;
76
+ let fileContents;
77
+ if (isJavaScript) {
78
+ // Preserved as UTF-8 text string (bundled with esbuild later during export)
79
+ fileContents = rawBuf.toString('utf8');
80
+ } else if (isKnownBinary) {
81
+ const mime = BINARY_MIME[ext] || (isWasmMagic ? 'application/wasm' : 'application/octet-stream');
82
+ fileContents = `data:${mime};base64,${rawBuf.toString('base64')}`;
83
+ } else if (isUtf8Text(rawBuf)) {
84
+ fileContents = rawBuf.toString('utf8');
85
+ } else {
86
+ // Content-based binary fallback for unknown binary formats (.wad, .gguf, ELF, images, etc.)
87
+ const mime = 'application/octet-stream';
88
+ fileContents = `data:${mime};base64,${rawBuf.toString('base64')}`;
89
+ }
74
90
 
75
- structure[key] = isJavaScript
76
- ? `require('${path.resolve(root, handle)}')`
77
- : fileContents;
91
+ structure[handle] = fileContents;
78
92
 
79
93
  // Record real Unix permission bits for this file
80
- permissions[virtualPrefix + '/' + key] = stat.mode & 0o777;
94
+ permissions[virtualPrefix + '/' + handle] = stat.mode & 0o777;
81
95
  } catch(fe) {
82
96
  // Silently skip files/dirs that are inaccessible
83
97
  }
@@ -85,4 +99,4 @@ function _buildTree(root, virtualPrefix, permissions) {
85
99
  }, {});
86
100
  }
87
101
 
88
- module.exports = { buildStructure, IGNORE };
102
+ module.exports = { buildStructure, IGNORE, BINARY_MIME, _isWasmBuffer };
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Checks whether a byte buffer contains valid UTF-8/ASCII text.
5
+ * Returns true if the buffer is text, false if it should be treated as binary.
6
+ *
7
+ * Rules:
8
+ * - Empty buffers are considered valid text.
9
+ * - Checks for NUL bytes (0x00) or non-printable ASCII control characters.
10
+ * Valid text allows tabs (\t, 0x09), line feeds (\n, 0x0A), carriage returns (\r, 0x0D),
11
+ * and ANSI escape sequences (ESC, 0x1B).
12
+ * Characters 0x00-0x08, 0x0B, 0x0C, 0x0E-0x1A, 0x1C-0x1F are treated as binary.
13
+ * - Uses TextDecoder('utf-8', { fatal: true }) to verify strict UTF-8 validity.
14
+ */
15
+ function isUtf8Text(buf) {
16
+ if (!buf || buf.length === 0) return true;
17
+
18
+ const u8 = buf instanceof Uint8Array
19
+ ? buf
20
+ : (typeof Buffer !== 'undefined' && Buffer.isBuffer(buf))
21
+ ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
22
+ : (buf instanceof ArrayBuffer || ArrayBuffer.isView(buf))
23
+ ? new Uint8Array(buf.buffer || buf, buf.byteOffset || 0, buf.byteLength)
24
+ : null;
25
+
26
+ if (!u8) return false;
27
+
28
+ // Fast check first 8KB (or entire buffer if smaller) for binary control bytes
29
+ const checkLength = Math.min(u8.length, 8192);
30
+ for (let i = 0; i < checkLength; i++) {
31
+ const b = u8[i];
32
+ if (b === 0x00) return false;
33
+ if (b < 0x20 && b !== 0x09 && b !== 0x0A && b !== 0x0D && b !== 0x1B) {
34
+ return false;
35
+ }
36
+ }
37
+
38
+ try {
39
+ new TextDecoder('utf-8', { fatal: true }).decode(u8);
40
+ return true;
41
+ } catch {
42
+ return false;
43
+ }
44
+ }
45
+
46
+ module.exports = isUtf8Text;
47
+ module.exports.isUtf8Text = isUtf8Text;
@@ -10,13 +10,18 @@ class Which extends LocalCommand {
10
10
  return Promise.reject('which requires a single argument');
11
11
  }
12
12
 
13
- const file = this.context.fs.getFileByPath(`${this.context.getVar('PATH')}/${this.arguments[0]}`);
13
+ const cmdPath = `${this.context.getVar('PATH')}/${this.arguments[0]}`;
14
+ const file = this.context.fs.getFileByPath(cmdPath);
14
15
 
15
- if(!file || file.constructor === Object || file.constructor === String) {
16
+ if(!file || file.constructor === Object) {
16
17
  return Promise.reject();
17
18
  }
18
19
 
19
- return Promise.resolve(`${this.context.getVar('PATH')}/${this.arguments[0]}`);
20
+ if(file.constructor === String && !file.startsWith('#!') && !this.context.fs.isExecutable(cmdPath)) {
21
+ return Promise.reject();
22
+ }
23
+
24
+ return Promise.resolve(cmdPath);
20
25
  }
21
26
  }
22
27