Skip to content

Commit

Permalink
feat: add support for a payload in a CONNECT packet
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Oct 8, 2020
1 parent 9eb8561 commit 78f9fc2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export declare class Decoder extends Emitter {
* @return {Object} packet
*/
private decodeString;
private static isPayloadValid;
/**
* Deallocates a parser's resources
*/
Expand Down
20 changes: 17 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
*/
Expand Down
22 changes: 18 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion test/arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "/",
};
Expand Down
4 changes: 2 additions & 2 deletions test/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("parser", () => {

const packet = {
type: PacketType.BINARY_EVENT,
data: [data],
data: ["a", data],
id: 0,
nsp: "/",
};
Expand All @@ -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",
};
Expand Down
17 changes: 14 additions & 3 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe("parser", () => {
{
type: PacketType.CONNECT,
nsp: "/woot",
data: {
token: "123",
},
},
done
);
Expand Down Expand Up @@ -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$/
);
Expand Down

0 comments on commit 78f9fc2

Please sign in to comment.