js: make tryDecompress async

This commit is contained in:
Brandon Liu
2022-11-14 21:41:57 +08:00
parent 6d866e3507
commit bcb2313d1e

View File

@@ -141,7 +141,7 @@ export enum Compression {
Zstd = 4, Zstd = 4,
} }
function tryDecompress(buf: ArrayBuffer, compression: Compression) { async function tryDecompress(buf: ArrayBuffer, compression: Compression) {
if (compression === Compression.None || compression === Compression.Unknown) { if (compression === Compression.None || compression === Compression.Unknown) {
return buf; return buf;
} else if (compression === Compression.Gzip) { } else if (compression === Compression.Gzip) {
@@ -461,7 +461,7 @@ async function getHeaderAndRoot(
header.rootDirectoryLength; header.rootDirectoryLength;
const rootDir = deserializeIndex( const rootDir = deserializeIndex(
tryDecompress(rootDirData, header.internalCompression) await tryDecompress(rootDirData, header.internalCompression)
); );
return [header, [dirKey, ENTRY_SIZE_BYTES * rootDir.length, rootDir]]; return [header, [dirKey, ENTRY_SIZE_BYTES * rootDir.length, rootDir]];
} }
@@ -481,7 +481,7 @@ async function getDirectory(
throw new EtagMismatch(resp.etag); throw new EtagMismatch(resp.etag);
} }
const data = tryDecompress(resp.data, header.internalCompression); const data = await tryDecompress(resp.data, header.internalCompression);
const directory = deserializeIndex(data); const directory = deserializeIndex(data);
if (directory.length === 0) { if (directory.length === 0) {
throw new Error("Empty directory is invalid"); throw new Error("Empty directory is invalid");
@@ -843,7 +843,7 @@ export class PMTiles {
throw new EtagMismatch(resp.etag); throw new EtagMismatch(resp.etag);
} }
return { return {
data: tryDecompress(resp.data, header.tileCompression), data: await tryDecompress(resp.data, header.tileCompression),
cacheControl: resp.cacheControl, cacheControl: resp.cacheControl,
expires: resp.expires, expires: resp.expires,
}; };
@@ -886,7 +886,7 @@ export class PMTiles {
if (header.etag && header.etag !== resp.etag) { if (header.etag && header.etag !== resp.etag) {
throw new EtagMismatch(resp.etag); throw new EtagMismatch(resp.etag);
} }
const decompressed = tryDecompress(resp.data, header.internalCompression); const decompressed = await tryDecompress(resp.data, header.internalCompression);
const dec = new TextDecoder("utf-8"); const dec = new TextDecoder("utf-8");
return JSON.parse(dec.decode(decompressed)); return JSON.parse(dec.decode(decompressed));
} }