MCPcopy Create free account
hub / github.com/ProtonDriveApps/sdk / readPasswordLine

Function readPasswordLine

cli/src/cli/readPasswordLine.ts:6–75  ·  view source on GitHub ↗
(prompt: string)

Source from the content-addressed store, hash-verified

4 * Read one line from stdin without echoing (TTY). Non-TTY falls back to visible readline.
5 */
6export async function readPasswordLine(prompt: string): Promise<string> {
7 const stdin = process.stdin;
8 const stdout = process.stdout;
9
10 if (!stdin.isTTY || typeof stdin.setRawMode !== 'function') {
11 const answer = await question(prompt);
12 return (answer ?? '').trim();
13 }
14
15 return new Promise((resolve) => {
16 stdout.write(prompt);
17
18 const wasRaw = stdin.isRaw;
19 stdin.setRawMode(true);
20 stdin.resume();
21
22 let password = '';
23 let settled = false;
24
25 const cleanup = () => {
26 stdin.setRawMode(wasRaw);
27 stdin.removeListener('data', onData);
28 stdin.removeListener('end', onEnd);
29 };
30
31 const finish = (value: string) => {
32 if (settled) {
33 return;
34 }
35 settled = true;
36 cleanup();
37 stdout.write('\n');
38 resolve(value);
39 };
40
41 const onEnd = () => {
42 finish(password);
43 };
44
45 const onData = (chunk: Buffer | string) => {
46 const s = typeof chunk === 'string' ? chunk : chunk.toString('utf8');
47 for (const char of s) {
48 // Enter, new line
49 if (char === '\n' || char === '\r') {
50 finish(password);
51 return;
52 }
53 // Ctrl+D
54 if (char === '\u0004') {
55 finish(password);
56 return;
57 }
58 // Ctrl+C
59 if (char === '\u0003') {
60 cleanup();
61 process.exit(130);
62 }
63 // Backspace (delete 0x7F, backspace 0x08)

Callers

nothing calls this directly

Calls 4

questionFunction · 0.90
resumeMethod · 0.65
onMethod · 0.65
writeMethod · 0.45

Tested by

no test coverage detected