◆結論
Compression Streams API – Web API | MDN (mozilla.org)
例えばdeflateの解凍だけを関数にしたら以下のとおり。
export const inflate = (data: ReadableStream): Body => { const decompressedStream = data.pipeThrough(new DecompressionStream('deflate')); return new Response(decompressedStream); }; export const inflateBlob = (data: Blob): Body => { const decompressedStream = data.stream().pipeThrough(new DecompressionStream('deflate')); return new Response(decompressedStream); }; export const inflateArrayBuffer = (data: ArrayBuffer): Body => { const resp = new Response(data); if (!resp.body) return resp; const decompressedStream = resp.body.pipeThrough(new DecompressionStream('deflate')); return new Response(decompressedStream); };
‘deflate’を’gzip’にすればgzipだし、DecompressionStreamをCompressionStreamにすれば圧縮できるし。
上記はTypeScriptなんで、JavaScriptで使おうと思ったら「:」の型指定を消して差し上げろ。exportも不要なら消せ。