brsh 2.5.0 → 2.5.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.
- package/CHANGELOG.md +7 -0
- package/bin/brsh-shell.js +0 -0
- package/bin/exportFileSystem.js +42 -47
- package/dist/shell.es.js +2075 -2038
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +9 -9
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +2074 -2037
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +9 -9
- package/dist/terminal.min.js.map +1 -1
- package/lib/FileSystem.js +63 -2
- package/lib/NodeFileSystem.js +1 -1
- package/package.json +1 -1
- package/test/binary-filesystem.test.js +91 -0
- package/test/exportFileSystem.test.js +33 -24
package/lib/FileSystem.js
CHANGED
|
@@ -2,9 +2,67 @@
|
|
|
2
2
|
|
|
3
3
|
const BaseFileSystem = require('./BaseFileSystem');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents a binary file stored as raw bytes in the virtual filesystem.
|
|
7
|
+
* Extends Uint8Array so binary-aware commands can access bytes directly,
|
|
8
|
+
* while text-based commands (cat, grep, etc.) get the original data URI
|
|
9
|
+
* via toString() — avoiding the 33% base64 memory overhead at runtime.
|
|
10
|
+
*/
|
|
11
|
+
class BinaryFile extends Uint8Array {
|
|
12
|
+
constructor(buffer, mimeType) {
|
|
13
|
+
super(buffer);
|
|
14
|
+
Object.defineProperty(this, 'mimeType', { value: mimeType, enumerable: false, configurable: false, writable: false });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toString() {
|
|
18
|
+
if (typeof Buffer !== 'undefined') {
|
|
19
|
+
return `data:${this.mimeType};base64,${Buffer.from(this).toString('base64')}`;
|
|
20
|
+
}
|
|
21
|
+
// Browser path — chunked to avoid stack overflow on large files
|
|
22
|
+
let binary = '';
|
|
23
|
+
const chunk = 8192;
|
|
24
|
+
for (let i = 0; i < this.length; i += chunk) {
|
|
25
|
+
binary += String.fromCharCode(...this.subarray(i, i + chunk));
|
|
26
|
+
}
|
|
27
|
+
return `data:${this.mimeType};base64,${btoa(binary)}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Recursively converts `data:<mime>;base64,<...>` strings in a filesystem
|
|
33
|
+
* node to BinaryFile instances (Uint8Array subclass), saving the ~33%
|
|
34
|
+
* memory overhead of storing base64-encoded binary data as UTF-16 strings.
|
|
35
|
+
* Mutates the node in-place.
|
|
36
|
+
*/
|
|
37
|
+
function decodeBinaryFiles(node) {
|
|
38
|
+
for (const key of Object.keys(node)) {
|
|
39
|
+
const value = node[key];
|
|
40
|
+
if (value !== null && typeof value === 'object' && !(value instanceof Uint8Array)) {
|
|
41
|
+
decodeBinaryFiles(value);
|
|
42
|
+
} else if (typeof value === 'string' && value.startsWith('data:')) {
|
|
43
|
+
const semicolon = value.indexOf(';');
|
|
44
|
+
const comma = value.indexOf(',', semicolon);
|
|
45
|
+
if (semicolon !== -1 && comma !== -1 && value.slice(semicolon + 1, comma) === 'base64') {
|
|
46
|
+
const mimeType = value.slice(5, semicolon);
|
|
47
|
+
const b64 = value.slice(comma + 1);
|
|
48
|
+
let bytes;
|
|
49
|
+
if (typeof Buffer !== 'undefined') {
|
|
50
|
+
bytes = new Uint8Array(Buffer.from(b64, 'base64'));
|
|
51
|
+
} else {
|
|
52
|
+
const binary = atob(b64);
|
|
53
|
+
bytes = new Uint8Array(binary.length);
|
|
54
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
55
|
+
}
|
|
56
|
+
node[key] = new BinaryFile(bytes.buffer, mimeType);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
5
62
|
class FileSystem extends BaseFileSystem {
|
|
6
63
|
constructor(cwd = '/', filesystem = {}, permissions = {}, onChange = null) {
|
|
7
64
|
super();
|
|
65
|
+
decodeBinaryFiles(filesystem);
|
|
8
66
|
this.fileSystem = filesystem;
|
|
9
67
|
this.permissions = Object.assign({}, permissions);
|
|
10
68
|
this.cwd = cwd;
|
|
@@ -39,8 +97,9 @@ class FileSystem extends BaseFileSystem {
|
|
|
39
97
|
if (file === null || file === undefined) return null;
|
|
40
98
|
return {
|
|
41
99
|
isDirectory: file.constructor === Object,
|
|
42
|
-
isFile: typeof file === 'string',
|
|
43
|
-
isCommand: typeof file === 'function'
|
|
100
|
+
isFile: typeof file === 'string' || file instanceof Uint8Array,
|
|
101
|
+
isCommand: typeof file === 'function',
|
|
102
|
+
isBinary: file instanceof Uint8Array,
|
|
44
103
|
};
|
|
45
104
|
}
|
|
46
105
|
|
|
@@ -191,6 +250,7 @@ class FileSystem extends BaseFileSystem {
|
|
|
191
250
|
const file = this._getFileByAbsolutePath(absolute);
|
|
192
251
|
if (file === null || file === undefined) return 0;
|
|
193
252
|
if (file.constructor === Object) return JSON.stringify(file).length;
|
|
253
|
+
if (file instanceof Uint8Array) return file.length;
|
|
194
254
|
return file.toString().length;
|
|
195
255
|
}
|
|
196
256
|
|
|
@@ -250,3 +310,4 @@ class FileSystem extends BaseFileSystem {
|
|
|
250
310
|
|
|
251
311
|
|
|
252
312
|
module.exports = FileSystem;
|
|
313
|
+
module.exports.BinaryFile = BinaryFile;
|
package/lib/NodeFileSystem.js
CHANGED
|
@@ -238,7 +238,7 @@ class NodeFileSystem extends BaseFileSystem {
|
|
|
238
238
|
const abs = this.absolutePath(path);
|
|
239
239
|
if (this._virtualFiles[abs] !== undefined) {
|
|
240
240
|
const v = this._virtualFiles[abs];
|
|
241
|
-
return { isDirectory: v !== null && v !== undefined && v.constructor === Object, isFile: typeof v === 'string', isCommand: typeof v === 'function' };
|
|
241
|
+
return { isDirectory: v !== null && v !== undefined && v.constructor === Object, isFile: typeof v === 'string' || v instanceof Uint8Array, isCommand: typeof v === 'function', isBinary: v instanceof Uint8Array };
|
|
242
242
|
}
|
|
243
243
|
try {
|
|
244
244
|
const s = nodeFs.statSync(abs);
|
package/package.json
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
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 } = 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
|
+
});
|
|
@@ -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 {
|
|
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('
|
|
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('
|
|
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('
|
|
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('
|
|
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(
|
|
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('
|
|
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
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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('
|
|
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
|
|
337
|
-
|
|
338
|
-
|
|
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
|
});
|