diff --git a/README.md b/README.md index 002798a..170fbda 100644 --- a/README.md +++ b/README.md @@ -252,12 +252,3 @@ Why did I choose it? : + The good modules (native addon) for synchronous execution exist. But node-gyp can't compile those in some platforms or Node versions. + I think that the security is important more than the speed. Some modules have problem about security. (Those don't protect data.) I think that the speed is not needed usually, because readlineSync is used while user types keys. - -## Release History - * 2015-02-22 v0.6.0 Add `setBufferSize()`. - * 2015-02-12 v0.5.5 Support the Synchronous Process Execution of Node v0.12(v0.11). - * 2015-01-27 v0.5.0 Add `options.noTrim`. - * 2014-07-12 v0.4.0 Add `options.noEchoBack`. - * 2014-07-12 v0.3.0 Add `setPrint()`. - * 2013-08-30 v0.2.0 Rewrite exporting methods. - * 2013-08-29 v0.1.0 Initial release. diff --git a/lib/encrypt.js b/lib/encrypt.js index ba8f62a..576eda2 100644 --- a/lib/encrypt.js +++ b/lib/encrypt.js @@ -1,3 +1,11 @@ +/* + * readlineSync + * https://github.com/anseki/readline-sync + * + * Copyright (c) 2015 anseki + * Licensed under the MIT license. + */ + var cipher = require('crypto').createCipher( process.argv[2] /*algorithm*/, process.argv[3] /*password*/), stdin = process.stdin, diff --git a/lib/read.cs.js b/lib/read.cs.js index 3fae749..b454100 100644 --- a/lib/read.cs.js +++ b/lib/read.cs.js @@ -1,4 +1,12 @@ -/*jshint wsh:true */ +/* jshint wsh:true */ + +/* + * readlineSync + * https://github.com/anseki/readline-sync + * + * Copyright (c) 2015 anseki + * Licensed under the MIT license. + */ var FSO_ForReading = 1, FSO_ForWriting = 2, diff --git a/lib/read.ps1 b/lib/read.ps1 index a10863d..ea43e4b 100644 --- a/lib/read.ps1 +++ b/lib/read.ps1 @@ -1,3 +1,8 @@ +# readlineSync +# https://github.com/anseki/readline-sync +# +# Copyright (c) 2015 anseki +# Licensed under the MIT license. Param( [string] $display, @@ -38,22 +43,23 @@ if ($options.encoded) { [bool] $isCooked = (-not $options.noEchoBack) -and (-not $options.keyIn) function writeTTY ($text) { - execWithTTY ('Write-Host ''' + ($text -replace '''', '''''') + ''' -NoNewline') | Out-Null + execWithTTY ('Write-Host ''' + ($text -replace '''', '''''') + ''' -NoNewline') $script:isInputLine = $True } # Instant method that opens TTY without CreateFile via P/Invoke in .NET Framework # **NOTE** Don't include special characters of DOS in $command when $getRes is True. +# [string] $cmdPath = $Env:ComSpec +# [string] $psPath = 'powershell.exe' function execWithTTY ($command, $getRes = $False) { if ($getRes) { $res = (cmd.exe /C "CON powershell.exe -Command -" if ($LastExitCode -ne 0) { exit 1 } - $res = '' } - return $res } if ($options.display -ne '') { @@ -76,11 +82,11 @@ while ($True) { # ReadKey() may returns [System.Array], then don't cast data. if ($chunk -isnot [string]) { $chunk = '' } $chunk = $chunk -replace '[\r\n]', '' - if ($chunk -eq '') { $isEol = $True } # NL or empty-text was input + if ($chunk -eq '') { $atEol = $True } # NL or empty-text was input } else { $chunk = execWithTTY 'Read-Host' $True $chunk = $chunk -replace '[\r\n]', '' - $isEol = $True + $atEol = $True } # other ctrl-chars @@ -95,10 +101,10 @@ while ($True) { } $inputTTY += $chunk - if ($isEol -or ($options.keyIn -and ($inputTTY.Length -ge $reqSize))) { break } + if ($atEol -or ($options.keyIn -and ($inputTTY.Length -ge $reqSize))) { break } } if ((-not $isCooked) -and (-not ($options.keyIn -and (-not $isInputLine)))) - { execWithTTY 'Write-Host ''''' | Out-Null } # new line + { execWithTTY 'Write-Host ''''' } # new line return '''' + $inputTTY + '''' diff --git a/lib/read.sh b/lib/read.sh index e91f034..2ecc442 100644 --- a/lib/read.sh +++ b/lib/read.sh @@ -1,3 +1,8 @@ +# readlineSync +# https://github.com/anseki/readline-sync +# +# Copyright (c) 2015 anseki +# Licensed under the MIT license. # getopt(s) while [ $# -ge 1 ]; do @@ -62,11 +67,11 @@ do if [ -z "$is_cooked" ]; then chunk="$(dd if=/dev/tty bs=1 count=1 2>/dev/null)" chunk="$(printf '%s' "$chunk" | tr -d '\r\n')" - [ -z "$chunk" ] && is_eol='true' # NL or empty-text was input + [ -z "$chunk" ] && at_eol='true' # NL or empty-text was input else IFS= read -r chunk 0 ? buffer.toString(encoding, 0, readSize) : '\n'; - if (!isCooked) { - chunk = chunk.replace(/[\r\n]/g, ''); - if (!chunk) { isEol = true; } // NL or empty-text was input - } else if (typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') { + if (typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') { chunk = line; - isEol = true; + atEol = true; } chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ''); // other ctrl-chars - if (chunk && options.keyIn && exclude) { chunk = chunk.replace(exclude, ''); } + if (chunk && exclude) { chunk = chunk.replace(exclude, ''); } if (chunk && !isCooked) { if (!options.noEchoBack) { @@ -177,7 +175,8 @@ function readlineSync(options) { } input += chunk; - if (isEol || options.keyIn && input.length >= reqSize) { break; } + if (!options.keyIn && atEol || + options.keyIn && input.length >= reqSize) { break; } } if (!isCooked && !silent) { fs.writeSync(fdW, '\n'); } @@ -454,7 +453,7 @@ exports.question = function(query, options) { exports.keyIn = function(query, options) { var limit = options ? flattenArray(options.limit, function(value) { - return typeof value === 'string' || typeof value === 'number'; }) : [], + return typeof value === 'string' || typeof value === 'number'; }).join('') : '', readOptions = { /* jshint eqnull:true */ display: query != null ? query + '' : '', @@ -462,8 +461,8 @@ exports.keyIn = function(query, options) { keyIn: true, noEchoBack: !!(options && options.noEchoBack), mask: mask, - exclude: limit.length ? - '[^' + limit.join('').replace(/\W/g, '\\$&') + ']' : '', + exclude: limit ? '[^' + + limit.replace(/\n/g, '').replace(/\W/g, '\\$&') + ']' : '', cs: !!(options && options.caseSensitive), noTrim: true };