brsh 2.6.0 → 2.8.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 (46) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/Readme.md +145 -1
  3. package/bin/Readme.md +6 -3
  4. package/bin/brsh-shell.js +68 -9
  5. package/bin/exportFileSystem.js +192 -80
  6. package/dist/shell.es.js +2497 -1300
  7. package/dist/shell.es.js.map +1 -1
  8. package/dist/shell.min.js +21 -14
  9. package/dist/shell.min.js.map +1 -1
  10. package/dist/terminal.cjs.js +101 -0
  11. package/dist/terminal.cjs.js.map +1 -0
  12. package/dist/terminal.es.js +290 -6965
  13. package/dist/terminal.es.js.map +1 -1
  14. package/dist/terminal.min.js +4 -25
  15. package/dist/terminal.min.js.map +1 -1
  16. package/dist/wbt.es.js +3974 -0
  17. package/dist/wbt.es.js.map +1 -0
  18. package/dist/wbt.min.js +10 -0
  19. package/dist/wbt.min.js.map +1 -0
  20. package/index.js +160 -20
  21. package/lib/BinaryContainer.js +528 -0
  22. package/lib/Command.js +2 -1
  23. package/lib/FileSystem.js +31 -0
  24. package/lib/NodeFileSystem.js +19 -9
  25. package/lib/ReplCommand.js +520 -0
  26. package/lib/ScriptCommand.js +1 -1
  27. package/lib/Terminal.js +45 -10
  28. package/lib/WasmCommand.js +1123 -35
  29. package/lib/buildFilesystemStructure.js +15 -4
  30. package/lib/local/curl.js +60 -2
  31. package/lib/wbt/index.js +756 -0
  32. package/lib/wbt/packages/wllama.js +1051 -0
  33. package/lib/wbt/registry.js +193 -0
  34. package/lib/wbt/utils.js +118 -0
  35. package/package.json +11 -3
  36. package/test/binary-filesystem.test.js +248 -0
  37. package/test/curl.test.js +166 -0
  38. package/test/exportFileSystem.test.js +78 -0
  39. package/test/terminal.test.js +113 -0
  40. package/test/wasm-cli.test.js +127 -0
  41. package/test/wbt.test.js +1175 -0
  42. package/vite.config.demo.js +4 -0
  43. package/vite.config.js +4 -0
  44. package/vite.config.terminal.js +16 -12
  45. package/vite.config.wbt.js +21 -0
  46. package/vitest.config.js +6 -0
@@ -13,8 +13,13 @@ const BINARY_MIME = {
13
13
  '.bmp': 'image/bmp',
14
14
  '.webp': 'image/webp',
15
15
  '.ico': 'image/x-icon',
16
+ '.wasm': 'application/wasm',
16
17
  };
17
18
 
19
+ function _isWasmBuffer(buf) {
20
+ return buf.length >= 4 && buf[0] === 0x00 && buf[1] === 0x61 && buf[2] === 0x73 && buf[3] === 0x6d;
21
+ }
22
+
18
23
  /**
19
24
  * Recursively builds a filesystem structure object from a real directory.
20
25
  * Returns { filesystem, permissions } where:
@@ -50,16 +55,22 @@ function _buildTree(root, virtualPrefix, permissions) {
50
55
  const filePath = path.join(root, handle);
51
56
  const isJavaScript = handle.endsWith('.js');
52
57
  const ext = path.extname(handle).toLowerCase();
53
- const isBinary = ext in BINARY_MIME;
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');
54
62
  const fileContents = isJavaScript
55
63
  ? null
56
64
  : isBinary
57
- ? `data:${BINARY_MIME[ext]};base64,${fs.readFileSync(filePath).toString('base64')}`
58
- : fs.readFileSync(filePath, 'utf8');
65
+ ? `data:${mime};base64,${rawBuf.toString('base64')}`
66
+ : rawBuf.toString('utf8');
59
67
 
68
+ const isWasmExt = ext === '.wasm';
60
69
  const key = isJavaScript
61
70
  ? handle.substring(0, handle.length - 3)
62
- : handle;
71
+ : isWasmExt
72
+ ? handle.substring(0, handle.length - 5)
73
+ : handle;
63
74
 
64
75
  structure[key] = isJavaScript
65
76
  ? `require('${path.resolve(root, handle)}')`
package/lib/local/curl.js CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  const LocalCommand = require('./LocalCommand');
4
4
 
5
+ function formatBytes(bytes) {
6
+ if (bytes >= 1048576) return (bytes / 1048576).toFixed(1) + ' MB';
7
+ if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB';
8
+ return bytes + ' B';
9
+ }
10
+
5
11
  class Curl extends LocalCommand {
6
12
  static get name() { return 'curl'; }
7
13
  get requiresFilesystem() { return true; }
@@ -49,6 +55,9 @@ class Curl extends LocalCommand {
49
55
  case '-s': case '--silent':
50
56
  silent = true;
51
57
  break;
58
+ case '-#': case '--progress-bar':
59
+ // Accepted — progress bar is displayed when downloading to output file
60
+ break;
52
61
  case '-L': case '--location':
53
62
  // fetch follows redirects by default — accepted and ignored
54
63
  break;
@@ -91,16 +100,65 @@ class Curl extends LocalCommand {
91
100
  this.stdOut = '';
92
101
  }
93
102
 
94
- const text = await res.text();
103
+ let bytes = null;
104
+ const total = Number(res.headers.get('content-length') || 0);
105
+
106
+ if (res.body && typeof res.body.getReader === 'function') {
107
+ const reader = res.body.getReader();
108
+ const chunks = [];
109
+ let loaded = 0;
110
+ while (true) {
111
+ const { done, value } = await reader.read();
112
+ if (done) break;
113
+ chunks.push(value);
114
+ loaded += value.length;
115
+ if (outputFile && !silent) {
116
+ if (total > 0) {
117
+ const pct = Math.min(100, Math.floor((loaded / total) * 100));
118
+ const width = 20;
119
+ const filled = Math.min(width, Math.round((loaded / total) * width));
120
+ const bar = '='.repeat(Math.max(0, filled - 1)) + (filled > 0 ? '>' : '') + ' '.repeat(Math.max(0, width - filled));
121
+ this.stdOut = `\r[${bar}] ${pct}% (${formatBytes(loaded)} / ${formatBytes(total)})`;
122
+ } else {
123
+ this.stdOut = `\rDownloading: ${formatBytes(loaded)}`;
124
+ }
125
+ this.flush();
126
+ }
127
+ }
128
+ bytes = new Uint8Array(loaded);
129
+ let offset = 0;
130
+ for (const chunk of chunks) {
131
+ bytes.set(chunk, offset);
132
+ offset += chunk.length;
133
+ }
134
+ if (outputFile && !silent && loaded > 0) {
135
+ const finalTotal = total || loaded;
136
+ this.stdOut = `\r[${'='.repeat(20)}] 100% (${formatBytes(loaded)} / ${formatBytes(finalTotal)})`;
137
+ this.flush();
138
+ }
139
+ } else {
140
+ const buf = await res.arrayBuffer();
141
+ bytes = new Uint8Array(buf);
142
+ }
95
143
 
96
144
  if (outputFile) {
97
145
  try {
98
- this.fs.setFileByPath(outputFile, text, false);
146
+ const contentType = res.headers.get('content-type') || '';
147
+ const isText = /^text\/|application\/(json|javascript|xml|x-sh)/i.test(contentType);
148
+ if (isText) {
149
+ const text = new TextDecoder().decode(bytes);
150
+ this.fs.setFileByPath(outputFile, text, true);
151
+ } else {
152
+ const { BinaryFile } = require('../FileSystem');
153
+ const fileObj = BinaryFile ? new BinaryFile(bytes.buffer, contentType || 'application/octet-stream') : bytes;
154
+ this.fs.setFileByPath(outputFile, fileObj, true);
155
+ }
99
156
  } catch(e) {
100
157
  this.stdErr = `curl: cannot write to '${outputFile}': ${e.message}`;
101
158
  this.exitCode = 1;
102
159
  }
103
160
  } else {
161
+ const text = new TextDecoder().decode(bytes);
104
162
  text.split(/\r?\n/).forEach(line => { this.stdOut = line; });
105
163
  }
106
164