Skip to content

Instantly share code, notes, and snippets.

@OR13
Created July 3, 2022 21:49
Show Gist options
  • Save OR13/e30720d88d72b7449f34c86781b9f185 to your computer and use it in GitHub Desktop.
Save OR13/e30720d88d72b7449f34c86781b9f185 to your computer and use it in GitHub Desktop.
zstd streams, buffers, promises
const { Readable } = require("stream");
const { ZSTDCompress, ZSTDDecompress } = require("simple-zstd");
const compress = (data) => {
return new Promise((resolve) => {
const stream = Readable.from(data);
const compressedStream = stream.pipe(ZSTDCompress(3));
const bufs = [];
compressedStream.on("data", function (d) {
bufs.push(d);
});
compressedStream.on("end", function () {
resolve(Buffer.concat(bufs));
});
});
};
const decompress = (data) => {
return new Promise((resolve) => {
const stream = Readable.from(data);
const compressedStream = stream.pipe(ZSTDDecompress());
const bufs = [];
compressedStream.on("data", function (d) {
bufs.push(d);
});
compressedStream.on("end", function () {
resolve(Buffer.concat(bufs));
});
});
};
module.exports = { compress, decompress };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment