Skip to content

Commit

Permalink
WEBDEV-6399 Legacy caching params (#44)
Browse files Browse the repository at this point in the history
* Translate legacy caching params into PPS equivalents

* Enable debugging for both 'debugging' and 'cacheDebug' aliases

* Add unit tests
  • Loading branch information
latonv committed Nov 10, 2023
1 parent 937e063 commit 56d2952
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 10 deletions.
47 changes: 37 additions & 10 deletions src/search-backend/base-search-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import {
} from '../search-service-error';
import type { SearchBackendOptionsInterface } from './search-backend-options';

/**
* Map from URL aliases for caching strategies to the corresponding PPS flags
*/
const CACHING_ALIASES: Record<string, string> = {
reCache: JSON.stringify({ recompute: true }),
noCache: JSON.stringify({ bypass: true }),
dontCache: JSON.stringify({ no_compute: true }),
};

/**
* An abstract base class for search backends.
*/
Expand All @@ -30,7 +39,6 @@ export abstract class BaseSearchBackend implements SearchBackendInterface {

constructor(options?: SearchBackendOptionsInterface) {
this.baseUrl = options?.baseUrl ?? 'archive.org';
this.debuggingEnabled = options?.debuggingEnabled ?? false;

if (options?.includeCredentials !== undefined) {
this.includeCredentials = options.includeCredentials;
Expand All @@ -44,23 +52,42 @@ export abstract class BaseSearchBackend implements SearchBackendInterface {
null;
}

const currentUrl = new URL(window.location.href);
const scopeParam = currentUrl.searchParams.get('scope');
const cachingParam = currentUrl.searchParams.get('caching');
const verboseParam = currentUrl.searchParams.get('verbose');

if (options?.scope !== undefined) {
this.requestScope = options.scope;
} else if (scopeParam) {
this.requestScope = scopeParam;
const searchParams = new URL(window.location.href).searchParams;
const scopeParam = searchParams.get('scope');
const verboseParam = searchParams.get('verbose');
const debuggingParam = searchParams.get('debugging');
const cacheDebugParam = searchParams.get('cacheDebug');

// Caching param aliases
let cachingParam = '';
for (const alias of Object.keys(CACHING_ALIASES)) {
if (searchParams.get(alias)) {
cachingParam = CACHING_ALIASES[alias];
break;
}
}

// Explicit PPS caching param overrides any aliases
cachingParam = searchParams.get('caching') ?? cachingParam;

if (options?.caching !== undefined) {
this.cachingFlags = options.caching;
} else if (cachingParam) {
this.cachingFlags = cachingParam;
}

if (options?.debuggingEnabled !== undefined) {
this.debuggingEnabled = options.debuggingEnabled;
} else if (debuggingParam || cacheDebugParam) {
this.debuggingEnabled = true;
}

if (options?.scope !== undefined) {
this.requestScope = options.scope;
} else if (scopeParam) {
this.requestScope = scopeParam;
}

if (options?.verbose !== undefined) {
this.verbose = options.verbose;
} else if (verboseParam) {
Expand Down
45 changes: 45 additions & 0 deletions test/search-backend/fulltext-search-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,51 @@ describe('FulltextSearchBackend', () => {
window.history.replaceState({}, '', url.toString());
});

it('includes reCache param from URL if not provided', async () => {
const url = new URL(window.location.href);
url.searchParams.set('reCache', '1');
window.history.replaceState({}, '', url.toString());

const backend = new FulltextSearchBackend();
await backend.performSearch({ query: 'foo' });

const queryParams = new URL(urlCalled!.toString()).searchParams;
expect(queryParams.get('caching')).to.equal('{"recompute":true}');

url.searchParams.delete('reCache');
window.history.replaceState({}, '', url.toString());
});

it('includes noCache param from URL if not provided', async () => {
const url = new URL(window.location.href);
url.searchParams.set('noCache', '1');
window.history.replaceState({}, '', url.toString());

const backend = new FulltextSearchBackend();
await backend.performSearch({ query: 'foo' });

const queryParams = new URL(urlCalled!.toString()).searchParams;
expect(queryParams.get('caching')).to.equal('{"bypass":true}');

url.searchParams.delete('noCache');
window.history.replaceState({}, '', url.toString());
});

it('includes dontCache param from URL if not provided', async () => {
const url = new URL(window.location.href);
url.searchParams.set('dontCache', '1');
window.history.replaceState({}, '', url.toString());

const backend = new FulltextSearchBackend();
await backend.performSearch({ query: 'foo' });

const queryParams = new URL(urlCalled!.toString()).searchParams;
expect(queryParams.get('caching')).to.equal('{"no_compute":true}');

url.searchParams.delete('dontCache');
window.history.replaceState({}, '', url.toString());
});

it('can enable debugging by default on all searches', async () => {
const backend = new FulltextSearchBackend({
baseUrl: 'foo.bar',
Expand Down
45 changes: 45 additions & 0 deletions test/search-backend/metadata-search-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,51 @@ describe('MetadataSearchBackend', () => {
window.history.replaceState({}, '', url.toString());
});

it('includes reCache param from URL if not provided', async () => {
const url = new URL(window.location.href);
url.searchParams.set('reCache', '1');
window.history.replaceState({}, '', url.toString());

const backend = new MetadataSearchBackend();
await backend.performSearch({ query: 'foo' });

const queryParams = new URL(urlCalled!.toString()).searchParams;
expect(queryParams.get('caching')).to.equal('{"recompute":true}');

url.searchParams.delete('reCache');
window.history.replaceState({}, '', url.toString());
});

it('includes noCache param from URL if not provided', async () => {
const url = new URL(window.location.href);
url.searchParams.set('noCache', '1');
window.history.replaceState({}, '', url.toString());

const backend = new MetadataSearchBackend();
await backend.performSearch({ query: 'foo' });

const queryParams = new URL(urlCalled!.toString()).searchParams;
expect(queryParams.get('caching')).to.equal('{"bypass":true}');

url.searchParams.delete('noCache');
window.history.replaceState({}, '', url.toString());
});

it('includes dontCache param from URL if not provided', async () => {
const url = new URL(window.location.href);
url.searchParams.set('dontCache', '1');
window.history.replaceState({}, '', url.toString());

const backend = new MetadataSearchBackend();
await backend.performSearch({ query: 'foo' });

const queryParams = new URL(urlCalled!.toString()).searchParams;
expect(queryParams.get('caching')).to.equal('{"no_compute":true}');

url.searchParams.delete('dontCache');
window.history.replaceState({}, '', url.toString());
});

it('can enable debugging by default on all searches', async () => {
const backend = new MetadataSearchBackend({
baseUrl: 'foo.bar',
Expand Down

0 comments on commit 56d2952

Please sign in to comment.