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';
|
|
|
|
import * as types from '../../types';
|
2020-06-26 12:28:27 -07:00
|
|
|
import { RequestChannel, ResponseChannel, RouteChannel, RequestInitializer, ResponseInitializer, RouteInitializer } from '../channels';
|
2020-06-25 16:05:36 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { Frame } from './frame';
|
2020-07-01 13:55:29 -07:00
|
|
|
import { ConnectionScope } from './connection';
|
2020-06-29 16:37:38 -07:00
|
|
|
import { normalizeFulfillParameters } from '../serializers';
|
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-06-25 16:05:36 -07:00
|
|
|
|
|
|
|
static from(request: RequestChannel): Request {
|
|
|
|
return request._object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromNullable(request: RequestChannel | null): Request | null {
|
|
|
|
return request ? Request.from(request) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:55:29 -07:00
|
|
|
constructor(scope: ConnectionScope, guid: string, initializer: RequestInitializer) {
|
|
|
|
super(scope, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-26 12:28:27 -07:00
|
|
|
return this._initializer.postData;
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
postDataJSON(): Object | null {
|
2020-06-26 12:28:27 -07:00
|
|
|
if (!this._initializer.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-06-26 12:28:27 -07:00
|
|
|
const parsed = new URLSearchParams(this._initializer.postData);
|
2020-06-25 16:05:36 -07:00
|
|
|
for (const [k, v] of parsed.entries())
|
|
|
|
entries[k] = v;
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:28:27 -07:00
|
|
|
return JSON.parse(this._initializer.postData);
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
headers(): {[key: string]: string} {
|
2020-06-26 12:28:27 -07:00
|
|
|
return { ...this._initializer.headers };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async response(): Promise<Response | null> {
|
|
|
|
return Response.fromNullable(await this._channel.response());
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 {
|
|
|
|
return route._object;
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:55:29 -07:00
|
|
|
constructor(scope: ConnectionScope, guid: string, initializer: RouteInitializer) {
|
|
|
|
super(scope, 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
|
|
|
}
|
|
|
|
|
|
|
|
async abort(errorCode: string = 'failed') {
|
2020-06-26 11:51:47 -07:00
|
|
|
await this._channel.abort({ errorCode });
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async fulfill(response: types.FulfillResponse & { path?: string }) {
|
2020-06-29 16:37:38 -07:00
|
|
|
const normalized = await normalizeFulfillParameters(response);
|
|
|
|
await this._channel.fulfill({ response: {
|
|
|
|
status: normalized.status,
|
|
|
|
headers: normalized.headers,
|
|
|
|
contentType: normalized.contentType,
|
|
|
|
body: (typeof normalized.body === 'string' ? Buffer.from(normalized.body) : normalized.body).toString('base64')
|
|
|
|
}});
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async continue(overrides: { method?: string; headers?: types.Headers; postData?: string } = {}) {
|
2020-06-26 11:51:47 -07:00
|
|
|
await this._channel.continue({ overrides });
|
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-06-25 16:05:36 -07:00
|
|
|
static from(response: ResponseChannel): Response {
|
|
|
|
return response._object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromNullable(response: ResponseChannel | null): Response | null {
|
|
|
|
return response ? Response.from(response) : null;
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:55:29 -07:00
|
|
|
constructor(scope: ConnectionScope, guid: string, initializer: ResponseInitializer) {
|
|
|
|
super(scope, guid, initializer);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
headers(): object {
|
2020-06-26 12:28:27 -07:00
|
|
|
return { ...this._initializer.headers };
|
2020-06-25 16:05:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async finished(): Promise<Error | null> {
|
|
|
|
return await this._channel.finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
async body(): Promise<Buffer> {
|
2020-06-29 18:58:09 -07:00
|
|
|
return Buffer.from(await this._channel.body(), '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
|
|
|
}
|
|
|
|
}
|