Skip to content

Commit

Permalink
Wildcard subdomains - optimisation: wildcard match logic now requires…
Browse files Browse the repository at this point in the history
… far fewer extra async storage requests (if any)
  • Loading branch information
mckenfra committed May 19, 2022
1 parent cf662b5 commit d4431c4
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/js/background/assignManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down

0 comments on commit d4431c4

Please sign in to comment.