postforme-terminal 1.0.0

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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,177 @@
1
+ #!/usr/bin/env node
2
+ import { WebSocketServer, WebSocket } from 'ws';
3
+ import * as pty from 'node-pty';
4
+ import { homedir } from 'os';
5
+ import { createServer } from 'http';
6
+ const PORT = parseInt(process.env.TERMINAL_PORT || '5101', 10);
7
+ const CWD = process.cwd();
8
+ const MAX_BUFFER = 100_000;
9
+ const state = {
10
+ process: null,
11
+ buffer: '',
12
+ clients: new Set(),
13
+ };
14
+ function broadcast(msg) {
15
+ const payload = JSON.stringify(msg);
16
+ for (const client of state.clients) {
17
+ if (client.readyState === WebSocket.OPEN) {
18
+ client.send(payload);
19
+ }
20
+ }
21
+ }
22
+ function spawnClaude() {
23
+ if (state.process) {
24
+ broadcast({ type: 'error', message: 'Claude Code is already running' });
25
+ return;
26
+ }
27
+ state.buffer = '';
28
+ const shell = process.env.SHELL || '/bin/zsh';
29
+ console.log(`[postforme] Starting Claude Code in ${CWD}`);
30
+ try {
31
+ const proc = pty.spawn(shell, [], {
32
+ name: 'xterm-256color',
33
+ cols: 120,
34
+ rows: 30,
35
+ cwd: CWD,
36
+ env: {
37
+ ...process.env,
38
+ TERM: 'xterm-256color',
39
+ HOME: homedir(),
40
+ PATH: `${homedir()}/.local/bin:/opt/homebrew/bin:${process.env.PATH || ''}`,
41
+ },
42
+ });
43
+ setTimeout(() => {
44
+ proc.write('claude\r');
45
+ }, 500);
46
+ proc.onData((data) => {
47
+ state.buffer += data;
48
+ if (state.buffer.length > MAX_BUFFER) {
49
+ state.buffer = state.buffer.slice(-MAX_BUFFER);
50
+ }
51
+ broadcast({ type: 'data', data });
52
+ });
53
+ proc.onExit(({ exitCode }) => {
54
+ console.log(`[postforme] Claude Code exited (code ${exitCode})`);
55
+ state.process = null;
56
+ broadcast({ type: 'exit', code: exitCode });
57
+ broadcast({ type: 'status', running: false });
58
+ });
59
+ state.process = proc;
60
+ broadcast({ type: 'status', running: true });
61
+ console.log(`[postforme] Claude Code started (pid: ${proc.pid})`);
62
+ }
63
+ catch (err) {
64
+ const message = err instanceof Error ? err.message : String(err);
65
+ console.error(`[postforme] Failed to start: ${message}`);
66
+ broadcast({ type: 'error', message: `Failed to start Claude Code: ${message}` });
67
+ }
68
+ }
69
+ function stopClaude() {
70
+ if (!state.process) {
71
+ broadcast({ type: 'error', message: 'No process running' });
72
+ return;
73
+ }
74
+ console.log('[postforme] Stopping Claude Code...');
75
+ state.process.kill();
76
+ state.process = null;
77
+ state.buffer = '';
78
+ broadcast({ type: 'status', running: false });
79
+ }
80
+ // --- HTTP handler (health check + CORS) ---
81
+ function handleHttp(_req, res) {
82
+ const origin = _req.headers.origin || '';
83
+ const allowed = [
84
+ 'http://localhost:5100',
85
+ 'http://localhost:3000',
86
+ 'https://postforme.ca',
87
+ ];
88
+ if (allowed.includes(origin)) {
89
+ res.setHeader('Access-Control-Allow-Origin', origin);
90
+ }
91
+ res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
92
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
93
+ if (_req.method === 'OPTIONS') {
94
+ res.writeHead(204);
95
+ res.end();
96
+ return;
97
+ }
98
+ if (_req.url === '/status') {
99
+ res.writeHead(200, { 'Content-Type': 'application/json' });
100
+ res.end(JSON.stringify({ ok: true, claudeCode: state.process !== null }));
101
+ return;
102
+ }
103
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
104
+ res.end('PostForMe Terminal Server');
105
+ }
106
+ // --- WebSocket Server ---
107
+ const httpServer = createServer(handleHttp);
108
+ const wss = new WebSocketServer({ server: httpServer });
109
+ wss.on('connection', (ws) => {
110
+ state.clients.add(ws);
111
+ if (state.buffer) {
112
+ ws.send(JSON.stringify({ type: 'data', data: state.buffer }));
113
+ }
114
+ ws.send(JSON.stringify({ type: 'status', running: state.process !== null }));
115
+ ws.on('message', (raw) => {
116
+ try {
117
+ const msg = JSON.parse(raw.toString());
118
+ switch (msg.type) {
119
+ case 'data':
120
+ if (state.process && msg.data) {
121
+ state.process.write(msg.data);
122
+ }
123
+ break;
124
+ case 'resize':
125
+ if (state.process && msg.cols && msg.rows) {
126
+ state.process.resize(msg.cols, msg.rows);
127
+ }
128
+ break;
129
+ case 'start':
130
+ spawnClaude();
131
+ break;
132
+ case 'stop':
133
+ stopClaude();
134
+ break;
135
+ case 'status':
136
+ ws.send(JSON.stringify({ type: 'status', running: state.process !== null }));
137
+ break;
138
+ }
139
+ }
140
+ catch (err) {
141
+ console.error('[postforme] Bad message:', err);
142
+ }
143
+ });
144
+ ws.on('close', () => {
145
+ state.clients.delete(ws);
146
+ });
147
+ ws.on('error', (err) => {
148
+ console.error('[postforme] WebSocket error:', err.message);
149
+ state.clients.delete(ws);
150
+ });
151
+ });
152
+ // --- Startup ---
153
+ httpServer.listen(PORT, () => {
154
+ console.log('');
155
+ console.log(' PostForMe Terminal Server running');
156
+ console.log(` → ws://localhost:${PORT}`);
157
+ console.log('');
158
+ console.log(' Open postforme.ca and click the Terminal button.');
159
+ console.log(' Press Ctrl+C to stop.');
160
+ console.log('');
161
+ });
162
+ // --- Graceful shutdown ---
163
+ function shutdown(signal) {
164
+ console.log(`\n[postforme] ${signal} — shutting down...`);
165
+ if (state.process) {
166
+ state.process.kill();
167
+ state.process = null;
168
+ }
169
+ for (const client of state.clients) {
170
+ client.close();
171
+ }
172
+ state.clients.clear();
173
+ httpServer.close(() => process.exit(0));
174
+ setTimeout(() => process.exit(1), 3000);
175
+ }
176
+ process.on('SIGTERM', () => shutdown('SIGTERM'));
177
+ process.on('SIGINT', () => shutdown('SIGINT'));
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "postforme-terminal",
3
+ "version": "1.0.0",
4
+ "description": "PostForMe terminal server — connects your browser to Claude Code",
5
+ "type": "module",
6
+ "bin": {
7
+ "postforme-terminal": "dist/index.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "start": "node dist/index.js"
15
+ },
16
+ "dependencies": {
17
+ "node-pty": "^1.2.0-beta.10",
18
+ "ws": "^8.18.0"
19
+ },
20
+ "devDependencies": {
21
+ "@types/ws": "^8.5.0",
22
+ "@types/node": "^20.0.0",
23
+ "typescript": "^5.0.0"
24
+ },
25
+ "engines": {
26
+ "node": ">=18"
27
+ },
28
+ "keywords": [
29
+ "postforme",
30
+ "terminal",
31
+ "claude",
32
+ "claude-code",
33
+ "mcp"
34
+ ],
35
+ "license": "MIT",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/postforme/terminal.git"
39
+ }
40
+ }