diff --git a/README.md b/README.md index bff0432..946aeaf 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ readlineSync.setPrompt(encoding) Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'. +## Note +The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms. + ## Release History + * 2013-12-18 v0.2.1 Error handle for the environment which don't support interactively reading from stdin. * 2013-08-30 v0.2.0 Rewrite exporting methods. * 2013-08-29 v0.1.0 Initial release. diff --git a/lib/readline-sync.js b/lib/readline-sync.js index d625ebd..02701cb 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -22,7 +22,22 @@ var _readlineSync = function(display) { if (display) { stdout.write(display, encoding); } stdin.resume(); - while ((rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)) > 0) { + while (true) { + rsize = 0; + try { + rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE); + } catch (e) { + if (e.code === 'EAGAIN') { + // Error: EAGAIN, resource temporarily unavailable + if (display) { stdout.write('\n', encoding); } // Next of prompt line. + console.error('Error: This machine don\'t support interactively reading from stdin.'); + process.exit(1); + } else if (e.code === 'EOF') { + break; + } + throw e; + } + if (rsize === 0) { break; } input += buffer.toString(encoding, 0, rsize); if (/[\r\n]$/.test(input)) { break; } } diff --git a/package.json b/package.json index 2ca3c7e..2ac2a02 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "readline-sync", "description": "Synchronous Readline", - "version": "0.2.0", + "version": "0.2.1", "homepage": "https://github.com/anseki/readline-sync", "author": { "name": "anseki"