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';
|
2021-09-07 13:27:53 -04:00
|
|
|
import { Headers, RemoteAddr, SecurityDetails, 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';
|
2021-09-11 13:27:00 -07:00
|
|
|
import { isString, headersObjectToArray } from '../utils/utils';
|
2021-08-31 14:44:08 -07:00
|
|
|
import { ManualPromise } from '../utils/async';
|
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';
|
2021-09-11 13:27:00 -07:00
|
|
|
import { HeadersArray, URLMatch } from '../common/types';
|
2021-08-24 20:45:50 +02:00
|
|
|
import { urlMatches } from './clientHelper';
|
2021-09-11 13:27:00 -07:00
|
|
|
import { MultiMap } from '../utils/multimap';
|
2021-11-05 16:27:49 +01:00
|
|
|
import { APIResponse } from './fetch';
|
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'
|
|
|
|
};
|
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class Request extends ChannelOwner<channels.RequestChannel> 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;
|
2021-09-11 13:27:00 -07:00
|
|
|
private _provisionalHeaders: RawHeaders;
|
|
|
|
private _actualHeadersPromise: Promise<RawHeaders> | undefined;
|
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;
|
2021-09-11 13:27:00 -07:00
|
|
|
this._provisionalHeaders = new RawHeaders(initializer.headers);
|
2022-01-24 16:06:36 -07:00
|
|
|
this._postData = initializer.postData !== undefined ? 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 === '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;
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:25:14 -08:00
|
|
|
try {
|
|
|
|
return JSON.parse(postData);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('POST data is not a valid JSON object: ' + postData);
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-09-01 18:28:20 -07:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2020-07-29 17:26:59 -07:00
|
|
|
headers(): Headers {
|
2021-09-11 13:27:00 -07:00
|
|
|
return this._provisionalHeaders.headers();
|
2021-09-07 13:27:53 -04:00
|
|
|
}
|
|
|
|
|
2021-09-11 13:27:00 -07:00
|
|
|
_actualHeaders(): Promise<RawHeaders> {
|
|
|
|
if (!this._actualHeadersPromise) {
|
2021-11-19 16:28:11 -08:00
|
|
|
this._actualHeadersPromise = this._wrapApiCall(async () => {
|
|
|
|
return new RawHeaders((await this._channel.rawRequestHeaders()).headers);
|
2021-09-07 13:27:53 -04:00
|
|
|
});
|
|
|
|
}
|
2021-09-11 13:27:00 -07:00
|
|
|
return this._actualHeadersPromise;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:27:53 -04:00
|
|
|
async allHeaders(): Promise<Headers> {
|
2021-09-11 13:27:00 -07:00
|
|
|
return (await this._actualHeaders()).headers();
|
2021-09-07 13:27:53 -04:00
|
|
|
}
|
|
|
|
|
2021-09-11 13:27:00 -07:00
|
|
|
async headersArray(): Promise<HeadersArray> {
|
|
|
|
return (await this._actualHeaders()).headersArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
async headerValue(name: string): Promise<string | null> {
|
|
|
|
return (await this._actualHeaders()).get(name);
|
2021-09-01 18:28:20 -07:00
|
|
|
}
|
|
|
|
|
2020-06-25 16:05:36 -07:00
|
|
|
async response(): Promise<Response | null> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return Response.fromNullable((await this._channel.response()).response);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-10-15 14:22:49 -08:00
|
|
|
async _internalResponse(): Promise<Response | null> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return this._wrapApiCall(async () => {
|
|
|
|
return Response.fromNullable((await this._channel.response()).response);
|
2021-11-18 22:30:09 -08:00
|
|
|
}, true);
|
2021-10-15 14:22:49 -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;
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:39:57 -07:00
|
|
|
async sizes(): Promise<RequestSizes> {
|
|
|
|
const response = await this.response();
|
|
|
|
if (!response)
|
|
|
|
throw new Error('Unable to fetch sizes for failed request');
|
2021-11-19 16:28:11 -08:00
|
|
|
return (await response._channel.sizes()).sizes;
|
2021-08-27 22:53:57 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class Route extends ChannelOwner<channels.RouteChannel> 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
|
|
|
}
|
|
|
|
|
2021-11-08 15:13:15 -08:00
|
|
|
private _raceWithPageClose(promise: Promise<any>): Promise<void> {
|
|
|
|
const page = this.request().frame()._page;
|
|
|
|
// When page closes or crashes, we catch any potential rejects from this Route.
|
|
|
|
// Note that page could be missing when routing popup's initial request that
|
|
|
|
// does not have a Page initialized just yet.
|
|
|
|
return Promise.race([
|
|
|
|
promise,
|
|
|
|
page ? page._closedOrCrashedPromise : Promise.resolve(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-07-30 11:14:41 -07:00
|
|
|
async abort(errorCode?: string) {
|
2021-11-19 16:28:11 -08:00
|
|
|
await this._raceWithPageClose(this._channel.abort({ errorCode }));
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:56:57 -07:00
|
|
|
async fulfill(options: { response?: api.APIResponse, status?: number, headers?: Headers, contentType?: string, cors?: 'allow' | 'none', body?: string | Buffer, path?: string } = {}) {
|
2021-11-19 16:28:11 -08:00
|
|
|
let fetchResponseUid;
|
|
|
|
let { status: statusOption, headers: headersOption, body } = options;
|
|
|
|
if (options.response) {
|
|
|
|
statusOption ||= options.response.status();
|
|
|
|
headersOption ||= options.response.headers();
|
2022-02-10 12:05:04 -08:00
|
|
|
if (options.body === undefined && options.path === undefined && options.response instanceof APIResponse) {
|
|
|
|
if (options.response._request._connection === this._connection)
|
|
|
|
fetchResponseUid = (options.response as APIResponse)._fetchUid();
|
|
|
|
else
|
|
|
|
body = await options.response.body();
|
|
|
|
}
|
2021-11-19 16:28:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let isBase64 = false;
|
|
|
|
let length = 0;
|
|
|
|
if (options.path) {
|
|
|
|
const buffer = await fs.promises.readFile(options.path);
|
|
|
|
body = buffer.toString('base64');
|
|
|
|
isBase64 = true;
|
|
|
|
length = buffer.length;
|
|
|
|
} else if (isString(body)) {
|
|
|
|
isBase64 = false;
|
|
|
|
length = Buffer.byteLength(body);
|
|
|
|
} else if (body) {
|
|
|
|
length = body.length;
|
|
|
|
body = body.toString('base64');
|
|
|
|
isBase64 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headers: Headers = {};
|
|
|
|
for (const header of Object.keys(headersOption || {}))
|
|
|
|
headers[header.toLowerCase()] = String(headersOption![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);
|
|
|
|
|
|
|
|
await this._raceWithPageClose(this._channel.fulfill({
|
|
|
|
status: statusOption || 200,
|
|
|
|
headers: headersObjectToArray(headers),
|
2022-03-25 14:56:57 -07:00
|
|
|
cors: options.cors,
|
2021-11-19 16:28:11 -08:00
|
|
|
body,
|
|
|
|
isBase64,
|
|
|
|
fetchResponseUid
|
|
|
|
}));
|
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-11-02 17:48:38 -07:00
|
|
|
await this._continue(options);
|
2021-06-18 11:04:48 -07:00
|
|
|
}
|
|
|
|
|
2021-10-15 14:22:49 -08:00
|
|
|
async _internalContinue(options: { url?: string, method?: string, headers?: Headers, postData?: string | Buffer } = {}) {
|
2021-11-02 17:48:38 -07:00
|
|
|
await this._continue(options, true).catch(() => {});
|
2021-10-15 14:22:49 -08:00
|
|
|
}
|
|
|
|
|
2021-11-02 17:48:38 -07:00
|
|
|
private async _continue(options: { url?: string, method?: string, headers?: Headers, postData?: string | Buffer }, isInternal?: boolean) {
|
2021-11-19 16:28:11 -08:00
|
|
|
return await this._wrapApiCall(async () => {
|
2021-01-22 06:49:59 -08:00
|
|
|
const postDataBuffer = isString(options.postData) ? Buffer.from(options.postData, 'utf8') : options.postData;
|
2021-11-19 16:28:11 -08:00
|
|
|
await this._raceWithPageClose(this._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,
|
2021-11-08 15:13:15 -08:00
|
|
|
}));
|
2021-11-18 22:30:09 -08:00
|
|
|
}, isInternal);
|
2021-06-18 11:04:48 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-08-24 20:45:50 +02:00
|
|
|
export type RouteHandlerCallback = (route: Route, request: Request) => void;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
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;
|
2021-08-27 22:53:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export type RequestSizes = {
|
|
|
|
requestBodySize: number;
|
|
|
|
requestHeadersSize: number;
|
|
|
|
responseBodySize: number;
|
|
|
|
responseHeadersSize: number;
|
2020-10-21 23:25:57 -07:00
|
|
|
};
|
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class Response extends ChannelOwner<channels.ResponseChannel> implements api.Response {
|
2021-09-11 13:27:00 -07:00
|
|
|
private _provisionalHeaders: RawHeaders;
|
|
|
|
private _actualHeadersPromise: Promise<RawHeaders> | undefined;
|
2020-10-21 23:25:57 -07:00
|
|
|
private _request: Request;
|
2021-08-30 20:43:40 -07:00
|
|
|
readonly _finishedPromise = new ManualPromise<void>();
|
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);
|
2021-09-11 13:27:00 -07:00
|
|
|
this._provisionalHeaders = new RawHeaders(initializer.headers);
|
2020-10-21 23:25:57 -07:00
|
|
|
this._request = Request.from(this._initializer.request);
|
|
|
|
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 {
|
2021-11-09 23:11:42 +01:00
|
|
|
// Status 0 is for file:// URLs
|
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
|
|
|
}
|
|
|
|
|
2021-09-01 18:28:20 -07:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2020-07-29 17:26:59 -07:00
|
|
|
headers(): Headers {
|
2021-09-11 13:27:00 -07:00
|
|
|
return this._provisionalHeaders.headers();
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2021-09-11 13:27:00 -07:00
|
|
|
async _actualHeaders(): Promise<RawHeaders> {
|
|
|
|
if (!this._actualHeadersPromise) {
|
2021-11-19 16:28:11 -08:00
|
|
|
this._actualHeadersPromise = (async () => {
|
|
|
|
return new RawHeaders((await this._channel.rawResponseHeaders()).headers);
|
|
|
|
})();
|
2021-09-07 13:27:53 -04:00
|
|
|
}
|
2021-09-11 13:27:00 -07:00
|
|
|
return this._actualHeadersPromise;
|
2021-09-01 18:28:20 -07:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:27:53 -04:00
|
|
|
async allHeaders(): Promise<Headers> {
|
2021-09-11 13:27:00 -07:00
|
|
|
return (await this._actualHeaders()).headers();
|
2021-09-07 13:27:53 -04:00
|
|
|
}
|
|
|
|
|
2021-09-11 13:27:00 -07:00
|
|
|
async headersArray(): Promise<HeadersArray> {
|
|
|
|
return (await this._actualHeaders()).headersArray().slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
async headerValue(name: string): Promise<string | null> {
|
|
|
|
return (await this._actualHeaders()).get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async headerValues(name: string): Promise<string[]> {
|
|
|
|
return (await this._actualHeaders()).getAll(name);
|
2021-09-07 13:27:53 -04:00
|
|
|
}
|
|
|
|
|
2021-08-30 20:43:40 -07:00
|
|
|
async finished(): Promise<null> {
|
|
|
|
return this._finishedPromise.then(() => null);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async body(): Promise<Buffer> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return Buffer.from((await this._channel.body()).binary, 'base64');
|
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
|
|
|
}
|
2021-06-17 13:04:55 -07:00
|
|
|
|
|
|
|
async serverAddr(): Promise<RemoteAddr|null> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return (await this._channel.serverAddr()).value || null;
|
2021-06-17 13:04:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async securityDetails(): Promise<SecurityDetails|null> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return (await this._channel.securityDetails()).value || null;
|
2021-06-17 13:04:55 -07:00
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
2020-08-18 15:38:29 -07:00
|
|
|
|
2021-11-17 15:26:01 -08:00
|
|
|
export class WebSocket extends ChannelOwner<channels.WebSocketChannel> 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 }) => {
|
2021-06-28 21:23:29 +02:00
|
|
|
if (event.opcode === 1)
|
|
|
|
this.emit(Events.WebSocket.FrameSent, { payload: event.data });
|
|
|
|
else if (event.opcode === 2)
|
|
|
|
this.emit(Events.WebSocket.FrameSent, { payload: Buffer.from(event.data, 'base64') });
|
2020-10-26 22:20:43 -07:00
|
|
|
});
|
|
|
|
this._channel.on('frameReceived', (event: { opcode: number, data: string }) => {
|
2021-06-28 21:23:29 +02:00
|
|
|
if (event.opcode === 1)
|
|
|
|
this.emit(Events.WebSocket.FrameReceived, { payload: event.data });
|
|
|
|
else if (event.opcode === 2)
|
|
|
|
this.emit(Events.WebSocket.FrameReceived, { payload: Buffer.from(event.data, 'base64') });
|
2020-10-26 22:20:43 -07:00
|
|
|
});
|
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> {
|
2021-11-19 16:28:11 -08:00
|
|
|
return this._wrapApiCall(async () => {
|
2021-06-28 13:27:38 -07:00
|
|
|
const timeout = this._page._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
|
|
|
|
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
|
2021-12-13 13:32:53 -08:00
|
|
|
const waiter = Waiter.createForEvent(this, event);
|
2021-12-06 15:42:57 -08:00
|
|
|
waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`);
|
2021-06-28 13:27:38 -07:00
|
|
|
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-11-02 14:09:58 -08:00
|
|
|
}
|
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.`);
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 20:45:50 +02:00
|
|
|
|
|
|
|
export class RouteHandler {
|
|
|
|
private handledCount = 0;
|
|
|
|
private readonly _baseURL: string | undefined;
|
2021-10-21 10:23:49 -08:00
|
|
|
private readonly _times: number;
|
2021-08-24 20:45:50 +02:00
|
|
|
readonly url: URLMatch;
|
|
|
|
readonly handler: RouteHandlerCallback;
|
|
|
|
|
2021-10-21 10:23:49 -08:00
|
|
|
constructor(baseURL: string | undefined, url: URLMatch, handler: RouteHandlerCallback, times: number = Number.MAX_SAFE_INTEGER) {
|
2021-08-24 20:45:50 +02:00
|
|
|
this._baseURL = baseURL;
|
|
|
|
this._times = times;
|
|
|
|
this.url = url;
|
|
|
|
this.handler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public matches(requestURL: string): boolean {
|
|
|
|
return urlMatches(this._baseURL, requestURL, this.url);
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:19:32 +01:00
|
|
|
public handle(route: Route, request: Request): void {
|
|
|
|
++this.handledCount;
|
|
|
|
this.handler(route, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public isActive(): boolean {
|
|
|
|
return this.handledCount < this._times;
|
2021-08-24 20:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-11 13:27:00 -07:00
|
|
|
|
|
|
|
export class RawHeaders {
|
|
|
|
private _headersArray: HeadersArray;
|
|
|
|
private _headersMap = new MultiMap<string, string>();
|
|
|
|
|
|
|
|
constructor(headers: HeadersArray) {
|
|
|
|
this._headersArray = headers;
|
|
|
|
for (const header of headers)
|
|
|
|
this._headersMap.set(header.name.toLowerCase(), header.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
get(name: string): string | null {
|
|
|
|
const values = this.getAll(name);
|
|
|
|
if (!values || !values.length)
|
|
|
|
return null;
|
|
|
|
return values.join(name.toLowerCase() === 'set-cookie' ? '\n' : ', ');
|
|
|
|
}
|
|
|
|
|
|
|
|
getAll(name: string): string[] {
|
|
|
|
return [...this._headersMap.get(name.toLowerCase())];
|
|
|
|
}
|
|
|
|
|
|
|
|
headers(): Headers {
|
|
|
|
const result: Headers = {};
|
|
|
|
for (const name of this._headersMap.keys())
|
|
|
|
result[name] = this.get(name)!;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
headersArray(): HeadersArray {
|
|
|
|
return this._headersArray;
|
|
|
|
}
|
|
|
|
}
|