Skip to content

Commit

Permalink
chore: bump socket.io-parser
Browse files Browse the repository at this point in the history
Breaking change:

- the encode() method is now synchronous

Please note that the exchange [protocol][1] is left untouched and thus
stays in version 4.

Diff: socketio/socket.io-parser@3.4.1...4.0.0

[1] https://github.com/socketio/socket.io-protocol
  • Loading branch information
darrachequesne committed Oct 5, 2020
1 parent cab895f commit 429846b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 38 deletions.
3 changes: 1 addition & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { url } from "./url";
import parser from "socket.io-parser";
import { Manager } from "./manager";
import { Socket } from "./socket";

Expand Down Expand Up @@ -72,7 +71,7 @@ function lookup(uri, opts): Socket {
* @api public
*/

exports.protocol = parser.protocol;
export { protocol } from "socket.io-parser";

/**
* `connect`.
Expand Down
27 changes: 13 additions & 14 deletions lib/manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import eio from "engine.io-client";
import { Socket } from "./socket";
import Emitter from "component-emitter";
import parser from "socket.io-parser";
import * as parser from "socket.io-parser";
import { Decoder, Encoder } from "socket.io-parser";
import { on } from "./on";
import bind from "component-bind";
import indexOf from "indexof";
Expand Down Expand Up @@ -36,8 +37,8 @@ export class Manager extends Emitter {
private lastPing: number = null;
private encoding: boolean;
private packetBuffer: Array<any> = [];
private encoder: any;
private decoder: any;
private encoder: Encoder;
private decoder: Decoder;
private engine: any;
private skipReconnect: boolean;

Expand Down Expand Up @@ -468,22 +469,20 @@ export class Manager extends Emitter {
*/
packet(packet) {
debug("writing packet %j", packet);
const self = this;
if (packet.query && packet.type === 0) packet.nsp += "?" + packet.query;

if (!self.encoding) {
if (!this.encoding) {
// encode, then write to engine with result
self.encoding = true;
this.encoder.encode(packet, function (encodedPackets) {
for (let i = 0; i < encodedPackets.length; i++) {
self.engine.write(encodedPackets[i], packet.options);
}
self.encoding = false;
self.processPacketQueue();
});
this.encoding = true;
const encodedPackets = this.encoder.encode(packet);
for (let i = 0; i < encodedPackets.length; i++) {
this.engine.write(encodedPackets[i], packet.options);
}
this.encoding = false;
this.processPacketQueue();
} else {
// add packet to the queue
self.packetBuffer.push(packet);
this.packetBuffer.push(packet);
}
}

Expand Down
32 changes: 16 additions & 16 deletions lib/socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parser from "socket.io-parser";
import { PacketType } from "socket.io-parser";
import Emitter from "component-emitter";
import toArray from "to-array";
import { on } from "./on";
Expand Down Expand Up @@ -142,8 +142,8 @@ export class Socket extends Emitter {
const args = toArray(arguments);
const packet: any = {
type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args))
? parser.BINARY_EVENT
: parser.EVENT,
? PacketType.BINARY_EVENT
: PacketType.EVENT,
data: args,
};

Expand Down Expand Up @@ -195,9 +195,9 @@ export class Socket extends Emitter {
? parseqs.encode(this.query)
: this.query;
debug("sending connect packet with query %s", query);
this.packet({ type: parser.CONNECT, query: query });
this.packet({ type: PacketType.CONNECT, query: query });
} else {
this.packet({ type: parser.CONNECT });
this.packet({ type: PacketType.CONNECT });
}
}
}
Expand Down Expand Up @@ -225,36 +225,36 @@ export class Socket extends Emitter {
onpacket(packet) {
const sameNamespace = packet.nsp === this.nsp;
const rootNamespaceError =
packet.type === parser.ERROR && packet.nsp === "/";
packet.type === PacketType.ERROR && packet.nsp === "/";

if (!sameNamespace && !rootNamespaceError) return;

switch (packet.type) {
case parser.CONNECT:
case PacketType.CONNECT:
this.onconnect();
break;

case parser.EVENT:
case PacketType.EVENT:
this.onevent(packet);
break;

case parser.BINARY_EVENT:
case PacketType.BINARY_EVENT:
this.onevent(packet);
break;

case parser.ACK:
case PacketType.ACK:
this.onack(packet);
break;

case parser.BINARY_ACK:
case PacketType.BINARY_ACK:
this.onack(packet);
break;

case parser.DISCONNECT:
case PacketType.DISCONNECT:
this.ondisconnect();
break;

case parser.ERROR:
case PacketType.ERROR:
super.emit("error", packet.data);
break;
}
Expand Down Expand Up @@ -298,7 +298,7 @@ export class Socket extends Emitter {
debug("sending ack %j", args);

self.packet({
type: hasBin(args) ? parser.BINARY_ACK : parser.ACK,
type: hasBin(args) ? PacketType.BINARY_ACK : PacketType.ACK,
id: id,
data: args,
});
Expand Down Expand Up @@ -390,7 +390,7 @@ export class Socket extends Emitter {
close() {
if (this.connected) {
debug("performing disconnect (%s)", this.nsp);
this.packet({ type: parser.DISCONNECT });
this.packet({ type: PacketType.DISCONNECT });
}

// remove socket from pool
Expand All @@ -406,7 +406,7 @@ export class Socket extends Emitter {
disconnect() {
if (this.connected) {
debug("performing disconnect (%s)", this.nsp);
this.packet({ type: parser.DISCONNECT });
this.packet({ type: PacketType.DISCONNECT });
}

// remove socket from pool
Expand Down
57 changes: 52 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"indexof": "0.0.1",
"parseqs": "0.0.6",
"parseuri": "0.0.6",
"socket.io-parser": "~3.3.0",
"socket.io-parser": "github:socketio/socket.io-parser#develop",
"to-array": "0.1.4"
},
"devDependencies": {
Expand Down

0 comments on commit 429846b

Please sign in to comment.