Skip to content

Commit f46f316

Browse files
committed
feat: move arrpc server into a worker thread
1 parent 5a72491 commit f46f316

File tree

4 files changed

+119
-14
lines changed

4 files changed

+119
-14
lines changed

Diff for: scripts/build/build.mts

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ await Promise.all([
5757
outfile: "dist/js/main.js",
5858
footer: { js: "//# sourceURL=VCDMain" }
5959
}),
60+
createContext({
61+
...NodeCommonOpts,
62+
entryPoints: ["src/main/arrpcWorker.ts"],
63+
outfile: "dist/js/arrpcWorker.js",
64+
footer: { js: "//# sourceURL=VCDArrpcWorker" }
65+
}),
6066
createContext({
6167
...NodeCommonOpts,
6268
entryPoints: ["src/preload/index.ts"],

Diff for: src/main/arrpc.ts

+45-14
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,62 @@
44
* Copyright (c) 2023 Vendicated and Vencord contributors
55
*/
66

7-
import Server from "arrpc";
7+
import { resolve } from "path";
88
import { IpcEvents } from "shared/IpcEvents";
9+
import { MessageChannel, Worker } from "worker_threads";
910

1011
import { mainWin } from "./mainWindow";
1112
import { Settings } from "./settings";
13+
import { ArrpcEvent, ArrpcHostEvent } from "./utils/arrpcWorkerTypes";
1214

13-
let server: any;
15+
let worker: any;
1416

1517
const inviteCodeRegex = /^(\w|-)+$/;
1618

1719
export async function initArRPC() {
18-
if (server || !Settings.store.arRPC) return;
20+
if (worker || !Settings.store.arRPC) return;
1921

2022
try {
21-
server = await new Server();
22-
server.on("activity", (data: any) => mainWin.webContents.send(IpcEvents.ARRPC_ACTIVITY, JSON.stringify(data)));
23-
server.on("invite", (invite: string, callback: (valid: boolean) => void) => {
24-
invite = String(invite);
25-
if (!inviteCodeRegex.test(invite)) return callback(false);
26-
27-
mainWin.webContents
28-
// Safety: Result of JSON.stringify should always be safe to equal
29-
// Also, just to be super super safe, invite is regex validated above
30-
.executeJavaScript(`Vesktop.openInviteModal(${JSON.stringify(invite)})`)
31-
.then(callback);
23+
const { port1: hostPort, port2: workerPort } = new MessageChannel();
24+
worker = new Worker(resolve(__dirname, "./arrpcWorker.js"), {
25+
workerData: {
26+
workerPort
27+
},
28+
transferList: [workerPort]
29+
});
30+
hostPort.on("message", (e: ArrpcEvent) => {
31+
switch (e.eventType) {
32+
case IpcEvents.ARRPC_ACTIVITY: {
33+
mainWin.webContents.send(IpcEvents.ARRPC_ACTIVITY, e.data);
34+
break;
35+
}
36+
case "invite": {
37+
const invite = String(e.data);
38+
39+
if (!inviteCodeRegex.test(invite)) {
40+
const hostEvent: ArrpcHostEvent = {
41+
eventType: "ack-invite",
42+
data: false,
43+
inviteId: e.inviteId
44+
};
45+
return hostPort.postMessage(hostEvent);
46+
}
47+
48+
mainWin.webContents
49+
// Safety: Result of JSON.stringify should always be safe to equal
50+
// Also, just to be super super safe, invite is regex validated above
51+
.executeJavaScript(`Vesktop.openInviteModal(${JSON.stringify(invite)})`)
52+
.then(() => {
53+
const hostEvent: ArrpcHostEvent = {
54+
eventType: "ack-invite",
55+
data: true,
56+
inviteId: e.inviteId
57+
};
58+
hostPort.postMessage(hostEvent);
59+
});
60+
break;
61+
}
62+
}
3263
});
3364
} catch (e) {
3465
console.error("Failed to start arRPC server", e);

Diff for: src/main/arrpcWorker.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0
3+
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
4+
* Copyright (c) 2023 Vendicated and Vencord contributors
5+
*/
6+
7+
import Server from "arrpc";
8+
import { IpcEvents } from "shared/IpcEvents";
9+
import { MessagePort, workerData } from "worker_threads";
10+
11+
import { ArrpcEvent, ArrpcHostEvent } from "./utils/arrpcWorkerTypes";
12+
13+
let server: any;
14+
15+
type InviteCallback = (valid: boolean) => void;
16+
17+
let inviteCallbacks: Array<InviteCallback> = [];
18+
19+
(async function () {
20+
const { workerPort }: { workerPort: MessagePort } = workerData;
21+
server = await new Server();
22+
server.on("activity", (data: any) => {
23+
const event: ArrpcEvent = {
24+
eventType: IpcEvents.ARRPC_ACTIVITY,
25+
data: JSON.stringify(data)
26+
};
27+
workerPort.postMessage(event);
28+
});
29+
server.on("invite", (invite: string, callback: InviteCallback) => {
30+
const event: ArrpcEvent = {
31+
eventType: "invite",
32+
data: invite,
33+
inviteId: inviteCallbacks.push(callback) - 1
34+
};
35+
workerPort.postMessage(event);
36+
});
37+
38+
workerPort.on("message", (e: ArrpcHostEvent) => {
39+
inviteCallbacks[e.inviteId](e.data);
40+
inviteCallbacks = [...inviteCallbacks.slice(0, e.inviteId), ...inviteCallbacks.slice(e.inviteId + 1)];
41+
});
42+
})();

Diff for: src/main/utils/arrpcWorkerTypes.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0
3+
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
4+
* Copyright (c) 2023 Vendicated and Vencord contributors
5+
*/
6+
7+
import { IpcEvents } from "shared/IpcEvents";
8+
9+
export type ArrpcEvent = ArrpcActivityEvent | ArrpcInviteEvent;
10+
11+
export interface ArrpcActivityEvent {
12+
eventType: IpcEvents.ARRPC_ACTIVITY;
13+
data: string;
14+
}
15+
16+
export interface ArrpcInviteEvent {
17+
eventType: "invite";
18+
data: string;
19+
inviteId: number;
20+
}
21+
22+
export interface ArrpcHostEvent {
23+
eventType: "ack-invite";
24+
inviteId: number;
25+
data: boolean;
26+
}

0 commit comments

Comments
 (0)