From a7bb63d3e317c5d283e5e188aa3060ffabcc804a Mon Sep 17 00:00:00 2001 From: anseki Date: Sun, 22 Mar 2015 14:11:55 +0900 Subject: [PATCH] Check that mask is '' --- README.md | 2 +- lib/read.cs.js | 3 ++- lib/readline-sync.js | 8 +++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b47950f..267b19b 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ readlineSync.setPrint(function(display, encoding) { currentMask = readlineSync.setMask([newMask]) ``` -Set the mask character that is shown instead of the secret text (e.g. password). (See `noEchoBack` option.) The default is `'*'`. If you want to show nothing, specify `''`. (But it's not user friendly.) +Set the mask character that is shown instead of the secret text (e.g. password). (See `noEchoBack` option.) The default is `'*'`. If you want to show nothing, specify `''`. (But it might be not user friendly in some cases.) *Note:* The some platforms might use `'*'` or `''` always. For example: diff --git a/lib/read.cs.js b/lib/read.cs.js index a8baa69..ce7226e 100644 --- a/lib/read.cs.js +++ b/lib/read.cs.js @@ -64,11 +64,12 @@ function getFso() { function readS() { var pw; + // Microsoft Windows PowerShell https://technet.microsoft.com/ja-jp/library/hh847837.aspx shellExec('powershell /?', function(exitCode, stdout, stderr, error) { if (error || exitCode !== 0) { pw = scriptPW(); } else { - shellExec('powershell -Command "$text = read-host -AsSecureString;' + + shellExec('powershell -Command "$text = Read-Host -AsSecureString;' + '$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' + '[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"', function(exitCode, stdout, stderr, error) { diff --git a/lib/readline-sync.js b/lib/readline-sync.js index 1f859de..0251cfa 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -155,9 +155,11 @@ function _readlineSync(options) { // options.display is string if ((chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '')) === '') { continue; } - if (!isEditable) { - displayInput = chunk.replace(/[\r\n]/g, ''); - if (options.noEchoBack) { displayInput = displayInput.replace(/./g, mask); } + if (!isEditable && (displayInput = chunk.replace(/[\r\n]/g, '')) !== '') { + if (options.noEchoBack) { + displayInput = mask === '' ? '' : + (new Array(displayInput.length + 1)).join(mask); + } if (displayInput !== '') { fs.writeSync(fdW, displayInput); } }