brsh 2.5.0 → 2.5.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.
@@ -0,0 +1,174 @@
1
+ import { createRequire } from 'module';
2
+ import { describe, it, expect } from 'vitest';
3
+ import { createShell, run } from './helpers.js';
4
+
5
+ const require = createRequire(import.meta.url);
6
+ const FileSystem = require('../lib/FileSystem.js');
7
+ const { BinaryFile, SymlinkRef } = require('../lib/FileSystem.js');
8
+
9
+ // Small 1x1 red PNG (base64)
10
+ const RED_PNG_B64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwADhQGAWjR9awAAAABJRU5ErkJggg==';
11
+ const RED_PNG_DATA_URI = `data:image/png;base64,${RED_PNG_B64}`;
12
+
13
+ // Helper: create BinaryFile from base64 without Buffer pool issues
14
+ function binaryFileFromB64(b64, mime) {
15
+ const bytes = new Uint8Array(Buffer.from(b64, 'base64'));
16
+ return new BinaryFile(bytes.buffer, mime);
17
+ }
18
+
19
+ describe('BinaryFile class', () => {
20
+ it('is a subclass of Uint8Array', () => {
21
+ const bf = binaryFileFromB64(RED_PNG_B64, 'image/png');
22
+ expect(bf instanceof Uint8Array).toBe(true);
23
+ expect(bf instanceof BinaryFile).toBe(true);
24
+ });
25
+
26
+ it('stores mimeType as non-enumerable property', () => {
27
+ const bf = binaryFileFromB64(RED_PNG_B64, 'image/png');
28
+ expect(bf.mimeType).toBe('image/png');
29
+ expect(Object.keys(bf)).not.toContain('mimeType');
30
+ });
31
+
32
+ it('toString() reconstructs the original data URI', () => {
33
+ const bf = binaryFileFromB64(RED_PNG_B64, 'image/png');
34
+ expect(bf.toString()).toBe(RED_PNG_DATA_URI);
35
+ });
36
+
37
+ it('has the correct byte length', () => {
38
+ const bf = binaryFileFromB64(RED_PNG_B64, 'image/png');
39
+ const expectedLen = Buffer.from(RED_PNG_B64, 'base64').length;
40
+ expect(bf.length).toBe(expectedLen);
41
+ });
42
+ });
43
+
44
+ describe('FileSystem binary conversion', () => {
45
+ it('converts data URI strings to BinaryFile on construction', () => {
46
+ const fs = new FileSystem('/', { img: RED_PNG_DATA_URI });
47
+ const file = fs.getFileByPath('/img');
48
+ expect(file instanceof BinaryFile).toBe(true);
49
+ });
50
+
51
+ it('does not modify non-data-URI strings', () => {
52
+ const fs = new FileSystem('/', { readme: 'hello world' });
53
+ const file = fs.getFileByPath('/readme');
54
+ expect(typeof file).toBe('string');
55
+ expect(file).toBe('hello world');
56
+ });
57
+
58
+ it('recurses into subdirectories', () => {
59
+ const fs = new FileSystem('/', { subdir: { img: RED_PNG_DATA_URI } });
60
+ const file = fs.getFileByPath('/subdir/img');
61
+ expect(file instanceof BinaryFile).toBe(true);
62
+ });
63
+
64
+ it('stat() reports isBinary:true for converted images', () => {
65
+ const fs = new FileSystem('/', { img: RED_PNG_DATA_URI });
66
+ const s = fs.stat('/img');
67
+ expect(s.isFile).toBe(true);
68
+ expect(s.isBinary).toBe(true);
69
+ expect(s.isDirectory).toBe(false);
70
+ expect(s.isCommand).toBe(false);
71
+ });
72
+
73
+ it('stat() reports isBinary:false for text files', () => {
74
+ const fs = new FileSystem('/', { readme: 'hello' });
75
+ const s = fs.stat('/readme');
76
+ expect(s.isFile).toBe(true);
77
+ expect(s.isBinary).toBe(false);
78
+ });
79
+
80
+ it('getSize() returns byte count for binary files', () => {
81
+ const fs = new FileSystem('/', { img: RED_PNG_DATA_URI });
82
+ const bytes = Buffer.from(RED_PNG_B64, 'base64');
83
+ expect(fs.getSize('/img')).toBe(bytes.length);
84
+ });
85
+
86
+ it('BinaryFile toString() is backward-compatible via cat', async () => {
87
+ const shell = await createShell({ filesystem: { img: RED_PNG_DATA_URI } });
88
+ const { output } = await run(shell, 'cat /img');
89
+ expect(output.join('')).toBe(RED_PNG_DATA_URI);
90
+ });
91
+ });
92
+
93
+ describe('FileSystem virtual symlinks', () => {
94
+ it('createSymlink stores a SymlinkRef', () => {
95
+ const fs = new FileSystem('/', { file: 'hello' });
96
+ fs.createSymlink('/file', '/link');
97
+ const raw = fs._getFileByAbsolutePath('/link');
98
+ expect(raw instanceof SymlinkRef).toBe(true);
99
+ expect(raw.target).toBe('/file');
100
+ });
101
+
102
+ it('isSymlink returns true for a symlink', () => {
103
+ const fs = new FileSystem('/', { file: 'hello' });
104
+ fs.createSymlink('/file', '/link');
105
+ expect(fs.isSymlink('/link')).toBe(true);
106
+ expect(fs.isSymlink('/file')).toBe(false);
107
+ });
108
+
109
+ it('readSymlink returns the target path', () => {
110
+ const fs = new FileSystem('/', { file: 'hello' });
111
+ fs.createSymlink('/file', '/link');
112
+ expect(fs.readSymlink('/link')).toBe('/file');
113
+ });
114
+
115
+ it('getFileByPath follows a symlink', () => {
116
+ const fs = new FileSystem('/', { file: 'hello' });
117
+ fs.createSymlink('/file', '/link');
118
+ expect(fs.getFileByPath('/link')).toBe('hello');
119
+ });
120
+
121
+ it('exists follows a symlink to a valid target', () => {
122
+ const fs = new FileSystem('/', { file: 'hello' });
123
+ fs.createSymlink('/file', '/link');
124
+ expect(fs.exists('/link')).toBe(true);
125
+ });
126
+
127
+ it('exists returns false for a broken symlink', () => {
128
+ const fs = new FileSystem('/', {});
129
+ fs.createSymlink('/missing', '/link');
130
+ expect(fs.exists('/link')).toBe(false);
131
+ });
132
+
133
+ it('stat returns isSymlink:true without following', () => {
134
+ const fs = new FileSystem('/', { file: 'hello' });
135
+ fs.createSymlink('/file', '/link');
136
+ const s = fs.stat('/link');
137
+ expect(s.isSymlink).toBe(true);
138
+ expect(s.isFile).toBe(false);
139
+ });
140
+
141
+ it('stat follows symlinks for a regular file', () => {
142
+ const fs = new FileSystem('/', { file: 'hello' });
143
+ const s = fs.stat('/file');
144
+ expect(s.isFile).toBe(true);
145
+ expect(s.isSymlink).toBe(false);
146
+ });
147
+
148
+ it('follows a chain of symlinks', () => {
149
+ const fs = new FileSystem('/', { file: 'hi' });
150
+ fs.createSymlink('/file', '/a');
151
+ fs.createSymlink('/a', '/b');
152
+ expect(fs.getFileByPath('/b')).toBe('hi');
153
+ });
154
+
155
+ it('returns null for a symlink loop', () => {
156
+ const fs = new FileSystem('/', {});
157
+ fs.createSymlink('/b', '/a');
158
+ fs.createSymlink('/a', '/b');
159
+ expect(fs.getFileByPath('/a')).toBeNull();
160
+ });
161
+
162
+ it('symlink in a subdirectory resolves relative target', () => {
163
+ const fs = new FileSystem('/', { dir: { file: 'content' } });
164
+ fs.createSymlink('file', '/dir/link'); // relative target
165
+ expect(fs.getFileByPath('/dir/link')).toBe('content');
166
+ });
167
+
168
+ it('cat reads through a symlink via the shell', async () => {
169
+ const shell = await createShell({ filesystem: { real: 'symlinked content' } });
170
+ await run(shell, 'ln -s /real /lnk');
171
+ const { output } = await run(shell, 'cat /lnk');
172
+ expect(output.join('')).toBe('symlinked content');
173
+ });
174
+ });
@@ -1,11 +1,11 @@
1
1
  import { createRequire } from 'module';
2
2
  import { describe, it, expect, beforeEach, afterEach } from 'vitest';
3
3
  import { spawnSync } from 'child_process';
4
- import { mkdtempSync, writeFileSync, mkdirSync, chmodSync, readFileSync, rmSync, statSync } from 'fs';
4
+ import { mkdtempSync, writeFileSync, mkdirSync, chmodSync, readFileSync, rmSync, statSync, existsSync } from 'fs';
5
5
  import { tmpdir } from 'os';
6
6
  import { join, resolve } from 'path';
7
7
  import { fileURLToPath } from 'url';
8
- import { createContext, runInContext } from 'vm';
8
+ import { gunzipSync } from 'zlib';
9
9
 
10
10
  const require = createRequire(import.meta.url);
11
11
  const { buildStructure } = require('../lib/buildFilesystemStructure.js');
@@ -273,28 +273,37 @@ describe('exportFileSystem CLI --compress', () => {
273
273
  expect(r.stdout).toContain('Build succeeded');
274
274
  });
275
275
 
276
- it('compressed output is valid JavaScript', () => {
276
+ it('writes a .brfs binary file alongside the .js loader', () => {
277
+ const outFile = join(outDir, 'fs.js');
278
+ runCLI([root, '-v', 'FS', '-o', outFile, '--compress']);
279
+ expect(existsSync(outFile)).toBe(true);
280
+ expect(existsSync(join(outDir, 'fs.brfs'))).toBe(true);
281
+ });
282
+
283
+ it('the .js loader is valid JavaScript', () => {
277
284
  const outFile = join(outDir, 'fs.js');
278
285
  runCLI([root, '-v', 'FS', '-o', outFile, '--compress']);
279
286
  const content = readFileSync(outFile, 'utf8');
280
287
  expect(() => new Function(content)).not.toThrow();
281
288
  });
282
289
 
283
- it('compressed output contains DecompressionStream reference', () => {
290
+ it('the .js loader references DecompressionStream and fetch', () => {
284
291
  const outFile = join(outDir, 'fs.js');
285
292
  runCLI([root, '-v', 'FS', '-o', outFile, '--compress']);
286
293
  const content = readFileSync(outFile, 'utf8');
287
294
  expect(content).toContain('DecompressionStream');
295
+ expect(content).toContain('fetch');
296
+ expect(content).toContain('.brfs');
288
297
  });
289
298
 
290
- it('compressed output does not contain plain text file content', () => {
299
+ it('the .js loader does not contain any filesystem content', () => {
291
300
  const outFile = join(outDir, 'fs.js');
292
301
  runCLI([root, '-v', 'FS', '-o', outFile, '--compress']);
293
302
  const content = readFileSync(outFile, 'utf8');
294
303
  expect(content).not.toContain('Hello World');
295
304
  });
296
305
 
297
- it('compressed output is smaller than uncompressed for text-heavy filesystem', () => {
306
+ it('the .brfs file is smaller than the uncompressed .js bundle for text-heavy filesystem', () => {
298
307
  const textRoot = tmpDir();
299
308
  try {
300
309
  for (let i = 0; i < 20; i++) {
@@ -304,37 +313,37 @@ describe('exportFileSystem CLI --compress', () => {
304
313
  const uncompressedOut = join(outDir, 'uncompressed.js');
305
314
  runCLI([textRoot, '-v', 'FS', '-o', compressedOut, '--compress']);
306
315
  runCLI([textRoot, '-v', 'FS', '-o', uncompressedOut]);
307
- expect(statSync(compressedOut).size).toBeLessThan(statSync(uncompressedOut).size);
316
+ expect(statSync(join(outDir, 'compressed.brfs')).size).toBeLessThan(statSync(uncompressedOut).size);
308
317
  } finally {
309
318
  rmSync(textRoot, { recursive: true, force: true });
310
319
  }
311
320
  });
312
321
 
313
- it('compressed output resolves to an object with filesystem and permissions', async () => {
322
+ it('the .brfs file is valid gzip that decompresses to executable JS', () => {
314
323
  const outFile = join(outDir, 'fs.js');
315
324
  runCLI([root, '-v', 'TestFS', '-o', outFile, '--compress']);
316
- const content = readFileSync(outFile, 'utf8');
317
- const ctx = createContext({
318
- atob: globalThis.atob,
319
- DecompressionStream: globalThis.DecompressionStream,
320
- Response: globalThis.Response,
321
- TextDecoder: globalThis.TextDecoder,
322
- Uint8Array,
323
- Set,
324
- Object,
325
- });
326
- runInContext(content, ctx);
327
- const result = await ctx.TestFS;
325
+ const brfsData = readFileSync(join(outDir, 'fs.brfs'));
326
+ const code = gunzipSync(brfsData).toString('utf8');
327
+ expect(() => new Function(code)).not.toThrow();
328
+ });
329
+
330
+ it('the .brfs bundle resolves to an object with filesystem and permissions', () => {
331
+ const outFile = join(outDir, 'fs.js');
332
+ runCLI([root, '-v', 'TestFS', '-o', outFile, '--compress']);
333
+ const brfsData = readFileSync(join(outDir, 'fs.brfs'));
334
+ const code = gunzipSync(brfsData).toString('utf8');
335
+ const result = new Function(code)();
328
336
  expect(result).toHaveProperty('filesystem');
329
337
  expect(result).toHaveProperty('permissions');
330
338
  expect(result.filesystem['hello.txt']).toBe('Hello World');
331
339
  });
332
340
 
333
- it('compressed output with JS command files still bundles the command code', () => {
341
+ it('the .brfs bundle contains JS command code', () => {
334
342
  const outFile = join(outDir, 'fs.js');
335
343
  runCLI([root, '-v', 'FS', '-o', outFile, '--compress']);
336
- const content = readFileSync(outFile, 'utf8');
337
- // app.js exports a function – its compiled form should appear in the bundle
338
- expect(content.length).toBeGreaterThan(0);
344
+ const brfsData = readFileSync(join(outDir, 'fs.brfs'));
345
+ const code = gunzipSync(brfsData).toString('utf8');
346
+ // app.js exports a function – its compiled form must be in the bundle
347
+ expect(code.length).toBeGreaterThan(100);
339
348
  });
340
349
  });
@@ -149,3 +149,172 @@ module.exports = Hello;
149
149
  expect(output).toContain('hello from js bin');
150
150
  });
151
151
  });
152
+
153
+ // ---------------------------------------------------------------------------
154
+ // touch
155
+ // ---------------------------------------------------------------------------
156
+ describe('touch', () => {
157
+ let tmpDir;
158
+ let shell;
159
+
160
+ beforeEach(async () => {
161
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-touch-'));
162
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
163
+ });
164
+ afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });
165
+
166
+ it('creates a new empty file', async () => {
167
+ const { exitCode } = await run(shell, 'touch newfile.txt');
168
+ expect(exitCode).toBe(0);
169
+ expect(fs.existsSync(path.join(tmpDir, 'newfile.txt'))).toBe(true);
170
+ expect(fs.readFileSync(path.join(tmpDir, 'newfile.txt'), 'utf8')).toBe('');
171
+ });
172
+
173
+ it('does not overwrite existing file content', async () => {
174
+ fs.writeFileSync(path.join(tmpDir, 'existing.txt'), 'keep me');
175
+ await run(shell, 'touch existing.txt');
176
+ expect(fs.readFileSync(path.join(tmpDir, 'existing.txt'), 'utf8')).toBe('keep me');
177
+ });
178
+
179
+ it('updates mtime of existing file', async () => {
180
+ const filePath = path.join(tmpDir, 'old.txt');
181
+ fs.writeFileSync(filePath, 'data');
182
+ const epoch = new Date('2000-01-01T00:00:00Z');
183
+ fs.utimesSync(filePath, epoch, epoch);
184
+ const before = fs.statSync(filePath).mtime;
185
+
186
+ await run(shell, 'touch old.txt');
187
+
188
+ const after = fs.statSync(filePath).mtime;
189
+ expect(after.getTime()).toBeGreaterThan(before.getTime());
190
+ });
191
+
192
+ it('-c does not create a missing file', async () => {
193
+ const { exitCode } = await run(shell, 'touch -c ghost.txt');
194
+ expect(exitCode).toBe(0);
195
+ expect(fs.existsSync(path.join(tmpDir, 'ghost.txt'))).toBe(false);
196
+ });
197
+
198
+ it('creates multiple files', async () => {
199
+ await run(shell, 'touch a.txt b.txt c.txt');
200
+ expect(fs.existsSync(path.join(tmpDir, 'a.txt'))).toBe(true);
201
+ expect(fs.existsSync(path.join(tmpDir, 'b.txt'))).toBe(true);
202
+ expect(fs.existsSync(path.join(tmpDir, 'c.txt'))).toBe(true);
203
+ });
204
+
205
+ it('works on virtual filesystem', async () => {
206
+ const vshell = await createShell({ filesystem: { home: {} } });
207
+ const { exitCode } = await run(vshell, 'touch /home/newfile.txt');
208
+ expect(exitCode).toBe(0);
209
+ expect(vshell.context.fs.exists('/home/newfile.txt')).toBe(true);
210
+ });
211
+ });
212
+
213
+ // ---------------------------------------------------------------------------
214
+ // ln
215
+ // ---------------------------------------------------------------------------
216
+ describe('ln', () => {
217
+ let tmpDir;
218
+ let shell;
219
+
220
+ beforeEach(async () => {
221
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-ln-'));
222
+ fs.writeFileSync(path.join(tmpDir, 'file.txt'), 'hello');
223
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
224
+ });
225
+ afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });
226
+
227
+ it('creates a symlink with ln -s', async () => {
228
+ const { exitCode } = await run(shell, 'ln -s file.txt link.txt');
229
+ expect(exitCode).toBe(0);
230
+ expect(fs.lstatSync(path.join(tmpDir, 'link.txt')).isSymbolicLink()).toBe(true);
231
+ expect(fs.readlinkSync(path.join(tmpDir, 'link.txt'))).toBe('file.txt');
232
+ });
233
+
234
+ it('symlink target is readable', async () => {
235
+ await run(shell, 'ln -s file.txt link.txt');
236
+ const { output } = await run(shell, 'cat link.txt');
237
+ expect(output.join('')).toBe('hello');
238
+ });
239
+
240
+ it('creates a hard link', async () => {
241
+ const { exitCode } = await run(shell, 'ln file.txt hardlink.txt');
242
+ expect(exitCode).toBe(0);
243
+ const orig = fs.statSync(path.join(tmpDir, 'file.txt'));
244
+ const link = fs.statSync(path.join(tmpDir, 'hardlink.txt'));
245
+ expect(link.ino).toBe(orig.ino);
246
+ });
247
+
248
+ it('-f overwrites existing symlink', async () => {
249
+ fs.writeFileSync(path.join(tmpDir, 'other.txt'), 'other');
250
+ await run(shell, 'ln -s file.txt link.txt');
251
+ const { exitCode } = await run(shell, 'ln -sf other.txt link.txt');
252
+ expect(exitCode).toBe(0);
253
+ expect(fs.readlinkSync(path.join(tmpDir, 'link.txt'))).toBe('other.txt');
254
+ });
255
+
256
+ it('errors when source missing for hard link', async () => {
257
+ const { exitCode, errors } = await run(shell, 'ln ghost.txt link.txt');
258
+ expect(exitCode).toBe(1);
259
+ expect(errors.join('')).toMatch(/ghost\.txt/);
260
+ });
261
+
262
+ it('errors with missing operands', async () => {
263
+ const { exitCode, errors } = await run(shell, 'ln');
264
+ expect(exitCode).toBe(1);
265
+ expect(errors.join('')).toMatch(/missing/);
266
+ });
267
+ });
268
+
269
+ // ---------------------------------------------------------------------------
270
+ // stat
271
+ // ---------------------------------------------------------------------------
272
+ describe('stat', () => {
273
+ let tmpDir;
274
+ let shell;
275
+
276
+ beforeEach(async () => {
277
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-stat-'));
278
+ fs.writeFileSync(path.join(tmpDir, 'file.txt'), 'hello world');
279
+ fs.mkdirSync(path.join(tmpDir, 'subdir'));
280
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
281
+ });
282
+ afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); });
283
+
284
+ it('outputs file info for a regular file', async () => {
285
+ const { output, exitCode } = await run(shell, 'stat file.txt');
286
+ expect(exitCode).toBe(0);
287
+ const out = output.join('\n');
288
+ expect(out).toMatch(/file\.txt/);
289
+ expect(out).toMatch(/regular file/);
290
+ expect(out).toMatch(/\d+/); // size
291
+ });
292
+
293
+ it('outputs directory info', async () => {
294
+ const { output, exitCode } = await run(shell, 'stat subdir');
295
+ expect(exitCode).toBe(0);
296
+ expect(output.join('\n')).toMatch(/directory/);
297
+ });
298
+
299
+ it('reports symlink type without following', async () => {
300
+ fs.symlinkSync('file.txt', path.join(tmpDir, 'link.txt'));
301
+ const { output, exitCode } = await run(shell, 'stat link.txt');
302
+ expect(exitCode).toBe(0);
303
+ const out = output.join('\n');
304
+ expect(out).toMatch(/symbolic link/);
305
+ expect(out).toMatch(/-> file\.txt/);
306
+ });
307
+
308
+ it('errors for missing file', async () => {
309
+ const { exitCode, errors } = await run(shell, 'stat ghost.txt');
310
+ expect(exitCode).toBe(1);
311
+ expect(errors.join('')).toMatch(/No such file/);
312
+ });
313
+
314
+ it('works on virtual filesystem', async () => {
315
+ const vshell = await createShell({ filesystem: { 'readme.txt': 'hello' } });
316
+ const { output, exitCode } = await run(vshell, 'stat /readme.txt');
317
+ expect(exitCode).toBe(0);
318
+ expect(output.join('\n')).toMatch(/readme\.txt/);
319
+ });
320
+ });