Skip to content

Commit 45cb8ca

Browse files
committed
feat: switched to hono server and added cors support
1 parent 78d5dd6 commit 45cb8ca

File tree

4 files changed

+89
-38
lines changed

4 files changed

+89
-38
lines changed

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"@codifycli/ink-form": "0.0.12",
88
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
9+
"@hono/node-server": "^1.17.1",
910
"@inkjs/ui": "^2",
1011
"@mischnic/json-sourcemap": "^0.1.1",
1112
"@oclif/core": "^4.0.8",
@@ -19,6 +20,7 @@
1920
"debug": "^4.3.4",
2021
"detect-indent": "^7.0.1",
2122
"diff": "^7.0.0",
23+
"hono": "^4.8.9",
2224
"ink": "^5.1.0",
2325
"ink-big-text": "^2.0.0",
2426
"ink-gradient": "^3.0.0",
@@ -73,8 +75,8 @@
7375
"strip-ansi": "^7.1.0",
7476
"tsx": "^4.7.3",
7577
"typescript": "5.3.3",
76-
"vitest": "^2.1.6",
77-
"uuid": "^10.0.0"
78+
"uuid": "^10.0.0",
79+
"vitest": "^2.1.6"
7880
},
7981
"overrides": {
8082
"ink-form": {

src/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const config = {
2+
loginServerPort: 51_039,
3+
corsAllowedOrigins: [
4+
'https://codify-dashboard.com',
5+
'https://https://codify-dashboard.kevinwang5658.workers.dev',
6+
'http://localhost:3000'
7+
]
8+
}

src/orchestrators/login.ts

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,69 @@
1+
import { serve } from '@hono/node-server';
2+
import { Hono } from 'hono';
3+
import { cors } from 'hono/cors'
4+
import { HTTPException } from 'hono/http-exception';
15
import * as fs from 'node:fs/promises';
2-
import { type IncomingMessage, ServerResponse, createServer } from 'node:http';
36
import * as os from 'node:os';
47
import path from 'node:path';
58
import open from 'open';
69

10+
import { config } from '../config.js';
11+
import { ajv } from '../utils/ajv.js';
12+
13+
const schema = {
14+
type: 'object',
15+
properties: {
16+
accessToken: {
17+
type: 'string',
18+
},
19+
email: {
20+
type: 'string',
21+
},
22+
userId: {
23+
type: 'string',
24+
},
25+
expiry: {
26+
type: 'string',
27+
}
28+
},
29+
additionalProperties: false,
30+
required: ['accessToken', 'email', 'userId', 'expiry'],
31+
}
32+
33+
interface Credentials {
34+
accessToken: string;
35+
email: string;
36+
userId: string;
37+
expiry: string;
38+
}
39+
740
export class LoginOrchestrator {
841
static async run(){
9-
const server = createServer((req, res) => {
10-
LoginOrchestrator.handleRequests(req, res);
11-
});
42+
const server = new Hono();
1243

13-
server.listen(51_039, 'localhost', () => {
44+
server.use('*', cors({ origin: config.corsAllowedOrigins }))
45+
server.post('/', async (c) => {
46+
const body = await c.req.json();
47+
if (!ajv.validate(schema, body)) {
48+
throw new HTTPException(400, { message: ajv.errorsText() })
49+
}
50+
51+
await LoginOrchestrator.saveCredentials(body as unknown as Credentials)
52+
return c.text('Success', 200);
53+
});
54+
55+
serve({
56+
fetch: server.fetch,
57+
port: config.loginServerPort,
58+
}, () => {
1459
console.log('Opening CLI auth page...')
1560
open('http://localhost:3000/auth/cli');
1661
})
1762
}
1863

19-
private static async handleRequests(req: IncomingMessage, res: ServerResponse<IncomingMessage>) {
20-
try {
21-
if (req.method !== 'POST') {
22-
res.writeHead(400, { 'Content-Type': 'application/json' });
23-
return;
24-
}
25-
26-
const json = await new Promise((resolve) => {
27-
const buf = new Array<Uint8Array>()
28-
req.on('data', (chunk) => {
29-
buf.push(chunk);
30-
}).on('end', () => {
31-
const body = Buffer.concat(buf).toString();
32-
const json = JSON.parse(body);
33-
resolve(json);
34-
}).on('error', (err) => {
35-
console.error(err);
36-
})
37-
});
38-
39-
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
40-
console.log(`Saving credentials to ${credentialsPath}`);
41-
await fs.writeFile(credentialsPath, JSON.stringify(json));
42-
43-
res.writeHead(200, { 'Content-Type': 'application/json' });
44-
process.exit(0);
45-
} catch (error) {
46-
console.error(error);
47-
res.writeHead(500, { 'Content-Type': 'application/json' });
48-
process.exit(1);
49-
}
64+
private static async saveCredentials(credentials: Credentials) {
65+
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
66+
console.log(`Saving credentials to ${credentialsPath}`);
67+
await fs.writeFile(credentialsPath, JSON.stringify(credentials));
5068
}
5169
}

0 commit comments

Comments
 (0)