Skip to content

Commit 5289fd3

Browse files
committed
Switched to socketIO
1 parent f5eddbc commit 5289fd3

File tree

18 files changed

+174
-263
lines changed

18 files changed

+174
-263
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ USAGE
2626
```
2727
<!-- usagestop -->
2828
# Commands
29-
<!-- commands -->
29+
<!-- handlers -->
3030
* [`codify apply`](#codify-apply)
3131
* [`codify destroy`](#codify-destroy)
3232
* [`codify help [COMMAND]`](#codify-help-command)

esbuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { build } from 'esbuild';
22

33
const result = await build({
4-
entryPoints: ['src/commands/**/index.ts', 'src/commands/*.ts', 'src/index.ts'],
4+
entryPoints: ['src/handlers/**/router.ts', 'src/handlers/*.ts', 'src/router.ts'],
55
bundle: true,
66
minify: true,
77
splitting: true,

src/commands/apply.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For more information, visit: https://docs.codifycli.com/commands/apply
2222
static flags = {
2323
'sudoPassword': Flags.string({
2424
optional: true,
25-
description: 'Automatically use this password for any commands that require elevated permissions.',
25+
description: 'Automatically use this password for any handlers that require elevated permissions.',
2626
char: 'S'
2727
}),
2828
}

src/commands/destroy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ For more information, visit: https://docs.codifycli.com/commands/destory`
2929
static flags = {
3030
'sudoPassword': Flags.string({
3131
optional: true,
32-
description: 'Automatically use this password for any commands that require elevated permissions.',
32+
description: 'Automatically use this password for any handlers that require elevated permissions.',
3333
char: 'S',
3434
helpValue: '<password>'
3535
}),

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const config = {
33
connectServerPort: 51_040,
44

55
corsAllowedOrigins: [
6-
'https://codify-dashboard.com',
6+
'https://dashboard.codifycli.com',
77
'https://https://codify-dashboard.kevinwang5658.workers.dev',
88
'http://localhost:3000'
99
]

src/connect/apply.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/connect/http-route-handler.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { spawn } from '@homebridge/node-pty-prebuilt-multiarch';
2+
import { Router } from 'express';
3+
4+
import { SocketServer } from '../../socket-server.js';
5+
6+
export function createCommandHandler(command: string, args?: string): Router {
7+
const router = Router({
8+
mergeParams: true,
9+
});
10+
11+
router.post('/:sessionId/start', async (req, res) => {
12+
const { sessionId } = req.params;
13+
console.log(`Received request to ${command}, sessionId: ${sessionId}`)
14+
15+
if (!sessionId) {
16+
return res.status(400).json({ error: 'SessionId must be provided' });
17+
}
18+
19+
const server = SocketServer.get();
20+
const socket = server.getSession(sessionId);
21+
if (!socket) {
22+
return res.status(400).json({ error: 'SessionId does not exist' });
23+
}
24+
25+
if (!socket.connected) {
26+
return res.status(400).json({ error: 'Socket not connected. Connect to socket before calling this endpoint' });
27+
}
28+
29+
const pty = spawn('zsh', ['-c', `codify ${command} ${args ?? ''}`], {
30+
name: 'xterm-color',
31+
cols: 80,
32+
rows: 30,
33+
cwd: process.env.HOME,
34+
env: process.env
35+
});
36+
37+
pty.onData((data) => {
38+
socket.emit('data', Buffer.from(data, 'utf8'));
39+
});
40+
41+
socket.on('data', (message) => {
42+
pty.write(message.toString('utf8'));
43+
})
44+
45+
pty.onExit(({ exitCode, signal }) => {
46+
console.log('pty exit', exitCode, signal);
47+
// socket.disconnect();
48+
})
49+
});
50+
51+
return router;
52+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Router } from 'express';
2+
import { v4 as uuid } from 'uuid';
3+
4+
import { SocketServer } from '../../socket-server.js';
5+
6+
const router = Router({
7+
mergeParams: true,
8+
});
9+
10+
router.post('/session', (req, res) => {
11+
const sessionId = uuid();
12+
const socketServer = SocketServer.get();
13+
14+
socketServer.addSession(sessionId);
15+
console.log('Terminal session created!', sessionId)
16+
17+
return res.status(200).json({ sessionId });
18+
})
19+
20+
export default router;

src/connect/http-routes/router.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Router } from 'express';
2+
3+
import { createCommandHandler } from './handlers/create-command.js';
4+
import defaultHandler from './handlers/index.js';
5+
6+
const router = Router();
7+
8+
router.use('/', defaultHandler);
9+
router.use('/apply', createCommandHandler('apply'));
10+
router.use('/plan', createCommandHandler('plan'));
11+
router.use('/import', createCommandHandler('import'));
12+
13+
export default router;

0 commit comments

Comments
 (0)