brsh 2.5.6 → 2.7.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.
@@ -137,6 +137,18 @@ describe('buildStructure', () => {
137
137
  const { permissions } = buildStructure(root);
138
138
  expect(permissions['/bin/tool.sh']).toBe(0o755);
139
139
  });
140
+
141
+ it('encodes .wasm files as data:application/wasm;base64 and strips .wasm extension', () => {
142
+ const sub = join(root, 'bin');
143
+ mkdirSync(sub);
144
+ const wasmBytes = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
145
+ writeFileSync(join(sub, 'jq.wasm'), wasmBytes);
146
+ chmodSync(join(sub, 'jq.wasm'), 0o755);
147
+ const { filesystem, permissions } = buildStructure(root);
148
+ expect(filesystem.bin).toHaveProperty('jq');
149
+ expect(filesystem.bin.jq).toBe(`data:application/wasm;base64,${wasmBytes.toString('base64')}`);
150
+ expect(permissions['/bin/jq']).toBe(0o755);
151
+ });
140
152
  });
141
153
 
142
154
  // ---------------------------------------------------------------------------
@@ -347,3 +359,69 @@ describe('exportFileSystem CLI --compress', () => {
347
359
  expect(code.length).toBeGreaterThan(100);
348
360
  });
349
361
  });
362
+
363
+ describe('exportFileSystem CLI --binary (.brfs indexed binary container)', () => {
364
+ const { isBinaryContainer, deserializeBinaryFilesystem } = require('../lib/BinaryContainer.js');
365
+ let root;
366
+ let outDir;
367
+
368
+ beforeEach(() => {
369
+ root = tmpDir();
370
+ outDir = tmpDir();
371
+ mkdirSync(join(root, 'bin'));
372
+ writeFileSync(join(root, 'hello.txt'), 'Hello Binary Container');
373
+ writeFileSync(join(root, 'bin', 'run.sh'), '#!/sh.js\necho hi');
374
+ chmodSync(join(root, 'bin', 'run.sh'), 0o755);
375
+ writeFileSync(join(root, 'bin', 'tool.wasm'), Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]));
376
+ chmodSync(join(root, 'bin', 'tool.wasm'), 0o755);
377
+ });
378
+
379
+ afterEach(() => {
380
+ rmSync(root, { recursive: true, force: true });
381
+ rmSync(outDir, { recursive: true, force: true });
382
+ });
383
+
384
+ it('writes a standalone BRFS v2 binary container when -o ends with .brfs', () => {
385
+ const outFile = join(outDir, 'fs.brfs');
386
+ const r = runCLI([root, '-o', outFile]);
387
+ expect(r.status).toBe(0);
388
+ expect(existsSync(outFile)).toBe(true);
389
+
390
+ const raw = new Uint8Array(readFileSync(outFile));
391
+ expect(isBinaryContainer(raw)).toBe(true);
392
+
393
+ const { filesystem, permissions } = deserializeBinaryFilesystem(raw);
394
+ expect(filesystem['hello.txt']).toBe('Hello Binary Container');
395
+ expect(filesystem.bin['run.sh']).toBe('#!/sh.js\necho hi');
396
+ expect(filesystem.bin.tool instanceof Uint8Array).toBe(true);
397
+ expect(permissions['/bin/run.sh']).toBe(0o755);
398
+ expect(permissions['/bin/tool']).toBe(0o755);
399
+ });
400
+
401
+ it('writes a JS loader and .brfs binary container when --binary (-b) is passed with -o fs.js', () => {
402
+ const outFile = join(outDir, 'fs.js');
403
+ const r = runCLI([root, '-v', 'FS', '-o', outFile, '--binary']);
404
+ expect(r.status).toBe(0);
405
+ expect(existsSync(outFile)).toBe(true);
406
+ expect(existsSync(join(outDir, 'fs.brfs'))).toBe(true);
407
+
408
+ const raw = new Uint8Array(readFileSync(join(outDir, 'fs.brfs')));
409
+ expect(isBinaryContainer(raw)).toBe(true);
410
+ });
411
+
412
+ it('boots a Shell directly from an exported .brfs file path and executes scripts inside it', async () => {
413
+ const Shell = require('../index.js');
414
+ const outFile = join(outDir, 'fs.brfs');
415
+ const r = runCLI([root, '-o', outFile]);
416
+ expect(r.status).toBe(0);
417
+
418
+ const shell = new Shell({ filesystem: outFile });
419
+ const output = [];
420
+ shell.on('stdOut', line => output.push(line));
421
+ await shell.onCommand('run.sh && cat /hello.txt');
422
+
423
+ expect(output).toContain('hi');
424
+ expect(output).toContain('Hello Binary Container');
425
+ });
426
+ });
427
+
@@ -335,3 +335,223 @@ describe('. (dot) command', () => {
335
335
  expect(shell.context.getVar('GREETED')).toBe('yes');
336
336
  });
337
337
  });
338
+
339
+ describe('MOTD, profile, and initial command startup chain', () => {
340
+ function bootShell(options) {
341
+ return new Promise((resolve) => {
342
+ const output = [];
343
+ const errors = [];
344
+ const shell = new Shell({ filesystem: {}, ...options });
345
+ shell.on('stdOut', (line) => output.push(line));
346
+ shell.on('stdErr', (line) => errors.push(line));
347
+ shell.once('status', (status) => {
348
+ resolve({ shell, output, errors, status });
349
+ });
350
+ });
351
+ }
352
+
353
+ it('prints static /etc/motd before sourcing profile', async () => {
354
+ const { output, shell } = await bootShell({
355
+ profile: '/home/.profile',
356
+ cwd: '/home',
357
+ filesystem: {
358
+ etc: {
359
+ motd: 'Welcome to the system!\nSecond MOTD line\n'
360
+ },
361
+ home: {
362
+ '.profile': '#!/bin/brsh\nexport PROFILE_LOADED=1\necho "profile ran"'
363
+ }
364
+ }
365
+ });
366
+
367
+ expect(output).toEqual([
368
+ 'Welcome to the system!',
369
+ 'Second MOTD line',
370
+ 'profile ran'
371
+ ]);
372
+ expect(shell.context.getVar('PROFILE_LOADED')).toBe('1');
373
+ });
374
+
375
+ it('runs executable scripts in /etc/update-motd.d in lexical order before /etc/motd and profile', async () => {
376
+ const { output } = await bootShell({
377
+ profile: '/home/.profile',
378
+ cwd: '/home',
379
+ filesystem: {
380
+ etc: {
381
+ 'update-motd.d': {
382
+ '20-second': '#!/bin/brsh\necho "motd-20"',
383
+ '10-first': '#!/bin/brsh\necho "motd-10"',
384
+ '30-disabled': '#!/bin/brsh\necho "motd-disabled"'
385
+ },
386
+ motd: 'static-motd'
387
+ },
388
+ home: {
389
+ '.profile': '#!/bin/brsh\necho "profile-last"'
390
+ }
391
+ },
392
+ permissions: {
393
+ '/etc/update-motd.d/10-first': 0o755,
394
+ '/etc/update-motd.d/20-second': 0o755,
395
+ '/etc/update-motd.d/30-disabled': 0o644
396
+ }
397
+ });
398
+
399
+ expect(output).toEqual([
400
+ 'motd-10',
401
+ 'motd-20',
402
+ 'static-motd',
403
+ 'profile-last'
404
+ ]);
405
+ });
406
+
407
+ it('supports a script file at /etc/motd or custom motd option', async () => {
408
+ const { output } = await bootShell({
409
+ motd: '/home/.motd',
410
+ cwd: '/home',
411
+ filesystem: {
412
+ home: {
413
+ '.motd': '#!/bin/brsh\necho "dynamic motd banner"'
414
+ }
415
+ }
416
+ });
417
+
418
+ expect(output).toEqual(['dynamic motd banner']);
419
+ });
420
+
421
+ it('suppresses MOTD when .hushlogin exists in cwd', async () => {
422
+ const { output, shell } = await bootShell({
423
+ profile: '/home/.profile',
424
+ cwd: '/home',
425
+ filesystem: {
426
+ etc: {
427
+ motd: 'Should not be shown'
428
+ },
429
+ home: {
430
+ '.hushlogin': '',
431
+ '.profile': '#!/bin/brsh\nexport HUSHED=yes'
432
+ }
433
+ }
434
+ });
435
+
436
+ expect(output).toEqual([]);
437
+ expect(shell.context.getVar('HUSHED')).toBe('yes');
438
+ });
439
+
440
+ it('suppresses MOTD when motd: false is set', async () => {
441
+ const { output } = await bootShell({
442
+ motd: false,
443
+ filesystem: {
444
+ etc: {
445
+ motd: 'Should not be shown'
446
+ }
447
+ }
448
+ });
449
+
450
+ expect(output).toEqual([]);
451
+ });
452
+
453
+ it('sources /etc/profile before user profile', async () => {
454
+ const { output } = await bootShell({
455
+ profile: '/home/.profile',
456
+ cwd: '/home',
457
+ filesystem: {
458
+ etc: {
459
+ profile: '#!/bin/brsh\nexport ORDER="etc"'
460
+ },
461
+ home: {
462
+ '.profile': '#!/bin/brsh\nexport ORDER="${ORDER}->user"\necho $ORDER'
463
+ }
464
+ }
465
+ });
466
+
467
+ expect(output).toEqual(['etc->user']);
468
+ });
469
+
470
+ it('skips MOTD when an initial command is provided, but still sources profile so aliases work', async () => {
471
+ const { output, shell } = await bootShell({
472
+ profile: '/home/.profile',
473
+ command: 'cv',
474
+ cwd: '/home',
475
+ filesystem: {
476
+ etc: {
477
+ 'update-motd.d': {
478
+ '00-info': '#!/bin/brsh\necho "MOTD INFO BANNER"'
479
+ },
480
+ motd: 'STATIC MOTD BANNER'
481
+ },
482
+ home: {
483
+ '.profile': '#!/bin/brsh\nalias cv="echo rendering-cv"'
484
+ }
485
+ },
486
+ permissions: {
487
+ '/etc/update-motd.d/00-info': 0o755
488
+ }
489
+ });
490
+
491
+ expect(output).toEqual(['rendering-cv']);
492
+ expect(output).not.toContain('MOTD INFO BANNER');
493
+ expect(output).not.toContain('STATIC MOTD BANNER');
494
+ expect(shell.context.getVar('-')).toBe('i');
495
+ });
496
+
497
+ it('skips MOTD when suppressMotd() is called synchronously before init (like Terminal.onCommand)', async () => {
498
+ const output = [];
499
+ const shell = new Shell({
500
+ profile: '/home/.profile',
501
+ cwd: '/home',
502
+ filesystem: {
503
+ etc: {
504
+ motd: 'STATIC MOTD BANNER'
505
+ },
506
+ home: {
507
+ '.profile': '#!/bin/brsh\nalias cv="echo rendering-cv"'
508
+ }
509
+ }
510
+ });
511
+ shell.suppressMotd();
512
+ shell.on('stdOut', (line) => output.push(line));
513
+
514
+ await new Promise((resolve) => shell.once('status', resolve));
515
+ await shell.onCommand('cv');
516
+
517
+ expect(output).toEqual(['rendering-cv']);
518
+ });
519
+
520
+ it('sets c in $- during profile execution when an initial command is run', async () => {
521
+ const { output } = await bootShell({
522
+ profile: '/home/.profile',
523
+ command: 'echo "ran command"',
524
+ cwd: '/home',
525
+ filesystem: {
526
+ home: {
527
+ '.profile': '#!/bin/brsh\ncase "$-" in\n *c*) echo "non-motd mode" ;;\n *) echo "motd mode" ;;\nesac'
528
+ }
529
+ }
530
+ });
531
+
532
+ expect(output).toEqual(['non-motd mode', 'ran command']);
533
+ });
534
+
535
+ it('does not re-run MOTD in subshells (pipes, command substitution, scripts)', async () => {
536
+ const { shell, output } = await bootShell({
537
+ cwd: '/home',
538
+ filesystem: {
539
+ etc: {
540
+ motd: 'WELCOME_MOTD'
541
+ },
542
+ home: {
543
+ 'script.sh': '#!/bin/brsh\necho $(echo sub) | cat'
544
+ }
545
+ },
546
+ permissions: {
547
+ '/home/script.sh': 0o755
548
+ }
549
+ });
550
+
551
+ expect(output).toEqual(['WELCOME_MOTD']);
552
+
553
+ const res = await run(shell, '/home/script.sh');
554
+ expect(res.output).toEqual(['sub']);
555
+ });
556
+ });
557
+