From 3fa2ded35a6353fc93551e9c2f7b025c8241a5a4 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 1 Oct 2021 21:30:36 +0200 Subject: :bug: fix schema + user modify not working --- api/scripts/generate_schema.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'api/scripts/generate_schema.js') diff --git a/api/scripts/generate_schema.js b/api/scripts/generate_schema.js index fc787c22..6bc35fe8 100644 --- a/api/scripts/generate_schema.js +++ b/api/scripts/generate_schema.js @@ -30,6 +30,15 @@ const Excluded = [ "supertest.Response" ]; +function modify(obj) { + delete obj.additionalProperties; + for (var k in obj) { + if (typeof obj[k] === "object" && obj[k] !== null) { + modify(obj[k]); + } + } +} + function main() { const program = TJS.getProgramFromFiles(walk(path.join(__dirname, "..", "src", "routes")), compilerOptions); const generator = TJS.buildGenerator(program, settings); @@ -47,7 +56,9 @@ function main() { definitions = { ...definitions, [name]: { ...part } }; } - fs.writeFileSync(schemaPath, JSON.stringify(definitions, null, 4)); + modify(definitions); + + fs.writeFileSync(schemaPath, JSON.stringify(definitions, null, 4).replace(/ "additionalProperties": ?false,?/g, "")); } main(); -- cgit 1.5.1 From 1716d92849633476690dd660a68b2992d0f467bf Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 2 Oct 2021 15:12:47 +0200 Subject: :art: restructure gateway --- api/scripts/generate_schema.js | 2 +- bundle/src/Server.ts | 16 +++++++++++++++- gateway/package.json | 2 +- gateway/src/Server.ts | 15 ++++++++++++--- gateway/src/events/Connection.ts | 7 +++++-- gateway/src/util/Heartbeat.ts | 11 +++++++++++ gateway/src/util/WebSocket.ts | 1 - gateway/src/util/index.ts | 2 +- gateway/src/util/setHeartbeat.ts | 11 ----------- gateway/tsconfig.json | 3 +-- 10 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 gateway/src/util/Heartbeat.ts delete mode 100644 gateway/src/util/setHeartbeat.ts (limited to 'api/scripts/generate_schema.js') diff --git a/api/scripts/generate_schema.js b/api/scripts/generate_schema.js index 6bc35fe8..c12f6b1a 100644 --- a/api/scripts/generate_schema.js +++ b/api/scripts/generate_schema.js @@ -58,7 +58,7 @@ function main() { modify(definitions); - fs.writeFileSync(schemaPath, JSON.stringify(definitions, null, 4).replace(/ "additionalProperties": ?false,?/g, "")); + fs.writeFileSync(schemaPath, JSON.stringify(definitions, null, 4)); } main(); diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts index 62764e8e..3a56ee2e 100644 --- a/bundle/src/Server.ts +++ b/bundle/src/Server.ts @@ -38,9 +38,23 @@ async function main() { '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}', endpointPrivate: `ws://localhost:${port}`, ...(!Config.get().gateway.endpointPublic && { - endpointPublic: `http://localhost:${port}`, + endpointPublic: `ws://localhost:${port}`, }), }, + // regions: { + // default: "fosscord", + // useDefaultAsOptimal: true, + // available: [ + // { + // id: "fosscord", + // name: "Fosscord", + // endpoint: "127.0.0.1:3001", + // vip: false, + // custom: false, + // deprecated: false, + // }, + // ], + // }, } as any); await Promise.all([api.start(), cdn.start(), gateway.start()]); diff --git a/gateway/package.json b/gateway/package.json index d0292925..863d7f4e 100644 --- a/gateway/package.json +++ b/gateway/package.json @@ -4,7 +4,7 @@ "description": "", "main": "dist/index.js", "scripts": { - "postinstall": "ts-patch install -s", + "postinstall": "npx ts-patch install -s", "test": "echo \"Error: no test specified\" && exit 1", "start": "npm run build && node dist/start.js", "build": "npx tsc -b .", diff --git a/gateway/src/Server.ts b/gateway/src/Server.ts index 944174c7..b4e92a75 100644 --- a/gateway/src/Server.ts +++ b/gateway/src/Server.ts @@ -1,7 +1,7 @@ import "missing-native-js-functions"; import dotenv from "dotenv"; dotenv.config(); -import { closeDatabase, Config, initDatabase, initEvent, RabbitMQ } from "@fosscord/util"; +import { closeDatabase, Config, initDatabase, initEvent } from "@fosscord/util"; import { Server as WebSocketServer } from "ws"; import { Connection } from "./events/Connection"; import http from "http"; @@ -12,15 +12,24 @@ export class Server { public server: http.Server; public production: boolean; - constructor({ port, server, production }: { port: number; server?: http.Server; production?: boolean }) { + constructor({ + port, + server, + production, + }: { + port: number; + server?: http.Server; + production?: boolean; + }) { this.port = port; this.production = production || false; if (server) this.server = server; - else + else { this.server = http.createServer(function (req, res) { res.writeHead(200).end("Online"); }); + } this.server.on("upgrade", (request, socket, head) => { console.log("socket requests upgrade", request.url); diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts index 85f92b77..f84f9d9b 100644 --- a/gateway/src/events/Connection.ts +++ b/gateway/src/events/Connection.ts @@ -35,8 +35,11 @@ export async function Connection( // @ts-ignore socket.encoding = searchParams.get("encoding") || "json"; if (!["json", "etf"].includes(socket.encoding)) { - if (socket.encoding === "etf" && erlpack) - throw new Error("Erlpack is not installed: 'npm i -D erlpack'"); + if (socket.encoding === "etf" && erlpack) { + throw new Error( + "Erlpack is not installed: 'npm i @yukikaze-bot/erlpack'" + ); + } return socket.close(CLOSECODES.Decode_error); } diff --git a/gateway/src/util/Heartbeat.ts b/gateway/src/util/Heartbeat.ts new file mode 100644 index 00000000..f6871cfe --- /dev/null +++ b/gateway/src/util/Heartbeat.ts @@ -0,0 +1,11 @@ +import { CLOSECODES } from "./Constants"; +import { WebSocket } from "./WebSocket"; + +// TODO: make heartbeat timeout configurable +export function setHeartbeat(socket: WebSocket) { + if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout); + + socket.heartbeatTimeout = setTimeout(() => { + return socket.close(CLOSECODES.Session_timed_out); + }, 1000 * 45); +} diff --git a/gateway/src/util/WebSocket.ts b/gateway/src/util/WebSocket.ts index b80265a7..49626b2a 100644 --- a/gateway/src/util/WebSocket.ts +++ b/gateway/src/util/WebSocket.ts @@ -1,7 +1,6 @@ import { Intents, Permissions } from "@fosscord/util"; import WS from "ws"; import { Deflate } from "zlib"; -import { Channel } from "amqplib"; export interface WebSocket extends WS { version: number; diff --git a/gateway/src/util/index.ts b/gateway/src/util/index.ts index 27af5813..0be5ecee 100644 --- a/gateway/src/util/index.ts +++ b/gateway/src/util/index.ts @@ -1,5 +1,5 @@ export * from "./Constants"; export * from "./Send"; export * from "./SessionUtils"; -export * from "./setHeartbeat"; +export * from "./Heartbeat"; export * from "./WebSocket"; diff --git a/gateway/src/util/setHeartbeat.ts b/gateway/src/util/setHeartbeat.ts deleted file mode 100644 index f6871cfe..00000000 --- a/gateway/src/util/setHeartbeat.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { CLOSECODES } from "./Constants"; -import { WebSocket } from "./WebSocket"; - -// TODO: make heartbeat timeout configurable -export function setHeartbeat(socket: WebSocket) { - if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout); - - socket.heartbeatTimeout = setTimeout(() => { - return socket.close(CLOSECODES.Session_timed_out); - }, 1000 * 45); -} diff --git a/gateway/tsconfig.json b/gateway/tsconfig.json index dd066383..7143c8a3 100644 --- a/gateway/tsconfig.json +++ b/gateway/tsconfig.json @@ -71,8 +71,7 @@ "baseUrl": ".", "paths": { "@fosscord/gateway": ["src/index"], - "@fosscord/gateway/*": ["src/*"], - "@fosscord/util/*": ["../util/src/*"] + "@fosscord/gateway/*": ["src/*"] }, "plugins": [{ "transform": "@zerollup/ts-transform-paths" }] } -- cgit 1.5.1