feat: move arrpc server into a worker thread

This commit is contained in:
Justin 2025-01-15 19:25:06 -05:00
parent 5a72491ab0
commit f46f3163da
No known key found for this signature in database
GPG key ID: A260B29AA42DE5A6
4 changed files with 118 additions and 13 deletions

View file

@ -57,6 +57,12 @@ await Promise.all([
outfile: "dist/js/main.js",
footer: { js: "//# sourceURL=VCDMain" }
}),
createContext({
...NodeCommonOpts,
entryPoints: ["src/main/arrpcWorker.ts"],
outfile: "dist/js/arrpcWorker.js",
footer: { js: "//# sourceURL=VCDArrpcWorker" }
}),
createContext({
...NodeCommonOpts,
entryPoints: ["src/preload/index.ts"],

View file

@ -4,31 +4,62 @@
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
import Server from "arrpc";
import { resolve } from "path";
import { IpcEvents } from "shared/IpcEvents";
import { MessageChannel, Worker } from "worker_threads";
import { mainWin } from "./mainWindow";
import { Settings } from "./settings";
import { ArrpcEvent, ArrpcHostEvent } from "./utils/arrpcWorkerTypes";
let server: any;
let worker: any;
const inviteCodeRegex = /^(\w|-)+$/;
export async function initArRPC() {
if (server || !Settings.store.arRPC) return;
if (worker || !Settings.store.arRPC) return;
try {
server = await new Server();
server.on("activity", (data: any) => mainWin.webContents.send(IpcEvents.ARRPC_ACTIVITY, JSON.stringify(data)));
server.on("invite", (invite: string, callback: (valid: boolean) => void) => {
invite = String(invite);
if (!inviteCodeRegex.test(invite)) return callback(false);
const { port1: hostPort, port2: workerPort } = new MessageChannel();
worker = new Worker(resolve(__dirname, "./arrpcWorker.js"), {
workerData: {
workerPort
},
transferList: [workerPort]
});
hostPort.on("message", (e: ArrpcEvent) => {
switch (e.eventType) {
case IpcEvents.ARRPC_ACTIVITY: {
mainWin.webContents.send(IpcEvents.ARRPC_ACTIVITY, e.data);
break;
}
case "invite": {
const invite = String(e.data);
mainWin.webContents
// Safety: Result of JSON.stringify should always be safe to equal
// Also, just to be super super safe, invite is regex validated above
.executeJavaScript(`Vesktop.openInviteModal(${JSON.stringify(invite)})`)
.then(callback);
if (!inviteCodeRegex.test(invite)) {
const hostEvent: ArrpcHostEvent = {
eventType: "ack-invite",
data: false,
inviteId: e.inviteId
};
return hostPort.postMessage(hostEvent);
}
mainWin.webContents
// Safety: Result of JSON.stringify should always be safe to equal
// Also, just to be super super safe, invite is regex validated above
.executeJavaScript(`Vesktop.openInviteModal(${JSON.stringify(invite)})`)
.then(() => {
const hostEvent: ArrpcHostEvent = {
eventType: "ack-invite",
data: true,
inviteId: e.inviteId
};
hostPort.postMessage(hostEvent);
});
break;
}
}
});
} catch (e) {
console.error("Failed to start arRPC server", e);

42
src/main/arrpcWorker.ts Normal file
View file

@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: GPL-3.0
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
import Server from "arrpc";
import { IpcEvents } from "shared/IpcEvents";
import { MessagePort, workerData } from "worker_threads";
import { ArrpcEvent, ArrpcHostEvent } from "./utils/arrpcWorkerTypes";
let server: any;
type InviteCallback = (valid: boolean) => void;
let inviteCallbacks: Array<InviteCallback> = [];
(async function () {
const { workerPort }: { workerPort: MessagePort } = workerData;
server = await new Server();
server.on("activity", (data: any) => {
const event: ArrpcEvent = {
eventType: IpcEvents.ARRPC_ACTIVITY,
data: JSON.stringify(data)
};
workerPort.postMessage(event);
});
server.on("invite", (invite: string, callback: InviteCallback) => {
const event: ArrpcEvent = {
eventType: "invite",
data: invite,
inviteId: inviteCallbacks.push(callback) - 1
};
workerPort.postMessage(event);
});
workerPort.on("message", (e: ArrpcHostEvent) => {
inviteCallbacks[e.inviteId](e.data);
inviteCallbacks = [...inviteCallbacks.slice(0, e.inviteId), ...inviteCallbacks.slice(e.inviteId + 1)];
});
})();

View file

@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: GPL-3.0
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/
import { IpcEvents } from "shared/IpcEvents";
export type ArrpcEvent = ArrpcActivityEvent | ArrpcInviteEvent;
export interface ArrpcActivityEvent {
eventType: IpcEvents.ARRPC_ACTIVITY;
data: string;
}
export interface ArrpcInviteEvent {
eventType: "invite";
data: string;
inviteId: number;
}
export interface ArrpcHostEvent {
eventType: "ack-invite";
inviteId: number;
data: boolean;
}