@webex/webex-core 3.3.1-next.1 → 3.3.1-next.11

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/package.json CHANGED
@@ -35,12 +35,12 @@
35
35
  "@webex/eslint-config-legacy": "0.0.0",
36
36
  "@webex/jest-config-legacy": "0.0.0",
37
37
  "@webex/legacy-tools": "0.0.0",
38
- "@webex/test-helper-chai": "3.3.1-next.1",
39
- "@webex/test-helper-make-local-url": "3.3.1-next.1",
40
- "@webex/test-helper-mocha": "3.3.1-next.1",
41
- "@webex/test-helper-mock-webex": "3.3.1-next.1",
42
- "@webex/test-helper-refresh-callback": "3.3.1-next.1",
43
- "@webex/test-helper-test-users": "3.3.1-next.1",
38
+ "@webex/test-helper-chai": "3.3.1-next.11",
39
+ "@webex/test-helper-make-local-url": "3.3.1-next.11",
40
+ "@webex/test-helper-mocha": "3.3.1-next.11",
41
+ "@webex/test-helper-mock-webex": "3.3.1-next.11",
42
+ "@webex/test-helper-refresh-callback": "3.3.1-next.11",
43
+ "@webex/test-helper-test-users": "3.3.1-next.11",
44
44
  "chai": "^4.3.4",
45
45
  "chai-as-promised": "^7.1.1",
46
46
  "eslint": "^8.24.0",
@@ -48,12 +48,12 @@
48
48
  "sinon": "^9.2.4"
49
49
  },
50
50
  "dependencies": {
51
- "@webex/common": "3.3.1-next.1",
52
- "@webex/common-timers": "3.3.1-next.1",
53
- "@webex/http-core": "3.3.1-next.1",
54
- "@webex/internal-plugin-device": "3.3.1-next.1",
55
- "@webex/plugin-logger": "3.3.1-next.1",
56
- "@webex/storage-adapter-spec": "3.3.1-next.1",
51
+ "@webex/common": "3.3.1-next.11",
52
+ "@webex/common-timers": "3.3.1-next.11",
53
+ "@webex/http-core": "3.3.1-next.11",
54
+ "@webex/internal-plugin-device": "3.3.1-next.11",
55
+ "@webex/plugin-logger": "3.3.1-next.11",
56
+ "@webex/storage-adapter-spec": "3.3.1-next.11",
57
57
  "ampersand-collection": "^2.0.2",
58
58
  "ampersand-events": "^2.0.2",
59
59
  "ampersand-state": "^5.0.3",
@@ -73,5 +73,5 @@
73
73
  "test:style": "eslint ./src/**/*.*",
74
74
  "test:unit": "webex-legacy-tools test --unit --runner jest"
75
75
  },
76
- "version": "3.3.1-next.1"
76
+ "version": "3.3.1-next.11"
77
77
  }
@@ -237,7 +237,6 @@ const ServiceCatalog = AmpState.extend({
237
237
  * @returns {serviceUrl} - ServiceUrl assocated with provided url
238
238
  */
239
239
  findServiceUrlFromUrl(url) {
240
- const incomingUrlObj = Url.parse(url);
241
240
  const serviceUrls = [
242
241
  ...this.serviceGroups.discovery,
243
242
  ...this.serviceGroups.preauth,
@@ -247,12 +246,21 @@ const ServiceCatalog = AmpState.extend({
247
246
  ];
248
247
 
249
248
  return serviceUrls.find((serviceUrl) => {
250
- if (incomingUrlObj.hostname === Url.parse(serviceUrl.defaultUrl).hostname) {
249
+ // Check to see if the URL we are checking starts with the default URL
250
+ if (url.startsWith(serviceUrl.defaultUrl)) {
251
251
  return true;
252
252
  }
253
253
 
254
- if (serviceUrl.hosts.find((host) => host.host === incomingUrlObj.hostname)) {
255
- return true;
254
+ // If not, we check to see if the alternate URLs match
255
+ // These are made by swapping the host of the default URL
256
+ // with that of an alternate host
257
+ for (const host of serviceUrl.hosts) {
258
+ const alternateUrl = new URL(serviceUrl.defaultUrl);
259
+ alternateUrl.host = host.host;
260
+
261
+ if (url.startsWith(alternateUrl.toString())) {
262
+ return true;
263
+ }
256
264
  }
257
265
 
258
266
  return false;
@@ -1,8 +1,6 @@
1
- import Url from 'url';
2
-
3
1
  import sha256 from 'crypto-js/sha256';
4
2
 
5
- import {union} from 'lodash';
3
+ import {union, forEach} from 'lodash';
6
4
  import WebexPlugin from '../webex-plugin';
7
5
 
8
6
  import METRICS from './metrics';
@@ -686,59 +684,74 @@ const Services = WebexPlugin.extend({
686
684
  */
687
685
  _formatReceivedHostmap(serviceHostmap) {
688
686
  this._updateHostCatalog(serviceHostmap.hostCatalog);
689
- // map the host catalog items to a formatted hostmap
690
- const formattedHostmap = Object.keys(serviceHostmap.hostCatalog).reduce((accumulator, key) => {
691
- if (serviceHostmap.hostCatalog[key].length === 0) {
692
- return accumulator;
693
- }
694
687
 
695
- const serviceName = serviceHostmap.hostCatalog[key][0].id.split(':')[3];
696
- const defaultUrl = serviceHostmap.serviceLinks[serviceName];
688
+ const extractId = (entry) => entry.id.split(':')[3];
697
689
 
698
- let serviceItem = accumulator.find((item) => item.name === serviceName);
690
+ const formattedHostmap = [];
699
691
 
700
- if (!serviceItem) {
701
- serviceItem = {
702
- name: serviceName,
703
- defaultUrl,
704
- defaultHost: Url.parse(defaultUrl).hostname,
705
- hosts: [],
706
- };
692
+ // for each of the services in the serviceLinks, find the matching host in the catalog
693
+ Object.keys(serviceHostmap.serviceLinks).forEach((serviceName) => {
694
+ const serviceUrl = serviceHostmap.serviceLinks[serviceName];
707
695
 
708
- accumulator.push(serviceItem);
696
+ let host;
697
+ try {
698
+ host = new URL(serviceUrl).host;
699
+ } catch (e) {
700
+ return;
709
701
  }
710
702
 
711
- serviceItem.hosts.push(
712
- // map the default key as a low priority default for cluster matching
713
- {
714
- host: key,
715
- ttl: -1,
716
- priority: 10,
717
- id: serviceHostmap.hostCatalog[key][0].id,
718
- homeCluster: serviceItem.defaultHost === key,
719
- },
720
- // map the rest of the hosts in their proper locations
721
- ...serviceHostmap.hostCatalog[key].map((host) => ({
722
- ...host,
723
- homeCluster: serviceItem.defaultHost === key,
724
- }))
725
- );
703
+ const matchingCatalogEntry = serviceHostmap.hostCatalog[host];
726
704
 
727
- return accumulator;
728
- }, []);
705
+ const formattedHost = {
706
+ name: serviceName,
707
+ defaultUrl: serviceUrl,
708
+ defaultHost: host,
709
+ hosts: [],
710
+ };
729
711
 
730
- // append service links that do not exist in the host catalog
731
- Object.keys(serviceHostmap.serviceLinks).forEach((key) => {
732
- const service = formattedHostmap.find((item) => item.name === key);
712
+ formattedHostmap.push(formattedHost);
733
713
 
734
- if (!service) {
735
- formattedHostmap.push({
736
- name: key,
737
- defaultUrl: serviceHostmap.serviceLinks[key],
738
- defaultHost: Url.parse(serviceHostmap.serviceLinks[key]).hostname,
739
- hosts: [],
740
- });
714
+ // If the catalog does not have any hosts we will be unable to find the service ID
715
+ // so can't search for other hosts
716
+ if (!matchingCatalogEntry || !matchingCatalogEntry[0]) {
717
+ return;
741
718
  }
719
+
720
+ const serviceId = extractId(matchingCatalogEntry[0]);
721
+
722
+ forEach(matchingCatalogEntry, (entry) => {
723
+ // The ids for all hosts within a hostCatalog entry should be the same
724
+ // but for safety, only add host entries that have the same id as the first one
725
+ if (extractId(entry) === serviceId) {
726
+ formattedHost.hosts.push({
727
+ ...entry,
728
+ homeCluster: true,
729
+ });
730
+ }
731
+ });
732
+
733
+ const otherHosts = [];
734
+
735
+ // find the services in the host catalog that have the same id
736
+ // and add them to the otherHosts
737
+ forEach(serviceHostmap.hostCatalog, (entry) => {
738
+ // exclude the matching catalog entry as we have already added that
739
+ if (entry === matchingCatalogEntry) {
740
+ return;
741
+ }
742
+
743
+ forEach(entry, (entryHost) => {
744
+ // only add hosts that have the correct id
745
+ if (extractId(entryHost) === serviceId) {
746
+ otherHosts.push({
747
+ ...entryHost,
748
+ homeCluster: false,
749
+ });
750
+ }
751
+ });
752
+ });
753
+
754
+ formattedHost.hosts.push(...otherHosts);
742
755
  });
743
756
 
744
757
  // update all the service urls in the host catalog
@@ -37,11 +37,15 @@ describe('webex-core', () => {
37
37
  orgId: process.env.EU_PRIMARY_ORG_ID,
38
38
  },
39
39
  }),
40
- ]).then(([[user], [userEU]]) => {
41
- webexUser = user;
42
- webexUserEU = userEU;
43
- })
44
- );
40
+ ]).then(([[user], [userEU]]) =>
41
+ new Promise((resolve) => {
42
+ setTimeout(() => {
43
+ webexUser = user;
44
+ webexUserEU = userEU;
45
+ resolve();
46
+ }, 1000)
47
+ })
48
+ ));
45
49
 
46
50
  beforeEach('create webex instance', () => {
47
51
  webex = new WebexCore({credentials: {supertoken: webexUser.token}});
@@ -13,7 +13,7 @@ describe('webex-core', () => {
13
13
  let services;
14
14
  let catalog;
15
15
 
16
- beforeAll(() => {
16
+ beforeEach(() => {
17
17
  webex = new MockWebex();
18
18
  services = new Services(undefined, {parent: webex});
19
19
  catalog = services._getCatalog();
@@ -201,6 +201,56 @@ describe('webex-core', () => {
201
201
  assert.match(['example-a', 'example-b', 'example-c', 'example-e', 'example-f'], list);
202
202
  });
203
203
  });
204
+
205
+ describe('findServiceUrlFromUrl()', () => {
206
+ const otherService = {
207
+ defaultUrl: 'https://example.com/differentresource',
208
+ hosts: [{host: 'example1.com'}, {host: 'example2.com'}],
209
+ };
210
+
211
+ it.each([
212
+ 'discovery',
213
+ 'preauth',
214
+ 'signin',
215
+ 'postauth',
216
+ 'override'
217
+ ])('matches a default url correctly', (serviceGroup) => {
218
+ const url = 'https://example.com/resource/id';
219
+
220
+
221
+ const exampleService = {
222
+ defaultUrl: 'https://example.com/resource',
223
+ hosts: [{host: 'example1.com'}, {host: 'example2.com'}],
224
+ };
225
+
226
+ catalog.serviceGroups[serviceGroup].push(otherService, exampleService);
227
+
228
+ const service = catalog.findServiceUrlFromUrl(url);
229
+
230
+ assert.equal(service, exampleService);
231
+ });
232
+
233
+ it.each([
234
+ 'discovery',
235
+ 'preauth',
236
+ 'signin',
237
+ 'postauth',
238
+ 'override'
239
+ ])('matches an alternate host url', (serviceGroup) => {
240
+ const url = 'https://example2.com/resource/id';
241
+
242
+ const exampleService = {
243
+ defaultUrl: 'https://example.com/resource',
244
+ hosts: [{host: 'example1.com'}, {host: 'example2.com'}],
245
+ };
246
+
247
+ catalog.serviceGroups[serviceGroup].push(otherService, exampleService);
248
+
249
+ const service = catalog.findServiceUrlFromUrl(url);
250
+
251
+ assert.equal(service, exampleService);
252
+ });
253
+ });
204
254
  });
205
255
  });
206
256
  /* eslint-enable no-underscore-dangle */
@@ -301,6 +301,9 @@ describe('webex-core', () => {
301
301
  'example-b': 'https://example-b.com/api/v1',
302
302
  'example-c': 'https://example-c.com/api/v1',
303
303
  'example-d': 'https://example-d.com/api/v1',
304
+ 'example-e': 'https://example-e.com/api/v1',
305
+ 'example-f': 'https://example-f.com/api/v1',
306
+ 'example-g': 'https://example-g.com/api/v1',
304
307
  },
305
308
  hostCatalog: {
306
309
  'example-a.com': [
@@ -383,6 +386,48 @@ describe('webex-core', () => {
383
386
  id: '0:0:0:example-d-x',
384
387
  },
385
388
  ],
389
+ 'example-e.com': [
390
+ {
391
+ host: 'example-e-1.com',
392
+ ttl: -1,
393
+ priority: 5,
394
+ id: '0:0:0:different-e',
395
+ },
396
+ {
397
+ host: 'example-e-2.com',
398
+ ttl: -1,
399
+ priority: 3,
400
+ id: '0:0:0:different-e',
401
+ },
402
+ {
403
+ host: 'example-e-3.com',
404
+ ttl: -1,
405
+ priority: 1,
406
+ id: '0:0:0:different-e',
407
+ },
408
+ ],
409
+ 'example-e-1.com': [
410
+ {
411
+ host: 'example-e-4.com',
412
+ ttl: -1,
413
+ priority: 5,
414
+ id: '0:0:0:different-e',
415
+ },
416
+ {
417
+ host: 'example-e-5.com',
418
+ ttl: -1,
419
+ priority: 3,
420
+ id: '0:0:0:different-e',
421
+ },
422
+ {
423
+ host: 'example-e-3.com',
424
+ ttl: -1,
425
+ priority: 1,
426
+ id: '0:0:0:different-e-x',
427
+ },
428
+ ],
429
+ 'example-f.com': [
430
+ ],
386
431
  },
387
432
  format: 'hostmap',
388
433
  };
@@ -440,16 +485,6 @@ describe('webex-core', () => {
440
485
  });
441
486
  });
442
487
 
443
- it('creates a formmated host map containing all received host map host entries', () => {
444
- formattedHM = services._formatReceivedHostmap(serviceHostmap);
445
-
446
- formattedHM.forEach((service) => {
447
- const foundHosts = serviceHostmap.hostCatalog[service.defaultHost];
448
-
449
- assert.isDefined(foundHosts);
450
- });
451
- });
452
-
453
488
  it('creates an array with matching names', () => {
454
489
  formattedHM = services._formatReceivedHostmap(serviceHostmap);
455
490
 
@@ -459,6 +494,151 @@ describe('webex-core', () => {
459
494
  );
460
495
  });
461
496
 
497
+ it('creates the expected formatted host map', () => {
498
+ formattedHM = services._formatReceivedHostmap(serviceHostmap);
499
+
500
+ assert.deepEqual(formattedHM, [
501
+ {
502
+ defaultHost: 'example-a.com',
503
+ defaultUrl: 'https://example-a.com/api/v1',
504
+ hosts: [
505
+ {
506
+ homeCluster: true,
507
+ host: 'example-a-1.com',
508
+ id: '0:0:0:example-a',
509
+ priority: 5,
510
+ ttl: -1,
511
+ },
512
+ {
513
+ homeCluster: true,
514
+ host: 'example-a-2.com',
515
+ id: '0:0:0:example-a',
516
+ priority: 3,
517
+ ttl: -1,
518
+ },
519
+ ],
520
+ name: 'example-a',
521
+ },
522
+ {
523
+ defaultHost: 'example-b.com',
524
+ defaultUrl: 'https://example-b.com/api/v1',
525
+ hosts: [
526
+ {
527
+ homeCluster: true,
528
+ host: 'example-b-1.com',
529
+ id: '0:0:0:example-b',
530
+ priority: 5,
531
+ ttl: -1,
532
+ },
533
+ {
534
+ homeCluster: true,
535
+ host: 'example-b-2.com',
536
+ id: '0:0:0:example-b',
537
+ priority: 3,
538
+ ttl: -1,
539
+ },
540
+ ],
541
+ name: 'example-b',
542
+ },
543
+ {
544
+ defaultHost: 'example-c.com',
545
+ defaultUrl: 'https://example-c.com/api/v1',
546
+ hosts: [
547
+ {
548
+ homeCluster: true,
549
+ host: 'example-c-1.com',
550
+ id: '0:0:0:example-c',
551
+ priority: 5,
552
+ ttl: -1,
553
+ },
554
+ {
555
+ homeCluster: true,
556
+ host: 'example-c-2.com',
557
+ id: '0:0:0:example-c',
558
+ priority: 3,
559
+ ttl: -1,
560
+ },
561
+ ],
562
+ name: 'example-c',
563
+ },
564
+ {
565
+ defaultHost: 'example-d.com',
566
+ defaultUrl: 'https://example-d.com/api/v1',
567
+ hosts: [
568
+ {
569
+ homeCluster: true,
570
+ host: 'example-c-1.com',
571
+ id: '0:0:0:example-d',
572
+ priority: 5,
573
+ ttl: -1,
574
+ },
575
+ {
576
+ homeCluster: true,
577
+ host: 'example-c-2.com',
578
+ id: '0:0:0:example-d',
579
+ priority: 3,
580
+ ttl: -1,
581
+ },
582
+ ],
583
+ name: 'example-d',
584
+ },
585
+ {
586
+ defaultHost: 'example-e.com',
587
+ defaultUrl: 'https://example-e.com/api/v1',
588
+ hosts: [
589
+ {
590
+ homeCluster: true,
591
+ host: 'example-e-1.com',
592
+ id: '0:0:0:different-e',
593
+ priority: 5,
594
+ ttl: -1,
595
+ },
596
+ {
597
+ homeCluster: true,
598
+ host: 'example-e-2.com',
599
+ id: '0:0:0:different-e',
600
+ priority: 3,
601
+ ttl: -1,
602
+ },
603
+ {
604
+ homeCluster: true,
605
+ host: 'example-e-3.com',
606
+ id: '0:0:0:different-e',
607
+ priority: 1,
608
+ ttl: -1,
609
+ },
610
+ {
611
+ homeCluster: false,
612
+ host: 'example-e-4.com',
613
+ id: '0:0:0:different-e',
614
+ priority: 5,
615
+ ttl: -1,
616
+ },
617
+ {
618
+ homeCluster: false,
619
+ host: 'example-e-5.com',
620
+ id: '0:0:0:different-e',
621
+ priority: 3,
622
+ ttl: -1,
623
+ },
624
+ ],
625
+ name: 'example-e',
626
+ },
627
+ {
628
+ defaultHost: 'example-f.com',
629
+ defaultUrl: 'https://example-f.com/api/v1',
630
+ hosts: [],
631
+ name: 'example-f',
632
+ },
633
+ {
634
+ defaultHost: 'example-g.com',
635
+ defaultUrl: 'https://example-g.com/api/v1',
636
+ hosts: [],
637
+ name: 'example-g',
638
+ }
639
+ ]);
640
+ });
641
+
462
642
  it('has hostCatalog updated', () => {
463
643
  services._formatReceivedHostmap(serviceHostmap);
464
644