Skip to content

Commit

Permalink
feat: transient mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rofe committed Dec 15, 2023
1 parent d1cbc80 commit c28b540
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/extension/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export async function getProject(project = {}) {
}
if (owner && repo) {
const handle = `${owner}/${repo}`;
const projectConfig = await getConfig('sync', handle);
if (projectConfig) {
// check session storage for auth token
return { ...projectConfig, ...(await getConfig('session', handle) || {}) };
}
return getConfig('sync', handle);
}
return undefined;
}
Expand Down
45 changes: 39 additions & 6 deletions src/extension/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,33 @@ export async function loadScript(path) {
* Checks if a host is a valid project host.
* @private
* @param {string} host The base host
* @param {string} owner The owner
* @param {string} host The repo
* @param {string} owner The owner (optional)
* @param {string} host The repo (optional)
* @returns {boolean} <code>true</code> if project host, else <code>false</code>
*/
export function isValidProjectHost(host, owner, repo) {
export function isValidHost(host, owner, repo) {
const [third, second, first] = host.split('.');
const any = '([0-9a-z-]+)';
return host.endsWith(first)
&& ['page', 'live'].includes(first)
&& ['aem', 'hlx'].includes(second)
&& third.endsWith(`--${repo}--${owner}`);
&& new RegExp(`--${repo || any}--${owner || any}$`, 'i').test(third);
}

/**
* Retrieves project details from a host name.
* @private
* @param {string} host The host name
* @returns {string[]} The project details as <code>[ref, repo, owner]</code>
*/
function getConfigDetails(host) {
if (isValidHost(host)) {
const details = host.split('.')[0].split('--');
if (details.length >= 2) {
return details;
}
}
return [];
}

/**
Expand All @@ -71,10 +88,26 @@ export async function getConfigMatches(configs, tabUrl) {
return checkHost === prodHost // production host
|| checkHost === previewHost // custom inner
|| checkHost === liveHost // custom outer
|| isValidProjectHost(checkHost, owner, repo); // inner or outer
|| isValidHost(checkHost, owner, repo); // inner or outer
});
// todo: check url cache if no matches
return matches;
// check if transient match can be derived from url or url cache
if (matches.length === 0) {
const [ref, repo, owner] = getConfigDetails(checkHost);
if (owner && repo && ref) {
matches.push({
owner,
repo,
ref,
transient: true,
});
}
}
// todo: check url cache for transient match
return matches
// exclude disabled configs
.filter(({ owner, repo }) => !configs
.find((cfg) => cfg.owner === owner && cfg.repo === repo && cfg.disabled));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/wtr/check-tab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Test checkTab', () => {
expect(executeScriptSpy.calledTwice).to.be.true;
// error handling
executeScriptSpy.restore();
const error = new Error('testing error handling');
const error = new Error('this error is just a test');
const consoleSpy = sandbox.spy(console, 'log');
sandbox.stub(chrome.scripting, 'executeScript').throws(error);
await checkTab(1);
Expand Down
20 changes: 12 additions & 8 deletions test/wtr/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import chromeMock from './mocks/chrome.js';
import {
loadScript,
getConfigMatches,
isValidProjectHost,
isValidHost,
getConfig,
setConfig,
removeConfig,
Expand Down Expand Up @@ -96,13 +96,15 @@ describe('Test utils', () => {
expect(spy.calledWith('.foo')).to.be.true;
});

it('isValidProjectHost', () => {
expect(isValidProjectHost('https://main--bar--foo.hlx.page', 'foo', 'bar')).to.be.true;
expect(isValidProjectHost('https://main--bar--foo.hlx.live', 'foo', 'bar')).to.be.true;
expect(isValidProjectHost('https://main--bar--foo.aem.page', 'foo', 'bar')).to.be.true;
expect(isValidProjectHost('https://main--bar--foo.aem.live', 'foo', 'bar')).to.be.true;
expect(isValidProjectHost('https://main--bar--fake.hlx.live', 'foo', 'bar')).to.be.false;
expect(isValidProjectHost('https://main--bar--foo.hlx.random', 'foo', 'bar')).to.be.false;
it('isValidHost', () => {
expect(isValidHost('https://main--bar--foo.hlx.page', 'foo', 'bar')).to.be.true;
expect(isValidHost('https://main--bar--foo.hlx.live', 'foo', 'bar')).to.be.true;
expect(isValidHost('https://main--bar--foo.aem.page', 'foo', 'bar')).to.be.true;
expect(isValidHost('https://main--bar--foo.aem.live', 'foo', 'bar')).to.be.true;
expect(isValidHost('https://main--bar--fake.hlx.live', 'foo', 'bar')).to.be.false;
expect(isValidHost('https://main--bar--foo.hlx.random', 'foo', 'bar')).to.be.false;
// check without owner & repo
expect(isValidHost('https://main--bar--foo.hlx.page')).to.be.true;
});

it('getConfigMatches', async () => {
Expand All @@ -120,6 +122,8 @@ describe('Test utils', () => {
expect((await getConfigMatches(CONFIGS, 'https://1.foo.bar/')).length).to.equal(1);
// ignore disabled config
expect((await getConfigMatches(CONFIGS, 'https://main--bar2--foo.hlx.live/')).length).to.equal(0);
// match transient URL
expect((await getConfigMatches(CONFIGS, 'https://main--bar0--foo.hlx.live/')).length).to.equal(1);
// todo: match sharepoint URL (docx)
// expect((await getConfigMatches(CONFIGS, 'https://foo.sharepoint.com/:w:/r/sites/foo/_layouts/15/Doc.aspx?sourcedoc=%7BBFD9A19C-4A68-4DBF-8641-DA2F1283C895%7D&file=index.docx&action=default&mobileredirect=true')).length).to.equal(1);
// todo: match gdrive URL
Expand Down

0 comments on commit c28b540

Please sign in to comment.