Skip to content

Commit ad5bd7d

Browse files
refactor: add additional types
Merged from socketio/engine.io#630
1 parent 25a4b2b commit ad5bd7d

9 files changed

+126
-37
lines changed

lib/commons.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ Object.keys(PACKET_TYPES).forEach(key => {
1212
PACKET_TYPES_REVERSE[PACKET_TYPES[key]] = key;
1313
});
1414

15-
const ERROR_PACKET = { type: "error", data: "parser error" };
15+
const ERROR_PACKET: Packet = { type: "error", data: "parser error" };
1616

1717
export { PACKET_TYPES, PACKET_TYPES_REVERSE, ERROR_PACKET };
18+
19+
export type PacketType =
20+
| "open"
21+
| "close"
22+
| "ping"
23+
| "pong"
24+
| "message"
25+
| "upgrade"
26+
| "noop"
27+
| "error";
28+
29+
export type RawData = string | Buffer | ArrayBuffer | ArrayBufferView | Blob;
30+
31+
export interface Packet {
32+
type: PacketType;
33+
options?: { compress: boolean };
34+
data?: RawData;
35+
}
36+
37+
export type BinaryType = "nodebuffer" | "arraybuffer" | "blob";

lib/decodePacket.browser.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { ERROR_PACKET, PACKET_TYPES_REVERSE } from "./commons.js";
1+
import {
2+
ERROR_PACKET,
3+
PACKET_TYPES_REVERSE,
4+
Packet,
5+
BinaryType,
6+
RawData
7+
} from "./commons.js";
28
import { decode } from "@socket.io/base64-arraybuffer";
39

410
const withNativeArrayBuffer = typeof ArrayBuffer === "function";
511

6-
const decodePacket = (encodedPacket, binaryType) => {
12+
const decodePacket = (
13+
encodedPacket: RawData,
14+
binaryType?: BinaryType
15+
): Packet => {
716
if (typeof encodedPacket !== "string") {
817
return {
918
type: "message",

lib/decodePacket.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import { ERROR_PACKET, PACKET_TYPES_REVERSE } from "./commons.js";
1+
import {
2+
ERROR_PACKET,
3+
PACKET_TYPES_REVERSE,
4+
Packet,
5+
BinaryType,
6+
RawData
7+
} from "./commons.js";
28

3-
const decodePacket = (encodedPacket, binaryType?) => {
9+
const decodePacket = (
10+
encodedPacket: RawData,
11+
binaryType?: BinaryType
12+
): Packet => {
413
if (typeof encodedPacket !== "string") {
514
return {
615
type: "message",
@@ -28,7 +37,7 @@ const decodePacket = (encodedPacket, binaryType?) => {
2837
};
2938
};
3039

31-
const mapBinary = (data, binaryType) => {
40+
const mapBinary = (data: RawData, binaryType?: BinaryType) => {
3241
const isBuffer = Buffer.isBuffer(data);
3342
switch (binaryType) {
3443
case "arraybuffer":
@@ -39,7 +48,7 @@ const mapBinary = (data, binaryType) => {
3948
}
4049
};
4150

42-
const toArrayBuffer = buffer => {
51+
const toArrayBuffer = (buffer: Buffer): ArrayBuffer => {
4352
const arrayBuffer = new ArrayBuffer(buffer.length);
4453
const view = new Uint8Array(arrayBuffer);
4554
for (let i = 0; i < buffer.length; i++) {

lib/encodePacket.browser.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PACKET_TYPES } from "./commons.js";
1+
import { PACKET_TYPES, Packet, RawData } from "./commons.js";
22

33
const withNativeBlob =
44
typeof Blob === "function" ||
@@ -13,7 +13,11 @@ const isView = obj => {
1313
: obj && obj.buffer instanceof ArrayBuffer;
1414
};
1515

16-
const encodePacket = ({ type, data }, supportsBinary, callback) => {
16+
const encodePacket = (
17+
{ type, data }: Packet,
18+
supportsBinary: boolean,
19+
callback: (encodedPacket: RawData) => void
20+
) => {
1721
if (withNativeBlob && data instanceof Blob) {
1822
if (supportsBinary) {
1923
return callback(data);
@@ -34,7 +38,10 @@ const encodePacket = ({ type, data }, supportsBinary, callback) => {
3438
return callback(PACKET_TYPES[type] + (data || ""));
3539
};
3640

37-
const encodeBlobAsBase64 = (data, callback) => {
41+
const encodeBlobAsBase64 = (
42+
data: Blob,
43+
callback: (encodedPacket: RawData) => void
44+
) => {
3845
const fileReader = new FileReader();
3946
fileReader.onload = function() {
4047
const content = (fileReader.result as string).split(",")[1];

lib/encodePacket.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { PACKET_TYPES } from "./commons.js";
1+
import { PACKET_TYPES, Packet, RawData } from "./commons.js";
22

3-
const encodePacket = ({ type, data }, supportsBinary, callback) => {
3+
const encodePacket = (
4+
{ type, data }: Packet,
5+
supportsBinary: boolean,
6+
callback: (encodedPacket: RawData) => void
7+
) => {
48
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
59
const buffer = toBuffer(data);
610
return callback(encodeBuffer(buffer, supportsBinary));
@@ -20,7 +24,7 @@ const toBuffer = data => {
2024
};
2125

2226
// only 'message' packets can contain binary, so the type prefix is not needed
23-
const encodeBuffer = (data, supportsBinary) => {
27+
const encodeBuffer = (data: Buffer, supportsBinary: boolean): RawData => {
2428
return supportsBinary ? data : "b" + data.toString("base64");
2529
};
2630

lib/index.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import encodePacket from "./encodePacket.js";
22
import decodePacket from "./decodePacket.js";
3+
import { Packet, PacketType, RawData, BinaryType } from "./commons";
34

45
const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
56

6-
const encodePayload = (packets, callback) => {
7+
const encodePayload = (
8+
packets: Packet[],
9+
callback: (encodedPayload: string) => void
10+
) => {
711
// some packets may be added to the array while encoding, so the initial length must be saved
812
const length = packets.length;
913
const encodedPackets = new Array(length);
@@ -20,7 +24,10 @@ const encodePayload = (packets, callback) => {
2024
});
2125
};
2226

23-
const decodePayload = (encodedPayload, binaryType?) => {
27+
const decodePayload = (
28+
encodedPayload: string,
29+
binaryType?: BinaryType
30+
): Packet[] => {
2431
const encodedPackets = encodedPayload.split(SEPARATOR);
2532
const packets = [];
2633
for (let i = 0; i < encodedPackets.length; i++) {
@@ -34,4 +41,13 @@ const decodePayload = (encodedPayload, binaryType?) => {
3441
};
3542

3643
export const protocol = 4;
37-
export { encodePacket, encodePayload, decodePacket, decodePayload };
44+
export {
45+
encodePacket,
46+
encodePayload,
47+
decodePacket,
48+
decodePayload,
49+
Packet,
50+
PacketType,
51+
RawData,
52+
BinaryType
53+
};

test/browser.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { decodePacket, decodePayload, encodePacket, encodePayload } from "..";
1+
import {
2+
decodePacket,
3+
decodePayload,
4+
encodePacket,
5+
encodePayload,
6+
Packet
7+
} from "..";
28
import * as expect from "expect.js";
39
import { areArraysEqual, createArrayBuffer } from "./util";
410

@@ -8,7 +14,7 @@ describe("engine.io-parser (browser only)", () => {
814
describe("single packet", () => {
915
if (withNativeArrayBuffer) {
1016
it("should encode/decode an ArrayBuffer", done => {
11-
const packet = {
17+
const packet: Packet = {
1218
type: "message",
1319
data: createArrayBuffer([1, 2, 3, 4])
1420
};
@@ -23,7 +29,7 @@ describe("engine.io-parser (browser only)", () => {
2329
});
2430

2531
it("should encode/decode an ArrayBuffer as base64", done => {
26-
const packet = {
32+
const packet: Packet = {
2733
type: "message",
2834
data: createArrayBuffer([1, 2, 3, 4])
2935
};
@@ -49,7 +55,7 @@ describe("engine.io-parser (browser only)", () => {
4955

5056
if (typeof Blob === "function") {
5157
it("should encode/decode a Blob", done => {
52-
const packet = {
58+
const packet: Packet = {
5359
type: "message",
5460
data: new Blob(["1234", createArrayBuffer([1, 2, 3, 4])])
5561
};
@@ -63,7 +69,7 @@ describe("engine.io-parser (browser only)", () => {
6369
});
6470

6571
it("should encode/decode a Blob as base64", done => {
66-
const packet = {
72+
const packet: Packet = {
6773
type: "message",
6874
data: new Blob(["1234", createArrayBuffer([1, 2, 3, 4])])
6975
};
@@ -81,7 +87,7 @@ describe("engine.io-parser (browser only)", () => {
8187
describe("payload", () => {
8288
if (withNativeArrayBuffer) {
8389
it("should encode/decode a string + ArrayBuffer payload", done => {
84-
const packets = [
90+
const packets: Packet[] = [
8591
{ type: "message", data: "test" },
8692
{ type: "message", data: createArrayBuffer([1, 2, 3, 4]) }
8793
];

test/index.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
import { decodePacket, decodePayload, encodePacket, encodePayload } from "..";
1+
import {
2+
decodePacket,
3+
decodePayload,
4+
encodePacket,
5+
encodePayload,
6+
Packet
7+
} from "..";
28
import * as expect from "expect.js";
39
import "./node";
410

511
describe("engine.io-parser", () => {
612
describe("single packet", () => {
713
it("should encode/decode a string", done => {
8-
const packet = { type: "message", data: "test" };
9-
encodePacket(packet, {}, encodedPacket => {
14+
const packet: Packet = { type: "message", data: "test" };
15+
encodePacket(packet, true, encodedPacket => {
1016
expect(encodedPacket).to.eql("4test");
1117
expect(decodePacket(encodedPacket)).to.eql(packet);
1218
done();
1319
});
1420
});
1521

1622
it("should fail to decode a malformed packet", () => {
17-
expect(decodePacket("", {})).to.eql({
23+
expect(decodePacket("")).to.eql({
1824
type: "error",
1925
data: "parser error"
2026
});
21-
expect(decodePacket("a123", {})).to.eql({
27+
expect(decodePacket("a123")).to.eql({
2228
type: "error",
2329
data: "parser error"
2430
});
@@ -27,7 +33,7 @@ describe("engine.io-parser", () => {
2733

2834
describe("payload", () => {
2935
it("should encode/decode all packet types", done => {
30-
const packets = [
36+
const packets: Packet[] = [
3137
{ type: "open" },
3238
{ type: "close" },
3339
{ type: "ping", data: "probe" },

test/node.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
import { decodePacket, decodePayload, encodePacket, encodePayload } from "..";
1+
import {
2+
decodePacket,
3+
decodePayload,
4+
encodePacket,
5+
encodePayload,
6+
Packet
7+
} from "..";
28
import * as expect from "expect.js";
39
import { areArraysEqual } from "./util";
410

511
describe("engine.io-parser (node.js only)", () => {
612
describe("single packet", () => {
713
it("should encode/decode a Buffer", done => {
8-
const packet = { type: "message", data: Buffer.from([1, 2, 3, 4]) };
14+
const packet: Packet = {
15+
type: "message",
16+
data: Buffer.from([1, 2, 3, 4])
17+
};
918
encodePacket(packet, true, encodedPacket => {
1019
expect(encodedPacket).to.eql(packet.data); // noop
11-
expect(decodePacket(encodedPacket, {})).to.eql(packet);
20+
expect(decodePacket(encodedPacket)).to.eql(packet);
1221
done();
1322
});
1423
});
1524

1625
it("should encode/decode a Buffer as base64", done => {
17-
const packet = { type: "message", data: Buffer.from([1, 2, 3, 4]) };
26+
const packet: Packet = {
27+
type: "message",
28+
data: Buffer.from([1, 2, 3, 4])
29+
};
1830
encodePacket(packet, false, encodedPacket => {
1931
expect(encodedPacket).to.eql("bAQIDBA==");
20-
expect(decodePacket(encodedPacket, "buffer")).to.eql(packet);
32+
expect(decodePacket(encodedPacket, "nodebuffer")).to.eql(packet);
2133
done();
2234
});
2335
});
2436

2537
it("should encode/decode an ArrayBuffer", done => {
26-
const packet = {
38+
const packet: Packet = {
2739
type: "message",
2840
data: Int8Array.from([1, 2, 3, 4]).buffer
2941
};
@@ -38,7 +50,7 @@ describe("engine.io-parser (node.js only)", () => {
3850
});
3951

4052
it("should encode/decode an ArrayBuffer as base64", done => {
41-
const packet = {
53+
const packet: Packet = {
4254
type: "message",
4355
data: Int8Array.from([1, 2, 3, 4]).buffer
4456
};
@@ -83,13 +95,13 @@ describe("engine.io-parser (node.js only)", () => {
8395

8496
describe("payload", () => {
8597
it("should encode/decode a string + Buffer payload", done => {
86-
const packets = [
98+
const packets: Packet[] = [
8799
{ type: "message", data: "test" },
88100
{ type: "message", data: Buffer.from([1, 2, 3, 4]) }
89101
];
90102
encodePayload(packets, payload => {
91103
expect(payload).to.eql("4test\x1ebAQIDBA==");
92-
expect(decodePayload(payload, "buffer")).to.eql(packets);
104+
expect(decodePayload(payload, "nodebuffer")).to.eql(packets);
93105
done();
94106
});
95107
});

0 commit comments

Comments
 (0)