Skip to content

Commit 9a77ecc

Browse files
committed
Merge branch 'main' into alsh/atlassian-analytics
2 parents e7a5928 + d904a75 commit 9a77ecc

File tree

6 files changed

+72
-28
lines changed

6 files changed

+72
-28
lines changed

packages/runtimes/js/src/helpers/.babelrc

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
export type AtlaspackAnalyticsEvent = {
2-
kind?: string;
32
action: string;
43
attributes?: {[key: string]: string | number | boolean};
5-
tags?: string[];
64
};
75

86
export function sendAnalyticsEvent(event: AtlaspackAnalyticsEvent): void;
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// @flow
22

33
export type AtlaspackAnalyticsEvent = {|
4-
kind?: string,
54
action: string,
65
attributes?: { [key: string]: string | number | boolean },
7-
tags?: string[],
86
|};
97

108
declare export function sendAnalyticsEvent(event: AtlaspackAnalyticsEvent): void;

packages/runtimes/js/src/helpers/browser/esm-js-loader-retry.js

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ async function load(id) {
6161

6262
function sendAnalyticsEvent(action, targetUrl, attempt) {
6363
require('./analytics/analytics.js').sendAnalyticsEvent({
64-
kind: 'operational',
6564
action: 'importRetry',
6665
attributes: {
6766
action,

packages/utils/domain-sharding/src/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ function getFilenameFromUrlPath(pathname) {
2828
* @returns {number}
2929
*/
3030
function getDomainShardIndex(str, maxShards) {
31+
// As we include the base domain as a shard option then we add 1 to maxShards
32+
// to account for that.
33+
let totalShards = maxShards + 1;
3134
let shard = str.split('').reduce((a, b) => {
32-
let n = (a << maxShards) - a + b.charCodeAt(0);
35+
let n = (a << totalShards) - a + b.charCodeAt(0);
3336

3437
// The value returned by << is 64 bit, the & operator coerces to 32,
3538
// prevents overflow as we iterate.
3639
return n & n;
3740
}, 0);
3841

39-
shard = shard % maxShards;
42+
shard = shard % totalShards;
4043

4144
// Make number positive
4245
if (shard < 0) {
43-
shard += maxShards;
46+
shard += totalShards;
4447
}
4548

4649
return shard;
@@ -69,17 +72,20 @@ function removeTrailingShard(subdomain) {
6972
*/
7073
function applyShardToDomain(domain, shard) {
7174
let i = domain.indexOf('.');
75+
// If the shard is 0, then just use the base domain.
76+
// If the shard is > 0, then remove 1 as the shards domains index from 0
77+
let shardSuffix = shard === 0 ? '' : `-${shard - 1}`;
7278

7379
// Domains like localhost have no . separators
7480
if (i === -1) {
75-
return `${removeTrailingShard(domain)}-${shard}`;
81+
return `${removeTrailingShard(domain)}${shardSuffix}`;
7682
}
7783

7884
// If this domain already has a shard number in it, strip it
7985
// out before adding the new one
8086
let firstSubdomain = removeTrailingShard(domain.slice(0, i));
8187

82-
return `${firstSubdomain}-${shard}${domain.slice(i)}`;
88+
return `${firstSubdomain}${shardSuffix}${domain.slice(i)}`;
8389
}
8490

8591
/**

packages/utils/domain-sharding/test/index.test.js

+61-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
11
// @flow
22
import assert from 'assert';
3-
import {shardUrlUnchecked, shardUrl, domainShardingKey} from '../src/index.js';
3+
import {
4+
shardUrlUnchecked,
5+
shardUrl,
6+
domainShardingKey,
7+
applyShardToDomain,
8+
} from '../src/index.js';
49

510
describe('domain sharding helpers', () => {
611
beforeEach(() => {
712
// $FlowFixMe
813
delete globalThis[domainShardingKey];
914
});
1015

16+
describe('applyShardToDomain', () => {
17+
it('should use the base domain for shard 0', () => {
18+
const testBundle =
19+
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js';
20+
21+
const result = applyShardToDomain(testBundle, 0);
22+
23+
assert.equal(
24+
result,
25+
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js',
26+
);
27+
});
28+
29+
it('should use the 0 shard domain for shard 1', () => {
30+
const testBundle =
31+
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js';
32+
33+
const result = applyShardToDomain(testBundle, 1);
34+
35+
assert.equal(
36+
result,
37+
'https://bundle-shard-0.assets.example.com/assets/test-bundle.123abc.js',
38+
);
39+
});
40+
41+
it('should use the 2 shard domain for shard 3', () => {
42+
const testBundle =
43+
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js';
44+
45+
const result = applyShardToDomain(testBundle, 3);
46+
47+
assert.equal(
48+
result,
49+
'https://bundle-shard-2.assets.example.com/assets/test-bundle.123abc.js',
50+
);
51+
});
52+
});
53+
1154
describe('shardUrlUnchecked', () => {
1255
it('should shard a URL', () => {
1356
const testBundle =
@@ -17,7 +60,19 @@ describe('domain sharding helpers', () => {
1760

1861
assert.equal(
1962
result,
20-
'https://bundle-shard-0.assets.example.com/assets/test-bundle.123abc.js',
63+
'https://bundle-shard-1.assets.example.com/assets/test-bundle.123abc.js',
64+
);
65+
});
66+
67+
it('should include the original domain in the shar', () => {
68+
const testBundle =
69+
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js';
70+
71+
const result = shardUrlUnchecked(testBundle, 5);
72+
73+
assert.equal(
74+
result,
75+
'https://bundle-shard-1.assets.example.com/assets/test-bundle.123abc.js',
2176
);
2277
});
2378

@@ -29,7 +84,7 @@ describe('domain sharding helpers', () => {
2984

3085
assert.equal(
3186
result,
32-
'https://bundle-shard-4.assets.example.com/assets/TestBundle.1a2b3c.js',
87+
'https://bundle-shard-2.assets.example.com/assets/TestBundle.1a2b3c.js',
3388
);
3489
});
3590

@@ -38,7 +93,7 @@ describe('domain sharding helpers', () => {
3893

3994
const result = shardUrlUnchecked(testBundle, 5);
4095

41-
assert.equal(result, 'http://localhost-3/assets/testBundle.123abc.js');
96+
assert.equal(result, 'http://localhost-2/assets/testBundle.123abc.js');
4297
});
4398

4499
it('should handle domains with ports', () => {
@@ -48,7 +103,7 @@ describe('domain sharding helpers', () => {
48103

49104
assert.equal(
50105
result,
51-
'http://localhost-3:8081/assets/testBundle.123abc.js',
106+
'http://localhost-2:8081/assets/testBundle.123abc.js',
52107
);
53108
});
54109
});
@@ -63,7 +118,7 @@ describe('domain sharding helpers', () => {
63118

64119
assert.equal(
65120
result,
66-
'https://bundle-shard-0.assets.example.com/assets/test-bundle.123abc.js',
121+
'https://bundle-shard-1.assets.example.com/assets/test-bundle.123abc.js',
67122
);
68123
});
69124

0 commit comments

Comments
 (0)