Files
bbs/internal/api/web/terminal.js
T
Claude f832430916 Add a web terminal page: the BBS in a browser
[http] serves /terminal (and / goes there): a page with xterm.js 5.5 and
its fit addon, built into the binary (MIT licensed, license included),
joined by a WebSocket (/terminal/ws, subprotocol bbs.terminal, binary
frames) to the text BBS, run in-process as for a telnet caller: a node
shown as "web terminal", the BBS's own login, ANSI and UTF-8, the size
the page fitted.

The page has a strict Content Security Policy (scripts only from the
BBS, no inline scripts, no framing); the WebSocket is taken only from
the BBS's own pages (or the cors origin), under the HTTP server's TLS,
connection and rate limits.  terminal = no turns it off.

The WebSocket's own stream closes the connection when a read deadline
passes, which the text BBS uses for idle time and notices: the line
adapter SSH sessions had (reads with deadlines that only end the read)
now serves both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-12 08:26:24 +00:00

50 lines
1.6 KiB
JavaScript

// The web terminal: xterm.js on the BBS's /terminal/ws, the caller's bytes
// both ways in binary frames, as a telnet client's would go.
"use strict";
(function () {
const status = document.getElementById("status");
const again = document.getElementById("again");
const term = new Terminal({
cursorBlink: true,
fontFamily: '"DejaVu Sans Mono", "Cascadia Mono", Consolas, "Liberation Mono", monospace',
fontSize: 16,
scrollback: 2000,
theme: { background: "#000000", foreground: "#c0c0c0" },
});
const fit = new FitAddon.FitAddon();
term.loadAddon(fit);
term.open(document.getElementById("term"));
window.addEventListener("resize", () => fit.fit());
const encoder = new TextEncoder();
let ws = null;
const send = (bytes) => {
if (ws && ws.readyState === WebSocket.OPEN) ws.send(bytes);
};
term.onData((d) => send(encoder.encode(d)));
term.onBinary((d) => send(Uint8Array.from(d, (c) => c.charCodeAt(0) & 0xff)));
function connect() {
fit.fit();
term.reset();
again.hidden = true;
status.textContent = "Connecting…";
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
const url = `${scheme}//${location.host}/terminal/ws?cols=${term.cols}&rows=${term.rows}`;
ws = new WebSocket(url, "bbs.terminal");
ws.binaryType = "arraybuffer";
ws.onopen = () => {
status.textContent = "Connected";
term.focus();
};
ws.onmessage = (e) => term.write(new Uint8Array(e.data));
ws.onclose = () => {
status.textContent = "Disconnected";
again.hidden = false;
ws = null;
};
}
again.addEventListener("click", connect);
connect();
})();