Skip to content

Instantly share code, notes, and snippets.

@BitLooter
Last active December 30, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BitLooter/d507cf7c5b320e806fec06b22d2ccaa4 to your computer and use it in GitHub Desktop.
Save BitLooter/d507cf7c5b320e806fec06b22d2ccaa4 to your computer and use it in GitHub Desktop.
import { Inflate } from 'fflate'
class FflateSink {
constructor(uncompressedSize) {
this.output = new Uint8Array(uncompressedSize)
let bytesWritten = 0
this.fflate = new Inflate( (data, final) => {
this.output.set(data, bytesWritten)
bytesWritten += data.byteLength
// In the original code output is a Promise that is resolved here when
// final is true, but I've removed those parts to keep the example simple
} )
}
write(chunk) {
this.fflate.push(chunk)
}
close() {
// No final chunk to push, using an empty buffer instead throws an error
this.fflate.push(new Uint8Array(), true)
}
}
const SAMPLE_ARRAY = [0x55,0xc8,0xb1,0x0d,0x80,0x30,0x0c,0x04,0xc0,0x9e,0x29,0x7e,0x02,0x06,0x60,0x0e,0x16,0x80,0xf8,0x43,0x2c,0x25,0x36,0x8a,0x2d,0x65,0x7d,0x6a,0xae,0xbc,0xb3,0x69,0xe0,0x42,0x77,0x77,0x7b,0x50,0xb5,0x13,0x07,0x16,0x61,0xa4,0x20,0x1d,0x41,0x22,0x1b,0x21,0x5a,0x2b,0x27,0xad,0x10,0x37,0x73,0x91,0xf6,0xeb,0x44,0xf1,0xf1,0x4e,0x46,0xa8,0x1b,0x06,0xb3,0xb9,0xc4,0xbe,0x7d]
const SAMPLE = new Uint8Array(SAMPLE_ARRAY)
const SAMPLE_UNCOMPRESSED_ARRAY = [0x54,0x68,0x69,0x73,0x20,0x61,0x20,0x6c,0x6f,0x6f,0x6f,0x6e,0x67,0x20,0x66,0x69,0x6c,0x65,0x20,0x3a,0x20,0x77,0x65,0x20,0x6e,0x65,0x65,0x64,0x20,0x74,0x6f,0x20,0x73,0x65,0x65,0x20,0x74,0x68,0x65,0x20,0x64,0x69,0x66,0x66,0x65,0x72,0x65,0x6e,0x63,0x65,0x20,0x62,0x65,0x74,0x77,0x65,0x65,0x6e,0x20,0x74,0x68,0x65,0x20,0x64,0x69,0x66,0x66,0x65,0x72,0x65,0x6e,0x74,0x20,0x63,0x6f,0x6d,0x70,0x72,0x65,0x73,0x73,0x69,0x6f,0x6e,0x20,0x6d,0x65,0x74,0x68,0x6f,0x64,0x73,0x2e,0x0a]
const SAMPLE_UNCOMPRESSED = new Uint8Array(SAMPLE_UNCOMPRESSED_ARRAY)
const SAMPLE_UNCOMPRESSED_SIZE = 94
const sink = new FflateSink(SAMPLE_UNCOMPRESSED_SIZE)
sink.write(SAMPLE)
// fflate throws an error here
sink.close()
console.log(
"Difference between expected and actual: ",
Buffer.compare(SAMPLE_UNCOMPRESSED, sink.output)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment