#Is there a way to compress using `zlib`?

1 messages · Page 1 of 1 (latest)

spark fox
#

Bun.gunzipSync() is perfectly happy to accept zlib compressed files that start with the following magic header:

78 5E - Fast Compression
78 9C - Default Compression
78 DA - Best Compression```

However when trying to use `gzipSync` to compress it again, the gzip header `1F 8B` is used. Can I force `gzipSync` to use the zlib header?
spark fox
#

Found a solution const compressed = Bun.deflateSync(data, { level: 9, windowBits: -15 }); seems to prefix the data with 78 DA as well. So I need gunzipSync for decompression and deflateSync for compression. Using inflateSync on the data outputted by deflateSync using those level/windowsettings, won't work as that results in error: invalid stored block lengths

#

I don't think this is the intended behavior 🙂

#

sample program of the problem```js

const buffer = new Uint8Array([0x12, 0x01, 0x3, 0x5, 0x5]);

{
const compressed = Bun.deflateSync(buffer, { level: 9, windowBits: -15 });

console.log(compressed);

const decompressed = Bun.inflateSync(compressed);

// decompressed isn't a Uint8array but an error: invalid stored block lengths
console.log(decompressed);

}

{
const compressed = Bun.deflateSync(buffer, { level: 9, windowBits: -15 });

console.log(compressed);

const decompressed = Bun.gunzipSync(compressed);

// decompressed works fine
console.log(decompressed);

}

agile condor
#

interesting. this could be a result of using the cloudflare fork for zlib

#

maybe, i dont know the internals of gzip/zlib enough

#

reminds me of https://github.com/oven-sh/bun/pull/8529
@stable gust do you think cloudflare zlib vs zlib-ng would affect anything related to this
asking because i simply dont know- i remember someone had an edge case really far out with zlib being incompatible, potentially related to this

stable gust
#

honestly not sure.. i could build bun using zlib-ng and send the binary to see if it helps later today

spark fox
#

great, I can test it if you want to. Although the sample problem above should be enough to demonstrate the problem

agile condor
#

the only thing i worry about the switch is this needs to be resolved. right now cloudflare zlib is used by ... cloudflare, very large and very confident in it's ability to work

spark fox
#

I understand that. Obviously it doesn't need fixing right away. But it is something to be aware of, inflate/deflate to not always work together, whereas technically obviously they should. And the result isn't always a Uint8Array, but can be an Error hidden in disguise as well. There is no throw.

agile condor
#

oh, that is a bug and it should be easy to fix

#

the error throw

#

can you check if that has an issue, otherwise open one. and then a separate issue for #1207001103011422208 message where the output is unexpected

spark fox
#

got it!

#

will do

agile condor
#

and for the latter should mention the pr related to zlib as something that might help but idk

spark fox
agile condor
#

wait this isnt specific to windows right

#

let me doublecheck

#

ye its not specific

spark fox
#

probably not

#

🙂

#

and this is the other one

#

I might have a related feature request: being able to get the compression settings from the gzip header and use that as input for inflateSync and/or gunzipSync.