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