1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-05 10:22:31 +01:00

Use erlpack instead of @yukikaze-bot/erlpack (#968)

This commit is contained in:
Madeline 2023-02-01 13:00:04 +11:00 committed by GitHub
parent b21d428580
commit d8f32cb05a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 80 additions and 49 deletions

52
package-lock.json generated
View File

@ -45,7 +45,7 @@
"probe-image-size": "^7.2.3", "probe-image-size": "^7.2.3",
"proxy-agent": "^5.0.0", "proxy-agent": "^5.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.4", "sqlite3": "*",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typeorm": "^0.3.10", "typeorm": "^0.3.10",
@ -79,7 +79,8 @@
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
"optionalDependencies": { "optionalDependencies": {
"@yukikaze-bot/erlpack": "^1.0.1" "erlpack": "^0.1.4",
"sqlite3": "^5.1.4"
} }
}, },
"node_modules/@acuminous/bitsyntax": { "node_modules/@acuminous/bitsyntax": {
@ -2327,17 +2328,6 @@
"url": "https://opencollective.com/typescript-eslint" "url": "https://opencollective.com/typescript-eslint"
} }
}, },
"node_modules/@yukikaze-bot/erlpack": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@yukikaze-bot/erlpack/-/erlpack-1.0.1.tgz",
"integrity": "sha512-PCJ2lGCf8DsQtrE411PY+NTsolK48l4InNn1kcBo0iUllKZYGLqeqXEWGA/INrmwanKcoYkU4pBySqUFLQDEoA==",
"hasInstallScript": true,
"optional": true,
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.5",
"node-addon-api": "^4.0.0"
}
},
"node_modules/abbrev": { "node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -2707,6 +2697,21 @@
"node": "*" "node": "*"
} }
}, },
"node_modules/bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"optional": true,
"dependencies": {
"file-uri-to-path": "1.0.0"
}
},
"node_modules/bindings/node_modules/file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"optional": true
},
"node_modules/body-parser": { "node_modules/body-parser": {
"version": "1.20.1", "version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
@ -3569,6 +3574,17 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/erlpack": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/erlpack/-/erlpack-0.1.4.tgz",
"integrity": "sha512-CJYbkEvsB5FqCCu2tLxF1eYKi28PvemC12oqzJ9oO6mDFrFO9G9G7nNJUHhiAyyL9zfXTOJx/tOcrQk+ncD65w==",
"hasInstallScript": true,
"optional": true,
"dependencies": {
"bindings": "^1.5.0",
"nan": "^2.15.0"
}
},
"node_modules/err-code": { "node_modules/err-code": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/err-code/-/err-code-2.0.3.tgz", "resolved": "https://registry.npmjs.org/err-code/-/err-code-2.0.3.tgz",
@ -5583,6 +5599,12 @@
"thenify-all": "^1.0.0" "thenify-all": "^1.0.0"
} }
}, },
"node_modules/nan": {
"version": "2.17.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz",
"integrity": "sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==",
"optional": true
},
"node_modules/natural-compare": { "node_modules/natural-compare": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
@ -5649,7 +5671,8 @@
"node_modules/node-addon-api": { "node_modules/node-addon-api": {
"version": "4.3.0", "version": "4.3.0",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz",
"integrity": "sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==" "integrity": "sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==",
"optional": true
}, },
"node_modules/node-fetch": { "node_modules/node-fetch": {
"version": "2.6.7", "version": "2.6.7",
@ -6782,6 +6805,7 @@
"resolved": "https://registry.npmjs.org/sqlite3/-/sqlite3-5.1.4.tgz", "resolved": "https://registry.npmjs.org/sqlite3/-/sqlite3-5.1.4.tgz",
"integrity": "sha512-i0UlWAzPlzX3B5XP2cYuhWQJsTtlMD6obOa1PgeEQ4DHEXUuyJkgv50I3isqZAP5oFc2T8OFvakmDh2W6I+YpA==", "integrity": "sha512-i0UlWAzPlzX3B5XP2cYuhWQJsTtlMD6obOa1PgeEQ4DHEXUuyJkgv50I3isqZAP5oFc2T8OFvakmDh2W6I+YpA==",
"hasInstallScript": true, "hasInstallScript": true,
"optional": true,
"dependencies": { "dependencies": {
"@mapbox/node-pre-gyp": "^1.0.0", "@mapbox/node-pre-gyp": "^1.0.0",
"node-addon-api": "^4.2.0", "node-addon-api": "^4.2.0",

View File

@ -99,20 +99,20 @@
"probe-image-size": "^7.2.3", "probe-image-size": "^7.2.3",
"proxy-agent": "^5.0.0", "proxy-agent": "^5.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.4",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typeorm": "^0.3.10", "typeorm": "^0.3.10",
"typescript-json-schema": "^0.50.1", "typescript-json-schema": "^0.50.1",
"ws": "^8.9.0" "ws": "^8.9.0"
}, },
"optionalDependencies": {
"@yukikaze-bot/erlpack": "^1.0.1"
},
"_moduleAliases": { "_moduleAliases": {
"@fosscord/api": "dist/api", "@fosscord/api": "dist/api",
"@fosscord/cdn": "dist/cdn", "@fosscord/cdn": "dist/cdn",
"@fosscord/gateway": "dist/gateway", "@fosscord/gateway": "dist/gateway",
"@fosscord/util": "dist/util" "@fosscord/util": "dist/util"
},
"optionalDependencies": {
"erlpack": "^0.1.4",
"sqlite3": "^5.1.4"
} }
} }

View File

@ -27,12 +27,13 @@ import { Close } from "./Close";
import { Message } from "./Message"; import { Message } from "./Message";
import { Deflate, Inflate } from "fast-zlib"; import { Deflate, Inflate } from "fast-zlib";
import { URL } from "url"; import { URL } from "url";
import { Config } from "@fosscord/util"; import { Config, ErlpackType } from "@fosscord/util";
let erlpack: unknown;
let erlpack: ErlpackType | null = null;
try { try {
erlpack = require("@yukikaze-bot/erlpack"); erlpack = require("erlpack") as ErlpackType;
} catch (error) { } catch (e) {
/* empty */ // empty
} }
// TODO: check rate limit // TODO: check rate limit
@ -84,14 +85,11 @@ export async function Connection(
const { searchParams } = new URL(`http://localhost${request.url}`); const { searchParams } = new URL(`http://localhost${request.url}`);
// @ts-ignore // @ts-ignore
socket.encoding = searchParams.get("encoding") || "json"; socket.encoding = searchParams.get("encoding") || "json";
if (!["json", "etf"].includes(socket.encoding)) { if (!["json", "etf"].includes(socket.encoding))
if (socket.encoding === "etf" && erlpack) {
throw new Error(
"Erlpack is not installed: 'npm i @yukikaze-bot/erlpack'",
);
}
return socket.close(CLOSECODES.Decode_error); return socket.close(CLOSECODES.Decode_error);
}
if (socket.encoding === "etf" && erlpack)
throw new Error("Erlpack is not installed: 'npm i erlpack'");
socket.version = Number(searchParams.get("version")) || 8; socket.version = Number(searchParams.get("version")) || 8;
if (socket.version != 8) if (socket.version != 8)

View File

@ -20,18 +20,18 @@ import { WebSocket, Payload, CLOSECODES, OPCODES } from "@fosscord/gateway";
import OPCodeHandlers from "../opcodes"; import OPCodeHandlers from "../opcodes";
import { check } from "../opcodes/instanceOf"; import { check } from "../opcodes/instanceOf";
import WS from "ws"; import WS from "ws";
import { PayloadSchema } from "@fosscord/util"; import { PayloadSchema, ErlpackType } from "@fosscord/util";
import * as Sentry from "@sentry/node"; import * as Sentry from "@sentry/node";
import BigIntJson from "json-bigint"; import BigIntJson from "json-bigint";
import path from "path"; import path from "path";
import fs from "fs/promises"; import fs from "fs/promises";
const bigIntJson = BigIntJson({ storeAsString: true }); const bigIntJson = BigIntJson({ storeAsString: true });
let erlpack: { unpack: (buffer: Buffer) => Payload }; let erlpack: ErlpackType | null = null;
try { try {
erlpack = require("@yukikaze-bot/erlpack"); erlpack = require("erlpack") as ErlpackType;
} catch (error) { } catch (e) {
/* empty */ // empty
} }
export async function Message(this: WebSocket, buffer: WS.Data) { export async function Message(this: WebSocket, buffer: WS.Data) {
@ -52,7 +52,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
} }
} }
data = bigIntJson.parse(buffer as string); data = bigIntJson.parse(buffer as string);
} else if (this.encoding === "etf" && buffer instanceof Buffer) { } else if (this.encoding === "etf" && buffer instanceof Buffer && erlpack) {
try { try {
data = erlpack.unpack(buffer); data = erlpack.unpack(buffer);
} catch { } catch {

View File

@ -16,18 +16,18 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
let erlpack: { pack: (data: Payload) => Buffer };
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {
console.log(
"Missing @yukikaze-bot/erlpack, electron-based desktop clients designed for discord.com will not be able to connect!",
);
}
import { Payload, WebSocket } from "@fosscord/gateway"; import { Payload, WebSocket } from "@fosscord/gateway";
import fs from "fs/promises"; import fs from "fs/promises";
import path from "path"; import path from "path";
import type { ErlpackType } from "@fosscord/util";
let erlpack: ErlpackType | null = null;
try {
erlpack = require("erlpack") as ErlpackType;
} catch (e) {
// empty
}
export function Send(socket: WebSocket, data: Payload) { export function Send(socket: WebSocket, data: Payload) {
if (process.env.WS_VERBOSE) if (process.env.WS_VERBOSE)
console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`); console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
@ -47,7 +47,7 @@ export function Send(socket: WebSocket, data: Payload) {
} }
let buffer: Buffer | string; let buffer: Buffer | string;
if (socket.encoding === "etf") buffer = erlpack.pack(data); if (socket.encoding === "etf" && erlpack) buffer = erlpack.pack(data);
// TODO: encode circular object // TODO: encode circular object
else if (socket.encoding === "json") buffer = JSON.stringify(data); else if (socket.encoding === "json") buffer = JSON.stringify(data);
else return; else return;

View File

@ -0,0 +1,12 @@
/*
https://github.com/discord/erlpack/blob/master/js/index.d.ts
MIT License
Copyright (c) 2017 Discord
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
// @fc-license-skip
export type ErlpackType = {
pack: (data: any) => Buffer;
unpack: <T = any>(data: Buffer) => T;
};

View File

@ -17,3 +17,4 @@
*/ */
export * from "./OrmUtils"; export * from "./OrmUtils";
export * from "./Erlpack";

View File

@ -23,10 +23,6 @@ import WS from "ws";
import { VoiceOPCodes } from "../util"; import { VoiceOPCodes } from "../util";
import { onClose } from "./Close"; import { onClose } from "./Close";
import { onMessage } from "./Message"; import { onMessage } from "./Message";
var erlpack: any;
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {}
// TODO: check rate limit // TODO: check rate limit
// TODO: specify rate limit in config // TODO: specify rate limit in config