diff --git a/package-lock.json b/package-lock.json index 827b0fead..ce23ed7b8 100644 Binary files a/package-lock.json and b/package-lock.json differ diff --git a/package.json b/package.json index 63fdd9233..3206e54dd 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "fido2-lib": "^3.5.9", "file-type": "^22.0.1", "form-data": "^4.0.5", + "harmony-erlpack": "^0.0.1", "i18next": "^26.0.6", "i18next-fs-backend": "^2.6.4", "i18next-http-middleware": "^3.9.3", @@ -131,7 +132,6 @@ }, "optionalDependencies": { "@sendgrid/mail": "^8.1.6", - "@yukikaze-bot/erlpack": "^1.0.1", "jimp": "^1.6.1", "mailgun.js": "^13.0.0", "node-mailjet": "^6.0.11", diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts index 8bd1adda9..043b73768 100644 --- a/src/gateway/events/Message.ts +++ b/src/gateway/events/Message.ts @@ -17,7 +17,8 @@ */ import { CLOSECODES, Payload, WebSocket } from "@spacebar/gateway"; -import { ErlpackType } from "@spacebar/util"; +// import { ErlpackType } from "@spacebar/util"; +import * as erlpack from "harmony-erlpack"; import fs from "node:fs/promises"; import BigIntJson from "json-bigint"; import path from "node:path"; @@ -28,12 +29,12 @@ import { PayloadSchema } from "@spacebar/schemas"; const bigIntJson = BigIntJson({ storeAsString: true }); -let erlpack: ErlpackType | null = null; -try { - erlpack = require("@yukikaze-bot/erlpack") as ErlpackType; -} catch (e) { - console.log("Failed to import @yukikaze-bot/erlpack: ", e); -} +// let erlpack: ErlpackType | null = null; +// try { +// erlpack = require("@yukikaze-bot/erlpack") as ErlpackType; +// } catch (e) { +// console.log("Failed to import @yukikaze-bot/erlpack: ", e); +// } export async function Message(this: WebSocket, buffer: WS.Data) { // TODO: compression @@ -61,7 +62,8 @@ export async function Message(this: WebSocket, buffer: WS.Data) { data = bigIntJson.parse(buffer as string); } else if (this.encoding === "etf" && Buffer.isBuffer(buffer) && erlpack) { try { - data = erlpack.unpack(buffer); + // cast is ~safe: unpack returns the parsed data in the shape it was provided, @yukikaze-bot/erlpack got around this by returning `any` instead of an actual type union. + data = erlpack.unpack(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)) as unknown as Payload; } catch { console.error(`[Gateway/${this.user_id ?? this.ipAddress}] Failed to decode ETF payload`); return this.close(CLOSECODES.Decode_error); diff --git a/src/gateway/util/Send.ts b/src/gateway/util/Send.ts index 00fd4b454..6a9c639d7 100644 --- a/src/gateway/util/Send.ts +++ b/src/gateway/util/Send.ts @@ -21,12 +21,14 @@ import fs from "node:fs/promises"; import path from "node:path"; import { ErlpackType, JSONReplacer } from "@spacebar/util"; -let erlpack: ErlpackType | null = null; -try { - erlpack = require("@yukikaze-bot/erlpack") as ErlpackType; -} catch (e) { - console.log("Failed to import @yukikaze-bot/erlpack: ", e); -} +import * as erlpack from "harmony-erlpack"; + +// let erlpack: ErlpackType | null = null; +// try { +// erlpack = require("@yukikaze-bot/erlpack") as ErlpackType; +// } catch (e) { +// console.log("Failed to import @yukikaze-bot/erlpack: ", e); +// } // don't care // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -58,7 +60,7 @@ export async function Send(socket: WebSocket, data: Payload) { if (socket.encoding === "etf" && erlpack) { // Erlpack doesn't like Date objects, encodes them as {} data = recurseJsonReplace(data); - buffer = erlpack.pack(data); + buffer = Buffer.from(erlpack.pack(data)); } // TODO: encode circular object else if (socket.encoding === "json") buffer = JSON.stringify(data, JSONReplacer);