FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

init CTProto · codex-team/notes.api@ffdde7f · GitHub

Commit ffdde7f

Browse files
Dobrunia Kostrigin
committed
init CTProto
1 parent f1662af commit ffdde7f

5 files changed

Lines changed: 108 additions & 25 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@fastify/swagger-ui": "^1.9.3",
5151
"@testcontainers/postgresql": "^10.2.1",
5252
"arg": "^5.0.2",
53+
"ctproto": "^0.0.13",
5354
"fastify": "^4.17.0",
5455
"http-status-codes": "^2.2.0",
5556
"jsonwebtoken": "^9.0.0",

‎src/index.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ const start = async (): Promise<void> => {
2020

2121
await httpApi.init(domainServices);
2222
await httpApi.run();
23-
24-
await socketApi.init(domainServices);
25-
2623
if (config.metrics.enabled) {
2724
await runMetricsServer();
2825
}

‎src/infrastructure/config/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const defaultConfig: AppConfig = {
174174
s3Storage: 'info',
175175
},
176176
socketApi: {
177-
host: '0.0.0.0',
177+
host: 'localhost',
178178
port: 8080,
179179
},
180180
database: {
Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,83 @@
1-
import http from "http";
2-
import { WebSocketServer, WebSocket } from "ws";
3-
import type { DomainServices } from "@domain/index.ts";
1+
import { CTProtoServer } from "ctproto";
2+
3+
/**
4+
* Интерфейсы для CTProto
5+
*/
6+
interface AuthRequestPayload {
7+
token: string;
8+
}
9+
10+
interface AuthResponsePayload {
11+
userId: string;
12+
username?: string;
13+
}
14+
15+
interface ApiRequest {
16+
messageId: string;
17+
type: string;
18+
payload: any;
19+
}
20+
21+
interface ApiResponse {
22+
messageId: string;
23+
type: string;
24+
payload: any;
25+
}
26+
27+
interface ApiUpdate {
28+
messageId: string;
29+
type: string;
30+
payload: any;
31+
}
432

533
export default class SocketApi {
6-
private wss: any | undefined;
7-
private server: http.Server;
8-
private config: any;
34+
private ctproto: CTProtoServer<
35+
AuthRequestPayload,
36+
AuthResponsePayload,
37+
ApiRequest,
38+
ApiResponse,
39+
ApiUpdate
40+
>;
941

10-
constructor(config: any) {
42+
private config: { host: string; port: number };
43+
44+
constructor(config: { host: string; port: number }) {
1145
this.config = config;
12-
this.server = http.createServer();
13-
}
46+
this.ctproto = new CTProtoServer({
47+
port: this.config.port,
1448

15-
public async init(domainServices: DomainServices): Promise<void> {
16-
this.wss = new WebSocketServer({ server: this.server });
49+
async onAuth(authRequestPayload: AuthRequestPayload): Promise<AuthResponsePayload> {
50+
console.log("🔑 Проверка токена:", authRequestPayload.token);
1751

18-
this.wss.on("connection", (ws: WebSocket) => {
19-
ws.on("message", (message: string) => {
20-
ws.send(`Echo: ${message}`);//TODO: note.ts route
21-
});
52+
if (!authRequestPayload.token || authRequestPayload.token !== "valid-token") {
53+
throw new Error("❌ Неверный токен");
54+
}
2255

23-
ws.on("close", () => {});
56+
return {
57+
userId: "111",
58+
username: "TestUser",
59+
};
60+
},
2461

25-
ws.on("error", (error) => {});
26-
});
62+
async onMessage(message: ApiRequest): Promise<ApiResponse> {
63+
console.log("📩 Получено сообщение от клиента:", message);
64+
65+
if (message.type === "note:join") {
66+
return {
67+
messageId: message.messageId,
68+
type: "response",
69+
payload: { status: "joined", noteId: message.payload.noteId },
70+
};
71+
}
2772

28-
const { host, port } = this.config;
29-
this.server.listen(port, host, () => {
30-
console.log(`WebSocket server running at ws://${host}:${port}`);
73+
return {
74+
messageId: message.messageId,
75+
type: "error",
76+
payload: "❌ Неизвестная команда",
77+
};
78+
},
3179
});
80+
81+
console.log(`✅ CTProto-сервер запущен на порту ${this.config.port}`);
3282
}
3383
}

‎yarn.lock‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,16 @@ __metadata:
36323632
languageName: node
36333633
linkType: hard
36343634

3635+
"ctproto@npm:^0.0.13":
3636+
version: 0.0.13
3637+
resolution: "ctproto@npm:0.0.13"
3638+
dependencies:
3639+
nanoid: ^3.1.20
3640+
ws: ^7.4.1
3641+
checksum: 48adbd6c3fa69c1fa11a00105422c7b8563aadfb55d70908eba3988666177525775be7d0a64c2ec73a2c688b42aa63124b1f9c2ac9a2ad523401478fa9e6c6db
3642+
languageName: node
3643+
linkType: hard
3644+
36353645
"data-view-buffer@npm:^1.0.1":
36363646
version: 1.0.1
36373647
resolution: "data-view-buffer@npm:1.0.1"
@@ -6431,6 +6441,15 @@ __metadata:
64316441
languageName: node
64326442
linkType: hard
64336443

6444+
"nanoid@npm:^3.1.20":
6445+
version: 3.3.8
6446+
resolution: "nanoid@npm:3.3.8"
6447+
bin:
6448+
nanoid: bin/nanoid.cjs
6449+
checksum: dfe0adbc0c77e9655b550c333075f51bb28cfc7568afbf3237249904f9c86c9aaaed1f113f0fddddba75673ee31c758c30c43d4414f014a52a7a626efc5958c9
6450+
languageName: node
6451+
linkType: hard
6452+
64346453
"nanoid@npm:^3.3.7":
64356454
version: 3.3.7
64366455
resolution: "nanoid@npm:3.3.7"
@@ -6567,6 +6586,7 @@ __metadata:
65676586
"@types/ws": ^8.5.14
65686587
"@vitest/coverage-v8": ^0.34.5
65696588
arg: ^5.0.2
6589+
ctproto: ^0.0.13
65706590
eslint: ^9.2.0
65716591
eslint-config-codex: ^2.0.0
65726592
eslint-import-resolver-alias: 1.1.2
@@ -9240,6 +9260,21 @@ __metadata:
92409260
languageName: node
92419261
linkType: hard
92429262

9263+
"ws@npm:^7.4.1":
9264+
version: 7.5.10
9265+
resolution: "ws@npm:7.5.10"
9266+
peerDependencies:
9267+
bufferutil: ^4.0.1
9268+
utf-8-validate: ^5.0.2
9269+
peerDependenciesMeta:
9270+
bufferutil:
9271+
optional: true
9272+
utf-8-validate:
9273+
optional: true
9274+
checksum: f9bb062abf54cc8f02d94ca86dcd349c3945d63851f5d07a3a61c2fcb755b15a88e943a63cf580cbdb5b74436d67ef6b67f745b8f7c0814e411379138e1863cb
9275+
languageName: node
9276+
linkType: hard
9277+
92439278
"ws@npm:^8.18.0":
92449279
version: 8.18.0
92459280
resolution: "ws@npm:8.18.0"

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL