Created
September 6, 2022 17:48
-
-
Save hildjj/b794aa0f87cf88b1a8f7416df69acec5 to your computer and use it in GitHub Desktop.
Pipes are binary safe
This file contains hidden or 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
// Compress a given string using the lzma program, and return the compressed contents. | |
const { spawn } = require('child_process') | |
const compress = str => new Promise((resolve, reject) => { | |
const lzma = spawn('cat', | |
{ stdio: ["pipe", "pipe", "inherit"] }) | |
lzma.on('error', err => { | |
reject(err) | |
}) | |
lzma.on('exit', (code, signal) => { | |
if (code) { | |
reject(code) | |
} | |
else if (signal) { | |
reject(signal) | |
} | |
else { | |
resolve(Buffer.concat(compressed)) | |
} | |
}) | |
lzma.stdin.write(str) | |
lzma.stdin.end() | |
const compressed = [] | |
lzma.stdout.on('data', data => { compressed.push(data) }) | |
}) | |
module.exports = compress | |
const b = Buffer.alloc(256) | |
for (let i = 0; i < 256; i++) { b[i] = i } | |
compress(b).then(c => console.log(c.toString('hex'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment