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