@zaplier/sdk 1.2.1 → 1.2.3

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