| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f1662af commit ffdde7f
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,6 +50,7 @@ | |||
| 50 | 50 | "@fastify/swagger-ui": "^1.9.3", | |
| 51 | 51 | "@testcontainers/postgresql": "^10.2.1", | |
| 52 | 52 | "arg": "^5.0.2", | |
| 53 | + "ctproto": "^0.0.13", | ||
| 53 | 54 | "fastify": "^4.17.0", | |
| 54 | 55 | "http-status-codes": "^2.2.0", | |
| 55 | 56 | "jsonwebtoken": "^9.0.0", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,9 +20,6 @@ const start = async (): Promise<void> => { | |||
| 20 | 20 | ||
| 21 | 21 | await httpApi.init(domainServices); | |
| 22 | 22 | await httpApi.run(); | |
| 23 | - | ||
| 24 | - await socketApi.init(domainServices); | ||
| 25 | - | ||
| 26 | 23 | if (config.metrics.enabled) { | |
| 27 | 24 | await runMetricsServer(); | |
| 28 | 25 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -174,7 +174,7 @@ const defaultConfig: AppConfig = { | |||
| 174 | 174 | s3Storage: 'info', | |
| 175 | 175 | }, | |
| 176 | 176 | socketApi: { | |
| 177 | - host: '0.0.0.0', | ||
| 177 | + host: 'localhost', | ||
| 178 | 178 | port: 8080, | |
| 179 | 179 | }, | |
| 180 | 180 | database: { | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| 4 | 32 | ||
| 5 | 33 | 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 | + >; | ||
| 9 | 41 | ||
| 10 | - constructor(config: any) { | ||
| 42 | + private config: { host: string; port: number }; | ||
| 43 | + | ||
| 44 | + constructor(config: { host: string; port: number }) { | ||
| 11 | 45 | this.config = config; | |
| 12 | - this.server = http.createServer(); | ||
| 13 | - } | ||
| 46 | + this.ctproto = new CTProtoServer({ | ||
| 47 | + port: this.config.port, | ||
| 14 | 48 | ||
| 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); | ||
| 17 | 51 | ||
| 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 | + } | ||
| 22 | 55 | ||
| 23 | - ws.on("close", () => {}); | ||
| 56 | + return { | ||
| 57 | + userId: "111", | ||
| 58 | + username: "TestUser", | ||
| 59 | + }; | ||
| 60 | + }, | ||
| 24 | 61 | ||
| 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 | + } | ||
| 27 | 72 | ||
| 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 | + }, | ||
| 31 | 79 | }); | |
| 80 | + | ||
| 81 | + console.log(`✅ CTProto-сервер запущен на порту ${this.config.port}`); | ||
| 32 | 82 | } | |
| 33 | 83 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3632,6 +3632,16 @@ __metadata: | |||
| 3632 | 3632 | languageName: node | |
| 3633 | 3633 | linkType: hard | |
| 3634 | 3634 | ||
| 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 | + | ||
| 3635 | 3645 | "data-view-buffer@npm:^1.0.1": | |
| 3636 | 3646 | version: 1.0.1 | |
| 3637 | 3647 | resolution: "data-view-buffer@npm:1.0.1" | |
@@ -6431,6 +6441,15 @@ __metadata: | |||
| 6431 | 6441 | languageName: node | |
| 6432 | 6442 | linkType: hard | |
| 6433 | 6443 | ||
| 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 | + | ||
| 6434 | 6453 | "nanoid@npm:^3.3.7": | |
| 6435 | 6454 | version: 3.3.7 | |
| 6436 | 6455 | resolution: "nanoid@npm:3.3.7" | |
@@ -6567,6 +6586,7 @@ __metadata: | |||
| 6567 | 6586 | "@types/ws": ^8.5.14 | |
| 6568 | 6587 | "@vitest/coverage-v8": ^0.34.5 | |
| 6569 | 6588 | arg: ^5.0.2 | |
| 6589 | + ctproto: ^0.0.13 | ||
| 6570 | 6590 | eslint: ^9.2.0 | |
| 6571 | 6591 | eslint-config-codex: ^2.0.0 | |
| 6572 | 6592 | eslint-import-resolver-alias: 1.1.2 | |
@@ -9240,6 +9260,21 @@ __metadata: | |||
| 9240 | 9260 | languageName: node | |
| 9241 | 9261 | linkType: hard | |
| 9242 | 9262 | ||
| 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 | + | ||
| 9243 | 9278 | "ws@npm:^8.18.0": | |
| 9244 | 9279 | version: 8.18.0 | |
| 9245 | 9280 | resolution: "ws@npm:8.18.0" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments