core-outline 1.1.3 → 1.1.4
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.es.js +2170 -25
- package/dist/index.js +2170 -25
- package/package.json +3 -2
- package/src/components/CoreOutline/CoreOutline.js +123 -9
- package/src/components/CoreOutline/rrweb-events (9).json +1 -0
package/dist/index.es.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import React, { useState, useRef, useEffect } from 'react';
|
|
2
|
+
import { v4 } from 'uuid';
|
|
3
|
+
import { parse, installTimerFunctions, Socket as Socket$1, nextTick } from 'engine.io-client';
|
|
2
4
|
|
|
3
5
|
var NodeType;
|
|
4
6
|
(function (NodeType) {
|
|
@@ -1022,7 +1024,7 @@ function snapshot(n, options) {
|
|
|
1022
1024
|
});
|
|
1023
1025
|
}
|
|
1024
1026
|
|
|
1025
|
-
function on(type, fn, target = document) {
|
|
1027
|
+
function on$1(type, fn, target = document) {
|
|
1026
1028
|
const options = { capture: true, passive: true };
|
|
1027
1029
|
target.addEventListener(type, fn, options);
|
|
1028
1030
|
return () => target.removeEventListener(type, fn, options);
|
|
@@ -1925,9 +1927,9 @@ function initMoveObserver({ mousemoveCb, sampling, doc, mirror, }) {
|
|
|
1925
1927
|
trailing: false,
|
|
1926
1928
|
});
|
|
1927
1929
|
const handlers = [
|
|
1928
|
-
on('mousemove', updatePosition, doc),
|
|
1929
|
-
on('touchmove', updatePosition, doc),
|
|
1930
|
-
on('drag', updatePosition, doc),
|
|
1930
|
+
on$1('mousemove', updatePosition, doc),
|
|
1931
|
+
on$1('touchmove', updatePosition, doc),
|
|
1932
|
+
on$1('drag', updatePosition, doc),
|
|
1931
1933
|
];
|
|
1932
1934
|
return () => {
|
|
1933
1935
|
handlers.forEach((h) => h());
|
|
@@ -1970,7 +1972,7 @@ function initMouseInteractionObserver({ mouseInteractionCb, doc, mirror, blockCl
|
|
|
1970
1972
|
.forEach((eventKey) => {
|
|
1971
1973
|
const eventName = eventKey.toLowerCase();
|
|
1972
1974
|
const handler = getHandler(eventKey);
|
|
1973
|
-
handlers.push(on(eventName, handler, doc));
|
|
1975
|
+
handlers.push(on$1(eventName, handler, doc));
|
|
1974
1976
|
});
|
|
1975
1977
|
return () => {
|
|
1976
1978
|
handlers.forEach((h) => h());
|
|
@@ -1999,7 +2001,7 @@ function initScrollObserver({ scrollCb, doc, mirror, blockClass, blockSelector,
|
|
|
1999
2001
|
});
|
|
2000
2002
|
}
|
|
2001
2003
|
}, sampling.scroll || 100);
|
|
2002
|
-
return on('scroll', updatePosition, doc);
|
|
2004
|
+
return on$1('scroll', updatePosition, doc);
|
|
2003
2005
|
}
|
|
2004
2006
|
function initViewportResizeObserver({ viewportResizeCb, }) {
|
|
2005
2007
|
let lastH = -1;
|
|
@@ -2016,7 +2018,7 @@ function initViewportResizeObserver({ viewportResizeCb, }) {
|
|
|
2016
2018
|
lastW = width;
|
|
2017
2019
|
}
|
|
2018
2020
|
}, 200);
|
|
2019
|
-
return on('resize', updateDimension, window);
|
|
2021
|
+
return on$1('resize', updateDimension, window);
|
|
2020
2022
|
}
|
|
2021
2023
|
function wrapEventWithUserTriggeredFlag(v, enable) {
|
|
2022
2024
|
const value = Object.assign({}, v);
|
|
@@ -2084,7 +2086,7 @@ function initInputObserver({ inputCb, doc, mirror, blockClass, blockSelector, ig
|
|
|
2084
2086
|
}
|
|
2085
2087
|
}
|
|
2086
2088
|
const events = sampling.input === 'last' ? ['change'] : ['input', 'change'];
|
|
2087
|
-
const handlers = events.map((eventName) => on(eventName, eventHandler, doc));
|
|
2089
|
+
const handlers = events.map((eventName) => on$1(eventName, eventHandler, doc));
|
|
2088
2090
|
const currentWindow = doc.defaultView;
|
|
2089
2091
|
if (!currentWindow) {
|
|
2090
2092
|
return () => {
|
|
@@ -2378,11 +2380,11 @@ function initMediaInteractionObserver({ mediaInteractionCb, blockClass, blockSel
|
|
|
2378
2380
|
});
|
|
2379
2381
|
}, sampling.media || 500);
|
|
2380
2382
|
const handlers = [
|
|
2381
|
-
on('play', handler(0)),
|
|
2382
|
-
on('pause', handler(1)),
|
|
2383
|
-
on('seeked', handler(2)),
|
|
2384
|
-
on('volumechange', handler(3)),
|
|
2385
|
-
on('ratechange', handler(4)),
|
|
2383
|
+
on$1('play', handler(0)),
|
|
2384
|
+
on$1('pause', handler(1)),
|
|
2385
|
+
on$1('seeked', handler(2)),
|
|
2386
|
+
on$1('volumechange', handler(3)),
|
|
2387
|
+
on$1('ratechange', handler(4)),
|
|
2386
2388
|
];
|
|
2387
2389
|
return () => {
|
|
2388
2390
|
handlers.forEach((h) => h());
|
|
@@ -2456,7 +2458,7 @@ function initSelectionObserver(param) {
|
|
|
2456
2458
|
selectionCb({ ranges });
|
|
2457
2459
|
};
|
|
2458
2460
|
updateSelection();
|
|
2459
|
-
return on('selectionchange', updateSelection);
|
|
2461
|
+
return on$1('selectionchange', updateSelection);
|
|
2460
2462
|
}
|
|
2461
2463
|
function mergeHooks(o, hooks) {
|
|
2462
2464
|
const { mutationCb, mousemoveCb, mouseInteractionCb, scrollCb, viewportResizeCb, inputCb, mediaInteractionCb, styleSheetRuleCb, styleDeclarationCb, canvasMutationCb, fontCb, selectionCb, } = o;
|
|
@@ -2932,9 +2934,9 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
2932
2934
|
*/
|
|
2933
2935
|
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
2934
2936
|
// Use a lookup table to find the index.
|
|
2935
|
-
var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
|
|
2937
|
+
var lookup$1 = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
|
|
2936
2938
|
for (var i = 0; i < chars.length; i++) {
|
|
2937
|
-
lookup[chars.charCodeAt(i)] = i;
|
|
2939
|
+
lookup$1[chars.charCodeAt(i)] = i;
|
|
2938
2940
|
}
|
|
2939
2941
|
var encode = function (arraybuffer) {
|
|
2940
2942
|
var bytes = new Uint8Array(arraybuffer), i, len = bytes.length, base64 = '';
|
|
@@ -3777,7 +3779,7 @@ function record(options = {}) {
|
|
|
3777
3779
|
};
|
|
3778
3780
|
try {
|
|
3779
3781
|
const handlers = [];
|
|
3780
|
-
handlers.push(on('DOMContentLoaded', () => {
|
|
3782
|
+
handlers.push(on$1('DOMContentLoaded', () => {
|
|
3781
3783
|
wrappedEmit(wrapEvent({
|
|
3782
3784
|
type: EventType.DomContentLoaded,
|
|
3783
3785
|
data: {},
|
|
@@ -3880,7 +3882,7 @@ function record(options = {}) {
|
|
|
3880
3882
|
init();
|
|
3881
3883
|
}
|
|
3882
3884
|
else {
|
|
3883
|
-
handlers.push(on('load', () => {
|
|
3885
|
+
handlers.push(on$1('load', () => {
|
|
3884
3886
|
wrappedEmit(wrapEvent({
|
|
3885
3887
|
type: EventType.Load,
|
|
3886
3888
|
data: {},
|
|
@@ -3920,29 +3922,2113 @@ record.takeFullSnapshot = (isCheckout) => {
|
|
|
3920
3922
|
};
|
|
3921
3923
|
record.mirror = mirror;
|
|
3922
3924
|
|
|
3925
|
+
/**
|
|
3926
|
+
* URL parser.
|
|
3927
|
+
*
|
|
3928
|
+
* @param uri - url
|
|
3929
|
+
* @param path - the request path of the connection
|
|
3930
|
+
* @param loc - An object meant to mimic window.location.
|
|
3931
|
+
* Defaults to window.location.
|
|
3932
|
+
* @public
|
|
3933
|
+
*/
|
|
3934
|
+
function url(uri, path = "", loc) {
|
|
3935
|
+
let obj = uri;
|
|
3936
|
+
// default to window.location
|
|
3937
|
+
loc = loc || (typeof location !== "undefined" && location);
|
|
3938
|
+
if (null == uri)
|
|
3939
|
+
uri = loc.protocol + "//" + loc.host;
|
|
3940
|
+
// relative path support
|
|
3941
|
+
if (typeof uri === "string") {
|
|
3942
|
+
if ("/" === uri.charAt(0)) {
|
|
3943
|
+
if ("/" === uri.charAt(1)) {
|
|
3944
|
+
uri = loc.protocol + uri;
|
|
3945
|
+
}
|
|
3946
|
+
else {
|
|
3947
|
+
uri = loc.host + uri;
|
|
3948
|
+
}
|
|
3949
|
+
}
|
|
3950
|
+
if (!/^(https?|wss?):\/\//.test(uri)) {
|
|
3951
|
+
if ("undefined" !== typeof loc) {
|
|
3952
|
+
uri = loc.protocol + "//" + uri;
|
|
3953
|
+
}
|
|
3954
|
+
else {
|
|
3955
|
+
uri = "https://" + uri;
|
|
3956
|
+
}
|
|
3957
|
+
}
|
|
3958
|
+
// parse
|
|
3959
|
+
obj = parse(uri);
|
|
3960
|
+
}
|
|
3961
|
+
// make sure we treat `localhost:80` and `localhost` equally
|
|
3962
|
+
if (!obj.port) {
|
|
3963
|
+
if (/^(http|ws)$/.test(obj.protocol)) {
|
|
3964
|
+
obj.port = "80";
|
|
3965
|
+
}
|
|
3966
|
+
else if (/^(http|ws)s$/.test(obj.protocol)) {
|
|
3967
|
+
obj.port = "443";
|
|
3968
|
+
}
|
|
3969
|
+
}
|
|
3970
|
+
obj.path = obj.path || "/";
|
|
3971
|
+
const ipv6 = obj.host.indexOf(":") !== -1;
|
|
3972
|
+
const host = ipv6 ? "[" + obj.host + "]" : obj.host;
|
|
3973
|
+
// define unique id
|
|
3974
|
+
obj.id = obj.protocol + "://" + host + ":" + obj.port + path;
|
|
3975
|
+
// define href
|
|
3976
|
+
obj.href =
|
|
3977
|
+
obj.protocol +
|
|
3978
|
+
"://" +
|
|
3979
|
+
host +
|
|
3980
|
+
(loc && loc.port === obj.port ? "" : ":" + obj.port);
|
|
3981
|
+
return obj;
|
|
3982
|
+
}
|
|
3983
|
+
|
|
3984
|
+
/**
|
|
3985
|
+
* Initialize a new `Emitter`.
|
|
3986
|
+
*
|
|
3987
|
+
* @api public
|
|
3988
|
+
*/
|
|
3989
|
+
|
|
3990
|
+
function Emitter(obj) {
|
|
3991
|
+
if (obj) return mixin(obj);
|
|
3992
|
+
}
|
|
3993
|
+
|
|
3994
|
+
/**
|
|
3995
|
+
* Mixin the emitter properties.
|
|
3996
|
+
*
|
|
3997
|
+
* @param {Object} obj
|
|
3998
|
+
* @return {Object}
|
|
3999
|
+
* @api private
|
|
4000
|
+
*/
|
|
4001
|
+
|
|
4002
|
+
function mixin(obj) {
|
|
4003
|
+
for (var key in Emitter.prototype) {
|
|
4004
|
+
obj[key] = Emitter.prototype[key];
|
|
4005
|
+
}
|
|
4006
|
+
return obj;
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
/**
|
|
4010
|
+
* Listen on the given `event` with `fn`.
|
|
4011
|
+
*
|
|
4012
|
+
* @param {String} event
|
|
4013
|
+
* @param {Function} fn
|
|
4014
|
+
* @return {Emitter}
|
|
4015
|
+
* @api public
|
|
4016
|
+
*/
|
|
4017
|
+
|
|
4018
|
+
Emitter.prototype.on =
|
|
4019
|
+
Emitter.prototype.addEventListener = function(event, fn){
|
|
4020
|
+
this._callbacks = this._callbacks || {};
|
|
4021
|
+
(this._callbacks['$' + event] = this._callbacks['$' + event] || [])
|
|
4022
|
+
.push(fn);
|
|
4023
|
+
return this;
|
|
4024
|
+
};
|
|
4025
|
+
|
|
4026
|
+
/**
|
|
4027
|
+
* Adds an `event` listener that will be invoked a single
|
|
4028
|
+
* time then automatically removed.
|
|
4029
|
+
*
|
|
4030
|
+
* @param {String} event
|
|
4031
|
+
* @param {Function} fn
|
|
4032
|
+
* @return {Emitter}
|
|
4033
|
+
* @api public
|
|
4034
|
+
*/
|
|
4035
|
+
|
|
4036
|
+
Emitter.prototype.once = function(event, fn){
|
|
4037
|
+
function on() {
|
|
4038
|
+
this.off(event, on);
|
|
4039
|
+
fn.apply(this, arguments);
|
|
4040
|
+
}
|
|
4041
|
+
|
|
4042
|
+
on.fn = fn;
|
|
4043
|
+
this.on(event, on);
|
|
4044
|
+
return this;
|
|
4045
|
+
};
|
|
4046
|
+
|
|
4047
|
+
/**
|
|
4048
|
+
* Remove the given callback for `event` or all
|
|
4049
|
+
* registered callbacks.
|
|
4050
|
+
*
|
|
4051
|
+
* @param {String} event
|
|
4052
|
+
* @param {Function} fn
|
|
4053
|
+
* @return {Emitter}
|
|
4054
|
+
* @api public
|
|
4055
|
+
*/
|
|
4056
|
+
|
|
4057
|
+
Emitter.prototype.off =
|
|
4058
|
+
Emitter.prototype.removeListener =
|
|
4059
|
+
Emitter.prototype.removeAllListeners =
|
|
4060
|
+
Emitter.prototype.removeEventListener = function(event, fn){
|
|
4061
|
+
this._callbacks = this._callbacks || {};
|
|
4062
|
+
|
|
4063
|
+
// all
|
|
4064
|
+
if (0 == arguments.length) {
|
|
4065
|
+
this._callbacks = {};
|
|
4066
|
+
return this;
|
|
4067
|
+
}
|
|
4068
|
+
|
|
4069
|
+
// specific event
|
|
4070
|
+
var callbacks = this._callbacks['$' + event];
|
|
4071
|
+
if (!callbacks) return this;
|
|
4072
|
+
|
|
4073
|
+
// remove all handlers
|
|
4074
|
+
if (1 == arguments.length) {
|
|
4075
|
+
delete this._callbacks['$' + event];
|
|
4076
|
+
return this;
|
|
4077
|
+
}
|
|
4078
|
+
|
|
4079
|
+
// remove specific handler
|
|
4080
|
+
var cb;
|
|
4081
|
+
for (var i = 0; i < callbacks.length; i++) {
|
|
4082
|
+
cb = callbacks[i];
|
|
4083
|
+
if (cb === fn || cb.fn === fn) {
|
|
4084
|
+
callbacks.splice(i, 1);
|
|
4085
|
+
break;
|
|
4086
|
+
}
|
|
4087
|
+
}
|
|
4088
|
+
|
|
4089
|
+
// Remove event specific arrays for event types that no
|
|
4090
|
+
// one is subscribed for to avoid memory leak.
|
|
4091
|
+
if (callbacks.length === 0) {
|
|
4092
|
+
delete this._callbacks['$' + event];
|
|
4093
|
+
}
|
|
4094
|
+
|
|
4095
|
+
return this;
|
|
4096
|
+
};
|
|
4097
|
+
|
|
4098
|
+
/**
|
|
4099
|
+
* Emit `event` with the given args.
|
|
4100
|
+
*
|
|
4101
|
+
* @param {String} event
|
|
4102
|
+
* @param {Mixed} ...
|
|
4103
|
+
* @return {Emitter}
|
|
4104
|
+
*/
|
|
4105
|
+
|
|
4106
|
+
Emitter.prototype.emit = function(event){
|
|
4107
|
+
this._callbacks = this._callbacks || {};
|
|
4108
|
+
|
|
4109
|
+
var args = new Array(arguments.length - 1)
|
|
4110
|
+
, callbacks = this._callbacks['$' + event];
|
|
4111
|
+
|
|
4112
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
4113
|
+
args[i - 1] = arguments[i];
|
|
4114
|
+
}
|
|
4115
|
+
|
|
4116
|
+
if (callbacks) {
|
|
4117
|
+
callbacks = callbacks.slice(0);
|
|
4118
|
+
for (var i = 0, len = callbacks.length; i < len; ++i) {
|
|
4119
|
+
callbacks[i].apply(this, args);
|
|
4120
|
+
}
|
|
4121
|
+
}
|
|
4122
|
+
|
|
4123
|
+
return this;
|
|
4124
|
+
};
|
|
4125
|
+
|
|
4126
|
+
// alias used for reserved events (protected method)
|
|
4127
|
+
Emitter.prototype.emitReserved = Emitter.prototype.emit;
|
|
4128
|
+
|
|
4129
|
+
/**
|
|
4130
|
+
* Return array of callbacks for `event`.
|
|
4131
|
+
*
|
|
4132
|
+
* @param {String} event
|
|
4133
|
+
* @return {Array}
|
|
4134
|
+
* @api public
|
|
4135
|
+
*/
|
|
4136
|
+
|
|
4137
|
+
Emitter.prototype.listeners = function(event){
|
|
4138
|
+
this._callbacks = this._callbacks || {};
|
|
4139
|
+
return this._callbacks['$' + event] || [];
|
|
4140
|
+
};
|
|
4141
|
+
|
|
4142
|
+
/**
|
|
4143
|
+
* Check if this emitter has `event` handlers.
|
|
4144
|
+
*
|
|
4145
|
+
* @param {String} event
|
|
4146
|
+
* @return {Boolean}
|
|
4147
|
+
* @api public
|
|
4148
|
+
*/
|
|
4149
|
+
|
|
4150
|
+
Emitter.prototype.hasListeners = function(event){
|
|
4151
|
+
return !! this.listeners(event).length;
|
|
4152
|
+
};
|
|
4153
|
+
|
|
4154
|
+
const withNativeArrayBuffer = typeof ArrayBuffer === "function";
|
|
4155
|
+
const isView = (obj) => {
|
|
4156
|
+
return typeof ArrayBuffer.isView === "function"
|
|
4157
|
+
? ArrayBuffer.isView(obj)
|
|
4158
|
+
: obj.buffer instanceof ArrayBuffer;
|
|
4159
|
+
};
|
|
4160
|
+
const toString = Object.prototype.toString;
|
|
4161
|
+
const withNativeBlob = typeof Blob === "function" ||
|
|
4162
|
+
(typeof Blob !== "undefined" &&
|
|
4163
|
+
toString.call(Blob) === "[object BlobConstructor]");
|
|
4164
|
+
const withNativeFile = typeof File === "function" ||
|
|
4165
|
+
(typeof File !== "undefined" &&
|
|
4166
|
+
toString.call(File) === "[object FileConstructor]");
|
|
4167
|
+
/**
|
|
4168
|
+
* Returns true if obj is a Buffer, an ArrayBuffer, a Blob or a File.
|
|
4169
|
+
*
|
|
4170
|
+
* @private
|
|
4171
|
+
*/
|
|
4172
|
+
function isBinary(obj) {
|
|
4173
|
+
return ((withNativeArrayBuffer && (obj instanceof ArrayBuffer || isView(obj))) ||
|
|
4174
|
+
(withNativeBlob && obj instanceof Blob) ||
|
|
4175
|
+
(withNativeFile && obj instanceof File));
|
|
4176
|
+
}
|
|
4177
|
+
function hasBinary(obj, toJSON) {
|
|
4178
|
+
if (!obj || typeof obj !== "object") {
|
|
4179
|
+
return false;
|
|
4180
|
+
}
|
|
4181
|
+
if (Array.isArray(obj)) {
|
|
4182
|
+
for (let i = 0, l = obj.length; i < l; i++) {
|
|
4183
|
+
if (hasBinary(obj[i])) {
|
|
4184
|
+
return true;
|
|
4185
|
+
}
|
|
4186
|
+
}
|
|
4187
|
+
return false;
|
|
4188
|
+
}
|
|
4189
|
+
if (isBinary(obj)) {
|
|
4190
|
+
return true;
|
|
4191
|
+
}
|
|
4192
|
+
if (obj.toJSON &&
|
|
4193
|
+
typeof obj.toJSON === "function" &&
|
|
4194
|
+
arguments.length === 1) {
|
|
4195
|
+
return hasBinary(obj.toJSON(), true);
|
|
4196
|
+
}
|
|
4197
|
+
for (const key in obj) {
|
|
4198
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
|
|
4199
|
+
return true;
|
|
4200
|
+
}
|
|
4201
|
+
}
|
|
4202
|
+
return false;
|
|
4203
|
+
}
|
|
4204
|
+
|
|
4205
|
+
/**
|
|
4206
|
+
* Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
|
|
4207
|
+
*
|
|
4208
|
+
* @param {Object} packet - socket.io event packet
|
|
4209
|
+
* @return {Object} with deconstructed packet and list of buffers
|
|
4210
|
+
* @public
|
|
4211
|
+
*/
|
|
4212
|
+
function deconstructPacket(packet) {
|
|
4213
|
+
const buffers = [];
|
|
4214
|
+
const packetData = packet.data;
|
|
4215
|
+
const pack = packet;
|
|
4216
|
+
pack.data = _deconstructPacket(packetData, buffers);
|
|
4217
|
+
pack.attachments = buffers.length; // number of binary 'attachments'
|
|
4218
|
+
return { packet: pack, buffers: buffers };
|
|
4219
|
+
}
|
|
4220
|
+
function _deconstructPacket(data, buffers) {
|
|
4221
|
+
if (!data)
|
|
4222
|
+
return data;
|
|
4223
|
+
if (isBinary(data)) {
|
|
4224
|
+
const placeholder = { _placeholder: true, num: buffers.length };
|
|
4225
|
+
buffers.push(data);
|
|
4226
|
+
return placeholder;
|
|
4227
|
+
}
|
|
4228
|
+
else if (Array.isArray(data)) {
|
|
4229
|
+
const newData = new Array(data.length);
|
|
4230
|
+
for (let i = 0; i < data.length; i++) {
|
|
4231
|
+
newData[i] = _deconstructPacket(data[i], buffers);
|
|
4232
|
+
}
|
|
4233
|
+
return newData;
|
|
4234
|
+
}
|
|
4235
|
+
else if (typeof data === "object" && !(data instanceof Date)) {
|
|
4236
|
+
const newData = {};
|
|
4237
|
+
for (const key in data) {
|
|
4238
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
4239
|
+
newData[key] = _deconstructPacket(data[key], buffers);
|
|
4240
|
+
}
|
|
4241
|
+
}
|
|
4242
|
+
return newData;
|
|
4243
|
+
}
|
|
4244
|
+
return data;
|
|
4245
|
+
}
|
|
4246
|
+
/**
|
|
4247
|
+
* Reconstructs a binary packet from its placeholder packet and buffers
|
|
4248
|
+
*
|
|
4249
|
+
* @param {Object} packet - event packet with placeholders
|
|
4250
|
+
* @param {Array} buffers - binary buffers to put in placeholder positions
|
|
4251
|
+
* @return {Object} reconstructed packet
|
|
4252
|
+
* @public
|
|
4253
|
+
*/
|
|
4254
|
+
function reconstructPacket(packet, buffers) {
|
|
4255
|
+
packet.data = _reconstructPacket(packet.data, buffers);
|
|
4256
|
+
delete packet.attachments; // no longer useful
|
|
4257
|
+
return packet;
|
|
4258
|
+
}
|
|
4259
|
+
function _reconstructPacket(data, buffers) {
|
|
4260
|
+
if (!data)
|
|
4261
|
+
return data;
|
|
4262
|
+
if (data && data._placeholder === true) {
|
|
4263
|
+
const isIndexValid = typeof data.num === "number" &&
|
|
4264
|
+
data.num >= 0 &&
|
|
4265
|
+
data.num < buffers.length;
|
|
4266
|
+
if (isIndexValid) {
|
|
4267
|
+
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
|
|
4268
|
+
}
|
|
4269
|
+
else {
|
|
4270
|
+
throw new Error("illegal attachments");
|
|
4271
|
+
}
|
|
4272
|
+
}
|
|
4273
|
+
else if (Array.isArray(data)) {
|
|
4274
|
+
for (let i = 0; i < data.length; i++) {
|
|
4275
|
+
data[i] = _reconstructPacket(data[i], buffers);
|
|
4276
|
+
}
|
|
4277
|
+
}
|
|
4278
|
+
else if (typeof data === "object") {
|
|
4279
|
+
for (const key in data) {
|
|
4280
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
4281
|
+
data[key] = _reconstructPacket(data[key], buffers);
|
|
4282
|
+
}
|
|
4283
|
+
}
|
|
4284
|
+
}
|
|
4285
|
+
return data;
|
|
4286
|
+
}
|
|
4287
|
+
|
|
4288
|
+
/**
|
|
4289
|
+
* These strings must not be used as event names, as they have a special meaning.
|
|
4290
|
+
*/
|
|
4291
|
+
const RESERVED_EVENTS$1 = [
|
|
4292
|
+
"connect",
|
|
4293
|
+
"connect_error",
|
|
4294
|
+
"disconnect",
|
|
4295
|
+
"disconnecting",
|
|
4296
|
+
"newListener",
|
|
4297
|
+
"removeListener", // used by the Node.js EventEmitter
|
|
4298
|
+
];
|
|
4299
|
+
/**
|
|
4300
|
+
* Protocol version.
|
|
4301
|
+
*
|
|
4302
|
+
* @public
|
|
4303
|
+
*/
|
|
4304
|
+
const protocol = 5;
|
|
4305
|
+
var PacketType;
|
|
4306
|
+
(function (PacketType) {
|
|
4307
|
+
PacketType[PacketType["CONNECT"] = 0] = "CONNECT";
|
|
4308
|
+
PacketType[PacketType["DISCONNECT"] = 1] = "DISCONNECT";
|
|
4309
|
+
PacketType[PacketType["EVENT"] = 2] = "EVENT";
|
|
4310
|
+
PacketType[PacketType["ACK"] = 3] = "ACK";
|
|
4311
|
+
PacketType[PacketType["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
|
|
4312
|
+
PacketType[PacketType["BINARY_EVENT"] = 5] = "BINARY_EVENT";
|
|
4313
|
+
PacketType[PacketType["BINARY_ACK"] = 6] = "BINARY_ACK";
|
|
4314
|
+
})(PacketType || (PacketType = {}));
|
|
4315
|
+
/**
|
|
4316
|
+
* A socket.io Encoder instance
|
|
4317
|
+
*/
|
|
4318
|
+
class Encoder {
|
|
4319
|
+
/**
|
|
4320
|
+
* Encoder constructor
|
|
4321
|
+
*
|
|
4322
|
+
* @param {function} replacer - custom replacer to pass down to JSON.parse
|
|
4323
|
+
*/
|
|
4324
|
+
constructor(replacer) {
|
|
4325
|
+
this.replacer = replacer;
|
|
4326
|
+
}
|
|
4327
|
+
/**
|
|
4328
|
+
* Encode a packet as a single string if non-binary, or as a
|
|
4329
|
+
* buffer sequence, depending on packet type.
|
|
4330
|
+
*
|
|
4331
|
+
* @param {Object} obj - packet object
|
|
4332
|
+
*/
|
|
4333
|
+
encode(obj) {
|
|
4334
|
+
if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) {
|
|
4335
|
+
if (hasBinary(obj)) {
|
|
4336
|
+
return this.encodeAsBinary({
|
|
4337
|
+
type: obj.type === PacketType.EVENT
|
|
4338
|
+
? PacketType.BINARY_EVENT
|
|
4339
|
+
: PacketType.BINARY_ACK,
|
|
4340
|
+
nsp: obj.nsp,
|
|
4341
|
+
data: obj.data,
|
|
4342
|
+
id: obj.id,
|
|
4343
|
+
});
|
|
4344
|
+
}
|
|
4345
|
+
}
|
|
4346
|
+
return [this.encodeAsString(obj)];
|
|
4347
|
+
}
|
|
4348
|
+
/**
|
|
4349
|
+
* Encode packet as string.
|
|
4350
|
+
*/
|
|
4351
|
+
encodeAsString(obj) {
|
|
4352
|
+
// first is type
|
|
4353
|
+
let str = "" + obj.type;
|
|
4354
|
+
// attachments if we have them
|
|
4355
|
+
if (obj.type === PacketType.BINARY_EVENT ||
|
|
4356
|
+
obj.type === PacketType.BINARY_ACK) {
|
|
4357
|
+
str += obj.attachments + "-";
|
|
4358
|
+
}
|
|
4359
|
+
// if we have a namespace other than `/`
|
|
4360
|
+
// we append it followed by a comma `,`
|
|
4361
|
+
if (obj.nsp && "/" !== obj.nsp) {
|
|
4362
|
+
str += obj.nsp + ",";
|
|
4363
|
+
}
|
|
4364
|
+
// immediately followed by the id
|
|
4365
|
+
if (null != obj.id) {
|
|
4366
|
+
str += obj.id;
|
|
4367
|
+
}
|
|
4368
|
+
// json data
|
|
4369
|
+
if (null != obj.data) {
|
|
4370
|
+
str += JSON.stringify(obj.data, this.replacer);
|
|
4371
|
+
}
|
|
4372
|
+
return str;
|
|
4373
|
+
}
|
|
4374
|
+
/**
|
|
4375
|
+
* Encode packet as 'buffer sequence' by removing blobs, and
|
|
4376
|
+
* deconstructing packet into object with placeholders and
|
|
4377
|
+
* a list of buffers.
|
|
4378
|
+
*/
|
|
4379
|
+
encodeAsBinary(obj) {
|
|
4380
|
+
const deconstruction = deconstructPacket(obj);
|
|
4381
|
+
const pack = this.encodeAsString(deconstruction.packet);
|
|
4382
|
+
const buffers = deconstruction.buffers;
|
|
4383
|
+
buffers.unshift(pack); // add packet info to beginning of data list
|
|
4384
|
+
return buffers; // write all the buffers
|
|
4385
|
+
}
|
|
4386
|
+
}
|
|
4387
|
+
// see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
|
|
4388
|
+
function isObject(value) {
|
|
4389
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
4390
|
+
}
|
|
4391
|
+
/**
|
|
4392
|
+
* A socket.io Decoder instance
|
|
4393
|
+
*
|
|
4394
|
+
* @return {Object} decoder
|
|
4395
|
+
*/
|
|
4396
|
+
class Decoder extends Emitter {
|
|
4397
|
+
/**
|
|
4398
|
+
* Decoder constructor
|
|
4399
|
+
*
|
|
4400
|
+
* @param {function} reviver - custom reviver to pass down to JSON.stringify
|
|
4401
|
+
*/
|
|
4402
|
+
constructor(reviver) {
|
|
4403
|
+
super();
|
|
4404
|
+
this.reviver = reviver;
|
|
4405
|
+
}
|
|
4406
|
+
/**
|
|
4407
|
+
* Decodes an encoded packet string into packet JSON.
|
|
4408
|
+
*
|
|
4409
|
+
* @param {String} obj - encoded packet
|
|
4410
|
+
*/
|
|
4411
|
+
add(obj) {
|
|
4412
|
+
let packet;
|
|
4413
|
+
if (typeof obj === "string") {
|
|
4414
|
+
if (this.reconstructor) {
|
|
4415
|
+
throw new Error("got plaintext data when reconstructing a packet");
|
|
4416
|
+
}
|
|
4417
|
+
packet = this.decodeString(obj);
|
|
4418
|
+
const isBinaryEvent = packet.type === PacketType.BINARY_EVENT;
|
|
4419
|
+
if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) {
|
|
4420
|
+
packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK;
|
|
4421
|
+
// binary packet's json
|
|
4422
|
+
this.reconstructor = new BinaryReconstructor(packet);
|
|
4423
|
+
// no attachments, labeled binary but no binary data to follow
|
|
4424
|
+
if (packet.attachments === 0) {
|
|
4425
|
+
super.emitReserved("decoded", packet);
|
|
4426
|
+
}
|
|
4427
|
+
}
|
|
4428
|
+
else {
|
|
4429
|
+
// non-binary full packet
|
|
4430
|
+
super.emitReserved("decoded", packet);
|
|
4431
|
+
}
|
|
4432
|
+
}
|
|
4433
|
+
else if (isBinary(obj) || obj.base64) {
|
|
4434
|
+
// raw binary data
|
|
4435
|
+
if (!this.reconstructor) {
|
|
4436
|
+
throw new Error("got binary data when not reconstructing a packet");
|
|
4437
|
+
}
|
|
4438
|
+
else {
|
|
4439
|
+
packet = this.reconstructor.takeBinaryData(obj);
|
|
4440
|
+
if (packet) {
|
|
4441
|
+
// received final buffer
|
|
4442
|
+
this.reconstructor = null;
|
|
4443
|
+
super.emitReserved("decoded", packet);
|
|
4444
|
+
}
|
|
4445
|
+
}
|
|
4446
|
+
}
|
|
4447
|
+
else {
|
|
4448
|
+
throw new Error("Unknown type: " + obj);
|
|
4449
|
+
}
|
|
4450
|
+
}
|
|
4451
|
+
/**
|
|
4452
|
+
* Decode a packet String (JSON data)
|
|
4453
|
+
*
|
|
4454
|
+
* @param {String} str
|
|
4455
|
+
* @return {Object} packet
|
|
4456
|
+
*/
|
|
4457
|
+
decodeString(str) {
|
|
4458
|
+
let i = 0;
|
|
4459
|
+
// look up type
|
|
4460
|
+
const p = {
|
|
4461
|
+
type: Number(str.charAt(0)),
|
|
4462
|
+
};
|
|
4463
|
+
if (PacketType[p.type] === undefined) {
|
|
4464
|
+
throw new Error("unknown packet type " + p.type);
|
|
4465
|
+
}
|
|
4466
|
+
// look up attachments if type binary
|
|
4467
|
+
if (p.type === PacketType.BINARY_EVENT ||
|
|
4468
|
+
p.type === PacketType.BINARY_ACK) {
|
|
4469
|
+
const start = i + 1;
|
|
4470
|
+
while (str.charAt(++i) !== "-" && i != str.length) { }
|
|
4471
|
+
const buf = str.substring(start, i);
|
|
4472
|
+
if (buf != Number(buf) || str.charAt(i) !== "-") {
|
|
4473
|
+
throw new Error("Illegal attachments");
|
|
4474
|
+
}
|
|
4475
|
+
p.attachments = Number(buf);
|
|
4476
|
+
}
|
|
4477
|
+
// look up namespace (if any)
|
|
4478
|
+
if ("/" === str.charAt(i + 1)) {
|
|
4479
|
+
const start = i + 1;
|
|
4480
|
+
while (++i) {
|
|
4481
|
+
const c = str.charAt(i);
|
|
4482
|
+
if ("," === c)
|
|
4483
|
+
break;
|
|
4484
|
+
if (i === str.length)
|
|
4485
|
+
break;
|
|
4486
|
+
}
|
|
4487
|
+
p.nsp = str.substring(start, i);
|
|
4488
|
+
}
|
|
4489
|
+
else {
|
|
4490
|
+
p.nsp = "/";
|
|
4491
|
+
}
|
|
4492
|
+
// look up id
|
|
4493
|
+
const next = str.charAt(i + 1);
|
|
4494
|
+
if ("" !== next && Number(next) == next) {
|
|
4495
|
+
const start = i + 1;
|
|
4496
|
+
while (++i) {
|
|
4497
|
+
const c = str.charAt(i);
|
|
4498
|
+
if (null == c || Number(c) != c) {
|
|
4499
|
+
--i;
|
|
4500
|
+
break;
|
|
4501
|
+
}
|
|
4502
|
+
if (i === str.length)
|
|
4503
|
+
break;
|
|
4504
|
+
}
|
|
4505
|
+
p.id = Number(str.substring(start, i + 1));
|
|
4506
|
+
}
|
|
4507
|
+
// look up json data
|
|
4508
|
+
if (str.charAt(++i)) {
|
|
4509
|
+
const payload = this.tryParse(str.substr(i));
|
|
4510
|
+
if (Decoder.isPayloadValid(p.type, payload)) {
|
|
4511
|
+
p.data = payload;
|
|
4512
|
+
}
|
|
4513
|
+
else {
|
|
4514
|
+
throw new Error("invalid payload");
|
|
4515
|
+
}
|
|
4516
|
+
}
|
|
4517
|
+
return p;
|
|
4518
|
+
}
|
|
4519
|
+
tryParse(str) {
|
|
4520
|
+
try {
|
|
4521
|
+
return JSON.parse(str, this.reviver);
|
|
4522
|
+
}
|
|
4523
|
+
catch (e) {
|
|
4524
|
+
return false;
|
|
4525
|
+
}
|
|
4526
|
+
}
|
|
4527
|
+
static isPayloadValid(type, payload) {
|
|
4528
|
+
switch (type) {
|
|
4529
|
+
case PacketType.CONNECT:
|
|
4530
|
+
return isObject(payload);
|
|
4531
|
+
case PacketType.DISCONNECT:
|
|
4532
|
+
return payload === undefined;
|
|
4533
|
+
case PacketType.CONNECT_ERROR:
|
|
4534
|
+
return typeof payload === "string" || isObject(payload);
|
|
4535
|
+
case PacketType.EVENT:
|
|
4536
|
+
case PacketType.BINARY_EVENT:
|
|
4537
|
+
return (Array.isArray(payload) &&
|
|
4538
|
+
(typeof payload[0] === "number" ||
|
|
4539
|
+
(typeof payload[0] === "string" &&
|
|
4540
|
+
RESERVED_EVENTS$1.indexOf(payload[0]) === -1)));
|
|
4541
|
+
case PacketType.ACK:
|
|
4542
|
+
case PacketType.BINARY_ACK:
|
|
4543
|
+
return Array.isArray(payload);
|
|
4544
|
+
}
|
|
4545
|
+
}
|
|
4546
|
+
/**
|
|
4547
|
+
* Deallocates a parser's resources
|
|
4548
|
+
*/
|
|
4549
|
+
destroy() {
|
|
4550
|
+
if (this.reconstructor) {
|
|
4551
|
+
this.reconstructor.finishedReconstruction();
|
|
4552
|
+
this.reconstructor = null;
|
|
4553
|
+
}
|
|
4554
|
+
}
|
|
4555
|
+
}
|
|
4556
|
+
/**
|
|
4557
|
+
* A manager of a binary event's 'buffer sequence'. Should
|
|
4558
|
+
* be constructed whenever a packet of type BINARY_EVENT is
|
|
4559
|
+
* decoded.
|
|
4560
|
+
*
|
|
4561
|
+
* @param {Object} packet
|
|
4562
|
+
* @return {BinaryReconstructor} initialized reconstructor
|
|
4563
|
+
*/
|
|
4564
|
+
class BinaryReconstructor {
|
|
4565
|
+
constructor(packet) {
|
|
4566
|
+
this.packet = packet;
|
|
4567
|
+
this.buffers = [];
|
|
4568
|
+
this.reconPack = packet;
|
|
4569
|
+
}
|
|
4570
|
+
/**
|
|
4571
|
+
* Method to be called when binary data received from connection
|
|
4572
|
+
* after a BINARY_EVENT packet.
|
|
4573
|
+
*
|
|
4574
|
+
* @param {Buffer | ArrayBuffer} binData - the raw binary data received
|
|
4575
|
+
* @return {null | Object} returns null if more binary data is expected or
|
|
4576
|
+
* a reconstructed packet object if all buffers have been received.
|
|
4577
|
+
*/
|
|
4578
|
+
takeBinaryData(binData) {
|
|
4579
|
+
this.buffers.push(binData);
|
|
4580
|
+
if (this.buffers.length === this.reconPack.attachments) {
|
|
4581
|
+
// done with buffer list
|
|
4582
|
+
const packet = reconstructPacket(this.reconPack, this.buffers);
|
|
4583
|
+
this.finishedReconstruction();
|
|
4584
|
+
return packet;
|
|
4585
|
+
}
|
|
4586
|
+
return null;
|
|
4587
|
+
}
|
|
4588
|
+
/**
|
|
4589
|
+
* Cleans up binary packet reconstruction variables.
|
|
4590
|
+
*/
|
|
4591
|
+
finishedReconstruction() {
|
|
4592
|
+
this.reconPack = null;
|
|
4593
|
+
this.buffers = [];
|
|
4594
|
+
}
|
|
4595
|
+
}
|
|
4596
|
+
|
|
4597
|
+
var parser = /*#__PURE__*/Object.freeze({
|
|
4598
|
+
__proto__: null,
|
|
4599
|
+
protocol: protocol,
|
|
4600
|
+
get PacketType () { return PacketType; },
|
|
4601
|
+
Encoder: Encoder,
|
|
4602
|
+
Decoder: Decoder
|
|
4603
|
+
});
|
|
4604
|
+
|
|
4605
|
+
function on(obj, ev, fn) {
|
|
4606
|
+
obj.on(ev, fn);
|
|
4607
|
+
return function subDestroy() {
|
|
4608
|
+
obj.off(ev, fn);
|
|
4609
|
+
};
|
|
4610
|
+
}
|
|
4611
|
+
|
|
4612
|
+
/**
|
|
4613
|
+
* Internal events.
|
|
4614
|
+
* These events can't be emitted by the user.
|
|
4615
|
+
*/
|
|
4616
|
+
const RESERVED_EVENTS = Object.freeze({
|
|
4617
|
+
connect: 1,
|
|
4618
|
+
connect_error: 1,
|
|
4619
|
+
disconnect: 1,
|
|
4620
|
+
disconnecting: 1,
|
|
4621
|
+
// EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener
|
|
4622
|
+
newListener: 1,
|
|
4623
|
+
removeListener: 1,
|
|
4624
|
+
});
|
|
4625
|
+
/**
|
|
4626
|
+
* A Socket is the fundamental class for interacting with the server.
|
|
4627
|
+
*
|
|
4628
|
+
* A Socket belongs to a certain Namespace (by default /) and uses an underlying {@link Manager} to communicate.
|
|
4629
|
+
*
|
|
4630
|
+
* @example
|
|
4631
|
+
* const socket = io();
|
|
4632
|
+
*
|
|
4633
|
+
* socket.on("connect", () => {
|
|
4634
|
+
* console.log("connected");
|
|
4635
|
+
* });
|
|
4636
|
+
*
|
|
4637
|
+
* // send an event to the server
|
|
4638
|
+
* socket.emit("foo", "bar");
|
|
4639
|
+
*
|
|
4640
|
+
* socket.on("foobar", () => {
|
|
4641
|
+
* // an event was received from the server
|
|
4642
|
+
* });
|
|
4643
|
+
*
|
|
4644
|
+
* // upon disconnection
|
|
4645
|
+
* socket.on("disconnect", (reason) => {
|
|
4646
|
+
* console.log(`disconnected due to ${reason}`);
|
|
4647
|
+
* });
|
|
4648
|
+
*/
|
|
4649
|
+
class Socket extends Emitter {
|
|
4650
|
+
/**
|
|
4651
|
+
* `Socket` constructor.
|
|
4652
|
+
*/
|
|
4653
|
+
constructor(io, nsp, opts) {
|
|
4654
|
+
super();
|
|
4655
|
+
/**
|
|
4656
|
+
* Whether the socket is currently connected to the server.
|
|
4657
|
+
*
|
|
4658
|
+
* @example
|
|
4659
|
+
* const socket = io();
|
|
4660
|
+
*
|
|
4661
|
+
* socket.on("connect", () => {
|
|
4662
|
+
* console.log(socket.connected); // true
|
|
4663
|
+
* });
|
|
4664
|
+
*
|
|
4665
|
+
* socket.on("disconnect", () => {
|
|
4666
|
+
* console.log(socket.connected); // false
|
|
4667
|
+
* });
|
|
4668
|
+
*/
|
|
4669
|
+
this.connected = false;
|
|
4670
|
+
/**
|
|
4671
|
+
* Whether the connection state was recovered after a temporary disconnection. In that case, any missed packets will
|
|
4672
|
+
* be transmitted by the server.
|
|
4673
|
+
*/
|
|
4674
|
+
this.recovered = false;
|
|
4675
|
+
/**
|
|
4676
|
+
* Buffer for packets received before the CONNECT packet
|
|
4677
|
+
*/
|
|
4678
|
+
this.receiveBuffer = [];
|
|
4679
|
+
/**
|
|
4680
|
+
* Buffer for packets that will be sent once the socket is connected
|
|
4681
|
+
*/
|
|
4682
|
+
this.sendBuffer = [];
|
|
4683
|
+
/**
|
|
4684
|
+
* The queue of packets to be sent with retry in case of failure.
|
|
4685
|
+
*
|
|
4686
|
+
* Packets are sent one by one, each waiting for the server acknowledgement, in order to guarantee the delivery order.
|
|
4687
|
+
* @private
|
|
4688
|
+
*/
|
|
4689
|
+
this._queue = [];
|
|
4690
|
+
/**
|
|
4691
|
+
* A sequence to generate the ID of the {@link QueuedPacket}.
|
|
4692
|
+
* @private
|
|
4693
|
+
*/
|
|
4694
|
+
this._queueSeq = 0;
|
|
4695
|
+
this.ids = 0;
|
|
4696
|
+
/**
|
|
4697
|
+
* A map containing acknowledgement handlers.
|
|
4698
|
+
*
|
|
4699
|
+
* The `withError` attribute is used to differentiate handlers that accept an error as first argument:
|
|
4700
|
+
*
|
|
4701
|
+
* - `socket.emit("test", (err, value) => { ... })` with `ackTimeout` option
|
|
4702
|
+
* - `socket.timeout(5000).emit("test", (err, value) => { ... })`
|
|
4703
|
+
* - `const value = await socket.emitWithAck("test")`
|
|
4704
|
+
*
|
|
4705
|
+
* From those that don't:
|
|
4706
|
+
*
|
|
4707
|
+
* - `socket.emit("test", (value) => { ... });`
|
|
4708
|
+
*
|
|
4709
|
+
* In the first case, the handlers will be called with an error when:
|
|
4710
|
+
*
|
|
4711
|
+
* - the timeout is reached
|
|
4712
|
+
* - the socket gets disconnected
|
|
4713
|
+
*
|
|
4714
|
+
* In the second case, the handlers will be simply discarded upon disconnection, since the client will never receive
|
|
4715
|
+
* an acknowledgement from the server.
|
|
4716
|
+
*
|
|
4717
|
+
* @private
|
|
4718
|
+
*/
|
|
4719
|
+
this.acks = {};
|
|
4720
|
+
this.flags = {};
|
|
4721
|
+
this.io = io;
|
|
4722
|
+
this.nsp = nsp;
|
|
4723
|
+
if (opts && opts.auth) {
|
|
4724
|
+
this.auth = opts.auth;
|
|
4725
|
+
}
|
|
4726
|
+
this._opts = Object.assign({}, opts);
|
|
4727
|
+
if (this.io._autoConnect)
|
|
4728
|
+
this.open();
|
|
4729
|
+
}
|
|
4730
|
+
/**
|
|
4731
|
+
* Whether the socket is currently disconnected
|
|
4732
|
+
*
|
|
4733
|
+
* @example
|
|
4734
|
+
* const socket = io();
|
|
4735
|
+
*
|
|
4736
|
+
* socket.on("connect", () => {
|
|
4737
|
+
* console.log(socket.disconnected); // false
|
|
4738
|
+
* });
|
|
4739
|
+
*
|
|
4740
|
+
* socket.on("disconnect", () => {
|
|
4741
|
+
* console.log(socket.disconnected); // true
|
|
4742
|
+
* });
|
|
4743
|
+
*/
|
|
4744
|
+
get disconnected() {
|
|
4745
|
+
return !this.connected;
|
|
4746
|
+
}
|
|
4747
|
+
/**
|
|
4748
|
+
* Subscribe to open, close and packet events
|
|
4749
|
+
*
|
|
4750
|
+
* @private
|
|
4751
|
+
*/
|
|
4752
|
+
subEvents() {
|
|
4753
|
+
if (this.subs)
|
|
4754
|
+
return;
|
|
4755
|
+
const io = this.io;
|
|
4756
|
+
this.subs = [
|
|
4757
|
+
on(io, "open", this.onopen.bind(this)),
|
|
4758
|
+
on(io, "packet", this.onpacket.bind(this)),
|
|
4759
|
+
on(io, "error", this.onerror.bind(this)),
|
|
4760
|
+
on(io, "close", this.onclose.bind(this)),
|
|
4761
|
+
];
|
|
4762
|
+
}
|
|
4763
|
+
/**
|
|
4764
|
+
* Whether the Socket will try to reconnect when its Manager connects or reconnects.
|
|
4765
|
+
*
|
|
4766
|
+
* @example
|
|
4767
|
+
* const socket = io();
|
|
4768
|
+
*
|
|
4769
|
+
* console.log(socket.active); // true
|
|
4770
|
+
*
|
|
4771
|
+
* socket.on("disconnect", (reason) => {
|
|
4772
|
+
* if (reason === "io server disconnect") {
|
|
4773
|
+
* // the disconnection was initiated by the server, you need to manually reconnect
|
|
4774
|
+
* console.log(socket.active); // false
|
|
4775
|
+
* }
|
|
4776
|
+
* // else the socket will automatically try to reconnect
|
|
4777
|
+
* console.log(socket.active); // true
|
|
4778
|
+
* });
|
|
4779
|
+
*/
|
|
4780
|
+
get active() {
|
|
4781
|
+
return !!this.subs;
|
|
4782
|
+
}
|
|
4783
|
+
/**
|
|
4784
|
+
* "Opens" the socket.
|
|
4785
|
+
*
|
|
4786
|
+
* @example
|
|
4787
|
+
* const socket = io({
|
|
4788
|
+
* autoConnect: false
|
|
4789
|
+
* });
|
|
4790
|
+
*
|
|
4791
|
+
* socket.connect();
|
|
4792
|
+
*/
|
|
4793
|
+
connect() {
|
|
4794
|
+
if (this.connected)
|
|
4795
|
+
return this;
|
|
4796
|
+
this.subEvents();
|
|
4797
|
+
if (!this.io["_reconnecting"])
|
|
4798
|
+
this.io.open(); // ensure open
|
|
4799
|
+
if ("open" === this.io._readyState)
|
|
4800
|
+
this.onopen();
|
|
4801
|
+
return this;
|
|
4802
|
+
}
|
|
4803
|
+
/**
|
|
4804
|
+
* Alias for {@link connect()}.
|
|
4805
|
+
*/
|
|
4806
|
+
open() {
|
|
4807
|
+
return this.connect();
|
|
4808
|
+
}
|
|
4809
|
+
/**
|
|
4810
|
+
* Sends a `message` event.
|
|
4811
|
+
*
|
|
4812
|
+
* This method mimics the WebSocket.send() method.
|
|
4813
|
+
*
|
|
4814
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
4815
|
+
*
|
|
4816
|
+
* @example
|
|
4817
|
+
* socket.send("hello");
|
|
4818
|
+
*
|
|
4819
|
+
* // this is equivalent to
|
|
4820
|
+
* socket.emit("message", "hello");
|
|
4821
|
+
*
|
|
4822
|
+
* @return self
|
|
4823
|
+
*/
|
|
4824
|
+
send(...args) {
|
|
4825
|
+
args.unshift("message");
|
|
4826
|
+
this.emit.apply(this, args);
|
|
4827
|
+
return this;
|
|
4828
|
+
}
|
|
4829
|
+
/**
|
|
4830
|
+
* Override `emit`.
|
|
4831
|
+
* If the event is in `events`, it's emitted normally.
|
|
4832
|
+
*
|
|
4833
|
+
* @example
|
|
4834
|
+
* socket.emit("hello", "world");
|
|
4835
|
+
*
|
|
4836
|
+
* // all serializable datastructures are supported (no need to call JSON.stringify)
|
|
4837
|
+
* socket.emit("hello", 1, "2", { 3: ["4"], 5: Uint8Array.from([6]) });
|
|
4838
|
+
*
|
|
4839
|
+
* // with an acknowledgement from the server
|
|
4840
|
+
* socket.emit("hello", "world", (val) => {
|
|
4841
|
+
* // ...
|
|
4842
|
+
* });
|
|
4843
|
+
*
|
|
4844
|
+
* @return self
|
|
4845
|
+
*/
|
|
4846
|
+
emit(ev, ...args) {
|
|
4847
|
+
var _a, _b, _c;
|
|
4848
|
+
if (RESERVED_EVENTS.hasOwnProperty(ev)) {
|
|
4849
|
+
throw new Error('"' + ev.toString() + '" is a reserved event name');
|
|
4850
|
+
}
|
|
4851
|
+
args.unshift(ev);
|
|
4852
|
+
if (this._opts.retries && !this.flags.fromQueue && !this.flags.volatile) {
|
|
4853
|
+
this._addToQueue(args);
|
|
4854
|
+
return this;
|
|
4855
|
+
}
|
|
4856
|
+
const packet = {
|
|
4857
|
+
type: PacketType.EVENT,
|
|
4858
|
+
data: args,
|
|
4859
|
+
};
|
|
4860
|
+
packet.options = {};
|
|
4861
|
+
packet.options.compress = this.flags.compress !== false;
|
|
4862
|
+
// event ack callback
|
|
4863
|
+
if ("function" === typeof args[args.length - 1]) {
|
|
4864
|
+
const id = this.ids++;
|
|
4865
|
+
const ack = args.pop();
|
|
4866
|
+
this._registerAckCallback(id, ack);
|
|
4867
|
+
packet.id = id;
|
|
4868
|
+
}
|
|
4869
|
+
const isTransportWritable = (_b = (_a = this.io.engine) === null || _a === void 0 ? void 0 : _a.transport) === null || _b === void 0 ? void 0 : _b.writable;
|
|
4870
|
+
const isConnected = this.connected && !((_c = this.io.engine) === null || _c === void 0 ? void 0 : _c._hasPingExpired());
|
|
4871
|
+
const discardPacket = this.flags.volatile && !isTransportWritable;
|
|
4872
|
+
if (discardPacket) ;
|
|
4873
|
+
else if (isConnected) {
|
|
4874
|
+
this.notifyOutgoingListeners(packet);
|
|
4875
|
+
this.packet(packet);
|
|
4876
|
+
}
|
|
4877
|
+
else {
|
|
4878
|
+
this.sendBuffer.push(packet);
|
|
4879
|
+
}
|
|
4880
|
+
this.flags = {};
|
|
4881
|
+
return this;
|
|
4882
|
+
}
|
|
4883
|
+
/**
|
|
4884
|
+
* @private
|
|
4885
|
+
*/
|
|
4886
|
+
_registerAckCallback(id, ack) {
|
|
4887
|
+
var _a;
|
|
4888
|
+
const timeout = (_a = this.flags.timeout) !== null && _a !== void 0 ? _a : this._opts.ackTimeout;
|
|
4889
|
+
if (timeout === undefined) {
|
|
4890
|
+
this.acks[id] = ack;
|
|
4891
|
+
return;
|
|
4892
|
+
}
|
|
4893
|
+
// @ts-ignore
|
|
4894
|
+
const timer = this.io.setTimeoutFn(() => {
|
|
4895
|
+
delete this.acks[id];
|
|
4896
|
+
for (let i = 0; i < this.sendBuffer.length; i++) {
|
|
4897
|
+
if (this.sendBuffer[i].id === id) {
|
|
4898
|
+
this.sendBuffer.splice(i, 1);
|
|
4899
|
+
}
|
|
4900
|
+
}
|
|
4901
|
+
ack.call(this, new Error("operation has timed out"));
|
|
4902
|
+
}, timeout);
|
|
4903
|
+
const fn = (...args) => {
|
|
4904
|
+
// @ts-ignore
|
|
4905
|
+
this.io.clearTimeoutFn(timer);
|
|
4906
|
+
ack.apply(this, args);
|
|
4907
|
+
};
|
|
4908
|
+
fn.withError = true;
|
|
4909
|
+
this.acks[id] = fn;
|
|
4910
|
+
}
|
|
4911
|
+
/**
|
|
4912
|
+
* Emits an event and waits for an acknowledgement
|
|
4913
|
+
*
|
|
4914
|
+
* @example
|
|
4915
|
+
* // without timeout
|
|
4916
|
+
* const response = await socket.emitWithAck("hello", "world");
|
|
4917
|
+
*
|
|
4918
|
+
* // with a specific timeout
|
|
4919
|
+
* try {
|
|
4920
|
+
* const response = await socket.timeout(1000).emitWithAck("hello", "world");
|
|
4921
|
+
* } catch (err) {
|
|
4922
|
+
* // the server did not acknowledge the event in the given delay
|
|
4923
|
+
* }
|
|
4924
|
+
*
|
|
4925
|
+
* @return a Promise that will be fulfilled when the server acknowledges the event
|
|
4926
|
+
*/
|
|
4927
|
+
emitWithAck(ev, ...args) {
|
|
4928
|
+
return new Promise((resolve, reject) => {
|
|
4929
|
+
const fn = (arg1, arg2) => {
|
|
4930
|
+
return arg1 ? reject(arg1) : resolve(arg2);
|
|
4931
|
+
};
|
|
4932
|
+
fn.withError = true;
|
|
4933
|
+
args.push(fn);
|
|
4934
|
+
this.emit(ev, ...args);
|
|
4935
|
+
});
|
|
4936
|
+
}
|
|
4937
|
+
/**
|
|
4938
|
+
* Add the packet to the queue.
|
|
4939
|
+
* @param args
|
|
4940
|
+
* @private
|
|
4941
|
+
*/
|
|
4942
|
+
_addToQueue(args) {
|
|
4943
|
+
let ack;
|
|
4944
|
+
if (typeof args[args.length - 1] === "function") {
|
|
4945
|
+
ack = args.pop();
|
|
4946
|
+
}
|
|
4947
|
+
const packet = {
|
|
4948
|
+
id: this._queueSeq++,
|
|
4949
|
+
tryCount: 0,
|
|
4950
|
+
pending: false,
|
|
4951
|
+
args,
|
|
4952
|
+
flags: Object.assign({ fromQueue: true }, this.flags),
|
|
4953
|
+
};
|
|
4954
|
+
args.push((err, ...responseArgs) => {
|
|
4955
|
+
if (packet !== this._queue[0]) {
|
|
4956
|
+
// the packet has already been acknowledged
|
|
4957
|
+
return;
|
|
4958
|
+
}
|
|
4959
|
+
const hasError = err !== null;
|
|
4960
|
+
if (hasError) {
|
|
4961
|
+
if (packet.tryCount > this._opts.retries) {
|
|
4962
|
+
this._queue.shift();
|
|
4963
|
+
if (ack) {
|
|
4964
|
+
ack(err);
|
|
4965
|
+
}
|
|
4966
|
+
}
|
|
4967
|
+
}
|
|
4968
|
+
else {
|
|
4969
|
+
this._queue.shift();
|
|
4970
|
+
if (ack) {
|
|
4971
|
+
ack(null, ...responseArgs);
|
|
4972
|
+
}
|
|
4973
|
+
}
|
|
4974
|
+
packet.pending = false;
|
|
4975
|
+
return this._drainQueue();
|
|
4976
|
+
});
|
|
4977
|
+
this._queue.push(packet);
|
|
4978
|
+
this._drainQueue();
|
|
4979
|
+
}
|
|
4980
|
+
/**
|
|
4981
|
+
* Send the first packet of the queue, and wait for an acknowledgement from the server.
|
|
4982
|
+
* @param force - whether to resend a packet that has not been acknowledged yet
|
|
4983
|
+
*
|
|
4984
|
+
* @private
|
|
4985
|
+
*/
|
|
4986
|
+
_drainQueue(force = false) {
|
|
4987
|
+
if (!this.connected || this._queue.length === 0) {
|
|
4988
|
+
return;
|
|
4989
|
+
}
|
|
4990
|
+
const packet = this._queue[0];
|
|
4991
|
+
if (packet.pending && !force) {
|
|
4992
|
+
return;
|
|
4993
|
+
}
|
|
4994
|
+
packet.pending = true;
|
|
4995
|
+
packet.tryCount++;
|
|
4996
|
+
this.flags = packet.flags;
|
|
4997
|
+
this.emit.apply(this, packet.args);
|
|
4998
|
+
}
|
|
4999
|
+
/**
|
|
5000
|
+
* Sends a packet.
|
|
5001
|
+
*
|
|
5002
|
+
* @param packet
|
|
5003
|
+
* @private
|
|
5004
|
+
*/
|
|
5005
|
+
packet(packet) {
|
|
5006
|
+
packet.nsp = this.nsp;
|
|
5007
|
+
this.io._packet(packet);
|
|
5008
|
+
}
|
|
5009
|
+
/**
|
|
5010
|
+
* Called upon engine `open`.
|
|
5011
|
+
*
|
|
5012
|
+
* @private
|
|
5013
|
+
*/
|
|
5014
|
+
onopen() {
|
|
5015
|
+
if (typeof this.auth == "function") {
|
|
5016
|
+
this.auth((data) => {
|
|
5017
|
+
this._sendConnectPacket(data);
|
|
5018
|
+
});
|
|
5019
|
+
}
|
|
5020
|
+
else {
|
|
5021
|
+
this._sendConnectPacket(this.auth);
|
|
5022
|
+
}
|
|
5023
|
+
}
|
|
5024
|
+
/**
|
|
5025
|
+
* Sends a CONNECT packet to initiate the Socket.IO session.
|
|
5026
|
+
*
|
|
5027
|
+
* @param data
|
|
5028
|
+
* @private
|
|
5029
|
+
*/
|
|
5030
|
+
_sendConnectPacket(data) {
|
|
5031
|
+
this.packet({
|
|
5032
|
+
type: PacketType.CONNECT,
|
|
5033
|
+
data: this._pid
|
|
5034
|
+
? Object.assign({ pid: this._pid, offset: this._lastOffset }, data)
|
|
5035
|
+
: data,
|
|
5036
|
+
});
|
|
5037
|
+
}
|
|
5038
|
+
/**
|
|
5039
|
+
* Called upon engine or manager `error`.
|
|
5040
|
+
*
|
|
5041
|
+
* @param err
|
|
5042
|
+
* @private
|
|
5043
|
+
*/
|
|
5044
|
+
onerror(err) {
|
|
5045
|
+
if (!this.connected) {
|
|
5046
|
+
this.emitReserved("connect_error", err);
|
|
5047
|
+
}
|
|
5048
|
+
}
|
|
5049
|
+
/**
|
|
5050
|
+
* Called upon engine `close`.
|
|
5051
|
+
*
|
|
5052
|
+
* @param reason
|
|
5053
|
+
* @param description
|
|
5054
|
+
* @private
|
|
5055
|
+
*/
|
|
5056
|
+
onclose(reason, description) {
|
|
5057
|
+
this.connected = false;
|
|
5058
|
+
delete this.id;
|
|
5059
|
+
this.emitReserved("disconnect", reason, description);
|
|
5060
|
+
this._clearAcks();
|
|
5061
|
+
}
|
|
5062
|
+
/**
|
|
5063
|
+
* Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
|
|
5064
|
+
* the server.
|
|
5065
|
+
*
|
|
5066
|
+
* @private
|
|
5067
|
+
*/
|
|
5068
|
+
_clearAcks() {
|
|
5069
|
+
Object.keys(this.acks).forEach((id) => {
|
|
5070
|
+
const isBuffered = this.sendBuffer.some((packet) => String(packet.id) === id);
|
|
5071
|
+
if (!isBuffered) {
|
|
5072
|
+
// note: handlers that do not accept an error as first argument are ignored here
|
|
5073
|
+
const ack = this.acks[id];
|
|
5074
|
+
delete this.acks[id];
|
|
5075
|
+
if (ack.withError) {
|
|
5076
|
+
ack.call(this, new Error("socket has been disconnected"));
|
|
5077
|
+
}
|
|
5078
|
+
}
|
|
5079
|
+
});
|
|
5080
|
+
}
|
|
5081
|
+
/**
|
|
5082
|
+
* Called with socket packet.
|
|
5083
|
+
*
|
|
5084
|
+
* @param packet
|
|
5085
|
+
* @private
|
|
5086
|
+
*/
|
|
5087
|
+
onpacket(packet) {
|
|
5088
|
+
const sameNamespace = packet.nsp === this.nsp;
|
|
5089
|
+
if (!sameNamespace)
|
|
5090
|
+
return;
|
|
5091
|
+
switch (packet.type) {
|
|
5092
|
+
case PacketType.CONNECT:
|
|
5093
|
+
if (packet.data && packet.data.sid) {
|
|
5094
|
+
this.onconnect(packet.data.sid, packet.data.pid);
|
|
5095
|
+
}
|
|
5096
|
+
else {
|
|
5097
|
+
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/)"));
|
|
5098
|
+
}
|
|
5099
|
+
break;
|
|
5100
|
+
case PacketType.EVENT:
|
|
5101
|
+
case PacketType.BINARY_EVENT:
|
|
5102
|
+
this.onevent(packet);
|
|
5103
|
+
break;
|
|
5104
|
+
case PacketType.ACK:
|
|
5105
|
+
case PacketType.BINARY_ACK:
|
|
5106
|
+
this.onack(packet);
|
|
5107
|
+
break;
|
|
5108
|
+
case PacketType.DISCONNECT:
|
|
5109
|
+
this.ondisconnect();
|
|
5110
|
+
break;
|
|
5111
|
+
case PacketType.CONNECT_ERROR:
|
|
5112
|
+
this.destroy();
|
|
5113
|
+
const err = new Error(packet.data.message);
|
|
5114
|
+
// @ts-ignore
|
|
5115
|
+
err.data = packet.data.data;
|
|
5116
|
+
this.emitReserved("connect_error", err);
|
|
5117
|
+
break;
|
|
5118
|
+
}
|
|
5119
|
+
}
|
|
5120
|
+
/**
|
|
5121
|
+
* Called upon a server event.
|
|
5122
|
+
*
|
|
5123
|
+
* @param packet
|
|
5124
|
+
* @private
|
|
5125
|
+
*/
|
|
5126
|
+
onevent(packet) {
|
|
5127
|
+
const args = packet.data || [];
|
|
5128
|
+
if (null != packet.id) {
|
|
5129
|
+
args.push(this.ack(packet.id));
|
|
5130
|
+
}
|
|
5131
|
+
if (this.connected) {
|
|
5132
|
+
this.emitEvent(args);
|
|
5133
|
+
}
|
|
5134
|
+
else {
|
|
5135
|
+
this.receiveBuffer.push(Object.freeze(args));
|
|
5136
|
+
}
|
|
5137
|
+
}
|
|
5138
|
+
emitEvent(args) {
|
|
5139
|
+
if (this._anyListeners && this._anyListeners.length) {
|
|
5140
|
+
const listeners = this._anyListeners.slice();
|
|
5141
|
+
for (const listener of listeners) {
|
|
5142
|
+
listener.apply(this, args);
|
|
5143
|
+
}
|
|
5144
|
+
}
|
|
5145
|
+
super.emit.apply(this, args);
|
|
5146
|
+
if (this._pid && args.length && typeof args[args.length - 1] === "string") {
|
|
5147
|
+
this._lastOffset = args[args.length - 1];
|
|
5148
|
+
}
|
|
5149
|
+
}
|
|
5150
|
+
/**
|
|
5151
|
+
* Produces an ack callback to emit with an event.
|
|
5152
|
+
*
|
|
5153
|
+
* @private
|
|
5154
|
+
*/
|
|
5155
|
+
ack(id) {
|
|
5156
|
+
const self = this;
|
|
5157
|
+
let sent = false;
|
|
5158
|
+
return function (...args) {
|
|
5159
|
+
// prevent double callbacks
|
|
5160
|
+
if (sent)
|
|
5161
|
+
return;
|
|
5162
|
+
sent = true;
|
|
5163
|
+
self.packet({
|
|
5164
|
+
type: PacketType.ACK,
|
|
5165
|
+
id: id,
|
|
5166
|
+
data: args,
|
|
5167
|
+
});
|
|
5168
|
+
};
|
|
5169
|
+
}
|
|
5170
|
+
/**
|
|
5171
|
+
* Called upon a server acknowledgement.
|
|
5172
|
+
*
|
|
5173
|
+
* @param packet
|
|
5174
|
+
* @private
|
|
5175
|
+
*/
|
|
5176
|
+
onack(packet) {
|
|
5177
|
+
const ack = this.acks[packet.id];
|
|
5178
|
+
if (typeof ack !== "function") {
|
|
5179
|
+
return;
|
|
5180
|
+
}
|
|
5181
|
+
delete this.acks[packet.id];
|
|
5182
|
+
// @ts-ignore FIXME ack is incorrectly inferred as 'never'
|
|
5183
|
+
if (ack.withError) {
|
|
5184
|
+
packet.data.unshift(null);
|
|
5185
|
+
}
|
|
5186
|
+
// @ts-ignore
|
|
5187
|
+
ack.apply(this, packet.data);
|
|
5188
|
+
}
|
|
5189
|
+
/**
|
|
5190
|
+
* Called upon server connect.
|
|
5191
|
+
*
|
|
5192
|
+
* @private
|
|
5193
|
+
*/
|
|
5194
|
+
onconnect(id, pid) {
|
|
5195
|
+
this.id = id;
|
|
5196
|
+
this.recovered = pid && this._pid === pid;
|
|
5197
|
+
this._pid = pid; // defined only if connection state recovery is enabled
|
|
5198
|
+
this.connected = true;
|
|
5199
|
+
this.emitBuffered();
|
|
5200
|
+
this.emitReserved("connect");
|
|
5201
|
+
this._drainQueue(true);
|
|
5202
|
+
}
|
|
5203
|
+
/**
|
|
5204
|
+
* Emit buffered events (received and emitted).
|
|
5205
|
+
*
|
|
5206
|
+
* @private
|
|
5207
|
+
*/
|
|
5208
|
+
emitBuffered() {
|
|
5209
|
+
this.receiveBuffer.forEach((args) => this.emitEvent(args));
|
|
5210
|
+
this.receiveBuffer = [];
|
|
5211
|
+
this.sendBuffer.forEach((packet) => {
|
|
5212
|
+
this.notifyOutgoingListeners(packet);
|
|
5213
|
+
this.packet(packet);
|
|
5214
|
+
});
|
|
5215
|
+
this.sendBuffer = [];
|
|
5216
|
+
}
|
|
5217
|
+
/**
|
|
5218
|
+
* Called upon server disconnect.
|
|
5219
|
+
*
|
|
5220
|
+
* @private
|
|
5221
|
+
*/
|
|
5222
|
+
ondisconnect() {
|
|
5223
|
+
this.destroy();
|
|
5224
|
+
this.onclose("io server disconnect");
|
|
5225
|
+
}
|
|
5226
|
+
/**
|
|
5227
|
+
* Called upon forced client/server side disconnections,
|
|
5228
|
+
* this method ensures the manager stops tracking us and
|
|
5229
|
+
* that reconnections don't get triggered for this.
|
|
5230
|
+
*
|
|
5231
|
+
* @private
|
|
5232
|
+
*/
|
|
5233
|
+
destroy() {
|
|
5234
|
+
if (this.subs) {
|
|
5235
|
+
// clean subscriptions to avoid reconnections
|
|
5236
|
+
this.subs.forEach((subDestroy) => subDestroy());
|
|
5237
|
+
this.subs = undefined;
|
|
5238
|
+
}
|
|
5239
|
+
this.io["_destroy"](this);
|
|
5240
|
+
}
|
|
5241
|
+
/**
|
|
5242
|
+
* Disconnects the socket manually. In that case, the socket will not try to reconnect.
|
|
5243
|
+
*
|
|
5244
|
+
* If this is the last active Socket instance of the {@link Manager}, the low-level connection will be closed.
|
|
5245
|
+
*
|
|
5246
|
+
* @example
|
|
5247
|
+
* const socket = io();
|
|
5248
|
+
*
|
|
5249
|
+
* socket.on("disconnect", (reason) => {
|
|
5250
|
+
* // console.log(reason); prints "io client disconnect"
|
|
5251
|
+
* });
|
|
5252
|
+
*
|
|
5253
|
+
* socket.disconnect();
|
|
5254
|
+
*
|
|
5255
|
+
* @return self
|
|
5256
|
+
*/
|
|
5257
|
+
disconnect() {
|
|
5258
|
+
if (this.connected) {
|
|
5259
|
+
this.packet({ type: PacketType.DISCONNECT });
|
|
5260
|
+
}
|
|
5261
|
+
// remove socket from pool
|
|
5262
|
+
this.destroy();
|
|
5263
|
+
if (this.connected) {
|
|
5264
|
+
// fire events
|
|
5265
|
+
this.onclose("io client disconnect");
|
|
5266
|
+
}
|
|
5267
|
+
return this;
|
|
5268
|
+
}
|
|
5269
|
+
/**
|
|
5270
|
+
* Alias for {@link disconnect()}.
|
|
5271
|
+
*
|
|
5272
|
+
* @return self
|
|
5273
|
+
*/
|
|
5274
|
+
close() {
|
|
5275
|
+
return this.disconnect();
|
|
5276
|
+
}
|
|
5277
|
+
/**
|
|
5278
|
+
* Sets the compress flag.
|
|
5279
|
+
*
|
|
5280
|
+
* @example
|
|
5281
|
+
* socket.compress(false).emit("hello");
|
|
5282
|
+
*
|
|
5283
|
+
* @param compress - if `true`, compresses the sending data
|
|
5284
|
+
* @return self
|
|
5285
|
+
*/
|
|
5286
|
+
compress(compress) {
|
|
5287
|
+
this.flags.compress = compress;
|
|
5288
|
+
return this;
|
|
5289
|
+
}
|
|
5290
|
+
/**
|
|
5291
|
+
* Sets a modifier for a subsequent event emission that the event message will be dropped when this socket is not
|
|
5292
|
+
* ready to send messages.
|
|
5293
|
+
*
|
|
5294
|
+
* @example
|
|
5295
|
+
* socket.volatile.emit("hello"); // the server may or may not receive it
|
|
5296
|
+
*
|
|
5297
|
+
* @returns self
|
|
5298
|
+
*/
|
|
5299
|
+
get volatile() {
|
|
5300
|
+
this.flags.volatile = true;
|
|
5301
|
+
return this;
|
|
5302
|
+
}
|
|
5303
|
+
/**
|
|
5304
|
+
* Sets a modifier for a subsequent event emission that the callback will be called with an error when the
|
|
5305
|
+
* given number of milliseconds have elapsed without an acknowledgement from the server:
|
|
5306
|
+
*
|
|
5307
|
+
* @example
|
|
5308
|
+
* socket.timeout(5000).emit("my-event", (err) => {
|
|
5309
|
+
* if (err) {
|
|
5310
|
+
* // the server did not acknowledge the event in the given delay
|
|
5311
|
+
* }
|
|
5312
|
+
* });
|
|
5313
|
+
*
|
|
5314
|
+
* @returns self
|
|
5315
|
+
*/
|
|
5316
|
+
timeout(timeout) {
|
|
5317
|
+
this.flags.timeout = timeout;
|
|
5318
|
+
return this;
|
|
5319
|
+
}
|
|
5320
|
+
/**
|
|
5321
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
5322
|
+
* callback.
|
|
5323
|
+
*
|
|
5324
|
+
* @example
|
|
5325
|
+
* socket.onAny((event, ...args) => {
|
|
5326
|
+
* console.log(`got ${event}`);
|
|
5327
|
+
* });
|
|
5328
|
+
*
|
|
5329
|
+
* @param listener
|
|
5330
|
+
*/
|
|
5331
|
+
onAny(listener) {
|
|
5332
|
+
this._anyListeners = this._anyListeners || [];
|
|
5333
|
+
this._anyListeners.push(listener);
|
|
5334
|
+
return this;
|
|
5335
|
+
}
|
|
5336
|
+
/**
|
|
5337
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
5338
|
+
* callback. The listener is added to the beginning of the listeners array.
|
|
5339
|
+
*
|
|
5340
|
+
* @example
|
|
5341
|
+
* socket.prependAny((event, ...args) => {
|
|
5342
|
+
* console.log(`got event ${event}`);
|
|
5343
|
+
* });
|
|
5344
|
+
*
|
|
5345
|
+
* @param listener
|
|
5346
|
+
*/
|
|
5347
|
+
prependAny(listener) {
|
|
5348
|
+
this._anyListeners = this._anyListeners || [];
|
|
5349
|
+
this._anyListeners.unshift(listener);
|
|
5350
|
+
return this;
|
|
5351
|
+
}
|
|
5352
|
+
/**
|
|
5353
|
+
* Removes the listener that will be fired when any event is emitted.
|
|
5354
|
+
*
|
|
5355
|
+
* @example
|
|
5356
|
+
* const catchAllListener = (event, ...args) => {
|
|
5357
|
+
* console.log(`got event ${event}`);
|
|
5358
|
+
* }
|
|
5359
|
+
*
|
|
5360
|
+
* socket.onAny(catchAllListener);
|
|
5361
|
+
*
|
|
5362
|
+
* // remove a specific listener
|
|
5363
|
+
* socket.offAny(catchAllListener);
|
|
5364
|
+
*
|
|
5365
|
+
* // or remove all listeners
|
|
5366
|
+
* socket.offAny();
|
|
5367
|
+
*
|
|
5368
|
+
* @param listener
|
|
5369
|
+
*/
|
|
5370
|
+
offAny(listener) {
|
|
5371
|
+
if (!this._anyListeners) {
|
|
5372
|
+
return this;
|
|
5373
|
+
}
|
|
5374
|
+
if (listener) {
|
|
5375
|
+
const listeners = this._anyListeners;
|
|
5376
|
+
for (let i = 0; i < listeners.length; i++) {
|
|
5377
|
+
if (listener === listeners[i]) {
|
|
5378
|
+
listeners.splice(i, 1);
|
|
5379
|
+
return this;
|
|
5380
|
+
}
|
|
5381
|
+
}
|
|
5382
|
+
}
|
|
5383
|
+
else {
|
|
5384
|
+
this._anyListeners = [];
|
|
5385
|
+
}
|
|
5386
|
+
return this;
|
|
5387
|
+
}
|
|
5388
|
+
/**
|
|
5389
|
+
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
|
5390
|
+
* e.g. to remove listeners.
|
|
5391
|
+
*/
|
|
5392
|
+
listenersAny() {
|
|
5393
|
+
return this._anyListeners || [];
|
|
5394
|
+
}
|
|
5395
|
+
/**
|
|
5396
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
5397
|
+
* callback.
|
|
5398
|
+
*
|
|
5399
|
+
* Note: acknowledgements sent to the server are not included.
|
|
5400
|
+
*
|
|
5401
|
+
* @example
|
|
5402
|
+
* socket.onAnyOutgoing((event, ...args) => {
|
|
5403
|
+
* console.log(`sent event ${event}`);
|
|
5404
|
+
* });
|
|
5405
|
+
*
|
|
5406
|
+
* @param listener
|
|
5407
|
+
*/
|
|
5408
|
+
onAnyOutgoing(listener) {
|
|
5409
|
+
this._anyOutgoingListeners = this._anyOutgoingListeners || [];
|
|
5410
|
+
this._anyOutgoingListeners.push(listener);
|
|
5411
|
+
return this;
|
|
5412
|
+
}
|
|
5413
|
+
/**
|
|
5414
|
+
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
|
|
5415
|
+
* callback. The listener is added to the beginning of the listeners array.
|
|
5416
|
+
*
|
|
5417
|
+
* Note: acknowledgements sent to the server are not included.
|
|
5418
|
+
*
|
|
5419
|
+
* @example
|
|
5420
|
+
* socket.prependAnyOutgoing((event, ...args) => {
|
|
5421
|
+
* console.log(`sent event ${event}`);
|
|
5422
|
+
* });
|
|
5423
|
+
*
|
|
5424
|
+
* @param listener
|
|
5425
|
+
*/
|
|
5426
|
+
prependAnyOutgoing(listener) {
|
|
5427
|
+
this._anyOutgoingListeners = this._anyOutgoingListeners || [];
|
|
5428
|
+
this._anyOutgoingListeners.unshift(listener);
|
|
5429
|
+
return this;
|
|
5430
|
+
}
|
|
5431
|
+
/**
|
|
5432
|
+
* Removes the listener that will be fired when any event is emitted.
|
|
5433
|
+
*
|
|
5434
|
+
* @example
|
|
5435
|
+
* const catchAllListener = (event, ...args) => {
|
|
5436
|
+
* console.log(`sent event ${event}`);
|
|
5437
|
+
* }
|
|
5438
|
+
*
|
|
5439
|
+
* socket.onAnyOutgoing(catchAllListener);
|
|
5440
|
+
*
|
|
5441
|
+
* // remove a specific listener
|
|
5442
|
+
* socket.offAnyOutgoing(catchAllListener);
|
|
5443
|
+
*
|
|
5444
|
+
* // or remove all listeners
|
|
5445
|
+
* socket.offAnyOutgoing();
|
|
5446
|
+
*
|
|
5447
|
+
* @param [listener] - the catch-all listener (optional)
|
|
5448
|
+
*/
|
|
5449
|
+
offAnyOutgoing(listener) {
|
|
5450
|
+
if (!this._anyOutgoingListeners) {
|
|
5451
|
+
return this;
|
|
5452
|
+
}
|
|
5453
|
+
if (listener) {
|
|
5454
|
+
const listeners = this._anyOutgoingListeners;
|
|
5455
|
+
for (let i = 0; i < listeners.length; i++) {
|
|
5456
|
+
if (listener === listeners[i]) {
|
|
5457
|
+
listeners.splice(i, 1);
|
|
5458
|
+
return this;
|
|
5459
|
+
}
|
|
5460
|
+
}
|
|
5461
|
+
}
|
|
5462
|
+
else {
|
|
5463
|
+
this._anyOutgoingListeners = [];
|
|
5464
|
+
}
|
|
5465
|
+
return this;
|
|
5466
|
+
}
|
|
5467
|
+
/**
|
|
5468
|
+
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
|
|
5469
|
+
* e.g. to remove listeners.
|
|
5470
|
+
*/
|
|
5471
|
+
listenersAnyOutgoing() {
|
|
5472
|
+
return this._anyOutgoingListeners || [];
|
|
5473
|
+
}
|
|
5474
|
+
/**
|
|
5475
|
+
* Notify the listeners for each packet sent
|
|
5476
|
+
*
|
|
5477
|
+
* @param packet
|
|
5478
|
+
*
|
|
5479
|
+
* @private
|
|
5480
|
+
*/
|
|
5481
|
+
notifyOutgoingListeners(packet) {
|
|
5482
|
+
if (this._anyOutgoingListeners && this._anyOutgoingListeners.length) {
|
|
5483
|
+
const listeners = this._anyOutgoingListeners.slice();
|
|
5484
|
+
for (const listener of listeners) {
|
|
5485
|
+
listener.apply(this, packet.data);
|
|
5486
|
+
}
|
|
5487
|
+
}
|
|
5488
|
+
}
|
|
5489
|
+
}
|
|
5490
|
+
|
|
5491
|
+
/**
|
|
5492
|
+
* Initialize backoff timer with `opts`.
|
|
5493
|
+
*
|
|
5494
|
+
* - `min` initial timeout in milliseconds [100]
|
|
5495
|
+
* - `max` max timeout [10000]
|
|
5496
|
+
* - `jitter` [0]
|
|
5497
|
+
* - `factor` [2]
|
|
5498
|
+
*
|
|
5499
|
+
* @param {Object} opts
|
|
5500
|
+
* @api public
|
|
5501
|
+
*/
|
|
5502
|
+
function Backoff(opts) {
|
|
5503
|
+
opts = opts || {};
|
|
5504
|
+
this.ms = opts.min || 100;
|
|
5505
|
+
this.max = opts.max || 10000;
|
|
5506
|
+
this.factor = opts.factor || 2;
|
|
5507
|
+
this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
|
|
5508
|
+
this.attempts = 0;
|
|
5509
|
+
}
|
|
5510
|
+
/**
|
|
5511
|
+
* Return the backoff duration.
|
|
5512
|
+
*
|
|
5513
|
+
* @return {Number}
|
|
5514
|
+
* @api public
|
|
5515
|
+
*/
|
|
5516
|
+
Backoff.prototype.duration = function () {
|
|
5517
|
+
var ms = this.ms * Math.pow(this.factor, this.attempts++);
|
|
5518
|
+
if (this.jitter) {
|
|
5519
|
+
var rand = Math.random();
|
|
5520
|
+
var deviation = Math.floor(rand * this.jitter * ms);
|
|
5521
|
+
ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation;
|
|
5522
|
+
}
|
|
5523
|
+
return Math.min(ms, this.max) | 0;
|
|
5524
|
+
};
|
|
5525
|
+
/**
|
|
5526
|
+
* Reset the number of attempts.
|
|
5527
|
+
*
|
|
5528
|
+
* @api public
|
|
5529
|
+
*/
|
|
5530
|
+
Backoff.prototype.reset = function () {
|
|
5531
|
+
this.attempts = 0;
|
|
5532
|
+
};
|
|
5533
|
+
/**
|
|
5534
|
+
* Set the minimum duration
|
|
5535
|
+
*
|
|
5536
|
+
* @api public
|
|
5537
|
+
*/
|
|
5538
|
+
Backoff.prototype.setMin = function (min) {
|
|
5539
|
+
this.ms = min;
|
|
5540
|
+
};
|
|
5541
|
+
/**
|
|
5542
|
+
* Set the maximum duration
|
|
5543
|
+
*
|
|
5544
|
+
* @api public
|
|
5545
|
+
*/
|
|
5546
|
+
Backoff.prototype.setMax = function (max) {
|
|
5547
|
+
this.max = max;
|
|
5548
|
+
};
|
|
5549
|
+
/**
|
|
5550
|
+
* Set the jitter
|
|
5551
|
+
*
|
|
5552
|
+
* @api public
|
|
5553
|
+
*/
|
|
5554
|
+
Backoff.prototype.setJitter = function (jitter) {
|
|
5555
|
+
this.jitter = jitter;
|
|
5556
|
+
};
|
|
5557
|
+
|
|
5558
|
+
class Manager extends Emitter {
|
|
5559
|
+
constructor(uri, opts) {
|
|
5560
|
+
var _a;
|
|
5561
|
+
super();
|
|
5562
|
+
this.nsps = {};
|
|
5563
|
+
this.subs = [];
|
|
5564
|
+
if (uri && "object" === typeof uri) {
|
|
5565
|
+
opts = uri;
|
|
5566
|
+
uri = undefined;
|
|
5567
|
+
}
|
|
5568
|
+
opts = opts || {};
|
|
5569
|
+
opts.path = opts.path || "/socket.io";
|
|
5570
|
+
this.opts = opts;
|
|
5571
|
+
installTimerFunctions(this, opts);
|
|
5572
|
+
this.reconnection(opts.reconnection !== false);
|
|
5573
|
+
this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
|
|
5574
|
+
this.reconnectionDelay(opts.reconnectionDelay || 1000);
|
|
5575
|
+
this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000);
|
|
5576
|
+
this.randomizationFactor((_a = opts.randomizationFactor) !== null && _a !== void 0 ? _a : 0.5);
|
|
5577
|
+
this.backoff = new Backoff({
|
|
5578
|
+
min: this.reconnectionDelay(),
|
|
5579
|
+
max: this.reconnectionDelayMax(),
|
|
5580
|
+
jitter: this.randomizationFactor(),
|
|
5581
|
+
});
|
|
5582
|
+
this.timeout(null == opts.timeout ? 20000 : opts.timeout);
|
|
5583
|
+
this._readyState = "closed";
|
|
5584
|
+
this.uri = uri;
|
|
5585
|
+
const _parser = opts.parser || parser;
|
|
5586
|
+
this.encoder = new _parser.Encoder();
|
|
5587
|
+
this.decoder = new _parser.Decoder();
|
|
5588
|
+
this._autoConnect = opts.autoConnect !== false;
|
|
5589
|
+
if (this._autoConnect)
|
|
5590
|
+
this.open();
|
|
5591
|
+
}
|
|
5592
|
+
reconnection(v) {
|
|
5593
|
+
if (!arguments.length)
|
|
5594
|
+
return this._reconnection;
|
|
5595
|
+
this._reconnection = !!v;
|
|
5596
|
+
if (!v) {
|
|
5597
|
+
this.skipReconnect = true;
|
|
5598
|
+
}
|
|
5599
|
+
return this;
|
|
5600
|
+
}
|
|
5601
|
+
reconnectionAttempts(v) {
|
|
5602
|
+
if (v === undefined)
|
|
5603
|
+
return this._reconnectionAttempts;
|
|
5604
|
+
this._reconnectionAttempts = v;
|
|
5605
|
+
return this;
|
|
5606
|
+
}
|
|
5607
|
+
reconnectionDelay(v) {
|
|
5608
|
+
var _a;
|
|
5609
|
+
if (v === undefined)
|
|
5610
|
+
return this._reconnectionDelay;
|
|
5611
|
+
this._reconnectionDelay = v;
|
|
5612
|
+
(_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMin(v);
|
|
5613
|
+
return this;
|
|
5614
|
+
}
|
|
5615
|
+
randomizationFactor(v) {
|
|
5616
|
+
var _a;
|
|
5617
|
+
if (v === undefined)
|
|
5618
|
+
return this._randomizationFactor;
|
|
5619
|
+
this._randomizationFactor = v;
|
|
5620
|
+
(_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setJitter(v);
|
|
5621
|
+
return this;
|
|
5622
|
+
}
|
|
5623
|
+
reconnectionDelayMax(v) {
|
|
5624
|
+
var _a;
|
|
5625
|
+
if (v === undefined)
|
|
5626
|
+
return this._reconnectionDelayMax;
|
|
5627
|
+
this._reconnectionDelayMax = v;
|
|
5628
|
+
(_a = this.backoff) === null || _a === void 0 ? void 0 : _a.setMax(v);
|
|
5629
|
+
return this;
|
|
5630
|
+
}
|
|
5631
|
+
timeout(v) {
|
|
5632
|
+
if (!arguments.length)
|
|
5633
|
+
return this._timeout;
|
|
5634
|
+
this._timeout = v;
|
|
5635
|
+
return this;
|
|
5636
|
+
}
|
|
5637
|
+
/**
|
|
5638
|
+
* Starts trying to reconnect if reconnection is enabled and we have not
|
|
5639
|
+
* started reconnecting yet
|
|
5640
|
+
*
|
|
5641
|
+
* @private
|
|
5642
|
+
*/
|
|
5643
|
+
maybeReconnectOnOpen() {
|
|
5644
|
+
// Only try to reconnect if it's the first time we're connecting
|
|
5645
|
+
if (!this._reconnecting &&
|
|
5646
|
+
this._reconnection &&
|
|
5647
|
+
this.backoff.attempts === 0) {
|
|
5648
|
+
// keeps reconnection from firing twice for the same reconnection loop
|
|
5649
|
+
this.reconnect();
|
|
5650
|
+
}
|
|
5651
|
+
}
|
|
5652
|
+
/**
|
|
5653
|
+
* Sets the current transport `socket`.
|
|
5654
|
+
*
|
|
5655
|
+
* @param {Function} fn - optional, callback
|
|
5656
|
+
* @return self
|
|
5657
|
+
* @public
|
|
5658
|
+
*/
|
|
5659
|
+
open(fn) {
|
|
5660
|
+
if (~this._readyState.indexOf("open"))
|
|
5661
|
+
return this;
|
|
5662
|
+
this.engine = new Socket$1(this.uri, this.opts);
|
|
5663
|
+
const socket = this.engine;
|
|
5664
|
+
const self = this;
|
|
5665
|
+
this._readyState = "opening";
|
|
5666
|
+
this.skipReconnect = false;
|
|
5667
|
+
// emit `open`
|
|
5668
|
+
const openSubDestroy = on(socket, "open", function () {
|
|
5669
|
+
self.onopen();
|
|
5670
|
+
fn && fn();
|
|
5671
|
+
});
|
|
5672
|
+
const onError = (err) => {
|
|
5673
|
+
this.cleanup();
|
|
5674
|
+
this._readyState = "closed";
|
|
5675
|
+
this.emitReserved("error", err);
|
|
5676
|
+
if (fn) {
|
|
5677
|
+
fn(err);
|
|
5678
|
+
}
|
|
5679
|
+
else {
|
|
5680
|
+
// Only do this if there is no fn to handle the error
|
|
5681
|
+
this.maybeReconnectOnOpen();
|
|
5682
|
+
}
|
|
5683
|
+
};
|
|
5684
|
+
// emit `error`
|
|
5685
|
+
const errorSub = on(socket, "error", onError);
|
|
5686
|
+
if (false !== this._timeout) {
|
|
5687
|
+
const timeout = this._timeout;
|
|
5688
|
+
// set timer
|
|
5689
|
+
const timer = this.setTimeoutFn(() => {
|
|
5690
|
+
openSubDestroy();
|
|
5691
|
+
onError(new Error("timeout"));
|
|
5692
|
+
socket.close();
|
|
5693
|
+
}, timeout);
|
|
5694
|
+
if (this.opts.autoUnref) {
|
|
5695
|
+
timer.unref();
|
|
5696
|
+
}
|
|
5697
|
+
this.subs.push(() => {
|
|
5698
|
+
this.clearTimeoutFn(timer);
|
|
5699
|
+
});
|
|
5700
|
+
}
|
|
5701
|
+
this.subs.push(openSubDestroy);
|
|
5702
|
+
this.subs.push(errorSub);
|
|
5703
|
+
return this;
|
|
5704
|
+
}
|
|
5705
|
+
/**
|
|
5706
|
+
* Alias for open()
|
|
5707
|
+
*
|
|
5708
|
+
* @return self
|
|
5709
|
+
* @public
|
|
5710
|
+
*/
|
|
5711
|
+
connect(fn) {
|
|
5712
|
+
return this.open(fn);
|
|
5713
|
+
}
|
|
5714
|
+
/**
|
|
5715
|
+
* Called upon transport open.
|
|
5716
|
+
*
|
|
5717
|
+
* @private
|
|
5718
|
+
*/
|
|
5719
|
+
onopen() {
|
|
5720
|
+
// clear old subs
|
|
5721
|
+
this.cleanup();
|
|
5722
|
+
// mark as open
|
|
5723
|
+
this._readyState = "open";
|
|
5724
|
+
this.emitReserved("open");
|
|
5725
|
+
// add new subs
|
|
5726
|
+
const socket = this.engine;
|
|
5727
|
+
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)),
|
|
5728
|
+
// @ts-ignore
|
|
5729
|
+
on(this.decoder, "decoded", this.ondecoded.bind(this)));
|
|
5730
|
+
}
|
|
5731
|
+
/**
|
|
5732
|
+
* Called upon a ping.
|
|
5733
|
+
*
|
|
5734
|
+
* @private
|
|
5735
|
+
*/
|
|
5736
|
+
onping() {
|
|
5737
|
+
this.emitReserved("ping");
|
|
5738
|
+
}
|
|
5739
|
+
/**
|
|
5740
|
+
* Called with data.
|
|
5741
|
+
*
|
|
5742
|
+
* @private
|
|
5743
|
+
*/
|
|
5744
|
+
ondata(data) {
|
|
5745
|
+
try {
|
|
5746
|
+
this.decoder.add(data);
|
|
5747
|
+
}
|
|
5748
|
+
catch (e) {
|
|
5749
|
+
this.onclose("parse error", e);
|
|
5750
|
+
}
|
|
5751
|
+
}
|
|
5752
|
+
/**
|
|
5753
|
+
* Called when parser fully decodes a packet.
|
|
5754
|
+
*
|
|
5755
|
+
* @private
|
|
5756
|
+
*/
|
|
5757
|
+
ondecoded(packet) {
|
|
5758
|
+
// the nextTick call prevents an exception in a user-provided event listener from triggering a disconnection due to a "parse error"
|
|
5759
|
+
nextTick(() => {
|
|
5760
|
+
this.emitReserved("packet", packet);
|
|
5761
|
+
}, this.setTimeoutFn);
|
|
5762
|
+
}
|
|
5763
|
+
/**
|
|
5764
|
+
* Called upon socket error.
|
|
5765
|
+
*
|
|
5766
|
+
* @private
|
|
5767
|
+
*/
|
|
5768
|
+
onerror(err) {
|
|
5769
|
+
this.emitReserved("error", err);
|
|
5770
|
+
}
|
|
5771
|
+
/**
|
|
5772
|
+
* Creates a new socket for the given `nsp`.
|
|
5773
|
+
*
|
|
5774
|
+
* @return {Socket}
|
|
5775
|
+
* @public
|
|
5776
|
+
*/
|
|
5777
|
+
socket(nsp, opts) {
|
|
5778
|
+
let socket = this.nsps[nsp];
|
|
5779
|
+
if (!socket) {
|
|
5780
|
+
socket = new Socket(this, nsp, opts);
|
|
5781
|
+
this.nsps[nsp] = socket;
|
|
5782
|
+
}
|
|
5783
|
+
else if (this._autoConnect && !socket.active) {
|
|
5784
|
+
socket.connect();
|
|
5785
|
+
}
|
|
5786
|
+
return socket;
|
|
5787
|
+
}
|
|
5788
|
+
/**
|
|
5789
|
+
* Called upon a socket close.
|
|
5790
|
+
*
|
|
5791
|
+
* @param socket
|
|
5792
|
+
* @private
|
|
5793
|
+
*/
|
|
5794
|
+
_destroy(socket) {
|
|
5795
|
+
const nsps = Object.keys(this.nsps);
|
|
5796
|
+
for (const nsp of nsps) {
|
|
5797
|
+
const socket = this.nsps[nsp];
|
|
5798
|
+
if (socket.active) {
|
|
5799
|
+
return;
|
|
5800
|
+
}
|
|
5801
|
+
}
|
|
5802
|
+
this._close();
|
|
5803
|
+
}
|
|
5804
|
+
/**
|
|
5805
|
+
* Writes a packet.
|
|
5806
|
+
*
|
|
5807
|
+
* @param packet
|
|
5808
|
+
* @private
|
|
5809
|
+
*/
|
|
5810
|
+
_packet(packet) {
|
|
5811
|
+
const encodedPackets = this.encoder.encode(packet);
|
|
5812
|
+
for (let i = 0; i < encodedPackets.length; i++) {
|
|
5813
|
+
this.engine.write(encodedPackets[i], packet.options);
|
|
5814
|
+
}
|
|
5815
|
+
}
|
|
5816
|
+
/**
|
|
5817
|
+
* Clean up transport subscriptions and packet buffer.
|
|
5818
|
+
*
|
|
5819
|
+
* @private
|
|
5820
|
+
*/
|
|
5821
|
+
cleanup() {
|
|
5822
|
+
this.subs.forEach((subDestroy) => subDestroy());
|
|
5823
|
+
this.subs.length = 0;
|
|
5824
|
+
this.decoder.destroy();
|
|
5825
|
+
}
|
|
5826
|
+
/**
|
|
5827
|
+
* Close the current socket.
|
|
5828
|
+
*
|
|
5829
|
+
* @private
|
|
5830
|
+
*/
|
|
5831
|
+
_close() {
|
|
5832
|
+
this.skipReconnect = true;
|
|
5833
|
+
this._reconnecting = false;
|
|
5834
|
+
this.onclose("forced close");
|
|
5835
|
+
}
|
|
5836
|
+
/**
|
|
5837
|
+
* Alias for close()
|
|
5838
|
+
*
|
|
5839
|
+
* @private
|
|
5840
|
+
*/
|
|
5841
|
+
disconnect() {
|
|
5842
|
+
return this._close();
|
|
5843
|
+
}
|
|
5844
|
+
/**
|
|
5845
|
+
* Called when:
|
|
5846
|
+
*
|
|
5847
|
+
* - the low-level engine is closed
|
|
5848
|
+
* - the parser encountered a badly formatted packet
|
|
5849
|
+
* - all sockets are disconnected
|
|
5850
|
+
*
|
|
5851
|
+
* @private
|
|
5852
|
+
*/
|
|
5853
|
+
onclose(reason, description) {
|
|
5854
|
+
var _a;
|
|
5855
|
+
this.cleanup();
|
|
5856
|
+
(_a = this.engine) === null || _a === void 0 ? void 0 : _a.close();
|
|
5857
|
+
this.backoff.reset();
|
|
5858
|
+
this._readyState = "closed";
|
|
5859
|
+
this.emitReserved("close", reason, description);
|
|
5860
|
+
if (this._reconnection && !this.skipReconnect) {
|
|
5861
|
+
this.reconnect();
|
|
5862
|
+
}
|
|
5863
|
+
}
|
|
5864
|
+
/**
|
|
5865
|
+
* Attempt a reconnection.
|
|
5866
|
+
*
|
|
5867
|
+
* @private
|
|
5868
|
+
*/
|
|
5869
|
+
reconnect() {
|
|
5870
|
+
if (this._reconnecting || this.skipReconnect)
|
|
5871
|
+
return this;
|
|
5872
|
+
const self = this;
|
|
5873
|
+
if (this.backoff.attempts >= this._reconnectionAttempts) {
|
|
5874
|
+
this.backoff.reset();
|
|
5875
|
+
this.emitReserved("reconnect_failed");
|
|
5876
|
+
this._reconnecting = false;
|
|
5877
|
+
}
|
|
5878
|
+
else {
|
|
5879
|
+
const delay = this.backoff.duration();
|
|
5880
|
+
this._reconnecting = true;
|
|
5881
|
+
const timer = this.setTimeoutFn(() => {
|
|
5882
|
+
if (self.skipReconnect)
|
|
5883
|
+
return;
|
|
5884
|
+
this.emitReserved("reconnect_attempt", self.backoff.attempts);
|
|
5885
|
+
// check again for the case socket closed in above events
|
|
5886
|
+
if (self.skipReconnect)
|
|
5887
|
+
return;
|
|
5888
|
+
self.open((err) => {
|
|
5889
|
+
if (err) {
|
|
5890
|
+
self._reconnecting = false;
|
|
5891
|
+
self.reconnect();
|
|
5892
|
+
this.emitReserved("reconnect_error", err);
|
|
5893
|
+
}
|
|
5894
|
+
else {
|
|
5895
|
+
self.onreconnect();
|
|
5896
|
+
}
|
|
5897
|
+
});
|
|
5898
|
+
}, delay);
|
|
5899
|
+
if (this.opts.autoUnref) {
|
|
5900
|
+
timer.unref();
|
|
5901
|
+
}
|
|
5902
|
+
this.subs.push(() => {
|
|
5903
|
+
this.clearTimeoutFn(timer);
|
|
5904
|
+
});
|
|
5905
|
+
}
|
|
5906
|
+
}
|
|
5907
|
+
/**
|
|
5908
|
+
* Called upon successful reconnect.
|
|
5909
|
+
*
|
|
5910
|
+
* @private
|
|
5911
|
+
*/
|
|
5912
|
+
onreconnect() {
|
|
5913
|
+
const attempt = this.backoff.attempts;
|
|
5914
|
+
this._reconnecting = false;
|
|
5915
|
+
this.backoff.reset();
|
|
5916
|
+
this.emitReserved("reconnect", attempt);
|
|
5917
|
+
}
|
|
5918
|
+
}
|
|
5919
|
+
|
|
5920
|
+
/**
|
|
5921
|
+
* Managers cache.
|
|
5922
|
+
*/
|
|
5923
|
+
const cache = {};
|
|
5924
|
+
function lookup(uri, opts) {
|
|
5925
|
+
if (typeof uri === "object") {
|
|
5926
|
+
opts = uri;
|
|
5927
|
+
uri = undefined;
|
|
5928
|
+
}
|
|
5929
|
+
opts = opts || {};
|
|
5930
|
+
const parsed = url(uri, opts.path || "/socket.io");
|
|
5931
|
+
const source = parsed.source;
|
|
5932
|
+
const id = parsed.id;
|
|
5933
|
+
const path = parsed.path;
|
|
5934
|
+
const sameNamespace = cache[id] && path in cache[id]["nsps"];
|
|
5935
|
+
const newConnection = opts.forceNew ||
|
|
5936
|
+
opts["force new connection"] ||
|
|
5937
|
+
false === opts.multiplex ||
|
|
5938
|
+
sameNamespace;
|
|
5939
|
+
let io;
|
|
5940
|
+
if (newConnection) {
|
|
5941
|
+
io = new Manager(source, opts);
|
|
5942
|
+
}
|
|
5943
|
+
else {
|
|
5944
|
+
if (!cache[id]) {
|
|
5945
|
+
cache[id] = new Manager(source, opts);
|
|
5946
|
+
}
|
|
5947
|
+
io = cache[id];
|
|
5948
|
+
}
|
|
5949
|
+
if (parsed.query && !opts.query) {
|
|
5950
|
+
opts.query = parsed.queryKey;
|
|
5951
|
+
}
|
|
5952
|
+
return io.socket(parsed.path, opts);
|
|
5953
|
+
}
|
|
5954
|
+
// so that "lookup" can be used both as a function (e.g. `io(...)`) and as a
|
|
5955
|
+
// namespace (e.g. `io.connect(...)`), for backward compatibility
|
|
5956
|
+
Object.assign(lookup, {
|
|
5957
|
+
Manager,
|
|
5958
|
+
Socket,
|
|
5959
|
+
io: lookup,
|
|
5960
|
+
connect: lookup,
|
|
5961
|
+
});
|
|
5962
|
+
|
|
5963
|
+
const socket = lookup("http://localhost:4000");
|
|
3923
5964
|
const CoreOutline = ({
|
|
3924
|
-
children
|
|
5965
|
+
children,
|
|
5966
|
+
data_source_id
|
|
3925
5967
|
}) => {
|
|
3926
5968
|
const [events, setEvents] = useState([]);
|
|
3927
5969
|
const recorderActive = useRef(false);
|
|
3928
5970
|
const [currentPage, setCurrentPage] = useState(window.location.href);
|
|
5971
|
+
const [location, setLocation] = useState({
|
|
5972
|
+
latitude: null,
|
|
5973
|
+
longitude: null
|
|
5974
|
+
});
|
|
5975
|
+
const [browser, setBrowser] = useState(getBrowserInfo());
|
|
5976
|
+
const sessionId = v4();
|
|
5977
|
+
useEffect(() => {
|
|
5978
|
+
socket.emit("dataEvent", {
|
|
5979
|
+
topic: "session-data",
|
|
5980
|
+
data_source_id: data_source_id,
|
|
5981
|
+
session_id: sessionId,
|
|
5982
|
+
start_date: new Date().toLocaleString(),
|
|
5983
|
+
latitude: location.latitude,
|
|
5984
|
+
longitude: location.longitude,
|
|
5985
|
+
browser: browser,
|
|
5986
|
+
event_id: "SESSION_START"
|
|
5987
|
+
});
|
|
5988
|
+
return () => {
|
|
5989
|
+
socket.emit("dataEvent", {
|
|
5990
|
+
topic: "session-data",
|
|
5991
|
+
data_source_id: data_source_id,
|
|
5992
|
+
session_id: sessionId,
|
|
5993
|
+
start_date: new Date().toLocaleString(),
|
|
5994
|
+
latitude: location.latitude,
|
|
5995
|
+
longitude: location.longitude,
|
|
5996
|
+
browser: browser,
|
|
5997
|
+
event_id: "SESSION_END"
|
|
5998
|
+
});
|
|
5999
|
+
};
|
|
6000
|
+
}, []);
|
|
3929
6001
|
useEffect(() => {
|
|
3930
|
-
console.log(currentPage);
|
|
3931
6002
|
const handleNavigation = () => {
|
|
3932
6003
|
setCurrentPage(window.location.href);
|
|
6004
|
+
socket.emit("dataEvent", {
|
|
6005
|
+
topic: "session-data",
|
|
6006
|
+
data_source_id: data_source_id,
|
|
6007
|
+
session_id: sessionId,
|
|
6008
|
+
start_date: new Date().toLocaleString(),
|
|
6009
|
+
latitude: location.latitude,
|
|
6010
|
+
longitude: location.longitude,
|
|
6011
|
+
browser: browser,
|
|
6012
|
+
page_name: window.location.href,
|
|
6013
|
+
event_id: "PAGE_NAVIGATION"
|
|
6014
|
+
});
|
|
3933
6015
|
saveEvents(events);
|
|
3934
6016
|
};
|
|
3935
|
-
window.addEventListener(
|
|
6017
|
+
window.addEventListener("popstate", handleNavigation);
|
|
3936
6018
|
const originalPushState = window.history.pushState;
|
|
3937
6019
|
window.history.pushState = function (...args) {
|
|
3938
6020
|
originalPushState.apply(this, args);
|
|
3939
6021
|
handleNavigation();
|
|
3940
6022
|
};
|
|
3941
6023
|
return () => {
|
|
3942
|
-
window.removeEventListener(
|
|
6024
|
+
window.removeEventListener("popstate", handleNavigation);
|
|
3943
6025
|
window.history.pushState = originalPushState;
|
|
3944
6026
|
};
|
|
3945
6027
|
}, [currentPage]);
|
|
6028
|
+
useEffect(() => {
|
|
6029
|
+
console.log("Location", location);
|
|
6030
|
+
getLocation();
|
|
6031
|
+
}, []);
|
|
3946
6032
|
useEffect(() => {
|
|
3947
6033
|
let stopFn;
|
|
3948
6034
|
if (!recorderActive.current) {
|
|
@@ -3961,19 +6047,78 @@ const CoreOutline = ({
|
|
|
3961
6047
|
}
|
|
3962
6048
|
};
|
|
3963
6049
|
}, []);
|
|
6050
|
+
useEffect(() => {
|
|
6051
|
+
const handleClick = event => {
|
|
6052
|
+
console.log("Button clicked:", event.target.id);
|
|
6053
|
+
// const itemId = event.target.id;
|
|
6054
|
+
socket.emit("dataEvent", {
|
|
6055
|
+
topic: "session-data",
|
|
6056
|
+
data_source_id: data_source_id,
|
|
6057
|
+
session_id: sessionId,
|
|
6058
|
+
start_date: new Date().toLocaleString(),
|
|
6059
|
+
latitude: location.latitude,
|
|
6060
|
+
longitude: location.longitude,
|
|
6061
|
+
browser: browser,
|
|
6062
|
+
event_id: "ITEM_CLICKED",
|
|
6063
|
+
item_clicked: event.target
|
|
6064
|
+
});
|
|
6065
|
+
};
|
|
6066
|
+
document.addEventListener("click", handleClick);
|
|
6067
|
+
return () => {
|
|
6068
|
+
document.removeEventListener("click", handleClick);
|
|
6069
|
+
};
|
|
6070
|
+
}, []);
|
|
3964
6071
|
function saveEvents(events) {
|
|
3965
6072
|
const requestOptions = {
|
|
3966
|
-
method:
|
|
6073
|
+
method: "PUT",
|
|
3967
6074
|
headers: {
|
|
3968
|
-
|
|
6075
|
+
"Content-Type": "application/json"
|
|
3969
6076
|
},
|
|
3970
6077
|
body: JSON.stringify(events)
|
|
3971
6078
|
};
|
|
3972
|
-
fetch(
|
|
6079
|
+
fetch(`http://localhost:8000/generate-s3-url/${data_source_id}/${sessionId}`).then(response => response.json()).then(data => {
|
|
3973
6080
|
const uploadURL = data.signed_url;
|
|
3974
6081
|
return fetch(uploadURL, requestOptions);
|
|
3975
6082
|
});
|
|
3976
6083
|
}
|
|
6084
|
+
function getBrowserInfo() {
|
|
6085
|
+
const userAgent = navigator.userAgent;
|
|
6086
|
+
let browserName = "Unknown";
|
|
6087
|
+
if (userAgent.indexOf("Firefox") > -1) {
|
|
6088
|
+
browserName = "Firefox";
|
|
6089
|
+
} else if (userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR") > -1) {
|
|
6090
|
+
browserName = "Opera";
|
|
6091
|
+
} else if (userAgent.indexOf("Chrome") > -1) {
|
|
6092
|
+
browserName = "Chrome";
|
|
6093
|
+
} else if (userAgent.indexOf("Safari") > -1) {
|
|
6094
|
+
browserName = "Safari";
|
|
6095
|
+
} else if (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident/") > -1) {
|
|
6096
|
+
browserName = "Internet Explorer";
|
|
6097
|
+
}
|
|
6098
|
+
return browserName;
|
|
6099
|
+
}
|
|
6100
|
+
useEffect(() => {
|
|
6101
|
+
if (events.length > 0) {
|
|
6102
|
+
socket.emit("record-event", {
|
|
6103
|
+
sessionId,
|
|
6104
|
+
events
|
|
6105
|
+
});
|
|
6106
|
+
}
|
|
6107
|
+
}, [events]);
|
|
6108
|
+
function getLocation() {
|
|
6109
|
+
if (navigator.geolocation) {
|
|
6110
|
+
navigator.geolocation.getCurrentPosition(position => {
|
|
6111
|
+
setLocation({
|
|
6112
|
+
latitude: position.coords.latitude,
|
|
6113
|
+
longitude: position.coords.longitude
|
|
6114
|
+
});
|
|
6115
|
+
}, error => {
|
|
6116
|
+
console.error("Error getting location:", error);
|
|
6117
|
+
});
|
|
6118
|
+
} else {
|
|
6119
|
+
console.error("Geolocation is not supported by this browser.");
|
|
6120
|
+
}
|
|
6121
|
+
}
|
|
3977
6122
|
return /*#__PURE__*/React.createElement("div", null, children);
|
|
3978
6123
|
};
|
|
3979
6124
|
|