pty-wrap 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.
- package/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +92 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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,44 @@
|
|
|
1
|
+
# pty-wrap
|
|
2
|
+
|
|
3
|
+
PTY wrapper for capturing terminal sessions via MQTT.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g pty-wrap
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# Capture any command
|
|
15
|
+
pty-wrap <command> [args...]
|
|
16
|
+
|
|
17
|
+
# Examples
|
|
18
|
+
pty-wrap claude
|
|
19
|
+
pty-wrap bash
|
|
20
|
+
pty-wrap vim file.txt
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Environment Variables
|
|
24
|
+
|
|
25
|
+
| Variable | Default | Description |
|
|
26
|
+
|----------|---------|-------------|
|
|
27
|
+
| `MQTT_URL` | `mqtt://localhost:1883` | MQTT broker URL |
|
|
28
|
+
| `SESSION_ID` | Auto-generated UUID | Custom session identifier |
|
|
29
|
+
|
|
30
|
+
## How It Works
|
|
31
|
+
|
|
32
|
+
1. Spawns the target command in a PTY (pseudo-terminal)
|
|
33
|
+
2. Captures all input/output streams
|
|
34
|
+
3. Publishes to MQTT topic `sessions/{SESSION_ID}/stream`
|
|
35
|
+
4. Transparently proxies terminal I/O
|
|
36
|
+
|
|
37
|
+
## Requirements
|
|
38
|
+
|
|
39
|
+
- Node.js >= 18.0.0
|
|
40
|
+
- MQTT broker (e.g., EMQX, Mosquitto)
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as pty from 'node-pty';
|
|
3
|
+
import mqtt from 'mqtt';
|
|
4
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import { execSync } from 'child_process';
|
|
7
|
+
const MQTT_URL = process.env.MQTT_URL || 'mqtt://localhost:1883';
|
|
8
|
+
const SESSION_ID = process.env.SESSION_ID || uuidv4();
|
|
9
|
+
const SHELL = process.env.SHELL || (os.platform() === 'win32' ? 'powershell.exe' : 'bash');
|
|
10
|
+
const COMMAND = process.argv.slice(2);
|
|
11
|
+
function resolveCommand(cmd) {
|
|
12
|
+
try {
|
|
13
|
+
const command = os.platform() === 'win32' ? `where ${cmd}` : `which ${cmd}`;
|
|
14
|
+
const result = execSync(command, { encoding: 'utf-8' }).trim();
|
|
15
|
+
return result.split('\n')[0].trim();
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return cmd;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
let seq = 0;
|
|
22
|
+
const topic = `sessions/${SESSION_ID}/stream`;
|
|
23
|
+
const client = mqtt.connect(MQTT_URL, {
|
|
24
|
+
clientId: `pty-wrapper-${SESSION_ID}`,
|
|
25
|
+
clean: false,
|
|
26
|
+
});
|
|
27
|
+
function publish(stream, payload) {
|
|
28
|
+
const msg = {
|
|
29
|
+
session_id: SESSION_ID,
|
|
30
|
+
seq: seq++,
|
|
31
|
+
ts: new Date().toISOString(),
|
|
32
|
+
stream,
|
|
33
|
+
payload,
|
|
34
|
+
encoding: 'utf-8',
|
|
35
|
+
};
|
|
36
|
+
client.publish(topic, JSON.stringify(msg), { qos: 1 });
|
|
37
|
+
}
|
|
38
|
+
client.on('connect', () => {
|
|
39
|
+
console.error(`[pty-wrapper] Connected to MQTT, session: ${SESSION_ID}`);
|
|
40
|
+
publish('event', JSON.stringify({
|
|
41
|
+
type: 'session_start',
|
|
42
|
+
cwd: process.cwd(),
|
|
43
|
+
host: os.hostname(),
|
|
44
|
+
shell: SHELL,
|
|
45
|
+
command: COMMAND.length > 0 ? COMMAND : undefined,
|
|
46
|
+
}));
|
|
47
|
+
const cols = process.stdout.columns || 80;
|
|
48
|
+
const rows = process.stdout.rows || 24;
|
|
49
|
+
const cmd = COMMAND.length > 0 ? resolveCommand(COMMAND[0]) : SHELL;
|
|
50
|
+
const args = COMMAND.length > 1 ? COMMAND.slice(1) : [];
|
|
51
|
+
const ptyProcess = pty.spawn(cmd, args, {
|
|
52
|
+
name: 'xterm-256color',
|
|
53
|
+
cols,
|
|
54
|
+
rows,
|
|
55
|
+
cwd: process.cwd(),
|
|
56
|
+
env: process.env,
|
|
57
|
+
});
|
|
58
|
+
ptyProcess.onData((data) => {
|
|
59
|
+
publish('output', data);
|
|
60
|
+
process.stdout.write(data);
|
|
61
|
+
});
|
|
62
|
+
if (process.stdin.isTTY) {
|
|
63
|
+
process.stdin.setRawMode(true);
|
|
64
|
+
}
|
|
65
|
+
process.stdin.resume();
|
|
66
|
+
process.stdin.on('data', (data) => {
|
|
67
|
+
const str = data.toString();
|
|
68
|
+
publish('input', str);
|
|
69
|
+
ptyProcess.write(str);
|
|
70
|
+
});
|
|
71
|
+
process.stdout.on('resize', () => {
|
|
72
|
+
const newCols = process.stdout.columns || 80;
|
|
73
|
+
const newRows = process.stdout.rows || 24;
|
|
74
|
+
ptyProcess.resize(newCols, newRows);
|
|
75
|
+
publish('event', JSON.stringify({ type: 'resize', cols: newCols, rows: newRows }));
|
|
76
|
+
});
|
|
77
|
+
ptyProcess.onExit(({ exitCode }) => {
|
|
78
|
+
publish('event', JSON.stringify({ type: 'session_end', exit_code: exitCode }));
|
|
79
|
+
setTimeout(() => {
|
|
80
|
+
client.end();
|
|
81
|
+
process.exit(exitCode);
|
|
82
|
+
}, 500);
|
|
83
|
+
});
|
|
84
|
+
process.on('SIGINT', () => ptyProcess.write('\x03'));
|
|
85
|
+
process.on('SIGTERM', () => {
|
|
86
|
+
ptyProcess.kill();
|
|
87
|
+
client.end();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
client.on('error', (err) => {
|
|
91
|
+
console.error('[pty-wrapper] MQTT error:', err.message);
|
|
92
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pty-wrap",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PTY wrapper for capturing terminal sessions via MQTT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"pty-wrap": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"start": "node --loader ts-node/esm src/index.ts",
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"dev": "node --loader ts-node/esm --watch src/index.ts",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"keywords": ["pty", "terminal", "session", "capture", "mqtt", "cli"],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18.0.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"mqtt": "^5.3.0",
|
|
28
|
+
"node-pty": "^1.0.0",
|
|
29
|
+
"uuid": "^9.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^20.10.0",
|
|
33
|
+
"@types/uuid": "^9.0.0",
|
|
34
|
+
"ts-node": "^10.9.2",
|
|
35
|
+
"typescript": "^5.3.0"
|
|
36
|
+
}
|
|
37
|
+
}
|