devbonzai 2.2.303 → 2.2.304

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/cli.js CHANGED
@@ -109,6 +109,8 @@ async function main() {
109
109
  packageJson.dependencies.express = "^4.18.2";
110
110
  packageJson.dependencies.cors = "^2.8.5";
111
111
  packageJson.dependencies["@babel/parser"] = "^7.23.0";
112
+ packageJson.dependencies["node-pty"] = "^1.0.0";
113
+ packageJson.dependencies.ws = "^8.16.0";
112
114
 
113
115
  // Add script to run receiver
114
116
  if (!packageJson.scripts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devbonzai",
3
- "version": "2.2.303",
3
+ "version": "2.2.304",
4
4
  "description": "Quickly set up a local file server in any repository for browser-based file access",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -24,6 +24,8 @@
24
24
  "dependencies": {
25
25
  "express": "^4.18.2",
26
26
  "cors": "^2.8.5",
27
- "@babel/parser": "^7.23.0"
27
+ "@babel/parser": "^7.23.0",
28
+ "node-pty": "^1.0.0",
29
+ "ws": "^8.16.0"
28
30
  }
29
31
  }
@@ -8,7 +8,8 @@ function indexHandler(req, res) {
8
8
  'POST /delete': 'Delete file or directory (body: {path})',
9
9
  'POST /open-cursor': 'Open Cursor (body: {path, line?})',
10
10
  'POST /shutdown': 'Gracefully shutdown the server',
11
- 'POST /scan_code_quality': 'Scan code quality (body: {projectPath})'
11
+ 'POST /scan_code_quality': 'Scan code quality (body: {projectPath})',
12
+ 'WS /terminal?cwd=<path>': 'WebSocket terminal connection (optional cwd query param)'
12
13
  },
13
14
  example: 'Try: /list or /read?path=README.md'
14
15
  });
@@ -0,0 +1,92 @@
1
+ const pty = require('node-pty');
2
+
3
+ // Store active terminal sessions
4
+ const terminals = new Map();
5
+
6
+ function handleTerminalConnection(ws, workingDirectory) {
7
+ const shell = process.platform === 'win32' ? 'powershell.exe' : 'zsh';
8
+ const cwd = workingDirectory || process.env.HOME;
9
+
10
+ console.log('🖥️ Terminal session started in:', cwd);
11
+
12
+ const ptyProcess = pty.spawn(shell, [], {
13
+ name: 'xterm-256color',
14
+ cols: 80,
15
+ rows: 24,
16
+ cwd: cwd,
17
+ env: {
18
+ ...process.env,
19
+ TERM: 'xterm-256color',
20
+ COLORTERM: 'truecolor',
21
+ },
22
+ });
23
+
24
+ const terminalId = Date.now().toString();
25
+ terminals.set(terminalId, { pty: ptyProcess, ws });
26
+
27
+ // Send PTY output to WebSocket
28
+ ptyProcess.onData((data) => {
29
+ try {
30
+ if (ws.readyState === 1) { // WebSocket.OPEN
31
+ ws.send(data);
32
+ }
33
+ } catch (err) {
34
+ console.error('Error sending to WebSocket:', err);
35
+ }
36
+ });
37
+
38
+ // Handle incoming messages from WebSocket
39
+ ws.on('message', (msg) => {
40
+ try {
41
+ const message = msg.toString();
42
+
43
+ // Check if it's a resize command
44
+ if (message.startsWith('\x1b[8;')) {
45
+ // Parse resize: ESC[8;rows;colst
46
+ const match = message.match(/\x1b\[8;(\d+);(\d+)t/);
47
+ if (match) {
48
+ const rows = parseInt(match[1], 10);
49
+ const cols = parseInt(match[2], 10);
50
+ ptyProcess.resize(cols, rows);
51
+ return;
52
+ }
53
+ }
54
+
55
+ // Check for JSON resize command
56
+ if (message.startsWith('{')) {
57
+ try {
58
+ const json = JSON.parse(message);
59
+ if (json.type === 'resize' && json.cols && json.rows) {
60
+ ptyProcess.resize(json.cols, json.rows);
61
+ return;
62
+ }
63
+ } catch (e) {
64
+ // Not JSON, treat as regular input
65
+ }
66
+ }
67
+
68
+ // Regular terminal input
69
+ ptyProcess.write(message);
70
+ } catch (err) {
71
+ console.error('Error processing message:', err);
72
+ }
73
+ });
74
+
75
+ // Clean up on WebSocket close
76
+ ws.on('close', () => {
77
+ console.log('🖥️ Terminal session closed');
78
+ ptyProcess.kill();
79
+ terminals.delete(terminalId);
80
+ });
81
+
82
+ // Handle PTY exit
83
+ ptyProcess.onExit(({ exitCode, signal }) => {
84
+ console.log(`🖥️ Terminal process exited (code: ${exitCode}, signal: ${signal})`);
85
+ terminals.delete(terminalId);
86
+ if (ws.readyState === 1) {
87
+ ws.close();
88
+ }
89
+ });
90
+ }
91
+
92
+ module.exports = { handleTerminalConnection };
@@ -2,6 +2,8 @@
2
2
 
3
3
  const express = require('./node_modules/express');
4
4
  const cors = require('./node_modules/cors');
5
+ const WebSocket = require('./node_modules/ws');
6
+ const { ROOT } = require('./config');
5
7
 
6
8
  // Import handlers
7
9
  const indexHandler = require('./handlers/index');
@@ -11,6 +13,7 @@ const deleteHandler = require('./handlers/delete');
11
13
  const openCursorHandler = require('./handlers/open-cursor');
12
14
  const shutdownHandler = require('./handlers/shutdown');
13
15
  const scanCodeQualityHandler = require('./handlers/scan_code_quality');
16
+ const { handleTerminalConnection } = require('./handlers/terminal');
14
17
 
15
18
  const app = express();
16
19
 
@@ -27,6 +30,30 @@ app.post('/shutdown', shutdownHandler);
27
30
  app.post('/scan_code_quality', scanCodeQualityHandler);
28
31
 
29
32
  const port = 3001;
30
- app.listen(port, () => {
33
+ const server = app.listen(port, () => {
31
34
  console.log('📂 File server running on http://localhost:' + port);
32
35
  });
36
+
37
+ // WebSocket server for terminal
38
+ const wss = new WebSocket.Server({
39
+ server,
40
+ path: '/terminal'
41
+ });
42
+
43
+ wss.on('connection', (ws, req) => {
44
+ // Extract working directory from query string if provided
45
+ let workingDirectory = ROOT;
46
+
47
+ if (req.url) {
48
+ const urlMatch = req.url.match(/[?&]cwd=([^&]+)/);
49
+ if (urlMatch) {
50
+ try {
51
+ workingDirectory = decodeURIComponent(urlMatch[1]);
52
+ } catch (e) {
53
+ console.warn('Invalid cwd parameter, using default:', e.message);
54
+ }
55
+ }
56
+ }
57
+
58
+ handleTerminalConnection(ws, workingDirectory);
59
+ });