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