Skip to content

Commit

Permalink
fix: input based on read library
Browse files Browse the repository at this point in the history
  • Loading branch information
en9inerd committed Feb 19, 2024
1 parent ef6dd40 commit 5533da7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 68 deletions.
11 changes: 1 addition & 10 deletions src/clients/user.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@ export class TelegramUserClient extends TelegramBotClient {
password: async () => await input.password('Enter your password: '),
phoneCode: async () => await input.text('Enter the code sent to your phone: '),
onError: async (err) => {
const errMessage = formatErrorMessage(err);

if (errMessage === 'Input closed unexpectedly') {
process.stdout.write('\n');
}

input.close();
throw new TelegramClientException(errMessage);
throw new TelegramClientException(formatErrorMessage(err));
}
});

input.close();

// Save the session if it doesn't exist
if (!Store.get(this.sessionName, '')) {
Store.set(this.sessionName, this.session.save());
Expand Down
69 changes: 11 additions & 58 deletions src/helpers/input.helper.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,21 @@
import readline from 'readline';
import { read } from 'read';
import { HelperException } from '../exceptions.js';

class InputHelper {
private rl: readline.Interface;

constructor() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}

public async text(question: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.rl.question(question, (answer: string) => {
resolve(answer);
});
this.rl.on('close', () => {
reject(new HelperException('Input closed unexpectedly'));
});
});
try {
return await read({ prompt: question });
} catch (error) {
throw new HelperException(`Error reading text: ${(<Error>error).message}`);
}
}

public async password(question: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
const stdin = process.openStdin();
let password = '';

const onKeyPress = (char: string) => {
char = char.toString().trim();
switch (char) {
case '\n':
case '\r':
case '\u0004':
cleanUp();
resolve(password);
break;
default:
process.stdout.write('\u001B[2K\u001B[200D' + question + Array(password.length + 1).join('*'));
password += char;
break;
}
};

const cleanUp = () => {
stdin.removeListener('data', onKeyPress);
process.stdin.removeListener('keypress', onKeyPress);
stdin.pause();
this.rl.removeListener('close', onClose);
};

const onClose = () => {
cleanUp();
reject(new HelperException('Input closed unexpectedly'));
};

stdin.on('data', onKeyPress);
process.stdin.on('keypress', onKeyPress);
this.rl.on('close', onClose);
});
}

public close(): void {
this.rl.close();
try {
return await read({ prompt: question, silent: true });
} catch (error) {
throw new HelperException(`Error reading password: ${(<Error>error).message}`);
}
}
}

Expand Down

0 comments on commit 5533da7

Please sign in to comment.