123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Decoder = exports.Encoder = exports.PacketType = exports.protocol = void 0;
- const Emitter = require("component-emitter");
- const binary_1 = require("./binary");
- const is_binary_1 = require("./is-binary");
- const debug = require("debug")("socket.io-parser");
- exports.protocol = 5;
- var PacketType;
- (function (PacketType) {
- PacketType[PacketType["CONNECT"] = 0] = "CONNECT";
- PacketType[PacketType["DISCONNECT"] = 1] = "DISCONNECT";
- PacketType[PacketType["EVENT"] = 2] = "EVENT";
- PacketType[PacketType["ACK"] = 3] = "ACK";
- PacketType[PacketType["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
- PacketType[PacketType["BINARY_EVENT"] = 5] = "BINARY_EVENT";
- PacketType[PacketType["BINARY_ACK"] = 6] = "BINARY_ACK";
- })(PacketType = exports.PacketType || (exports.PacketType = {}));
- class Encoder {
-
- encode(obj) {
- debug("encoding packet %j", obj);
- if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) {
- if (is_binary_1.hasBinary(obj)) {
- obj.type =
- obj.type === PacketType.EVENT
- ? PacketType.BINARY_EVENT
- : PacketType.BINARY_ACK;
- return this.encodeAsBinary(obj);
- }
- }
- return [this.encodeAsString(obj)];
- }
-
- encodeAsString(obj) {
-
- let str = "" + obj.type;
-
- if (obj.type === PacketType.BINARY_EVENT ||
- obj.type === PacketType.BINARY_ACK) {
- str += obj.attachments + "-";
- }
-
-
- if (obj.nsp && "/" !== obj.nsp) {
- str += obj.nsp + ",";
- }
-
- if (null != obj.id) {
- str += obj.id;
- }
-
- if (null != obj.data) {
- str += JSON.stringify(obj.data);
- }
- debug("encoded %j as %s", obj, str);
- return str;
- }
-
- encodeAsBinary(obj) {
- const deconstruction = binary_1.deconstructPacket(obj);
- const pack = this.encodeAsString(deconstruction.packet);
- const buffers = deconstruction.buffers;
- buffers.unshift(pack);
- return buffers;
- }
- }
- exports.Encoder = Encoder;
- class Decoder extends Emitter {
- constructor() {
- super();
- }
-
- add(obj) {
- let packet;
- if (typeof obj === "string") {
- packet = this.decodeString(obj);
- if (packet.type === PacketType.BINARY_EVENT ||
- packet.type === PacketType.BINARY_ACK) {
-
- this.reconstructor = new BinaryReconstructor(packet);
-
- if (packet.attachments === 0) {
- super.emit("decoded", packet);
- }
- }
- else {
-
- super.emit("decoded", packet);
- }
- }
- else if (is_binary_1.isBinary(obj) || obj.base64) {
-
- if (!this.reconstructor) {
- throw new Error("got binary data when not reconstructing a packet");
- }
- else {
- packet = this.reconstructor.takeBinaryData(obj);
- if (packet) {
-
- this.reconstructor = null;
- super.emit("decoded", packet);
- }
- }
- }
- else {
- throw new Error("Unknown type: " + obj);
- }
- }
-
- decodeString(str) {
- let i = 0;
-
- const p = {
- type: Number(str.charAt(0)),
- };
- if (PacketType[p.type] === undefined) {
- throw new Error("unknown packet type " + p.type);
- }
-
- if (p.type === PacketType.BINARY_EVENT ||
- p.type === PacketType.BINARY_ACK) {
- const start = i + 1;
- while (str.charAt(++i) !== "-" && i != str.length) { }
- const buf = str.substring(start, i);
- if (buf != Number(buf) || str.charAt(i) !== "-") {
- throw new Error("Illegal attachments");
- }
- p.attachments = Number(buf);
- }
-
- if ("/" === str.charAt(i + 1)) {
- const start = i + 1;
- while (++i) {
- const c = str.charAt(i);
- if ("," === c)
- break;
- if (i === str.length)
- break;
- }
- p.nsp = str.substring(start, i);
- }
- else {
- p.nsp = "/";
- }
-
- const next = str.charAt(i + 1);
- if ("" !== next && Number(next) == next) {
- const start = i + 1;
- while (++i) {
- const c = str.charAt(i);
- if (null == c || Number(c) != c) {
- --i;
- break;
- }
- if (i === str.length)
- break;
- }
- p.id = Number(str.substring(start, i + 1));
- }
-
- if (str.charAt(++i)) {
- const payload = tryParse(str.substr(i));
- if (Decoder.isPayloadValid(p.type, payload)) {
- p.data = payload;
- }
- else {
- throw new Error("invalid payload");
- }
- }
- 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.CONNECT_ERROR:
- return typeof payload === "string" || typeof payload === "object";
- case PacketType.EVENT:
- case PacketType.BINARY_EVENT:
- return Array.isArray(payload) && payload.length > 0;
- case PacketType.ACK:
- case PacketType.BINARY_ACK:
- return Array.isArray(payload);
- }
- }
-
- destroy() {
- if (this.reconstructor) {
- this.reconstructor.finishedReconstruction();
- }
- }
- }
- exports.Decoder = Decoder;
- function tryParse(str) {
- try {
- return JSON.parse(str);
- }
- catch (e) {
- return false;
- }
- }
- class BinaryReconstructor {
- constructor(packet) {
- this.packet = packet;
- this.buffers = [];
- this.reconPack = packet;
- }
-
- takeBinaryData(binData) {
- this.buffers.push(binData);
- if (this.buffers.length === this.reconPack.attachments) {
-
- const packet = binary_1.reconstructPacket(this.reconPack, this.buffers);
- this.finishedReconstruction();
- return packet;
- }
- return null;
- }
-
- finishedReconstruction() {
- this.reconPack = null;
- this.buffers = [];
- }
- }
|