2022-06-15 08:41:46 -07: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
2022-06-17 21:17:30 -07:00
|
|
|
import type { HAREntry, HARFile } from '../../types/types';
|
2022-06-16 07:48:57 -07:00
|
|
|
import { debugLogger } from '../common/debugLogger';
|
|
|
|
import { rewriteErrorMessage } from '../utils/stackTrace';
|
2022-06-17 16:11:22 -07:00
|
|
|
import { ZipFile } from '../utils/zipFile';
|
2022-06-15 08:41:46 -07:00
|
|
|
import type { BrowserContext } from './browserContext';
|
2022-06-17 16:11:22 -07:00
|
|
|
import { Events } from './events';
|
2022-06-15 08:41:46 -07:00
|
|
|
import type { Route } from './network';
|
2022-06-15 16:35:44 -07:00
|
|
|
import type { BrowserContextOptions } from './types';
|
2022-06-15 08:41:46 -07:00
|
|
|
|
2022-06-15 16:35:44 -07:00
|
|
|
type HarOptions = NonNullable<BrowserContextOptions['har']>;
|
2022-06-15 08:41:46 -07:00
|
|
|
|
|
|
|
export class HarRouter {
|
2022-06-15 16:35:44 -07:00
|
|
|
private _pattern: string | RegExp;
|
2022-06-17 16:11:22 -07:00
|
|
|
private _harFile: HARFile;
|
|
|
|
private _zipFile: ZipFile | null;
|
|
|
|
private _options: HarOptions | undefined;
|
2022-06-15 08:41:46 -07:00
|
|
|
|
2022-06-15 16:35:44 -07:00
|
|
|
static async create(options: HarOptions): Promise<HarRouter> {
|
2022-06-17 16:11:22 -07:00
|
|
|
if (options.path.endsWith('.zip')) {
|
|
|
|
const zipFile = new ZipFile(options.path);
|
|
|
|
const har = await zipFile.read('har.har');
|
|
|
|
const harFile = JSON.parse(har.toString()) as HARFile;
|
|
|
|
return new HarRouter(harFile, zipFile, options);
|
|
|
|
}
|
2022-06-15 16:35:44 -07:00
|
|
|
const harFile = JSON.parse(await fs.promises.readFile(options.path, 'utf-8')) as HARFile;
|
2022-06-17 16:11:22 -07:00
|
|
|
return new HarRouter(harFile, null, options);
|
2022-06-15 08:41:46 -07:00
|
|
|
}
|
|
|
|
|
2022-06-17 16:11:22 -07:00
|
|
|
constructor(harFile: HARFile, zipFile: ZipFile | null, options?: HarOptions) {
|
|
|
|
this._harFile = harFile;
|
|
|
|
this._zipFile = zipFile;
|
2022-06-15 16:35:44 -07:00
|
|
|
this._pattern = options?.urlFilter ?? /.*/;
|
2022-06-17 16:11:22 -07:00
|
|
|
this._options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _handle(route: Route) {
|
2022-06-17 21:17:30 -07:00
|
|
|
let entry;
|
2022-06-17 16:11:22 -07:00
|
|
|
try {
|
2022-06-17 21:17:30 -07:00
|
|
|
entry = harFindResponse(this._harFile, {
|
2022-06-17 16:11:22 -07:00
|
|
|
url: route.request().url(),
|
|
|
|
method: route.request().method()
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
rewriteErrorMessage(e, `Error while finding entry for ${route.request().method()} ${route.request().url()} in HAR file:\n${e.message}`);
|
|
|
|
debugLogger.log('api', e);
|
|
|
|
}
|
|
|
|
|
2022-06-17 21:17:30 -07:00
|
|
|
if (entry) {
|
|
|
|
// If navigation is being redirected, restart it with the final url to ensure the document's url changes.
|
|
|
|
if (entry.request.url !== route.request().url() && route.request().isNavigationRequest()) {
|
|
|
|
debugLogger.log('api', `redirecting HAR navigation: ${route.request().url()} => ${entry.request.url}`);
|
|
|
|
await route._abort(undefined, entry.request.url);
|
|
|
|
return;
|
|
|
|
}
|
2022-06-17 16:11:22 -07:00
|
|
|
debugLogger.log('api', `serving from HAR: ${route.request().method()} ${route.request().url()}`);
|
2022-06-17 21:17:30 -07:00
|
|
|
const response = entry.response;
|
2022-06-17 16:11:22 -07:00
|
|
|
const sha1 = (response.content as any)._sha1;
|
|
|
|
|
|
|
|
if (this._zipFile && sha1) {
|
|
|
|
const body = await this._zipFile.read(sha1).catch(() => {
|
|
|
|
debugLogger.log('api', `payload ${sha1} for request ${route.request().url()} is not found in archive`);
|
|
|
|
return null;
|
2022-06-15 16:35:44 -07:00
|
|
|
});
|
2022-06-17 16:11:22 -07:00
|
|
|
if (body) {
|
|
|
|
await route.fulfill({
|
|
|
|
status: response.status,
|
|
|
|
headers: Object.fromEntries(response.headers.map(h => [h.name, h.value])),
|
|
|
|
body
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2022-06-16 07:48:57 -07:00
|
|
|
}
|
2022-06-17 16:11:22 -07:00
|
|
|
|
|
|
|
await route.fulfill({ response });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._options?.fallback === 'continue') {
|
|
|
|
await route.fallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debugLogger.log('api', `request not in HAR, aborting: ${route.request().method()} ${route.request().url()}`);
|
|
|
|
await route.abort();
|
2022-06-15 08:41:46 -07:00
|
|
|
}
|
|
|
|
|
2022-06-15 16:35:44 -07:00
|
|
|
async addRoute(context: BrowserContext) {
|
2022-06-17 16:11:22 -07:00
|
|
|
await context.route(this._pattern, route => this._handle(route));
|
|
|
|
context.once(Events.BrowserContext.Close, () => this.dispose());
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._zipFile?.close();
|
2022-06-15 08:41:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const redirectStatus = [301, 302, 303, 307, 308];
|
|
|
|
|
2022-06-17 21:17:30 -07:00
|
|
|
function harFindResponse(har: HARFile, params: { url: string, method: string }): HAREntry | undefined {
|
2022-06-15 08:41:46 -07:00
|
|
|
const harLog = har.log;
|
|
|
|
const visited = new Set<HAREntry>();
|
|
|
|
let url = params.url;
|
|
|
|
let method = params.method;
|
|
|
|
while (true) {
|
|
|
|
const entry = harLog.entries.find(entry => entry.request.url === url && entry.request.method === method);
|
|
|
|
if (!entry)
|
2022-06-16 07:48:57 -07:00
|
|
|
return;
|
2022-06-15 08:41:46 -07:00
|
|
|
if (visited.has(entry))
|
|
|
|
throw new Error(`Found redirect cycle for ${params.url}`);
|
|
|
|
visited.add(entry);
|
|
|
|
|
|
|
|
const locationHeader = entry.response.headers.find(h => h.name.toLowerCase() === 'location');
|
|
|
|
if (redirectStatus.includes(entry.response.status) && locationHeader) {
|
|
|
|
const locationURL = new URL(locationHeader.value, url);
|
|
|
|
url = locationURL.toString();
|
|
|
|
if ((entry.response.status === 301 || entry.response.status === 302) && method === 'POST' ||
|
|
|
|
entry.response.status === 303 && !['GET', 'HEAD'].includes(method)) {
|
|
|
|
// HTTP-redirect fetch step 13 (https://fetch.spec.whatwg.org/#http-redirect-fetch)
|
|
|
|
method = 'GET';
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-17 21:17:30 -07:00
|
|
|
return entry;
|
2022-06-15 08:41:46 -07:00
|
|
|
}
|
|
|
|
}
|