xlerobot-sim / src /meshpack.js
VectorW's picture
Ship the engine and meshes gzipped: 11 MB instead of 31 MB in 4 requests
718fed8 verified
Raw History Blame Contribute Delete
1 kB
// The model's meshes travel as one gzip file plus an index. The Space's static host sends files
// uncompressed and redirects each request, so one 7.5 MB request replaces 33 files of 19 MB.
// Used in the browser and in Node (tools/), which both provide DecompressionStream.
/** Decompress gzip bytes. Bytes without the gzip magic number are returned as they are: some
* servers (Vite's preview, for one) send .gz files with Content-Encoding: gzip, and the browser
* has already inflated them. */
export async function gunzip(bytes) {
if (bytes[0] !== 0x1f || bytes[1] !== 0x8b) return bytes;
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"));
return new Uint8Array(await new Response(stream).arrayBuffer());
}
/** Split an unpacked mesh file into [name, bytes] pairs, from index entries [name, offset, length]. */
export function unpack(bytes, index) {
return index.map(([name, offset, length]) => [name, bytes.subarray(offset, offset + length)]);
}