| "use strict"; |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.Decoder = exports.Encoder = exports.PacketType = exports.protocol = void 0; |
| exports.isPacketValid = isPacketValid; |
| const component_emitter_1 = require("@socket.io/component-emitter"); |
| const binary_js_1 = require("./binary.js"); |
| const is_binary_js_1 = require("./is-binary.js"); |
| const debug_1 = require("debug"); |
| const debug = (0, debug_1.default)("socket.io-parser"); |
| |
| |
| |
| const RESERVED_EVENTS = [ |
| "connect", |
| "connect_error", |
| "disconnect", |
| "disconnecting", |
| "newListener", |
| "removeListener", |
| ]; |
| |
| |
| |
| |
| |
| 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 = PacketType = {})); |
| |
| |
| |
| class Encoder { |
| |
| |
| |
| |
| |
| constructor(replacer) { |
| this.replacer = replacer; |
| } |
| |
| |
| |
| |
| |
| |
| encode(obj) { |
| debug("encoding packet %j", obj); |
| if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) { |
| if ((0, is_binary_js_1.hasBinary)(obj)) { |
| return this.encodeAsBinary({ |
| type: obj.type === PacketType.EVENT |
| ? PacketType.BINARY_EVENT |
| : PacketType.BINARY_ACK, |
| nsp: obj.nsp, |
| data: obj.data, |
| id: obj.id, |
| }); |
| } |
| } |
| 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, this.replacer); |
| } |
| debug("encoded %j as %s", obj, str); |
| return str; |
| } |
| |
| |
| |
| |
| |
| encodeAsBinary(obj) { |
| const deconstruction = (0, binary_js_1.deconstructPacket)(obj); |
| const pack = this.encodeAsString(deconstruction.packet); |
| const buffers = deconstruction.buffers; |
| buffers.unshift(pack); |
| return buffers; |
| } |
| } |
| exports.Encoder = Encoder; |
| |
| |
| |
| |
| |
| class Decoder extends component_emitter_1.Emitter { |
| |
| |
| |
| constructor(opts) { |
| super(); |
| this.opts = Object.assign({ |
| reviver: undefined, |
| maxAttachments: 10, |
| }, typeof opts === "function" ? { reviver: opts } : opts); |
| } |
| |
| |
| |
| |
| |
| add(obj) { |
| let packet; |
| if (typeof obj === "string") { |
| if (this.reconstructor) { |
| throw new Error("got plaintext data when reconstructing a packet"); |
| } |
| packet = this.decodeString(obj); |
| const isBinaryEvent = packet.type === PacketType.BINARY_EVENT; |
| if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) { |
| packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK; |
| |
| this.reconstructor = new BinaryReconstructor(packet); |
| |
| if (packet.attachments === 0) { |
| super.emitReserved("decoded", packet); |
| } |
| } |
| else { |
| |
| super.emitReserved("decoded", packet); |
| } |
| } |
| else if ((0, is_binary_js_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.emitReserved("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"); |
| } |
| const n = Number(buf); |
| if (!isInteger(n) || n < 0) { |
| throw new Error("Illegal attachments"); |
| } |
| else if (n > this.opts.maxAttachments) { |
| throw new Error("too many attachments"); |
| } |
| p.attachments = n; |
| } |
| |
| 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 = this.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; |
| } |
| tryParse(str) { |
| try { |
| return JSON.parse(str, this.opts.reviver); |
| } |
| catch (e) { |
| return false; |
| } |
| } |
| static isPayloadValid(type, payload) { |
| switch (type) { |
| case PacketType.CONNECT: |
| return isObject(payload); |
| case PacketType.DISCONNECT: |
| return payload === undefined; |
| case PacketType.CONNECT_ERROR: |
| return typeof payload === "string" || isObject(payload); |
| case PacketType.EVENT: |
| case PacketType.BINARY_EVENT: |
| return (Array.isArray(payload) && |
| (typeof payload[0] === "number" || |
| (typeof payload[0] === "string" && |
| RESERVED_EVENTS.indexOf(payload[0]) === -1))); |
| case PacketType.ACK: |
| case PacketType.BINARY_ACK: |
| return Array.isArray(payload); |
| } |
| } |
| |
| |
| |
| destroy() { |
| if (this.reconstructor) { |
| this.reconstructor.finishedReconstruction(); |
| this.reconstructor = null; |
| } |
| } |
| } |
| exports.Decoder = Decoder; |
| |
| |
| |
| |
| |
| |
| |
| |
| 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 = (0, binary_js_1.reconstructPacket)(this.reconPack, this.buffers); |
| this.finishedReconstruction(); |
| return packet; |
| } |
| return null; |
| } |
| |
| |
| |
| finishedReconstruction() { |
| this.reconPack = null; |
| this.buffers = []; |
| } |
| } |
| function isNamespaceValid(nsp) { |
| return typeof nsp === "string"; |
| } |
| |
| const isInteger = Number.isInteger || |
| function (value) { |
| return (typeof value === "number" && |
| isFinite(value) && |
| Math.floor(value) === value); |
| }; |
| function isAckIdValid(id) { |
| return id === undefined || isInteger(id); |
| } |
| |
| function isObject(value) { |
| return Object.prototype.toString.call(value) === "[object Object]"; |
| } |
| function isDataValid(type, payload) { |
| switch (type) { |
| case PacketType.CONNECT: |
| return payload === undefined || isObject(payload); |
| case PacketType.DISCONNECT: |
| return payload === undefined; |
| case PacketType.EVENT: |
| return (Array.isArray(payload) && |
| (typeof payload[0] === "number" || |
| (typeof payload[0] === "string" && |
| RESERVED_EVENTS.indexOf(payload[0]) === -1))); |
| case PacketType.ACK: |
| return Array.isArray(payload); |
| case PacketType.CONNECT_ERROR: |
| return typeof payload === "string" || isObject(payload); |
| default: |
| return false; |
| } |
| } |
| function isPacketValid(packet) { |
| return (isNamespaceValid(packet.nsp) && |
| isAckIdValid(packet.id) && |
| isDataValid(packet.type, packet.data)); |
| } |
|
|