@webex/internal-plugin-mercury 3.10.0-next.3 → 3.10.0-wxc-disconnect.1

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/mercury.js CHANGED
@@ -11,11 +11,11 @@ _Object$defineProperty(exports, "__esModule", {
11
11
  value: true
12
12
  });
13
13
  exports.default = void 0;
14
- var _now = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/date/now"));
15
14
  var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
16
15
  var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));
17
16
  var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));
18
17
  var _deleteProperty = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/reflect/delete-property"));
18
+ var _now = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/date/now"));
19
19
  var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor"));
20
20
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
21
21
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/slicedToArray"));
@@ -102,87 +102,6 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
102
102
  }
103
103
  });
104
104
  },
105
- /**
106
- * Attach event listeners to a socket.
107
- * @param {Socket} socket - The socket to attach listeners to
108
- * @returns {void}
109
- */
110
- _attachSocketEventListeners: function _attachSocketEventListeners(socket) {
111
- var _this2 = this;
112
- socket.on('close', function (event) {
113
- return _this2._onclose(event, socket);
114
- });
115
- socket.on('message', function () {
116
- return _this2._onmessage.apply(_this2, arguments);
117
- });
118
- socket.on('pong', function () {
119
- return _this2._setTimeOffset.apply(_this2, arguments);
120
- });
121
- socket.on('sequence-mismatch', function () {
122
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
123
- args[_key] = arguments[_key];
124
- }
125
- return _this2._emit.apply(_this2, ['sequence-mismatch'].concat(args));
126
- });
127
- socket.on('ping-pong-latency', function () {
128
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
129
- args[_key2] = arguments[_key2];
130
- }
131
- return _this2._emit.apply(_this2, ['ping-pong-latency'].concat(args));
132
- });
133
- },
134
- /**
135
- * Handle imminent shutdown by establishing a new connection while keeping
136
- * the current one alive (make-before-break).
137
- * Idempotent: will no-op if already in progress.
138
- * @returns {void}
139
- */
140
- _handleImminentShutdown: function _handleImminentShutdown() {
141
- var _this3 = this;
142
- try {
143
- if (this._shutdownSwitchoverInProgress) {
144
- this.logger.info("".concat(this.namespace, ": [shutdown] switchover already in progress"));
145
- return;
146
- }
147
- this._shutdownSwitchoverInProgress = true;
148
- this._shutdownSwitchoverId = "".concat((0, _now.default)());
149
- this.logger.info("".concat(this.namespace, ": [shutdown] switchover start, id=").concat(this._shutdownSwitchoverId));
150
- this._connectWithBackoff(undefined, {
151
- isShutdownSwitchover: true,
152
- attemptOptions: {
153
- isShutdownSwitchover: true,
154
- onSuccess: function onSuccess(newSocket, webSocketUrl) {
155
- _this3.logger.info("".concat(_this3.namespace, ": [shutdown] switchover connected, url: ").concat(webSocketUrl));
156
- var oldSocket = _this3.socket;
157
- // Atomically switch active socket reference
158
- _this3.socket = newSocket;
159
- _this3.connected = true; // remain connected throughout
160
-
161
- _this3._emit('event:mercury_shutdown_switchover_complete', {
162
- url: webSocketUrl
163
- });
164
- if (oldSocket) {
165
- _this3.logger.info("".concat(_this3.namespace, ": [shutdown] old socket retained; server will close with 4001"));
166
- }
167
- }
168
- }
169
- }).then(function () {
170
- _this3.logger.info("".concat(_this3.namespace, ": [shutdown] switchover completed successfully"));
171
- }).catch(function (err) {
172
- _this3.logger.info("".concat(_this3.namespace, ": [shutdown] switchover exhausted retries; will fall back to normal reconnection"), err);
173
- _this3._emit('event:mercury_shutdown_switchover_failed', {
174
- reason: err
175
- });
176
- // Old socket will eventually close with 4001, triggering normal reconnection
177
- });
178
- } catch (e) {
179
- this.logger.error("".concat(this.namespace, ": [shutdown] error during switchover"), e);
180
- this._shutdownSwitchoverInProgress = false;
181
- this._emit('event:mercury_shutdown_switchover_failed', {
182
- reason: e
183
- });
184
- }
185
- },
186
105
  /**
187
106
  * Get the last error.
188
107
  * @returns {any} The last error.
@@ -191,7 +110,7 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
191
110
  return this.lastError;
192
111
  },
193
112
  connect: function connect(webSocketUrl) {
194
- var _this4 = this;
113
+ var _this2 = this;
195
114
  if (this.connected) {
196
115
  this.logger.info("".concat(this.namespace, ": already connected, will not connect again"));
197
116
  return _promise.default.resolve();
@@ -200,8 +119,8 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
200
119
  this.logger.info("".concat(this.namespace, ": starting connection attempt"));
201
120
  this.logger.info("".concat(this.namespace, ": debug_mercury_logging stack: "), new Error('debug_mercury_logging').stack);
202
121
  return _promise.default.resolve(this.webex.internal.device.registered || this.webex.internal.device.register()).then(function () {
203
- _this4.logger.info("".concat(_this4.namespace, ": connecting"));
204
- return _this4._connectWithBackoff(webSocketUrl);
122
+ _this2.logger.info("".concat(_this2.namespace, ": connecting"));
123
+ return _this2._connectWithBackoff(webSocketUrl);
205
124
  });
206
125
  },
207
126
  logout: function logout() {
@@ -213,20 +132,16 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
213
132
  } : undefined);
214
133
  },
215
134
  disconnect: function disconnect(options) {
216
- var _this5 = this;
135
+ var _this3 = this;
217
136
  return new _promise.default(function (resolve) {
218
- if (_this5.backoffCall) {
219
- _this5.logger.info("".concat(_this5.namespace, ": aborting connection"));
220
- _this5.backoffCall.abort();
137
+ if (_this3.backoffCall) {
138
+ _this3.logger.info("".concat(_this3.namespace, ": aborting connection"));
139
+ _this3.backoffCall.abort();
221
140
  }
222
- if (_this5._shutdownSwitchoverBackoffCall) {
223
- _this5.logger.info("".concat(_this5.namespace, ": aborting shutdown switchover"));
224
- _this5._shutdownSwitchoverBackoffCall.abort();
225
- }
226
- if (_this5.socket) {
227
- _this5.socket.removeAllListeners('message');
228
- _this5.once('offline', resolve);
229
- resolve(_this5.socket.close(options || undefined));
141
+ if (_this3.socket) {
142
+ _this3.socket.removeAllListeners('message');
143
+ _this3.once('offline', resolve);
144
+ resolve(_this3.socket.close(options || undefined));
230
145
  }
231
146
  resolve();
232
147
  });
@@ -252,19 +167,19 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
252
167
  });
253
168
  },
254
169
  _prepareUrl: function _prepareUrl(webSocketUrl) {
255
- var _this6 = this;
170
+ var _this4 = this;
256
171
  if (!webSocketUrl) {
257
172
  webSocketUrl = this.webex.internal.device.webSocketUrl;
258
173
  }
259
174
  return this.webex.internal.feature.getFeature('developer', 'web-high-availability').then(function (haMessagingEnabled) {
260
175
  if (haMessagingEnabled) {
261
- return _this6.webex.internal.services.convertUrlToPriorityHostUrl(webSocketUrl);
176
+ return _this4.webex.internal.services.convertUrlToPriorityHostUrl(webSocketUrl);
262
177
  }
263
178
  return webSocketUrl;
264
179
  }).then(function (wsUrl) {
265
180
  webSocketUrl = wsUrl;
266
181
  }).then(function () {
267
- return _this6.webex.internal.feature.getFeature('developer', 'web-shared-mercury');
182
+ return _this4.webex.internal.feature.getFeature('developer', 'web-shared-mercury');
268
183
  }).then(function (webSharedMercury) {
269
184
  webSocketUrl = _url.default.parse(webSocketUrl, true);
270
185
  (0, _assign.default)(webSocketUrl.query, {
@@ -279,7 +194,7 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
279
194
  });
280
195
  (0, _deleteProperty.default)(webSocketUrl.query, 'bufferStates');
281
196
  }
282
- if ((0, _lodash.get)(_this6, 'webex.config.device.ephemeral', false)) {
197
+ if ((0, _lodash.get)(_this4, 'webex.config.device.ephemeral', false)) {
283
198
  webSocketUrl.query.multipleConnections = true;
284
199
  }
285
200
  webSocketUrl.query.clientTimestamp = (0, _now.default)();
@@ -287,94 +202,96 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
287
202
  });
288
203
  },
289
204
  _attemptConnection: function _attemptConnection(socketUrl, callback) {
290
- var _this7 = this;
291
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
292
- var _options$isShutdownSw = options.isShutdownSwitchover,
293
- isShutdownSwitchover = _options$isShutdownSw === void 0 ? false : _options$isShutdownSw,
294
- _options$onSuccess = options.onSuccess,
295
- onSuccess = _options$onSuccess === void 0 ? null : _options$onSuccess;
205
+ var _this5 = this;
296
206
  var socket = new _socket.default();
297
- var newWSUrl;
298
- this._attachSocketEventListeners(socket);
299
-
300
- // Check appropriate backoff call based on connection type
301
- if (isShutdownSwitchover && !this._shutdownSwitchoverBackoffCall) {
302
- var msg = "".concat(this.namespace, ": prevent socket open when switchover backoff call no longer defined");
303
- var err = new Error(msg);
304
- this.logger.info(msg);
305
-
306
- // Call the callback with the error before rejecting
307
- callback(err);
308
- return _promise.default.reject(err);
309
- }
310
- if (!isShutdownSwitchover && !this.backoffCall) {
311
- var _msg = "".concat(this.namespace, ": prevent socket open when backoffCall no longer defined");
312
- var _err = new Error(_msg);
313
- this.logger.info(_msg);
314
-
315
- // Call the callback with the error before rejecting
316
- callback(_err);
317
- return _promise.default.reject(_err);
318
- }
319
-
320
- // For shutdown switchover, don't set socket yet (make-before-break)
321
- // For normal connection, set socket before opening to allow disconnect() to close it
322
- if (!isShutdownSwitchover) {
323
- this.socket = socket;
324
- }
325
- return this._prepareAndOpenSocket(socket, socketUrl, isShutdownSwitchover).then(function (webSocketUrl) {
326
- newWSUrl = webSocketUrl;
327
- _this7.logger.info("".concat(_this7.namespace, ": ").concat(isShutdownSwitchover ? '[shutdown] switchover' : '', " connected to mercury, success, action: connected, url: ").concat(newWSUrl));
207
+ var attemptWSUrl;
208
+ socket.on('close', function () {
209
+ return _this5._onclose.apply(_this5, arguments);
210
+ });
211
+ socket.on('message', function () {
212
+ return _this5._onmessage.apply(_this5, arguments);
213
+ });
214
+ socket.on('pong', function () {
215
+ return _this5._setTimeOffset.apply(_this5, arguments);
216
+ });
217
+ socket.on('sequence-mismatch', function () {
218
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
219
+ args[_key] = arguments[_key];
220
+ }
221
+ return _this5._emit.apply(_this5, ['sequence-mismatch'].concat(args));
222
+ });
223
+ socket.on('ping-pong-latency', function () {
224
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
225
+ args[_key2] = arguments[_key2];
226
+ }
227
+ return _this5._emit.apply(_this5, ['ping-pong-latency'].concat(args));
228
+ });
229
+ _promise.default.all([this._prepareUrl(socketUrl), this.webex.credentials.getUserToken()]).then(function (_ref) {
230
+ var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
231
+ webSocketUrl = _ref2[0],
232
+ token = _ref2[1];
233
+ if (!_this5.backoffCall) {
234
+ var msg = "".concat(_this5.namespace, ": prevent socket open when backoffCall no longer defined");
235
+ _this5.logger.info(msg);
236
+ return _promise.default.reject(new Error(msg));
237
+ }
238
+ attemptWSUrl = webSocketUrl;
239
+ var options = {
240
+ forceCloseDelay: _this5.config.forceCloseDelay,
241
+ pingInterval: _this5.config.pingInterval,
242
+ pongTimeout: _this5.config.pongTimeout,
243
+ token: token.toString(),
244
+ trackingId: "".concat(_this5.webex.sessionId, "_").concat((0, _now.default)()),
245
+ logger: _this5.logger
246
+ };
328
247
 
329
- // Custom success handler for shutdown switchover
330
- if (onSuccess) {
331
- onSuccess(socket, webSocketUrl);
332
- callback();
333
- return _promise.default.resolve();
248
+ // if the consumer has supplied request options use them
249
+ if (_this5.webex.config.defaultMercuryOptions) {
250
+ _this5.logger.info("".concat(_this5.namespace, ": setting custom options"));
251
+ options = _objectSpread(_objectSpread({}, options), _this5.webex.config.defaultMercuryOptions);
334
252
  }
335
253
 
336
- // Default behavior for normal connection
254
+ // Set the socket before opening it. This allows a disconnect() to close
255
+ // the socket if it is in the process of being opened.
256
+ _this5.socket = socket;
257
+ _this5.logger.info("".concat(_this5.namespace, " connection url: ").concat(webSocketUrl));
258
+ return socket.open(webSocketUrl, options);
259
+ }).then(function () {
260
+ _this5.logger.info("".concat(_this5.namespace, ": connected to mercury, success, action: connected, url: ").concat(attemptWSUrl));
337
261
  callback();
338
- return _this7.webex.internal.feature.getFeature('developer', 'web-high-availability').then(function (haMessagingEnabled) {
262
+ return _this5.webex.internal.feature.getFeature('developer', 'web-high-availability').then(function (haMessagingEnabled) {
339
263
  if (haMessagingEnabled) {
340
- return _this7.webex.internal.device.refresh();
264
+ return _this5.webex.internal.device.refresh();
341
265
  }
342
266
  return _promise.default.resolve();
343
267
  });
344
268
  }).catch(function (reason) {
345
- var _this7$backoffCall, _this7$backoffCall3;
346
- // For shutdown, simpler error handling - just callback for retry
347
- if (isShutdownSwitchover) {
348
- _this7.logger.info("".concat(_this7.namespace, ": [shutdown] switchover attempt failed"), reason);
349
- return callback(reason);
350
- }
351
-
352
- // Normal connection error handling (existing complex logic)
353
- _this7.lastError = reason; // remember the last error
269
+ var _this5$backoffCall, _this5$backoffCall3;
270
+ _this5.lastError = reason; // remember the last error
354
271
 
355
272
  // Suppress connection errors that appear to be network related. This
356
273
  // may end up suppressing metrics during outages, but we might not care
357
274
  // (especially since many of our outages happen in a way that client
358
275
  // metrics can't be trusted).
359
- if (reason.code !== 1006 && _this7.backoffCall && ((_this7$backoffCall = _this7.backoffCall) === null || _this7$backoffCall === void 0 ? void 0 : _this7$backoffCall.getNumRetries()) > 0) {
360
- var _this7$backoffCall2;
361
- _this7._emit('connection_failed', reason, {
362
- retries: (_this7$backoffCall2 = _this7.backoffCall) === null || _this7$backoffCall2 === void 0 ? void 0 : _this7$backoffCall2.getNumRetries()
276
+ if (reason.code !== 1006 && _this5.backoffCall && ((_this5$backoffCall = _this5.backoffCall) === null || _this5$backoffCall === void 0 ? void 0 : _this5$backoffCall.getNumRetries()) > 0) {
277
+ var _this5$backoffCall2;
278
+ _this5._emit('connection_failed', reason, {
279
+ retries: (_this5$backoffCall2 = _this5.backoffCall) === null || _this5$backoffCall2 === void 0 ? void 0 : _this5$backoffCall2.getNumRetries()
363
280
  });
364
281
  }
365
- _this7.logger.info("".concat(_this7.namespace, ": connection attempt failed"), reason, ((_this7$backoffCall3 = _this7.backoffCall) === null || _this7$backoffCall3 === void 0 ? void 0 : _this7$backoffCall3.getNumRetries()) === 0 ? reason.stack : '');
282
+ _this5.logger.info("".concat(_this5.namespace, ": connection attempt failed"), reason, ((_this5$backoffCall3 = _this5.backoffCall) === null || _this5$backoffCall3 === void 0 ? void 0 : _this5$backoffCall3.getNumRetries()) === 0 ? reason.stack : '');
366
283
  // UnknownResponse is produced by IE for any 4XXX; treated it like a bad
367
284
  // web socket url and let WDM handle the token checking
368
285
  if (reason instanceof _errors.UnknownResponse) {
369
- _this7.logger.info("".concat(_this7.namespace, ": received unknown response code, refreshing device registration"));
370
- return _this7.webex.internal.device.refresh().then(function () {
286
+ _this5.logger.info("".concat(_this5.namespace, ": received unknown response code, refreshing device registration"));
287
+ return _this5.webex.internal.device.refresh().then(function () {
371
288
  return callback(reason);
372
289
  });
373
290
  }
374
291
  // NotAuthorized implies expired token
375
292
  if (reason instanceof _errors.NotAuthorized) {
376
- _this7.logger.info("".concat(_this7.namespace, ": received authorization error, reauthorizing"));
377
- return _this7.webex.credentials.refresh({
293
+ _this5.logger.info("".concat(_this5.namespace, ": received authorization error, reauthorizing"));
294
+ return _this5.webex.credentials.refresh({
378
295
  force: true
379
296
  }).then(function () {
380
297
  return callback(reason);
@@ -389,15 +306,15 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
389
306
  // BadRequest implies current credentials are for a Service Account
390
307
  // Forbidden implies current user is not entitle for Webex
391
308
  if (reason instanceof _errors.BadRequest || reason instanceof _errors.Forbidden) {
392
- _this7.logger.warn("".concat(_this7.namespace, ": received unrecoverable response from mercury"));
393
- _this7.backoffCall.abort();
309
+ _this5.logger.warn("".concat(_this5.namespace, ": received unrecoverable response from mercury"));
310
+ _this5.backoffCall.abort();
394
311
  return callback(reason);
395
312
  }
396
313
  if (reason instanceof _errors.ConnectionError) {
397
- return _this7.webex.internal.feature.getFeature('developer', 'web-high-availability').then(function (haMessagingEnabled) {
314
+ return _this5.webex.internal.feature.getFeature('developer', 'web-high-availability').then(function (haMessagingEnabled) {
398
315
  if (haMessagingEnabled) {
399
- _this7.logger.info("".concat(_this7.namespace, ": received a generic connection error, will try to connect to another datacenter. failed, action: 'failed', url: ").concat(newWSUrl, " error: ").concat(reason.message));
400
- return _this7.webex.internal.services.markFailedUrl(newWSUrl);
316
+ _this5.logger.info("".concat(_this5.namespace, ": received a generic connection error, will try to connect to another datacenter. failed, action: 'failed', url: ").concat(attemptWSUrl, " error: ").concat(reason.message));
317
+ return _this5.webex.internal.services.markFailedUrl(attemptWSUrl);
401
318
  }
402
319
  return null;
403
320
  }).then(function () {
@@ -406,116 +323,63 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
406
323
  }
407
324
  return callback(reason);
408
325
  }).catch(function (reason) {
409
- _this7.logger.error("".concat(_this7.namespace, ": failed to handle connection failure"), reason);
326
+ _this5.logger.error("".concat(_this5.namespace, ": failed to handle connection failure"), reason);
410
327
  callback(reason);
411
328
  });
412
329
  },
413
- _prepareAndOpenSocket: function _prepareAndOpenSocket(socket, socketUrl) {
414
- var _this8 = this;
415
- var isShutdownSwitchover = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
416
- var logPrefix = isShutdownSwitchover ? '[shutdown] switchover' : 'connection';
417
- return _promise.default.all([this._prepareUrl(socketUrl), this.webex.credentials.getUserToken()]).then(function (_ref) {
418
- var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
419
- webSocketUrl = _ref2[0],
420
- token = _ref2[1];
421
- var options = {
422
- forceCloseDelay: _this8.config.forceCloseDelay,
423
- pingInterval: _this8.config.pingInterval,
424
- pongTimeout: _this8.config.pongTimeout,
425
- token: token.toString(),
426
- trackingId: "".concat(_this8.webex.sessionId, "_").concat((0, _now.default)()),
427
- logger: _this8.logger
428
- };
429
- if (_this8.webex.config.defaultMercuryOptions) {
430
- var customOptionsMsg = isShutdownSwitchover ? 'setting custom options for switchover' : 'setting custom options';
431
- _this8.logger.info("".concat(_this8.namespace, ": ").concat(customOptionsMsg));
432
- options = _objectSpread(_objectSpread({}, options), _this8.webex.config.defaultMercuryOptions);
433
- }
434
- _this8.logger.info("".concat(_this8.namespace, ": ").concat(logPrefix, " url: ").concat(webSocketUrl));
435
- return socket.open(webSocketUrl, options).then(function () {
436
- return webSocketUrl;
437
- });
438
- });
439
- },
440
330
  _connectWithBackoff: function _connectWithBackoff(webSocketUrl) {
441
- var _this9 = this;
442
- var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
443
- var _context$isShutdownSw = context.isShutdownSwitchover,
444
- isShutdownSwitchover = _context$isShutdownSw === void 0 ? false : _context$isShutdownSw,
445
- _context$attemptOptio = context.attemptOptions,
446
- attemptOptions = _context$attemptOptio === void 0 ? {} : _context$attemptOptio;
331
+ var _this6 = this;
447
332
  return new _promise.default(function (resolve, reject) {
448
333
  // eslint gets confused about whether or not call is actually used
449
334
  // eslint-disable-next-line prefer-const
450
335
  var call;
451
336
  var onComplete = function onComplete(err) {
452
- // Clear state flags based on connection type
453
- if (isShutdownSwitchover) {
454
- _this9._shutdownSwitchoverInProgress = false;
455
- _this9._shutdownSwitchoverBackoffCall = undefined;
456
- } else {
457
- _this9.connecting = false;
458
- _this9.backoffCall = undefined;
459
- }
337
+ _this6.connecting = false;
338
+ _this6.backoffCall = undefined;
460
339
  if (err) {
461
- var msg = isShutdownSwitchover ? "[shutdown] switchover failed after ".concat(call.getNumRetries(), " retries") : "failed to connect after ".concat(call.getNumRetries(), " retries");
462
- _this9.logger.info("".concat(_this9.namespace, ": ").concat(msg, "; log statement about next retry was inaccurate; ").concat(err));
340
+ _this6.logger.info("".concat(_this6.namespace, ": failed to connect after ").concat(call.getNumRetries(), " retries; log statement about next retry was inaccurate; ").concat(err));
463
341
  return reject(err);
464
342
  }
465
-
466
- // Default success handling for normal connections
467
- if (!isShutdownSwitchover) {
468
- _this9.connected = true;
469
- _this9.hasEverConnected = true;
470
- _this9._emit('online');
471
- _this9.webex.internal.newMetrics.callDiagnosticMetrics.setMercuryConnectedStatus(true);
472
- }
343
+ _this6.connected = true;
344
+ _this6.hasEverConnected = true;
345
+ _this6._emit('online');
346
+ _this6.webex.internal.newMetrics.callDiagnosticMetrics.setMercuryConnectedStatus(true);
473
347
  return resolve();
474
348
  };
475
349
 
476
350
  // eslint-disable-next-line prefer-reflect
477
351
  call = _backoff.default.call(function (callback) {
478
- var attemptNum = call.getNumRetries();
479
- var logPrefix = isShutdownSwitchover ? '[shutdown] switchover' : 'connection';
480
- _this9.logger.info("".concat(_this9.namespace, ": executing ").concat(logPrefix, " attempt ").concat(attemptNum));
481
- _this9._attemptConnection(webSocketUrl, callback, attemptOptions);
352
+ _this6.logger.info("".concat(_this6.namespace, ": executing connection attempt ").concat(call.getNumRetries()));
353
+ _this6._attemptConnection(webSocketUrl, callback);
482
354
  }, onComplete);
483
355
  call.setStrategy(new _backoff.default.ExponentialStrategy({
484
- initialDelay: _this9.config.backoffTimeReset,
485
- maxDelay: _this9.config.backoffTimeMax
356
+ initialDelay: _this6.config.backoffTimeReset,
357
+ maxDelay: _this6.config.backoffTimeMax
486
358
  }));
487
- if (_this9.config.initialConnectionMaxRetries && !_this9.hasEverConnected && !isShutdownSwitchover) {
488
- call.failAfter(_this9.config.initialConnectionMaxRetries);
489
- } else if (_this9.config.maxRetries) {
490
- call.failAfter(_this9.config.maxRetries);
359
+ if (_this6.config.initialConnectionMaxRetries && !_this6.hasEverConnected) {
360
+ call.failAfter(_this6.config.initialConnectionMaxRetries);
361
+ } else if (_this6.config.maxRetries) {
362
+ call.failAfter(_this6.config.maxRetries);
491
363
  }
492
364
  call.on('abort', function () {
493
- var msg = isShutdownSwitchover ? 'Shutdown Switchover' : 'Connection';
494
- _this9.logger.info("".concat(_this9.namespace, ": ").concat(msg, " aborted"));
495
- reject(new Error("Mercury ".concat(msg, " Aborted")));
365
+ _this6.logger.info("".concat(_this6.namespace, ": connection aborted"));
366
+ reject(new Error('Mercury Connection Aborted'));
496
367
  });
497
368
  call.on('callback', function (err) {
498
369
  if (err) {
499
370
  var number = call.getNumRetries();
500
- var delay = Math.min(call.strategy_.nextBackoffDelay_, _this9.config.backoffTimeMax);
501
- var logPrefix = isShutdownSwitchover ? '[shutdown] switchover' : '';
502
- _this9.logger.info("".concat(_this9.namespace, ": ").concat(logPrefix, " failed to connect; attempting retry ").concat(number + 1, " in ").concat(delay, " ms"));
371
+ var delay = Math.min(call.strategy_.nextBackoffDelay_, _this6.config.backoffTimeMax);
372
+ _this6.logger.info("".concat(_this6.namespace, ": failed to connect; attempting retry ").concat(number + 1, " in ").concat(delay, " ms"));
503
373
  /* istanbul ignore if */
504
374
  if (process.env.NODE_ENV === 'development') {
505
- _this9.logger.debug("".concat(_this9.namespace, ": "), err, err.stack);
375
+ _this6.logger.debug("".concat(_this6.namespace, ": "), err, err.stack);
506
376
  }
507
377
  return;
508
378
  }
509
- _this9.logger.info("".concat(_this9.namespace, ": connected"));
379
+ _this6.logger.info("".concat(_this6.namespace, ": connected"));
510
380
  });
511
-
512
- // Store backoff call reference BEFORE starting (so it's available in _attemptConnection)
513
- if (isShutdownSwitchover) {
514
- _this9._shutdownSwitchoverBackoffCall = call;
515
- } else {
516
- _this9.backoffCall = call;
517
- }
518
381
  call.start();
382
+ _this6.backoffCall = call;
519
383
  });
520
384
  },
521
385
  _emit: function _emit() {
@@ -546,74 +410,36 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
546
410
  }
547
411
  return handlers;
548
412
  },
549
- _onclose: function _onclose(event, sourceSocket) {
413
+ _onclose: function _onclose(event) {
550
414
  // I don't see any way to avoid the complexity or statement count in here.
551
415
  /* eslint complexity: [0] */
552
416
 
553
417
  try {
554
- var isActiveSocket = sourceSocket === this.socket;
555
418
  var reason = event.reason && event.reason.toLowerCase();
556
- var socketUrl;
557
- if (isActiveSocket && this.socket) {
558
- // Active socket closed - get URL from current socket reference
559
- socketUrl = this.socket.url;
560
- } else if (sourceSocket) {
561
- // Old socket closed - get URL from the closed socket
562
- socketUrl = sourceSocket.url;
563
- }
564
- if (isActiveSocket) {
565
- // Only tear down state if the currently active socket closed
566
- if (this.socket) {
567
- this.socket.removeAllListeners();
568
- }
569
- this.unset('socket');
570
- this.connected = false;
571
- this._emit('offline', event);
572
- this.webex.internal.newMetrics.callDiagnosticMetrics.setMercuryConnectedStatus(false);
573
- } else {
574
- // Old socket closed; do not flip connection state
575
- this.logger.info("".concat(this.namespace, ": [shutdown] non-active socket closed, code=").concat(event.code));
576
- // Clean up listeners from old socket now that it's closed
577
- if (sourceSocket) {
578
- sourceSocket.removeAllListeners();
579
- }
580
- }
419
+ var socketUrl = this.socket.url;
420
+ this.socket.removeAllListeners();
421
+ this.unset('socket');
422
+ this.connected = false;
423
+ this._emit('offline', event);
424
+ this.webex.internal.newMetrics.callDiagnosticMetrics.setMercuryConnectedStatus(false);
581
425
  switch (event.code) {
582
426
  case 1003:
583
427
  // metric: disconnect
584
428
  this.logger.info("".concat(this.namespace, ": Mercury service rejected last message; will not reconnect: ").concat(event.reason));
585
- if (isActiveSocket) this._emit('offline.permanent', event);
429
+ this._emit('offline.permanent', event);
586
430
  break;
587
431
  case 4000:
588
432
  // metric: disconnect
589
433
  this.logger.info("".concat(this.namespace, ": socket replaced; will not reconnect"));
590
- if (isActiveSocket) this._emit('offline.replaced', event);
591
- // If not active, nothing to do
592
- break;
593
- case 4001:
594
- // replaced during shutdown
595
- if (isActiveSocket) {
596
- // Server closed active socket with 4001, meaning it expected this connection
597
- // to be replaced, but the switchover in _handleImminentShutdown failed.
598
- // This is a permanent failure - do not reconnect.
599
- this.logger.warn("".concat(this.namespace, ": active socket closed with 4001; shutdown switchover failed"));
600
- this._emit('offline.permanent', event);
601
- } else {
602
- // Expected: old socket closed after successful switchover
603
- this.logger.info("".concat(this.namespace, ": old socket closed with 4001 (replaced during shutdown); no reconnect needed"));
604
- this._emit('offline.replaced', event);
605
- }
434
+ this._emit('offline.replaced', event);
606
435
  break;
607
436
  case 1001:
608
437
  case 1005:
609
438
  case 1006:
610
439
  case 1011:
611
440
  this.logger.info("".concat(this.namespace, ": socket disconnected; reconnecting"));
612
- if (isActiveSocket) {
613
- this._emit('offline.transient', event);
614
- this.logger.info("".concat(this.namespace, ": [shutdown] reconnecting active socket to recover"));
615
- this._reconnect(socketUrl);
616
- }
441
+ this._emit('offline.transient', event);
442
+ this._reconnect(socketUrl);
617
443
  // metric: disconnect
618
444
  // if (code == 1011 && reason !== ping error) metric: unexpected disconnect
619
445
  break;
@@ -622,42 +448,31 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
622
448
  // 3050 indicates logout form of closure, default to old behavior, use config reason defined by consumer to proceed with the permanent block
623
449
  if (normalReconnectReasons.includes(reason)) {
624
450
  this.logger.info("".concat(this.namespace, ": socket disconnected; reconnecting"));
625
- if (isActiveSocket) {
626
- this._emit('offline.transient', event);
627
- this.logger.info("".concat(this.namespace, ": [shutdown] reconnecting due to normal close"));
628
- this._reconnect(socketUrl);
629
- }
451
+ this._emit('offline.transient', event);
452
+ this._reconnect(socketUrl);
630
453
  // metric: disconnect
631
454
  // if (reason === done forced) metric: force closure
632
455
  } else {
633
456
  this.logger.info("".concat(this.namespace, ": socket disconnected; will not reconnect: ").concat(event.reason));
634
- if (isActiveSocket) this._emit('offline.permanent', event);
457
+ this._emit('offline.permanent', event);
635
458
  }
636
459
  break;
637
460
  default:
638
461
  this.logger.info("".concat(this.namespace, ": socket disconnected unexpectedly; will not reconnect"));
639
462
  // unexpected disconnect
640
- if (isActiveSocket) this._emit('offline.permanent', event);
463
+ this._emit('offline.permanent', event);
641
464
  }
642
465
  } catch (error) {
643
466
  this.logger.error("".concat(this.namespace, ": error occurred in close handler"), error);
644
467
  }
645
468
  },
646
469
  _onmessage: function _onmessage(event) {
647
- var _this10 = this;
470
+ var _this7 = this;
648
471
  this._setTimeOffset(event);
649
472
  var envelope = event.data;
650
473
  if (process.env.ENABLE_MERCURY_LOGGING) {
651
474
  this.logger.debug("".concat(this.namespace, ": message envelope: "), envelope);
652
475
  }
653
-
654
- // Handle shutdown message shape: { type: 'shutdown' }
655
- if (envelope && envelope.type === 'shutdown') {
656
- this.logger.info("".concat(this.namespace, ": [shutdown] imminent shutdown message received"));
657
- this._emit('event:mercury_shutdown_imminent', envelope);
658
- this._handleImminentShutdown();
659
- return _promise.default.resolve();
660
- }
661
476
  var data = envelope.data;
662
477
  this._applyOverrides(data);
663
478
  return this._getEventHandlers(data.eventType).reduce(function (promise, handler) {
@@ -665,24 +480,24 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
665
480
  var namespace = handler.namespace,
666
481
  name = handler.name;
667
482
  return new _promise.default(function (resolve) {
668
- return resolve((_this10.webex[namespace] || _this10.webex.internal[namespace])[name](data));
483
+ return resolve((_this7.webex[namespace] || _this7.webex.internal[namespace])[name](data));
669
484
  }).catch(function (reason) {
670
- return _this10.logger.error("".concat(_this10.namespace, ": error occurred in autowired event handler for ").concat(data.eventType), reason);
485
+ return _this7.logger.error("".concat(_this7.namespace, ": error occurred in autowired event handler for ").concat(data.eventType), reason);
671
486
  });
672
487
  });
673
488
  }, _promise.default.resolve()).then(function () {
674
- _this10._emit('event', event.data);
489
+ _this7._emit('event', event.data);
675
490
  var _data$eventType$split = data.eventType.split('.'),
676
491
  _data$eventType$split2 = (0, _slicedToArray2.default)(_data$eventType$split, 1),
677
492
  namespace = _data$eventType$split2[0];
678
493
  if (namespace === data.eventType) {
679
- _this10._emit("event:".concat(namespace), envelope);
494
+ _this7._emit("event:".concat(namespace), envelope);
680
495
  } else {
681
- _this10._emit("event:".concat(namespace), envelope);
682
- _this10._emit("event:".concat(data.eventType), envelope);
496
+ _this7._emit("event:".concat(namespace), envelope);
497
+ _this7._emit("event:".concat(data.eventType), envelope);
683
498
  }
684
499
  }).catch(function (reason) {
685
- _this10.logger.error("".concat(_this10.namespace, ": error occurred processing socket message"), reason);
500
+ _this7.logger.error("".concat(_this7.namespace, ": error occurred processing socket message"), reason);
686
501
  });
687
502
  },
688
503
  _setTimeOffset: function _setTimeOffset(event) {
@@ -695,7 +510,7 @@ var Mercury = _webexCore.WebexPlugin.extend((_dec = (0, _common.deprecated)('Mer
695
510
  this.logger.info("".concat(this.namespace, ": reconnecting"));
696
511
  return this.connect(webSocketUrl);
697
512
  },
698
- version: "3.10.0-next.3"
513
+ version: "3.10.0-wxc-disconnect.1"
699
514
  }, ((0, _applyDecoratedDescriptor2.default)(_obj, "connect", [_common.oneFlight], (0, _getOwnPropertyDescriptor.default)(_obj, "connect"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "disconnect", [_common.oneFlight], (0, _getOwnPropertyDescriptor.default)(_obj, "disconnect"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "listen", [_dec], (0, _getOwnPropertyDescriptor.default)(_obj, "listen"), _obj), (0, _applyDecoratedDescriptor2.default)(_obj, "stopListening", [_dec2], (0, _getOwnPropertyDescriptor.default)(_obj, "stopListening"), _obj)), _obj)));
700
515
  var _default = exports.default = Mercury;
701
516
  //# sourceMappingURL=mercury.js.map