@atlaspack/domain-sharding
Advanced tools
Comparing version 2.12.1-canary.3611 to 2.12.1-canary.3612
@@ -30,4 +30,7 @@ "use strict"; | ||
function getDomainShardIndex(str, maxShards) { | ||
// As we include the base domain as a shard option then we add 1 to maxShards | ||
// to account for that. | ||
let totalShards = maxShards + 1; | ||
let shard = str.split('').reduce((a, b) => { | ||
let n = (a << maxShards) - a + b.charCodeAt(0); | ||
let n = (a << totalShards) - a + b.charCodeAt(0); | ||
@@ -38,7 +41,7 @@ // The value returned by << is 64 bit, the & operator coerces to 32, | ||
}, 0); | ||
shard = shard % maxShards; | ||
shard = shard % totalShards; | ||
// Make number positive | ||
if (shard < 0) { | ||
shard += maxShards; | ||
shard += totalShards; | ||
} | ||
@@ -69,6 +72,9 @@ return shard; | ||
let i = domain.indexOf('.'); | ||
// If the shard is 0, then just use the base domain. | ||
// If the shard is > 0, then remove 1 as the shards domains index from 0 | ||
let shardSuffix = shard === 0 ? '' : `-${shard - 1}`; | ||
// Domains like localhost have no . separators | ||
if (i === -1) { | ||
return `${removeTrailingShard(domain)}-${shard}`; | ||
return `${removeTrailingShard(domain)}${shardSuffix}`; | ||
} | ||
@@ -79,3 +85,3 @@ | ||
let firstSubdomain = removeTrailingShard(domain.slice(0, i)); | ||
return `${firstSubdomain}-${shard}${domain.slice(i)}`; | ||
return `${firstSubdomain}${shardSuffix}${domain.slice(i)}`; | ||
} | ||
@@ -82,0 +88,0 @@ |
{ | ||
"name": "@atlaspack/domain-sharding", | ||
"version": "2.12.1-canary.3611+4cc252099", | ||
"version": "2.12.1-canary.3612+2fea5f2c1", | ||
"license": "(MIT OR Apache-2.0)", | ||
@@ -19,3 +19,3 @@ "source": "src/index.js", | ||
"engines": { | ||
"atlaspack": "2.12.1-canary.3611+4cc252099", | ||
"atlaspack": "2.12.1-canary.3612+2fea5f2c1", | ||
"node": ">= 16.0.0" | ||
@@ -26,3 +26,3 @@ }, | ||
}, | ||
"gitHead": "4cc252099f2ea29bd8c3ab7575c08532270074e4" | ||
"gitHead": "2fea5f2c1ab381008f21fe960c9103b8b692c8ef" | ||
} |
@@ -31,4 +31,7 @@ let globalKeyName = '__ATLASPACK_ENABLE_DOMAIN_SHARDS'; | ||
function getDomainShardIndex(str, maxShards) { | ||
// As we include the base domain as a shard option then we add 1 to maxShards | ||
// to account for that. | ||
let totalShards = maxShards + 1; | ||
let shard = str.split('').reduce((a, b) => { | ||
let n = (a << maxShards) - a + b.charCodeAt(0); | ||
let n = (a << totalShards) - a + b.charCodeAt(0); | ||
@@ -40,7 +43,7 @@ // The value returned by << is 64 bit, the & operator coerces to 32, | ||
shard = shard % maxShards; | ||
shard = shard % totalShards; | ||
// Make number positive | ||
if (shard < 0) { | ||
shard += maxShards; | ||
shard += totalShards; | ||
} | ||
@@ -74,6 +77,9 @@ | ||
let i = domain.indexOf('.'); | ||
// If the shard is 0, then just use the base domain. | ||
// If the shard is > 0, then remove 1 as the shards domains index from 0 | ||
let shardSuffix = shard === 0 ? '' : `-${shard - 1}`; | ||
// Domains like localhost have no . separators | ||
if (i === -1) { | ||
return `${removeTrailingShard(domain)}-${shard}`; | ||
return `${removeTrailingShard(domain)}${shardSuffix}`; | ||
} | ||
@@ -85,3 +91,3 @@ | ||
return `${firstSubdomain}-${shard}${domain.slice(i)}`; | ||
return `${firstSubdomain}${shardSuffix}${domain.slice(i)}`; | ||
} | ||
@@ -88,0 +94,0 @@ |
// @flow | ||
import assert from 'assert'; | ||
import {shardUrlUnchecked, shardUrl, domainShardingKey} from '../src/index.js'; | ||
import { | ||
shardUrlUnchecked, | ||
shardUrl, | ||
domainShardingKey, | ||
applyShardToDomain, | ||
} from '../src/index.js'; | ||
@@ -11,2 +16,40 @@ describe('domain sharding helpers', () => { | ||
describe('applyShardToDomain', () => { | ||
it('should use the base domain for shard 0', () => { | ||
const testBundle = | ||
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js'; | ||
const result = applyShardToDomain(testBundle, 0); | ||
assert.equal( | ||
result, | ||
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js', | ||
); | ||
}); | ||
it('should use the 0 shard domain for shard 1', () => { | ||
const testBundle = | ||
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js'; | ||
const result = applyShardToDomain(testBundle, 1); | ||
assert.equal( | ||
result, | ||
'https://bundle-shard-0.assets.example.com/assets/test-bundle.123abc.js', | ||
); | ||
}); | ||
it('should use the 2 shard domain for shard 3', () => { | ||
const testBundle = | ||
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js'; | ||
const result = applyShardToDomain(testBundle, 3); | ||
assert.equal( | ||
result, | ||
'https://bundle-shard-2.assets.example.com/assets/test-bundle.123abc.js', | ||
); | ||
}); | ||
}); | ||
describe('shardUrlUnchecked', () => { | ||
@@ -21,6 +64,18 @@ it('should shard a URL', () => { | ||
result, | ||
'https://bundle-shard-0.assets.example.com/assets/test-bundle.123abc.js', | ||
'https://bundle-shard-1.assets.example.com/assets/test-bundle.123abc.js', | ||
); | ||
}); | ||
it('should include the original domain in the shar', () => { | ||
const testBundle = | ||
'https://bundle-shard.assets.example.com/assets/test-bundle.123abc.js'; | ||
const result = shardUrlUnchecked(testBundle, 5); | ||
assert.equal( | ||
result, | ||
'https://bundle-shard-1.assets.example.com/assets/test-bundle.123abc.js', | ||
); | ||
}); | ||
it('should re-shard a domain that has already been sharded', () => { | ||
@@ -34,3 +89,3 @@ const testBundle = | ||
result, | ||
'https://bundle-shard-4.assets.example.com/assets/TestBundle.1a2b3c.js', | ||
'https://bundle-shard-2.assets.example.com/assets/TestBundle.1a2b3c.js', | ||
); | ||
@@ -44,3 +99,3 @@ }); | ||
assert.equal(result, 'http://localhost-3/assets/testBundle.123abc.js'); | ||
assert.equal(result, 'http://localhost-2/assets/testBundle.123abc.js'); | ||
}); | ||
@@ -55,3 +110,3 @@ | ||
result, | ||
'http://localhost-3:8081/assets/testBundle.123abc.js', | ||
'http://localhost-2:8081/assets/testBundle.123abc.js', | ||
); | ||
@@ -71,3 +126,3 @@ }); | ||
result, | ||
'https://bundle-shard-0.assets.example.com/assets/test-bundle.123abc.js', | ||
'https://bundle-shard-1.assets.example.com/assets/test-bundle.123abc.js', | ||
); | ||
@@ -74,0 +129,0 @@ }); |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
25084
381