2021-02-25 22:09:26 +01: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-03-01 21:20:04 +01:00
|
|
|
import { EventEmitter } from 'events';
|
2021-02-26 23:16:32 +01:00
|
|
|
import { HttpServer } from '../../utils/httpServer';
|
2021-02-25 22:09:26 +01:00
|
|
|
import { BrowserContext } from '../browserContext';
|
2021-03-01 21:20:04 +01:00
|
|
|
import { helper } from '../helper';
|
2021-02-26 23:16:32 +01:00
|
|
|
import { Page } from '../page';
|
2021-02-25 22:09:26 +01:00
|
|
|
import { ContextResources, FrameSnapshot } from './snapshot';
|
|
|
|
|
import { SnapshotRenderer } from './snapshotRenderer';
|
2021-02-26 23:16:32 +01:00
|
|
|
import { NetworkResponse, SnapshotServer, SnapshotStorage } from './snapshotServer';
|
2021-02-25 22:09:26 +01:00
|
|
|
import { Snapshotter, SnapshotterBlob, SnapshotterDelegate, SnapshotterResource } from './snapshotter';
|
|
|
|
|
|
2021-03-01 21:20:04 +01:00
|
|
|
const kSnapshotInterval = 25;
|
|
|
|
|
|
|
|
|
|
export class InMemorySnapshotter extends EventEmitter implements SnapshotStorage, SnapshotterDelegate {
|
2021-02-25 22:09:26 +01:00
|
|
|
private _blobs = new Map<string, Buffer>();
|
|
|
|
|
private _resources = new Map<string, SnapshotterResource>();
|
|
|
|
|
private _frameSnapshots = new Map<string, FrameSnapshot[]>();
|
|
|
|
|
private _snapshots = new Map<string, SnapshotRenderer>();
|
|
|
|
|
private _contextResources: ContextResources = new Map();
|
2021-02-26 23:16:32 +01:00
|
|
|
private _server: HttpServer;
|
2021-02-25 22:09:26 +01:00
|
|
|
private _snapshotter: Snapshotter;
|
|
|
|
|
|
|
|
|
|
constructor(context: BrowserContext) {
|
2021-03-01 21:20:04 +01:00
|
|
|
super();
|
2021-02-26 23:16:32 +01:00
|
|
|
this._server = new HttpServer();
|
|
|
|
|
new SnapshotServer(this._server, this);
|
2021-02-25 22:09:26 +01:00
|
|
|
this._snapshotter = new Snapshotter(context, this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 21:20:04 +01:00
|
|
|
async initialize(): Promise<string> {
|
|
|
|
|
await this._snapshotter.initialize();
|
2021-02-26 23:16:32 +01:00
|
|
|
return await this._server.start();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 21:20:04 +01:00
|
|
|
async start(): Promise<void> {
|
|
|
|
|
await this._snapshotter.setAutoSnapshotInterval(kSnapshotInterval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async dispose() {
|
2021-02-26 23:16:32 +01:00
|
|
|
this._snapshotter.dispose();
|
2021-03-01 21:20:04 +01:00
|
|
|
await this._server.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async captureSnapshot(page: Page, snapshotId: string): Promise<SnapshotRenderer> {
|
|
|
|
|
if (this._snapshots.has(snapshotId))
|
|
|
|
|
throw new Error('Duplicate snapshotId: ' + snapshotId);
|
|
|
|
|
|
|
|
|
|
this._snapshotter.captureSnapshot(page, snapshotId);
|
|
|
|
|
return new Promise<SnapshotRenderer>(fulfill => {
|
|
|
|
|
const listener = helper.addEventListener(this, 'snapshot', (renderer: SnapshotRenderer) => {
|
|
|
|
|
if (renderer.snapshotId === snapshotId) {
|
|
|
|
|
helper.removeEventListeners([listener]);
|
|
|
|
|
fulfill(renderer);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-02-26 23:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-01 21:20:04 +01:00
|
|
|
async setAutoSnapshotInterval(interval: number): Promise<void> {
|
|
|
|
|
await this._snapshotter.setAutoSnapshotInterval(interval);
|
2021-02-26 23:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-25 22:09:26 +01:00
|
|
|
onBlob(blob: SnapshotterBlob): void {
|
|
|
|
|
this._blobs.set(blob.sha1, blob.buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onResource(resource: SnapshotterResource): void {
|
|
|
|
|
this._resources.set(resource.resourceId, resource);
|
|
|
|
|
let resources = this._contextResources.get(resource.url);
|
|
|
|
|
if (!resources) {
|
|
|
|
|
resources = [];
|
|
|
|
|
this._contextResources.set(resource.url, resources);
|
|
|
|
|
}
|
|
|
|
|
resources.push({ frameId: resource.frameId, resourceId: resource.resourceId });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFrameSnapshot(snapshot: FrameSnapshot): void {
|
2021-03-01 21:20:04 +01:00
|
|
|
let frameSnapshots = this._frameSnapshots.get(snapshot.frameId);
|
2021-02-25 22:09:26 +01:00
|
|
|
if (!frameSnapshots) {
|
|
|
|
|
frameSnapshots = [];
|
2021-03-01 21:20:04 +01:00
|
|
|
this._frameSnapshots.set(snapshot.frameId, frameSnapshots);
|
2021-02-25 22:09:26 +01:00
|
|
|
}
|
|
|
|
|
frameSnapshots.push(snapshot);
|
2021-03-01 21:20:04 +01:00
|
|
|
const renderer = new SnapshotRenderer(new Map(this._contextResources), frameSnapshots, frameSnapshots.length - 1);
|
|
|
|
|
this._snapshots.set(snapshot.snapshotId, renderer);
|
|
|
|
|
this.emit('snapshot', renderer);
|
2021-02-25 22:09:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resourceContent(sha1: string): Buffer | undefined {
|
|
|
|
|
return this._blobs.get(sha1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resourceById(resourceId: string): NetworkResponse | undefined {
|
|
|
|
|
return this._resources.get(resourceId)!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snapshotById(snapshotId: string): SnapshotRenderer | undefined {
|
|
|
|
|
return this._snapshots.get(snapshotId);
|
|
|
|
|
}
|
2021-03-01 21:20:04 +01:00
|
|
|
|
|
|
|
|
frameSnapshots(frameId: string): FrameSnapshot[] {
|
|
|
|
|
return this._frameSnapshots.get(frameId) || [];
|
|
|
|
|
}
|
2021-02-25 22:09:26 +01:00
|
|
|
}
|