Skip to content

Commit b35733d

Browse files
refactor: make Parser.encodePacket() synchronous
1 parent cb19163 commit b35733d

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

lib/parser.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ const ERROR_PACKET: Packet = { type: "error", data: "parser error" };
4040
type BinaryType = "arraybuffer" | "blob";
4141

4242
export const Parser = {
43-
encodePacket(
44-
{ type, data }: Packet,
45-
supportsBinary: boolean,
46-
callback: (encodedPacket: RawData) => void,
47-
) {
43+
encodePacket({ type, data }: Packet, supportsBinary: boolean): RawData {
4844
if (Buffer.isBuffer(data)) {
49-
return callback(supportsBinary ? data : "b" + data.toString("base64"));
45+
return supportsBinary ? data : "b" + data.toString("base64");
5046
} else {
51-
return callback(PACKET_TYPES.get(type) + (data || ""));
47+
return PACKET_TYPES.get(type) + (data || "");
5248
}
5349
},
5450

@@ -81,21 +77,14 @@ export const Parser = {
8177
};
8278
},
8379

84-
encodePayload(packets: Packet[], callback: (encodedPayload: string) => void) {
85-
// some packets may be added to the array while encoding, so the initial length must be saved
86-
const length = packets.length;
87-
const encodedPackets = new Array(length);
88-
let count = 0;
80+
encodePayload(packets: Packet[]) {
81+
const encodedPackets = [];
8982

90-
packets.forEach((packet, i) => {
91-
// force base64 encoding for binary packets
92-
this.encodePacket(packet, false, (encodedPacket) => {
93-
encodedPackets[i] = encodedPacket;
94-
if (++count === length) {
95-
callback(encodedPackets.join(SEPARATOR));
96-
}
97-
});
98-
});
83+
for (const packet of packets) {
84+
encodedPackets.push(this.encodePacket(packet, false));
85+
}
86+
87+
return encodedPackets.join(SEPARATOR);
9988
},
10089

10190
decodePayload(encodedPayload: string, binaryType?: BinaryType): Packet[] {

lib/transports/polling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class Polling extends Transport {
107107

108108
public send(packets: Packet[]) {
109109
this.writable = false;
110-
Parser.encodePayload(packets, (data: string) => this.write(data));
110+
this.write(Parser.encodePayload(packets));
111111
}
112112

113113
/**

lib/transports/websocket.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ export class WS extends Transport {
2121

2222
public send(packets: Packet[]) {
2323
for (const packet of packets) {
24-
Parser.encodePacket(packet, true, (data: RawData) => {
25-
if (this.writable && this.socket?.readyState === WebSocket.OPEN) {
26-
this.socket.send(data);
27-
}
28-
});
24+
const data = Parser.encodePacket(packet, true);
25+
if (this.writable && this.socket?.readyState === WebSocket.OPEN) {
26+
this.socket.send(data);
27+
}
2928
}
3029
}
3130

0 commit comments

Comments
 (0)