Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad readline behavior with line ending characters? #4402

Closed
TankMasterRL opened this issue Dec 23, 2015 · 5 comments
Closed

Bad readline behavior with line ending characters? #4402

TankMasterRL opened this issue Dec 23, 2015 · 5 comments
Labels
confirmed-bug Issues with confirmed bugs. doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. readline Issues and PRs related to the built-in readline module.

Comments

@TankMasterRL
Copy link

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! [email protected] start: `node main.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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! [email protected] start: `node main.js`
npm ERR! Exit status 3221225725
npm ERR!
npm ERR! Failed at the [email protected] 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.

@mscdex mscdex added readline Issues and PRs related to the built-in readline module. question Issues that look for answers. labels Dec 24, 2015
@GenuineRex
Copy link

the node.js documentation for readline does not mention that the write function also writes the data to the input tty as if the user had typed the string (https://nodejs.org/api/readline.html#readline_rl_write_data_key). Since you are writing to tty input "Help\n" a recursion is happening where the line event is triggering itself. A stack overflow is expected.

Here is a version that demonstrates that write sends the string to the input and output:

"use strict"

var readline = require("readline");

function consoleOptions(line, readlineInstance) {
    var trimmedLine = line.trim();

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

    readlineInstance.prompt();
}
function createConsole()
{
    var readlineInstance;

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

    // Monitoring user actions
    readlineInstance.on("SIGINT", function() {
        readlineInstance.close();
    });
    readlineInstance.on("line", function(line) {
        console.log('--- line = ' + line );
        consoleOptions(line, this);
    });

    return readlineInstance;


}

var r = createConsole();

r.write("Help\n");

@mscdex
Copy link
Contributor

mscdex commented Apr 1, 2016

Re-opening this as this is a real wtf with the current behavior and the documentation. Here is a simpler reproduction of the issue:

require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
}).on('line', function(line) {
  this.write("new line\n");
});

This behavior goes back to at least v0.10, so I'm not sure how this would be viewed. It's a bug fix if the documentation is supposed to be correct, but it would be changing behavior so it's kind of a semver-major thing?

@mscdex mscdex reopened this Apr 1, 2016
@mscdex mscdex added confirmed-bug Issues with confirmed bugs. and removed question Issues that look for answers. labels Apr 1, 2016
@jasnell
Copy link
Member

jasnell commented Jun 6, 2016

I think this is a case of bad documentation and a confusing API. If you look at how rl.write() is used it becomes clear that it's generally being used to interact with the terminal, causing the data to be read as input. If you change the test case to :

require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
}).on('line', function(line) {
  process.stdout.write("new line\n");
});

you'll see that it operates with no problem. The code is working as it's supposed to but the documentation is definitely wrong.

@jasnell
Copy link
Member

jasnell commented Jul 6, 2016

@nodejs/documentation

@Fishrock123 Fishrock123 added the good first issue Issues that are suitable for first-time contributors. label Jul 19, 2016
jasnell added a commit to jasnell/node that referenced this issue Aug 29, 2016
Fishrock123 pushed a commit to Fishrock123/node that referenced this issue Sep 8, 2016
Fixes: nodejs#4402
PR-URL: nodejs#8295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Fishrock123 pushed a commit that referenced this issue Sep 9, 2016
Fixes: #4402
PR-URL: #8295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this issue Sep 30, 2016
Fixes: #4402
PR-URL: #8295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Fixes: #4402
PR-URL: #8295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
rvagg pushed a commit that referenced this issue Oct 18, 2016
Fixes: #4402
PR-URL: #8295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 26, 2016
Fixes: #4402
PR-URL: #8295
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
@sam-github sam-github added doc Issues and PRs related to the documentations. and removed doc Issues and PRs related to the documentations. labels Dec 1, 2016
@nourqweder
Copy link

How to convert scanf("%d %d", &j, &k); in c to node js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. readline Issues and PRs related to the built-in readline module.
Projects
None yet
Development

No branches or pull requests

7 participants