Add frontend app with terminal UI, sandbox integration, and asset handling.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
91049379f7
commit
b841c2d1f6
19 files changed
+800
-13
No files matched your search
@@ -1,7 +1,8 @@
|
||||
import { createServer, type IncomingMessage, type Server } from 'node:http';
|
||||
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
||||
import type { Duplex } from 'node:stream';
|
||||
import { InvalidMessageError, parseClientMessage, TERMINAL_PATH } from '@homepage/protocol/terminal';
|
||||
import { WebSocketServer, type RawData, type WebSocket } from 'ws';
|
||||
import type { Asset } from './assets.ts';
|
||||
import type { BackendConfig } from './config.ts';
|
||||
import { Session, type Spawner } from './session.ts';
|
||||
|
||||
@@ -14,6 +15,29 @@ const PING_INTERVAL_MS = 30_000;
|
||||
/** Close code for protocol violations by the client. */
|
||||
const POLICY_VIOLATION = 1008;
|
||||
|
||||
// xterm.js creates style elements at runtime, so styles need 'unsafe-inline'.
|
||||
const SECURITY_HEADERS = {
|
||||
'Content-Security-Policy': [
|
||||
"default-src 'self'",
|
||||
"script-src 'self'",
|
||||
"style-src 'self' 'unsafe-inline'",
|
||||
"img-src 'self' data:",
|
||||
"connect-src 'self'",
|
||||
"frame-ancestors 'none'",
|
||||
"base-uri 'none'",
|
||||
"form-action 'none'",
|
||||
].join('; '),
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
'Referrer-Policy': 'no-referrer',
|
||||
'Permissions-Policy': 'camera=(), microphone=(), geolocation=()',
|
||||
};
|
||||
|
||||
function send(response: ServerResponse, status: number, type: string, body: string | Buffer, headers: Record<string, string> = {}, head = false): void
|
||||
{
|
||||
response.writeHead(status, { ...SECURITY_HEADERS, 'Content-Type': type, 'Content-Length': Buffer.byteLength(body), ...headers });
|
||||
response.end(head ? undefined : body);
|
||||
}
|
||||
|
||||
function reject(socket: Duplex, status: number, reason: string): void
|
||||
{
|
||||
socket.end(`HTTP/1.1 ${status} ${reason}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
|
||||
@@ -87,17 +111,24 @@ function handleConnection(socket: WebSocket, spawner: Spawner, config: BackendCo
|
||||
});
|
||||
}
|
||||
|
||||
export function createBackend(config: BackendConfig, spawner: Spawner, log: Logger): Server
|
||||
export function createBackend(config: BackendConfig, spawner: Spawner, log: Logger, assets = new Map<string, Asset>()): Server
|
||||
{
|
||||
const server = createServer((request, response) =>
|
||||
{
|
||||
if (request.method === 'GET' && request.url === '/healthz')
|
||||
const head = request.method === 'HEAD';
|
||||
if (request.method !== 'GET' && !head) return send(response, 405, 'text/plain', 'method not allowed', { Allow: 'GET, HEAD' });
|
||||
|
||||
const path = new URL(request.url ?? '/', 'http://backend').pathname;
|
||||
if (path === '/healthz') return send(response, 200, 'text/plain', 'ok', {}, head);
|
||||
|
||||
const asset = assets.get(path);
|
||||
if (asset)
|
||||
{
|
||||
response.writeHead(200, { 'Content-Type': 'text/plain' }).end('ok');
|
||||
return;
|
||||
const cache = asset.immutable ? 'public, max-age=31536000, immutable' : 'no-cache';
|
||||
return send(response, 200, asset.type, asset.body, { 'Cache-Control': cache }, head);
|
||||
}
|
||||
|
||||
response.writeHead(404, { 'Content-Type': 'text/plain' }).end('not found');
|
||||
send(response, 404, 'text/plain', 'not found', {}, head);
|
||||
});
|
||||
|
||||
const sockets = new WebSocketServer({ noServer: true, maxPayload: MAX_FRAME_BYTES });
|
||||
|
||||
Reference in new issue
Block a user