Wildcard subdomains - optimisation: wildcard match logic now requires far fewer extra async storage requests (if any)

This commit is contained in:
Francis McKenzie 2022-05-19 17:46:29 +02:00
parent cf662b5f01
commit d4431c4eff

View file

@ -24,6 +24,18 @@ window.assignManager = {
return `wildcardMap@@_${wildcardHostname}`;
},
getWildcardStoreKeys(siteStoreKey) {
// E.g. "siteContainerMap@@_www.mozilla.org" =>
// ["wildcardMap@@_www.mozilla.org", "wildcardMap@@_mozilla.org", "wildcardMap@@_org"]
let previous;
return siteStoreKey.replace(/^siteContainerMap@@_/, "")
.split(".")
.reverse()
.map((subdomain) => previous = previous ? `${subdomain}.${previous}` : subdomain)
.map((hostname) => this.getWildcardStoreKey(hostname))
.reverse();
},
setExempted(pageUrlorUrlKey, tabId) {
const siteStoreKey = this.getSiteStoreKey(pageUrlorUrlKey);
if (!(siteStoreKey in this.exemptedTabs)) {
@ -51,15 +63,39 @@ window.assignManager = {
},
async getOrWildcardMatch(pageUrlorUrlKey) {
// 1st store request: siteStoreKey + wildcardStoreKeys
const siteStoreKey = this.getSiteStoreKey(pageUrlorUrlKey);
const siteSettings = await this.getByUrlKey(siteStoreKey);
const wildcardStoreKeys = this.getWildcardStoreKeys(siteStoreKey);
const combinedStoreKeys = [siteStoreKey].concat(wildcardStoreKeys);
let storageResponse = await this.area.get(combinedStoreKeys);
if (!storageResponse) { return null; }
// Try exact match
const siteSettings = storageResponse[siteStoreKey];
if (siteSettings) {
return {
siteStoreKey,
siteSettings
};
}
return this.getByWildcardMatch(siteStoreKey);
// 2nd store request (maybe): siteStoreKeys that were mapped from wildcardStoreKeys
const siteStoreKeys = wildcardStoreKeys.map((k) => storageResponse[k]).filter((k) => !!k);
if (siteStoreKeys.length > 0) {
storageResponse = await this.area.get(siteStoreKeys);
if (!storageResponse) { return null; }
// Try wildcard matches
for (const siteStoreKey of siteStoreKeys) {
const siteSettings = storageResponse[siteStoreKey];
if (siteSettings) {
return {
siteStoreKey,
siteSettings
};
}
}
}
},
async getSyncEnabled() {
@ -85,26 +121,6 @@ window.assignManager = {
});
},
async getByWildcardMatch(siteStoreKey) {
// Keep stripping subdomains off site hostname until match a wildcard hostname
let remainingHostname = siteStoreKey.replace(/^siteContainerMap@@_/, "");
while (remainingHostname) {
const wildcardStoreKey = this.getWildcardStoreKey(remainingHostname);
siteStoreKey = await this.getByUrlKey(wildcardStoreKey);
if (siteStoreKey) {
const siteSettings = await this.getByUrlKey(siteStoreKey);
if (siteSettings) {
return {
siteStoreKey,
siteSettings
};
}
}
const indexOfDot = remainingHostname.indexOf(".");
remainingHostname = indexOfDot < 0 ? null : remainingHostname.substring(indexOfDot + 1);
}
},
async set(pageUrlorUrlKey, data, exemptedTabIds, backup = true) {
const siteStoreKey = this.getSiteStoreKey(pageUrlorUrlKey);
if (exemptedTabIds) {