2021-10-11 19:52:28 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
import { SnapshotServer } from './snapshotServer';
|
|
|
|
import { TraceModel } from './traceModel';
|
2021-10-11 19:52:28 -08:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
declare const self: ServiceWorkerGlobalScope;
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
self.addEventListener('install', function(event: any) {});
|
2021-10-11 19:52:28 -08:00
|
|
|
|
|
|
|
self.addEventListener('activate', function(event: any) {
|
|
|
|
event.waitUntil(self.clients.claim());
|
|
|
|
});
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
let traceModel: TraceModel | undefined;
|
|
|
|
let snapshotServer: SnapshotServer | undefined;
|
2021-10-11 19:52:28 -08:00
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
async function loadTrace(trace: string): Promise<TraceModel> {
|
|
|
|
const traceModel = new TraceModel();
|
2021-10-12 20:21:06 -08:00
|
|
|
const url = trace.startsWith('http') || trace.startsWith('blob') ? trace : `/file?path=${trace}`;
|
2021-10-12 13:42:50 -08:00
|
|
|
await traceModel.load(url);
|
|
|
|
return traceModel;
|
2021-10-11 19:52:28 -08:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
// @ts-ignore
|
|
|
|
async function doFetch(event: FetchEvent): Promise<Response> {
|
2021-10-11 19:52:28 -08:00
|
|
|
const request = event.request;
|
2021-10-12 13:42:50 -08:00
|
|
|
const { pathname, searchParams } = new URL(request.url);
|
2021-10-11 19:52:28 -08:00
|
|
|
const snapshotUrl = request.mode === 'navigate' ?
|
|
|
|
request.url : (await self.clients.get(event.clientId))!.url;
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
if (request.url.startsWith(self.location.origin)) {
|
|
|
|
if (pathname === '/context') {
|
|
|
|
const trace = searchParams.get('trace')!;
|
|
|
|
traceModel = await loadTrace(trace);
|
|
|
|
snapshotServer = new SnapshotServer(traceModel.storage());
|
|
|
|
return new Response(JSON.stringify(traceModel!.contextEntry), {
|
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (pathname.startsWith('/snapshotSize/'))
|
|
|
|
return snapshotServer!.serveSnapshotSize(pathname, searchParams);
|
|
|
|
if (pathname.startsWith('/snapshot/'))
|
|
|
|
return snapshotServer!.serveSnapshot(pathname, searchParams, snapshotUrl);
|
|
|
|
if (pathname.startsWith('/sha1/')) {
|
|
|
|
const blob = await traceModel!.resourceForSha1(pathname.slice('/sha1/'.length));
|
|
|
|
if (blob)
|
|
|
|
return new Response(blob, { status: 200 });
|
|
|
|
else
|
|
|
|
return new Response(null, { status: 404 });
|
|
|
|
}
|
|
|
|
return fetch(event.request);
|
2021-10-11 19:52:28 -08:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
if (!snapshotServer)
|
|
|
|
return new Response(null, { status: 404 });
|
|
|
|
return snapshotServer!.serveResource(request.url, snapshotUrl);
|
2021-10-11 19:52:28 -08:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:42:50 -08:00
|
|
|
// @ts-ignore
|
|
|
|
self.addEventListener('fetch', function(event: FetchEvent) {
|
2021-10-11 19:52:28 -08:00
|
|
|
event.respondWith(doFetch(event));
|
|
|
|
});
|