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