Skip to content

Bad readline behavior with line ending characters? #4402

Closed
@TankMasterRL

Description

@TankMasterRL

When including a trailing newline character (or carriage return character) in a string that is supplied to a write function, an RangeError is thrown (RangeError: Maximum call stack size exceeded). The following code snippet shows the context:

else if (trimmedLine.match(/^h(elp)?$/i)) {
    readlineInstance.write("Help\n");
}

Here is an error log I created by issuing a command that passes the console output to a text file (npm start > console_output.txt):

path\GitHubBackup\console\console.js:12
        var trimmedLine = line.trim();
                               ^

RangeError: Maximum call stack size exceeded
    at String.trim (native)
    at consoleOptions (path\GitHubBackup\console\console.js:12:25)
    at Interface.<anonymous> (path\GitHubBackup\console\console.js:46:14)
    at emitOne (events.js:82:20)
    at Interface.emit (events.js:169:7)
    at Interface._onLine (readline.js:210:10)
    at Interface.<anonymous> (readline.js:340:12)
    at Array.forEach (native)
    at Interface._normalWrite (readline.js:339:11)
    at Interface.write (readline.js:309:49)

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\***\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v5.3.0
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! github.backup@0.1.0 start: `node main.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the github.backup@0.1.0 start script 'node main.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the github.backup package
,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node main.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs github.backup
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls github.backup
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     path\GitHubBackup\npm-debug.log

The ordinary error log doesn't show the details above, only an exit status entry:

(...Break...)
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help
Help

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\***\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v5.3.0
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! github.backup@0.1.0 start: `node main.js`
npm ERR! Exit status 3221225725
npm ERR!
npm ERR! Failed at the github.backup@0.1.0 start script 'node main.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the github.backup package
,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node main.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs github.backup
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls github.backup
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     path\GitHubBackup\npm-debug.log

The full code:

"use strict"

// CONSTANTS
var CONSOLE_PROMPT_NAME = "GitHubBackup";

// DEPENDENCIES
var readline = require("readline");
const chalk = require("chalk");

// -CONSOLE OPTIONS
function consoleOptions(line, readlineInstance) {
    var trimmedLine = line.trim();

    if (trimmedLine.match(/^q(uit)?$/i)) {
        readlineInstance.write("Closing...");
        readlineInstance.close();
    }
    else if (trimmedLine.match(/^h(elp)?$/i)) {
        readlineInstance.write("Help\n");
    }

    readlineInstance.prompt();
}

module.exports = function() {
    return {
        // -CONSOLE FUNCTIONS
        createConsole: function() {
            var readlineInstance;

            // Setting up a prompt using the "readline" module
            readlineInstance = readline.createInterface({
                input: process.stdin,
                output: process.stdout
            });
            readlineInstance.setPrompt(chalk.green(CONSOLE_PROMPT_NAME + ">"));
            readlineInstance.prompt();

            // Monitoring user actions
            readlineInstance.on("SIGINT", function() {
                readlineInstance.close();
            });
            readlineInstance.on("line", function(line) {
                consoleOptions(line, this);
            });

            return readlineInstance;
        }
    };
}

Is this a valid behaviour of the readline module? Note again that this only happens with trailing line endings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.docIssues and PRs related to the documentations.good first issueIssues that are suitable for first-time contributors.readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions