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-13 10:07:29 -08:00
|
|
|
self.addEventListener('install', function(event: any) {
|
|
|
|
self.skipWaiting();
|
|
|
|
});
|
2021-10-11 19:52:28 -08:00
|
|
|
|
|
|
|
self.addEventListener('activate', function(event: any) {
|
|
|
|
event.waitUntil(self.clients.claim());
|
|
|
|
});
|
|
|
|
|
2021-10-13 10:07:29 -08:00
|
|
|
const scopePath = new URL(self.registration.scope).pathname;
|
2021-10-11 19:52:28 -08:00
|
|
|
|
2021-10-13 12:31:54 -08:00
|
|
|
const loadedTraces = new Map<string, { traceModel: TraceModel, snapshotServer: SnapshotServer, clientId: string }>();
|
|
|
|
|
2021-10-22 15:59:17 -08:00
|
|
|
async function loadTrace(trace: string, clientId: string, progress: (done: number, total: number) => void): Promise<TraceModel> {
|
2021-10-13 12:31:54 -08:00
|
|
|
const entry = loadedTraces.get(trace);
|
|
|
|
if (entry)
|
|
|
|
return entry.traceModel;
|
2021-10-12 13:42:50 -08:00
|
|
|
const traceModel = new TraceModel();
|
2021-10-23 10:23:39 -08:00
|
|
|
let url = trace.startsWith('http') || trace.startsWith('blob') ? trace : `file?path=${trace}`;
|
2021-10-22 15:59:17 -08:00
|
|
|
// Dropbox does not support cors.
|
|
|
|
if (url.startsWith('https://www.dropbox.com/'))
|
|
|
|
url = 'https://dl.dropboxusercontent.com/' + url.substring('https://www.dropbox.com/'.length);
|
|
|
|
await traceModel.load(url, progress);
|
2021-10-13 12:31:54 -08:00
|
|
|
const snapshotServer = new SnapshotServer(traceModel.storage());
|
|
|
|
loadedTraces.set(trace, { traceModel, snapshotServer, clientId });
|
2021-10-12 13:42:50 -08:00
|
|
|
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-22 15:59:17 -08:00
|
|
|
const client = await self.clients.get(event.clientId);
|
|
|
|
const snapshotUrl = request.mode === 'navigate' ? request.url : client!.url;
|
2021-10-13 12:31:54 -08:00
|
|
|
const traceUrl = new URL(snapshotUrl).searchParams.get('trace')!;
|
|
|
|
const { snapshotServer } = loadedTraces.get(traceUrl) || {};
|
2021-10-11 19:52:28 -08:00
|
|
|
|
2021-10-13 10:07:29 -08:00
|
|
|
if (request.url.startsWith(self.registration.scope)) {
|
2021-10-13 12:31:54 -08:00
|
|
|
const url = new URL(request.url);
|
|
|
|
|
2021-10-13 10:07:29 -08:00
|
|
|
const relativePath = url.pathname.substring(scopePath.length - 1);
|
2021-10-22 14:14:58 -08:00
|
|
|
if (relativePath === '/ping') {
|
2021-10-13 12:31:54 -08:00
|
|
|
await gc();
|
2021-10-22 14:14:58 -08:00
|
|
|
return new Response(null, { status: 200 });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (relativePath === '/context') {
|
2021-10-22 15:59:17 -08:00
|
|
|
const traceModel = await loadTrace(traceUrl, event.clientId, (done: number, total: number) => {
|
|
|
|
client.postMessage({ method: 'progress', params: { done, total } });
|
|
|
|
});
|
2021-10-12 13:42:50 -08:00
|
|
|
return new Response(JSON.stringify(traceModel!.contextEntry), {
|
|
|
|
status: 200,
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
});
|
|
|
|
}
|
2021-10-13 12:31:54 -08:00
|
|
|
|
2021-11-02 16:35:23 -08:00
|
|
|
if (relativePath.startsWith('/snapshotInfo/')) {
|
2021-10-13 12:31:54 -08:00
|
|
|
if (!snapshotServer)
|
2021-10-12 13:42:50 -08:00
|
|
|
return new Response(null, { status: 404 });
|
2021-11-02 16:35:23 -08:00
|
|
|
return snapshotServer.serveSnapshotInfo(relativePath, url.searchParams);
|
2021-10-12 13:42:50 -08:00
|
|
|
}
|
2021-10-13 12:31:54 -08:00
|
|
|
|
|
|
|
if (relativePath.startsWith('/snapshot/')) {
|
|
|
|
if (!snapshotServer)
|
|
|
|
return new Response(null, { status: 404 });
|
|
|
|
return snapshotServer.serveSnapshot(relativePath, url.searchParams, snapshotUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (relativePath.startsWith('/sha1/')) {
|
|
|
|
// Sha1 is unique, load it from either of the models for simplicity.
|
|
|
|
for (const { traceModel } of loadedTraces.values()) {
|
|
|
|
const blob = await traceModel!.resourceForSha1(relativePath.slice('/sha1/'.length));
|
|
|
|
if (blob)
|
|
|
|
return new Response(blob, { status: 200 });
|
|
|
|
}
|
|
|
|
return new Response(null, { status: 404 });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to network.
|
2021-10-12 13:42:50 -08:00
|
|
|
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 });
|
2021-10-13 12:31:54 -08:00
|
|
|
return snapshotServer.serveResource(request.url, snapshotUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function gc() {
|
|
|
|
const usedTraces = new Set<string>();
|
|
|
|
for (const [traceUrl, entry] of loadedTraces) {
|
|
|
|
const client = await self.clients.get(entry.clientId);
|
|
|
|
if (client)
|
|
|
|
usedTraces.add(traceUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const traceUrl of loadedTraces.keys()) {
|
|
|
|
if (!usedTraces.has(traceUrl))
|
|
|
|
loadedTraces.delete(traceUrl);
|
|
|
|
}
|
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));
|
|
|
|
});
|