X-Git-Url: https://git.squeep.com/?p=squeep-authentication-module;a=blobdiff_plain;f=lib%2Fstdio-credential.js;fp=lib%2Fstdio-credential.js;h=5e7632e9b7f116f2dcd6d74f56be51b685de21d7;hp=0000000000000000000000000000000000000000;hb=53ef948ea83106e82d55e60d6695a15e94bf725e;hpb=842a3da269de1ab82e9a2a12aae8ed5677f064d8 diff --git a/lib/stdio-credential.js b/lib/stdio-credential.js new file mode 100644 index 0000000..5e7632e --- /dev/null +++ b/lib/stdio-credential.js @@ -0,0 +1,36 @@ +'use strict'; + +const readline = require('node:readline'); +const stream = require('node:stream'); + +/** + * Read a credential from stdin in a silent manner. + * @param {String} prompt + * @returns {Promise} + */ +async function stdioCredential(prompt) { + const input = process.stdin; + const output = new stream.Writable({ + write: function (chunk, encoding, callback) { + if (!this.muted) { + process.stdout.write(chunk, encoding); + } + callback(); + }, + }); + const rl = readline.createInterface({ input, output, terminal: !!process.stdin.isTTY }); + rl.setPrompt(prompt); + rl.prompt(); + output.muted = true; + + return new Promise((resolve) => { + rl.question('', (answer) => { + output.muted = false; + rl.close(); + output.write('\n'); + resolve(answer); + }); + }); +} + +module.exports = stdioCredential; \ No newline at end of file