add account settings page, rest of otp support, stdio credential helper, other misc
[squeep-authentication-module] / lib / stdio-credential.js
1 'use strict';
2
3 const readline = require('node:readline');
4 const stream = require('node:stream');
5
6 /**
7 * Read a credential from stdin in a silent manner.
8 * @param {String} prompt
9 * @returns {Promise<String>}
10 */
11 async function stdioCredential(prompt) {
12 const input = process.stdin;
13 const output = new stream.Writable({
14 write: function (chunk, encoding, callback) {
15 if (!this.muted) {
16 process.stdout.write(chunk, encoding);
17 }
18 callback();
19 },
20 });
21 const rl = readline.createInterface({ input, output, terminal: !!process.stdin.isTTY });
22 rl.setPrompt(prompt);
23 rl.prompt();
24 output.muted = true;
25
26 return new Promise((resolve) => {
27 rl.question('', (answer) => {
28 output.muted = false;
29 rl.close();
30 output.write('\n');
31 resolve(answer);
32 });
33 });
34 }
35
36 module.exports = stdioCredential;