有没有办法让“ readline”正确响应“ pause”事件?

时间:2019-10-26 15:51:39

标签: node.js readline

我正在尝试编写一个可以在输出中包含其他文件的降价预处理器。

很遗憾,我发现了"the pause method does not stop queued up line events"

我编写的解决方案将完成读写原始文件,然后在末尾附加其他文件:

import { createInterface } from "readline";
import * as fs from "fs";
import * as path from "path";
import minimist from "minimist";

const argv = minimist(process.argv.slice(2));

const inputFile = path.resolve(argv._[0]);
const outputFile = path.resolve(argv._[1]);

const inputFileReadStream = createInterface({
    "input": fs.createReadStream(inputFile)
});

const writeStream = fs.createWriteStream(outputFile);

readStream.on("line", function readLine(line) {
    if (line.startsWith("@import") === true) {
        inputFileReadStream.pause();

        console.log(line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\"")));

        const readStream = createInterface({
            "input": fs.createReadStream(path.resolve(path.dirname(inputFile), line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""))))
        });

        readStream.on("line", function(line) {
            readLine(line);
        });

        readStream.on("end", function() {
            inputFileReadStream.resume();
        });
    } else {
        writeStream.write(line + "\n");
    }
});

因此,我尝试对其进行修改,以使用我链接的文章中提到的line-by-line库,以查看是否可行:

let readStream = new (require("line-by-line"))(fs.createReadStream(inputFile));

const writeStream = fs.createWriteStream(outputFile);

readStream.on("line", function readLine(line) {
    if (line.startsWith("@import") === true) {
        const tempStream = readStream;

        tempStream.pause();

        console.log(line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\"")));

        readStream = new (require("line-by-line"))(fs.createReadStream(path.resolve(path.dirname(inputFile), line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\"")))));

        readStream.on("line", function(line) {
            readLine(line);
        });

        readStream.on("end", function() {
            tempStream.resume();
        });
    } else {
        writeStream.write(line + "\n");
    }
});

但是这产生了一个文件,该文件逐渐将行从导入的文件散布到主文件中。

我的问题是:有没有办法让readline正确响应pause事件?

这可以用Transform来解决吗?

如果有帮助,请访问以下链接到存储库:https://github.com/brianjenkins94/Rosettas-Node

1 个答案:

答案 0 :(得分:0)

这可能会对性能产生负面影响,但是通过将highWaterMark的值设置为1,我得到了我想要的。

import { createInterface } from "readline";
import * as fs from "fs";
import * as path from "path";
import minimist from "minimist";

const argv = minimist(process.argv.slice(2));

const inputFile = path.resolve(argv._[0]);
const outputFile = path.resolve(argv._[1]);

const inputFileReadStream = createInterface({
    "input": fs.createReadStream(inputFile, { "highWaterMark": 1 })
});

const writeStream = fs.createWriteStream(outputFile);

inputFileReadStream.on("line", function readLine(line, readStream = inputFileReadStream) {
    if (line.startsWith("@import") === true) {
        readStream.pause();

        const childReadStream = createInterface({
            "input": fs.createReadStream(path.resolve(path.dirname(inputFile), line.substring(line.indexOf("\"") + 1, line.lastIndexOf("\""))), { "highWaterMark": 1 })
        });

        childReadStream.on("line", function(line) {
            readLine(line, childReadStream);
        });

        childReadStream.on("close", function() {
            readStream.resume();
        });
    } else {
        writeStream.write(line + "\n");
    }
});