update-file-content
Version:
Utility for executing RegEx replacement on files, powered by stream.
73 lines (63 loc) • 2.02 kB
JavaScript
;
const { pipeline } = require("stream");
const rw = require("./rw-stream/index.js");
const { Transform, NukableTransform } = require("./transform.js");
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
}
module.exports = { rw_stream, process_stream };