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-21 18:46:11 -07:00
|
|
|
import { RequestChannel, ResponseChannel, RouteChannel, RequestInitializer, ResponseInitializer, RouteInitializer } from '../../protocol/channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { Frame } from './frame';
|
2020-08-18 15:38:29 -07:00
|
|
|
import { headersArrayToObject, headersObjectToArray } from '../../converters';
|
2020-07-29 17:26:59 -07:00
|
|
|
import { Headers } from './types';
|
2020-08-18 15:38:29 -07:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as mime from 'mime';
|
|
|
|
import * as util from 'util';
|
|
|
|
import { helper } from '../../helper';
|
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-06-26 12:28:27 -07:00
|
|
|
export class Request extends ChannelOwner<RequestChannel, RequestInitializer> {
|
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-07-29 17:26:59 -07:00
|
|
|
private _headers: Headers;
|
2020-07-22 15:59:37 -07:00
|
|
|
private _postData: Buffer | null;
|
2020-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
static from(request: RequestChannel): Request {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (request as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
static fromNullable(request: RequestChannel | undefined): Request | null {
|
2020-06-25 16:05:36 -07:00
|
|
|
return request ? Request.from(request) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:00:10 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: RequestInitializer) {
|
|
|
|
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-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> {
|
2020-07-14 18:26:50 -07:00
|
|
|
return Response.fromNullable((await this._channel.response()).response);
|
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
|
|
|
|
|
|
|
_finalRequest(): Request {
|
|
|
|
return this._redirectedTo ? this._redirectedTo._finalRequest() : this;
|
|
|
|
}
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
export class Route extends ChannelOwner<RouteChannel, RouteInitializer> {
|
2020-06-26 11:51:47 -07:00
|
|
|
static from(route: RouteChannel): Route {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (route as any)._object;
|
2020-06-26 11:51:47 -07:00
|
|
|
}
|
|
|
|
|
2020-07-10 18:00:10 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: RouteInitializer) {
|
|
|
|
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) {
|
2020-06-26 11:51:47 -07:00
|
|
|
await this._channel.abort({ errorCode });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-18 15:38:29 -07:00
|
|
|
async fulfill(response: { status?: number, headers?: Headers, contentType?: string, body?: string | Buffer, path?: string }) {
|
|
|
|
let body = '';
|
|
|
|
let isBase64 = false;
|
|
|
|
let length = 0;
|
|
|
|
if (response.path) {
|
|
|
|
const buffer = await util.promisify(fs.readFile)(response.path);
|
|
|
|
body = buffer.toString('base64');
|
|
|
|
isBase64 = true;
|
|
|
|
length = buffer.length;
|
|
|
|
} else if (helper.isString(response.body)) {
|
|
|
|
body = response.body;
|
|
|
|
isBase64 = false;
|
|
|
|
length = Buffer.byteLength(body);
|
|
|
|
} else if (response.body) {
|
|
|
|
body = response.body.toString('base64');
|
|
|
|
isBase64 = true;
|
|
|
|
length = response.body.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
const headers: Headers = {};
|
|
|
|
for (const header of Object.keys(response.headers || {}))
|
|
|
|
headers[header.toLowerCase()] = String(response.headers![header]);
|
|
|
|
if (response.contentType)
|
|
|
|
headers['content-type'] = String(response.contentType);
|
|
|
|
else if (response.path)
|
|
|
|
headers['content-type'] = mime.getType(response.path) || 'application/octet-stream';
|
|
|
|
if (length && !('content-length' in headers))
|
|
|
|
headers['content-length'] = String(length);
|
|
|
|
|
|
|
|
await this._channel.fulfill({
|
|
|
|
status: response.status || 200,
|
|
|
|
headers: headersObjectToArray(headers),
|
|
|
|
body,
|
|
|
|
isBase64
|
|
|
|
});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-18 15:38:29 -07:00
|
|
|
async continue(overrides: { method?: string, headers?: Headers, postData?: string | Buffer } = {}) {
|
|
|
|
const postDataBuffer = helper.isString(overrides.postData) ? Buffer.from(overrides.postData, 'utf8') : overrides.postData;
|
2020-07-24 12:16:45 -07:00
|
|
|
await this._channel.continue({
|
2020-08-18 15:38:29 -07:00
|
|
|
method: overrides.method,
|
|
|
|
headers: overrides.headers ? headersObjectToArray(overrides.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-06-26 12:28:27 -07:00
|
|
|
export class Response extends ChannelOwner<ResponseChannel, ResponseInitializer> {
|
2020-07-29 17:26:59 -07:00
|
|
|
private _headers: Headers;
|
2020-07-15 13:21:21 -07:00
|
|
|
|
2020-06-25 16:05:36 -07:00
|
|
|
static from(response: ResponseChannel): Response {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (response as any)._object;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
static fromNullable(response: ResponseChannel | undefined): Response | null {
|
2020-06-25 16:05:36 -07:00
|
|
|
return response ? Response.from(response) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:00:10 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ResponseInitializer) {
|
|
|
|
super(parent, type, guid, initializer);
|
2020-08-18 15:38:29 -07:00
|
|
|
this._headers = headersArrayToObject(initializer.headers, true /* lowerCase */);
|
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> {
|
2020-07-14 18:26:50 -07: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-06-26 12:28:27 -07:00
|
|
|
return Request.from(this._initializer.request);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
frame(): Frame {
|
2020-06-26 12:28:27 -07:00
|
|
|
return Request.from(this._initializer.request).frame();
|
2020-06-25 16:05:36 -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];
|
|
|
|
if (!Object.is(value, undefined) && !helper.isString(value))
|
|
|
|
throw new Error(`Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
|
|
|
}
|
|
|
|
}
|