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 CHANGED
@@ -1,3 +1,10 @@
1
+ ## [2.5.1](https://github.com/eliotstocker/Brsh-JS/compare/v2.5.0...v2.5.1) (2026-05-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * store binary data at runtime as uint8array rather than base64 string ([f2edfec](https://github.com/eliotstocker/Brsh-JS/commit/f2edfec18cb890846e152f71f1e033ee6f9c80f1))
7
+
1
8
  # [2.5.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.4.1...v2.5.0) (2026-05-25)
2
9
 
3
10
 
package/bin/brsh-shell.js CHANGED
File without changes
@@ -77,56 +77,16 @@ try {
77
77
  }
78
78
  const { filesystem, permissions } = struct;
79
79
 
80
- /**
81
- * Recursively splits a filesystem tree into two trees:
82
- * code – entries whose value is a require() placeholder (bundled by esbuild)
83
- * data – entries whose value is a plain string/null (text / binary content)
84
- */
85
- function splitFilesystem(node) {
86
- const code = {};
87
- const data = {};
88
- for (const [key, value] of Object.entries(node)) {
89
- if (value !== null && typeof value === 'object') {
90
- const [subCode, subData] = splitFilesystem(value);
91
- if (Object.keys(subCode).length > 0) code[key] = subCode;
92
- if (Object.keys(subData).length > 0) data[key] = subData;
93
- } else if (typeof value === 'string' && value.startsWith("require('")) {
94
- code[key] = value;
95
- } else {
96
- data[key] = value;
97
- }
98
- }
99
- return [code, data];
80
+ if (options.compress && !options.output) {
81
+ console.log('--compress requires --output (-o) to be set');
82
+ process.exit(1);
100
83
  }
101
84
 
102
- let JS;
103
-
104
- if (options.compress) {
105
- const [codeFilesystem, dataFilesystem] = splitFilesystem(filesystem);
85
+ const fsJson = JSON.stringify(filesystem, null, options.pretty ? 2 : null);
86
+ const permJson = JSON.stringify(permissions, null, options.pretty ? 2 : null);
87
+ const fsJS = fsJson.replace(/"(require\('.*?'\))"/gm, '$1');
106
88
 
107
- // Compress the data portion (text/binary file contents + permissions)
108
- const dataPayload = JSON.stringify({ filesystem: dataFilesystem, permissions });
109
- const compressed = zlib.gzipSync(Buffer.from(dataPayload, 'utf8'));
110
- const b64 = compressed.toString('base64');
111
-
112
- // Serialise only the code portion (require() entries) with substitution
113
- const codeFsJSON = JSON.stringify(codeFilesystem);
114
- const codeFsJS = codeFsJSON.replace(/"(require\('.*?'\))"/gm, '$1');
115
-
116
- JS = `
117
- const __c="${b64}";
118
- function __bd(s){const b=atob(s);const a=new Uint8Array(b.length);for(let i=0;i<b.length;i++)a[i]=b.charCodeAt(i);return a;}
119
- async function __decompress(b64){const ds=new DecompressionStream("gzip");const w=ds.writable.getWriter();w.write(__bd(b64));w.close();const buf=await new Response(ds.readable).arrayBuffer();return JSON.parse(new TextDecoder().decode(buf));}
120
- function __merge(a,b){const r={};const ks=new Set([...Object.keys(a||{}),...Object.keys(b||{})]);for(const k of ks){const av=a&&a[k],bv=b&&b[k];r[k]=(av!==null&&typeof av==="object"&&bv!==null&&typeof bv==="object")?__merge(av,bv):(av!==undefined?av:bv);}return r;}
121
- const __codeFs=${codeFsJS};
122
- module.exports=__decompress(__c).then(function(__d){return{filesystem:__merge(__codeFs,__d.filesystem),permissions:__d.permissions};});
123
- `;
124
- } else {
125
- const fsJson = JSON.stringify(filesystem, null, options.pretty ? 2 : null);
126
- const permJson = JSON.stringify(permissions, null, options.pretty ? 2 : null);
127
- const fsJS = fsJson.replace(/"(require\('.*?'\))"/gm, '$1');
128
- JS = `module.exports = { filesystem: ${fsJS}, permissions: ${permJson} }`;
129
- }
89
+ const JS = `module.exports = { filesystem: ${fsJS}, permissions: ${permJson} }`;
130
90
 
131
91
  const tf = tmp.fileSync({ postfix: '.tmp.js' });
132
92
  fs.writeFileSync(tf.name, JS);
@@ -195,5 +155,40 @@ esbuild.build({
195
155
  alias,
196
156
  })
197
157
  .then(() => {
158
+ if (options.compress) {
159
+ // Post-process: gzip the entire esbuild output.
160
+ // The compressed bytes are written to <output>.gz as raw binary.
161
+ // The JS file is replaced with a tiny loader that fetches the .gz,
162
+ // decompresses it, and executes it — no base64 overhead.
163
+ const bundle = fs.readFileSync(options.output, 'utf8');
164
+ // Append a return statement so new Function() can hand back the value
165
+ const codeToCompress = bundle + `\nreturn ${options.variable};`;
166
+ const compressed = zlib.gzipSync(Buffer.from(codeToCompress, 'utf8'));
167
+ const gzPath = options.output.replace(/\.js$/, '') + '.brfs';
168
+ fs.writeFileSync(gzPath, compressed);
169
+
170
+ const varName = options.variable;
171
+ // The loader captures document.currentScript.src synchronously
172
+ // (unavailable once the async IIFE runs) and derives the .brfs URL.
173
+ const loader =
174
+ `globalThis.${varName}=(function(){` +
175
+ `var s=typeof document!=="undefined"&&document.currentScript?document.currentScript.src:` +
176
+ `typeof location!=="undefined"?location.href:null;` +
177
+ `if(!s)throw new Error("${varName}: cannot resolve .brfs URL");` +
178
+ `var u=s.replace(/\\.js(\\?[^#]*)?$/,".brfs");` +
179
+ `return fetch(u)` +
180
+ `.then(function(r){return r.arrayBuffer();})` +
181
+ `.then(function(b){` +
182
+ `var ds=new DecompressionStream("gzip");` +
183
+ `var w=ds.writable.getWriter();` +
184
+ `w.write(new Uint8Array(b));` +
185
+ `w.close();` +
186
+ `return new Response(ds.readable).text();` +
187
+ `})` +
188
+ `.then(function(code){return new Function(code)();});` +
189
+ `})();`;
190
+ fs.writeFileSync(options.output, loader);
191
+ console.log(`Compressed bundle written to ${gzPath}`);
192
+ }
198
193
  console.log('Build succeeded');
199
194
  });