refactor traceModelBackend

This commit is contained in:
Simon Knott 2024-10-23 13:31:37 +02:00
parent 1cb4bebdbb
commit 3c74ac841c

View file

@ -23,9 +23,6 @@ const zipjs = zipImport as typeof zip;
type Progress = (done: number, total: number) => undefined;
const searchParams = new URL(self.location.href).searchParams;
const testServerPort = searchParams.get('testServerPort');
export class ZipTraceModelBackend implements TraceModelBackend {
private _zipReader: zip.ZipReader<unknown>;
private _entriesPromise: Promise<Map<string, zip.Entry>>;
@ -33,16 +30,8 @@ export class ZipTraceModelBackend implements TraceModelBackend {
constructor(traceURL: string, progress: Progress) {
this._traceURL = traceURL;
const baseURL = new URL(self.location.href);
if (testServerPort) {
baseURL.pathname = '/trace/';
baseURL.port = testServerPort;
}
const url = new URL(formatUrl(traceURL), baseURL);
this._zipReader = new zipjs.ZipReader(
new zipjs.HttpReader(url, { mode: 'cors', preventHeadRequest: true } as any),
new zipjs.HttpReader(formatUrl(traceURL), { mode: 'cors', preventHeadRequest: true } as any),
{ useWebWorkers: false });
this._entriesPromise = this._zipReader.getEntries({ onprogress: progress }).then(entries => {
const map = new Map<string, zip.Entry>();
@ -97,7 +86,7 @@ export class FetchTraceModelBackend implements TraceModelBackend {
constructor(traceURL: string) {
this._traceURL = traceURL;
this._entriesPromise = this._fetchFile(traceURL).then(async response => {
this._entriesPromise = fetch(formatUrl(traceURL)).then(async response => {
const json = JSON.parse(await response.text());
const entries = new Map<string, string>();
for (const entry of json.entries)
@ -140,22 +129,21 @@ export class FetchTraceModelBackend implements TraceModelBackend {
if (!fileName)
return;
return this._fetchFile(fileName);
return fetch(formatUrl(fileName));
}
}
private async _fetchFile(path: string) {
const url = new URL('/trace/file', self.location.href);
url.searchParams.set('path', path);
if (testServerPort)
url.port = testServerPort;
return fetch(url);
}
}
const baseURL = new URL(self.location.href);
baseURL.port = baseURL.searchParams.get('testServerPort') ?? baseURL.port;
function formatUrl(trace: string) {
let url = trace.startsWith('http') || trace.startsWith('blob') ? trace : `file?path=${encodeURIComponent(trace)}`;
// Dropbox does not support cors.
if (url.startsWith('https://www.dropbox.com/'))
url = 'https://dl.dropboxusercontent.com/' + url.substring('https://www.dropbox.com/'.length);
if (trace.startsWith('https://www.dropbox.com/'))
return 'https://dl.dropboxusercontent.com/' + trace.substring('https://www.dropbox.com/'.length);
if (trace.startsWith('http') || trace.startsWith('blob'))
return trace;
const url = new URL('/trace/file', baseURL);
url.searchParams.set('path', trace);
return url;
}