Construct a compressed Response to take advantage of native decompression

Viewed 170

My application bundles some brotli-compresset assets and I want to somehow decompress them using browser itself instead of shipping a decompression library. The best way I could think of to trick browser into doing that is simulating a compressed response, which, unfortunately, doesn't work:

// Here's a brotli-compressed JSON
const compressedBytes = new Uint8Array([161,232,7,128,225,60,176,219,56,82,174,50,55,131,242,211,168,22,217,235,218,154,50,66,77,182,205,166,93,90,37,27,71,177,131,132,178,39,139,111,22,157,174,62,78,57,215,150,98,130,73,70,223,18,63,30,0,14,14,57,104,135,68,162,247,46,194,120,39,9,132,46,144,108,191,147,200,106,67,47,244,253,195,243,78,156,106,123,173,29,132,142,15,209,17,230,72,114,152,70,211,188,205,84,49,205,172,101,245,67,59,130,0,116,246,124,222,250,15]);
const headers = new Headers();
headers.append( 'Content-Encoding', 'br' );
headers.append( 'Content-Type', 'application/json' );
const r = new Response( compressedBytes.buffer, { status: 200, statusText: 'OK', headers } );
await r.arrayBuffer(); // Unfortunately just outputs a provided buffer w/o decompressing it

I tried to put a request into cache, clone it, but nothing seems to work. Perhaps anyone know a way to achieve native decompression? DecompressionStream API is somehow relevant, but it doesn't support brotli and have compatibility issues.

1 Answers

According to this issue, it seems that the brotli decoder included in the browsers is not exposed to JS and that the prefered way for decoding brotli is to use a library (like brotli.js)

DecompressionStream API in its current version is not relevant because :

Compression formats other than "gzip" and "deflate" will not be supported in the first version of the API.

(source)

An issue to support brotli has been opened, but I guess you'll have to wait a bit longer ^^

Related