Skip to content

Instantly share code, notes, and snippets.

@hildjj
Created September 6, 2022 17:48
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 hildjj/b794aa0f87cf88b1a8f7416df69acec5 to your computer and use it in GitHub Desktop.
Save hildjj/b794aa0f87cf88b1a8f7416df69acec5 to your computer and use it in GitHub Desktop.
Pipes are binary safe
// 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