-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathdemo.js
More file actions
629 lines (542 loc) · 19.1 KB
/
Copy pathdemo.js
File metadata and controls
629 lines (542 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/usr/bin/env node
/**
* @ghostty-web/demo - Cross-platform demo server
*
* Starts a local HTTP server with WebSocket PTY support.
* Run with: npx @ghostty-web/demo
*/
import fs from 'fs';
import http from 'http';
import { homedir } from 'os';
import path from 'path';
import { fileURLToPath } from 'url';
// Node-pty for cross-platform PTY support. The 1.2.0-beta.x line adds a
// `pixelSize` argument to resize(), which sets ws_xpixel / ws_ypixel in
// the slave PTY's winsize struct so kitty kittens (icat etc.) can detect
// graphics support via TIOCGWINSZ instead of falling back to terminal
// queries. Lydell's fork is based on 1.1.0-beta14 (pre-pixelSize), so we
// use upstream's beta directly.
import pty from 'node-pty';
// WebSocket server
import { WebSocketServer } from 'ws';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DEV_MODE = process.argv.includes('--dev');
const HTTP_PORT = process.env.PORT || (DEV_MODE ? 8000 : 8080);
// ============================================================================
// Locate ghostty-web assets
// ============================================================================
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
function findGhosttyWeb() {
// In dev mode, we use Vite - no need to find built assets
if (DEV_MODE) {
const repoRoot = path.join(__dirname, '..', '..');
const wasmPath = path.join(repoRoot, 'ghostty-vt.wasm');
if (!fs.existsSync(wasmPath)) {
console.error('Error: ghostty-vt.wasm not found.');
console.error('Run: bun run build:wasm');
process.exit(1);
}
return { distPath: null, wasmPath, repoRoot };
}
// First, check for local development (repo root dist/)
const localDist = path.join(__dirname, '..', '..', 'dist');
const localJs = path.join(localDist, 'ghostty-web.js');
const localWasm = path.join(__dirname, '..', '..', 'ghostty-vt.wasm');
if (fs.existsSync(localJs) && fs.existsSync(localWasm)) {
return { distPath: localDist, wasmPath: localWasm, repoRoot: path.join(__dirname, '..', '..') };
}
// Use require.resolve to find the installed ghostty-web package
try {
const ghosttyWebMain = require.resolve('ghostty-web');
// Strip dist/... from path to get package root (regex already gives us the root)
const ghosttyWebRoot = ghosttyWebMain.replace(/[/\\]dist[/\\].*$/, '');
const distPath = path.join(ghosttyWebRoot, 'dist');
const wasmPath = path.join(ghosttyWebRoot, 'ghostty-vt.wasm');
if (fs.existsSync(path.join(distPath, 'ghostty-web.js')) && fs.existsSync(wasmPath)) {
return { distPath, wasmPath, repoRoot: null };
}
} catch (e) {
// require.resolve failed, package not found
}
console.error('Error: Could not find ghostty-web package.');
console.error('');
console.error('If developing locally, run: bun run build');
console.error('If using npx, the package should install automatically.');
process.exit(1);
}
const { distPath, wasmPath, repoRoot } = findGhosttyWeb();
// ============================================================================
// HTML Template
// ============================================================================
const HTML_TEMPLATE = `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ghostty-web</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 40px 20px;
}
.terminal-window {
width: 100%;
max-width: 1000px;
background: #1e1e1e;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
overflow: hidden;
}
.title-bar {
background: #2d2d2d;
padding: 12px 16px;
display: flex;
align-items: center;
gap: 12px;
border-bottom: 1px solid #1a1a1a;
}
.traffic-lights {
display: flex;
gap: 8px;
}
.light {
width: 12px;
height: 12px;
border-radius: 50%;
}
.light.red { background: #ff5f56; }
.light.yellow { background: #ffbd2e; }
.light.green { background: #27c93f; }
.title {
color: #e5e5e5;
font-size: 13px;
font-weight: 500;
letter-spacing: 0.3px;
}
.connection-status {
margin-left: auto;
font-size: 11px;
color: #888;
display: flex;
align-items: center;
gap: 6px;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #888;
}
.status-dot.connected { background: #27c93f; }
.status-dot.disconnected { background: #ff5f56; }
.status-dot.connecting { background: #ffbd2e; animation: pulse 1s infinite; }
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.terminal-content {
height: 600px;
padding: 16px;
background: #1e1e1e;
position: relative;
overflow: hidden;
}
/* Ensure terminal canvas can handle scrolling */
.terminal-content canvas {
display: block;
}
@media (max-width: 768px) {
.terminal-content {
height: 500px;
}
}
</style>
</head>
<body>
<div class="terminal-window">
<div class="title-bar">
<div class="traffic-lights">
<div class="light red"></div>
<div class="light yellow"></div>
<div class="light green"></div>
</div>
<span class="title">ghostty-web</span>
<div class="connection-status">
<div class="status-dot connecting" id="status-dot"></div>
<span id="status-text">Connecting...</span>
</div>
</div>
<div class="terminal-content" id="terminal"></div>
</div>
<script type="module">
import { init, Terminal, FitAddon } from '/dist/ghostty-web.js';
await init();
const term = new Terminal({
cols: 80,
rows: 24,
fontFamily: 'JetBrains Mono, Menlo, Monaco, monospace',
fontSize: 14,
theme: {
background: '#1e1e1e',
foreground: '#d4d4d4',
},
});
const fitAddon = new FitAddon();
term.loadAddon(fitAddon);
const container = document.getElementById('terminal');
await term.open(container);
fitAddon.fit();
fitAddon.observeResize(); // Auto-fit when container resizes
// Status elements
const statusDot = document.getElementById('status-dot');
const statusText = document.getElementById('status-text');
function setStatus(status, text) {
statusDot.className = 'status-dot ' + status;
statusText.textContent = text;
}
// Connect to WebSocket PTY server (use same origin as HTTP server)
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = protocol + '//' + window.location.host + '/ws?cols=' + term.cols + '&rows=' + term.rows;
let ws;
// Read total canvas pixel dims (CSS pixels). The server stuffs these
// into ws_xpixel / ws_ypixel via node-pty's resize(cols, rows, pixelSize)
// so kittens like icat see non-zero TIOCGWINSZ pixel fields.
function getPixelSize() {
const canvas = container.querySelector('canvas');
return canvas
? { xpixel: canvas.clientWidth, ypixel: canvas.clientHeight }
: { xpixel: 0, ypixel: 0 };
}
function connect() {
setStatus('connecting', 'Connecting...');
ws = new WebSocket(wsUrl);
ws.onopen = () => {
setStatus('connected', 'Connected');
// Push initial pixel dims so TIOCGWINSZ-gated tools see them
// before the first resize event.
const px = getPixelSize();
ws.send(JSON.stringify({
type: 'resize',
cols: term.cols,
rows: term.rows,
xpixel: px.xpixel,
ypixel: px.ypixel,
}));
};
ws.onmessage = (event) => {
term.write(event.data);
};
ws.onclose = () => {
setStatus('disconnected', 'Disconnected');
term.write('\\r\\n\\x1b[31mConnection closed. Reconnecting in 2s...\\x1b[0m\\r\\n');
setTimeout(connect, 2000);
};
ws.onerror = () => {
setStatus('disconnected', 'Error');
};
}
connect();
// Send terminal input to server
term.onData((data) => {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(data);
}
});
// Handle resize - notify PTY when terminal dimensions change
term.onResize(({ cols, rows }) => {
if (ws && ws.readyState === WebSocket.OPEN) {
const px = getPixelSize();
ws.send(JSON.stringify({
type: 'resize',
cols,
rows,
xpixel: px.xpixel,
ypixel: px.ypixel,
}));
}
});
// Also handle window resize (for browsers that don't trigger ResizeObserver on window resize)
window.addEventListener('resize', () => {
fitAddon.fit();
});
// Handle mobile keyboard showing/hiding using visualViewport API
if (window.visualViewport) {
const terminalContent = document.querySelector('.terminal-content');
const terminalWindow = document.querySelector('.terminal-window');
const originalHeight = terminalContent.style.height;
const body = document.body;
window.visualViewport.addEventListener('resize', () => {
const keyboardHeight = window.innerHeight - window.visualViewport.height;
if (keyboardHeight > 100) {
body.style.padding = '0';
body.style.alignItems = 'flex-start';
terminalWindow.style.borderRadius = '0';
terminalWindow.style.maxWidth = '100%';
terminalContent.style.height = (window.visualViewport.height - 60) + 'px';
window.scrollTo(0, 0);
} else {
body.style.padding = '40px 20px';
body.style.alignItems = 'center';
terminalWindow.style.borderRadius = '12px';
terminalWindow.style.maxWidth = '1000px';
terminalContent.style.height = originalHeight || '600px';
}
fitAddon.fit();
});
}
</script>
</body>
</html>`;
// ============================================================================
// MIME Types
// ============================================================================
const MIME_TYPES = {
'.html': 'text/html',
'.js': 'application/javascript',
'.mjs': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.wasm': 'application/wasm',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
// ============================================================================
// HTTP Server
// ============================================================================
const httpServer = http.createServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const pathname = url.pathname;
// Serve index page
if (pathname === '/' || pathname === '/index.html') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(HTML_TEMPLATE);
return;
}
// Serve dist files
if (pathname.startsWith('/dist/')) {
const filePath = path.join(distPath, pathname.slice(6));
serveFile(filePath, res);
return;
}
// Serve WASM file
if (pathname === '/ghostty-vt.wasm') {
serveFile(wasmPath, res);
return;
}
// 404
res.writeHead(404);
res.end('Not Found');
});
function serveFile(filePath, res) {
const ext = path.extname(filePath);
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not Found');
return;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
}
// ============================================================================
// WebSocket Server (using ws package)
// ============================================================================
const sessions = new Map();
function getShell() {
if (process.platform === 'win32') {
return process.env.COMSPEC || 'cmd.exe';
}
return process.env.SHELL || '/bin/bash';
}
function createPtySession(cols, rows) {
const shell = getShell();
const shellArgs = process.platform === 'win32' ? [] : [];
const ptyProcess = pty.spawn(shell, shellArgs, {
name: 'xterm-256color',
cols: cols,
rows: rows,
cwd: homedir(),
env: {
...process.env,
TERM: 'xterm-256color',
COLORTERM: 'truecolor',
},
});
return ptyProcess;
}
// WebSocket server attached to HTTP server (same port)
const wss = new WebSocketServer({ noServer: true });
// Handle HTTP upgrade for WebSocket connections
httpServer.on('upgrade', (req, socket, head) => {
const url = new URL(req.url, `http://${req.headers.host}`);
if (url.pathname === '/ws') {
// In production, consider validating req.headers.origin to prevent CSRF
// For development/demo purposes, we allow all origins
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
} else {
socket.destroy();
}
});
wss.on('connection', (ws, req) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const cols = Number.parseInt(url.searchParams.get('cols') || '80');
const rows = Number.parseInt(url.searchParams.get('rows') || '24');
// Create PTY
const ptyProcess = createPtySession(cols, rows);
sessions.set(ws, { pty: ptyProcess });
// PTY -> WebSocket
ptyProcess.onData((data) => {
if (ws.readyState === ws.OPEN) {
ws.send(data);
}
});
ptyProcess.onExit(({ exitCode }) => {
if (ws.readyState === ws.OPEN) {
ws.send(`\r\n\x1b[33mShell exited (code: ${exitCode})\x1b[0m\r\n`);
ws.close();
}
});
// WebSocket -> PTY
ws.on('message', (data) => {
const message = data.toString('utf8');
// Check for resize message
if (message.startsWith('{')) {
try {
const msg = JSON.parse(message);
if (msg.type === 'resize') {
// node-pty 1.2.0+ accepts a third pixelSize arg that sets
// ws_xpixel / ws_ypixel in the PTY winsize struct. Without it,
// kitty kittens (icat, etc.) read zeros via TIOCGWINSZ and
// refuse to render images.
if (msg.xpixel > 0 && msg.ypixel > 0) {
ptyProcess.resize(msg.cols, msg.rows, {
width: msg.xpixel,
height: msg.ypixel,
});
} else {
ptyProcess.resize(msg.cols, msg.rows);
}
return;
}
} catch (e) {
// Not JSON, treat as input
}
}
// Send to PTY
ptyProcess.write(message);
});
ws.on('close', () => {
const session = sessions.get(ws);
if (session) {
session.pty.kill();
sessions.delete(ws);
}
});
ws.on('error', () => {
// Ignore socket errors (connection reset, etc.)
});
// Send welcome message
const C = '\x1b[1;36m'; // Cyan
const G = '\x1b[1;32m'; // Green
const Y = '\x1b[1;33m'; // Yellow
const R = '\x1b[0m'; // Reset
ws.send(`${C}╔══════════════════════════════════════════════════════════════╗${R}\r\n`);
ws.send(
`${C}║${R} ${G}Welcome to ghostty-web!${R} ${C}║${R}\r\n`
);
ws.send(`${C}║${R} ${C}║${R}\r\n`);
ws.send(`${C}║${R} You have a real shell session with full PTY support. ${C}║${R}\r\n`);
ws.send(
`${C}║${R} Try: ${Y}ls${R}, ${Y}cd${R}, ${Y}top${R}, ${Y}vim${R}, or any command! ${C}║${R}\r\n`
);
ws.send(`${C}╚══════════════════════════════════════════════════════════════╝${R}\r\n\r\n`);
});
// ============================================================================
// Startup
// ============================================================================
function printBanner(url) {
console.log('\n' + '═'.repeat(60));
console.log(' 🚀 ghostty-web demo server' + (DEV_MODE ? ' (dev mode)' : ''));
console.log('═'.repeat(60));
console.log(`\n 📺 Open: ${url}`);
console.log(` 📡 WebSocket PTY: same endpoint /ws`);
console.log(` 🐚 Shell: ${getShell()}`);
console.log(` 📁 Home: ${homedir()}`);
if (DEV_MODE) {
console.log(` 🔥 Hot reload enabled via Vite`);
} else if (repoRoot) {
console.log(` 📦 Using local build: ${distPath}`);
}
console.log('\n ⚠️ This server provides shell access.');
console.log(' Only use for local development.\n');
console.log('═'.repeat(60));
console.log(' Press Ctrl+C to stop.\n');
}
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n\nShutting down...');
for (const [ws, session] of sessions.entries()) {
session.pty.kill();
ws.close();
}
wss.close();
process.exit(0);
});
// Start HTTP/Vite server
if (DEV_MODE) {
// Dev mode: use Vite for hot reload
const { createServer } = await import('vite');
const vite = await createServer({
root: repoRoot,
server: {
port: HTTP_PORT,
strictPort: true,
},
});
await vite.listen();
// Attach WebSocket handler AFTER Vite has fully initialized
// Use prependListener (not prependOnceListener) so it runs for every request
// This ensures our handler runs BEFORE Vite's handlers
if (vite.httpServer) {
vite.httpServer.prependListener('upgrade', (req, socket, head) => {
const pathname = req.url?.split('?')[0] || req.url || '';
// ONLY handle /ws - everything else passes through unchanged to Vite
if (pathname === '/ws') {
if (!socket.destroyed && !socket.readableEnded) {
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
}
// Stop here - we handled it, socket is consumed
// Don't call other listeners
return;
}
// For non-/ws paths, explicitly do nothing and let the event propagate
// The key is: don't return, don't touch the socket, just let it pass through
// Vite's handlers (which were added before ours via prependListener) will process it
});
}
printBanner(`http://localhost:${HTTP_PORT}/demo/`);
} else {
// Production mode: static file server
httpServer.listen(HTTP_PORT, () => {
printBanner(`http://localhost:${HTTP_PORT}`);
});
}