update-file-content
Version:
Utility for executing RegEx replacement on files, powered by stream.
72 lines (62 loc) • 1.98 kB
JavaScript
import { pipeline } from "stream";
import rw from "./rw-stream/index.mjs";
import { Transform, NukableTransform } from "./transform.mjs";
async function process_stream (
readableStream,
writableStream,
options
) {
const { processFunc, truncate } = options;
let transformStream;
try {
if (processFunc.withLimit) {
transformStream = new NukableTransform({
...options,
withFalloutShelter: !truncate,
});
let limitReached = false;
processFunc._cb_limit = () => {
if (limitReached) {
return Symbol.for("notified");
} else {
limitReached = true;
transformStream.detonateTheBombNow = true
// starting from v14.0.0, The pipeline will wait for the 'close' event
// for non-duplex & non-legacy streams created with the emitClose option.
// so marking the end of the readableStream manually is required.
if(truncate)
readableStream.push(null);
}
}
} else {
transformStream = new Transform(options);
}
} catch (err) {
readableStream.destroy();
transformStream
&& typeof transformStream.destroy === "function"
&& transformStream.destroy();
writableStream.destroy();
return Promise.reject(err);
}
return new Promise((resolve, reject) => {
pipeline (
readableStream,
transformStream,
writableStream,
err => err ? reject(err) : resolve(writableStream)
);
});
}
async function rw_stream(filepath, options) {
const { readableStream, writableStream } = await rw(
filepath,
{
readStart: options.readStart,
writeStart: options.writeStart
}
);
return process_stream(readableStream, writableStream, options)
.then(() => void 0); // not leaking the reference to local writableStream
}
export { rw_stream, process_stream };