brsh 3.1.0 → 3.1.2

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.
package/index.js CHANGED
@@ -1286,6 +1286,26 @@ class Shell extends EventEmitter {
1286
1286
  continue;
1287
1287
  }
1288
1288
 
1289
+ // Escape handling inside double quotes
1290
+ if (doubleQuoteOpen && ch === '\\' && i + 1 < string.length) {
1291
+ const next = string[i + 1];
1292
+ if (next === '"' || next === '\\' || next === '$') {
1293
+ tokenBuffer.push(next);
1294
+ i++;
1295
+ continue;
1296
+ }
1297
+ }
1298
+
1299
+ // Escape handling outside quotes
1300
+ if (!singleQuoteOpen && !doubleQuoteOpen && ch === '\\' && i + 1 < string.length) {
1301
+ const next = string[++i];
1302
+ tokenBuffer.push(next);
1303
+ if (next === '"' || next === "'") {
1304
+ tokenHasQuote = true;
1305
+ }
1306
+ continue;
1307
+ }
1308
+
1289
1309
  // Quote handling
1290
1310
  if (ch === "'" && !doubleQuoteOpen) {
1291
1311
  singleQuoteOpen = !singleQuoteOpen;
@@ -33,11 +33,12 @@ class ScriptCommand extends LocalCommand {
33
33
  }
34
34
 
35
35
  run() {
36
- const fsOpts = this.context.fs.isRealFilesystem
36
+ const fs = (this.context && this.context.fs) || this.fs;
37
+ const fsOpts = fs && fs.isRealFilesystem
37
38
  ? { useRealFilesystem: true }
38
- : { filesystem: this.context.fs.getRaw(), permissions: this.context.fs.getRawPermissions() };
39
+ : { filesystem: (fs && fs.getRaw()) || {}, permissions: (fs && fs.getRawPermissions()) || {} };
39
40
  this.sandbox = new globalThis.shell({
40
- cwd: this.context.fs.cwd,
41
+ cwd: (fs && fs.cwd) || '/',
41
42
  ...fsOpts,
42
43
  subshell: true
43
44
  });
@@ -47,16 +47,18 @@ class WasmCommand extends ScriptCommand {
47
47
  }
48
48
 
49
49
  _loadBytes(filepath, nodeFs) {
50
- if (this.context.fs.isRealFilesystem && nodeFs) {
50
+ const fs = (this.context && this.context.fs) || this.fs;
51
+ if (!fs) return Promise.reject(new Error(`${filepath}: Filesystem is not available`));
52
+ if (fs.isRealFilesystem && nodeFs) {
51
53
  try {
52
- const buf = nodeFs.readFileSync(this.context.fs.absolutePath(filepath));
54
+ const buf = nodeFs.readFileSync(fs.absolutePath(filepath));
53
55
  const bytes = new Uint8Array(buf);
54
56
  return Promise.resolve({ bytes, isWat: this._isWat(filepath, bytes) });
55
57
  } catch(e) {
56
58
  return Promise.reject(new Error(`${filepath}: ${e.message}`));
57
59
  }
58
60
  }
59
- const content = this.context.fs.getFileByPath(filepath);
61
+ const content = fs.getFileByPath(filepath);
60
62
  if (content == null) return Promise.reject(new Error(`${filepath}: No such file or directory`));
61
63
  const bytes = this._contentToBytes(content);
62
64
  return Promise.resolve({ bytes, isWat: this._isWat(filepath, bytes) });
package/lib/wbt/index.js CHANGED
@@ -17,12 +17,16 @@ function createCustomWasmBinCommand(binName, wasmPath, pkgMeta = {}) {
17
17
  class CustomWasmBin extends WasmCommand {
18
18
  static get name() { return binName; }
19
19
  get name() { return binName; }
20
+ get requiresFilesystem() { return true; }
20
21
 
21
22
  constructor(args, context) {
22
23
  super(wasmPath, args, context);
23
24
  this.script = wasmPath;
24
25
  this.arguments = args || [];
25
26
  this.context = context || null;
27
+ if (context && context.fs && !this.fs) {
28
+ this.fs = context.fs;
29
+ }
26
30
  this._pkgMeta = pkgMeta;
27
31
  }
28
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -215,6 +215,55 @@ describe('unclosed quote error', () => {
215
215
  });
216
216
  });
217
217
 
218
+ describe('backslash quote escaping', () => {
219
+ let shell;
220
+ beforeEach(async () => { shell = await createShell(); });
221
+
222
+ it('parses echo "hello \\"world\\"" to [\'echo\', \'hello "world"\']', async () => {
223
+ const cmd = 'echo "hello \\"world\\""';
224
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
225
+ expect(parts).toEqual(['echo', 'hello "world"']);
226
+
227
+ const { output, errors, exitCode } = await run(shell, cmd);
228
+ expect(errors).toHaveLength(0);
229
+ expect(exitCode).toBe(0);
230
+ expect(output).toEqual(['hello "world"']);
231
+ });
232
+
233
+ it('parses echo "Eliot\'s CV with \\"nested quotes\\"" cleanly without throwing quote expression not closed', async () => {
234
+ const cmd = 'echo "Eliot\'s CV with \\"nested quotes\\""';
235
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
236
+ expect(parts).toEqual(['echo', 'Eliot\'s CV with "nested quotes"']);
237
+
238
+ const { output, errors, exitCode } = await run(shell, cmd);
239
+ expect(errors).toHaveLength(0);
240
+ expect(exitCode).toBe(0);
241
+ expect(output).toEqual(['Eliot\'s CV with "nested quotes"']);
242
+ });
243
+
244
+ it('parses echo \'Eliot\'\\\'\'s experience\' correctly into [\'echo\', "Eliot\'s experience"]', async () => {
245
+ const cmd = "echo 'Eliot'\\''s experience'";
246
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
247
+ expect(parts).toEqual(['echo', "Eliot's experience"]);
248
+
249
+ const { output, errors, exitCode } = await run(shell, cmd);
250
+ expect(errors).toHaveLength(0);
251
+ expect(exitCode).toBe(0);
252
+ expect(output).toEqual(["Eliot's experience"]);
253
+ });
254
+
255
+ it('parses echo \\"unquoted\\" to [\'echo\', \'"unquoted"\']', async () => {
256
+ const cmd = 'echo \\"unquoted\\"';
257
+ const parts = Shell._splitCommand(cmd).map(p => p.value);
258
+ expect(parts).toEqual(['echo', '"unquoted"']);
259
+
260
+ const { output, errors, exitCode } = await run(shell, cmd);
261
+ expect(errors).toHaveLength(0);
262
+ expect(exitCode).toBe(0);
263
+ expect(output).toEqual(['"unquoted"']);
264
+ });
265
+ });
266
+
218
267
  describe('output redirection', () => {
219
268
  it('writes command stdout to a file with >', async () => {
220
269
  const shell = await createShell({ filesystem: { home: {} } });
package/test/wbt.test.js CHANGED
@@ -1279,6 +1279,23 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
1279
1279
  expect(shell.context.fs.exists('/bin/nano-wasm')).toBe(false);
1280
1280
  expect(shell.context.fs.exists('/var/lib/wbt/wasm/nano.wasm')).toBe(false);
1281
1281
  });
1282
+
1283
+ it('runs nano even when invoked with readonlyContext without context.fs', async () => {
1284
+ await run(shell, 'wbt install nano');
1285
+ const NanoCmd = shell.context.fs.getFileByPath('/bin/nano');
1286
+ expect(NanoCmd).toBeDefined();
1287
+
1288
+ const readonlyCtx = Object.freeze({
1289
+ getVar: (k) => shell.context.getVar(k),
1290
+ getAlias: (k) => shell.context.getAlias(k),
1291
+ getCommand: (k) => shell.context.getCommand(k)
1292
+ });
1293
+ const instance = new NanoCmd(['--version'], readonlyCtx);
1294
+ instance.fs = shell.context.fs.sandbox();
1295
+ const res = await instance.runCommand();
1296
+ expect(res.getExitCode()).toBe(0);
1297
+ expect(res.getStdOutput().map(l => l.string).join('\n')).toMatch(/GNU nano, version 8\.0|nano.*8\.0\.0.*wasm/i);
1298
+ });
1282
1299
  });
1283
1300
 
1284
1301