524 lines
14 KiB
JavaScript
524 lines
14 KiB
JavaScript
// In-browser PTY fixture. No command here is executed on the host computer.
|
|
export function createDemoPty({
|
|
write,
|
|
onClose,
|
|
host = "edge-01",
|
|
rows = 24,
|
|
cols = 80,
|
|
}) {
|
|
let line = "",
|
|
cursor = 0,
|
|
history = ["uptime", "ls", "systemctl status gateway"],
|
|
index = history.length,
|
|
kill = "",
|
|
closed = false;
|
|
let running = null,
|
|
timer = null,
|
|
pagerOffset = 0,
|
|
search = null,
|
|
suspended = false,
|
|
queued = "",
|
|
escapeBuffer = "",
|
|
paste = false,
|
|
pasteBuffer = "";
|
|
const prompt = () =>
|
|
`\x1b[38;5;37mdev@${host}\x1b[0m:\x1b[34m~/anidev\x1b[0m$ `;
|
|
const emit = (s) => {
|
|
if (suspended) queued += s;
|
|
else write(s);
|
|
};
|
|
let paintedCursor = 0;
|
|
const promptLength = () => `dev@${host}:~/anidev$ `.length;
|
|
function putPrompt() {
|
|
emit(prompt());
|
|
paintedCursor = promptLength();
|
|
}
|
|
function paint(text, length, offset) {
|
|
const oldRow = Math.floor(paintedCursor / Math.max(1, cols));
|
|
emit("\r" + (oldRow ? `\x1b[${oldRow}A` : "") + "\x1b[J" + text);
|
|
const endRow = Math.floor(length / Math.max(1, cols)),
|
|
targetRow = Math.floor(offset / Math.max(1, cols));
|
|
emit(length && length % cols === 0 ? "\r\n" : "\r");
|
|
if (endRow > targetRow) emit(`\x1b[${endRow - targetRow}A`);
|
|
if (offset % cols) emit(`\x1b[${offset % cols}C`);
|
|
paintedCursor = offset;
|
|
}
|
|
function redraw() {
|
|
paint(
|
|
prompt() + line,
|
|
promptLength() + line.length,
|
|
promptLength() + cursor,
|
|
);
|
|
}
|
|
function stop() {
|
|
clearInterval(timer);
|
|
timer = null;
|
|
if (running === "top" || running === "less") emit("\x1b[?1049l");
|
|
running = null;
|
|
}
|
|
function close() {
|
|
if (closed) return;
|
|
stop();
|
|
closed = true;
|
|
emit("\r\nConnection to " + host + " closed.\r\n");
|
|
onClose?.();
|
|
}
|
|
function historyMove(delta) {
|
|
index = Math.max(0, Math.min(history.length, index + delta));
|
|
line = history[index] || "";
|
|
cursor = line.length;
|
|
redraw();
|
|
}
|
|
function wordLeft() {
|
|
while (cursor && /\s/.test(line[cursor - 1])) cursor--;
|
|
while (cursor && !/\s/.test(line[cursor - 1])) cursor--;
|
|
}
|
|
function wordRight() {
|
|
while (cursor < line.length && !/\s/.test(line[cursor])) cursor++;
|
|
while (cursor < line.length && /\s/.test(line[cursor])) cursor++;
|
|
}
|
|
function remove(a, b) {
|
|
kill = line.slice(a, b);
|
|
line = line.slice(0, a) + line.slice(b);
|
|
cursor = a;
|
|
}
|
|
function insert(s) {
|
|
line = line.slice(0, cursor) + s + line.slice(cursor);
|
|
cursor += s.length;
|
|
redraw();
|
|
}
|
|
const commands = [
|
|
"help",
|
|
"ls",
|
|
"pwd",
|
|
"whoami",
|
|
"hostname",
|
|
"date",
|
|
"uptime",
|
|
"clear",
|
|
"echo",
|
|
"history",
|
|
"cat",
|
|
"top",
|
|
"less",
|
|
"tail",
|
|
"systemctl",
|
|
"exit",
|
|
];
|
|
const logLines = Array.from(
|
|
{ length: 160 },
|
|
(_, i) =>
|
|
`${String(i + 1).padStart(3)} 12:08:${String(i % 60).padStart(2, "0")} ${i % 17 === 0 ? "WARN " : "INFO "} gateway request_id=demo-${i + 1} status=${i % 17 === 0 ? "429" : "200"} duration=${12 + (i % 38)}ms`,
|
|
);
|
|
function pager() {
|
|
emit(
|
|
"\x1b[H\x1b[2J" +
|
|
logLines
|
|
.slice(pagerOffset, pagerOffset + Math.max(3, rows - 2))
|
|
.join("\r\n") +
|
|
`\r\n\x1b[7m demo gateway.log ${pagerOffset + 1}/${logLines.length} · arrows / PgUp / PgDn · q to exit \x1b[0m`,
|
|
);
|
|
}
|
|
function top() {
|
|
emit(
|
|
"\x1b[H\x1b[2Jtop — " +
|
|
host +
|
|
" demo PTY\r\nTasks: 5 total, 1 running CPU 4.2% Memory 184M / 2G\r\n\r\n\x1b[7m PID USER CPU% MEM% COMMAND \x1b[0m\r\n 842 anidev 2.4 1.8 gateway\r\n 901 anidev 1.1 2.3 identity\r\n 722 postgres 0.6 3.2 postgres\r\n 611 redis 0.1 0.4 redis-server\r\n\r\nq / Ctrl+C — exit · Ctrl+Z — suspend\r\n",
|
|
);
|
|
}
|
|
function execute() {
|
|
const input = line.trim();
|
|
cursor = line.length;
|
|
redraw();
|
|
emit("\r\n");
|
|
paintedCursor = 0;
|
|
line = "";
|
|
cursor = 0;
|
|
if (!input) {
|
|
putPrompt();
|
|
return;
|
|
}
|
|
history.push(input);
|
|
history = history.slice(-100);
|
|
index = history.length;
|
|
const [cmd, ...args] = input.split(/\s+/);
|
|
switch (cmd) {
|
|
case "help":
|
|
emit(
|
|
"Demo commands\r\n ls, pwd, whoami, hostname, date, uptime, echo, history\r\n systemctl status gateway · cat gateway.log · less gateway.log\r\n top · tail -f gateway.log · clear · exit\r\n\r\nEditing: arrows, Home/End, Ctrl+A/E/W/U/K/Y, Alt+B/F/D\r\nHistory: Up/Down, Ctrl+P/N/R · Tab completion\r\nSignals: Ctrl+C interrupt, Ctrl+D EOF, Ctrl+Z suspend\r\nClipboard: Ctrl+Shift+C / Ctrl+Shift+V\r\n",
|
|
);
|
|
break;
|
|
case "ls":
|
|
emit(
|
|
"\x1b[34mconfig/ deploy/ logs/\x1b[0m gateway.log README.md\r\n",
|
|
);
|
|
break;
|
|
case "pwd":
|
|
emit("/home/dev/anidev\r\n");
|
|
break;
|
|
case "whoami":
|
|
emit("dev\r\n");
|
|
break;
|
|
case "hostname":
|
|
emit(host + "\r\n");
|
|
break;
|
|
case "date":
|
|
emit(new Date().toISOString() + "\r\n");
|
|
break;
|
|
case "uptime":
|
|
emit(
|
|
"12:08:42 up 18 days, 4:32, 2 users, load average: 0.08, 0.12, 0.10\r\n",
|
|
);
|
|
break;
|
|
case "echo":
|
|
emit(args.join(" ") + "\r\n");
|
|
break;
|
|
case "history":
|
|
emit(
|
|
history
|
|
.map((s, i) => `${String(i + 1).padStart(3)} ${s}`)
|
|
.join("\r\n") + "\r\n",
|
|
);
|
|
break;
|
|
case "clear":
|
|
emit("\x1b[2J\x1b[H");
|
|
break;
|
|
case "systemctl":
|
|
emit(
|
|
"\x1b[32m●\x1b[0m gateway.service — Anidev Gateway\r\n Loaded: loaded; enabled\r\n Active: \x1b[32mactive (running)\x1b[0m\r\n Main PID: 842 (gateway)\r\n Memory: 38.2M\r\n",
|
|
);
|
|
break;
|
|
case "cat":
|
|
emit(
|
|
(args[0] === "gateway.log"
|
|
? logLines.slice(-12).join("\r\n")
|
|
: "# Anidev workspace\r\nLocal demonstration of a remote PTY.") +
|
|
"\r\n",
|
|
);
|
|
break;
|
|
case "top":
|
|
running = "top";
|
|
emit("\x1b[?1049h");
|
|
top();
|
|
timer = setInterval(top, 2000);
|
|
return;
|
|
case "less":
|
|
running = "less";
|
|
pagerOffset = 0;
|
|
emit("\x1b[?1049h");
|
|
pager();
|
|
return;
|
|
case "tail":
|
|
running = "tail";
|
|
emit(logLines.slice(-4).join("\r\n") + "\r\n");
|
|
let tick = 0;
|
|
timer = setInterval(
|
|
() =>
|
|
emit(
|
|
`12:09:${String(tick++ % 60).padStart(2, "0")} INFO gateway demo request status=200 duration=18ms\r\n`,
|
|
),
|
|
1100,
|
|
);
|
|
return;
|
|
case "exit":
|
|
close();
|
|
return;
|
|
default:
|
|
emit(`${cmd}: not available in this demo. Type help.\r\n`);
|
|
}
|
|
putPrompt();
|
|
}
|
|
function completion(reverse = false) {
|
|
const stem = line.slice(0, cursor),
|
|
matches = commands.filter((s) => s.startsWith(stem));
|
|
if (matches.length === 1) {
|
|
line = matches[0] + " " + line.slice(cursor);
|
|
cursor = matches[0].length + 1;
|
|
redraw();
|
|
} else if (matches.length > 1) {
|
|
cursor = line.length;
|
|
redraw();
|
|
emit(
|
|
"\r\n" +
|
|
(reverse ? [...matches].reverse() : matches).join(" ") +
|
|
"\r\n",
|
|
);
|
|
paintedCursor = 0;
|
|
redraw();
|
|
}
|
|
}
|
|
function sequence(s) {
|
|
if (running === "less") {
|
|
if (["\x1b[B", "\x1b[6~"].includes(s))
|
|
pagerOffset = Math.min(
|
|
logLines.length - 3,
|
|
pagerOffset + (s === "\x1b[B" ? 1 : rows - 3),
|
|
);
|
|
else if (["\x1b[A", "\x1b[5~"].includes(s))
|
|
pagerOffset = Math.max(
|
|
0,
|
|
pagerOffset - (s === "\x1b[A" ? 1 : rows - 3),
|
|
);
|
|
else if (["\x1b[H", "\x1b[1~"].includes(s)) pagerOffset = 0;
|
|
else if (["\x1b[F", "\x1b[4~"].includes(s))
|
|
pagerOffset = Math.max(0, logLines.length - rows + 2);
|
|
pager();
|
|
return;
|
|
}
|
|
const before = cursor;
|
|
switch (s) {
|
|
case "\x1b[A":
|
|
historyMove(-1);
|
|
return;
|
|
case "\x1b[B":
|
|
historyMove(1);
|
|
return;
|
|
case "\x1b[D":
|
|
cursor = Math.max(0, cursor - 1);
|
|
break;
|
|
case "\x1b[C":
|
|
cursor = Math.min(line.length, cursor + 1);
|
|
break;
|
|
case "\x1b[H":
|
|
case "\x1bOH":
|
|
case "\x1b[1~":
|
|
cursor = 0;
|
|
break;
|
|
case "\x1b[F":
|
|
case "\x1bOF":
|
|
case "\x1b[4~":
|
|
cursor = line.length;
|
|
break;
|
|
case "\x1b[3~":
|
|
line = line.slice(0, cursor) + line.slice(cursor + 1);
|
|
break;
|
|
case "\x1bb":
|
|
case "\x1b[1;5D":
|
|
wordLeft();
|
|
break;
|
|
case "\x1bf":
|
|
case "\x1b[1;5C":
|
|
wordRight();
|
|
break;
|
|
case "\x1bd":
|
|
wordRight();
|
|
remove(before, cursor);
|
|
break;
|
|
case "\x1b\x7f":
|
|
wordLeft();
|
|
remove(cursor, before);
|
|
break;
|
|
case "\x1b[Z":
|
|
completion(true);
|
|
return;
|
|
}
|
|
redraw();
|
|
}
|
|
function char(c) {
|
|
if (c === "\x11") {
|
|
suspended = false;
|
|
write(queued);
|
|
queued = "";
|
|
return;
|
|
}
|
|
if (c === "\x13") {
|
|
suspended = true;
|
|
return;
|
|
}
|
|
if (c === "\x03" || c === "\x1a") {
|
|
suspended = false;
|
|
queued = "";
|
|
const active = running;
|
|
stop();
|
|
line = "";
|
|
cursor = 0;
|
|
search = null;
|
|
emit(
|
|
c === "\x03"
|
|
? "^C\r\n"
|
|
: `^Z\r\n${active ? "[1]+ Stopped (demo)\r\n" : ""}`,
|
|
);
|
|
putPrompt();
|
|
return;
|
|
}
|
|
if (running) {
|
|
if (c === "q" || c === "\x1b") {
|
|
stop();
|
|
putPrompt();
|
|
} else if (running === "less") {
|
|
if (c === "j" || c === " ")
|
|
pagerOffset = Math.min(
|
|
logLines.length - 3,
|
|
pagerOffset + (c === " " ? rows - 3 : 1),
|
|
);
|
|
if (c === "k") pagerOffset = Math.max(0, pagerOffset - 1);
|
|
pager();
|
|
}
|
|
return;
|
|
}
|
|
if (search !== null) {
|
|
if (c === "\r") {
|
|
line = [...history].reverse().find((s) => s.includes(search)) || "";
|
|
cursor = line.length;
|
|
search = null;
|
|
redraw();
|
|
return;
|
|
}
|
|
if (c === "\x1b") {
|
|
search = null;
|
|
redraw();
|
|
return;
|
|
}
|
|
if (c === "\x7f") search = search.slice(0, -1);
|
|
else if (c >= " ") search += c;
|
|
const match =
|
|
[...history].reverse().find((s) => s.includes(search)) || "";
|
|
emit(`\r\x1b[2K(reverse-i-search) '${search}': ${match}`);
|
|
return;
|
|
}
|
|
const before = cursor;
|
|
switch (c) {
|
|
case "\r":
|
|
case "\n":
|
|
execute();
|
|
return;
|
|
case "\t":
|
|
completion();
|
|
return;
|
|
case "\x7f":
|
|
case "\b":
|
|
if (cursor) {
|
|
line = line.slice(0, cursor - 1) + line.slice(cursor);
|
|
cursor--;
|
|
}
|
|
break;
|
|
case "\x01":
|
|
cursor = 0;
|
|
break;
|
|
case "\x05":
|
|
cursor = line.length;
|
|
break;
|
|
case "\x02":
|
|
cursor = Math.max(0, cursor - 1);
|
|
break;
|
|
case "\x06":
|
|
cursor = Math.min(line.length, cursor + 1);
|
|
break;
|
|
case "\x04":
|
|
if (!line) {
|
|
close();
|
|
return;
|
|
}
|
|
line = line.slice(0, cursor) + line.slice(cursor + 1);
|
|
break;
|
|
case "\x0b":
|
|
remove(cursor, line.length);
|
|
break;
|
|
case "\x15":
|
|
remove(0, cursor);
|
|
break;
|
|
case "\x17":
|
|
wordLeft();
|
|
remove(cursor, before);
|
|
break;
|
|
case "\x19":
|
|
insert(kill);
|
|
return;
|
|
case "\x0c":
|
|
emit("\x1b[2J\x1b[H");
|
|
paintedCursor = 0;
|
|
break;
|
|
case "\x10":
|
|
historyMove(-1);
|
|
return;
|
|
case "\x0e":
|
|
historyMove(1);
|
|
return;
|
|
case "\x12":
|
|
search = "";
|
|
emit("\r\x1b[2K(reverse-i-search) '': ");
|
|
return;
|
|
case "\x14":
|
|
if (cursor > 0 && line.length > 1) {
|
|
const at = Math.min(cursor, line.length - 1);
|
|
line =
|
|
line.slice(0, at - 1) +
|
|
line[at] +
|
|
line[at - 1] +
|
|
line.slice(at + 1);
|
|
cursor = Math.min(line.length, at + 1);
|
|
}
|
|
break;
|
|
default:
|
|
if (c >= " ") {
|
|
insert(c);
|
|
return;
|
|
}
|
|
}
|
|
redraw();
|
|
}
|
|
function send(data) {
|
|
if (closed) return;
|
|
// A bracketed paste is inserted as text; pasted newlines never auto-execute commands.
|
|
if (paste) {
|
|
pasteBuffer += data;
|
|
const end = pasteBuffer.indexOf("\x1b[201~");
|
|
if (end < 0) return;
|
|
const rest = pasteBuffer.slice(end + 6),
|
|
text = pasteBuffer
|
|
.slice(0, end)
|
|
.replace(/[\r\n]+/g, " ")
|
|
.replace(/[\x00-\x1f\x7f]/g, "");
|
|
paste = false;
|
|
pasteBuffer = "";
|
|
insert(text);
|
|
if (rest) send(rest);
|
|
return;
|
|
}
|
|
const start = data.indexOf("\x1b[200~");
|
|
if (start >= 0) {
|
|
if (start) send(data.slice(0, start));
|
|
paste = true;
|
|
pasteBuffer = "";
|
|
send(data.slice(start + 6));
|
|
return;
|
|
}
|
|
data = escapeBuffer + data;
|
|
escapeBuffer = "";
|
|
for (let i = 0; i < data.length;) {
|
|
if (data[i] === "\x1b" && i + 1 < data.length) {
|
|
const match = data
|
|
.slice(i)
|
|
.match(/^\x1b(?:\[[0-9;?]*[A-Za-z~]|O[A-Za-z]|[^\[O])/);
|
|
if (match) {
|
|
sequence(match[0]);
|
|
i += match[0].length;
|
|
continue;
|
|
}
|
|
escapeBuffer = data.slice(i);
|
|
break;
|
|
}
|
|
char(data[i++]);
|
|
}
|
|
}
|
|
emit(
|
|
"\x1b[?2004h\x1b[36manidev / SSH workspace\x1b[0m\r\nUbuntu 24.04 LTS · xterm-256color · demo PTY\r\nType \x1b[36mhelp\x1b[0m, try \x1b[36mtop\x1b[0m or \x1b[36mless gateway.log\x1b[0m.\r\n\r\n" +
|
|
prompt(),
|
|
);
|
|
paintedCursor = promptLength();
|
|
return {
|
|
send,
|
|
resize(c, r) {
|
|
cols = c;
|
|
rows = r;
|
|
if (running === "less") pager();
|
|
if (running === "top") top();
|
|
},
|
|
close,
|
|
dispose() {
|
|
clearInterval(timer);
|
|
closed = true;
|
|
},
|
|
};
|
|
}
|