fix: Removes extra pulling in readFromKeyboard.
This commit is contained in:
parent
32b77b12a2
commit
f7a6a4356d
1 changed files with 21 additions and 27 deletions
|
@ -1,37 +1,36 @@
|
||||||
import type { ServerChannel } from "ssh2";
|
import type { ServerChannel } from "ssh2";
|
||||||
|
|
||||||
|
const pullRate = process.env.KEYBOARD_PULLING_RATE ? parseInt(process.env.KEYBOARD_PULLING_RATE) : 5;
|
||||||
|
|
||||||
|
const leftEscape = "\x1B[D";
|
||||||
|
const rightEscape = "\x1B[C";
|
||||||
|
|
||||||
|
const ourBackspace = "\u0008";
|
||||||
|
const clientBackspace = "\x7F";
|
||||||
|
|
||||||
export async function readFromKeyboard(
|
export async function readFromKeyboard(
|
||||||
stream: ServerChannel,
|
stream: ServerChannel,
|
||||||
disableEcho: boolean = false,
|
disableEcho: boolean = false,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const leftEscape = "\x1B[D";
|
let promise: (value: string | PromiseLike<string>) => void;
|
||||||
const rightEscape = "\x1B[C";
|
|
||||||
|
|
||||||
const ourBackspace = "\u0008";
|
|
||||||
|
|
||||||
// \x7F = Ascii escape code for backspace (client side)
|
|
||||||
|
|
||||||
let line = "";
|
let line = "";
|
||||||
let lineIndex = 0;
|
let lineIndex = 0;
|
||||||
let isReady = false;
|
|
||||||
|
|
||||||
async function eventLoop(): Promise<any> {
|
async function eventLoop(): Promise<any> {
|
||||||
const readStreamData = stream.read();
|
const readStreamData = stream.read();
|
||||||
if (readStreamData == null) return setTimeout(eventLoop, 5);
|
if (readStreamData == null) return setTimeout(eventLoop, pullRate);
|
||||||
|
|
||||||
if (readStreamData.includes("\r") || readStreamData.includes("\n")) {
|
if (readStreamData.includes("\r") || readStreamData.includes("\n")) {
|
||||||
line = line.replace("\r", "");
|
return promise(line.replace("\r", ""));
|
||||||
isReady = true;
|
} else if (readStreamData.includes(clientBackspace)) {
|
||||||
|
if (line.length == 0) return setTimeout(eventLoop, pullRate); // Here because if we do it in the parent if statement, shit breaks
|
||||||
return;
|
|
||||||
} else if (readStreamData.includes("\x7F")) {
|
|
||||||
if (line.length == 0) return setTimeout(eventLoop, 5); // Here because if we do it in the parent if statement, shit breaks
|
|
||||||
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
|
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
|
||||||
|
|
||||||
if (!disableEcho) {
|
if (!disableEcho) {
|
||||||
const deltaCursor = line.length - lineIndex;
|
const deltaCursor = line.length - lineIndex;
|
||||||
|
|
||||||
if (deltaCursor == line.length) return setTimeout(eventLoop, 5);
|
if (deltaCursor == line.length) return setTimeout(eventLoop, pullRate);
|
||||||
|
|
||||||
if (deltaCursor < 0) {
|
if (deltaCursor < 0) {
|
||||||
// Use old technique if the delta is < 0, as the new one is tailored to the start + 1 to end - 1
|
// Use old technique if the delta is < 0, as the new one is tailored to the start + 1 to end - 1
|
||||||
|
@ -52,13 +51,13 @@ export async function readFromKeyboard(
|
||||||
}
|
}
|
||||||
} else if (readStreamData.includes("\x1B")) {
|
} else if (readStreamData.includes("\x1B")) {
|
||||||
if (readStreamData.includes(rightEscape)) {
|
if (readStreamData.includes(rightEscape)) {
|
||||||
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5);
|
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, pullRate);
|
||||||
lineIndex += 1;
|
lineIndex += 1;
|
||||||
} else if (readStreamData.includes(leftEscape)) {
|
} else if (readStreamData.includes(leftEscape)) {
|
||||||
if (lineIndex - 1 < 0) return setTimeout(eventLoop, 5);
|
if (lineIndex - 1 < 0) return setTimeout(eventLoop, pullRate);
|
||||||
lineIndex -= 1;
|
lineIndex -= 1;
|
||||||
} else {
|
} else {
|
||||||
return setTimeout(eventLoop, 5);
|
return setTimeout(eventLoop, pullRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!disableEcho) stream.write(readStreamData);
|
if (!disableEcho) stream.write(readStreamData);
|
||||||
|
@ -88,17 +87,12 @@ export async function readFromKeyboard(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(eventLoop, 5);
|
setTimeout(eventLoop, pullRate);
|
||||||
}
|
};
|
||||||
|
|
||||||
// Yes, this is bad practice. Currently, I don't care.
|
// Yes, this is bad practice. Currently, I don't care.
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
eventLoop();
|
setTimeout(eventLoop, pullRate);
|
||||||
|
promise = resolve;
|
||||||
while (!isReady) {
|
|
||||||
await new Promise(i => setTimeout(i, 5));
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(line);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue