/** * The zlib and gzip payloads of a JData document, compressed or decompressed together on threads * (zlibmt), for decodeAsync, encodeAsync and the other async functions. * * Copyright (c) 2019-2026 Qianqian Fang . BSD 3-clause license. */ import { Codec, getCodec, giveAway, ZLIB_CODEC, GZIP_CODEC } from './codecs'; import { canonicalType, ELEM_BYTES, product, isTypedArray } from './ndarray'; import { ThreadOptions, runner, execute, zlibmtDecode, zlibmtEncode } from './zlibmt'; // ---- payloads of a document ------------------------------------------------------------------- /** Bytes of a _ArrayZipData_ entry for zlib and gzip, as the decoder reads it. */ type ZipBytes = (z: any) => Uint8Array; /** The zlib and gzip payloads of a JData value, inflated on threads: entry -> bytes. */ export async function unzipPayloads(value: any, codecs: { [name: string]: Codec } | undefined, bytesOfEntry: ZipBytes, o: ThreadOptions): Promise> { const found: Array<{ z: any; gz: boolean; size?: number }> = []; const seen = new Set(); const walk = (x: any): void => { if (!x || typeof x !== 'object' || isTypedArray(x) || seen.has(x)) { return; } seen.add(x); if (Array.isArray(x)) { for (const e of x) { walk(e); } return; } if (x instanceof Map) { return; } if (x._ArrayZipData_ !== undefined && x._ArrayType_ !== undefined) { const zt = (x._ArrayZipType_ === undefined ? 'zlib' : String(x._ArrayZipType_)).toLowerCase(); const c = zt === 'zlib' || zt === 'gzip' ? getCodec(zt, codecs) : null; if (c === ZLIB_CODEC || c === GZIP_CODEC) { const t = canonicalType(x._ArrayType_); const chunked = x._ArrayChunks_ !== undefined && Array.isArray(x._ArrayZipData_); let size: number | undefined; if (!chunked && t) { const zs = x._ArrayZipSize_ !== undefined ? x._ArrayZipSize_ : [product(Array.from(x._ArraySize_ || [], Number) as number[])]; size = product(Array.from(typeof zs === 'number' ? [zs] : zs, Number) as number[]) * ELEM_BYTES[t]; } for (const z of chunked ? x._ArrayZipData_ : [x._ArrayZipData_]) { found.push({ z, gz: c === GZIP_CODEC, size: Number.isFinite(size as number) ? size : undefined }); } } return; } for (const k of Object.keys(x)) { walk(x[k]); } }; walk(value); const out = new Map(); if (!found.length) { return out; } const R = runner(o); await Promise.all(found.map(async (f) => { if (out.has(f.z)) { return; } const b = bytesOfEntry(f.z); // (small payloads here: a thread costs more than they do) const r = b.length < (16 << 10) ? execute({ op: f.gz ? 'gunzip' : 'inflate', data: b }).out : f.gz ? (await R.run({ op: 'gunzip', data: b })).out : await zlibmtDecode(b, f.size, R); out.set(f.z, giveAway(r)); })); return out; } /** A zlib or gzip payload to compress later: the bytes, and where its result goes. */ export interface ZipJob { bytes: Uint8Array; gz: boolean; level: number; put: (c: Uint8Array) => void; } /** Compresses the jobs on threads (zlib in zlibmt blocks), putting each result in place. */ export async function zipPayloads(jobs: ZipJob[], o: ThreadOptions): Promise { if (!jobs.length) { return; } const R = runner(o); await Promise.all(jobs.map(async (j) => { const c = j.bytes.length < (16 << 10) ? execute({ op: j.gz ? 'gzip' : 'deflate', data: j.bytes, level: j.level }).out : j.gz ? (await R.run({ op: 'gzip', data: j.bytes, level: j.level })).out : await zlibmtEncode(j.bytes, j.level, R); j.put(c); })); }