From 78f9fc2999b15804b02f2c22a2b4007734a26af9 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 7 Oct 2020 18:36:33 +0200 Subject: [PATCH] feat: add support for a payload in a CONNECT packet --- dist/index.d.ts | 1 + dist/index.js | 20 +++++++++++++++++--- lib/index.ts | 22 ++++++++++++++++++---- test/arraybuffer.js | 2 +- test/blob.js | 4 ++-- test/parser.js | 17 ++++++++++++++--- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 0b88a8a..c2e4753 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -64,6 +64,7 @@ export declare class Decoder extends Emitter { * @return {Object} packet */ private decodeString; + private static isPayloadValid; /** * Deallocates a parser's resources */ diff --git a/dist/index.js b/dist/index.js index b7b716b..69cf092 100644 --- a/dist/index.js +++ b/dist/index.js @@ -195,9 +195,7 @@ class Decoder extends component_emitter_1.default { // look up json data if (str.charAt(++i)) { const payload = tryParse(str.substr(i)); - const isPayloadValid = payload !== false && - (p.type === PacketType.ERROR || Array.isArray(payload)); - if (isPayloadValid) { + if (Decoder.isPayloadValid(p.type, payload)) { p.data = payload; } else { @@ -207,6 +205,22 @@ class Decoder extends component_emitter_1.default { debug("decoded %s as %j", str, p); return p; } + static isPayloadValid(type, payload) { + switch (type) { + case PacketType.CONNECT: + return typeof payload === "object"; + case PacketType.DISCONNECT: + return payload === undefined; + case PacketType.ERROR: + return typeof payload === "string"; + case PacketType.EVENT: + case PacketType.BINARY_EVENT: + return Array.isArray(payload) && typeof payload[0] === "string"; + case PacketType.ACK: + case PacketType.BINARY_ACK: + return Array.isArray(payload); + } + } /** * Deallocates a parser's resources */ diff --git a/lib/index.ts b/lib/index.ts index a9c9043..ff5671e 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -223,10 +223,7 @@ export class Decoder extends Emitter { // look up json data if (str.charAt(++i)) { const payload = tryParse(str.substr(i)); - const isPayloadValid = - payload !== false && - (p.type === PacketType.ERROR || Array.isArray(payload)); - if (isPayloadValid) { + if (Decoder.isPayloadValid(p.type, payload)) { p.data = payload; } else { throw new Error("invalid payload"); @@ -237,6 +234,23 @@ export class Decoder extends Emitter { return p; } + private static isPayloadValid(type: PacketType, payload: any): boolean { + switch (type) { + case PacketType.CONNECT: + return typeof payload === "object"; + case PacketType.DISCONNECT: + return payload === undefined; + case PacketType.ERROR: + return typeof payload === "string"; + case PacketType.EVENT: + case PacketType.BINARY_EVENT: + return Array.isArray(payload) && typeof payload[0] === "string"; + case PacketType.ACK: + case PacketType.BINARY_ACK: + return Array.isArray(payload); + } + } + /** * Deallocates a parser's resources */ diff --git a/test/arraybuffer.js b/test/arraybuffer.js index b5df85e..14ef58c 100644 --- a/test/arraybuffer.js +++ b/test/arraybuffer.js @@ -57,7 +57,7 @@ describe("parser", () => { it("cleans itself up on close", () => { const packet = { type: PacketType.BINARY_EVENT, - data: [new ArrayBuffer(2), new ArrayBuffer(3)], + data: ["a", new ArrayBuffer(2), new ArrayBuffer(3)], id: 0, nsp: "/", }; diff --git a/test/blob.js b/test/blob.js index 43ef584..559d7ef 100644 --- a/test/blob.js +++ b/test/blob.js @@ -25,7 +25,7 @@ describe("parser", () => { const packet = { type: PacketType.BINARY_EVENT, - data: [data], + data: ["a", data], id: 0, nsp: "/", }; @@ -44,7 +44,7 @@ describe("parser", () => { const packet = { type: PacketType.BINARY_EVENT, - data: [{ a: "hi", b: { why: data }, c: "bye" }], + data: ["a", { a: "hi", b: { why: data }, c: "bye" }], id: 999, nsp: "/deep", }; diff --git a/test/parser.js b/test/parser.js index f2bd2e9..34d5002 100644 --- a/test/parser.js +++ b/test/parser.js @@ -18,6 +18,9 @@ describe("parser", () => { { type: PacketType.CONNECT, nsp: "/woot", + data: { + token: "123", + }, }, done ); @@ -105,9 +108,17 @@ describe("parser", () => { }); it("throw an error upon parsing error", () => { - expect(() => new Decoder().add('442["some","data"')).to.throwException( - /^invalid payload$/ - ); + const isInvalidPayload = (str) => + expect(() => new Decoder().add(str)).to.throwException( + /^invalid payload$/ + ); + + isInvalidPayload('442["some","data"'); + isInvalidPayload('0/admin,"invalid"'); + isInvalidPayload("1/admin,{}"); + isInvalidPayload('2/admin,"invalid'); + isInvalidPayload("2/admin,{}"); + expect(() => new Decoder().add("999")).to.throwException( /^unknown packet type 9$/ );