Skip to content

Commit 60a157e

Browse files
committed
refactor(transports/ipc): cleaner decode function
1 parent 35f801c commit 60a157e

File tree

1 file changed

+86
-87
lines changed

1 file changed

+86
-87
lines changed

src/transports/ipc.js

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,91 +14,86 @@ const OPCodes = {
1414
PONG: 4,
1515
};
1616

17-
function getIPCPath(id) {
17+
const getIPCPath = (id) => {
1818
if (process.platform === 'win32') {
1919
return `\\\\?\\pipe\\discord-ipc-${id}`;
2020
}
2121
const { env: { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } } = process;
2222
const prefix = XDG_RUNTIME_DIR || TMPDIR || TMP || TEMP || '/tmp';
2323
return `${prefix.replace(/\/$/, '')}/discord-ipc-${id}`;
24-
}
24+
};
2525

26-
function getIPC(id = 0) {
27-
return new Promise((resolve, reject) => {
28-
const path = getIPCPath(id);
29-
const onError = () => {
30-
if (id < 10) {
31-
resolve(getIPC(id + 1));
32-
} else {
33-
reject(new Error('COULD_NOT_CONNECT'));
34-
}
35-
};
36-
const socket = net.createConnection(path, () => {
37-
socket.removeListener('error', onError);
38-
resolve(socket);
39-
});
40-
socket.once('error', onError);
26+
const getIPC = (id = 0) => new Promise((resolve, reject) => {
27+
const path = getIPCPath(id);
28+
const onError = () => {
29+
if (id < 10) {
30+
resolve(getIPC(id + 1));
31+
} else {
32+
reject(new Error('COULD_NOT_CONNECT'));
33+
}
34+
};
35+
const socket = net.createConnection(path, () => {
36+
socket.removeListener('error', onError);
37+
resolve(socket);
4138
});
42-
}
39+
socket.once('error', onError);
40+
});
4341

44-
async function findEndpoint(tries = 0) {
42+
const findEndpoint = async (tries = 0) => {
4543
if (tries > 30) {
4644
throw new Error('COULD_NOT_FIND_ENDPOINT');
4745
}
4846
const endpoint = `http://127.0.0.1:${6463 + (tries % 10)}`;
4947
try {
50-
const r = await fetch(endpoint);
51-
if (r.status === 404) {
48+
const response = await fetch(endpoint);
49+
if (response.status === 404) {
5250
return endpoint;
5351
}
54-
return findEndpoint(tries + 1);
55-
} catch (e) {
56-
return findEndpoint(tries + 1);
57-
}
58-
}
52+
} catch { } // eslint-disable-line no-empty
53+
return findEndpoint(tries + 1);
54+
};
5955

60-
function encode(op, data) {
56+
const encode = (op, data) => {
6157
data = JSON.stringify(data);
62-
const len = Buffer.byteLength(data);
63-
const packet = Buffer.alloc(8 + len);
58+
const length = Buffer.byteLength(data);
59+
const packet = Buffer.alloc(length + 8);
6460
packet.writeInt32LE(op, 0);
65-
packet.writeInt32LE(len, 4);
66-
packet.write(data, 8, len);
61+
packet.writeInt32LE(length, 4);
62+
packet.write(data, 8, length);
6763
return packet;
68-
}
69-
70-
const working = {
71-
full: '',
72-
op: undefined,
7364
};
7465

75-
function decode(socket, callback) {
76-
const packet = socket.read();
77-
if (!packet) {
78-
return;
79-
}
66+
const decode = (socket) => {
67+
let op;
68+
let jsonString = '';
8069

81-
let { op } = working;
82-
let raw;
83-
if (working.full === '') {
84-
op = working.op = packet.readInt32LE(0);
85-
const len = packet.readInt32LE(4);
86-
raw = packet.slice(8, len + 8);
87-
} else {
88-
raw = packet.toString();
89-
}
70+
const read = () => {
71+
const packet = socket.read();
72+
if (!packet) {
73+
return null;
74+
}
75+
let part;
76+
77+
if (jsonString === '') {
78+
op = packet.readInt32LE(0);
79+
const length = packet.readInt32LE(4);
80+
part = packet.slice(8, length + 8);
81+
} else {
82+
part = packet.toString();
83+
}
9084

91-
try {
92-
const data = JSON.parse(working.full + raw);
93-
callback({ op, data }); // eslint-disable-line callback-return
94-
working.full = '';
95-
working.op = undefined;
96-
} catch (err) {
97-
working.full += raw;
98-
}
85+
jsonString += part;
9986

100-
decode(socket, callback);
101-
}
87+
try {
88+
const data = JSON.parse(jsonString);
89+
return { data, op };
90+
} catch {
91+
return read();
92+
}
93+
};
94+
95+
return read();
96+
};
10297

10398
class IPCTransport extends EventEmitter {
10499
constructor(client) {
@@ -109,6 +104,7 @@ class IPCTransport extends EventEmitter {
109104

110105
async connect() {
111106
const socket = this.socket = await getIPC();
107+
112108
socket.on('close', this.onClose.bind(this));
113109
socket.on('error', this.onClose.bind(this));
114110
this.emit('open');
@@ -118,33 +114,36 @@ class IPCTransport extends EventEmitter {
118114
}));
119115
socket.pause();
120116
socket.on('readable', () => {
121-
decode(socket, ({ op, data }) => {
122-
switch (op) {
123-
case OPCodes.PING:
124-
this.send(data, OPCodes.PONG);
125-
break;
126-
case OPCodes.FRAME:
127-
if (!data) {
128-
return;
129-
}
130-
if (data.cmd === 'AUTHORIZE' && data.evt !== 'ERROR') {
131-
findEndpoint()
132-
.then((endpoint) => {
133-
this.client.request.endpoint = endpoint;
134-
})
135-
.catch((error) => {
136-
this.client.emit('error', error);
137-
});
138-
}
139-
this.emit('message', data);
140-
break;
141-
case OPCodes.CLOSE:
142-
this.emit('close', data);
143-
break;
144-
default:
145-
break;
146-
}
147-
});
117+
const decoded = decode(socket);
118+
if (!decoded) {
119+
return;
120+
}
121+
const { data, op } = decoded;
122+
switch (op) {
123+
case OPCodes.PING:
124+
this.send(data, OPCodes.PONG);
125+
break;
126+
case OPCodes.FRAME:
127+
if (!data) {
128+
return;
129+
}
130+
if (data.cmd === 'AUTHORIZE' && data.evt !== 'ERROR') {
131+
findEndpoint()
132+
.then((endpoint) => {
133+
this.client.request.endpoint = endpoint;
134+
})
135+
.catch((error) => {
136+
this.client.emit('error', error);
137+
});
138+
}
139+
this.emit('message', data);
140+
break;
141+
case OPCodes.CLOSE:
142+
this.emit('close', data);
143+
break;
144+
default:
145+
break;
146+
}
148147
});
149148
}
150149

0 commit comments

Comments
 (0)