2020-06-25 16:05:36 -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 { URLSearchParams } from 'url';
|
2020-08-24 17:05:16 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { Frame } from './frame';
|
2020-11-02 14:09:58 -08:00
|
|
|
import { Headers, WaitForEventOptions } from './types';
|
2021-02-11 06:36:15 -08:00
|
|
|
import fs from 'fs';
|
2020-08-18 15:38:29 -07:00
|
|
|
import * as mime from 'mime';
|
|
|
|
import * as util from 'util';
|
2020-08-22 15:13:51 -07:00
|
|
|
import { isString, headersObjectToArray, headersArrayToObject } from '../utils/utils';
|
2020-10-26 22:20:43 -07:00
|
|
|
import { Events } from './events';
|
2020-11-02 14:09:58 -08:00
|
|
|
import { Page } from './page';
|
|
|
|
import { Waiter } from './waiter';
|
2020-12-26 17:05:57 -08:00
|
|
|
import * as api from '../../types/types';
|
2020-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
export type NetworkCookie = {
|
|
|
|
name: string,
|
|
|
|
value: string,
|
|
|
|
domain: string,
|
|
|
|
path: string,
|
|
|
|
expires: number,
|
|
|
|
httpOnly: boolean,
|
|
|
|
secure: boolean,
|
|
|
|
sameSite: 'Strict' | 'Lax' | 'None'
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SetNetworkCookieParam = {
|
|
|
|
name: string,
|
|
|
|
value: string,
|
|
|
|
url?: string,
|
|
|
|
domain?: string,
|
|
|
|
path?: string,
|
|
|
|
expires?: number,
|
|
|
|
httpOnly?: boolean,
|
|
|
|
secure?: boolean,
|
|
|
|
sameSite?: 'Strict' | 'Lax' | 'None'
|
|
|
|
};
|
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
export class Request extends ChannelOwner<channels.RequestChannel, channels.RequestInitializer> implements api.Request {
|
2020-06-25 16:05:36 -07:00
|
|
|
private _redirectedFrom: Request | null = null;
|
|
|
|
private _redirectedTo: Request | null = null;
|
2020-06-26 11:51:47 -07:00
|
|
|
_failureText: string | null = null;
|
2020-10-22 08:49:16 -07:00
|
|
|
_headers: Headers;
|
2020-07-22 15:59:37 -07:00
|
|
|
private _postData: Buffer | null;
|
2020-10-21 23:25:57 -07:00
|
|
|
_timing: ResourceTiming;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(request: channels.RequestChannel): Request {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (request as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static fromNullable(request: channels.RequestChannel | undefined): Request | null {
|
2020-06-25 16:05:36 -07:00
|
|
|
return request ? Request.from(request) : null;
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.RequestInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-06-26 12:28:27 -07:00
|
|
|
this._redirectedFrom = Request.fromNullable(initializer.redirectedFrom);
|
2020-06-25 16:05:36 -07:00
|
|
|
if (this._redirectedFrom)
|
|
|
|
this._redirectedFrom._redirectedTo = this;
|
2020-08-18 15:38:29 -07:00
|
|
|
this._headers = headersArrayToObject(initializer.headers, true /* lowerCase */);
|
2020-07-22 15:59:37 -07:00
|
|
|
this._postData = initializer.postData ? Buffer.from(initializer.postData, 'base64') : null;
|
2020-10-21 23:25:57 -07:00
|
|
|
this._timing = {
|
|
|
|
startTime: 0,
|
|
|
|
domainLookupStart: -1,
|
|
|
|
domainLookupEnd: -1,
|
|
|
|
connectStart: -1,
|
|
|
|
secureConnectionStart: -1,
|
|
|
|
connectEnd: -1,
|
|
|
|
requestStart: -1,
|
|
|
|
responseStart: -1,
|
|
|
|
responseEnd: -1,
|
|
|
|
};
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.url;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resourceType(): string {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.resourceType;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
method(): string {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.method;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
postData(): string | null {
|
2020-07-22 15:59:37 -07:00
|
|
|
return this._postData ? this._postData.toString('utf8') : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
postDataBuffer(): Buffer | null {
|
|
|
|
return this._postData;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
postDataJSON(): Object | null {
|
2020-07-22 15:59:37 -07:00
|
|
|
const postData = this.postData();
|
|
|
|
if (!postData)
|
2020-06-25 16:05:36 -07:00
|
|
|
return null;
|
|
|
|
|
|
|
|
const contentType = this.headers()['content-type'];
|
|
|
|
if (!contentType)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (contentType === 'application/x-www-form-urlencoded') {
|
|
|
|
const entries: Record<string, string> = {};
|
2020-07-22 15:59:37 -07:00
|
|
|
const parsed = new URLSearchParams(postData);
|
2020-06-25 16:05:36 -07:00
|
|
|
for (const [k, v] of parsed.entries())
|
|
|
|
entries[k] = v;
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:59:37 -07:00
|
|
|
return JSON.parse(postData);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
headers(): Headers {
|
2020-07-15 13:21:21 -07:00
|
|
|
return { ...this._headers };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async response(): Promise<Response | null> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('request.response', async (channel: channels.RequestChannel) => {
|
|
|
|
return Response.fromNullable((await channel.response()).response);
|
2021-01-22 06:49:59 -08:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
frame(): Frame {
|
2020-06-26 12:28:27 -07:00
|
|
|
return Frame.from(this._initializer.frame);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isNavigationRequest(): boolean {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.isNavigationRequest;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
redirectedFrom(): Request | null {
|
|
|
|
return this._redirectedFrom;
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectedTo(): Request | null {
|
|
|
|
return this._redirectedTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
failure(): { errorText: string; } | null {
|
|
|
|
if (this._failureText === null)
|
|
|
|
return null;
|
|
|
|
return {
|
|
|
|
errorText: this._failureText
|
|
|
|
};
|
|
|
|
}
|
2020-07-15 18:48:19 -07:00
|
|
|
|
2020-10-21 23:25:57 -07:00
|
|
|
timing(): ResourceTiming {
|
|
|
|
return this._timing;
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:48:19 -07:00
|
|
|
_finalRequest(): Request {
|
|
|
|
return this._redirectedTo ? this._redirectedTo._finalRequest() : this;
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
export class Route extends ChannelOwner<channels.RouteChannel, channels.RouteInitializer> implements api.Route {
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(route: channels.RouteChannel): Route {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (route as any)._object;
|
2020-06-26 11:51:47 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.RouteInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
request(): Request {
|
2020-06-26 12:28:27 -07:00
|
|
|
return Request.from(this._initializer.request);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:14:41 -07:00
|
|
|
async abort(errorCode?: string) {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('route.abort', async (channel: channels.RouteChannel) => {
|
|
|
|
await channel.abort({ errorCode });
|
2021-01-22 06:49:59 -08:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-01-08 16:17:54 -08:00
|
|
|
async fulfill(options: { status?: number, headers?: Headers, contentType?: string, body?: string | Buffer, path?: string } = {}) {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('route.fulfill', async (channel: channels.RouteChannel) => {
|
2021-01-22 06:49:59 -08:00
|
|
|
let body = '';
|
|
|
|
let isBase64 = false;
|
|
|
|
let length = 0;
|
|
|
|
if (options.path) {
|
|
|
|
const buffer = await util.promisify(fs.readFile)(options.path);
|
|
|
|
body = buffer.toString('base64');
|
|
|
|
isBase64 = true;
|
|
|
|
length = buffer.length;
|
|
|
|
} else if (isString(options.body)) {
|
|
|
|
body = options.body;
|
|
|
|
isBase64 = false;
|
|
|
|
length = Buffer.byteLength(body);
|
|
|
|
} else if (options.body) {
|
|
|
|
body = options.body.toString('base64');
|
|
|
|
isBase64 = true;
|
|
|
|
length = options.body.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headers: Headers = {};
|
|
|
|
for (const header of Object.keys(options.headers || {}))
|
|
|
|
headers[header.toLowerCase()] = String(options.headers![header]);
|
|
|
|
if (options.contentType)
|
|
|
|
headers['content-type'] = String(options.contentType);
|
|
|
|
else if (options.path)
|
|
|
|
headers['content-type'] = mime.getType(options.path) || 'application/octet-stream';
|
|
|
|
if (length && !('content-length' in headers))
|
|
|
|
headers['content-length'] = String(length);
|
|
|
|
|
2021-02-19 16:21:39 -08:00
|
|
|
await channel.fulfill({
|
2021-01-22 06:49:59 -08:00
|
|
|
status: options.status || 200,
|
|
|
|
headers: headersObjectToArray(headers),
|
|
|
|
body,
|
|
|
|
isBase64
|
|
|
|
});
|
2020-08-18 15:38:29 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-01-08 16:17:54 -08:00
|
|
|
async continue(options: { url?: string, method?: string, headers?: Headers, postData?: string | Buffer } = {}) {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('route.continue', async (channel: channels.RouteChannel) => {
|
2021-01-22 06:49:59 -08:00
|
|
|
const postDataBuffer = isString(options.postData) ? Buffer.from(options.postData, 'utf8') : options.postData;
|
2021-02-19 16:21:39 -08:00
|
|
|
await channel.continue({
|
2021-01-22 06:49:59 -08:00
|
|
|
url: options.url,
|
|
|
|
method: options.method,
|
|
|
|
headers: options.headers ? headersObjectToArray(options.headers) : undefined,
|
|
|
|
postData: postDataBuffer ? postDataBuffer.toString('base64') : undefined,
|
|
|
|
});
|
2020-07-24 12:16:45 -07:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type RouteHandler = (route: Route, request: Request) => void;
|
|
|
|
|
2020-10-21 23:25:57 -07:00
|
|
|
export type ResourceTiming = {
|
|
|
|
startTime: number;
|
|
|
|
domainLookupStart: number;
|
|
|
|
domainLookupEnd: number;
|
|
|
|
connectStart: number;
|
|
|
|
secureConnectionStart: number;
|
|
|
|
connectEnd: number;
|
|
|
|
requestStart: number;
|
|
|
|
responseStart: number;
|
|
|
|
responseEnd: number;
|
|
|
|
};
|
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
export class Response extends ChannelOwner<channels.ResponseChannel, channels.ResponseInitializer> implements api.Response {
|
2020-07-29 17:26:59 -07:00
|
|
|
private _headers: Headers;
|
2020-10-21 23:25:57 -07:00
|
|
|
private _request: Request;
|
2020-07-15 13:21:21 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(response: channels.ResponseChannel): Response {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (response as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static fromNullable(response: channels.ResponseChannel | undefined): Response | null {
|
2020-06-25 16:05:36 -07:00
|
|
|
return response ? Response.from(response) : null;
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ResponseInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-08-18 15:38:29 -07:00
|
|
|
this._headers = headersArrayToObject(initializer.headers, true /* lowerCase */);
|
2020-10-21 23:25:57 -07:00
|
|
|
this._request = Request.from(this._initializer.request);
|
2020-10-22 08:49:16 -07:00
|
|
|
this._request._headers = headersArrayToObject(initializer.requestHeaders, true /* lowerCase */);
|
2020-10-21 23:25:57 -07:00
|
|
|
Object.assign(this._request._timing, this._initializer.timing);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.url;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ok(): boolean {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.status === 0 || (this._initializer.status >= 200 && this._initializer.status <= 299);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
status(): number {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.status;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
statusText(): string {
|
2020-06-26 12:28:27 -07:00
|
|
|
return this._initializer.statusText;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
headers(): Headers {
|
2020-07-15 13:21:21 -07:00
|
|
|
return { ...this._headers };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async finished(): Promise<Error | null> {
|
2020-07-20 17:38:06 -07:00
|
|
|
const result = await this._channel.finished();
|
|
|
|
if (result.error)
|
2020-07-21 14:40:53 -07:00
|
|
|
return new Error(result.error);
|
2020-07-20 17:38:06 -07:00
|
|
|
return null;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async body(): Promise<Buffer> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('response.body', async (channel: channels.ResponseChannel) => {
|
|
|
|
return Buffer.from((await channel.body()).binary, 'base64');
|
2021-01-22 06:49:59 -08:00
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async text(): Promise<string> {
|
|
|
|
const content = await this.body();
|
|
|
|
return content.toString('utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
async json(): Promise<object> {
|
|
|
|
const content = await this.text();
|
|
|
|
return JSON.parse(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
request(): Request {
|
2020-10-21 23:25:57 -07:00
|
|
|
return this._request;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
frame(): Frame {
|
2020-10-21 23:25:57 -07:00
|
|
|
return this._request.frame();
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 15:38:29 -07:00
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
export class WebSocket extends ChannelOwner<channels.WebSocketChannel, channels.WebSocketInitializer> implements api.WebSocket {
|
2020-11-02 14:09:58 -08:00
|
|
|
private _page: Page;
|
|
|
|
private _isClosed: boolean;
|
|
|
|
|
2020-10-26 22:20:43 -07:00
|
|
|
static from(webSocket: channels.WebSocketChannel): WebSocket {
|
|
|
|
return (webSocket as any)._object;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.WebSocketInitializer) {
|
|
|
|
super(parent, type, guid, initializer);
|
2020-11-02 14:09:58 -08:00
|
|
|
this._isClosed = false;
|
|
|
|
this._page = parent as Page;
|
2020-10-26 22:20:43 -07:00
|
|
|
this._channel.on('frameSent', (event: { opcode: number, data: string }) => {
|
|
|
|
const payload = event.opcode === 2 ? Buffer.from(event.data, 'base64') : event.data;
|
|
|
|
this.emit(Events.WebSocket.FrameSent, { payload });
|
|
|
|
});
|
|
|
|
this._channel.on('frameReceived', (event: { opcode: number, data: string }) => {
|
|
|
|
const payload = event.opcode === 2 ? Buffer.from(event.data, 'base64') : event.data;
|
|
|
|
this.emit(Events.WebSocket.FrameReceived, { payload });
|
|
|
|
});
|
2020-11-19 12:09:42 -08:00
|
|
|
this._channel.on('socketError', ({ error }) => this.emit(Events.WebSocket.Error, error));
|
2020-11-02 14:09:58 -08:00
|
|
|
this._channel.on('close', () => {
|
|
|
|
this._isClosed = true;
|
2021-01-22 09:58:31 -08:00
|
|
|
this.emit(Events.WebSocket.Close, this);
|
2020-11-02 14:09:58 -08:00
|
|
|
});
|
2020-10-26 22:20:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._initializer.url;
|
|
|
|
}
|
2020-11-02 14:09:58 -08:00
|
|
|
|
|
|
|
isClosed(): boolean {
|
|
|
|
return this._isClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
|
|
|
|
const timeout = this._page._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
|
|
|
|
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
|
2021-02-19 18:12:33 -08:00
|
|
|
const waiter = Waiter.createForEvent(this, 'webSocket', event);
|
2020-11-02 14:09:58 -08:00
|
|
|
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
|
|
|
|
if (event !== Events.WebSocket.Error)
|
|
|
|
waiter.rejectOnEvent(this, Events.WebSocket.Error, new Error('Socket error'));
|
|
|
|
if (event !== Events.WebSocket.Close)
|
|
|
|
waiter.rejectOnEvent(this, Events.WebSocket.Close, new Error('Socket closed'));
|
|
|
|
waiter.rejectOnEvent(this._page, Events.Page.Close, new Error('Page closed'));
|
|
|
|
const result = await waiter.waitForEvent(this, event, predicate as any);
|
|
|
|
waiter.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
2020-10-26 22:20:43 -07:00
|
|
|
}
|
|
|
|
|
2020-08-18 15:38:29 -07:00
|
|
|
export function validateHeaders(headers: Headers) {
|
|
|
|
for (const key of Object.keys(headers)) {
|
|
|
|
const value = headers[key];
|
2020-08-22 07:07:13 -07:00
|
|
|
if (!Object.is(value, undefined) && !isString(value))
|
2020-08-18 15:38:29 -07:00
|
|
|
throw new Error(`Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
|
|
|
}
|
|
|
|
}
|