add account settings page, rest of otp support, stdio credential helper, other misc
[squeep-authentication-module] / lib / stdio-credential.js
diff --git a/lib/stdio-credential.js b/lib/stdio-credential.js
new file mode 100644 (file)
index 0000000..5e7632e
--- /dev/null
@@ -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<String>}
+ */
+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