2021-03-08 19:49:57 -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.
|
|
|
|
*/
|
|
|
|
|
2022-09-20 18:41:51 -07:00
|
|
|
import type { FrameSnapshot, ResourceSnapshot } from '@trace/snapshot';
|
2022-04-04 19:56:04 -08:00
|
|
|
import { rewriteURLForCustomProtocol, SnapshotRenderer } from './snapshotRenderer';
|
2021-03-08 19:49:57 -08:00
|
|
|
|
2023-03-22 09:32:21 -07:00
|
|
|
export class SnapshotStorage {
|
|
|
|
private _resources: ResourceSnapshot[] = [];
|
|
|
|
private _frameSnapshots = new Map<string, {
|
2021-03-08 19:49:57 -08:00
|
|
|
raw: FrameSnapshot[],
|
2023-03-22 09:32:21 -07:00
|
|
|
renderers: SnapshotRenderer[]
|
2021-03-08 19:49:57 -08:00
|
|
|
}>();
|
2021-04-08 22:59:05 +08:00
|
|
|
|
2021-03-08 19:49:57 -08:00
|
|
|
addResource(resource: ResourceSnapshot): void {
|
2022-04-04 19:56:04 -08:00
|
|
|
resource.request.url = rewriteURLForCustomProtocol(resource.request.url);
|
2021-03-08 19:49:57 -08:00
|
|
|
this._resources.push(resource);
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:32:21 -07:00
|
|
|
addFrameSnapshot(snapshot: FrameSnapshot) {
|
2022-04-04 19:56:04 -08:00
|
|
|
for (const override of snapshot.resourceOverrides)
|
|
|
|
override.url = rewriteURLForCustomProtocol(override.url);
|
2021-03-08 19:49:57 -08:00
|
|
|
let frameSnapshots = this._frameSnapshots.get(snapshot.frameId);
|
|
|
|
if (!frameSnapshots) {
|
|
|
|
frameSnapshots = {
|
|
|
|
raw: [],
|
2023-03-22 09:32:21 -07:00
|
|
|
renderers: [],
|
2021-03-08 19:49:57 -08:00
|
|
|
};
|
|
|
|
this._frameSnapshots.set(snapshot.frameId, frameSnapshots);
|
2021-04-20 23:03:56 -07:00
|
|
|
if (snapshot.isMainFrame)
|
|
|
|
this._frameSnapshots.set(snapshot.pageId, frameSnapshots);
|
2021-03-08 19:49:57 -08:00
|
|
|
}
|
|
|
|
frameSnapshots.raw.push(snapshot);
|
2021-08-10 12:08:19 -07:00
|
|
|
const renderer = new SnapshotRenderer(this._resources, frameSnapshots.raw, frameSnapshots.raw.length - 1);
|
2023-03-22 09:32:21 -07:00
|
|
|
frameSnapshots.renderers.push(renderer);
|
|
|
|
return renderer;
|
2021-03-08 19:49:57 -08:00
|
|
|
}
|
|
|
|
|
2021-04-20 23:03:56 -07:00
|
|
|
snapshotByName(pageOrFrameId: string, snapshotName: string): SnapshotRenderer | undefined {
|
|
|
|
const snapshot = this._frameSnapshots.get(pageOrFrameId);
|
2023-03-22 09:32:21 -07:00
|
|
|
return snapshot?.renderers.find(r => r.snapshotName === snapshotName);
|
2021-08-10 21:23:31 -07:00
|
|
|
}
|
2023-05-05 15:12:18 -07:00
|
|
|
|
|
|
|
snapshotsForTest() {
|
|
|
|
return [...this._frameSnapshots.keys()];
|
|
|
|
}
|
2021-03-08 19:49:57 -08:00
|
|
|
}
|