@webex/internal-plugin-mercury 3.0.0-bnr.4 → 3.0.0

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.
@@ -32,6 +32,5 @@ _socketBase.default.getWebSocketConstructor = function getWebSocketConstructor()
32
32
  }
33
33
  return ws;
34
34
  };
35
- var _default = _socketBase.default;
36
- exports.default = _default;
35
+ var _default = exports.default = _socketBase.default;
37
36
  //# sourceMappingURL=socket.shim.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["Socket","getWebSocketConstructor","ws","WebSocket","MozWebSocket","global","window","self"],"sources":["socket.shim.js"],"sourcesContent":["/* eslint-disable no-restricted-globals */\n\n/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\n/* eslint-env browser */\n\nimport Socket from './socket-base';\n\nSocket.getWebSocketConstructor = function getWebSocketConstructor() {\n // Grabed from https://github.com/heineiuo/isomorphic-ws/blob/9b977394ac875638c045fd9cf774ed418484b394/browser.js\n let ws;\n\n if (typeof WebSocket !== 'undefined') {\n ws = WebSocket;\n } else if (typeof MozWebSocket !== 'undefined') {\n // eslint-disable-next-line no-undef\n ws = MozWebSocket;\n } else if (typeof global !== 'undefined') {\n ws = global.WebSocket || global.MozWebSocket;\n } else if (typeof window !== 'undefined') {\n ws = window.WebSocket || window.MozWebSocket;\n } else if (typeof self !== 'undefined') {\n ws = self.WebSocket || self.MozWebSocket;\n }\n\n return ws;\n};\n\nexport default Socket;\n"],"mappings":";;;;;;;;AAQA;AARA;;AAEA;AACA;AACA;;AAEA;;AAIAA,mBAAM,CAACC,uBAAuB,GAAG,SAASA,uBAAuB,GAAG;EAClE;EACA,IAAIC,EAAE;EAEN,IAAI,OAAOC,SAAS,KAAK,WAAW,EAAE;IACpCD,EAAE,GAAGC,SAAS;EAChB,CAAC,MAAM,IAAI,OAAOC,YAAY,KAAK,WAAW,EAAE;IAC9C;IACAF,EAAE,GAAGE,YAAY;EACnB,CAAC,MAAM,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;IACxCH,EAAE,GAAGG,MAAM,CAACF,SAAS,IAAIE,MAAM,CAACD,YAAY;EAC9C,CAAC,MAAM,IAAI,OAAOE,MAAM,KAAK,WAAW,EAAE;IACxCJ,EAAE,GAAGI,MAAM,CAACH,SAAS,IAAIG,MAAM,CAACF,YAAY;EAC9C,CAAC,MAAM,IAAI,OAAOG,IAAI,KAAK,WAAW,EAAE;IACtCL,EAAE,GAAGK,IAAI,CAACJ,SAAS,IAAII,IAAI,CAACH,YAAY;EAC1C;EAEA,OAAOF,EAAE;AACX,CAAC;AAAC,eAEaF,mBAAM;AAAA"}
1
+ {"version":3,"names":["_socketBase","_interopRequireDefault","require","Socket","getWebSocketConstructor","ws","WebSocket","MozWebSocket","global","window","self","_default","exports","default"],"sources":["socket.shim.js"],"sourcesContent":["/* eslint-disable no-restricted-globals */\n\n/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\n/* eslint-env browser */\n\nimport Socket from './socket-base';\n\nSocket.getWebSocketConstructor = function getWebSocketConstructor() {\n // Grabed from https://github.com/heineiuo/isomorphic-ws/blob/9b977394ac875638c045fd9cf774ed418484b394/browser.js\n let ws;\n\n if (typeof WebSocket !== 'undefined') {\n ws = WebSocket;\n } else if (typeof MozWebSocket !== 'undefined') {\n // eslint-disable-next-line no-undef\n ws = MozWebSocket;\n } else if (typeof global !== 'undefined') {\n ws = global.WebSocket || global.MozWebSocket;\n } else if (typeof window !== 'undefined') {\n ws = window.WebSocket || window.MozWebSocket;\n } else if (typeof self !== 'undefined') {\n ws = self.WebSocket || self.MozWebSocket;\n }\n\n return ws;\n};\n\nexport default Socket;\n"],"mappings":";;;;;;;;AAQA,IAAAA,WAAA,GAAAC,sBAAA,CAAAC,OAAA;AARA;;AAEA;AACA;AACA;;AAEA;;AAIAC,mBAAM,CAACC,uBAAuB,GAAG,SAASA,uBAAuBA,CAAA,EAAG;EAClE;EACA,IAAIC,EAAE;EAEN,IAAI,OAAOC,SAAS,KAAK,WAAW,EAAE;IACpCD,EAAE,GAAGC,SAAS;EAChB,CAAC,MAAM,IAAI,OAAOC,YAAY,KAAK,WAAW,EAAE;IAC9C;IACAF,EAAE,GAAGE,YAAY;EACnB,CAAC,MAAM,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;IACxCH,EAAE,GAAGG,MAAM,CAACF,SAAS,IAAIE,MAAM,CAACD,YAAY;EAC9C,CAAC,MAAM,IAAI,OAAOE,MAAM,KAAK,WAAW,EAAE;IACxCJ,EAAE,GAAGI,MAAM,CAACH,SAAS,IAAIG,MAAM,CAACF,YAAY;EAC9C,CAAC,MAAM,IAAI,OAAOG,IAAI,KAAK,WAAW,EAAE;IACtCL,EAAE,GAAGK,IAAI,CAACJ,SAAS,IAAII,IAAI,CAACH,YAAY;EAC1C;EAEA,OAAOF,EAAE;AACX,CAAC;AAAC,IAAAM,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaV,mBAAM"}
package/jest.config.js ADDED
@@ -0,0 +1,3 @@
1
+ const config = require('@webex/jest-config-legacy');
2
+
3
+ module.exports = config;
package/package.json CHANGED
@@ -1,6 +1,5 @@
1
1
  {
2
2
  "name": "@webex/internal-plugin-mercury",
3
- "version": "3.0.0-bnr.4",
4
3
  "description": "",
5
4
  "license": "MIT",
6
5
  "main": "dist/index.js",
@@ -24,26 +23,46 @@
24
23
  ]
25
24
  },
26
25
  "devDependencies": {
26
+ "@babel/core": "^7.17.10",
27
27
  "@sinonjs/fake-timers": "^6.0.1",
28
+ "@webex/babel-config-legacy": "0.0.0",
29
+ "@webex/eslint-config-legacy": "0.0.0",
30
+ "@webex/jest-config-legacy": "0.0.0",
31
+ "@webex/legacy-tools": "0.0.0",
32
+ "@webex/test-helper-chai": "3.0.0",
33
+ "@webex/test-helper-mocha": "3.0.0",
34
+ "@webex/test-helper-mock-webex": "3.0.0",
35
+ "@webex/test-helper-test-users": "3.0.0",
36
+ "eslint": "^8.24.0",
37
+ "prettier": "^2.7.1",
28
38
  "sinon": "^9.2.4"
29
39
  },
30
40
  "dependencies": {
31
- "@webex/common": "workspace:^",
32
- "@webex/common-timers": "workspace:^",
33
- "@webex/internal-plugin-device": "workspace:^",
34
- "@webex/internal-plugin-feature": "workspace:^",
35
- "@webex/internal-plugin-mercury": "workspace:^",
36
- "@webex/internal-plugin-metrics": "workspace:^",
37
- "@webex/test-helper-chai": "workspace:^",
38
- "@webex/test-helper-mocha": "workspace:^",
39
- "@webex/test-helper-mock-web-socket": "workspace:^",
40
- "@webex/test-helper-mock-webex": "workspace:^",
41
- "@webex/test-helper-refresh-callback": "workspace:^",
42
- "@webex/test-helper-test-users": "workspace:^",
43
- "@webex/webex-core": "workspace:^",
41
+ "@webex/common": "3.0.0",
42
+ "@webex/common-timers": "3.0.0",
43
+ "@webex/internal-plugin-device": "3.0.0",
44
+ "@webex/internal-plugin-feature": "3.0.0",
45
+ "@webex/internal-plugin-metrics": "3.0.0",
46
+ "@webex/test-helper-chai": "3.0.0",
47
+ "@webex/test-helper-mocha": "3.0.0",
48
+ "@webex/test-helper-mock-web-socket": "3.0.0",
49
+ "@webex/test-helper-mock-webex": "3.0.0",
50
+ "@webex/test-helper-refresh-callback": "3.0.0",
51
+ "@webex/test-helper-test-users": "3.0.0",
52
+ "@webex/webex-core": "3.0.0",
44
53
  "backoff": "^2.5.0",
45
54
  "lodash": "^4.17.21",
46
55
  "uuid": "^3.3.2",
47
56
  "ws": "^8.2.2"
48
- }
49
- }
57
+ },
58
+ "scripts": {
59
+ "build": "yarn build:src",
60
+ "build:src": "webex-legacy-tools build -dest \"./dist\" -src \"./src\" -js -ts -maps",
61
+ "deploy:npm": "yarn npm publish",
62
+ "test": "yarn test:style && yarn test:unit && yarn test:integration && yarn test:browser",
63
+ "test:browser": "webex-legacy-tools test --integration --unit --runner karma",
64
+ "test:style": "eslint ./src/**/*.*",
65
+ "test:unit": "webex-legacy-tools test --unit --runner jest"
66
+ },
67
+ "version": "3.0.0"
68
+ }
package/process ADDED
@@ -0,0 +1 @@
1
+ module.exports = {browser: true};
package/src/mercury.js CHANGED
@@ -49,7 +49,7 @@ const Mercury = WebexPlugin.extend({
49
49
  @oneFlight
50
50
  connect(webSocketUrl) {
51
51
  if (this.connected) {
52
- this.logger.info('mercury: already connected, will not connect again');
52
+ this.logger.info(`${this.namespace}: already connected, will not connect again`);
53
53
 
54
54
  return Promise.resolve();
55
55
  }
@@ -59,7 +59,7 @@ const Mercury = WebexPlugin.extend({
59
59
  return Promise.resolve(
60
60
  this.webex.internal.device.registered || this.webex.internal.device.register()
61
61
  ).then(() => {
62
- this.logger.info('mercury: connecting');
62
+ this.logger.info(`${this.namespace}: connecting`);
63
63
 
64
64
  return this._connectWithBackoff(webSocketUrl);
65
65
  });
@@ -69,16 +69,14 @@ const Mercury = WebexPlugin.extend({
69
69
  disconnect() {
70
70
  return new Promise((resolve) => {
71
71
  if (this.backoffCall) {
72
- this.logger.info('mercury: aborting connection');
72
+ this.logger.info(`${this.namespace}: aborting connection`);
73
73
  this.backoffCall.abort();
74
74
  }
75
75
 
76
76
  if (this.socket) {
77
77
  this.socket.removeAllListeners('message');
78
78
  this.once('offline', resolve);
79
- this.socket.close();
80
-
81
- return;
79
+ resolve(this.socket.close());
82
80
  }
83
81
 
84
82
  resolve();
@@ -161,11 +159,12 @@ const Mercury = WebexPlugin.extend({
161
159
  socket.on('close', (...args) => this._onclose(...args));
162
160
  socket.on('message', (...args) => this._onmessage(...args));
163
161
  socket.on('sequence-mismatch', (...args) => this._emit('sequence-mismatch', ...args));
162
+ socket.on('ping-pong-latency', (...args) => this._emit('ping-pong-latency', ...args));
164
163
 
165
164
  Promise.all([this._prepareUrl(socketUrl), this.webex.credentials.getUserToken()])
166
165
  .then(([webSocketUrl, token]) => {
167
166
  if (!this.backoffCall) {
168
- const msg = 'mercury: prevent socket open when backoffCall no longer defined';
167
+ const msg = `${this.namespace}: prevent socket open when backoffCall no longer defined`;
169
168
 
170
169
  this.logger.info(msg);
171
170
 
@@ -185,7 +184,7 @@ const Mercury = WebexPlugin.extend({
185
184
 
186
185
  // if the consumer has supplied request options use them
187
186
  if (this.webex.config.defaultMercuryOptions) {
188
- this.logger.info('mercury: setting custom options');
187
+ this.logger.info(`${this.namespace}: setting custom options`);
189
188
  options = {...options, ...this.webex.config.defaultMercuryOptions};
190
189
  }
191
190
 
@@ -193,18 +192,14 @@ const Mercury = WebexPlugin.extend({
193
192
  // the socket if it is in the process of being opened.
194
193
  this.socket = socket;
195
194
 
195
+ this.logger.info(`${this.namespace} connection url: ${webSocketUrl}`);
196
+
196
197
  return socket.open(webSocketUrl, options);
197
198
  })
198
199
  .then(() => {
199
- this.webex.internal.metrics.submitClientMetrics('web-ha-mercury', {
200
- fields: {
201
- success: true,
202
- },
203
- tags: {
204
- action: 'connected',
205
- url: attemptWSUrl,
206
- },
207
- });
200
+ this.logger.info(
201
+ `${this.namespace}: connected to mercury, success, action: connected, url: ${attemptWSUrl}`
202
+ );
208
203
  callback();
209
204
 
210
205
  return this.webex.internal.feature
@@ -225,19 +220,19 @@ const Mercury = WebexPlugin.extend({
225
220
  if (reason.code !== 1006 && this.backoffCall && this.backoffCall.getNumRetries() > 0) {
226
221
  this._emit('connection_failed', reason, {retries: this.backoffCall.getNumRetries()});
227
222
  }
228
- this.logger.info('mercury: connection attempt failed', reason);
223
+ this.logger.info(`${this.namespace}: connection attempt failed`, reason);
229
224
  // UnknownResponse is produced by IE for any 4XXX; treated it like a bad
230
225
  // web socket url and let WDM handle the token checking
231
226
  if (reason instanceof UnknownResponse) {
232
227
  this.logger.info(
233
- 'mercury: received unknown response code, refreshing device registration'
228
+ `${this.namespace}: received unknown response code, refreshing device registration`
234
229
  );
235
230
 
236
231
  return this.webex.internal.device.refresh().then(() => callback(reason));
237
232
  }
238
233
  // NotAuthorized implies expired token
239
234
  if (reason instanceof NotAuthorized) {
240
- this.logger.info('mercury: received authorization error, reauthorizing');
235
+ this.logger.info(`${this.namespace}: received authorization error, reauthorizing`);
241
236
 
242
237
  return this.webex.credentials.refresh({force: true}).then(() => callback(reason));
243
238
  }
@@ -250,7 +245,7 @@ const Mercury = WebexPlugin.extend({
250
245
  // BadRequest implies current credentials are for a Service Account
251
246
  // Forbidden implies current user is not entitle for Webex
252
247
  if (reason instanceof BadRequest || reason instanceof Forbidden) {
253
- this.logger.warn('mercury: received unrecoverable response from mercury');
248
+ this.logger.warn(`${this.namespace}: received unrecoverable response from mercury`);
254
249
  this.backoffCall.abort();
255
250
 
256
251
  return callback(reason);
@@ -261,18 +256,8 @@ const Mercury = WebexPlugin.extend({
261
256
  .then((haMessagingEnabled) => {
262
257
  if (haMessagingEnabled) {
263
258
  this.logger.info(
264
- 'mercury: received a generic connection error, will try to connect to another datacenter'
259
+ `${this.namespace}: received a generic connection error, will try to connect to another datacenter. failed, action: 'failed', url: ${attemptWSUrl} error: ${reason.message}`
265
260
  );
266
- this.webex.internal.metrics.submitClientMetrics('web-ha-mercury', {
267
- fields: {
268
- success: false,
269
- },
270
- tags: {
271
- action: 'failed',
272
- error: reason.message,
273
- url: attemptWSUrl,
274
- },
275
- });
276
261
 
277
262
  return this.webex.internal.services.markFailedUrl(attemptWSUrl);
278
263
  }
@@ -285,7 +270,7 @@ const Mercury = WebexPlugin.extend({
285
270
  return callback(reason);
286
271
  })
287
272
  .catch((reason) => {
288
- this.logger.error('mercury: failed to handle connection failure', reason);
273
+ this.logger.error(`${this.namespace}: failed to handle connection failure`, reason);
289
274
  callback(reason);
290
275
  });
291
276
  },
@@ -301,7 +286,9 @@ const Mercury = WebexPlugin.extend({
301
286
  this.backoffCall = undefined;
302
287
  if (err) {
303
288
  this.logger.info(
304
- `mercury: failed to connect after ${call.getNumRetries()} retries; log statement about next retry was inaccurate; ${err}`
289
+ `${
290
+ this.namespace
291
+ }: failed to connect after ${call.getNumRetries()} retries; log statement about next retry was inaccurate; ${err}`
305
292
  );
306
293
 
307
294
  return reject(err);
@@ -314,7 +301,7 @@ const Mercury = WebexPlugin.extend({
314
301
 
315
302
  // eslint-disable-next-line prefer-reflect
316
303
  call = backoff.call((callback) => {
317
- this.logger.info(`mercury: executing connection attempt ${call.getNumRetries()}`);
304
+ this.logger.info(`${this.namespace}: executing connection attempt ${call.getNumRetries()}`);
318
305
  this._attemptConnection(webSocketUrl, callback);
319
306
  }, onComplete);
320
307
 
@@ -330,7 +317,7 @@ const Mercury = WebexPlugin.extend({
330
317
  }
331
318
 
332
319
  call.on('abort', () => {
333
- this.logger.info('mercury: connection aborted');
320
+ this.logger.info(`${this.namespace}: connection aborted`);
334
321
  reject(new Error('Mercury Connection Aborted'));
335
322
  });
336
323
 
@@ -340,16 +327,16 @@ const Mercury = WebexPlugin.extend({
340
327
  const delay = Math.min(call.strategy_.nextBackoffDelay_, this.config.backoffTimeMax);
341
328
 
342
329
  this.logger.info(
343
- `mercury: failed to connect; attempting retry ${number + 1} in ${delay} ms`
330
+ `${this.namespace}: failed to connect; attempting retry ${number + 1} in ${delay} ms`
344
331
  );
345
332
  /* istanbul ignore if */
346
333
  if (process.env.NODE_ENV === 'development') {
347
- this.logger.debug('mercury: ', err, err.stack);
334
+ this.logger.debug(`${this.namespace}: `, err, err.stack);
348
335
  }
349
336
 
350
337
  return;
351
338
  }
352
- this.logger.info('mercury: connected');
339
+ this.logger.info(`${this.namespace}: connected`);
353
340
  });
354
341
 
355
342
  call.start();
@@ -362,7 +349,10 @@ const Mercury = WebexPlugin.extend({
362
349
  try {
363
350
  this.trigger(...args);
364
351
  } catch (error) {
365
- this.logger.error('mercury: error occurred in event handler', error);
352
+ this.logger.error(`${this.namespace}: error occurred in event handler`, {
353
+ error,
354
+ arguments: args,
355
+ });
366
356
  }
367
357
  },
368
358
 
@@ -403,20 +393,20 @@ const Mercury = WebexPlugin.extend({
403
393
  case 1003:
404
394
  // metric: disconnect
405
395
  this.logger.info(
406
- `mercury: Mercury service rejected last message; will not reconnect: ${event.reason}`
396
+ `${this.namespace}: Mercury service rejected last message; will not reconnect: ${event.reason}`
407
397
  );
408
398
  this._emit('offline.permanent', event);
409
399
  break;
410
400
  case 4000:
411
401
  // metric: disconnect
412
- this.logger.info('mercury: socket replaced; will not reconnect');
402
+ this.logger.info(`${this.namespace}: socket replaced; will not reconnect`);
413
403
  this._emit('offline.replaced', event);
414
404
  break;
415
405
  case 1001:
416
406
  case 1005:
417
407
  case 1006:
418
408
  case 1011:
419
- this.logger.info('mercury: socket disconnected; reconnecting');
409
+ this.logger.info(`${this.namespace}: socket disconnected; reconnecting`);
420
410
  this._emit('offline.transient', event);
421
411
  this._reconnect(socketUrl);
422
412
  // metric: disconnect
@@ -424,23 +414,25 @@ const Mercury = WebexPlugin.extend({
424
414
  break;
425
415
  case 1000:
426
416
  if (normalReconnectReasons.includes(reason)) {
427
- this.logger.info('mercury: socket disconnected; reconnecting');
417
+ this.logger.info(`${this.namespace}: socket disconnected; reconnecting`);
428
418
  this._emit('offline.transient', event);
429
419
  this._reconnect(socketUrl);
430
420
  // metric: disconnect
431
421
  // if (reason === done forced) metric: force closure
432
422
  } else {
433
- this.logger.info('mercury: socket disconnected; will not reconnect');
423
+ this.logger.info(`${this.namespace}: socket disconnected; will not reconnect`);
434
424
  this._emit('offline.permanent', event);
435
425
  }
436
426
  break;
437
427
  default:
438
- this.logger.info('mercury: socket disconnected unexpectedly; will not reconnect');
428
+ this.logger.info(
429
+ `${this.namespace}: socket disconnected unexpectedly; will not reconnect`
430
+ );
439
431
  // unexpected disconnect
440
432
  this._emit('offline.permanent', event);
441
433
  }
442
434
  } catch (error) {
443
- this.logger.error('mercury: error occurred in close handler', error);
435
+ this.logger.error(`${this.namespace}: error occurred in close handler`, error);
444
436
  }
445
437
  },
446
438
 
@@ -448,7 +440,7 @@ const Mercury = WebexPlugin.extend({
448
440
  const envelope = event.data;
449
441
 
450
442
  if (process.env.ENABLE_MERCURY_LOGGING) {
451
- this.logger.debug('mercury: message envelope: ', envelope);
443
+ this.logger.debug(`${this.namespace}: message envelope: `, envelope);
452
444
  }
453
445
 
454
446
  const {data} = envelope;
@@ -465,7 +457,7 @@ const Mercury = WebexPlugin.extend({
465
457
  resolve((this.webex[namespace] || this.webex.internal[namespace])[name](data))
466
458
  ).catch((reason) =>
467
459
  this.logger.error(
468
- `mercury: error occurred in autowired event handler for ${data.eventType}`,
460
+ `${this.namespace}: error occurred in autowired event handler for ${data.eventType}`,
469
461
  reason
470
462
  )
471
463
  );
@@ -484,12 +476,12 @@ const Mercury = WebexPlugin.extend({
484
476
  }
485
477
  })
486
478
  .catch((reason) => {
487
- this.logger.error('mercury: error occurred processing socket message', reason);
479
+ this.logger.error(`${this.namespace}: error occurred processing socket message`, reason);
488
480
  });
489
481
  },
490
482
 
491
483
  _reconnect(webSocketUrl) {
492
- this.logger.info('mercury: reconnecting');
484
+ this.logger.info(`${this.namespace}: reconnecting`);
493
485
 
494
486
  return this.connect(webSocketUrl);
495
487
  },
@@ -30,6 +30,7 @@ export default class Socket extends EventEmitter {
30
30
  */
31
31
  constructor() {
32
32
  super();
33
+ this._domain = 'unknown-domain';
33
34
  this.onmessage = this.onmessage.bind(this);
34
35
  this.onclose = this.onclose.bind(this);
35
36
  }
@@ -111,10 +112,10 @@ export default class Socket extends EventEmitter {
111
112
  return;
112
113
  }
113
114
  // logger is defined once open is called
114
- this.logger.info('socket: closing');
115
+ this.logger.info(`socket,${this._domain}: closing`);
115
116
 
116
117
  if (socket.readyState === 2 || socket.readyState === 3) {
117
- this.logger.info('socket: already closed');
118
+ this.logger.info(`socket,${this._domain}: already closed`);
118
119
  resolve();
119
120
 
120
121
  return;
@@ -134,7 +135,7 @@ export default class Socket extends EventEmitter {
134
135
 
135
136
  const closeTimer = safeSetTimeout(() => {
136
137
  try {
137
- this.logger.info('socket: no close event received, forcing closure');
138
+ this.logger.info(`socket,${this._domain}: no close event received, forcing closure`);
138
139
  resolve(
139
140
  this.onclose({
140
141
  code: 1000,
@@ -142,12 +143,12 @@ export default class Socket extends EventEmitter {
142
143
  })
143
144
  );
144
145
  } catch (error) {
145
- this.logger.warn('socket: force-close failed', error);
146
+ this.logger.warn(`socket,${this._domain}: force-close failed`, error);
146
147
  }
147
148
  }, this.forceCloseDelay);
148
149
 
149
150
  socket.onclose = (event) => {
150
- this.logger.info('socket: close event fired', event.code, event.reason);
151
+ this.logger.info(`socket,${this._domain}: close event fired`, event.code, event.reason);
151
152
  clearTimeout(closeTimer);
152
153
  this.onclose(event);
153
154
  resolve(event);
@@ -171,6 +172,12 @@ export default class Socket extends EventEmitter {
171
172
  * @returns {Promise}
172
173
  */
173
174
  open(url, options) {
175
+ try {
176
+ this._domain = new URL(url).hostname;
177
+ } catch {
178
+ this._domain = url;
179
+ }
180
+
174
181
  return new Promise((resolve, reject) => {
175
182
  /* eslint complexity: [0] */
176
183
  if (!url) {
@@ -201,7 +208,7 @@ export default class Socket extends EventEmitter {
201
208
 
202
209
  const WebSocket = Socket.getWebSocketConstructor();
203
210
 
204
- this.logger.info('socket: creating WebSocket');
211
+ this.logger.info(`socket,${this._domain}: creating WebSocket`);
205
212
  const socket = new WebSocket(url, [], options);
206
213
 
207
214
  socket.binaryType = 'arraybuffer';
@@ -209,7 +216,7 @@ export default class Socket extends EventEmitter {
209
216
 
210
217
  socket.onclose = (event) => {
211
218
  event = this._fixCloseCode(event);
212
- this.logger.info('socket: closed before open', event.code, event.reason);
219
+ this.logger.info(`socket,${this._domain}: closed before open`, event.code, event.reason);
213
220
  switch (event.code) {
214
221
  case 1005:
215
222
  // IE 11 doesn't seem to allow 4XXX codes, so if we get a 1005, assume
@@ -231,10 +238,10 @@ export default class Socket extends EventEmitter {
231
238
  };
232
239
 
233
240
  socket.onopen = () => {
234
- this.logger.info('socket: connected');
241
+ this.logger.info(`socket,${this._domain}: connected`);
235
242
  this._authorize()
236
243
  .then(() => {
237
- this.logger.info('socket: authorized');
244
+ this.logger.info(`socket,${this._domain}: authorized`);
238
245
  socket.onclose = this.onclose;
239
246
  resolve();
240
247
  })
@@ -242,11 +249,11 @@ export default class Socket extends EventEmitter {
242
249
  };
243
250
 
244
251
  socket.onerror = (event) => {
245
- this.logger.warn('socket: error event fired', event);
252
+ this.logger.warn(`socket,${this._domain}: error event fired`, event);
246
253
  };
247
254
 
248
255
  sockets.set(this, socket);
249
- this.logger.info('socket: waiting for server');
256
+ this.logger.info(`socket,${this._domain}: waiting for server`);
250
257
  });
251
258
  }
252
259
 
@@ -256,7 +263,7 @@ export default class Socket extends EventEmitter {
256
263
  * @returns {undefined}
257
264
  */
258
265
  onclose(event) {
259
- this.logger.info('socket: closed', event.code, event.reason);
266
+ this.logger.info(`socket,${this._domain}: closed`, event.code, event.reason);
260
267
  clearTimeout(this.pongTimer);
261
268
  clearTimeout(this.pingTimer);
262
269
 
@@ -278,10 +285,10 @@ export default class Socket extends EventEmitter {
278
285
  const data = JSON.parse(event.data);
279
286
  const sequenceNumber = parseInt(data.sequenceNumber, 10);
280
287
 
281
- this.logger.debug('socket: sequence number: ', sequenceNumber);
288
+ this.logger.debug(`socket,${this._domain}: sequence number: `, sequenceNumber);
282
289
  if (this.expectedSequenceNumber && sequenceNumber !== this.expectedSequenceNumber) {
283
290
  this.logger.debug(
284
- `socket: sequence number mismatch indicates lost mercury message. expected: ${this.expectedSequenceNumber}, actual: ${sequenceNumber}`
291
+ `socket,${this._domain}: sequence number mismatch indicates lost mercury message. expected: ${this.expectedSequenceNumber}, actual: ${sequenceNumber}`
285
292
  );
286
293
  this.emit('sequence-mismatch', sequenceNumber, this.expectedSequenceNumber);
287
294
  }
@@ -303,7 +310,7 @@ export default class Socket extends EventEmitter {
303
310
  // message from Mercury. At this time, the only action we have is to
304
311
  // ignore it and move on.
305
312
  /* istanbul ignore next */
306
- this.logger.warn('socket: error while receiving WebSocket message', error);
313
+ this.logger.warn(`socket,${this._domain}: error while receiving WebSocket message`, error);
307
314
  }
308
315
  }
309
316
 
@@ -357,7 +364,7 @@ export default class Socket extends EventEmitter {
357
364
  */
358
365
  _authorize() {
359
366
  return new Promise((resolve) => {
360
- this.logger.info('socket: authorizing');
367
+ this.logger.info(`socket,${this._domain}: authorizing`);
361
368
  this.send({
362
369
  id: uuid.v4(),
363
370
  type: 'authorization',
@@ -395,12 +402,18 @@ export default class Socket extends EventEmitter {
395
402
  if (event.code === 1005 && event.reason) {
396
403
  switch (event.reason.toLowerCase()) {
397
404
  case 'replaced':
398
- this.logger.info('socket: fixing CloseEvent code for reason: ', event.reason);
405
+ this.logger.info(
406
+ `socket,${this._domain}: fixing CloseEvent code for reason: `,
407
+ event.reason
408
+ );
399
409
  event.code = 4000;
400
410
  break;
401
411
  case 'authentication failed':
402
412
  case 'authentication did not happen within the timeout window of 30000 seconds.':
403
- this.logger.info('socket: fixing CloseEvent code for reason: ', event.reason);
413
+ this.logger.info(
414
+ `socket,${this._domain}: fixing CloseEvent code for reason: `,
415
+ event.reason
416
+ );
404
417
  event.code = 1008;
405
418
  break;
406
419
  default:
@@ -420,10 +433,12 @@ export default class Socket extends EventEmitter {
420
433
  _ping(id) {
421
434
  const confirmPongId = (event) => {
422
435
  try {
423
- this.logger.debug('socket: pong', event.data.id);
436
+ this.logger.debug(`socket,${this._domain}: pong`, event.data.id);
424
437
  if (event.data && event.data.id !== id) {
425
- this.logger.info('socket: received pong for wrong ping id, closing socket');
426
- this.logger.debug('socket: expected', id, 'received', event.data.id);
438
+ this.logger.info(
439
+ `socket,${this._domain}: received pong for wrong ping id, closing socket`
440
+ );
441
+ this.logger.debug(`socket,${this._domain}: expected`, id, 'received', event.data.id);
427
442
  this.close({
428
443
  code: 1000,
429
444
  reason: 'Pong mismatch',
@@ -433,24 +448,29 @@ export default class Socket extends EventEmitter {
433
448
  // This try/catch block was added as a debugging step; to the best of my
434
449
  // knowledge, the above can never throw.
435
450
  /* istanbul ignore next */
436
- this.logger.error('socket: error occurred in confirmPongId', error);
451
+ this.logger.error(`socket,${this._domain}: error occurred in confirmPongId`, error);
437
452
  }
438
453
  };
439
454
 
440
455
  const onPongNotReceived = () => {
441
456
  try {
442
- this.logger.info('socket: pong not receive in expected period, closing socket');
457
+ this.logger.info(
458
+ `socket,${this._domain}: pong not receive in expected period, closing socket`
459
+ );
443
460
  this.close({
444
461
  code: 1000,
445
462
  reason: 'Pong not received',
446
463
  }).catch((reason) => {
447
- this.logger.warn('socket: failed to close socket after missed pong', reason);
464
+ this.logger.warn(
465
+ `socket,${this._domain}: failed to close socket after missed pong`,
466
+ reason
467
+ );
448
468
  });
449
469
  } catch (error) {
450
470
  // This try/catch block was added as a debugging step; to the best of my
451
471
  // knowledge, the above can never throw.
452
472
  /* istanbul ignore next */
453
- this.logger.error('socket: error occurred in onPongNotReceived', error);
473
+ this.logger.error(`socket,${this._domain}: error occurred in onPongNotReceived`, error);
454
474
  }
455
475
  };
456
476
 
@@ -462,16 +482,29 @@ export default class Socket extends EventEmitter {
462
482
  // This try/catch block was added as a debugging step; to the best of my
463
483
  // knowledge, the above can never throw.
464
484
  /* istanbul ignore next */
465
- this.logger.error('socket: error occurred in scheduleNextPingAndCancelPongTimer', error);
485
+ this.logger.error(
486
+ `socket,${this._domain}: error occurred in scheduleNextPingAndCancelPongTimer`,
487
+ error
488
+ );
466
489
  }
467
490
  };
468
491
 
492
+ const calculateLatency = (pingTimestamp) => {
493
+ const now = performance.now();
494
+ const latency = now - pingTimestamp;
495
+
496
+ this.logger.debug(`socket,${this._domain}: latency: `, latency);
497
+ this.emit('ping-pong-latency', latency);
498
+ };
499
+
469
500
  id = id || uuid.v4();
501
+ const pingTimestamp = performance.now();
502
+
470
503
  this.pongTimer = safeSetTimeout(onPongNotReceived, this.pongTimeout);
471
504
  this.once('pong', scheduleNextPingAndCancelPongTimer);
472
505
  this.once('pong', confirmPongId);
473
-
474
- this.logger.debug(`socket: ping ${id}`);
506
+ this.once('pong', () => calculateLatency(pingTimestamp));
507
+ this.logger.debug(`socket,${this._domain}: ping ${id}`);
475
508
 
476
509
  return this.send({
477
510
  id,
@@ -32,12 +32,13 @@ describe('plugin-mercury', function () {
32
32
  );
33
33
 
34
34
  describe('onBeforeLogout()', () => {
35
- it('disconnects the web socket', () =>
35
+ it('disconnects the web socket', () => {
36
36
  webex.logout({noRedirect: true}).then(() => {
37
37
  assert.called(webex.internal.mercury.disconnect);
38
38
  assert.isFalse(webex.internal.mercury.connected);
39
39
  assert.called(webex.internal.device.unregister);
40
40
  assert.isFalse(webex.internal.device.registered);
41
- }));
41
+ });
42
+ });
42
43
  });
43
44
  });
@@ -142,7 +142,7 @@ describe('plugin-mercury', () => {
142
142
  });
143
143
  });
144
144
 
145
- describe('when `mercury.buffer_state` is received', () => {
145
+ describe.skip('when `mercury.buffer_state` is received', () => {
146
146
  // This test is here because the buffer states message may arrive before
147
147
  // the mercury Promise resolves.
148
148
  it('gets emitted', (done) => {