"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../../@socket.io/component-emitter/lib/esm/index.js"),t=require("./binary.js"),r=require("./is-binary.js");const s=["connect","connect_error","disconnect","disconnecting","newListener","removeListener"];var n;exports.PacketType=void 0,(n=exports.PacketType||(exports.PacketType={}))[n.CONNECT=0]="CONNECT",n[n.DISCONNECT=1]="DISCONNECT",n[n.EVENT=2]="EVENT",n[n.ACK=3]="ACK",n[n.CONNECT_ERROR=4]="CONNECT_ERROR",n[n.BINARY_EVENT=5]="BINARY_EVENT",n[n.BINARY_ACK=6]="BINARY_ACK";class o extends e.Emitter{constructor(e){super(),this.opts=Object.assign({reviver:void 0,maxAttachments:10},"function"==typeof e?{reviver:e}:e)}add(e){let t;if("string"==typeof e){if(this.reconstructor)throw new Error("got plaintext data when reconstructing a packet");t=this.decodeString(e);const r=t.type===exports.PacketType.BINARY_EVENT;r||t.type===exports.PacketType.BINARY_ACK?(t.type=r?exports.PacketType.EVENT:exports.PacketType.ACK,this.reconstructor=new c(t),0===t.attachments&&super.emitReserved("decoded",t)):super.emitReserved("decoded",t)}else{if(!r.isBinary(e)&&!e.base64)throw new Error("Unknown type: "+e);if(!this.reconstructor)throw new Error("got binary data when not reconstructing a packet");t=this.reconstructor.takeBinaryData(e),t&&(this.reconstructor=null,super.emitReserved("decoded",t))}}decodeString(e){let t=0;const r={type:Number(e.charAt(0))};if(void 0===exports.PacketType[r.type])throw new Error("unknown packet type "+r.type);if(r.type===exports.PacketType.BINARY_EVENT||r.type===exports.PacketType.BINARY_ACK){const s=t+1;for(;"-"!==e.charAt(++t)&&t!=e.length;);const n=e.substring(s,t);if(n!=Number(n)||"-"!==e.charAt(t))throw new Error("Illegal attachments");const o=Number(n);if(!a(o)||o<0)throw new Error("Illegal attachments");if(o>this.opts.maxAttachments)throw new Error("too many attachments");r.attachments=o}if("/"===e.charAt(t+1)){const s=t+1;for(;++t;){if(","===e.charAt(t))break;if(t===e.length)break}r.nsp=e.substring(s,t)}else r.nsp="/";const s=e.charAt(t+1);if(""!==s&&Number(s)==s){const s=t+1;for(;++t;){const r=e.charAt(t);if(null==r||Number(r)!=r){--t;break}if(t===e.length)break}r.id=Number(e.substring(s,t+1))}if(e.charAt(++t)){const s=this.tryParse(e.substr(t));if(!o.isPayloadValid(r.type,s))throw new Error("invalid payload");r.data=s}return r}tryParse(e){try{return JSON.parse(e,this.opts.reviver)}catch(e){return!1}}static isPayloadValid(e,t){switch(e){case exports.PacketType.CONNECT:return i(t);case exports.PacketType.DISCONNECT:return void 0===t;case exports.PacketType.CONNECT_ERROR:return"string"==typeof t||i(t);case exports.PacketType.EVENT:case exports.PacketType.BINARY_EVENT:return Array.isArray(t)&&("number"==typeof t[0]||"string"==typeof t[0]&&-1===s.indexOf(t[0]));case exports.PacketType.ACK:case exports.PacketType.BINARY_ACK:return Array.isArray(t)}}destroy(){this.reconstructor&&(this.reconstructor.finishedReconstruction(),this.reconstructor=null)}}class c{constructor(e){this.packet=e,this.buffers=[],this.reconPack=e}takeBinaryData(e){if(this.buffers.push(e),this.buffers.length===this.reconPack.attachments){const e=t.reconstructPacket(this.reconPack,this.buffers);return this.finishedReconstruction(),e}return null}finishedReconstruction(){this.reconPack=null,this.buffers=[]}}const a=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e};function i(e){return"[object Object]"===Object.prototype.toString.call(e)}exports.Decoder=o,exports.Encoder=class{constructor(e){this.replacer=e}encode(e){return e.type!==exports.PacketType.EVENT&&e.type!==exports.PacketType.ACK||!r.hasBinary(e)?[this.encodeAsString(e)]:this.encodeAsBinary({type:e.type===exports.PacketType.EVENT?exports.PacketType.BINARY_EVENT:exports.PacketType.BINARY_ACK,nsp:e.nsp,data:e.data,id:e.id})}encodeAsString(e){let t=""+e.type;return e.type!==exports.PacketType.BINARY_EVENT&&e.type!==exports.PacketType.BINARY_ACK||(t+=e.attachments+"-"),e.nsp&&"/"!==e.nsp&&(t+=e.nsp+","),null!=e.id&&(t+=e.id),null!=e.data&&(t+=JSON.stringify(e.data,this.replacer)),t}encodeAsBinary(e){const r=t.deconstructPacket(e),s=this.encodeAsString(r.packet),n=r.buffers;return n.unshift(s),n}},exports.isPacketValid=function(e){return"string"==typeof e.nsp&&(void 0===(t=e.id)||a(t))&&function(e,t){switch(e){case exports.PacketType.CONNECT:return void 0===t||i(t);case exports.PacketType.DISCONNECT:return void 0===t;case exports.PacketType.EVENT:return Array.isArray(t)&&("number"==typeof t[0]||"string"==typeof t[0]&&-1===s.indexOf(t[0]));case exports.PacketType.ACK:return Array.isArray(t);case exports.PacketType.CONNECT_ERROR:return"string"==typeof t||i(t);default:return!1}}(e.type,e.data);var t},exports.protocol=5;
//# sourceMappingURL=index.js.map
