Created
July 3, 2022 21:49
-
-
Save OR13/e30720d88d72b7449f34c86781b9f185 to your computer and use it in GitHub Desktop.
zstd streams, buffers, promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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