@zaplier/sdk 1.1.5 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -6316,7 +6316,99 @@ class ResourceSpoofTransport {
6316
6316
  }
6317
6317
  }
6318
6318
  /**
6319
- * WebRTC DataChannel Transport (experimental)
6319
+ * Geckos.io WebRTC Transport (UDP over WebRTC for anti-adblock)
6320
+ */
6321
+ class GeckosTransport {
6322
+ constructor(baseUrl) {
6323
+ this.name = 'websocket-enhanced';
6324
+ this.available = typeof WebSocket !== 'undefined';
6325
+ this.connected = false;
6326
+ this.baseUrl = baseUrl;
6327
+ }
6328
+ async send(data, endpoint) {
6329
+ const start = Date.now();
6330
+ if (!this.available) {
6331
+ return { success: false, method: this.name, error: 'WebRTC not available' };
6332
+ }
6333
+ try {
6334
+ if (!this.connected) {
6335
+ await this.connect();
6336
+ }
6337
+ return new Promise((resolve) => {
6338
+ // Emit tracking data via enhanced WebSocket
6339
+ this.socket.emit('track', data);
6340
+ // Listen for success response
6341
+ this.socket.once('track_success', () => {
6342
+ resolve({
6343
+ success: true,
6344
+ method: this.name,
6345
+ latency: Date.now() - start
6346
+ });
6347
+ });
6348
+ // Listen for error response
6349
+ this.socket.once('track_error', (error) => {
6350
+ resolve({
6351
+ success: false,
6352
+ method: this.name,
6353
+ error: error.error || 'WebSocket send failed'
6354
+ });
6355
+ });
6356
+ // Timeout after 5 seconds
6357
+ setTimeout(() => {
6358
+ resolve({
6359
+ success: false,
6360
+ method: this.name,
6361
+ error: 'WebSocket timeout'
6362
+ });
6363
+ }, 5000);
6364
+ });
6365
+ }
6366
+ catch (error) {
6367
+ return {
6368
+ success: false,
6369
+ method: this.name,
6370
+ error: error instanceof Error ? error.message : String(error)
6371
+ };
6372
+ }
6373
+ }
6374
+ async connect() {
6375
+ return new Promise((resolve, reject) => {
6376
+ try {
6377
+ // Use dynamic import to load socket.io-client only when needed
6378
+ Promise.resolve().then(function () { return index; }).then(({ io }) => {
6379
+ // Extract domain from baseUrl
6380
+ const url = new URL(this.baseUrl);
6381
+ const socketUrl = `${url.protocol}//${url.host}:9208`;
6382
+ this.socket = io(socketUrl, {
6383
+ transports: ['websocket', 'polling'],
6384
+ timeout: 5000
6385
+ });
6386
+ this.socket.on('connect', () => {
6387
+ this.connected = true;
6388
+ resolve();
6389
+ });
6390
+ this.socket.on('connect_error', (error) => {
6391
+ reject(error);
6392
+ });
6393
+ this.socket.on('disconnect', () => {
6394
+ this.connected = false;
6395
+ });
6396
+ }).catch(reject);
6397
+ }
6398
+ catch (error) {
6399
+ reject(error);
6400
+ }
6401
+ });
6402
+ }
6403
+ destroy() {
6404
+ if (this.socket) {
6405
+ this.socket.disconnect();
6406
+ this.connected = false;
6407
+ }
6408
+ }
6409
+ }
6410
+ /**
6411
+ * WebRTC DataChannel Transport (experimental - legacy)
6320
6412
  */
6321
6413
  class WebRTCTransport {
6322
6414
  constructor() {
@@ -6407,7 +6499,7 @@ class AntiAdblockManager {
6407
6499
  this.baseUrl = baseUrl;
6408
6500
  this.config = {
6409
6501
  enabled: true,
6410
- methods: ['fetch', 'websocket', 'resource'],
6502
+ methods: ['geckos', 'fetch', 'websocket', 'resource'], // Geckos as primary
6411
6503
  fallbackDelay: 100,
6412
6504
  maxRetries: 2,
6413
6505
  debug: false,
@@ -6417,6 +6509,7 @@ class AntiAdblockManager {
6417
6509
  }
6418
6510
  initializeTransports() {
6419
6511
  const transportMap = {
6512
+ geckos: () => new GeckosTransport(this.baseUrl),
6420
6513
  fetch: () => new FetchTransport(),
6421
6514
  websocket: () => new WebSocketTransport(this.baseUrl),
6422
6515
  resource: () => new ResourceSpoofTransport(this.baseUrl),
@@ -6826,11 +6919,24 @@ class HeatmapEngine {
6826
6919
  this.sendToBackend(payload);
6827
6920
  }
6828
6921
  /**
6829
- * Send data to backend (will be enhanced with anti-adblock)
6922
+ * Set anti-adblock manager for WebRTC transport
6923
+ */
6924
+ setAntiAdblockManager(manager) {
6925
+ this.antiAdblockManager = manager;
6926
+ }
6927
+ /**
6928
+ * Send data via WebRTC (anti-adblock) or fallback to standard fetch
6830
6929
  */
6831
6930
  async sendToBackend(payload) {
6832
6931
  try {
6833
- // For now, use standard fetch - will be replaced with anti-adblock manager
6932
+ // Try WebRTC first if anti-adblock manager is available
6933
+ if (this.antiAdblockManager) {
6934
+ const result = await this.antiAdblockManager.send(payload, '/api/heatmap/record');
6935
+ if (result.success) {
6936
+ return; // Success via WebRTC
6937
+ }
6938
+ }
6939
+ // Fallback to standard fetch
6834
6940
  const response = await fetch('/api/heatmap/track', {
6835
6941
  method: 'POST',
6836
6942
  headers: { 'Content-Type': 'application/json' },
@@ -7295,11 +7401,24 @@ class SessionReplayEngine {
7295
7401
  this.sendToBackend(payload);
7296
7402
  }
7297
7403
  /**
7298
- * Send data to backend (will be enhanced with anti-adblock)
7404
+ * Set anti-adblock manager for WebRTC transport
7405
+ */
7406
+ setAntiAdblockManager(manager) {
7407
+ this.antiAdblockManager = manager;
7408
+ }
7409
+ /**
7410
+ * Send data via WebRTC (anti-adblock) or fallback to standard fetch
7299
7411
  */
7300
7412
  async sendToBackend(payload) {
7301
7413
  try {
7302
- // For now, use standard fetch - will be replaced with anti-adblock manager
7414
+ // Try WebRTC first if anti-adblock manager is available
7415
+ if (this.antiAdblockManager) {
7416
+ const result = await this.antiAdblockManager.send(payload, '/api/replay/record');
7417
+ if (result.success) {
7418
+ return; // Success via WebRTC
7419
+ }
7420
+ }
7421
+ // Fallback to standard fetch
7303
7422
  const response = await fetch('/api/replay/record', {
7304
7423
  method: 'POST',
7305
7424
  headers: { 'Content-Type': 'application/json' },
@@ -7379,6 +7498,10 @@ class ZaplierSDK {
7379
7498
  trackRageClicks: true,
7380
7499
  trackMouseMoves: false,
7381
7500
  });
7501
+ // Connect to anti-adblock manager
7502
+ if (this.antiAdblockManager) {
7503
+ this.heatmapEngine.setAntiAdblockManager(this.antiAdblockManager);
7504
+ }
7382
7505
  this.heatmapEngine.start();
7383
7506
  }
7384
7507
  if (this.config.debug) {
@@ -7429,6 +7552,10 @@ class ZaplierSDK {
7429
7552
  maskSensitiveFields: this.config.replayMaskInputs,
7430
7553
  compressionEnabled: true,
7431
7554
  });
7555
+ // Connect to anti-adblock manager
7556
+ if (this.antiAdblockManager) {
7557
+ this.replayEngine.setAntiAdblockManager(this.antiAdblockManager);
7558
+ }
7432
7559
  this.replayEngine.start();
7433
7560
  }
7434
7561
  if (this.config.debug) {
@@ -7457,6 +7584,10 @@ class ZaplierSDK {
7457
7584
  maskSensitiveFields: this.config.replayMaskInputs,
7458
7585
  compressionEnabled: true,
7459
7586
  });
7587
+ // Connect to anti-adblock manager
7588
+ if (this.antiAdblockManager) {
7589
+ this.replayEngine.setAntiAdblockManager(this.antiAdblockManager);
7590
+ }
7460
7591
  return this.replayEngine.start();
7461
7592
  }
7462
7593
  return this.replayEngine ? this.replayEngine.start() : false;
@@ -7520,9 +7651,10 @@ class ZaplierSDK {
7520
7651
  this.trackPageView();
7521
7652
  }
7522
7653
  this.isInitialized = true;
7523
- // Initialize tracking engines and anti-adblock system
7524
- this.initializeTrackingEngines();
7654
+ // Initialize anti-adblock system first
7525
7655
  this.initializeAntiAdblock();
7656
+ // Then initialize tracking engines (they'll connect to anti-adblock)
7657
+ this.initializeTrackingEngines();
7526
7658
  // Process queued events
7527
7659
  this.processEventQueue();
7528
7660
  if (this.config.debug) {
@@ -7555,6 +7687,10 @@ class ZaplierSDK {
7555
7687
  maskSensitiveFields: this.config.replayMaskInputs,
7556
7688
  compressionEnabled: true,
7557
7689
  });
7690
+ // Connect to anti-adblock manager for WebRTC transport
7691
+ if (this.antiAdblockManager) {
7692
+ this.replayEngine.setAntiAdblockManager(this.antiAdblockManager);
7693
+ }
7558
7694
  this.replayEngine.start();
7559
7695
  if (this.config.debug) {
7560
7696
  console.log("[Zaplier] Session Replay started");
@@ -7568,6 +7704,10 @@ class ZaplierSDK {
7568
7704
  trackRageClicks: true,
7569
7705
  trackMouseMoves: false, // Can be enabled via API
7570
7706
  });
7707
+ // Connect to anti-adblock manager for WebRTC transport
7708
+ if (this.antiAdblockManager) {
7709
+ this.heatmapEngine.setAntiAdblockManager(this.antiAdblockManager);
7710
+ }
7571
7711
  this.heatmapEngine.start();
7572
7712
  if (this.config.debug) {
7573
7713
  console.log("[Zaplier] Heatmap tracking started");
@@ -7617,7 +7757,7 @@ class ZaplierSDK {
7617
7757
  try {
7618
7758
  this.antiAdblockManager = new AntiAdblockManager(this.config.apiEndpoint, {
7619
7759
  enabled: true,
7620
- methods: ["fetch", "websocket", "resource"],
7760
+ methods: ["geckos", "fetch", "websocket", "resource"], // Geckos as primary
7621
7761
  fallbackDelay: 100,
7622
7762
  maxRetries: 2,
7623
7763
  debug: this.config.debug,
@@ -12674,5 +12814,4044 @@ var deviceSignals = /*#__PURE__*/Object.freeze({
12674
12814
  collectDeviceSignals: collectDeviceSignals
12675
12815
  });
12676
12816
 
12817
+ const PACKET_TYPES = Object.create(null); // no Map = no polyfill
12818
+ PACKET_TYPES["open"] = "0";
12819
+ PACKET_TYPES["close"] = "1";
12820
+ PACKET_TYPES["ping"] = "2";
12821
+ PACKET_TYPES["pong"] = "3";
12822
+ PACKET_TYPES["message"] = "4";
12823
+ PACKET_TYPES["upgrade"] = "5";
12824
+ PACKET_TYPES["noop"] = "6";
12825
+ const PACKET_TYPES_REVERSE = Object.create(null);
12826
+ Object.keys(PACKET_TYPES).forEach((key) => {
12827
+ PACKET_TYPES_REVERSE[PACKET_TYPES[key]] = key;
12828
+ });
12829
+ const ERROR_PACKET = { type: "error", data: "parser error" };
12830
+
12831
+ const withNativeBlob$1 = typeof Blob === "function" ||
12832
+ (typeof Blob !== "undefined" &&
12833
+ Object.prototype.toString.call(Blob) === "[object BlobConstructor]");
12834
+ const withNativeArrayBuffer$2 = typeof ArrayBuffer === "function";
12835
+ // ArrayBuffer.isView method is not defined in IE10
12836
+ const isView$1 = (obj) => {
12837
+ return typeof ArrayBuffer.isView === "function"
12838
+ ? ArrayBuffer.isView(obj)
12839
+ : obj && obj.buffer instanceof ArrayBuffer;
12840
+ };
12841
+ const encodePacket = ({ type, data }, supportsBinary, callback) => {
12842
+ if (withNativeBlob$1 && data instanceof Blob) {
12843
+ if (supportsBinary) {
12844
+ return callback(data);
12845
+ }
12846
+ else {
12847
+ return encodeBlobAsBase64(data, callback);
12848
+ }
12849
+ }
12850
+ else if (withNativeArrayBuffer$2 &&
12851
+ (data instanceof ArrayBuffer || isView$1(data))) {
12852
+ if (supportsBinary) {
12853
+ return callback(data);
12854
+ }
12855
+ else {
12856
+ return encodeBlobAsBase64(new Blob([data]), callback);
12857
+ }
12858
+ }
12859
+ // plain string
12860
+ return callback(PACKET_TYPES[type] + (data || ""));
12861
+ };
12862
+ const encodeBlobAsBase64 = (data, callback) => {
12863
+ const fileReader = new FileReader();
12864
+ fileReader.onload = function () {
12865
+ const content = fileReader.result.split(",")[1];
12866
+ callback("b" + (content || ""));
12867
+ };
12868
+ return fileReader.readAsDataURL(data);
12869
+ };
12870
+ function toArray(data) {
12871
+ if (data instanceof Uint8Array) {
12872
+ return data;
12873
+ }
12874
+ else if (data instanceof ArrayBuffer) {
12875
+ return new Uint8Array(data);
12876
+ }
12877
+ else {
12878
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
12879
+ }
12880
+ }
12881
+ let TEXT_ENCODER;
12882
+ function encodePacketToBinary(packet, callback) {
12883
+ if (withNativeBlob$1 && packet.data instanceof Blob) {
12884
+ return packet.data.arrayBuffer().then(toArray).then(callback);
12885
+ }
12886
+ else if (withNativeArrayBuffer$2 &&
12887
+ (packet.data instanceof ArrayBuffer || isView$1(packet.data))) {
12888
+ return callback(toArray(packet.data));
12889
+ }
12890
+ encodePacket(packet, false, (encoded) => {
12891
+ if (!TEXT_ENCODER) {
12892
+ TEXT_ENCODER = new TextEncoder();
12893
+ }
12894
+ callback(TEXT_ENCODER.encode(encoded));
12895
+ });
12896
+ }
12897
+
12898
+ // imported from https://github.com/socketio/base64-arraybuffer
12899
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
12900
+ // Use a lookup table to find the index.
12901
+ const lookup$1 = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
12902
+ for (let i = 0; i < chars.length; i++) {
12903
+ lookup$1[chars.charCodeAt(i)] = i;
12904
+ }
12905
+ const decode$1 = (base64) => {
12906
+ let bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
12907
+ if (base64[base64.length - 1] === '=') {
12908
+ bufferLength--;
12909
+ if (base64[base64.length - 2] === '=') {
12910
+ bufferLength--;
12911
+ }
12912
+ }
12913
+ const arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer);
12914
+ for (i = 0; i < len; i += 4) {
12915
+ encoded1 = lookup$1[base64.charCodeAt(i)];
12916
+ encoded2 = lookup$1[base64.charCodeAt(i + 1)];
12917
+ encoded3 = lookup$1[base64.charCodeAt(i + 2)];
12918
+ encoded4 = lookup$1[base64.charCodeAt(i + 3)];
12919
+ bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
12920
+ bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
12921
+ bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
12922
+ }
12923
+ return arraybuffer;
12924
+ };
12925
+
12926
+ const withNativeArrayBuffer$1 = typeof ArrayBuffer === "function";
12927
+ const decodePacket = (encodedPacket, binaryType) => {
12928
+ if (typeof encodedPacket !== "string") {
12929
+ return {
12930
+ type: "message",
12931
+ data: mapBinary(encodedPacket, binaryType),
12932
+ };
12933
+ }
12934
+ const type = encodedPacket.charAt(0);
12935
+ if (type === "b") {
12936
+ return {
12937
+ type: "message",
12938
+ data: decodeBase64Packet(encodedPacket.substring(1), binaryType),
12939
+ };
12940
+ }
12941
+ const packetType = PACKET_TYPES_REVERSE[type];
12942
+ if (!packetType) {
12943
+ return ERROR_PACKET;
12944
+ }
12945
+ return encodedPacket.length > 1
12946
+ ? {
12947
+ type: PACKET_TYPES_REVERSE[type],
12948
+ data: encodedPacket.substring(1),
12949
+ }
12950
+ : {
12951
+ type: PACKET_TYPES_REVERSE[type],
12952
+ };
12953
+ };
12954
+ const decodeBase64Packet = (data, binaryType) => {
12955
+ if (withNativeArrayBuffer$1) {
12956
+ const decoded = decode$1(data);
12957
+ return mapBinary(decoded, binaryType);
12958
+ }
12959
+ else {
12960
+ return { base64: true, data }; // fallback for old browsers
12961
+ }
12962
+ };
12963
+ const mapBinary = (data, binaryType) => {
12964
+ switch (binaryType) {
12965
+ case "blob":
12966
+ if (data instanceof Blob) {
12967
+ // from WebSocket + binaryType "blob"
12968
+ return data;
12969
+ }
12970
+ else {
12971
+ // from HTTP long-polling or WebTransport
12972
+ return new Blob([data]);
12973
+ }
12974
+ case "arraybuffer":
12975
+ default:
12976
+ if (data instanceof ArrayBuffer) {
12977
+ // from HTTP long-polling (base64) or WebSocket + binaryType "arraybuffer"
12978
+ return data;
12979
+ }
12980
+ else {
12981
+ // from WebTransport (Uint8Array)
12982
+ return data.buffer;
12983
+ }
12984
+ }
12985
+ };
12986
+
12987
+ const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
12988
+ const encodePayload = (packets, callback) => {
12989
+ // some packets may be added to the array while encoding, so the initial length must be saved
12990
+ const length = packets.length;
12991
+ const encodedPackets = new Array(length);
12992
+ let count = 0;
12993
+ packets.forEach((packet, i) => {
12994
+ // force base64 encoding for binary packets
12995
+ encodePacket(packet, false, (encodedPacket) => {
12996
+ encodedPackets[i] = encodedPacket;
12997
+ if (++count === length) {
12998
+ callback(encodedPackets.join(SEPARATOR));
12999
+ }
13000
+ });
13001
+ });
13002
+ };
13003
+ const decodePayload = (encodedPayload, binaryType) => {
13004
+ const encodedPackets = encodedPayload.split(SEPARATOR);
13005
+ const packets = [];
13006
+ for (let i = 0; i < encodedPackets.length; i++) {
13007
+ const decodedPacket = decodePacket(encodedPackets[i], binaryType);
13008
+ packets.push(decodedPacket);
13009
+ if (decodedPacket.type === "error") {
13010
+ break;
13011
+ }
13012
+ }
13013
+ return packets;
13014
+ };
13015
+ function createPacketEncoderStream() {
13016
+ return new TransformStream({
13017
+ transform(packet, controller) {
13018
+ encodePacketToBinary(packet, (encodedPacket) => {
13019
+ const payloadLength = encodedPacket.length;
13020
+ let header;
13021
+ // inspired by the WebSocket format: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#decoding_payload_length
13022
+ if (payloadLength < 126) {
13023
+ header = new Uint8Array(1);
13024
+ new DataView(header.buffer).setUint8(0, payloadLength);
13025
+ }
13026
+ else if (payloadLength < 65536) {
13027
+ header = new Uint8Array(3);
13028
+ const view = new DataView(header.buffer);
13029
+ view.setUint8(0, 126);
13030
+ view.setUint16(1, payloadLength);
13031
+ }
13032
+ else {
13033
+ header = new Uint8Array(9);
13034
+ const view = new DataView(header.buffer);
13035
+ view.setUint8(0, 127);
13036
+ view.setBigUint64(1, BigInt(payloadLength));
13037
+ }
13038
+ // first bit indicates whether the payload is plain text (0) or binary (1)
13039
+ if (packet.data && typeof packet.data !== "string") {
13040
+ header[0] |= 0x80;
13041
+ }
13042
+ controller.enqueue(header);
13043
+ controller.enqueue(encodedPacket);
13044
+ });
13045
+ },
13046
+ });
13047
+ }
13048
+ let TEXT_DECODER;
13049
+ function totalLength(chunks) {
13050
+ return chunks.reduce((acc, chunk) => acc + chunk.length, 0);
13051
+ }
13052
+ function concatChunks(chunks, size) {
13053
+ if (chunks[0].length === size) {
13054
+ return chunks.shift();
13055
+ }
13056
+ const buffer = new Uint8Array(size);
13057
+ let j = 0;
13058
+ for (let i = 0; i < size; i++) {
13059
+ buffer[i] = chunks[0][j++];
13060
+ if (j === chunks[0].length) {
13061
+ chunks.shift();
13062
+ j = 0;
13063
+ }
13064
+ }
13065
+ if (chunks.length && j < chunks[0].length) {
13066
+ chunks[0] = chunks[0].slice(j);
13067
+ }
13068
+ return buffer;
13069
+ }
13070
+ function createPacketDecoderStream(maxPayload, binaryType) {
13071
+ if (!TEXT_DECODER) {
13072
+ TEXT_DECODER = new TextDecoder();
13073
+ }
13074
+ const chunks = [];
13075
+ let state = 0 /* State.READ_HEADER */;
13076
+ let expectedLength = -1;
13077
+ let isBinary = false;
13078
+ return new TransformStream({
13079
+ transform(chunk, controller) {
13080
+ chunks.push(chunk);
13081
+ while (true) {
13082
+ if (state === 0 /* State.READ_HEADER */) {
13083
+ if (totalLength(chunks) < 1) {
13084
+ break;
13085
+ }
13086
+ const header = concatChunks(chunks, 1);
13087
+ isBinary = (header[0] & 0x80) === 0x80;
13088
+ expectedLength = header[0] & 0x7f;
13089
+ if (expectedLength < 126) {
13090
+ state = 3 /* State.READ_PAYLOAD */;
13091
+ }
13092
+ else if (expectedLength === 126) {
13093
+ state = 1 /* State.READ_EXTENDED_LENGTH_16 */;
13094
+ }
13095
+ else {
13096
+ state = 2 /* State.READ_EXTENDED_LENGTH_64 */;
13097
+ }
13098
+ }
13099
+ else if (state === 1 /* State.READ_EXTENDED_LENGTH_16 */) {
13100
+ if (totalLength(chunks) < 2) {
13101
+ break;
13102
+ }
13103
+ const headerArray = concatChunks(chunks, 2);
13104
+ expectedLength = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length).getUint16(0);
13105
+ state = 3 /* State.READ_PAYLOAD */;
13106
+ }
13107
+ else if (state === 2 /* State.READ_EXTENDED_LENGTH_64 */) {
13108
+ if (totalLength(chunks) < 8) {
13109
+ break;
13110
+ }
13111
+ const headerArray = concatChunks(chunks, 8);
13112
+ const view = new DataView(headerArray.buffer, headerArray.byteOffset, headerArray.length);
13113
+ const n = view.getUint32(0);
13114
+ if (n > Math.pow(2, 53 - 32) - 1) {
13115
+ // the maximum safe integer in JavaScript is 2^53 - 1
13116
+ controller.enqueue(ERROR_PACKET);
13117
+ break;
13118
+ }
13119
+ expectedLength = n * Math.pow(2, 32) + view.getUint32(4);
13120
+ state = 3 /* State.READ_PAYLOAD */;
13121
+ }
13122
+ else {
13123
+ if (totalLength(chunks) < expectedLength) {
13124
+ break;
13125
+ }
13126
+ const data = concatChunks(chunks, expectedLength);
13127
+ controller.enqueue(decodePacket(isBinary ? data : TEXT_DECODER.decode(data), binaryType));
13128
+ state = 0 /* State.READ_HEADER */;
13129
+ }
13130
+ if (expectedLength === 0 || expectedLength > maxPayload) {
13131
+ controller.enqueue(ERROR_PACKET);
13132
+ break;
13133
+ }
13134
+ }
13135
+ },
13136
+ });
13137
+ }
13138
+ const protocol$1 = 4;
13139
+
13140
+ /**
13141
+ * Initialize a new `Emitter`.
13142
+ *
13143
+ * @api public
13144
+ */
13145
+
13146
+ function Emitter(obj) {
13147
+ if (obj) return mixin(obj);
13148
+ }
13149
+
13150
+ /**
13151
+ * Mixin the emitter properties.
13152
+ *
13153
+ * @param {Object} obj
13154
+ * @return {Object}
13155
+ * @api private
13156
+ */
13157
+
13158
+ function mixin(obj) {
13159
+ for (var key in Emitter.prototype) {
13160
+ obj[key] = Emitter.prototype[key];
13161
+ }
13162
+ return obj;
13163
+ }
13164
+
13165
+ /**
13166
+ * Listen on the given `event` with `fn`.
13167
+ *
13168
+ * @param {String} event
13169
+ * @param {Function} fn
13170
+ * @return {Emitter}
13171
+ * @api public
13172
+ */
13173
+
13174
+ Emitter.prototype.on =
13175
+ Emitter.prototype.addEventListener = function(event, fn){
13176
+ this._callbacks = this._callbacks || {};
13177
+ (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
13178
+ .push(fn);
13179
+ return this;
13180
+ };
13181
+
13182
+ /**
13183
+ * Adds an `event` listener that will be invoked a single
13184
+ * time then automatically removed.
13185
+ *
13186
+ * @param {String} event
13187
+ * @param {Function} fn
13188
+ * @return {Emitter}
13189
+ * @api public
13190
+ */
13191
+
13192
+ Emitter.prototype.once = function(event, fn){
13193
+ function on() {
13194
+ this.off(event, on);
13195
+ fn.apply(this, arguments);
13196
+ }
13197
+
13198
+ on.fn = fn;
13199
+ this.on(event, on);
13200
+ return this;
13201
+ };
13202
+
13203
+ /**
13204
+ * Remove the given callback for `event` or all
13205
+ * registered callbacks.
13206
+ *
13207
+ * @param {String} event
13208
+ * @param {Function} fn
13209
+ * @return {Emitter}
13210
+ * @api public
13211
+ */
13212
+
13213
+ Emitter.prototype.off =
13214
+ Emitter.prototype.removeListener =
13215
+ Emitter.prototype.removeAllListeners =
13216
+ Emitter.prototype.removeEventListener = function(event, fn){
13217
+ this._callbacks = this._callbacks || {};
13218
+
13219
+ // all
13220
+ if (0 == arguments.length) {
13221
+ this._callbacks = {};
13222
+ return this;
13223
+ }
13224
+
13225
+ // specific event
13226
+ var callbacks = this._callbacks['$' + event];
13227
+ if (!callbacks) return this;
13228
+
13229
+ // remove all handlers
13230
+ if (1 == arguments.length) {
13231
+ delete this._callbacks['$' + event];
13232
+ return this;
13233
+ }
13234
+
13235
+ // remove specific handler
13236
+ var cb;
13237
+ for (var i = 0; i < callbacks.length; i++) {
13238
+ cb = callbacks[i];
13239
+ if (cb === fn || cb.fn === fn) {
13240
+ callbacks.splice(i, 1);
13241
+ break;
13242
+ }
13243
+ }
13244
+
13245
+ // Remove event specific arrays for event types that no
13246
+ // one is subscribed for to avoid memory leak.
13247
+ if (callbacks.length === 0) {
13248
+ delete this._callbacks['$' + event];
13249
+ }
13250
+
13251
+ return this;
13252
+ };
13253
+
13254
+ /**
13255
+ * Emit `event` with the given args.
13256
+ *
13257
+ * @param {String} event
13258
+ * @param {Mixed} ...
13259
+ * @return {Emitter}
13260
+ */
13261
+
13262
+ Emitter.prototype.emit = function(event){
13263
+ this._callbacks = this._callbacks || {};
13264
+
13265
+ var args = new Array(arguments.length - 1)
13266
+ , callbacks = this._callbacks['$' + event];
13267
+
13268
+ for (var i = 1; i < arguments.length; i++) {
13269
+ args[i - 1] = arguments[i];
13270
+ }
13271
+
13272
+ if (callbacks) {
13273
+ callbacks = callbacks.slice(0);
13274
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
13275
+ callbacks[i].apply(this, args);
13276
+ }
13277
+ }
13278
+
13279
+ return this;
13280
+ };
13281
+
13282
+ // alias used for reserved events (protected method)
13283
+ Emitter.prototype.emitReserved = Emitter.prototype.emit;
13284
+
13285
+ /**
13286
+ * Return array of callbacks for `event`.
13287
+ *
13288
+ * @param {String} event
13289
+ * @return {Array}
13290
+ * @api public
13291
+ */
13292
+
13293
+ Emitter.prototype.listeners = function(event){
13294
+ this._callbacks = this._callbacks || {};
13295
+ return this._callbacks['$' + event] || [];
13296
+ };
13297
+
13298
+ /**
13299
+ * Check if this emitter has `event` handlers.
13300
+ *
13301
+ * @param {String} event
13302
+ * @return {Boolean}
13303
+ * @api public
13304
+ */
13305
+
13306
+ Emitter.prototype.hasListeners = function(event){
13307
+ return !! this.listeners(event).length;
13308
+ };
13309
+
13310
+ const nextTick = (() => {
13311
+ const isPromiseAvailable = typeof Promise === "function" && typeof Promise.resolve === "function";
13312
+ if (isPromiseAvailable) {
13313
+ return (cb) => Promise.resolve().then(cb);
13314
+ }
13315
+ else {
13316
+ return (cb, setTimeoutFn) => setTimeoutFn(cb, 0);
13317
+ }
13318
+ })();
13319
+ const globalThisShim = (() => {
13320
+ if (typeof self !== "undefined") {
13321
+ return self;
13322
+ }
13323
+ else if (typeof window !== "undefined") {
13324
+ return window;
13325
+ }
13326
+ else {
13327
+ return Function("return this")();
13328
+ }
13329
+ })();
13330
+ const defaultBinaryType = "arraybuffer";
13331
+ function createCookieJar() { }
13332
+
13333
+ function pick(obj, ...attr) {
13334
+ return attr.reduce((acc, k) => {
13335
+ if (obj.hasOwnProperty(k)) {
13336
+ acc[k] = obj[k];
13337
+ }
13338
+ return acc;
13339
+ }, {});
13340
+ }
13341
+ // Keep a reference to the real timeout functions so they can be used when overridden
13342
+ const NATIVE_SET_TIMEOUT = globalThisShim.setTimeout;
13343
+ const NATIVE_CLEAR_TIMEOUT = globalThisShim.clearTimeout;
13344
+ function installTimerFunctions(obj, opts) {
13345
+ if (opts.useNativeTimers) {
13346
+ obj.setTimeoutFn = NATIVE_SET_TIMEOUT.bind(globalThisShim);
13347
+ obj.clearTimeoutFn = NATIVE_CLEAR_TIMEOUT.bind(globalThisShim);
13348
+ }
13349
+ else {
13350
+ obj.setTimeoutFn = globalThisShim.setTimeout.bind(globalThisShim);
13351
+ obj.clearTimeoutFn = globalThisShim.clearTimeout.bind(globalThisShim);
13352
+ }
13353
+ }
13354
+ // base64 encoded buffers are about 33% bigger (https://en.wikipedia.org/wiki/Base64)
13355
+ const BASE64_OVERHEAD = 1.33;
13356
+ // we could also have used `new Blob([obj]).size`, but it isn't supported in IE9
13357
+ function byteLength(obj) {
13358
+ if (typeof obj === "string") {
13359
+ return utf8Length(obj);
13360
+ }
13361
+ // arraybuffer or blob
13362
+ return Math.ceil((obj.byteLength || obj.size) * BASE64_OVERHEAD);
13363
+ }
13364
+ function utf8Length(str) {
13365
+ let c = 0, length = 0;
13366
+ for (let i = 0, l = str.length; i < l; i++) {
13367
+ c = str.charCodeAt(i);
13368
+ if (c < 0x80) {
13369
+ length += 1;
13370
+ }
13371
+ else if (c < 0x800) {
13372
+ length += 2;
13373
+ }
13374
+ else if (c < 0xd800 || c >= 0xe000) {
13375
+ length += 3;
13376
+ }
13377
+ else {
13378
+ i++;
13379
+ length += 4;
13380
+ }
13381
+ }
13382
+ return length;
13383
+ }
13384
+ /**
13385
+ * Generates a random 8-characters string.
13386
+ */
13387
+ function randomString() {
13388
+ return (Date.now().toString(36).substring(3) +
13389
+ Math.random().toString(36).substring(2, 5));
13390
+ }
13391
+
13392
+ // imported from https://github.com/galkn/querystring
13393
+ /**
13394
+ * Compiles a querystring
13395
+ * Returns string representation of the object
13396
+ *
13397
+ * @param {Object}
13398
+ * @api private
13399
+ */
13400
+ function encode(obj) {
13401
+ let str = '';
13402
+ for (let i in obj) {
13403
+ if (obj.hasOwnProperty(i)) {
13404
+ if (str.length)
13405
+ str += '&';
13406
+ str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
13407
+ }
13408
+ }
13409
+ return str;
13410
+ }
13411
+ /**
13412
+ * Parses a simple querystring into an object
13413
+ *
13414
+ * @param {String} qs
13415
+ * @api private
13416
+ */
13417
+ function decode(qs) {
13418
+ let qry = {};
13419
+ let pairs = qs.split('&');
13420
+ for (let i = 0, l = pairs.length; i < l; i++) {
13421
+ let pair = pairs[i].split('=');
13422
+ qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
13423
+ }
13424
+ return qry;
13425
+ }
13426
+
13427
+ class TransportError extends Error {
13428
+ constructor(reason, description, context) {
13429
+ super(reason);
13430
+ this.description = description;
13431
+ this.context = context;
13432
+ this.type = "TransportError";
13433
+ }
13434
+ }
13435
+ class Transport extends Emitter {
13436
+ /**
13437
+ * Transport abstract constructor.
13438
+ *
13439
+ * @param {Object} opts - options
13440
+ * @protected
13441
+ */
13442
+ constructor(opts) {
13443
+ super();
13444
+ this.writable = false;
13445
+ installTimerFunctions(this, opts);
13446
+ this.opts = opts;
13447
+ this.query = opts.query;
13448
+ this.socket = opts.socket;
13449
+ this.supportsBinary = !opts.forceBase64;
13450
+ }
13451
+ /**
13452
+ * Emits an error.
13453
+ *
13454
+ * @param {String} reason
13455
+ * @param description
13456
+ * @param context - the error context
13457
+ * @return {Transport} for chaining
13458
+ * @protected
13459
+ */
13460
+ onError(reason, description, context) {
13461
+ super.emitReserved("error", new TransportError(reason, description, context));
13462
+ return this;
13463
+ }
13464
+ /**
13465
+ * Opens the transport.
13466
+ */
13467
+ open() {
13468
+ this.readyState = "opening";
13469
+ this.doOpen();
13470
+ return this;
13471
+ }
13472
+ /**
13473
+ * Closes the transport.
13474
+ */
13475
+ close() {
13476
+ if (this.readyState === "opening" || this.readyState === "open") {
13477
+ this.doClose();
13478
+ this.onClose();
13479
+ }
13480
+ return this;
13481
+ }
13482
+ /**
13483
+ * Sends multiple packets.
13484
+ *
13485
+ * @param {Array} packets
13486
+ */
13487
+ send(packets) {
13488
+ if (this.readyState === "open") {
13489
+ this.write(packets);
13490
+ }
13491
+ }
13492
+ /**
13493
+ * Called upon open
13494
+ *
13495
+ * @protected
13496
+ */
13497
+ onOpen() {
13498
+ this.readyState = "open";
13499
+ this.writable = true;
13500
+ super.emitReserved("open");
13501
+ }
13502
+ /**
13503
+ * Called with data.
13504
+ *
13505
+ * @param {String} data
13506
+ * @protected
13507
+ */
13508
+ onData(data) {
13509
+ const packet = decodePacket(data, this.socket.binaryType);
13510
+ this.onPacket(packet);
13511
+ }
13512
+ /**
13513
+ * Called with a decoded packet.
13514
+ *
13515
+ * @protected
13516
+ */
13517
+ onPacket(packet) {
13518
+ super.emitReserved("packet", packet);
13519
+ }
13520
+ /**
13521
+ * Called upon close.
13522
+ *
13523
+ * @protected
13524
+ */
13525
+ onClose(details) {
13526
+ this.readyState = "closed";
13527
+ super.emitReserved("close", details);
13528
+ }
13529
+ /**
13530
+ * Pauses the transport, in order not to lose packets during an upgrade.
13531
+ *
13532
+ * @param onPause
13533
+ */
13534
+ pause(onPause) { }
13535
+ createUri(schema, query = {}) {
13536
+ return (schema +
13537
+ "://" +
13538
+ this._hostname() +
13539
+ this._port() +
13540
+ this.opts.path +
13541
+ this._query(query));
13542
+ }
13543
+ _hostname() {
13544
+ const hostname = this.opts.hostname;
13545
+ return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]";
13546
+ }
13547
+ _port() {
13548
+ if (this.opts.port &&
13549
+ ((this.opts.secure && Number(this.opts.port !== 443)) ||
13550
+ (!this.opts.secure && Number(this.opts.port) !== 80))) {
13551
+ return ":" + this.opts.port;
13552
+ }
13553
+ else {
13554
+ return "";
13555
+ }
13556
+ }
13557
+ _query(query) {
13558
+ const encodedQuery = encode(query);
13559
+ return encodedQuery.length ? "?" + encodedQuery : "";
13560
+ }
13561
+ }
13562
+
13563
+ class Polling extends Transport {
13564
+ constructor() {
13565
+ super(...arguments);
13566
+ this._polling = false;
13567
+ }
13568
+ get name() {
13569
+ return "polling";
13570
+ }
13571
+ /**
13572
+ * Opens the socket (triggers polling). We write a PING message to determine
13573
+ * when the transport is open.
13574
+ *
13575
+ * @protected
13576
+ */
13577
+ doOpen() {
13578
+ this._poll();
13579
+ }
13580
+ /**
13581
+ * Pauses polling.
13582
+ *
13583
+ * @param {Function} onPause - callback upon buffers are flushed and transport is paused
13584
+ * @package
13585
+ */
13586
+ pause(onPause) {
13587
+ this.readyState = "pausing";
13588
+ const pause = () => {
13589
+ this.readyState = "paused";
13590
+ onPause();
13591
+ };
13592
+ if (this._polling || !this.writable) {
13593
+ let total = 0;
13594
+ if (this._polling) {
13595
+ total++;
13596
+ this.once("pollComplete", function () {
13597
+ --total || pause();
13598
+ });
13599
+ }
13600
+ if (!this.writable) {
13601
+ total++;
13602
+ this.once("drain", function () {
13603
+ --total || pause();
13604
+ });
13605
+ }
13606
+ }
13607
+ else {
13608
+ pause();
13609
+ }
13610
+ }
13611
+ /**
13612
+ * Starts polling cycle.
13613
+ *
13614
+ * @private
13615
+ */
13616
+ _poll() {
13617
+ this._polling = true;
13618
+ this.doPoll();
13619
+ this.emitReserved("poll");
13620
+ }
13621
+ /**
13622
+ * Overloads onData to detect payloads.
13623
+ *
13624
+ * @protected
13625
+ */
13626
+ onData(data) {
13627
+ const callback = (packet) => {
13628
+ // if its the first message we consider the transport open
13629
+ if ("opening" === this.readyState && packet.type === "open") {
13630
+ this.onOpen();
13631
+ }
13632
+ // if its a close packet, we close the ongoing requests
13633
+ if ("close" === packet.type) {
13634
+ this.onClose({ description: "transport closed by the server" });
13635
+ return false;
13636
+ }
13637
+ // otherwise bypass onData and handle the message
13638
+ this.onPacket(packet);
13639
+ };
13640
+ // decode payload
13641
+ decodePayload(data, this.socket.binaryType).forEach(callback);
13642
+ // if an event did not trigger closing
13643
+ if ("closed" !== this.readyState) {
13644
+ // if we got data we're not polling
13645
+ this._polling = false;
13646
+ this.emitReserved("pollComplete");
13647
+ if ("open" === this.readyState) {
13648
+ this._poll();
13649
+ }
13650
+ }
13651
+ }
13652
+ /**
13653
+ * For polling, send a close packet.
13654
+ *
13655
+ * @protected
13656
+ */
13657
+ doClose() {
13658
+ const close = () => {
13659
+ this.write([{ type: "close" }]);
13660
+ };
13661
+ if ("open" === this.readyState) {
13662
+ close();
13663
+ }
13664
+ else {
13665
+ // in case we're trying to close while
13666
+ // handshaking is in progress (GH-164)
13667
+ this.once("open", close);
13668
+ }
13669
+ }
13670
+ /**
13671
+ * Writes a packets payload.
13672
+ *
13673
+ * @param {Array} packets - data packets
13674
+ * @protected
13675
+ */
13676
+ write(packets) {
13677
+ this.writable = false;
13678
+ encodePayload(packets, (data) => {
13679
+ this.doWrite(data, () => {
13680
+ this.writable = true;
13681
+ this.emitReserved("drain");
13682
+ });
13683
+ });
13684
+ }
13685
+ /**
13686
+ * Generates uri for connection.
13687
+ *
13688
+ * @private
13689
+ */
13690
+ uri() {
13691
+ const schema = this.opts.secure ? "https" : "http";
13692
+ const query = this.query || {};
13693
+ // cache busting is forced
13694
+ if (false !== this.opts.timestampRequests) {
13695
+ query[this.opts.timestampParam] = randomString();
13696
+ }
13697
+ if (!this.supportsBinary && !query.sid) {
13698
+ query.b64 = 1;
13699
+ }
13700
+ return this.createUri(schema, query);
13701
+ }
13702
+ }
13703
+
13704
+ // imported from https://github.com/component/has-cors
13705
+ let value = false;
13706
+ try {
13707
+ value = typeof XMLHttpRequest !== 'undefined' &&
13708
+ 'withCredentials' in new XMLHttpRequest();
13709
+ }
13710
+ catch (err) {
13711
+ // if XMLHttp support is disabled in IE then it will throw
13712
+ // when trying to create
13713
+ }
13714
+ const hasCORS = value;
13715
+
13716
+ function empty() { }
13717
+ class BaseXHR extends Polling {
13718
+ /**
13719
+ * XHR Polling constructor.
13720
+ *
13721
+ * @param {Object} opts
13722
+ * @package
13723
+ */
13724
+ constructor(opts) {
13725
+ super(opts);
13726
+ if (typeof location !== "undefined") {
13727
+ const isSSL = "https:" === location.protocol;
13728
+ let port = location.port;
13729
+ // some user agents have empty `location.port`
13730
+ if (!port) {
13731
+ port = isSSL ? "443" : "80";
13732
+ }
13733
+ this.xd =
13734
+ (typeof location !== "undefined" &&
13735
+ opts.hostname !== location.hostname) ||
13736
+ port !== opts.port;
13737
+ }
13738
+ }
13739
+ /**
13740
+ * Sends data.
13741
+ *
13742
+ * @param {String} data to send.
13743
+ * @param {Function} called upon flush.
13744
+ * @private
13745
+ */
13746
+ doWrite(data, fn) {
13747
+ const req = this.request({
13748
+ method: "POST",
13749
+ data: data,
13750
+ });
13751
+ req.on("success", fn);
13752
+ req.on("error", (xhrStatus, context) => {
13753
+ this.onError("xhr post error", xhrStatus, context);
13754
+ });
13755
+ }
13756
+ /**
13757
+ * Starts a poll cycle.
13758
+ *
13759
+ * @private
13760
+ */
13761
+ doPoll() {
13762
+ const req = this.request();
13763
+ req.on("data", this.onData.bind(this));
13764
+ req.on("error", (xhrStatus, context) => {
13765
+ this.onError("xhr poll error", xhrStatus, context);
13766
+ });
13767
+ this.pollXhr = req;
13768
+ }
13769
+ }
13770
+ class Request extends Emitter {
13771
+ /**
13772
+ * Request constructor
13773
+ *
13774
+ * @param {Object} options
13775
+ * @package
13776
+ */
13777
+ constructor(createRequest, uri, opts) {
13778
+ super();
13779
+ this.createRequest = createRequest;
13780
+ installTimerFunctions(this, opts);
13781
+ this._opts = opts;
13782
+ this._method = opts.method || "GET";
13783
+ this._uri = uri;
13784
+ this._data = undefined !== opts.data ? opts.data : null;
13785
+ this._create();
13786
+ }
13787
+ /**
13788
+ * Creates the XHR object and sends the request.
13789
+ *
13790
+ * @private
13791
+ */
13792
+ _create() {
13793
+ var _a;
13794
+ const opts = pick(this._opts, "agent", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "autoUnref");
13795
+ opts.xdomain = !!this._opts.xd;
13796
+ const xhr = (this._xhr = this.createRequest(opts));
13797
+ try {
13798
+ xhr.open(this._method, this._uri, true);
13799
+ try {
13800
+ if (this._opts.extraHeaders) {
13801
+ // @ts-ignore
13802
+ xhr.setDisableHeaderCheck && xhr.setDisableHeaderCheck(true);
13803
+ for (let i in this._opts.extraHeaders) {
13804
+ if (this._opts.extraHeaders.hasOwnProperty(i)) {
13805
+ xhr.setRequestHeader(i, this._opts.extraHeaders[i]);
13806
+ }
13807
+ }
13808
+ }
13809
+ }
13810
+ catch (e) { }
13811
+ if ("POST" === this._method) {
13812
+ try {
13813
+ xhr.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
13814
+ }
13815
+ catch (e) { }
13816
+ }
13817
+ try {
13818
+ xhr.setRequestHeader("Accept", "*/*");
13819
+ }
13820
+ catch (e) { }
13821
+ (_a = this._opts.cookieJar) === null || _a === void 0 ? void 0 : _a.addCookies(xhr);
13822
+ // ie6 check
13823
+ if ("withCredentials" in xhr) {
13824
+ xhr.withCredentials = this._opts.withCredentials;
13825
+ }
13826
+ if (this._opts.requestTimeout) {
13827
+ xhr.timeout = this._opts.requestTimeout;
13828
+ }
13829
+ xhr.onreadystatechange = () => {
13830
+ var _a;
13831
+ if (xhr.readyState === 3) {
13832
+ (_a = this._opts.cookieJar) === null || _a === void 0 ? void 0 : _a.parseCookies(
13833
+ // @ts-ignore
13834
+ xhr.getResponseHeader("set-cookie"));
13835
+ }
13836
+ if (4 !== xhr.readyState)
13837
+ return;
13838
+ if (200 === xhr.status || 1223 === xhr.status) {
13839
+ this._onLoad();
13840
+ }
13841
+ else {
13842
+ // make sure the `error` event handler that's user-set
13843
+ // does not throw in the same tick and gets caught here
13844
+ this.setTimeoutFn(() => {
13845
+ this._onError(typeof xhr.status === "number" ? xhr.status : 0);
13846
+ }, 0);
13847
+ }
13848
+ };
13849
+ xhr.send(this._data);
13850
+ }
13851
+ catch (e) {
13852
+ // Need to defer since .create() is called directly from the constructor
13853
+ // and thus the 'error' event can only be only bound *after* this exception
13854
+ // occurs. Therefore, also, we cannot throw here at all.
13855
+ this.setTimeoutFn(() => {
13856
+ this._onError(e);
13857
+ }, 0);
13858
+ return;
13859
+ }
13860
+ if (typeof document !== "undefined") {
13861
+ this._index = Request.requestsCount++;
13862
+ Request.requests[this._index] = this;
13863
+ }
13864
+ }
13865
+ /**
13866
+ * Called upon error.
13867
+ *
13868
+ * @private
13869
+ */
13870
+ _onError(err) {
13871
+ this.emitReserved("error", err, this._xhr);
13872
+ this._cleanup(true);
13873
+ }
13874
+ /**
13875
+ * Cleans up house.
13876
+ *
13877
+ * @private
13878
+ */
13879
+ _cleanup(fromError) {
13880
+ if ("undefined" === typeof this._xhr || null === this._xhr) {
13881
+ return;
13882
+ }
13883
+ this._xhr.onreadystatechange = empty;
13884
+ if (fromError) {
13885
+ try {
13886
+ this._xhr.abort();
13887
+ }
13888
+ catch (e) { }
13889
+ }
13890
+ if (typeof document !== "undefined") {
13891
+ delete Request.requests[this._index];
13892
+ }
13893
+ this._xhr = null;
13894
+ }
13895
+ /**
13896
+ * Called upon load.
13897
+ *
13898
+ * @private
13899
+ */
13900
+ _onLoad() {
13901
+ const data = this._xhr.responseText;
13902
+ if (data !== null) {
13903
+ this.emitReserved("data", data);
13904
+ this.emitReserved("success");
13905
+ this._cleanup();
13906
+ }
13907
+ }
13908
+ /**
13909
+ * Aborts the request.
13910
+ *
13911
+ * @package
13912
+ */
13913
+ abort() {
13914
+ this._cleanup();
13915
+ }
13916
+ }
13917
+ Request.requestsCount = 0;
13918
+ Request.requests = {};
13919
+ /**
13920
+ * Aborts pending requests when unloading the window. This is needed to prevent
13921
+ * memory leaks (e.g. when using IE) and to ensure that no spurious error is
13922
+ * emitted.
13923
+ */
13924
+ if (typeof document !== "undefined") {
13925
+ // @ts-ignore
13926
+ if (typeof attachEvent === "function") {
13927
+ // @ts-ignore
13928
+ attachEvent("onunload", unloadHandler);
13929
+ }
13930
+ else if (typeof addEventListener === "function") {
13931
+ const terminationEvent = "onpagehide" in globalThisShim ? "pagehide" : "unload";
13932
+ addEventListener(terminationEvent, unloadHandler, false);
13933
+ }
13934
+ }
13935
+ function unloadHandler() {
13936
+ for (let i in Request.requests) {
13937
+ if (Request.requests.hasOwnProperty(i)) {
13938
+ Request.requests[i].abort();
13939
+ }
13940
+ }
13941
+ }
13942
+ const hasXHR2 = (function () {
13943
+ const xhr = newRequest({
13944
+ xdomain: false,
13945
+ });
13946
+ return xhr && xhr.responseType !== null;
13947
+ })();
13948
+ /**
13949
+ * HTTP long-polling based on the built-in `XMLHttpRequest` object.
13950
+ *
13951
+ * Usage: browser
13952
+ *
13953
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
13954
+ */
13955
+ class XHR extends BaseXHR {
13956
+ constructor(opts) {
13957
+ super(opts);
13958
+ const forceBase64 = opts && opts.forceBase64;
13959
+ this.supportsBinary = hasXHR2 && !forceBase64;
13960
+ }
13961
+ request(opts = {}) {
13962
+ Object.assign(opts, { xd: this.xd }, this.opts);
13963
+ return new Request(newRequest, this.uri(), opts);
13964
+ }
13965
+ }
13966
+ function newRequest(opts) {
13967
+ const xdomain = opts.xdomain;
13968
+ // XMLHttpRequest can be disabled on IE
13969
+ try {
13970
+ if ("undefined" !== typeof XMLHttpRequest && (!xdomain || hasCORS)) {
13971
+ return new XMLHttpRequest();
13972
+ }
13973
+ }
13974
+ catch (e) { }
13975
+ if (!xdomain) {
13976
+ try {
13977
+ return new globalThisShim[["Active"].concat("Object").join("X")]("Microsoft.XMLHTTP");
13978
+ }
13979
+ catch (e) { }
13980
+ }
13981
+ }
13982
+
13983
+ // detect ReactNative environment
13984
+ const isReactNative = typeof navigator !== "undefined" &&
13985
+ typeof navigator.product === "string" &&
13986
+ navigator.product.toLowerCase() === "reactnative";
13987
+ class BaseWS extends Transport {
13988
+ get name() {
13989
+ return "websocket";
13990
+ }
13991
+ doOpen() {
13992
+ const uri = this.uri();
13993
+ const protocols = this.opts.protocols;
13994
+ // React Native only supports the 'headers' option, and will print a warning if anything else is passed
13995
+ const opts = isReactNative
13996
+ ? {}
13997
+ : pick(this.opts, "agent", "perMessageDeflate", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "localAddress", "protocolVersion", "origin", "maxPayload", "family", "checkServerIdentity");
13998
+ if (this.opts.extraHeaders) {
13999
+ opts.headers = this.opts.extraHeaders;
14000
+ }
14001
+ try {
14002
+ this.ws = this.createSocket(uri, protocols, opts);
14003
+ }
14004
+ catch (err) {
14005
+ return this.emitReserved("error", err);
14006
+ }
14007
+ this.ws.binaryType = this.socket.binaryType;
14008
+ this.addEventListeners();
14009
+ }
14010
+ /**
14011
+ * Adds event listeners to the socket
14012
+ *
14013
+ * @private
14014
+ */
14015
+ addEventListeners() {
14016
+ this.ws.onopen = () => {
14017
+ if (this.opts.autoUnref) {
14018
+ this.ws._socket.unref();
14019
+ }
14020
+ this.onOpen();
14021
+ };
14022
+ this.ws.onclose = (closeEvent) => this.onClose({
14023
+ description: "websocket connection closed",
14024
+ context: closeEvent,
14025
+ });
14026
+ this.ws.onmessage = (ev) => this.onData(ev.data);
14027
+ this.ws.onerror = (e) => this.onError("websocket error", e);
14028
+ }
14029
+ write(packets) {
14030
+ this.writable = false;
14031
+ // encodePacket efficient as it uses WS framing
14032
+ // no need for encodePayload
14033
+ for (let i = 0; i < packets.length; i++) {
14034
+ const packet = packets[i];
14035
+ const lastPacket = i === packets.length - 1;
14036
+ encodePacket(packet, this.supportsBinary, (data) => {
14037
+ // Sometimes the websocket has already been closed but the browser didn't
14038
+ // have a chance of informing us about it yet, in that case send will
14039
+ // throw an error
14040
+ try {
14041
+ this.doWrite(packet, data);
14042
+ }
14043
+ catch (e) {
14044
+ }
14045
+ if (lastPacket) {
14046
+ // fake drain
14047
+ // defer to next tick to allow Socket to clear writeBuffer
14048
+ nextTick(() => {
14049
+ this.writable = true;
14050
+ this.emitReserved("drain");
14051
+ }, this.setTimeoutFn);
14052
+ }
14053
+ });
14054
+ }
14055
+ }
14056
+ doClose() {
14057
+ if (typeof this.ws !== "undefined") {
14058
+ this.ws.onerror = () => { };
14059
+ this.ws.close();
14060
+ this.ws = null;
14061
+ }
14062
+ }
14063
+ /**
14064
+ * Generates uri for connection.
14065
+ *
14066
+ * @private
14067
+ */
14068
+ uri() {
14069
+ const schema = this.opts.secure ? "wss" : "ws";
14070
+ const query = this.query || {};
14071
+ // append timestamp to URI
14072
+ if (this.opts.timestampRequests) {
14073
+ query[this.opts.timestampParam] = randomString();
14074
+ }
14075
+ // communicate binary support capabilities
14076
+ if (!this.supportsBinary) {
14077
+ query.b64 = 1;
14078
+ }
14079
+ return this.createUri(schema, query);
14080
+ }
14081
+ }
14082
+ const WebSocketCtor = globalThisShim.WebSocket || globalThisShim.MozWebSocket;
14083
+ /**
14084
+ * WebSocket transport based on the built-in `WebSocket` object.
14085
+ *
14086
+ * Usage: browser, Node.js (since v21), Deno, Bun
14087
+ *
14088
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
14089
+ * @see https://caniuse.com/mdn-api_websocket
14090
+ * @see https://nodejs.org/api/globals.html#websocket
14091
+ */
14092
+ class WS extends BaseWS {
14093
+ createSocket(uri, protocols, opts) {
14094
+ return !isReactNative
14095
+ ? protocols
14096
+ ? new WebSocketCtor(uri, protocols)
14097
+ : new WebSocketCtor(uri)
14098
+ : new WebSocketCtor(uri, protocols, opts);
14099
+ }
14100
+ doWrite(_packet, data) {
14101
+ this.ws.send(data);
14102
+ }
14103
+ }
14104
+
14105
+ /**
14106
+ * WebTransport transport based on the built-in `WebTransport` object.
14107
+ *
14108
+ * Usage: browser, Node.js (with the `@fails-components/webtransport` package)
14109
+ *
14110
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebTransport
14111
+ * @see https://caniuse.com/webtransport
14112
+ */
14113
+ class WT extends Transport {
14114
+ get name() {
14115
+ return "webtransport";
14116
+ }
14117
+ doOpen() {
14118
+ try {
14119
+ // @ts-ignore
14120
+ this._transport = new WebTransport(this.createUri("https"), this.opts.transportOptions[this.name]);
14121
+ }
14122
+ catch (err) {
14123
+ return this.emitReserved("error", err);
14124
+ }
14125
+ this._transport.closed
14126
+ .then(() => {
14127
+ this.onClose();
14128
+ })
14129
+ .catch((err) => {
14130
+ this.onError("webtransport error", err);
14131
+ });
14132
+ // note: we could have used async/await, but that would require some additional polyfills
14133
+ this._transport.ready.then(() => {
14134
+ this._transport.createBidirectionalStream().then((stream) => {
14135
+ const decoderStream = createPacketDecoderStream(Number.MAX_SAFE_INTEGER, this.socket.binaryType);
14136
+ const reader = stream.readable.pipeThrough(decoderStream).getReader();
14137
+ const encoderStream = createPacketEncoderStream();
14138
+ encoderStream.readable.pipeTo(stream.writable);
14139
+ this._writer = encoderStream.writable.getWriter();
14140
+ const read = () => {
14141
+ reader
14142
+ .read()
14143
+ .then(({ done, value }) => {
14144
+ if (done) {
14145
+ return;
14146
+ }
14147
+ this.onPacket(value);
14148
+ read();
14149
+ })
14150
+ .catch((err) => {
14151
+ });
14152
+ };
14153
+ read();
14154
+ const packet = { type: "open" };
14155
+ if (this.query.sid) {
14156
+ packet.data = `{"sid":"${this.query.sid}"}`;
14157
+ }
14158
+ this._writer.write(packet).then(() => this.onOpen());
14159
+ });
14160
+ });
14161
+ }
14162
+ write(packets) {
14163
+ this.writable = false;
14164
+ for (let i = 0; i < packets.length; i++) {
14165
+ const packet = packets[i];
14166
+ const lastPacket = i === packets.length - 1;
14167
+ this._writer.write(packet).then(() => {
14168
+ if (lastPacket) {
14169
+ nextTick(() => {
14170
+ this.writable = true;
14171
+ this.emitReserved("drain");
14172
+ }, this.setTimeoutFn);
14173
+ }
14174
+ });
14175
+ }
14176
+ }
14177
+ doClose() {
14178
+ var _a;
14179
+ (_a = this._transport) === null || _a === void 0 ? void 0 : _a.close();
14180
+ }
14181
+ }
14182
+
14183
+ const transports = {
14184
+ websocket: WS,
14185
+ webtransport: WT,
14186
+ polling: XHR,
14187
+ };
14188
+
14189
+ // imported from https://github.com/galkn/parseuri
14190
+ /**
14191
+ * Parses a URI
14192
+ *
14193
+ * Note: we could also have used the built-in URL object, but it isn't supported on all platforms.
14194
+ *
14195
+ * See:
14196
+ * - https://developer.mozilla.org/en-US/docs/Web/API/URL
14197
+ * - https://caniuse.com/url
14198
+ * - https://www.rfc-editor.org/rfc/rfc3986#appendix-B
14199
+ *
14200
+ * History of the parse() method:
14201
+ * - first commit: https://github.com/socketio/socket.io-client/commit/4ee1d5d94b3906a9c052b459f1a818b15f38f91c
14202
+ * - export into its own module: https://github.com/socketio/engine.io-client/commit/de2c561e4564efeb78f1bdb1ba39ef81b2822cb3
14203
+ * - reimport: https://github.com/socketio/engine.io-client/commit/df32277c3f6d622eec5ed09f493cae3f3391d242
14204
+ *
14205
+ * @author Steven Levithan <stevenlevithan.com> (MIT license)
14206
+ * @api private
14207
+ */
14208
+ const re = /^(?:(?![^:@\/?#]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@\/?#]*)(?::([^:@\/?#]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
14209
+ const parts = [
14210
+ 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
14211
+ ];
14212
+ function parse(str) {
14213
+ if (str.length > 8000) {
14214
+ throw "URI too long";
14215
+ }
14216
+ const src = str, b = str.indexOf('['), e = str.indexOf(']');
14217
+ if (b != -1 && e != -1) {
14218
+ str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
14219
+ }
14220
+ let m = re.exec(str || ''), uri = {}, i = 14;
14221
+ while (i--) {
14222
+ uri[parts[i]] = m[i] || '';
14223
+ }
14224
+ if (b != -1 && e != -1) {
14225
+ uri.source = src;
14226
+ uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
14227
+ uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
14228
+ uri.ipv6uri = true;
14229
+ }
14230
+ uri.pathNames = pathNames(uri, uri['path']);
14231
+ uri.queryKey = queryKey(uri, uri['query']);
14232
+ return uri;
14233
+ }
14234
+ function pathNames(obj, path) {
14235
+ const regx = /\/{2,9}/g, names = path.replace(regx, "/").split("/");
14236
+ if (path.slice(0, 1) == '/' || path.length === 0) {
14237
+ names.splice(0, 1);
14238
+ }
14239
+ if (path.slice(-1) == '/') {
14240
+ names.splice(names.length - 1, 1);
14241
+ }
14242
+ return names;
14243
+ }
14244
+ function queryKey(uri, query) {
14245
+ const data = {};
14246
+ query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) {
14247
+ if ($1) {
14248
+ data[$1] = $2;
14249
+ }
14250
+ });
14251
+ return data;
14252
+ }
14253
+
14254
+ const withEventListeners = typeof addEventListener === "function" &&
14255
+ typeof removeEventListener === "function";
14256
+ const OFFLINE_EVENT_LISTENERS = [];
14257
+ if (withEventListeners) {
14258
+ // within a ServiceWorker, any event handler for the 'offline' event must be added on the initial evaluation of the
14259
+ // script, so we create one single event listener here which will forward the event to the socket instances
14260
+ addEventListener("offline", () => {
14261
+ OFFLINE_EVENT_LISTENERS.forEach((listener) => listener());
14262
+ }, false);
14263
+ }
14264
+ /**
14265
+ * This class provides a WebSocket-like interface to connect to an Engine.IO server. The connection will be established
14266
+ * with one of the available low-level transports, like HTTP long-polling, WebSocket or WebTransport.
14267
+ *
14268
+ * This class comes without upgrade mechanism, which means that it will keep the first low-level transport that
14269
+ * successfully establishes the connection.
14270
+ *
14271
+ * In order to allow tree-shaking, there are no transports included, that's why the `transports` option is mandatory.
14272
+ *
14273
+ * @example
14274
+ * import { SocketWithoutUpgrade, WebSocket } from "engine.io-client";
14275
+ *
14276
+ * const socket = new SocketWithoutUpgrade({
14277
+ * transports: [WebSocket]
14278
+ * });
14279
+ *
14280
+ * socket.on("open", () => {
14281
+ * socket.send("hello");
14282
+ * });
14283
+ *
14284
+ * @see SocketWithUpgrade
14285
+ * @see Socket
14286
+ */
14287
+ class SocketWithoutUpgrade extends Emitter {
14288
+ /**
14289
+ * Socket constructor.
14290
+ *
14291
+ * @param {String|Object} uri - uri or options
14292
+ * @param {Object} opts - options
14293
+ */
14294
+ constructor(uri, opts) {
14295
+ super();
14296
+ this.binaryType = defaultBinaryType;
14297
+ this.writeBuffer = [];
14298
+ this._prevBufferLen = 0;
14299
+ this._pingInterval = -1;
14300
+ this._pingTimeout = -1;
14301
+ this._maxPayload = -1;
14302
+ /**
14303
+ * The expiration timestamp of the {@link _pingTimeoutTimer} object is tracked, in case the timer is throttled and the
14304
+ * callback is not fired on time. This can happen for example when a laptop is suspended or when a phone is locked.
14305
+ */
14306
+ this._pingTimeoutTime = Infinity;
14307
+ if (uri && "object" === typeof uri) {
14308
+ opts = uri;
14309
+ uri = null;
14310
+ }
14311
+ if (uri) {
14312
+ const parsedUri = parse(uri);
14313
+ opts.hostname = parsedUri.host;
14314
+ opts.secure =
14315
+ parsedUri.protocol === "https" || parsedUri.protocol === "wss";
14316
+ opts.port = parsedUri.port;
14317
+ if (parsedUri.query)
14318
+ opts.query = parsedUri.query;
14319
+ }
14320
+ else if (opts.host) {
14321
+ opts.hostname = parse(opts.host).host;
14322
+ }
14323
+ installTimerFunctions(this, opts);
14324
+ this.secure =
14325
+ null != opts.secure
14326
+ ? opts.secure
14327
+ : typeof location !== "undefined" && "https:" === location.protocol;
14328
+ if (opts.hostname && !opts.port) {
14329
+ // if no port is specified manually, use the protocol default
14330
+ opts.port = this.secure ? "443" : "80";
14331
+ }
14332
+ this.hostname =
14333
+ opts.hostname ||
14334
+ (typeof location !== "undefined" ? location.hostname : "localhost");
14335
+ this.port =
14336
+ opts.port ||
14337
+ (typeof location !== "undefined" && location.port
14338
+ ? location.port
14339
+ : this.secure
14340
+ ? "443"
14341
+ : "80");
14342
+ this.transports = [];
14343
+ this._transportsByName = {};
14344
+ opts.transports.forEach((t) => {
14345
+ const transportName = t.prototype.name;
14346
+ this.transports.push(transportName);
14347
+ this._transportsByName[transportName] = t;
14348
+ });
14349
+ this.opts = Object.assign({
14350
+ path: "/engine.io",
14351
+ agent: false,
14352
+ withCredentials: false,
14353
+ upgrade: true,
14354
+ timestampParam: "t",
14355
+ rememberUpgrade: false,
14356
+ addTrailingSlash: true,
14357
+ rejectUnauthorized: true,
14358
+ perMessageDeflate: {
14359
+ threshold: 1024,
14360
+ },
14361
+ transportOptions: {},
14362
+ closeOnBeforeunload: false,
14363
+ }, opts);
14364
+ this.opts.path =
14365
+ this.opts.path.replace(/\/$/, "") +
14366
+ (this.opts.addTrailingSlash ? "/" : "");
14367
+ if (typeof this.opts.query === "string") {
14368
+ this.opts.query = decode(this.opts.query);
14369
+ }
14370
+ if (withEventListeners) {
14371
+ if (this.opts.closeOnBeforeunload) {
14372
+ // Firefox closes the connection when the "beforeunload" event is emitted but not Chrome. This event listener
14373
+ // ensures every browser behaves the same (no "disconnect" event at the Socket.IO level when the page is
14374
+ // closed/reloaded)
14375
+ this._beforeunloadEventListener = () => {
14376
+ if (this.transport) {
14377
+ // silently close the transport
14378
+ this.transport.removeAllListeners();
14379
+ this.transport.close();
14380
+ }
14381
+ };
14382
+ addEventListener("beforeunload", this._beforeunloadEventListener, false);
14383
+ }
14384
+ if (this.hostname !== "localhost") {
14385
+ this._offlineEventListener = () => {
14386
+ this._onClose("transport close", {
14387
+ description: "network connection lost",
14388
+ });
14389
+ };
14390
+ OFFLINE_EVENT_LISTENERS.push(this._offlineEventListener);
14391
+ }
14392
+ }
14393
+ if (this.opts.withCredentials) {
14394
+ this._cookieJar = createCookieJar();
14395
+ }
14396
+ this._open();
14397
+ }
14398
+ /**
14399
+ * Creates transport of the given type.
14400
+ *
14401
+ * @param {String} name - transport name
14402
+ * @return {Transport}
14403
+ * @private
14404
+ */
14405
+ createTransport(name) {
14406
+ const query = Object.assign({}, this.opts.query);
14407
+ // append engine.io protocol identifier
14408
+ query.EIO = protocol$1;
14409
+ // transport name
14410
+ query.transport = name;
14411
+ // session id if we already have one
14412
+ if (this.id)
14413
+ query.sid = this.id;
14414
+ const opts = Object.assign({}, this.opts, {
14415
+ query,
14416
+ socket: this,
14417
+ hostname: this.hostname,
14418
+ secure: this.secure,
14419
+ port: this.port,
14420
+ }, this.opts.transportOptions[name]);
14421
+ return new this._transportsByName[name](opts);
14422
+ }
14423
+ /**
14424
+ * Initializes transport to use and starts probe.
14425
+ *
14426
+ * @private
14427
+ */
14428
+ _open() {
14429
+ if (this.transports.length === 0) {
14430
+ // Emit error on next tick so it can be listened to
14431
+ this.setTimeoutFn(() => {
14432
+ this.emitReserved("error", "No transports available");
14433
+ }, 0);
14434
+ return;
14435
+ }
14436
+ const transportName = this.opts.rememberUpgrade &&
14437
+ SocketWithoutUpgrade.priorWebsocketSuccess &&
14438
+ this.transports.indexOf("websocket") !== -1
14439
+ ? "websocket"
14440
+ : this.transports[0];
14441
+ this.readyState = "opening";
14442
+ const transport = this.createTransport(transportName);
14443
+ transport.open();
14444
+ this.setTransport(transport);
14445
+ }
14446
+ /**
14447
+ * Sets the current transport. Disables the existing one (if any).
14448
+ *
14449
+ * @private
14450
+ */
14451
+ setTransport(transport) {
14452
+ if (this.transport) {
14453
+ this.transport.removeAllListeners();
14454
+ }
14455
+ // set up transport
14456
+ this.transport = transport;
14457
+ // set up transport listeners
14458
+ transport
14459
+ .on("drain", this._onDrain.bind(this))
14460
+ .on("packet", this._onPacket.bind(this))
14461
+ .on("error", this._onError.bind(this))
14462
+ .on("close", (reason) => this._onClose("transport close", reason));
14463
+ }
14464
+ /**
14465
+ * Called when connection is deemed open.
14466
+ *
14467
+ * @private
14468
+ */
14469
+ onOpen() {
14470
+ this.readyState = "open";
14471
+ SocketWithoutUpgrade.priorWebsocketSuccess =
14472
+ "websocket" === this.transport.name;
14473
+ this.emitReserved("open");
14474
+ this.flush();
14475
+ }
14476
+ /**
14477
+ * Handles a packet.
14478
+ *
14479
+ * @private
14480
+ */
14481
+ _onPacket(packet) {
14482
+ if ("opening" === this.readyState ||
14483
+ "open" === this.readyState ||
14484
+ "closing" === this.readyState) {
14485
+ this.emitReserved("packet", packet);
14486
+ // Socket is live - any packet counts
14487
+ this.emitReserved("heartbeat");
14488
+ switch (packet.type) {
14489
+ case "open":
14490
+ this.onHandshake(JSON.parse(packet.data));
14491
+ break;
14492
+ case "ping":
14493
+ this._sendPacket("pong");
14494
+ this.emitReserved("ping");
14495
+ this.emitReserved("pong");
14496
+ this._resetPingTimeout();
14497
+ break;
14498
+ case "error":
14499
+ const err = new Error("server error");
14500
+ // @ts-ignore
14501
+ err.code = packet.data;
14502
+ this._onError(err);
14503
+ break;
14504
+ case "message":
14505
+ this.emitReserved("data", packet.data);
14506
+ this.emitReserved("message", packet.data);
14507
+ break;
14508
+ }
14509
+ }
14510
+ }
14511
+ /**
14512
+ * Called upon handshake completion.
14513
+ *
14514
+ * @param {Object} data - handshake obj
14515
+ * @private
14516
+ */
14517
+ onHandshake(data) {
14518
+ this.emitReserved("handshake", data);
14519
+ this.id = data.sid;
14520
+ this.transport.query.sid = data.sid;
14521
+ this._pingInterval = data.pingInterval;
14522
+ this._pingTimeout = data.pingTimeout;
14523
+ this._maxPayload = data.maxPayload;
14524
+ this.onOpen();
14525
+ // In case open handler closes socket
14526
+ if ("closed" === this.readyState)
14527
+ return;
14528
+ this._resetPingTimeout();
14529
+ }
14530
+ /**
14531
+ * Sets and resets ping timeout timer based on server pings.
14532
+ *
14533
+ * @private
14534
+ */
14535
+ _resetPingTimeout() {
14536
+ this.clearTimeoutFn(this._pingTimeoutTimer);
14537
+ const delay = this._pingInterval + this._pingTimeout;
14538
+ this._pingTimeoutTime = Date.now() + delay;
14539
+ this._pingTimeoutTimer = this.setTimeoutFn(() => {
14540
+ this._onClose("ping timeout");
14541
+ }, delay);
14542
+ if (this.opts.autoUnref) {
14543
+ this._pingTimeoutTimer.unref();
14544
+ }
14545
+ }
14546
+ /**
14547
+ * Called on `drain` event
14548
+ *
14549
+ * @private
14550
+ */
14551
+ _onDrain() {
14552
+ this.writeBuffer.splice(0, this._prevBufferLen);
14553
+ // setting prevBufferLen = 0 is very important
14554
+ // for example, when upgrading, upgrade packet is sent over,
14555
+ // and a nonzero prevBufferLen could cause problems on `drain`
14556
+ this._prevBufferLen = 0;
14557
+ if (0 === this.writeBuffer.length) {
14558
+ this.emitReserved("drain");
14559
+ }
14560
+ else {
14561
+ this.flush();
14562
+ }
14563
+ }
14564
+ /**
14565
+ * Flush write buffers.
14566
+ *
14567
+ * @private
14568
+ */
14569
+ flush() {
14570
+ if ("closed" !== this.readyState &&
14571
+ this.transport.writable &&
14572
+ !this.upgrading &&
14573
+ this.writeBuffer.length) {
14574
+ const packets = this._getWritablePackets();
14575
+ this.transport.send(packets);
14576
+ // keep track of current length of writeBuffer
14577
+ // splice writeBuffer and callbackBuffer on `drain`
14578
+ this._prevBufferLen = packets.length;
14579
+ this.emitReserved("flush");
14580
+ }
14581
+ }
14582
+ /**
14583
+ * Ensure the encoded size of the writeBuffer is below the maxPayload value sent by the server (only for HTTP
14584
+ * long-polling)
14585
+ *
14586
+ * @private
14587
+ */
14588
+ _getWritablePackets() {
14589
+ const shouldCheckPayloadSize = this._maxPayload &&
14590
+ this.transport.name === "polling" &&
14591
+ this.writeBuffer.length > 1;
14592
+ if (!shouldCheckPayloadSize) {
14593
+ return this.writeBuffer;
14594
+ }
14595
+ let payloadSize = 1; // first packet type
14596
+ for (let i = 0; i < this.writeBuffer.length; i++) {
14597
+ const data = this.writeBuffer[i].data;
14598
+ if (data) {
14599
+ payloadSize += byteLength(data);
14600
+ }
14601
+ if (i > 0 && payloadSize > this._maxPayload) {
14602
+ return this.writeBuffer.slice(0, i);
14603
+ }
14604
+ payloadSize += 2; // separator + packet type
14605
+ }
14606
+ return this.writeBuffer;
14607
+ }
14608
+ /**
14609
+ * Checks whether the heartbeat timer has expired but the socket has not yet been notified.
14610
+ *
14611
+ * Note: this method is private for now because it does not really fit the WebSocket API, but if we put it in the
14612
+ * `write()` method then the message would not be buffered by the Socket.IO client.
14613
+ *
14614
+ * @return {boolean}
14615
+ * @private
14616
+ */
14617
+ /* private */ _hasPingExpired() {
14618
+ if (!this._pingTimeoutTime)
14619
+ return true;
14620
+ const hasExpired = Date.now() > this._pingTimeoutTime;
14621
+ if (hasExpired) {
14622
+ this._pingTimeoutTime = 0;
14623
+ nextTick(() => {
14624
+ this._onClose("ping timeout");
14625
+ }, this.setTimeoutFn);
14626
+ }
14627
+ return hasExpired;
14628
+ }
14629
+ /**
14630
+ * Sends a message.
14631
+ *
14632
+ * @param {String} msg - message.
14633
+ * @param {Object} options.
14634
+ * @param {Function} fn - callback function.
14635
+ * @return {Socket} for chaining.
14636
+ */
14637
+ write(msg, options, fn) {
14638
+ this._sendPacket("message", msg, options, fn);
14639
+ return this;
14640
+ }
14641
+ /**
14642
+ * Sends a message. Alias of {@link Socket#write}.
14643
+ *
14644
+ * @param {String} msg - message.
14645
+ * @param {Object} options.
14646
+ * @param {Function} fn - callback function.
14647
+ * @return {Socket} for chaining.
14648
+ */
14649
+ send(msg, options, fn) {
14650
+ this._sendPacket("message", msg, options, fn);
14651
+ return this;
14652
+ }
14653
+ /**
14654
+ * Sends a packet.
14655
+ *
14656
+ * @param {String} type: packet type.
14657
+ * @param {String} data.
14658
+ * @param {Object} options.
14659
+ * @param {Function} fn - callback function.
14660
+ * @private
14661
+ */
14662
+ _sendPacket(type, data, options, fn) {
14663
+ if ("function" === typeof data) {
14664
+ fn = data;
14665
+ data = undefined;
14666
+ }
14667
+ if ("function" === typeof options) {
14668
+ fn = options;
14669
+ options = null;
14670
+ }
14671
+ if ("closing" === this.readyState || "closed" === this.readyState) {
14672
+ return;
14673
+ }
14674
+ options = options || {};
14675
+ options.compress = false !== options.compress;
14676
+ const packet = {
14677
+ type: type,
14678
+ data: data,
14679
+ options: options,
14680
+ };
14681
+ this.emitReserved("packetCreate", packet);
14682
+ this.writeBuffer.push(packet);
14683
+ if (fn)
14684
+ this.once("flush", fn);
14685
+ this.flush();
14686
+ }
14687
+ /**
14688
+ * Closes the connection.
14689
+ */
14690
+ close() {
14691
+ const close = () => {
14692
+ this._onClose("forced close");
14693
+ this.transport.close();
14694
+ };
14695
+ const cleanupAndClose = () => {
14696
+ this.off("upgrade", cleanupAndClose);
14697
+ this.off("upgradeError", cleanupAndClose);
14698
+ close();
14699
+ };
14700
+ const waitForUpgrade = () => {
14701
+ // wait for upgrade to finish since we can't send packets while pausing a transport
14702
+ this.once("upgrade", cleanupAndClose);
14703
+ this.once("upgradeError", cleanupAndClose);
14704
+ };
14705
+ if ("opening" === this.readyState || "open" === this.readyState) {
14706
+ this.readyState = "closing";
14707
+ if (this.writeBuffer.length) {
14708
+ this.once("drain", () => {
14709
+ if (this.upgrading) {
14710
+ waitForUpgrade();
14711
+ }
14712
+ else {
14713
+ close();
14714
+ }
14715
+ });
14716
+ }
14717
+ else if (this.upgrading) {
14718
+ waitForUpgrade();
14719
+ }
14720
+ else {
14721
+ close();
14722
+ }
14723
+ }
14724
+ return this;
14725
+ }
14726
+ /**
14727
+ * Called upon transport error
14728
+ *
14729
+ * @private
14730
+ */
14731
+ _onError(err) {
14732
+ SocketWithoutUpgrade.priorWebsocketSuccess = false;
14733
+ if (this.opts.tryAllTransports &&
14734
+ this.transports.length > 1 &&
14735
+ this.readyState === "opening") {
14736
+ this.transports.shift();
14737
+ return this._open();
14738
+ }
14739
+ this.emitReserved("error", err);
14740
+ this._onClose("transport error", err);
14741
+ }
14742
+ /**
14743
+ * Called upon transport close.
14744
+ *
14745
+ * @private
14746
+ */
14747
+ _onClose(reason, description) {
14748
+ if ("opening" === this.readyState ||
14749
+ "open" === this.readyState ||
14750
+ "closing" === this.readyState) {
14751
+ // clear timers
14752
+ this.clearTimeoutFn(this._pingTimeoutTimer);
14753
+ // stop event from firing again for transport
14754
+ this.transport.removeAllListeners("close");
14755
+ // ensure transport won't stay open
14756
+ this.transport.close();
14757
+ // ignore further transport communication
14758
+ this.transport.removeAllListeners();
14759
+ if (withEventListeners) {
14760
+ if (this._beforeunloadEventListener) {
14761
+ removeEventListener("beforeunload", this._beforeunloadEventListener, false);
14762
+ }
14763
+ if (this._offlineEventListener) {
14764
+ const i = OFFLINE_EVENT_LISTENERS.indexOf(this._offlineEventListener);
14765
+ if (i !== -1) {
14766
+ OFFLINE_EVENT_LISTENERS.splice(i, 1);
14767
+ }
14768
+ }
14769
+ }
14770
+ // set ready state
14771
+ this.readyState = "closed";
14772
+ // clear session id
14773
+ this.id = null;
14774
+ // emit close event
14775
+ this.emitReserved("close", reason, description);
14776
+ // clean buffers after, so users can still
14777
+ // grab the buffers on `close` event
14778
+ this.writeBuffer = [];
14779
+ this._prevBufferLen = 0;
14780
+ }
14781
+ }
14782
+ }
14783
+ SocketWithoutUpgrade.protocol = protocol$1;
14784
+ /**
14785
+ * This class provides a WebSocket-like interface to connect to an Engine.IO server. The connection will be established
14786
+ * with one of the available low-level transports, like HTTP long-polling, WebSocket or WebTransport.
14787
+ *
14788
+ * This class comes with an upgrade mechanism, which means that once the connection is established with the first
14789
+ * low-level transport, it will try to upgrade to a better transport.
14790
+ *
14791
+ * In order to allow tree-shaking, there are no transports included, that's why the `transports` option is mandatory.
14792
+ *
14793
+ * @example
14794
+ * import { SocketWithUpgrade, WebSocket } from "engine.io-client";
14795
+ *
14796
+ * const socket = new SocketWithUpgrade({
14797
+ * transports: [WebSocket]
14798
+ * });
14799
+ *
14800
+ * socket.on("open", () => {
14801
+ * socket.send("hello");
14802
+ * });
14803
+ *
14804
+ * @see SocketWithoutUpgrade
14805
+ * @see Socket
14806
+ */
14807
+ class SocketWithUpgrade extends SocketWithoutUpgrade {
14808
+ constructor() {
14809
+ super(...arguments);
14810
+ this._upgrades = [];
14811
+ }
14812
+ onOpen() {
14813
+ super.onOpen();
14814
+ if ("open" === this.readyState && this.opts.upgrade) {
14815
+ for (let i = 0; i < this._upgrades.length; i++) {
14816
+ this._probe(this._upgrades[i]);
14817
+ }
14818
+ }
14819
+ }
14820
+ /**
14821
+ * Probes a transport.
14822
+ *
14823
+ * @param {String} name - transport name
14824
+ * @private
14825
+ */
14826
+ _probe(name) {
14827
+ let transport = this.createTransport(name);
14828
+ let failed = false;
14829
+ SocketWithoutUpgrade.priorWebsocketSuccess = false;
14830
+ const onTransportOpen = () => {
14831
+ if (failed)
14832
+ return;
14833
+ transport.send([{ type: "ping", data: "probe" }]);
14834
+ transport.once("packet", (msg) => {
14835
+ if (failed)
14836
+ return;
14837
+ if ("pong" === msg.type && "probe" === msg.data) {
14838
+ this.upgrading = true;
14839
+ this.emitReserved("upgrading", transport);
14840
+ if (!transport)
14841
+ return;
14842
+ SocketWithoutUpgrade.priorWebsocketSuccess =
14843
+ "websocket" === transport.name;
14844
+ this.transport.pause(() => {
14845
+ if (failed)
14846
+ return;
14847
+ if ("closed" === this.readyState)
14848
+ return;
14849
+ cleanup();
14850
+ this.setTransport(transport);
14851
+ transport.send([{ type: "upgrade" }]);
14852
+ this.emitReserved("upgrade", transport);
14853
+ transport = null;
14854
+ this.upgrading = false;
14855
+ this.flush();
14856
+ });
14857
+ }
14858
+ else {
14859
+ const err = new Error("probe error");
14860
+ // @ts-ignore
14861
+ err.transport = transport.name;
14862
+ this.emitReserved("upgradeError", err);
14863
+ }
14864
+ });
14865
+ };
14866
+ function freezeTransport() {
14867
+ if (failed)
14868
+ return;
14869
+ // Any callback called by transport should be ignored since now
14870
+ failed = true;
14871
+ cleanup();
14872
+ transport.close();
14873
+ transport = null;
14874
+ }
14875
+ // Handle any error that happens while probing
14876
+ const onerror = (err) => {
14877
+ const error = new Error("probe error: " + err);
14878
+ // @ts-ignore
14879
+ error.transport = transport.name;
14880
+ freezeTransport();
14881
+ this.emitReserved("upgradeError", error);
14882
+ };
14883
+ function onTransportClose() {
14884
+ onerror("transport closed");
14885
+ }
14886
+ // When the socket is closed while we're probing
14887
+ function onclose() {
14888
+ onerror("socket closed");
14889
+ }
14890
+ // When the socket is upgraded while we're probing
14891
+ function onupgrade(to) {
14892
+ if (transport && to.name !== transport.name) {
14893
+ freezeTransport();
14894
+ }
14895
+ }
14896
+ // Remove all listeners on the transport and on self
14897
+ const cleanup = () => {
14898
+ transport.removeListener("open", onTransportOpen);
14899
+ transport.removeListener("error", onerror);
14900
+ transport.removeListener("close", onTransportClose);
14901
+ this.off("close", onclose);
14902
+ this.off("upgrading", onupgrade);
14903
+ };
14904
+ transport.once("open", onTransportOpen);
14905
+ transport.once("error", onerror);
14906
+ transport.once("close", onTransportClose);
14907
+ this.once("close", onclose);
14908
+ this.once("upgrading", onupgrade);
14909
+ if (this._upgrades.indexOf("webtransport") !== -1 &&
14910
+ name !== "webtransport") {
14911
+ // favor WebTransport
14912
+ this.setTimeoutFn(() => {
14913
+ if (!failed) {
14914
+ transport.open();
14915
+ }
14916
+ }, 200);
14917
+ }
14918
+ else {
14919
+ transport.open();
14920
+ }
14921
+ }
14922
+ onHandshake(data) {
14923
+ this._upgrades = this._filterUpgrades(data.upgrades);
14924
+ super.onHandshake(data);
14925
+ }
14926
+ /**
14927
+ * Filters upgrades, returning only those matching client transports.
14928
+ *
14929
+ * @param {Array} upgrades - server upgrades
14930
+ * @private
14931
+ */
14932
+ _filterUpgrades(upgrades) {
14933
+ const filteredUpgrades = [];
14934
+ for (let i = 0; i < upgrades.length; i++) {
14935
+ if (~this.transports.indexOf(upgrades[i]))
14936
+ filteredUpgrades.push(upgrades[i]);
14937
+ }
14938
+ return filteredUpgrades;
14939
+ }
14940
+ }
14941
+ /**
14942
+ * This class provides a WebSocket-like interface to connect to an Engine.IO server. The connection will be established
14943
+ * with one of the available low-level transports, like HTTP long-polling, WebSocket or WebTransport.
14944
+ *
14945
+ * This class comes with an upgrade mechanism, which means that once the connection is established with the first
14946
+ * low-level transport, it will try to upgrade to a better transport.
14947
+ *
14948
+ * @example
14949
+ * import { Socket } from "engine.io-client";
14950
+ *
14951
+ * const socket = new Socket();
14952
+ *
14953
+ * socket.on("open", () => {
14954
+ * socket.send("hello");
14955
+ * });
14956
+ *
14957
+ * @see SocketWithoutUpgrade
14958
+ * @see SocketWithUpgrade
14959
+ */
14960
+ let Socket$1 = class Socket extends SocketWithUpgrade {
14961
+ constructor(uri, opts = {}) {
14962
+ const o = typeof uri === "object" ? uri : opts;
14963
+ if (!o.transports ||
14964
+ (o.transports && typeof o.transports[0] === "string")) {
14965
+ o.transports = (o.transports || ["polling", "websocket", "webtransport"])
14966
+ .map((transportName) => transports[transportName])
14967
+ .filter((t) => !!t);
14968
+ }
14969
+ super(uri, o);
14970
+ }
14971
+ };
14972
+
14973
+ /**
14974
+ * URL parser.
14975
+ *
14976
+ * @param uri - url
14977
+ * @param path - the request path of the connection
14978
+ * @param loc - An object meant to mimic window.location.
14979
+ * Defaults to window.location.
14980
+ * @public
14981
+ */
14982
+ function url(uri, path = "", loc) {
14983
+ let obj = uri;
14984
+ // default to window.location
14985
+ loc = loc || (typeof location !== "undefined" && location);
14986
+ if (null == uri)
14987
+ uri = loc.protocol + "//" + loc.host;
14988
+ // relative path support
14989
+ if (typeof uri === "string") {
14990
+ if ("/" === uri.charAt(0)) {
14991
+ if ("/" === uri.charAt(1)) {
14992
+ uri = loc.protocol + uri;
14993
+ }
14994
+ else {
14995
+ uri = loc.host + uri;
14996
+ }
14997
+ }
14998
+ if (!/^(https?|wss?):\/\//.test(uri)) {
14999
+ if ("undefined" !== typeof loc) {
15000
+ uri = loc.protocol + "//" + uri;
15001
+ }
15002
+ else {
15003
+ uri = "https://" + uri;
15004
+ }
15005
+ }
15006
+ // parse
15007
+ obj = parse(uri);
15008
+ }
15009
+ // make sure we treat `localhost:80` and `localhost` equally
15010
+ if (!obj.port) {
15011
+ if (/^(http|ws)$/.test(obj.protocol)) {
15012
+ obj.port = "80";
15013
+ }
15014
+ else if (/^(http|ws)s$/.test(obj.protocol)) {
15015
+ obj.port = "443";
15016
+ }
15017
+ }
15018
+ obj.path = obj.path || "/";
15019
+ const ipv6 = obj.host.indexOf(":") !== -1;
15020
+ const host = ipv6 ? "[" + obj.host + "]" : obj.host;
15021
+ // define unique id
15022
+ obj.id = obj.protocol + "://" + host + ":" + obj.port + path;
15023
+ // define href
15024
+ obj.href =
15025
+ obj.protocol +
15026
+ "://" +
15027
+ host +
15028
+ (loc && loc.port === obj.port ? "" : ":" + obj.port);
15029
+ return obj;
15030
+ }
15031
+
15032
+ const withNativeArrayBuffer = typeof ArrayBuffer === "function";
15033
+ const isView = (obj) => {
15034
+ return typeof ArrayBuffer.isView === "function"
15035
+ ? ArrayBuffer.isView(obj)
15036
+ : obj.buffer instanceof ArrayBuffer;
15037
+ };
15038
+ const toString = Object.prototype.toString;
15039
+ const withNativeBlob = typeof Blob === "function" ||
15040
+ (typeof Blob !== "undefined" &&
15041
+ toString.call(Blob) === "[object BlobConstructor]");
15042
+ const withNativeFile = typeof File === "function" ||
15043
+ (typeof File !== "undefined" &&
15044
+ toString.call(File) === "[object FileConstructor]");
15045
+ /**
15046
+ * Returns true if obj is a Buffer, an ArrayBuffer, a Blob or a File.
15047
+ *
15048
+ * @private
15049
+ */
15050
+ function isBinary(obj) {
15051
+ return ((withNativeArrayBuffer && (obj instanceof ArrayBuffer || isView(obj))) ||
15052
+ (withNativeBlob && obj instanceof Blob) ||
15053
+ (withNativeFile && obj instanceof File));
15054
+ }
15055
+ function hasBinary(obj, toJSON) {
15056
+ if (!obj || typeof obj !== "object") {
15057
+ return false;
15058
+ }
15059
+ if (Array.isArray(obj)) {
15060
+ for (let i = 0, l = obj.length; i < l; i++) {
15061
+ if (hasBinary(obj[i])) {
15062
+ return true;
15063
+ }
15064
+ }
15065
+ return false;
15066
+ }
15067
+ if (isBinary(obj)) {
15068
+ return true;
15069
+ }
15070
+ if (obj.toJSON &&
15071
+ typeof obj.toJSON === "function" &&
15072
+ arguments.length === 1) {
15073
+ return hasBinary(obj.toJSON(), true);
15074
+ }
15075
+ for (const key in obj) {
15076
+ if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
15077
+ return true;
15078
+ }
15079
+ }
15080
+ return false;
15081
+ }
15082
+
15083
+ /**
15084
+ * Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
15085
+ *
15086
+ * @param {Object} packet - socket.io event packet
15087
+ * @return {Object} with deconstructed packet and list of buffers
15088
+ * @public
15089
+ */
15090
+ function deconstructPacket(packet) {
15091
+ const buffers = [];
15092
+ const packetData = packet.data;
15093
+ const pack = packet;
15094
+ pack.data = _deconstructPacket(packetData, buffers);
15095
+ pack.attachments = buffers.length; // number of binary 'attachments'
15096
+ return { packet: pack, buffers: buffers };
15097
+ }
15098
+ function _deconstructPacket(data, buffers) {
15099
+ if (!data)
15100
+ return data;
15101
+ if (isBinary(data)) {
15102
+ const placeholder = { _placeholder: true, num: buffers.length };
15103
+ buffers.push(data);
15104
+ return placeholder;
15105
+ }
15106
+ else if (Array.isArray(data)) {
15107
+ const newData = new Array(data.length);
15108
+ for (let i = 0; i < data.length; i++) {
15109
+ newData[i] = _deconstructPacket(data[i], buffers);
15110
+ }
15111
+ return newData;
15112
+ }
15113
+ else if (typeof data === "object" && !(data instanceof Date)) {
15114
+ const newData = {};
15115
+ for (const key in data) {
15116
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
15117
+ newData[key] = _deconstructPacket(data[key], buffers);
15118
+ }
15119
+ }
15120
+ return newData;
15121
+ }
15122
+ return data;
15123
+ }
15124
+ /**
15125
+ * Reconstructs a binary packet from its placeholder packet and buffers
15126
+ *
15127
+ * @param {Object} packet - event packet with placeholders
15128
+ * @param {Array} buffers - binary buffers to put in placeholder positions
15129
+ * @return {Object} reconstructed packet
15130
+ * @public
15131
+ */
15132
+ function reconstructPacket(packet, buffers) {
15133
+ packet.data = _reconstructPacket(packet.data, buffers);
15134
+ delete packet.attachments; // no longer useful
15135
+ return packet;
15136
+ }
15137
+ function _reconstructPacket(data, buffers) {
15138
+ if (!data)
15139
+ return data;
15140
+ if (data && data._placeholder === true) {
15141
+ const isIndexValid = typeof data.num === "number" &&
15142
+ data.num >= 0 &&
15143
+ data.num < buffers.length;
15144
+ if (isIndexValid) {
15145
+ return buffers[data.num]; // appropriate buffer (should be natural order anyway)
15146
+ }
15147
+ else {
15148
+ throw new Error("illegal attachments");
15149
+ }
15150
+ }
15151
+ else if (Array.isArray(data)) {
15152
+ for (let i = 0; i < data.length; i++) {
15153
+ data[i] = _reconstructPacket(data[i], buffers);
15154
+ }
15155
+ }
15156
+ else if (typeof data === "object") {
15157
+ for (const key in data) {
15158
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
15159
+ data[key] = _reconstructPacket(data[key], buffers);
15160
+ }
15161
+ }
15162
+ }
15163
+ return data;
15164
+ }
15165
+
15166
+ /**
15167
+ * These strings must not be used as event names, as they have a special meaning.
15168
+ */
15169
+ const RESERVED_EVENTS$1 = [
15170
+ "connect",
15171
+ "connect_error",
15172
+ "disconnect",
15173
+ "disconnecting",
15174
+ "newListener",
15175
+ "removeListener", // used by the Node.js EventEmitter
15176
+ ];
15177
+ /**
15178
+ * Protocol version.
15179
+ *
15180
+ * @public
15181
+ */
15182
+ const protocol = 5;
15183
+ var PacketType;
15184
+ (function (PacketType) {
15185
+ PacketType[PacketType["CONNECT"] = 0] = "CONNECT";
15186
+ PacketType[PacketType["DISCONNECT"] = 1] = "DISCONNECT";
15187
+ PacketType[PacketType["EVENT"] = 2] = "EVENT";
15188
+ PacketType[PacketType["ACK"] = 3] = "ACK";
15189
+ PacketType[PacketType["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
15190
+ PacketType[PacketType["BINARY_EVENT"] = 5] = "BINARY_EVENT";
15191
+ PacketType[PacketType["BINARY_ACK"] = 6] = "BINARY_ACK";
15192
+ })(PacketType || (PacketType = {}));
15193
+ /**
15194
+ * A socket.io Encoder instance
15195
+ */
15196
+ class Encoder {
15197
+ /**
15198
+ * Encoder constructor
15199
+ *
15200
+ * @param {function} replacer - custom replacer to pass down to JSON.parse
15201
+ */
15202
+ constructor(replacer) {
15203
+ this.replacer = replacer;
15204
+ }
15205
+ /**
15206
+ * Encode a packet as a single string if non-binary, or as a
15207
+ * buffer sequence, depending on packet type.
15208
+ *
15209
+ * @param {Object} obj - packet object
15210
+ */
15211
+ encode(obj) {
15212
+ if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) {
15213
+ if (hasBinary(obj)) {
15214
+ return this.encodeAsBinary({
15215
+ type: obj.type === PacketType.EVENT
15216
+ ? PacketType.BINARY_EVENT
15217
+ : PacketType.BINARY_ACK,
15218
+ nsp: obj.nsp,
15219
+ data: obj.data,
15220
+ id: obj.id,
15221
+ });
15222
+ }
15223
+ }
15224
+ return [this.encodeAsString(obj)];
15225
+ }
15226
+ /**
15227
+ * Encode packet as string.
15228
+ */
15229
+ encodeAsString(obj) {
15230
+ // first is type
15231
+ let str = "" + obj.type;
15232
+ // attachments if we have them
15233
+ if (obj.type === PacketType.BINARY_EVENT ||
15234
+ obj.type === PacketType.BINARY_ACK) {
15235
+ str += obj.attachments + "-";
15236
+ }
15237
+ // if we have a namespace other than `/`
15238
+ // we append it followed by a comma `,`
15239
+ if (obj.nsp && "/" !== obj.nsp) {
15240
+ str += obj.nsp + ",";
15241
+ }
15242
+ // immediately followed by the id
15243
+ if (null != obj.id) {
15244
+ str += obj.id;
15245
+ }
15246
+ // json data
15247
+ if (null != obj.data) {
15248
+ str += JSON.stringify(obj.data, this.replacer);
15249
+ }
15250
+ return str;
15251
+ }
15252
+ /**
15253
+ * Encode packet as 'buffer sequence' by removing blobs, and
15254
+ * deconstructing packet into object with placeholders and
15255
+ * a list of buffers.
15256
+ */
15257
+ encodeAsBinary(obj) {
15258
+ const deconstruction = deconstructPacket(obj);
15259
+ const pack = this.encodeAsString(deconstruction.packet);
15260
+ const buffers = deconstruction.buffers;
15261
+ buffers.unshift(pack); // add packet info to beginning of data list
15262
+ return buffers; // write all the buffers
15263
+ }
15264
+ }
15265
+ // see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
15266
+ function isObject(value) {
15267
+ return Object.prototype.toString.call(value) === "[object Object]";
15268
+ }
15269
+ /**
15270
+ * A socket.io Decoder instance
15271
+ *
15272
+ * @return {Object} decoder
15273
+ */
15274
+ class Decoder extends Emitter {
15275
+ /**
15276
+ * Decoder constructor
15277
+ *
15278
+ * @param {function} reviver - custom reviver to pass down to JSON.stringify
15279
+ */
15280
+ constructor(reviver) {
15281
+ super();
15282
+ this.reviver = reviver;
15283
+ }
15284
+ /**
15285
+ * Decodes an encoded packet string into packet JSON.
15286
+ *
15287
+ * @param {String} obj - encoded packet
15288
+ */
15289
+ add(obj) {
15290
+ let packet;
15291
+ if (typeof obj === "string") {
15292
+ if (this.reconstructor) {
15293
+ throw new Error("got plaintext data when reconstructing a packet");
15294
+ }
15295
+ packet = this.decodeString(obj);
15296
+ const isBinaryEvent = packet.type === PacketType.BINARY_EVENT;
15297
+ if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) {
15298
+ packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK;
15299
+ // binary packet's json
15300
+ this.reconstructor = new BinaryReconstructor(packet);
15301
+ // no attachments, labeled binary but no binary data to follow
15302
+ if (packet.attachments === 0) {
15303
+ super.emitReserved("decoded", packet);
15304
+ }
15305
+ }
15306
+ else {
15307
+ // non-binary full packet
15308
+ super.emitReserved("decoded", packet);
15309
+ }
15310
+ }
15311
+ else if (isBinary(obj) || obj.base64) {
15312
+ // raw binary data
15313
+ if (!this.reconstructor) {
15314
+ throw new Error("got binary data when not reconstructing a packet");
15315
+ }
15316
+ else {
15317
+ packet = this.reconstructor.takeBinaryData(obj);
15318
+ if (packet) {
15319
+ // received final buffer
15320
+ this.reconstructor = null;
15321
+ super.emitReserved("decoded", packet);
15322
+ }
15323
+ }
15324
+ }
15325
+ else {
15326
+ throw new Error("Unknown type: " + obj);
15327
+ }
15328
+ }
15329
+ /**
15330
+ * Decode a packet String (JSON data)
15331
+ *
15332
+ * @param {String} str
15333
+ * @return {Object} packet
15334
+ */
15335
+ decodeString(str) {
15336
+ let i = 0;
15337
+ // look up type
15338
+ const p = {
15339
+ type: Number(str.charAt(0)),
15340
+ };
15341
+ if (PacketType[p.type] === undefined) {
15342
+ throw new Error("unknown packet type " + p.type);
15343
+ }
15344
+ // look up attachments if type binary
15345
+ if (p.type === PacketType.BINARY_EVENT ||
15346
+ p.type === PacketType.BINARY_ACK) {
15347
+ const start = i + 1;
15348
+ while (str.charAt(++i) !== "-" && i != str.length) { }
15349
+ const buf = str.substring(start, i);
15350
+ if (buf != Number(buf) || str.charAt(i) !== "-") {
15351
+ throw new Error("Illegal attachments");
15352
+ }
15353
+ p.attachments = Number(buf);
15354
+ }
15355
+ // look up namespace (if any)
15356
+ if ("/" === str.charAt(i + 1)) {
15357
+ const start = i + 1;
15358
+ while (++i) {
15359
+ const c = str.charAt(i);
15360
+ if ("," === c)
15361
+ break;
15362
+ if (i === str.length)
15363
+ break;
15364
+ }
15365
+ p.nsp = str.substring(start, i);
15366
+ }
15367
+ else {
15368
+ p.nsp = "/";
15369
+ }
15370
+ // look up id
15371
+ const next = str.charAt(i + 1);
15372
+ if ("" !== next && Number(next) == next) {
15373
+ const start = i + 1;
15374
+ while (++i) {
15375
+ const c = str.charAt(i);
15376
+ if (null == c || Number(c) != c) {
15377
+ --i;
15378
+ break;
15379
+ }
15380
+ if (i === str.length)
15381
+ break;
15382
+ }
15383
+ p.id = Number(str.substring(start, i + 1));
15384
+ }
15385
+ // look up json data
15386
+ if (str.charAt(++i)) {
15387
+ const payload = this.tryParse(str.substr(i));
15388
+ if (Decoder.isPayloadValid(p.type, payload)) {
15389
+ p.data = payload;
15390
+ }
15391
+ else {
15392
+ throw new Error("invalid payload");
15393
+ }
15394
+ }
15395
+ return p;
15396
+ }
15397
+ tryParse(str) {
15398
+ try {
15399
+ return JSON.parse(str, this.reviver);
15400
+ }
15401
+ catch (e) {
15402
+ return false;
15403
+ }
15404
+ }
15405
+ static isPayloadValid(type, payload) {
15406
+ switch (type) {
15407
+ case PacketType.CONNECT:
15408
+ return isObject(payload);
15409
+ case PacketType.DISCONNECT:
15410
+ return payload === undefined;
15411
+ case PacketType.CONNECT_ERROR:
15412
+ return typeof payload === "string" || isObject(payload);
15413
+ case PacketType.EVENT:
15414
+ case PacketType.BINARY_EVENT:
15415
+ return (Array.isArray(payload) &&
15416
+ (typeof payload[0] === "number" ||
15417
+ (typeof payload[0] === "string" &&
15418
+ RESERVED_EVENTS$1.indexOf(payload[0]) === -1)));
15419
+ case PacketType.ACK:
15420
+ case PacketType.BINARY_ACK:
15421
+ return Array.isArray(payload);
15422
+ }
15423
+ }
15424
+ /**
15425
+ * Deallocates a parser's resources
15426
+ */
15427
+ destroy() {
15428
+ if (this.reconstructor) {
15429
+ this.reconstructor.finishedReconstruction();
15430
+ this.reconstructor = null;
15431
+ }
15432
+ }
15433
+ }
15434
+ /**
15435
+ * A manager of a binary event's 'buffer sequence'. Should
15436
+ * be constructed whenever a packet of type BINARY_EVENT is
15437
+ * decoded.
15438
+ *
15439
+ * @param {Object} packet
15440
+ * @return {BinaryReconstructor} initialized reconstructor
15441
+ */
15442
+ class BinaryReconstructor {
15443
+ constructor(packet) {
15444
+ this.packet = packet;
15445
+ this.buffers = [];
15446
+ this.reconPack = packet;
15447
+ }
15448
+ /**
15449
+ * Method to be called when binary data received from connection
15450
+ * after a BINARY_EVENT packet.
15451
+ *
15452
+ * @param {Buffer | ArrayBuffer} binData - the raw binary data received
15453
+ * @return {null | Object} returns null if more binary data is expected or
15454
+ * a reconstructed packet object if all buffers have been received.
15455
+ */
15456
+ takeBinaryData(binData) {
15457
+ this.buffers.push(binData);
15458
+ if (this.buffers.length === this.reconPack.attachments) {
15459
+ // done with buffer list
15460
+ const packet = reconstructPacket(this.reconPack, this.buffers);
15461
+ this.finishedReconstruction();
15462
+ return packet;
15463
+ }
15464
+ return null;
15465
+ }
15466
+ /**
15467
+ * Cleans up binary packet reconstruction variables.
15468
+ */
15469
+ finishedReconstruction() {
15470
+ this.reconPack = null;
15471
+ this.buffers = [];
15472
+ }
15473
+ }
15474
+
15475
+ var parser = /*#__PURE__*/Object.freeze({
15476
+ __proto__: null,
15477
+ Decoder: Decoder,
15478
+ Encoder: Encoder,
15479
+ get PacketType () { return PacketType; },
15480
+ protocol: protocol
15481
+ });
15482
+
15483
+ function on(obj, ev, fn) {
15484
+ obj.on(ev, fn);
15485
+ return function subDestroy() {
15486
+ obj.off(ev, fn);
15487
+ };
15488
+ }
15489
+
15490
+ /**
15491
+ * Internal events.
15492
+ * These events can't be emitted by the user.
15493
+ */
15494
+ const RESERVED_EVENTS = Object.freeze({
15495
+ connect: 1,
15496
+ connect_error: 1,
15497
+ disconnect: 1,
15498
+ disconnecting: 1,
15499
+ // EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
15500
+ newListener: 1,
15501
+ removeListener: 1,
15502
+ });
15503
+ /**
15504
+ * A Socket is the fundamental class for interacting with the server.
15505
+ *
15506
+ * A Socket belongs to a certain Namespace (by default /) and uses an underlying {@link Manager} to communicate.
15507
+ *
15508
+ * @example
15509
+ * const socket = io();
15510
+ *
15511
+ * socket.on("connect", () => {
15512
+ * console.log("connected");
15513
+ * });
15514
+ *
15515
+ * // send an event to the server
15516
+ * socket.emit("foo", "bar");
15517
+ *
15518
+ * socket.on("foobar", () => {
15519
+ * // an event was received from the server
15520
+ * });
15521
+ *
15522
+ * // upon disconnection
15523
+ * socket.on("disconnect", (reason) => {
15524
+ * console.log(`disconnected due to ${reason}`);
15525
+ * });
15526
+ */
15527
+ class Socket extends Emitter {
15528
+ /**
15529
+ * `Socket` constructor.
15530
+ */
15531
+ constructor(io, nsp, opts) {
15532
+ super();
15533
+ /**
15534
+ * Whether the socket is currently connected to the server.
15535
+ *
15536
+ * @example
15537
+ * const socket = io();
15538
+ *
15539
+ * socket.on("connect", () => {
15540
+ * console.log(socket.connected); // true
15541
+ * });
15542
+ *
15543
+ * socket.on("disconnect", () => {
15544
+ * console.log(socket.connected); // false
15545
+ * });
15546
+ */
15547
+ this.connected = false;
15548
+ /**
15549
+ * Whether the connection state was recovered after a temporary disconnection. In that case, any missed packets will
15550
+ * be transmitted by the server.
15551
+ */
15552
+ this.recovered = false;
15553
+ /**
15554
+ * Buffer for packets received before the CONNECT packet
15555
+ */
15556
+ this.receiveBuffer = [];
15557
+ /**
15558
+ * Buffer for packets that will be sent once the socket is connected
15559
+ */
15560
+ this.sendBuffer = [];
15561
+ /**
15562
+ * The queue of packets to be sent with retry in case of failure.
15563
+ *
15564
+ * Packets are sent one by one, each waiting for the server acknowledgement, in order to guarantee the delivery order.
15565
+ * @private
15566
+ */
15567
+ this._queue = [];
15568
+ /**
15569
+ * A sequence to generate the ID of the {@link QueuedPacket}.
15570
+ * @private
15571
+ */
15572
+ this._queueSeq = 0;
15573
+ this.ids = 0;
15574
+ /**
15575
+ * A map containing acknowledgement handlers.
15576
+ *
15577
+ * The `withError` attribute is used to differentiate handlers that accept an error as first argument:
15578
+ *
15579
+ * - `socket.emit("test", (err, value) => { ... })` with `ackTimeout` option
15580
+ * - `socket.timeout(5000).emit("test", (err, value) => { ... })`
15581
+ * - `const value = await socket.emitWithAck("test")`
15582
+ *
15583
+ * From those that don't:
15584
+ *
15585
+ * - `socket.emit("test", (value) => { ... });`
15586
+ *
15587
+ * In the first case, the handlers will be called with an error when:
15588
+ *
15589
+ * - the timeout is reached
15590
+ * - the socket gets disconnected
15591
+ *
15592
+ * In the second case, the handlers will be simply discarded upon disconnection, since the client will never receive
15593
+ * an acknowledgement from the server.
15594
+ *
15595
+ * @private
15596
+ */
15597
+ this.acks = {};
15598
+ this.flags = {};
15599
+ this.io = io;
15600
+ this.nsp = nsp;
15601
+ if (opts && opts.auth) {
15602
+ this.auth = opts.auth;
15603
+ }
15604
+ this._opts = Object.assign({}, opts);
15605
+ if (this.io._autoConnect)
15606
+ this.open();
15607
+ }
15608
+ /**
15609
+ * Whether the socket is currently disconnected
15610
+ *
15611
+ * @example
15612
+ * const socket = io();
15613
+ *
15614
+ * socket.on("connect", () => {
15615
+ * console.log(socket.disconnected); // false
15616
+ * });
15617
+ *
15618
+ * socket.on("disconnect", () => {
15619
+ * console.log(socket.disconnected); // true
15620
+ * });
15621
+ */
15622
+ get disconnected() {
15623
+ return !this.connected;
15624
+ }
15625
+ /**
15626
+ * Subscribe to open, close and packet events
15627
+ *
15628
+ * @private
15629
+ */
15630
+ subEvents() {
15631
+ if (this.subs)
15632
+ return;
15633
+ const io = this.io;
15634
+ this.subs = [
15635
+ on(io, "open", this.onopen.bind(this)),
15636
+ on(io, "packet", this.onpacket.bind(this)),
15637
+ on(io, "error", this.onerror.bind(this)),
15638
+ on(io, "close", this.onclose.bind(this)),
15639
+ ];
15640
+ }
15641
+ /**
15642
+ * Whether the Socket will try to reconnect when its Manager connects or reconnects.
15643
+ *
15644
+ * @example
15645
+ * const socket = io();
15646
+ *
15647
+ * console.log(socket.active); // true
15648
+ *
15649
+ * socket.on("disconnect", (reason) => {
15650
+ * if (reason === "io server disconnect") {
15651
+ * // the disconnection was initiated by the server, you need to manually reconnect
15652
+ * console.log(socket.active); // false
15653
+ * }
15654
+ * // else the socket will automatically try to reconnect
15655
+ * console.log(socket.active); // true
15656
+ * });
15657
+ */
15658
+ get active() {
15659
+ return !!this.subs;
15660
+ }
15661
+ /**
15662
+ * "Opens" the socket.
15663
+ *
15664
+ * @example
15665
+ * const socket = io({
15666
+ * autoConnect: false
15667
+ * });
15668
+ *
15669
+ * socket.connect();
15670
+ */
15671
+ connect() {
15672
+ if (this.connected)
15673
+ return this;
15674
+ this.subEvents();
15675
+ if (!this.io["_reconnecting"])
15676
+ this.io.open(); // ensure open
15677
+ if ("open" === this.io._readyState)
15678
+ this.onopen();
15679
+ return this;
15680
+ }
15681
+ /**
15682
+ * Alias for {@link connect()}.
15683
+ */
15684
+ open() {
15685
+ return this.connect();
15686
+ }
15687
+ /**
15688
+ * Sends a `message` event.
15689
+ *
15690
+ * This method mimics the WebSocket.send() method.
15691
+ *
15692
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
15693
+ *
15694
+ * @example
15695
+ * socket.send("hello");
15696
+ *
15697
+ * // this is equivalent to
15698
+ * socket.emit("message", "hello");
15699
+ *
15700
+ * @return self
15701
+ */
15702
+ send(...args) {
15703
+ args.unshift("message");
15704
+ this.emit.apply(this, args);
15705
+ return this;
15706
+ }
15707
+ /**
15708
+ * Override `emit`.
15709
+ * If the event is in `events`, it's emitted normally.
15710
+ *
15711
+ * @example
15712
+ * socket.emit("hello", "world");
15713
+ *
15714
+ * // all serializable datastructures are supported (no need to call JSON.stringify)
15715
+ * socket.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
15716
+ *
15717
+ * // with an acknowledgement from the server
15718
+ * socket.emit("hello", "world", (val) => {
15719
+ * // ...
15720
+ * });
15721
+ *
15722
+ * @return self
15723
+ */
15724
+ emit(ev, ...args) {
15725
+ var _a, _b, _c;
15726
+ if (RESERVED_EVENTS.hasOwnProperty(ev)) {
15727
+ throw new Error('"' + ev.toString() + '" is a reserved event name');
15728
+ }
15729
+ args.unshift(ev);
15730
+ if (this._opts.retries && !this.flags.fromQueue && !this.flags.volatile) {
15731
+ this._addToQueue(args);
15732
+ return this;
15733
+ }
15734
+ const packet = {
15735
+ type: PacketType.EVENT,
15736
+ data: args,
15737
+ };
15738
+ packet.options = {};
15739
+ packet.options.compress = this.flags.compress !== false;
15740
+ // event ack callback
15741
+ if ("function" === typeof args[args.length - 1]) {
15742
+ const id = this.ids++;
15743
+ const ack = args.pop();
15744
+ this._registerAckCallback(id, ack);
15745
+ packet.id = id;
15746
+ }
15747
+ const isTransportWritable = (_b = (_a = this.io.engine) === null || _a === void 0 ? void 0 : _a.transport) === null || _b === void 0 ? void 0 : _b.writable;
15748
+ const isConnected = this.connected && !((_c = this.io.engine) === null || _c === void 0 ? void 0 : _c._hasPingExpired());
15749
+ const discardPacket = this.flags.volatile && !isTransportWritable;
15750
+ if (discardPacket) ;
15751
+ else if (isConnected) {
15752
+ this.notifyOutgoingListeners(packet);
15753
+ this.packet(packet);
15754
+ }
15755
+ else {
15756
+ this.sendBuffer.push(packet);
15757
+ }
15758
+ this.flags = {};
15759
+ return this;
15760
+ }
15761
+ /**
15762
+ * @private
15763
+ */
15764
+ _registerAckCallback(id, ack) {
15765
+ var _a;
15766
+ const timeout = (_a = this.flags.timeout) !== null && _a !== void 0 ? _a : this._opts.ackTimeout;
15767
+ if (timeout === undefined) {
15768
+ this.acks[id] = ack;
15769
+ return;
15770
+ }
15771
+ // @ts-ignore
15772
+ const timer = this.io.setTimeoutFn(() => {
15773
+ delete this.acks[id];
15774
+ for (let i = 0; i < this.sendBuffer.length; i++) {
15775
+ if (this.sendBuffer[i].id === id) {
15776
+ this.sendBuffer.splice(i, 1);
15777
+ }
15778
+ }
15779
+ ack.call(this, new Error("operation has timed out"));
15780
+ }, timeout);
15781
+ const fn = (...args) => {
15782
+ // @ts-ignore
15783
+ this.io.clearTimeoutFn(timer);
15784
+ ack.apply(this, args);
15785
+ };
15786
+ fn.withError = true;
15787
+ this.acks[id] = fn;
15788
+ }
15789
+ /**
15790
+ * Emits an event and waits for an acknowledgement
15791
+ *
15792
+ * @example
15793
+ * // without timeout
15794
+ * const response = await socket.emitWithAck("hello", "world");
15795
+ *
15796
+ * // with a specific timeout
15797
+ * try {
15798
+ * const response = await socket.timeout(1000).emitWithAck("hello", "world");
15799
+ * } catch (err) {
15800
+ * // the server did not acknowledge the event in the given delay
15801
+ * }
15802
+ *
15803
+ * @return a Promise that will be fulfilled when the server acknowledges the event
15804
+ */
15805
+ emitWithAck(ev, ...args) {
15806
+ return new Promise((resolve, reject) => {
15807
+ const fn = (arg1, arg2) => {
15808
+ return arg1 ? reject(arg1) : resolve(arg2);
15809
+ };
15810
+ fn.withError = true;
15811
+ args.push(fn);
15812
+ this.emit(ev, ...args);
15813
+ });
15814
+ }
15815
+ /**
15816
+ * Add the packet to the queue.
15817
+ * @param args
15818
+ * @private
15819
+ */
15820
+ _addToQueue(args) {
15821
+ let ack;
15822
+ if (typeof args[args.length - 1] === "function") {
15823
+ ack = args.pop();
15824
+ }
15825
+ const packet = {
15826
+ id: this._queueSeq++,
15827
+ tryCount: 0,
15828
+ pending: false,
15829
+ args,
15830
+ flags: Object.assign({ fromQueue: true }, this.flags),
15831
+ };
15832
+ args.push((err, ...responseArgs) => {
15833
+ if (packet !== this._queue[0]) {
15834
+ // the packet has already been acknowledged
15835
+ return;
15836
+ }
15837
+ const hasError = err !== null;
15838
+ if (hasError) {
15839
+ if (packet.tryCount > this._opts.retries) {
15840
+ this._queue.shift();
15841
+ if (ack) {
15842
+ ack(err);
15843
+ }
15844
+ }
15845
+ }
15846
+ else {
15847
+ this._queue.shift();
15848
+ if (ack) {
15849
+ ack(null, ...responseArgs);
15850
+ }
15851
+ }
15852
+ packet.pending = false;
15853
+ return this._drainQueue();
15854
+ });
15855
+ this._queue.push(packet);
15856
+ this._drainQueue();
15857
+ }
15858
+ /**
15859
+ * Send the first packet of the queue, and wait for an acknowledgement from the server.
15860
+ * @param force - whether to resend a packet that has not been acknowledged yet
15861
+ *
15862
+ * @private
15863
+ */
15864
+ _drainQueue(force = false) {
15865
+ if (!this.connected || this._queue.length === 0) {
15866
+ return;
15867
+ }
15868
+ const packet = this._queue[0];
15869
+ if (packet.pending && !force) {
15870
+ return;
15871
+ }
15872
+ packet.pending = true;
15873
+ packet.tryCount++;
15874
+ this.flags = packet.flags;
15875
+ this.emit.apply(this, packet.args);
15876
+ }
15877
+ /**
15878
+ * Sends a packet.
15879
+ *
15880
+ * @param packet
15881
+ * @private
15882
+ */
15883
+ packet(packet) {
15884
+ packet.nsp = this.nsp;
15885
+ this.io._packet(packet);
15886
+ }
15887
+ /**
15888
+ * Called upon engine `open`.
15889
+ *
15890
+ * @private
15891
+ */
15892
+ onopen() {
15893
+ if (typeof this.auth == "function") {
15894
+ this.auth((data) => {
15895
+ this._sendConnectPacket(data);
15896
+ });
15897
+ }
15898
+ else {
15899
+ this._sendConnectPacket(this.auth);
15900
+ }
15901
+ }
15902
+ /**
15903
+ * Sends a CONNECT packet to initiate the Socket.IO session.
15904
+ *
15905
+ * @param data
15906
+ * @private
15907
+ */
15908
+ _sendConnectPacket(data) {
15909
+ this.packet({
15910
+ type: PacketType.CONNECT,
15911
+ data: this._pid
15912
+ ? Object.assign({ pid: this._pid, offset: this._lastOffset }, data)
15913
+ : data,
15914
+ });
15915
+ }
15916
+ /**
15917
+ * Called upon engine or manager `error`.
15918
+ *
15919
+ * @param err
15920
+ * @private
15921
+ */
15922
+ onerror(err) {
15923
+ if (!this.connected) {
15924
+ this.emitReserved("connect_error", err);
15925
+ }
15926
+ }
15927
+ /**
15928
+ * Called upon engine `close`.
15929
+ *
15930
+ * @param reason
15931
+ * @param description
15932
+ * @private
15933
+ */
15934
+ onclose(reason, description) {
15935
+ this.connected = false;
15936
+ delete this.id;
15937
+ this.emitReserved("disconnect", reason, description);
15938
+ this._clearAcks();
15939
+ }
15940
+ /**
15941
+ * Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
15942
+ * the server.
15943
+ *
15944
+ * @private
15945
+ */
15946
+ _clearAcks() {
15947
+ Object.keys(this.acks).forEach((id) => {
15948
+ const isBuffered = this.sendBuffer.some((packet) => String(packet.id) === id);
15949
+ if (!isBuffered) {
15950
+ // note: handlers that do not accept an error as first argument are ignored here
15951
+ const ack = this.acks[id];
15952
+ delete this.acks[id];
15953
+ if (ack.withError) {
15954
+ ack.call(this, new Error("socket has been disconnected"));
15955
+ }
15956
+ }
15957
+ });
15958
+ }
15959
+ /**
15960
+ * Called with socket packet.
15961
+ *
15962
+ * @param packet
15963
+ * @private
15964
+ */
15965
+ onpacket(packet) {
15966
+ const sameNamespace = packet.nsp === this.nsp;
15967
+ if (!sameNamespace)
15968
+ return;
15969
+ switch (packet.type) {
15970
+ case PacketType.CONNECT:
15971
+ if (packet.data && packet.data.sid) {
15972
+ this.onconnect(packet.data.sid, packet.data.pid);
15973
+ }
15974
+ else {
15975
+ this.emitReserved("connect_error", new Error("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)"));
15976
+ }
15977
+ break;
15978
+ case PacketType.EVENT:
15979
+ case PacketType.BINARY_EVENT:
15980
+ this.onevent(packet);
15981
+ break;
15982
+ case PacketType.ACK:
15983
+ case PacketType.BINARY_ACK:
15984
+ this.onack(packet);
15985
+ break;
15986
+ case PacketType.DISCONNECT:
15987
+ this.ondisconnect();
15988
+ break;
15989
+ case PacketType.CONNECT_ERROR:
15990
+ this.destroy();
15991
+ const err = new Error(packet.data.message);
15992
+ // @ts-ignore
15993
+ err.data = packet.data.data;
15994
+ this.emitReserved("connect_error", err);
15995
+ break;
15996
+ }
15997
+ }
15998
+ /**
15999
+ * Called upon a server event.
16000
+ *
16001
+ * @param packet
16002
+ * @private
16003
+ */
16004
+ onevent(packet) {
16005
+ const args = packet.data || [];
16006
+ if (null != packet.id) {
16007
+ args.push(this.ack(packet.id));
16008
+ }
16009
+ if (this.connected) {
16010
+ this.emitEvent(args);
16011
+ }
16012
+ else {
16013
+ this.receiveBuffer.push(Object.freeze(args));
16014
+ }
16015
+ }
16016
+ emitEvent(args) {
16017
+ if (this._anyListeners && this._anyListeners.length) {
16018
+ const listeners = this._anyListeners.slice();
16019
+ for (const listener of listeners) {
16020
+ listener.apply(this, args);
16021
+ }
16022
+ }
16023
+ super.emit.apply(this, args);
16024
+ if (this._pid && args.length && typeof args[args.length - 1] === "string") {
16025
+ this._lastOffset = args[args.length - 1];
16026
+ }
16027
+ }
16028
+ /**
16029
+ * Produces an ack callback to emit with an event.
16030
+ *
16031
+ * @private
16032
+ */
16033
+ ack(id) {
16034
+ const self = this;
16035
+ let sent = false;
16036
+ return function (...args) {
16037
+ // prevent double callbacks
16038
+ if (sent)
16039
+ return;
16040
+ sent = true;
16041
+ self.packet({
16042
+ type: PacketType.ACK,
16043
+ id: id,
16044
+ data: args,
16045
+ });
16046
+ };
16047
+ }
16048
+ /**
16049
+ * Called upon a server acknowledgement.
16050
+ *
16051
+ * @param packet
16052
+ * @private
16053
+ */
16054
+ onack(packet) {
16055
+ const ack = this.acks[packet.id];
16056
+ if (typeof ack !== "function") {
16057
+ return;
16058
+ }
16059
+ delete this.acks[packet.id];
16060
+ // @ts-ignore FIXME ack is incorrectly inferred as 'never'
16061
+ if (ack.withError) {
16062
+ packet.data.unshift(null);
16063
+ }
16064
+ // @ts-ignore
16065
+ ack.apply(this, packet.data);
16066
+ }
16067
+ /**
16068
+ * Called upon server connect.
16069
+ *
16070
+ * @private
16071
+ */
16072
+ onconnect(id, pid) {
16073
+ this.id = id;
16074
+ this.recovered = pid && this._pid === pid;
16075
+ this._pid = pid; // defined only if connection state recovery is enabled
16076
+ this.connected = true;
16077
+ this.emitBuffered();
16078
+ this.emitReserved("connect");
16079
+ this._drainQueue(true);
16080
+ }
16081
+ /**
16082
+ * Emit buffered events (received and emitted).
16083
+ *
16084
+ * @private
16085
+ */
16086
+ emitBuffered() {
16087
+ this.receiveBuffer.forEach((args) => this.emitEvent(args));
16088
+ this.receiveBuffer = [];
16089
+ this.sendBuffer.forEach((packet) => {
16090
+ this.notifyOutgoingListeners(packet);
16091
+ this.packet(packet);
16092
+ });
16093
+ this.sendBuffer = [];
16094
+ }
16095
+ /**
16096
+ * Called upon server disconnect.
16097
+ *
16098
+ * @private
16099
+ */
16100
+ ondisconnect() {
16101
+ this.destroy();
16102
+ this.onclose("io server disconnect");
16103
+ }
16104
+ /**
16105
+ * Called upon forced client/server side disconnections,
16106
+ * this method ensures the manager stops tracking us and
16107
+ * that reconnections don't get triggered for this.
16108
+ *
16109
+ * @private
16110
+ */
16111
+ destroy() {
16112
+ if (this.subs) {
16113
+ // clean subscriptions to avoid reconnections
16114
+ this.subs.forEach((subDestroy) => subDestroy());
16115
+ this.subs = undefined;
16116
+ }
16117
+ this.io["_destroy"](this);
16118
+ }
16119
+ /**
16120
+ * Disconnects the socket manually. In that case, the socket will not try to reconnect.
16121
+ *
16122
+ * If this is the last active Socket instance of the {@link Manager}, the low-level connection will be closed.
16123
+ *
16124
+ * @example
16125
+ * const socket = io();
16126
+ *
16127
+ * socket.on("disconnect", (reason) => {
16128
+ * // console.log(reason); prints "io client disconnect"
16129
+ * });
16130
+ *
16131
+ * socket.disconnect();
16132
+ *
16133
+ * @return self
16134
+ */
16135
+ disconnect() {
16136
+ if (this.connected) {
16137
+ this.packet({ type: PacketType.DISCONNECT });
16138
+ }
16139
+ // remove socket from pool
16140
+ this.destroy();
16141
+ if (this.connected) {
16142
+ // fire events
16143
+ this.onclose("io client disconnect");
16144
+ }
16145
+ return this;
16146
+ }
16147
+ /**
16148
+ * Alias for {@link disconnect()}.
16149
+ *
16150
+ * @return self
16151
+ */
16152
+ close() {
16153
+ return this.disconnect();
16154
+ }
16155
+ /**
16156
+ * Sets the compress flag.
16157
+ *
16158
+ * @example
16159
+ * socket.compress(false).emit("hello");
16160
+ *
16161
+ * @param compress - if `true`, compresses the sending data
16162
+ * @return self
16163
+ */
16164
+ compress(compress) {
16165
+ this.flags.compress = compress;
16166
+ return this;
16167
+ }
16168
+ /**
16169
+ * Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not
16170
+ * ready to send messages.
16171
+ *
16172
+ * @example
16173
+ * socket.volatile.emit("hello"); // the server may or may not receive it
16174
+ *
16175
+ * @returns self
16176
+ */
16177
+ get volatile() {
16178
+ this.flags.volatile = true;
16179
+ return this;
16180
+ }
16181
+ /**
16182
+ * Sets a modifier for a subsequent event emission that the callback will be called with an error when the
16183
+ * given number of milliseconds have elapsed without an acknowledgement from the server:
16184
+ *
16185
+ * @example
16186
+ * socket.timeout(5000).emit("my-event", (err) => {
16187
+ * if (err) {
16188
+ * // the server did not acknowledge the event in the given delay
16189
+ * }
16190
+ * });
16191
+ *
16192
+ * @returns self
16193
+ */
16194
+ timeout(timeout) {
16195
+ this.flags.timeout = timeout;
16196
+ return this;
16197
+ }
16198
+ /**
16199
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
16200
+ * callback.
16201
+ *
16202
+ * @example
16203
+ * socket.onAny((event, ...args) => {
16204
+ * console.log(`got ${event}`);
16205
+ * });
16206
+ *
16207
+ * @param listener
16208
+ */
16209
+ onAny(listener) {
16210
+ this._anyListeners = this._anyListeners || [];
16211
+ this._anyListeners.push(listener);
16212
+ return this;
16213
+ }
16214
+ /**
16215
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
16216
+ * callback. The listener is added to the beginning of the listeners array.
16217
+ *
16218
+ * @example
16219
+ * socket.prependAny((event, ...args) => {
16220
+ * console.log(`got event ${event}`);
16221
+ * });
16222
+ *
16223
+ * @param listener
16224
+ */
16225
+ prependAny(listener) {
16226
+ this._anyListeners = this._anyListeners || [];
16227
+ this._anyListeners.unshift(listener);
16228
+ return this;
16229
+ }
16230
+ /**
16231
+ * Removes the listener that will be fired when any event is emitted.
16232
+ *
16233
+ * @example
16234
+ * const catchAllListener = (event, ...args) => {
16235
+ * console.log(`got event ${event}`);
16236
+ * }
16237
+ *
16238
+ * socket.onAny(catchAllListener);
16239
+ *
16240
+ * // remove a specific listener
16241
+ * socket.offAny(catchAllListener);
16242
+ *
16243
+ * // or remove all listeners
16244
+ * socket.offAny();
16245
+ *
16246
+ * @param listener
16247
+ */
16248
+ offAny(listener) {
16249
+ if (!this._anyListeners) {
16250
+ return this;
16251
+ }
16252
+ if (listener) {
16253
+ const listeners = this._anyListeners;
16254
+ for (let i = 0; i < listeners.length; i++) {
16255
+ if (listener === listeners[i]) {
16256
+ listeners.splice(i, 1);
16257
+ return this;
16258
+ }
16259
+ }
16260
+ }
16261
+ else {
16262
+ this._anyListeners = [];
16263
+ }
16264
+ return this;
16265
+ }
16266
+ /**
16267
+ * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
16268
+ * e.g. to remove listeners.
16269
+ */
16270
+ listenersAny() {
16271
+ return this._anyListeners || [];
16272
+ }
16273
+ /**
16274
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
16275
+ * callback.
16276
+ *
16277
+ * Note: acknowledgements sent to the server are not included.
16278
+ *
16279
+ * @example
16280
+ * socket.onAnyOutgoing((event, ...args) => {
16281
+ * console.log(`sent event ${event}`);
16282
+ * });
16283
+ *
16284
+ * @param listener
16285
+ */
16286
+ onAnyOutgoing(listener) {
16287
+ this._anyOutgoingListeners = this._anyOutgoingListeners || [];
16288
+ this._anyOutgoingListeners.push(listener);
16289
+ return this;
16290
+ }
16291
+ /**
16292
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
16293
+ * callback. The listener is added to the beginning of the listeners array.
16294
+ *
16295
+ * Note: acknowledgements sent to the server are not included.
16296
+ *
16297
+ * @example
16298
+ * socket.prependAnyOutgoing((event, ...args) => {
16299
+ * console.log(`sent event ${event}`);
16300
+ * });
16301
+ *
16302
+ * @param listener
16303
+ */
16304
+ prependAnyOutgoing(listener) {
16305
+ this._anyOutgoingListeners = this._anyOutgoingListeners || [];
16306
+ this._anyOutgoingListeners.unshift(listener);
16307
+ return this;
16308
+ }
16309
+ /**
16310
+ * Removes the listener that will be fired when any event is emitted.
16311
+ *
16312
+ * @example
16313
+ * const catchAllListener = (event, ...args) => {
16314
+ * console.log(`sent event ${event}`);
16315
+ * }
16316
+ *
16317
+ * socket.onAnyOutgoing(catchAllListener);
16318
+ *
16319
+ * // remove a specific listener
16320
+ * socket.offAnyOutgoing(catchAllListener);
16321
+ *
16322
+ * // or remove all listeners
16323
+ * socket.offAnyOutgoing();
16324
+ *
16325
+ * @param [listener] - the catch-all listener (optional)
16326
+ */
16327
+ offAnyOutgoing(listener) {
16328
+ if (!this._anyOutgoingListeners) {
16329
+ return this;
16330
+ }
16331
+ if (listener) {
16332
+ const listeners = this._anyOutgoingListeners;
16333
+ for (let i = 0; i < listeners.length; i++) {
16334
+ if (listener === listeners[i]) {
16335
+ listeners.splice(i, 1);
16336
+ return this;
16337
+ }
16338
+ }
16339
+ }
16340
+ else {
16341
+ this._anyOutgoingListeners = [];
16342
+ }
16343
+ return this;
16344
+ }
16345
+ /**
16346
+ * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
16347
+ * e.g. to remove listeners.
16348
+ */
16349
+ listenersAnyOutgoing() {
16350
+ return this._anyOutgoingListeners || [];
16351
+ }
16352
+ /**
16353
+ * Notify the listeners for each packet sent
16354
+ *
16355
+ * @param packet
16356
+ *
16357
+ * @private
16358
+ */
16359
+ notifyOutgoingListeners(packet) {
16360
+ if (this._anyOutgoingListeners && this._anyOutgoingListeners.length) {
16361
+ const listeners = this._anyOutgoingListeners.slice();
16362
+ for (const listener of listeners) {
16363
+ listener.apply(this, packet.data);
16364
+ }
16365
+ }
16366
+ }
16367
+ }
16368
+
16369
+ /**
16370
+ * Initialize backoff timer with `opts`.
16371
+ *
16372
+ * - `min` initial timeout in milliseconds [100]
16373
+ * - `max` max timeout [10000]
16374
+ * - `jitter` [0]
16375
+ * - `factor` [2]
16376
+ *
16377
+ * @param {Object} opts
16378
+ * @api public
16379
+ */
16380
+ function Backoff(opts) {
16381
+ opts = opts || {};
16382
+ this.ms = opts.min || 100;
16383
+ this.max = opts.max || 10000;
16384
+ this.factor = opts.factor || 2;
16385
+ this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
16386
+ this.attempts = 0;
16387
+ }
16388
+ /**
16389
+ * Return the backoff duration.
16390
+ *
16391
+ * @return {Number}
16392
+ * @api public
16393
+ */
16394
+ Backoff.prototype.duration = function () {
16395
+ var ms = this.ms * Math.pow(this.factor, this.attempts++);
16396
+ if (this.jitter) {
16397
+ var rand = Math.random();
16398
+ var deviation = Math.floor(rand * this.jitter * ms);
16399
+ ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
16400
+ }
16401
+ return Math.min(ms, this.max) | 0;
16402
+ };
16403
+ /**
16404
+ * Reset the number of attempts.
16405
+ *
16406
+ * @api public
16407
+ */
16408
+ Backoff.prototype.reset = function () {
16409
+ this.attempts = 0;
16410
+ };
16411
+ /**
16412
+ * Set the minimum duration
16413
+ *
16414
+ * @api public
16415
+ */
16416
+ Backoff.prototype.setMin = function (min) {
16417
+ this.ms = min;
16418
+ };
16419
+ /**
16420
+ * Set the maximum duration
16421
+ *
16422
+ * @api public
16423
+ */
16424
+ Backoff.prototype.setMax = function (max) {
16425
+ this.max = max;
16426
+ };
16427
+ /**
16428
+ * Set the jitter
16429
+ *
16430
+ * @api public
16431
+ */
16432
+ Backoff.prototype.setJitter = function (jitter) {
16433
+ this.jitter = jitter;
16434
+ };
16435
+
16436
+ class Manager extends Emitter {
16437
+ constructor(uri, opts) {
16438
+ var _a;
16439
+ super();
16440
+ this.nsps = {};
16441
+ this.subs = [];
16442
+ if (uri && "object" === typeof uri) {
16443
+ opts = uri;
16444
+ uri = undefined;
16445
+ }
16446
+ opts = opts || {};
16447
+ opts.path = opts.path || "/socket.io";
16448
+ this.opts = opts;
16449
+ installTimerFunctions(this, opts);
16450
+ this.reconnection(opts.reconnection !== false);
16451
+ this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
16452
+ this.reconnectionDelay(opts.reconnectionDelay || 1000);
16453
+ this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
16454
+ this.randomizationFactor((_a = opts.randomizationFactor) !== null && _a !== void 0 ? _a : 0.5);
16455
+ this.backoff = new Backoff({
16456
+ min: this.reconnectionDelay(),
16457
+ max: this.reconnectionDelayMax(),
16458
+ jitter: this.randomizationFactor(),
16459
+ });
16460
+ this.timeout(null == opts.timeout ? 20000 : opts.timeout);
16461
+ this._readyState = "closed";
16462
+ this.uri = uri;
16463
+ const _parser = opts.parser || parser;
16464
+ this.encoder = new _parser.Encoder();
16465
+ this.decoder = new _parser.Decoder();
16466
+ this._autoConnect = opts.autoConnect !== false;
16467
+ if (this._autoConnect)
16468
+ this.open();
16469
+ }
16470
+ reconnection(v) {
16471
+ if (!arguments.length)
16472
+ return this._reconnection;
16473
+ this._reconnection = !!v;
16474
+ if (!v) {
16475
+ this.skipReconnect = true;
16476
+ }
16477
+ return this;
16478
+ }
16479
+ reconnectionAttempts(v) {
16480
+ if (v === undefined)
16481
+ return this._reconnectionAttempts;
16482
+ this._reconnectionAttempts = v;
16483
+ return this;
16484
+ }
16485
+ reconnectionDelay(v) {
16486
+ var _a;
16487
+ if (v === undefined)
16488
+ return this._reconnectionDelay;
16489
+ this._reconnectionDelay = v;
16490
+ (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMin(v);
16491
+ return this;
16492
+ }
16493
+ randomizationFactor(v) {
16494
+ var _a;
16495
+ if (v === undefined)
16496
+ return this._randomizationFactor;
16497
+ this._randomizationFactor = v;
16498
+ (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setJitter(v);
16499
+ return this;
16500
+ }
16501
+ reconnectionDelayMax(v) {
16502
+ var _a;
16503
+ if (v === undefined)
16504
+ return this._reconnectionDelayMax;
16505
+ this._reconnectionDelayMax = v;
16506
+ (_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMax(v);
16507
+ return this;
16508
+ }
16509
+ timeout(v) {
16510
+ if (!arguments.length)
16511
+ return this._timeout;
16512
+ this._timeout = v;
16513
+ return this;
16514
+ }
16515
+ /**
16516
+ * Starts trying to reconnect if reconnection is enabled and we have not
16517
+ * started reconnecting yet
16518
+ *
16519
+ * @private
16520
+ */
16521
+ maybeReconnectOnOpen() {
16522
+ // Only try to reconnect if it's the first time we're connecting
16523
+ if (!this._reconnecting &&
16524
+ this._reconnection &&
16525
+ this.backoff.attempts === 0) {
16526
+ // keeps reconnection from firing twice for the same reconnection loop
16527
+ this.reconnect();
16528
+ }
16529
+ }
16530
+ /**
16531
+ * Sets the current transport `socket`.
16532
+ *
16533
+ * @param {Function} fn - optional, callback
16534
+ * @return self
16535
+ * @public
16536
+ */
16537
+ open(fn) {
16538
+ if (~this._readyState.indexOf("open"))
16539
+ return this;
16540
+ this.engine = new Socket$1(this.uri, this.opts);
16541
+ const socket = this.engine;
16542
+ const self = this;
16543
+ this._readyState = "opening";
16544
+ this.skipReconnect = false;
16545
+ // emit `open`
16546
+ const openSubDestroy = on(socket, "open", function () {
16547
+ self.onopen();
16548
+ fn && fn();
16549
+ });
16550
+ const onError = (err) => {
16551
+ this.cleanup();
16552
+ this._readyState = "closed";
16553
+ this.emitReserved("error", err);
16554
+ if (fn) {
16555
+ fn(err);
16556
+ }
16557
+ else {
16558
+ // Only do this if there is no fn to handle the error
16559
+ this.maybeReconnectOnOpen();
16560
+ }
16561
+ };
16562
+ // emit `error`
16563
+ const errorSub = on(socket, "error", onError);
16564
+ if (false !== this._timeout) {
16565
+ const timeout = this._timeout;
16566
+ // set timer
16567
+ const timer = this.setTimeoutFn(() => {
16568
+ openSubDestroy();
16569
+ onError(new Error("timeout"));
16570
+ socket.close();
16571
+ }, timeout);
16572
+ if (this.opts.autoUnref) {
16573
+ timer.unref();
16574
+ }
16575
+ this.subs.push(() => {
16576
+ this.clearTimeoutFn(timer);
16577
+ });
16578
+ }
16579
+ this.subs.push(openSubDestroy);
16580
+ this.subs.push(errorSub);
16581
+ return this;
16582
+ }
16583
+ /**
16584
+ * Alias for open()
16585
+ *
16586
+ * @return self
16587
+ * @public
16588
+ */
16589
+ connect(fn) {
16590
+ return this.open(fn);
16591
+ }
16592
+ /**
16593
+ * Called upon transport open.
16594
+ *
16595
+ * @private
16596
+ */
16597
+ onopen() {
16598
+ // clear old subs
16599
+ this.cleanup();
16600
+ // mark as open
16601
+ this._readyState = "open";
16602
+ this.emitReserved("open");
16603
+ // add new subs
16604
+ const socket = this.engine;
16605
+ this.subs.push(on(socket, "ping", this.onping.bind(this)), on(socket, "data", this.ondata.bind(this)), on(socket, "error", this.onerror.bind(this)), on(socket, "close", this.onclose.bind(this)),
16606
+ // @ts-ignore
16607
+ on(this.decoder, "decoded", this.ondecoded.bind(this)));
16608
+ }
16609
+ /**
16610
+ * Called upon a ping.
16611
+ *
16612
+ * @private
16613
+ */
16614
+ onping() {
16615
+ this.emitReserved("ping");
16616
+ }
16617
+ /**
16618
+ * Called with data.
16619
+ *
16620
+ * @private
16621
+ */
16622
+ ondata(data) {
16623
+ try {
16624
+ this.decoder.add(data);
16625
+ }
16626
+ catch (e) {
16627
+ this.onclose("parse error", e);
16628
+ }
16629
+ }
16630
+ /**
16631
+ * Called when parser fully decodes a packet.
16632
+ *
16633
+ * @private
16634
+ */
16635
+ ondecoded(packet) {
16636
+ // the nextTick call prevents an exception in a user-provided event listener from triggering a disconnection due to a "parse error"
16637
+ nextTick(() => {
16638
+ this.emitReserved("packet", packet);
16639
+ }, this.setTimeoutFn);
16640
+ }
16641
+ /**
16642
+ * Called upon socket error.
16643
+ *
16644
+ * @private
16645
+ */
16646
+ onerror(err) {
16647
+ this.emitReserved("error", err);
16648
+ }
16649
+ /**
16650
+ * Creates a new socket for the given `nsp`.
16651
+ *
16652
+ * @return {Socket}
16653
+ * @public
16654
+ */
16655
+ socket(nsp, opts) {
16656
+ let socket = this.nsps[nsp];
16657
+ if (!socket) {
16658
+ socket = new Socket(this, nsp, opts);
16659
+ this.nsps[nsp] = socket;
16660
+ }
16661
+ else if (this._autoConnect && !socket.active) {
16662
+ socket.connect();
16663
+ }
16664
+ return socket;
16665
+ }
16666
+ /**
16667
+ * Called upon a socket close.
16668
+ *
16669
+ * @param socket
16670
+ * @private
16671
+ */
16672
+ _destroy(socket) {
16673
+ const nsps = Object.keys(this.nsps);
16674
+ for (const nsp of nsps) {
16675
+ const socket = this.nsps[nsp];
16676
+ if (socket.active) {
16677
+ return;
16678
+ }
16679
+ }
16680
+ this._close();
16681
+ }
16682
+ /**
16683
+ * Writes a packet.
16684
+ *
16685
+ * @param packet
16686
+ * @private
16687
+ */
16688
+ _packet(packet) {
16689
+ const encodedPackets = this.encoder.encode(packet);
16690
+ for (let i = 0; i < encodedPackets.length; i++) {
16691
+ this.engine.write(encodedPackets[i], packet.options);
16692
+ }
16693
+ }
16694
+ /**
16695
+ * Clean up transport subscriptions and packet buffer.
16696
+ *
16697
+ * @private
16698
+ */
16699
+ cleanup() {
16700
+ this.subs.forEach((subDestroy) => subDestroy());
16701
+ this.subs.length = 0;
16702
+ this.decoder.destroy();
16703
+ }
16704
+ /**
16705
+ * Close the current socket.
16706
+ *
16707
+ * @private
16708
+ */
16709
+ _close() {
16710
+ this.skipReconnect = true;
16711
+ this._reconnecting = false;
16712
+ this.onclose("forced close");
16713
+ }
16714
+ /**
16715
+ * Alias for close()
16716
+ *
16717
+ * @private
16718
+ */
16719
+ disconnect() {
16720
+ return this._close();
16721
+ }
16722
+ /**
16723
+ * Called when:
16724
+ *
16725
+ * - the low-level engine is closed
16726
+ * - the parser encountered a badly formatted packet
16727
+ * - all sockets are disconnected
16728
+ *
16729
+ * @private
16730
+ */
16731
+ onclose(reason, description) {
16732
+ var _a;
16733
+ this.cleanup();
16734
+ (_a = this.engine) === null || _a === void 0 ? void 0 : _a.close();
16735
+ this.backoff.reset();
16736
+ this._readyState = "closed";
16737
+ this.emitReserved("close", reason, description);
16738
+ if (this._reconnection && !this.skipReconnect) {
16739
+ this.reconnect();
16740
+ }
16741
+ }
16742
+ /**
16743
+ * Attempt a reconnection.
16744
+ *
16745
+ * @private
16746
+ */
16747
+ reconnect() {
16748
+ if (this._reconnecting || this.skipReconnect)
16749
+ return this;
16750
+ const self = this;
16751
+ if (this.backoff.attempts >= this._reconnectionAttempts) {
16752
+ this.backoff.reset();
16753
+ this.emitReserved("reconnect_failed");
16754
+ this._reconnecting = false;
16755
+ }
16756
+ else {
16757
+ const delay = this.backoff.duration();
16758
+ this._reconnecting = true;
16759
+ const timer = this.setTimeoutFn(() => {
16760
+ if (self.skipReconnect)
16761
+ return;
16762
+ this.emitReserved("reconnect_attempt", self.backoff.attempts);
16763
+ // check again for the case socket closed in above events
16764
+ if (self.skipReconnect)
16765
+ return;
16766
+ self.open((err) => {
16767
+ if (err) {
16768
+ self._reconnecting = false;
16769
+ self.reconnect();
16770
+ this.emitReserved("reconnect_error", err);
16771
+ }
16772
+ else {
16773
+ self.onreconnect();
16774
+ }
16775
+ });
16776
+ }, delay);
16777
+ if (this.opts.autoUnref) {
16778
+ timer.unref();
16779
+ }
16780
+ this.subs.push(() => {
16781
+ this.clearTimeoutFn(timer);
16782
+ });
16783
+ }
16784
+ }
16785
+ /**
16786
+ * Called upon successful reconnect.
16787
+ *
16788
+ * @private
16789
+ */
16790
+ onreconnect() {
16791
+ const attempt = this.backoff.attempts;
16792
+ this._reconnecting = false;
16793
+ this.backoff.reset();
16794
+ this.emitReserved("reconnect", attempt);
16795
+ }
16796
+ }
16797
+
16798
+ /**
16799
+ * Managers cache.
16800
+ */
16801
+ const cache = {};
16802
+ function lookup(uri, opts) {
16803
+ if (typeof uri === "object") {
16804
+ opts = uri;
16805
+ uri = undefined;
16806
+ }
16807
+ opts = opts || {};
16808
+ const parsed = url(uri, opts.path || "/socket.io");
16809
+ const source = parsed.source;
16810
+ const id = parsed.id;
16811
+ const path = parsed.path;
16812
+ const sameNamespace = cache[id] && path in cache[id]["nsps"];
16813
+ const newConnection = opts.forceNew ||
16814
+ opts["force new connection"] ||
16815
+ false === opts.multiplex ||
16816
+ sameNamespace;
16817
+ let io;
16818
+ if (newConnection) {
16819
+ io = new Manager(source, opts);
16820
+ }
16821
+ else {
16822
+ if (!cache[id]) {
16823
+ cache[id] = new Manager(source, opts);
16824
+ }
16825
+ io = cache[id];
16826
+ }
16827
+ if (parsed.query && !opts.query) {
16828
+ opts.query = parsed.queryKey;
16829
+ }
16830
+ return io.socket(parsed.path, opts);
16831
+ }
16832
+ // so that "lookup" can be used both as a function (e.g. `io(...)`) and as a
16833
+ // namespace (e.g. `io.connect(...)`), for backward compatibility
16834
+ Object.assign(lookup, {
16835
+ Manager,
16836
+ Socket,
16837
+ io: lookup,
16838
+ connect: lookup,
16839
+ });
16840
+
16841
+ var index = /*#__PURE__*/Object.freeze({
16842
+ __proto__: null,
16843
+ Manager: Manager,
16844
+ NodeWebSocket: WS,
16845
+ NodeXHR: XHR,
16846
+ Socket: Socket,
16847
+ WebSocket: WS,
16848
+ WebTransport: WT,
16849
+ XHR: XHR,
16850
+ connect: lookup,
16851
+ default: lookup,
16852
+ io: lookup,
16853
+ protocol: protocol
16854
+ });
16855
+
12677
16856
  export { Zaplier, ZaplierSDK, analyzeUserAgent, autoInitializeFromScript, clearFingerprintCache, collectFingerprint, ZaplierSDK as default, detectBot, detectIncognitoMode, generateVisitorId, generateVisitorIdFromFingerprint, getAvailableComponents, getBrowserEngine, getCompleteFingerprint, getLightweightFingerprint, hash32, hashFingerprint, isAndroid, isChromium, isDesktop, isFingerprintingAvailable, isGecko, isIPad, isMobile, isSamsungInternet, isTablet, isWebKit, quickBotDetection, quickIncognitoDetection, x64hash128 };
12678
16857
  //# sourceMappingURL=index.esm.js.map