Skip to content

Commit

Permalink
feat: Implemented 417ddd53d9d26cad23123a2a359905a557150ade
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Jul 29, 2022
1 parent 9d55319 commit 28e4273
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"license": "ISC",
"types": "dist/index.d.ts",
"contributors": [
"discord.js - discordjs/rpc"
"discord.js - discordjs/rpc",
"JakeMakesStuff - snap support"
],
"scripts": {
"build": "rimraf dist && npx tsc",
Expand Down
12 changes: 8 additions & 4 deletions src/transport/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ HANDSHAKE 40bytes { " v " : 1 , "
///////////////////////////////////////////////////////////////////////////////////////////////////

export type IPCTransportOptions = {
formatPathFunction?: (id: number) => string;
formatPathFunction?: (id: number, snap?: boolean) => string;
}

export const createPacket = (opcode: OPCODE, data?: any): Buffer => {
Expand All @@ -45,12 +45,12 @@ export const parsePacket = (packet: Buffer): { op: OPCODE, length: number, data?
return { op, length, data };
}

const formatPath = (id: number): string => {
const formatPath = (id: number, snap: boolean = false): string => {
if (process.platform === 'win32') return `\\\\?\\pipe\\discord-ipc-${id}`;

const { env: { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } } = process;
const prefix = fs.realpathSync(XDG_RUNTIME_DIR || TMPDIR || TMP || TEMP || `${path.sep}tmp`);
return `${prefix}${path.sep}discord-ipc-${id}`;
return `${prefix}${snap ? `${path.sep}snap.discord` : ""}${path.sep}discord-ipc-${id}`;
}

const createSocket = async (path: string): Promise<net.Socket> => {
Expand Down Expand Up @@ -88,11 +88,15 @@ export class IPCTransport extends Transport {
const formatFunc = this.options.formatPathFunction || formatPath;
return new Promise(async (resolve, reject) => {
for (let i = 0; i < 10; i++) {
const socket = await createSocket(formatFunc(i)).catch(() => null);
let socket = await createSocket(formatFunc(i)).catch(() => null);

if (socket) {
resolve(socket);
return;
} else if (process.platform === 'linux') {
socket = await createSocket(formatFunc(i, true)).catch(() => null);
if (socket) resolve(socket);
return;
}
}

Expand Down

0 comments on commit 28e4273

Please sign in to comment.