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. But I feel the
name makes more sense:

```js
socket.on("connect", () => {});
socket.on("connect_error", () => {});

// instead of
socket.on("error", () => {});
```
  • Loading branch information
darrachequesne committed Oct 25, 2020
1 parent e3d272f commit db1d274
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/index.ts
Expand Up @@ -17,7 +17,7 @@ export enum PacketType {
DISCONNECT,
EVENT,
ACK,
ERROR,
CONNECT_ERROR,
BINARY_EVENT,
BINARY_ACK,
}
Expand Down Expand Up @@ -241,8 +241,8 @@ export class Decoder extends Emitter {
return typeof payload === "object";
case PacketType.DISCONNECT:
return payload === undefined;
case PacketType.ERROR:
return typeof payload === "string";
case PacketType.CONNECT_ERROR:
return typeof payload === "string" || typeof payload === "object";
case PacketType.EVENT:
case PacketType.BINARY_EVENT:
return Array.isArray(payload) && typeof payload[0] === "string";
Expand Down
19 changes: 16 additions & 3 deletions test/parser.js
Expand Up @@ -8,7 +8,7 @@ describe("parser", () => {
expect(PacketType.DISCONNECT).to.be.a("number");
expect(PacketType.EVENT).to.be.a("number");
expect(PacketType.ACK).to.be.a("number");
expect(PacketType.ERROR).to.be.a("number");
expect(PacketType.CONNECT_ERROR).to.be.a("number");
expect(PacketType.BINARY_EVENT).to.be.a("number");
expect(PacketType.BINARY_ACK).to.be.a("number");
});
Expand Down Expand Up @@ -71,17 +71,30 @@ describe("parser", () => {
);
});

it("encodes an error", (done) => {
it("encodes an connect error", (done) => {
helpers.test(
{
type: PacketType.ERROR,
type: PacketType.CONNECT_ERROR,
data: "Unauthorized",
nsp: "/",
},
done
);
});

it("encodes an connect error (with object)", (done) => {
helpers.test(
{
type: PacketType.CONNECT_ERROR,
data: {
message: "Unauthorized",
},
nsp: "/",
},
done
);
});

it("throws an error when encoding circular objects", () => {
const a = {};
a.b = a;
Expand Down

0 comments on commit db1d274

Please sign in to comment.