Open williamstein opened 10 months ago
I used the following async code as a workaround, for anybody who sees this. This took me a while to work out, but does properly allow for dealing with error cases...
import { createDecoderStream, createEncoderStream } from "lz4";
import { createReadStream, createWriteStream } from "fs";
import { PassThrough } from "stream";
export async function readFileLz4(path: string): Promise<Buffer> {
const decoder = createDecoderStream();
const input = createReadStream(path);
const output = new PassThrough();
input.pipe(decoder).pipe(output);
const chunks: Buffer[] = [];
const waitForFinish = new Promise((resolve, reject) => {
decoder.on("error", reject);
output.on("finish", resolve);
output.on("error", reject);
output.on("data", (chunk) => {
chunks.push(chunk);
});
});
await waitForFinish;
return Buffer.concat(chunks);
}
export async function writeFileLz4(path: string, contents: string) {
// We use a stream instead of blocking in process for compression
// because this code is likely to run in the project's daemon,
// and blocking here would block interactive functionality such
// as terminals.
// Create readable stream from the input.
const input = new Readable({
read() {
this.push(contents);
this.push(null);
},
});
// lz4 compression encoder
const encoder = createEncoderStream();
const output = createWriteStream(path);
// start writing
input.pipe(encoder).pipe(output);
// wait until done
const waitForFinish = new Promise((resolve, reject) => {
encoder.on("error", reject);
output.on("finish", resolve);
output.on("error", reject);
});
await waitForFinish;
}
I'm using nodejs.
Unfortunately, this makes decode unusable in certain robust applications. The underlying code I think forgets to listen to an error event.