Skip to content

Commit

Permalink
fix: exists doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Oct 24, 2022
1 parent 6f7e5cd commit 0f816de
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions src/transport/IPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export enum IPC_OPCODE {
}

export type FormatFunction = (
id: number,
id: number
) => [path: string, skipCheck?: boolean];

export type IPCTransportOptions = {
Expand All @@ -39,7 +39,7 @@ const defaultPathList: FormatFunction[] = [
const { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } = Deno.env.toObject();

const prefix = Deno.realPathSync(
XDG_RUNTIME_DIR ?? TMPDIR ?? TMP ?? TEMP ?? `${path.sep}tmp`,
XDG_RUNTIME_DIR ?? TMPDIR ?? TMP ?? TEMP ?? `${path.sep}tmp`
);
return [path.join(prefix, `discord-ipc-${id}`)];
},
Expand All @@ -51,7 +51,7 @@ const defaultPathList: FormatFunction[] = [
const { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } = Deno.env.toObject();

const prefix = Deno.realPathSync(
XDG_RUNTIME_DIR ?? TMPDIR ?? TMP ?? TEMP ?? `${path.sep}tmp`,
XDG_RUNTIME_DIR ?? TMPDIR ?? TMP ?? TEMP ?? `${path.sep}tmp`
);
return [path.join(prefix, "snap.discord", `discord-ipc-${id}`)];
},
Expand All @@ -63,7 +63,7 @@ const defaultPathList: FormatFunction[] = [
const { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } = Deno.env.toObject();

const prefix = Deno.realPathSync(
XDG_RUNTIME_DIR ?? TMPDIR ?? TMP ?? TEMP ?? `${path.sep}tmp`,
XDG_RUNTIME_DIR ?? TMPDIR ?? TMP ?? TEMP ?? `${path.sep}tmp`
);
return [
path.join(prefix, "app", "com.discordapp.Discord", `discord-ipc-${id}`),
Expand Down Expand Up @@ -92,16 +92,14 @@ const createSocket = async (path: string): Promise<net.Socket> => {

// https://stackoverflow.com/a/61868755
const exists = async (filename: string): Promise<boolean> => {
try {
await Deno.stat(filename);
return true;
} catch (error) {
if (error && error.kind === Deno.errors.NotFound) {
return false;
} else {
throw error;
}
}
return new Promise((resolve, reject) => {
Deno.stat(filename)
.then(() => resolve(true))
.catch((err) => {
if (err && err.kind === Deno.errors.NotFound) return resolve(false);
reject(err);
});
});
};

export class IPCTransport extends Transport {
Expand Down Expand Up @@ -131,7 +129,7 @@ export class IPCTransport extends Transport {
};

const handleSocketId = async (
id: number,
id: number
): Promise<net.Socket | undefined> => {
const [socketPath, skipCheck] = formatFunc(id);

Expand Down Expand Up @@ -163,8 +161,8 @@ export class IPCTransport extends Transport {
reject(
new RPCError(
CUSTOM_RPC_ERROR_CODE.RPC_COULD_NOT_CONNECT,
"Could not connect",
),
"Could not connect"
)
);
});
}
Expand All @@ -183,35 +181,31 @@ export class IPCTransport extends Transport {
v: 1,
client_id: this.client.clientId,
},
IPC_OPCODE.HANDSHAKE,
IPC_OPCODE.HANDSHAKE
);

this.socket.on("readable", () => {
let data = this.socket?.read() as Buffer | undefined;
if (!data) return;
this.client.emit(
"debug",
`SERVER => CLIENT | ${
data
.toString("hex")
.match(/.{1,2}/g)
?.join(" ")
.toUpperCase()
}`,
`SERVER => CLIENT | ${data
.toString("hex")
.match(/.{1,2}/g)
?.join(" ")
.toUpperCase()}`
);

do {
const chunk = this.socket?.read() as Buffer | undefined;
if (!chunk) break;
this.client.emit(
"debug",
`SERVER => CLIENT | ${
chunk
.toString("hex")
.match(/.{1,2}/g)
?.join(" ")
.toUpperCase()
}`,
`SERVER => CLIENT | ${chunk
.toString("hex")
.match(/.{1,2}/g)
?.join(" ")
.toUpperCase()}`
);
data = Buffer.concat([data, chunk]);
} while (true);
Expand All @@ -223,7 +217,7 @@ export class IPCTransport extends Transport {
this.client.emit(
"debug",
`SERVER => CLIENT | OPCODE.${IPC_OPCODE[op]} |`,
parsedData,
parsedData
);

switch (op) {
Expand All @@ -249,13 +243,11 @@ export class IPCTransport extends Transport {
send(message?: any, op: IPC_OPCODE = IPC_OPCODE.FRAME): void {
this.client.emit(
"debug",
`| [CLIENT] => [SERVER] | OPCODE.${IPC_OPCODE[op]} | ${
JSON.stringify(
message,
null,
2,
)
}`,
`| [CLIENT] => [SERVER] | OPCODE.${IPC_OPCODE[op]} | ${JSON.stringify(
message,
null,
2
)}`
);

const dataBuffer = message
Expand Down

0 comments on commit 0f816de

Please sign in to comment.