Skip to content

Commit 78f9fc2

Browse files
feat: add support for a payload in a CONNECT packet
1 parent 9eb8561 commit 78f9fc2

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export declare class Decoder extends Emitter {
6464
* @return {Object} packet
6565
*/
6666
private decodeString;
67+
private static isPayloadValid;
6768
/**
6869
* Deallocates a parser's resources
6970
*/

dist/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ class Decoder extends component_emitter_1.default {
195195
// look up json data
196196
if (str.charAt(++i)) {
197197
const payload = tryParse(str.substr(i));
198-
const isPayloadValid = payload !== false &&
199-
(p.type === PacketType.ERROR || Array.isArray(payload));
200-
if (isPayloadValid) {
198+
if (Decoder.isPayloadValid(p.type, payload)) {
201199
p.data = payload;
202200
}
203201
else {
@@ -207,6 +205,22 @@ class Decoder extends component_emitter_1.default {
207205
debug("decoded %s as %j", str, p);
208206
return p;
209207
}
208+
static isPayloadValid(type, payload) {
209+
switch (type) {
210+
case PacketType.CONNECT:
211+
return typeof payload === "object";
212+
case PacketType.DISCONNECT:
213+
return payload === undefined;
214+
case PacketType.ERROR:
215+
return typeof payload === "string";
216+
case PacketType.EVENT:
217+
case PacketType.BINARY_EVENT:
218+
return Array.isArray(payload) && typeof payload[0] === "string";
219+
case PacketType.ACK:
220+
case PacketType.BINARY_ACK:
221+
return Array.isArray(payload);
222+
}
223+
}
210224
/**
211225
* Deallocates a parser's resources
212226
*/

lib/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,7 @@ export class Decoder extends Emitter {
223223
// look up json data
224224
if (str.charAt(++i)) {
225225
const payload = tryParse(str.substr(i));
226-
const isPayloadValid =
227-
payload !== false &&
228-
(p.type === PacketType.ERROR || Array.isArray(payload));
229-
if (isPayloadValid) {
226+
if (Decoder.isPayloadValid(p.type, payload)) {
230227
p.data = payload;
231228
} else {
232229
throw new Error("invalid payload");
@@ -237,6 +234,23 @@ export class Decoder extends Emitter {
237234
return p;
238235
}
239236

237+
private static isPayloadValid(type: PacketType, payload: any): boolean {
238+
switch (type) {
239+
case PacketType.CONNECT:
240+
return typeof payload === "object";
241+
case PacketType.DISCONNECT:
242+
return payload === undefined;
243+
case PacketType.ERROR:
244+
return typeof payload === "string";
245+
case PacketType.EVENT:
246+
case PacketType.BINARY_EVENT:
247+
return Array.isArray(payload) && typeof payload[0] === "string";
248+
case PacketType.ACK:
249+
case PacketType.BINARY_ACK:
250+
return Array.isArray(payload);
251+
}
252+
}
253+
240254
/**
241255
* Deallocates a parser's resources
242256
*/

test/arraybuffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("parser", () => {
5757
it("cleans itself up on close", () => {
5858
const packet = {
5959
type: PacketType.BINARY_EVENT,
60-
data: [new ArrayBuffer(2), new ArrayBuffer(3)],
60+
data: ["a", new ArrayBuffer(2), new ArrayBuffer(3)],
6161
id: 0,
6262
nsp: "/",
6363
};

test/blob.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("parser", () => {
2525

2626
const packet = {
2727
type: PacketType.BINARY_EVENT,
28-
data: [data],
28+
data: ["a", data],
2929
id: 0,
3030
nsp: "/",
3131
};
@@ -44,7 +44,7 @@ describe("parser", () => {
4444

4545
const packet = {
4646
type: PacketType.BINARY_EVENT,
47-
data: [{ a: "hi", b: { why: data }, c: "bye" }],
47+
data: ["a", { a: "hi", b: { why: data }, c: "bye" }],
4848
id: 999,
4949
nsp: "/deep",
5050
};

test/parser.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ describe("parser", () => {
1818
{
1919
type: PacketType.CONNECT,
2020
nsp: "/woot",
21+
data: {
22+
token: "123",
23+
},
2124
},
2225
done
2326
);
@@ -105,9 +108,17 @@ describe("parser", () => {
105108
});
106109

107110
it("throw an error upon parsing error", () => {
108-
expect(() => new Decoder().add('442["some","data"')).to.throwException(
109-
/^invalid payload$/
110-
);
111+
const isInvalidPayload = (str) =>
112+
expect(() => new Decoder().add(str)).to.throwException(
113+
/^invalid payload$/
114+
);
115+
116+
isInvalidPayload('442["some","data"');
117+
isInvalidPayload('0/admin,"invalid"');
118+
isInvalidPayload("1/admin,{}");
119+
isInvalidPayload('2/admin,"invalid');
120+
isInvalidPayload("2/admin,{}");
121+
111122
expect(() => new Decoder().add("999")).to.throwException(
112123
/^unknown packet type 9$/
113124
);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy