This commit is contained in:
Simon Knott 2024-10-15 15:16:34 +02:00
parent e19f0ee735
commit 8687e5505e
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 13 additions and 39 deletions

View file

@ -129,13 +129,6 @@ async function doFetch(event: FetchEvent): Promise<Response> {
return response;
}
if (relativePath.startsWith('/screenshot/')) {
const { snapshotServer } = loadedTraces.get(traceUrl!) || {};
if (!snapshotServer)
return new Response(null, { status: 404 });
return snapshotServer.serveClosestScreenshot(relativePath, url.searchParams);
}
if (relativePath.startsWith('/sha1/')) {
// Sha1 for sources is based on the file path, can't load it of a random model.
const sha1 = relativePath.slice('/sha1/'.length);

View file

@ -78,7 +78,7 @@ export class SnapshotRenderer {
return this._snapshots[this._index].viewport;
}
render(swScope: string, traceURL: string): RenderedFrameSnapshot {
render(screenshotUrl: string | undefined): RenderedFrameSnapshot {
const result: string[] = [];
const visit = (n: NodeSnapshot, snapshotIndex: number, parentTag: string | undefined, parentAttrs: [string, string][] | undefined) => {
// Text node.
@ -154,16 +154,12 @@ export class SnapshotRenderer {
const html = lruCache(this, () => {
visit(snapshot.html, this._index, undefined, undefined);
const screenshotURL = new URL(`./screenshot/${snapshot.pageId}`, swScope);
screenshotURL.searchParams.set('trace', traceURL);
screenshotURL.searchParams.set('name', this.snapshotName!);
const html = result.join('');
// Hide the document in order to prevent flickering. We will unhide once script has processed shadow.
const prefix = snapshot.doctype ? `<!DOCTYPE ${snapshot.doctype}>` : '';
return prefix + [
'<style>*,*::before,*::after { visibility: hidden }</style>',
`<script>${snapshotScript(screenshotURL.toString(), this._callId, this.snapshotName)}</script>`
`<script>${snapshotScript(screenshotUrl, this._callId, this.snapshotName)}</script>`
].join('') + html;
});

View file

@ -47,36 +47,21 @@ export class SnapshotServer {
const snapshot = this._snapshot(pathname.substring('/snapshot'.length), searchParams);
if (!snapshot)
return new Response(null, { status: 404 });
const renderedSnapshot = snapshot.render(swScope, traceUrl);
let screenshotUrl: URL | undefined;
const { wallTime, timestamp, pageId } = snapshot.snapshot();
const page = this._pages.get(pageId);
if (page) {
const closestFrame = (wallTime && page.screencastFrames[0]?.frameSwapWallTime) ? findClosest(page.screencastFrames, frame => frame.frameSwapWallTime!, wallTime) : findClosest(page.screencastFrames, frame => frame.timestamp, timestamp);
if (closestFrame)
screenshotUrl = new URL(`./sha1/${closestFrame.sha1}`, swScope);
}
const renderedSnapshot = snapshot.render(screenshotUrl?.toString());
this._snapshotIds.set(snapshotUrl, snapshot);
return new Response(renderedSnapshot.html, { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' } });
}
async serveClosestScreenshot(pathname: string, searchParams: URLSearchParams): Promise<Response> {
const snapshotRenderer = this._snapshot(pathname.substring('/screenshot'.length), searchParams);
if (!snapshotRenderer)
return new Response(undefined, { status: 404 });
const { wallTime, timestamp, pageId } = snapshotRenderer.snapshot();
const page = this._pages.get(pageId);
if (!page)
return new Response(undefined, { status: 404 });
let sha1 = undefined;
if (wallTime && page.screencastFrames[0]?.frameSwapWallTime)
sha1 = findClosest(page.screencastFrames, frame => frame.frameSwapWallTime!, wallTime)?.sha1;
sha1 ??= findClosest(page.screencastFrames, frame => frame.timestamp, timestamp)?.sha1;
if (!sha1)
return new Response(undefined, { status: 404 });
const blob = await this._resourceLoader(sha1);
if (!blob)
return new Response(undefined, { status: 404 });
return new Response(blob);
}
serveSnapshotInfo(pathname: string, searchParams: URLSearchParams): Response {
const snapshot = this._snapshot(pathname.substring('/snapshotInfo'.length), searchParams);
return this._respondWithJson(snapshot ? {