s2maps-gpu
Version:
S2 Maps GPU - An open source, high-performance, and GPU-accelerated map engine for rendering large-scale, interactive maps.
26 lines (25 loc) • 1.02 kB
JavaScript
import { concatUint8Arrays } from './index.js';
/**
* A Browser compatible Gzip decompression function
* @param bytes - the data to decompress
* @param format - the format of the data. Defaults to 'gzip'
* @returns - the decompressed data
*/
export async function decompressStream(bytes, format) {
// first test if polyfill was already added
if (globalThis.decompressionPolyfill !== undefined) {
return globalThis.decompressionPolyfill(bytes, format);
}
if (format === undefined)
format = bytes[0] === 0x1f && bytes[1] === 0x8b ? 'gzip' : 'deflate';
// Convert the bytes to a stream.
const stream = new Blob([bytes]).stream();
// Create a decompressed stream.
const decompressedStream = stream.pipeThrough(new DecompressionStream(format));
// Read all the bytes from this stream.
const chunks = [];
// @ts-expect-error - this works
for await (const chunk of decompressedStream)
chunks.push(chunk);
return await concatUint8Arrays(chunks);
}