Skip to content

Commit

Permalink
refactor: rename ERROR to CONNECT_ERROR
Browse files Browse the repository at this point in the history
The meaning is not modified: this packet type is still used by the
server when the connection to a namespace is refused.

Breaking change: the Socket instance will now emit a "connect_error"
event instead of "error" (which is not a reserved event anymore)

```js
// before
socket.on("error", () => {});

// after
socket.on("connect_error", () => {});
```
  • Loading branch information
darrachequesne committed Oct 26, 2020
1 parent 55f464f commit 13e1db7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
10 changes: 4 additions & 6 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export interface SocketOptions {

const RESERVED_EVENTS = {
connect: 1,
connect_error: 1,
disconnect: 1,
disconnecting: 1,
error: 1,
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
newListener: 1,
removeListener: 1,
Expand Down Expand Up @@ -220,10 +220,8 @@ export class Socket extends Emitter {
*/
private onpacket(packet) {
const sameNamespace = packet.nsp === this.nsp;
const rootNamespaceError =
packet.type === PacketType.ERROR && packet.nsp === "/";

if (!sameNamespace && !rootNamespaceError) return;
if (!sameNamespace) return;

switch (packet.type) {
case PacketType.CONNECT:
Expand Down Expand Up @@ -251,8 +249,8 @@ export class Socket extends Emitter {
this.ondisconnect();
break;

case PacketType.ERROR:
super.emit("error", packet.data);
case PacketType.CONNECT_ERROR:
super.emit("connect_error", packet.data);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"debug": "~4.1.0",
"engine.io-client": "~4.0.0",
"parseuri": "0.0.6",
"socket.io-parser": "4.0.1-rc2"
"socket.io-parser": "4.0.1-rc3"
},
"devDependencies": {
"@babel/core": "^7.11.6",
Expand Down
2 changes: 1 addition & 1 deletion test/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe("socket", function () {

it("should fire an error event on middleware failure from custom namespace", (done) => {
const socket = io("/no", { forceNew: true });
socket.on("error", (err) => {
socket.on("connect_error", (err) => {
expect(err).to.eql("Auth failed (custom namespace)");
socket.disconnect();
done();
Expand Down

0 comments on commit 13e1db7

Please sign in to comment.