Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEBDEV-6399 Legacy caching params #44

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading