@atlaspack/runtime-js 2.12.1-canary.3583 → 2.12.1-canary.3585

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.
@@ -0,0 +1,10 @@
1
+ // Get the URL without the filename (last / segment)
2
+ function getBaseURL(url: string) {
3
+ return url.slice(0, url.lastIndexOf('/')) + '/';
4
+ }
5
+
6
+ const stackTraceUrlRegexp =
7
+ /(https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/[^)\n]+/g;
8
+
9
+ exports.getBaseURL = getBaseURL;
10
+ exports.stackTraceUrlRegexp = stackTraceUrlRegexp;
@@ -0,0 +1,95 @@
1
+ const {getBaseURL, stackTraceUrlRegexp} = require('./bundle-url-common');
2
+
3
+ const bundleURL: Record<string, string> = {};
4
+
5
+ function getShardedBundleURL(
6
+ bundleName: string,
7
+ cookieName: string,
8
+ cookieString: string,
9
+ maxShards: number,
10
+ inputError?: string,
11
+ ): string {
12
+ let value = bundleURL[bundleName];
13
+
14
+ if (value) {
15
+ return value;
16
+ }
17
+
18
+ try {
19
+ throw inputError ?? new Error();
20
+ } catch (err) {
21
+ var matches = ('' + err.stack).match(stackTraceUrlRegexp);
22
+
23
+ if (!matches) {
24
+ return '/';
25
+ }
26
+
27
+ // The first stack frame will be this function.
28
+ // Use the 2nd one, which will be a runtime in the original bundle.
29
+ const stackUrl = matches[1];
30
+ const baseUrl = getBaseURL(stackUrl);
31
+
32
+ // If the cookie doesn't exist then we don't need to shard
33
+ if (cookieString.indexOf(cookieName) === -1) {
34
+ return baseUrl;
35
+ }
36
+
37
+ const shardNumber = getDomainShardIndex(bundleName, maxShards);
38
+ const url = new URL(baseUrl);
39
+
40
+ const shardedDomain = getShardedDomain(url.hostname, shardNumber);
41
+ url.hostname = shardedDomain;
42
+
43
+ value = url.toString();
44
+
45
+ bundleURL[bundleName] = value;
46
+ return value;
47
+ }
48
+ }
49
+
50
+ function getDomainShardIndex(str: string, maxShards: number) {
51
+ let shard = str.split('').reduce((a, b) => {
52
+ const n = (a << maxShards) - a + b.charCodeAt(0);
53
+
54
+ // The value returned by << is 64 bit, the & operator coerces to 32,
55
+ // prevents overflow as we iterate.
56
+ return n & n;
57
+ }, 0);
58
+
59
+ shard = shard % maxShards;
60
+
61
+ // Make number positive
62
+ if (shard < 0) {
63
+ shard += maxShards;
64
+ }
65
+
66
+ return shard;
67
+ }
68
+
69
+ function getShardedDomain(domain: string, shard: number) {
70
+ let i = domain.indexOf('.');
71
+
72
+ // Domains like localhost have no . separators
73
+ if (i === -1) {
74
+ return `${removeTrailingShard(domain)}-${shard}`;
75
+ }
76
+
77
+ // If this domain already has a shard number in it, strip it out before adding
78
+ // the new one
79
+ const firstSubdomain = removeTrailingShard(domain.slice(0, i));
80
+
81
+ return `${firstSubdomain}-${shard}${domain.slice(i)}`;
82
+ }
83
+
84
+ const trailingShardRegex = /-\d+$/;
85
+
86
+ function removeTrailingShard(subdomain: string) {
87
+ if (!trailingShardRegex.test(subdomain)) {
88
+ return subdomain;
89
+ }
90
+
91
+ const shardIdx = subdomain.lastIndexOf('-');
92
+ return subdomain.slice(0, shardIdx);
93
+ }
94
+
95
+ exports.getShardedBundleURL = getShardedBundleURL;
@@ -0,0 +1,36 @@
1
+ const {getBaseURL, stackTraceUrlRegexp} = require('./bundle-url-common');
2
+
3
+ const bundleURL = {};
4
+
5
+ function getBundleURLCached(id: string) {
6
+ let value = bundleURL[id];
7
+
8
+ if (!value) {
9
+ value = getBundleURL();
10
+ bundleURL[id] = value;
11
+ }
12
+
13
+ return value;
14
+ }
15
+
16
+ function getBundleURL() {
17
+ try {
18
+ throw new Error();
19
+ } catch (err) {
20
+ var matches = ('' + err.stack).match(stackTraceUrlRegexp);
21
+ if (matches) {
22
+ // The first two stack frames will be this function and getBundleURLCached.
23
+ // Use the 3rd one, which will be a runtime in the original bundle.
24
+ return getBaseURL(matches[2]);
25
+ }
26
+ }
27
+
28
+ return '/';
29
+ }
30
+
31
+ function getOrigin(url: string) {
32
+ return new URL(url).origin;
33
+ }
34
+
35
+ exports.getOrigin = getOrigin;
36
+ exports.getBundleURL = getBundleURLCached;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/runtime-js",
3
- "version": "2.12.1-canary.3583+3054dda39",
3
+ "version": "2.12.1-canary.3585+0929ac006",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,15 +12,15 @@
12
12
  "main": "lib/JSRuntime.js",
13
13
  "source": "src/JSRuntime.js",
14
14
  "engines": {
15
- "atlaspack": "2.12.1-canary.3583+3054dda39",
15
+ "atlaspack": "2.12.1-canary.3585+0929ac006",
16
16
  "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/diagnostic": "2.12.1-canary.3583+3054dda39",
20
- "@atlaspack/feature-flags": "2.12.1-canary.3583+3054dda39",
21
- "@atlaspack/plugin": "2.12.1-canary.3583+3054dda39",
22
- "@atlaspack/utils": "2.12.1-canary.3583+3054dda39",
19
+ "@atlaspack/diagnostic": "2.12.1-canary.3585+0929ac006",
20
+ "@atlaspack/feature-flags": "2.12.1-canary.3585+0929ac006",
21
+ "@atlaspack/plugin": "2.12.1-canary.3585+0929ac006",
22
+ "@atlaspack/utils": "2.12.1-canary.3585+0929ac006",
23
23
  "nullthrows": "^1.1.1"
24
24
  },
25
- "gitHead": "3054dda39b54666caba268f09cadf801b43ce3a5"
25
+ "gitHead": "0929ac006bd11b9b91c2ef27f90a272d2355b3a1"
26
26
  }