@seflless/ghosttown 1.0.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Coder
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # ghost-town
2
+
3
+ Ghost town let's you continue terminal sessions from other devices using a browser. The runtime is based on Ghostty with a recreated UI that should feel like using Ghostty for desktop.
4
+
5
+ We're building this to make it easy to build web apps using coding agents, while on the go.
6
+
7
+ # Credits
8
+
9
+ - [Ghostty](https://ghostty.org/)
10
+ - [ghostty-web](https://github.com/coder/ghostty-web)
@@ -0,0 +1,496 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * ghosttown CLI - Web-based terminal emulator
5
+ *
6
+ * Starts a local HTTP server with WebSocket PTY support.
7
+ * Run with: npx @seflless/ghosttown
8
+ */
9
+
10
+ import fs from 'fs';
11
+ import http from 'http';
12
+ import { homedir, networkInterfaces } from 'os';
13
+ import path from 'path';
14
+ import { fileURLToPath } from 'url';
15
+
16
+ // Node-pty for cross-platform PTY support
17
+ import pty from '@lydell/node-pty';
18
+ // WebSocket server
19
+ import { WebSocketServer } from 'ws';
20
+
21
+ const __filename = fileURLToPath(import.meta.url);
22
+ const __dirname = path.dirname(__filename);
23
+
24
+ const HTTP_PORT = process.env.PORT || 8080;
25
+
26
+ // ============================================================================
27
+ // Locate ghosttown assets
28
+ // ============================================================================
29
+
30
+ function findAssets() {
31
+ // Assets are in the package root (one level up from bin/)
32
+ const packageRoot = path.join(__dirname, '..');
33
+ const distPath = path.join(packageRoot, 'dist');
34
+ const wasmPath = path.join(packageRoot, 'ghostty-vt.wasm');
35
+
36
+ if (!fs.existsSync(path.join(distPath, 'ghostty-web.js'))) {
37
+ console.error('Error: dist/ghostty-web.js not found.');
38
+ console.error('The package may not have been built correctly.');
39
+ process.exit(1);
40
+ }
41
+
42
+ if (!fs.existsSync(wasmPath)) {
43
+ console.error('Error: ghostty-vt.wasm not found.');
44
+ console.error('The package may not have been built correctly.');
45
+ process.exit(1);
46
+ }
47
+
48
+ return { distPath, wasmPath };
49
+ }
50
+
51
+ const { distPath, wasmPath } = findAssets();
52
+
53
+ // ============================================================================
54
+ // HTML Template
55
+ // ============================================================================
56
+
57
+ const HTML_TEMPLATE = `<!doctype html>
58
+ <html lang="en">
59
+ <head>
60
+ <meta charset="UTF-8" />
61
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
62
+ <title>ghosttown</title>
63
+ <style>
64
+ * {
65
+ margin: 0;
66
+ padding: 0;
67
+ box-sizing: border-box;
68
+ }
69
+
70
+ body {
71
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
72
+ background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
73
+ min-height: 100vh;
74
+ display: flex;
75
+ align-items: center;
76
+ justify-content: center;
77
+ padding: 40px 20px;
78
+ }
79
+
80
+ .terminal-window {
81
+ width: 100%;
82
+ max-width: 1000px;
83
+ background: #1e1e1e;
84
+ border-radius: 12px;
85
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
86
+ overflow: hidden;
87
+ }
88
+
89
+ .title-bar {
90
+ background: #2d2d2d;
91
+ padding: 12px 16px;
92
+ display: flex;
93
+ align-items: center;
94
+ gap: 12px;
95
+ border-bottom: 1px solid #1a1a1a;
96
+ }
97
+
98
+ .traffic-lights {
99
+ display: flex;
100
+ gap: 8px;
101
+ }
102
+
103
+ .light {
104
+ width: 12px;
105
+ height: 12px;
106
+ border-radius: 50%;
107
+ }
108
+
109
+ .light.red { background: #ff5f56; }
110
+ .light.yellow { background: #ffbd2e; }
111
+ .light.green { background: #27c93f; }
112
+
113
+ .title {
114
+ color: #e5e5e5;
115
+ font-size: 13px;
116
+ font-weight: 500;
117
+ letter-spacing: 0.3px;
118
+ }
119
+
120
+ .connection-status {
121
+ margin-left: auto;
122
+ font-size: 11px;
123
+ color: #888;
124
+ display: flex;
125
+ align-items: center;
126
+ gap: 6px;
127
+ }
128
+
129
+ .status-dot {
130
+ width: 8px;
131
+ height: 8px;
132
+ border-radius: 50%;
133
+ background: #888;
134
+ }
135
+
136
+ .status-dot.connected { background: #27c93f; }
137
+ .status-dot.disconnected { background: #ff5f56; }
138
+ .status-dot.connecting { background: #ffbd2e; animation: pulse 1s infinite; }
139
+
140
+ @keyframes pulse {
141
+ 0%, 100% { opacity: 1; }
142
+ 50% { opacity: 0.5; }
143
+ }
144
+
145
+ .terminal-content {
146
+ height: 600px;
147
+ padding: 16px;
148
+ background: #1e1e1e;
149
+ position: relative;
150
+ overflow: hidden;
151
+ }
152
+
153
+ .terminal-content canvas {
154
+ display: block;
155
+ }
156
+
157
+ @media (max-width: 768px) {
158
+ .terminal-content {
159
+ height: 500px;
160
+ }
161
+ }
162
+ </style>
163
+ </head>
164
+ <body>
165
+ <div class="terminal-window">
166
+ <div class="title-bar">
167
+ <div class="traffic-lights">
168
+ <div class="light red"></div>
169
+ <div class="light yellow"></div>
170
+ <div class="light green"></div>
171
+ </div>
172
+ <span class="title">ghosttown</span>
173
+ <div class="connection-status">
174
+ <div class="status-dot connecting" id="status-dot"></div>
175
+ <span id="status-text">Connecting...</span>
176
+ </div>
177
+ </div>
178
+ <div class="terminal-content" id="terminal"></div>
179
+ </div>
180
+
181
+ <script type="module">
182
+ import { init, Terminal, FitAddon } from '/dist/ghostty-web.js';
183
+
184
+ await init();
185
+ const term = new Terminal({
186
+ cols: 80,
187
+ rows: 24,
188
+ fontFamily: 'JetBrains Mono, Menlo, Monaco, monospace',
189
+ fontSize: 14,
190
+ theme: {
191
+ background: '#1e1e1e',
192
+ foreground: '#d4d4d4',
193
+ },
194
+ });
195
+
196
+ const fitAddon = new FitAddon();
197
+ term.loadAddon(fitAddon);
198
+
199
+ const container = document.getElementById('terminal');
200
+ await term.open(container);
201
+ fitAddon.fit();
202
+ fitAddon.observeResize();
203
+
204
+ const statusDot = document.getElementById('status-dot');
205
+ const statusText = document.getElementById('status-text');
206
+
207
+ function setStatus(status, text) {
208
+ statusDot.className = 'status-dot ' + status;
209
+ statusText.textContent = text;
210
+ }
211
+
212
+ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
213
+ const wsUrl = protocol + '//' + window.location.host + '/ws?cols=' + term.cols + '&rows=' + term.rows;
214
+ let ws;
215
+
216
+ function connect() {
217
+ setStatus('connecting', 'Connecting...');
218
+ ws = new WebSocket(wsUrl);
219
+
220
+ ws.onopen = () => {
221
+ setStatus('connected', 'Connected');
222
+ };
223
+
224
+ ws.onmessage = (event) => {
225
+ term.write(event.data);
226
+ };
227
+
228
+ ws.onclose = () => {
229
+ setStatus('disconnected', 'Disconnected');
230
+ term.write('\\r\\n\\x1b[31mConnection closed. Reconnecting in 2s...\\x1b[0m\\r\\n');
231
+ setTimeout(connect, 2000);
232
+ };
233
+
234
+ ws.onerror = () => {
235
+ setStatus('disconnected', 'Error');
236
+ };
237
+ }
238
+
239
+ connect();
240
+
241
+ term.onData((data) => {
242
+ if (ws && ws.readyState === WebSocket.OPEN) {
243
+ ws.send(data);
244
+ }
245
+ });
246
+
247
+ term.onResize(({ cols, rows }) => {
248
+ if (ws && ws.readyState === WebSocket.OPEN) {
249
+ ws.send(JSON.stringify({ type: 'resize', cols, rows }));
250
+ }
251
+ });
252
+
253
+ window.addEventListener('resize', () => {
254
+ fitAddon.fit();
255
+ });
256
+
257
+ if (window.visualViewport) {
258
+ const terminalContent = document.querySelector('.terminal-content');
259
+ const terminalWindow = document.querySelector('.terminal-window');
260
+ const originalHeight = terminalContent.style.height;
261
+ const body = document.body;
262
+
263
+ window.visualViewport.addEventListener('resize', () => {
264
+ const keyboardHeight = window.innerHeight - window.visualViewport.height;
265
+ if (keyboardHeight > 100) {
266
+ body.style.padding = '0';
267
+ body.style.alignItems = 'flex-start';
268
+ terminalWindow.style.borderRadius = '0';
269
+ terminalWindow.style.maxWidth = '100%';
270
+ terminalContent.style.height = (window.visualViewport.height - 60) + 'px';
271
+ window.scrollTo(0, 0);
272
+ } else {
273
+ body.style.padding = '40px 20px';
274
+ body.style.alignItems = 'center';
275
+ terminalWindow.style.borderRadius = '12px';
276
+ terminalWindow.style.maxWidth = '1000px';
277
+ terminalContent.style.height = originalHeight || '600px';
278
+ }
279
+ fitAddon.fit();
280
+ });
281
+ }
282
+ </script>
283
+ </body>
284
+ </html>`;
285
+
286
+ // ============================================================================
287
+ // MIME Types
288
+ // ============================================================================
289
+
290
+ const MIME_TYPES = {
291
+ '.html': 'text/html',
292
+ '.js': 'application/javascript',
293
+ '.mjs': 'application/javascript',
294
+ '.css': 'text/css',
295
+ '.json': 'application/json',
296
+ '.wasm': 'application/wasm',
297
+ '.png': 'image/png',
298
+ '.svg': 'image/svg+xml',
299
+ '.ico': 'image/x-icon',
300
+ };
301
+
302
+ // ============================================================================
303
+ // HTTP Server
304
+ // ============================================================================
305
+
306
+ const httpServer = http.createServer((req, res) => {
307
+ const url = new URL(req.url, `http://${req.headers.host}`);
308
+ const pathname = url.pathname;
309
+
310
+ // Serve index page
311
+ if (pathname === '/' || pathname === '/index.html') {
312
+ res.writeHead(200, { 'Content-Type': 'text/html' });
313
+ res.end(HTML_TEMPLATE);
314
+ return;
315
+ }
316
+
317
+ // Serve dist files
318
+ if (pathname.startsWith('/dist/')) {
319
+ const filePath = path.join(distPath, pathname.slice(6));
320
+ serveFile(filePath, res);
321
+ return;
322
+ }
323
+
324
+ // Serve WASM file
325
+ if (pathname === '/ghostty-vt.wasm') {
326
+ serveFile(wasmPath, res);
327
+ return;
328
+ }
329
+
330
+ // 404
331
+ res.writeHead(404);
332
+ res.end('Not Found');
333
+ });
334
+
335
+ function serveFile(filePath, res) {
336
+ const ext = path.extname(filePath);
337
+ const contentType = MIME_TYPES[ext] || 'application/octet-stream';
338
+
339
+ fs.readFile(filePath, (err, data) => {
340
+ if (err) {
341
+ res.writeHead(404);
342
+ res.end('Not Found');
343
+ return;
344
+ }
345
+ res.writeHead(200, { 'Content-Type': contentType });
346
+ res.end(data);
347
+ });
348
+ }
349
+
350
+ // ============================================================================
351
+ // WebSocket Server
352
+ // ============================================================================
353
+
354
+ const sessions = new Map();
355
+
356
+ function getShell() {
357
+ if (process.platform === 'win32') {
358
+ return process.env.COMSPEC || 'cmd.exe';
359
+ }
360
+ return process.env.SHELL || '/bin/bash';
361
+ }
362
+
363
+ function createPtySession(cols, rows) {
364
+ const shell = getShell();
365
+
366
+ const ptyProcess = pty.spawn(shell, [], {
367
+ name: 'xterm-256color',
368
+ cols: cols,
369
+ rows: rows,
370
+ cwd: homedir(),
371
+ env: {
372
+ ...process.env,
373
+ TERM: 'xterm-256color',
374
+ COLORTERM: 'truecolor',
375
+ },
376
+ });
377
+
378
+ return ptyProcess;
379
+ }
380
+
381
+ const wss = new WebSocketServer({ noServer: true });
382
+
383
+ httpServer.on('upgrade', (req, socket, head) => {
384
+ const url = new URL(req.url, `http://${req.headers.host}`);
385
+
386
+ if (url.pathname === '/ws') {
387
+ wss.handleUpgrade(req, socket, head, (ws) => {
388
+ wss.emit('connection', ws, req);
389
+ });
390
+ } else {
391
+ socket.destroy();
392
+ }
393
+ });
394
+
395
+ wss.on('connection', (ws, req) => {
396
+ const url = new URL(req.url, `http://${req.headers.host}`);
397
+ const cols = Number.parseInt(url.searchParams.get('cols') || '80');
398
+ const rows = Number.parseInt(url.searchParams.get('rows') || '24');
399
+
400
+ const ptyProcess = createPtySession(cols, rows);
401
+ sessions.set(ws, { pty: ptyProcess });
402
+
403
+ ptyProcess.onData((data) => {
404
+ if (ws.readyState === ws.OPEN) {
405
+ ws.send(data);
406
+ }
407
+ });
408
+
409
+ ptyProcess.onExit(({ exitCode }) => {
410
+ if (ws.readyState === ws.OPEN) {
411
+ ws.send(`\r\n\x1b[33mShell exited (code: ${exitCode})\x1b[0m\r\n`);
412
+ ws.close();
413
+ }
414
+ });
415
+
416
+ ws.on('message', (data) => {
417
+ const message = data.toString('utf8');
418
+
419
+ if (message.startsWith('{')) {
420
+ try {
421
+ const msg = JSON.parse(message);
422
+ if (msg.type === 'resize') {
423
+ ptyProcess.resize(msg.cols, msg.rows);
424
+ return;
425
+ }
426
+ } catch (e) {
427
+ // Not JSON, treat as input
428
+ }
429
+ }
430
+
431
+ ptyProcess.write(message);
432
+ });
433
+
434
+ ws.on('close', () => {
435
+ const session = sessions.get(ws);
436
+ if (session) {
437
+ session.pty.kill();
438
+ sessions.delete(ws);
439
+ }
440
+ });
441
+
442
+ ws.on('error', () => {
443
+ // Ignore socket errors
444
+ });
445
+ });
446
+
447
+ // ============================================================================
448
+ // Startup
449
+ // ============================================================================
450
+
451
+ function getLocalIPs() {
452
+ const interfaces = networkInterfaces();
453
+ const ips = [];
454
+ for (const name of Object.keys(interfaces)) {
455
+ for (const iface of interfaces[name] || []) {
456
+ if (iface.family === 'IPv4' && !iface.internal) {
457
+ ips.push(iface.address);
458
+ }
459
+ }
460
+ }
461
+ return ips;
462
+ }
463
+
464
+ function printBanner(url) {
465
+ const localIPs = getLocalIPs();
466
+ console.log('\n' + '═'.repeat(50));
467
+ console.log(' 👻 ghosttown');
468
+ console.log('═'.repeat(50));
469
+ console.log(`\n Open: ${url}`);
470
+ if (localIPs.length > 0) {
471
+ console.log(` Network:`);
472
+ for (const ip of localIPs) {
473
+ console.log(` http://${ip}:${HTTP_PORT}`);
474
+ }
475
+ }
476
+ console.log(`\n Shell: ${getShell()}`);
477
+ console.log(` Home: ${homedir()}`);
478
+ console.log('\n Warning: This server provides shell access.');
479
+ console.log(' Only use for local development.\n');
480
+ console.log('═'.repeat(50));
481
+ console.log(' Press Ctrl+C to stop.\n');
482
+ }
483
+
484
+ process.on('SIGINT', () => {
485
+ console.log('\n\nShutting down...');
486
+ for (const [ws, session] of sessions.entries()) {
487
+ session.pty.kill();
488
+ ws.close();
489
+ }
490
+ wss.close();
491
+ process.exit(0);
492
+ });
493
+
494
+ httpServer.listen(HTTP_PORT, '0.0.0.0', () => {
495
+ printBanner(`http://localhost:${HTTP_PORT}`);
496
+ });
@@ -0,0 +1,4 @@
1
+ const e = {};
2
+ export {
3
+ e as default
4
+ };
Binary file