2021-08-24 14:29:04 -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.
|
|
|
|
*/
|
|
|
|
|
2021-08-26 16:18:54 -07:00
|
|
|
import * as http from 'http';
|
|
|
|
import * as https from 'https';
|
2021-09-22 12:44:22 -07:00
|
|
|
import { HttpsProxyAgent } from 'https-proxy-agent';
|
2021-08-31 09:34:58 -07:00
|
|
|
import { pipeline, Readable, Transform } from 'stream';
|
2021-09-22 12:44:22 -07:00
|
|
|
import url from 'url';
|
|
|
|
import zlib from 'zlib';
|
|
|
|
import { HTTPCredentials } from '../../types/types';
|
|
|
|
import { NameValue, NewRequestOptions } from '../common/types';
|
|
|
|
import { TimeoutSettings } from '../utils/timeoutSettings';
|
2021-09-16 17:48:43 -07:00
|
|
|
import { createGuid, isFilePayload, monotonicTime } from '../utils/utils';
|
2021-09-22 12:44:22 -07:00
|
|
|
import { BrowserContext } from './browserContext';
|
|
|
|
import { MultipartFormData } from './formData';
|
2021-09-14 18:31:35 -07:00
|
|
|
import { SdkObject } from './instrumentation';
|
|
|
|
import { Playwright } from './playwright';
|
2021-09-22 12:44:22 -07:00
|
|
|
import * as types from './types';
|
2021-09-14 18:31:35 -07:00
|
|
|
import { HeadersArray, ProxySettings } from './types';
|
2021-08-27 08:26:19 -07:00
|
|
|
|
2021-08-27 23:47:21 -07:00
|
|
|
|
2021-09-22 12:44:22 -07:00
|
|
|
export type FetchRequestOptions = {
|
2021-09-14 18:31:35 -07:00
|
|
|
userAgent: string;
|
|
|
|
extraHTTPHeaders?: HeadersArray;
|
|
|
|
httpCredentials?: HTTPCredentials;
|
|
|
|
proxy?: ProxySettings;
|
|
|
|
timeoutSettings: TimeoutSettings;
|
|
|
|
ignoreHTTPSErrors?: boolean;
|
|
|
|
baseURL?: string;
|
|
|
|
};
|
2021-08-24 14:29:04 -07:00
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
export abstract class FetchRequest extends SdkObject {
|
2021-09-15 14:02:55 -07:00
|
|
|
static Events = {
|
|
|
|
Dispose: 'dispose',
|
|
|
|
};
|
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
readonly fetchResponses: Map<string, Buffer> = new Map();
|
2021-09-15 14:02:55 -07:00
|
|
|
protected static allInstances: Set<FetchRequest> = new Set();
|
|
|
|
|
|
|
|
static findResponseBody(guid: string): Buffer | undefined {
|
|
|
|
for (const request of FetchRequest.allInstances) {
|
|
|
|
const body = request.fetchResponses.get(guid);
|
|
|
|
if (body)
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-08 10:01:40 -07:00
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
constructor(parent: SdkObject) {
|
|
|
|
super(parent, 'fetchRequest');
|
2021-09-15 14:02:55 -07:00
|
|
|
FetchRequest.allInstances.add(this);
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
2021-09-13 14:29:44 -07:00
|
|
|
|
2021-09-15 14:02:55 -07:00
|
|
|
protected _disposeImpl() {
|
|
|
|
FetchRequest.allInstances.delete(this);
|
2021-09-14 18:31:35 -07:00
|
|
|
this.fetchResponses.clear();
|
2021-09-15 14:02:55 -07:00
|
|
|
this.emit(FetchRequest.Events.Dispose);
|
2021-08-24 14:29:04 -07:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:02:55 -07:00
|
|
|
abstract dispose(): void;
|
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
abstract _defaultOptions(): FetchRequestOptions;
|
|
|
|
abstract _addCookies(cookies: types.SetNetworkCookieParam[]): Promise<void>;
|
|
|
|
abstract _cookies(url: string): Promise<types.NetworkCookie[]>;
|
|
|
|
|
|
|
|
private _storeResponseBody(body: Buffer): string {
|
|
|
|
const uid = createGuid();
|
|
|
|
this.fetchResponses.set(uid, body);
|
|
|
|
return uid;
|
2021-08-26 16:18:54 -07:00
|
|
|
}
|
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
async fetch(params: types.FetchOptions): Promise<{fetchResponse?: Omit<types.FetchResponse, 'body'> & { fetchUid: string }, error?: string}> {
|
|
|
|
try {
|
|
|
|
const headers: { [name: string]: string } = {};
|
|
|
|
const defaults = this._defaultOptions();
|
|
|
|
headers['user-agent'] = defaults.userAgent;
|
|
|
|
headers['accept'] = '*/*';
|
|
|
|
headers['accept-encoding'] = 'gzip,deflate,br';
|
|
|
|
|
|
|
|
if (defaults.extraHTTPHeaders) {
|
2021-09-27 18:58:08 +02:00
|
|
|
for (const { name, value } of defaults.extraHTTPHeaders)
|
2021-09-14 18:31:35 -07:00
|
|
|
headers[name.toLowerCase()] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.headers) {
|
|
|
|
for (const [name, value] of Object.entries(params.headers))
|
|
|
|
headers[name.toLowerCase()] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const method = params.method?.toUpperCase() || 'GET';
|
|
|
|
const proxy = defaults.proxy;
|
|
|
|
let agent;
|
|
|
|
if (proxy) {
|
|
|
|
// TODO: support bypass proxy
|
|
|
|
const proxyOpts = url.parse(proxy.server);
|
|
|
|
if (proxy.username)
|
|
|
|
proxyOpts.auth = `${proxy.username}:${proxy.password || ''}`;
|
|
|
|
agent = new HttpsProxyAgent(proxyOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = defaults.timeoutSettings.timeout(params);
|
2021-09-22 19:30:56 +02:00
|
|
|
const deadline = timeout && (monotonicTime() + timeout);
|
2021-09-14 18:31:35 -07:00
|
|
|
|
|
|
|
const options: https.RequestOptions & { maxRedirects: number, deadline: number } = {
|
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
agent,
|
|
|
|
maxRedirects: 20,
|
|
|
|
timeout,
|
|
|
|
deadline
|
|
|
|
};
|
|
|
|
// rejectUnauthorized = undefined is treated as true in node 12.
|
|
|
|
if (defaults.ignoreHTTPSErrors)
|
|
|
|
options.rejectUnauthorized = false;
|
|
|
|
|
|
|
|
const requestUrl = new URL(params.url, defaults.baseURL);
|
|
|
|
if (params.params) {
|
|
|
|
for (const [name, value] of Object.entries(params.params))
|
|
|
|
requestUrl.searchParams.set(name, value);
|
|
|
|
}
|
|
|
|
|
2021-09-16 17:48:43 -07:00
|
|
|
let postData;
|
|
|
|
if (['POST', 'PUSH', 'PATCH'].includes(method))
|
2021-09-17 12:43:00 -07:00
|
|
|
postData = params.formData ? serializeFormData(params.formData, headers) : params.postData;
|
2021-09-16 17:48:43 -07:00
|
|
|
else if (params.postData || params.formData)
|
|
|
|
throw new Error(`Method ${method} does not accept post data`);
|
2021-09-17 09:00:18 -07:00
|
|
|
if (postData) {
|
|
|
|
headers['content-length'] = String(postData.byteLength);
|
|
|
|
headers['content-type'] ??= 'application/octet-stream';
|
|
|
|
}
|
2021-09-16 17:48:43 -07:00
|
|
|
const fetchResponse = await this._sendRequest(requestUrl, options, postData);
|
2021-09-14 18:31:35 -07:00
|
|
|
const fetchUid = this._storeResponseBody(fetchResponse.body);
|
|
|
|
if (params.failOnStatusCode && (fetchResponse.status < 200 || fetchResponse.status >= 400))
|
|
|
|
return { error: `${fetchResponse.status} ${fetchResponse.statusText}` };
|
|
|
|
return { fetchResponse: { ...fetchResponse, fetchUid } };
|
|
|
|
} catch (e) {
|
|
|
|
return { error: String(e) };
|
|
|
|
}
|
2021-08-27 15:28:36 -07:00
|
|
|
}
|
2021-08-26 16:18:54 -07:00
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
private async _updateCookiesFromHeader(responseUrl: string, setCookie: string[]) {
|
|
|
|
const url = new URL(responseUrl);
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4
|
|
|
|
const defaultPath = '/' + url.pathname.substr(1).split('/').slice(0, -1).join('/');
|
|
|
|
const cookies: types.SetNetworkCookieParam[] = [];
|
|
|
|
for (const header of setCookie) {
|
|
|
|
// Decode cookie value?
|
|
|
|
const cookie: types.SetNetworkCookieParam | null = parseCookie(header);
|
|
|
|
if (!cookie)
|
|
|
|
continue;
|
|
|
|
if (!cookie.domain)
|
|
|
|
cookie.domain = url.hostname;
|
|
|
|
if (!canSetCookie(cookie.domain!, url.hostname))
|
|
|
|
continue;
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4
|
|
|
|
if (!cookie.path || !cookie.path.startsWith('/'))
|
|
|
|
cookie.path = defaultPath;
|
|
|
|
cookies.push(cookie);
|
|
|
|
}
|
|
|
|
if (cookies.length)
|
|
|
|
await this._addCookies(cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _updateRequestCookieHeader(url: URL, options: http.RequestOptions) {
|
|
|
|
if (options.headers!['cookie'] !== undefined)
|
|
|
|
return;
|
|
|
|
const cookies = await this._cookies(url.toString());
|
|
|
|
if (cookies.length) {
|
|
|
|
const valueArray = cookies.map(c => `${c.name}=${c.value}`);
|
|
|
|
options.headers!['cookie'] = valueArray.join('; ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _sendRequest(url: URL, options: https.RequestOptions & { maxRedirects: number, deadline: number }, postData?: Buffer): Promise<types.FetchResponse>{
|
|
|
|
await this._updateRequestCookieHeader(url, options);
|
|
|
|
return new Promise<types.FetchResponse>((fulfill, reject) => {
|
|
|
|
const requestConstructor: ((url: URL, options: http.RequestOptions, callback?: (res: http.IncomingMessage) => void) => http.ClientRequest)
|
|
|
|
= (url.protocol === 'https:' ? https : http).request;
|
|
|
|
const request = requestConstructor(url, options, async response => {
|
|
|
|
if (response.headers['set-cookie'])
|
|
|
|
await this._updateCookiesFromHeader(response.url || url.toString(), response.headers['set-cookie']);
|
|
|
|
if (redirectStatus.includes(response.statusCode!)) {
|
|
|
|
if (!options.maxRedirects) {
|
|
|
|
reject(new Error('Max redirect count exceeded'));
|
|
|
|
request.abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const headers = { ...options.headers };
|
|
|
|
delete headers[`cookie`];
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 13 (https://fetch.spec.whatwg.org/#http-redirect-fetch)
|
|
|
|
const status = response.statusCode!;
|
|
|
|
let method = options.method!;
|
|
|
|
if ((status === 301 || status === 302) && method === 'POST' ||
|
|
|
|
status === 303 && !['GET', 'HEAD'].includes(method)) {
|
|
|
|
method = 'GET';
|
|
|
|
postData = undefined;
|
|
|
|
delete headers[`content-encoding`];
|
|
|
|
delete headers[`content-language`];
|
|
|
|
delete headers[`content-location`];
|
|
|
|
delete headers[`content-type`];
|
|
|
|
}
|
|
|
|
|
|
|
|
const redirectOptions: http.RequestOptions & { maxRedirects: number, deadline: number } = {
|
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
agent: options.agent,
|
|
|
|
maxRedirects: options.maxRedirects - 1,
|
|
|
|
timeout: options.timeout,
|
|
|
|
deadline: options.deadline
|
|
|
|
};
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 4: If locationURL is null, then return response.
|
|
|
|
if (response.headers.location) {
|
|
|
|
const locationURL = new URL(response.headers.location, url);
|
|
|
|
fulfill(this._sendRequest(locationURL, redirectOptions, postData));
|
|
|
|
request.abort();
|
|
|
|
return;
|
|
|
|
}
|
2021-08-26 16:18:54 -07:00
|
|
|
}
|
2021-09-14 18:31:35 -07:00
|
|
|
if (response.statusCode === 401 && !options.headers!['authorization']) {
|
|
|
|
const auth = response.headers['www-authenticate'];
|
|
|
|
const credentials = this._defaultOptions().httpCredentials;
|
|
|
|
if (auth?.trim().startsWith('Basic ') && credentials) {
|
2021-09-27 18:58:08 +02:00
|
|
|
const { username, password } = credentials;
|
2021-09-14 18:31:35 -07:00
|
|
|
const encoded = Buffer.from(`${username || ''}:${password || ''}`).toString('base64');
|
|
|
|
options.headers!['authorization'] = `Basic ${encoded}`;
|
|
|
|
fulfill(this._sendRequest(url, options, postData));
|
|
|
|
request.abort();
|
|
|
|
return;
|
|
|
|
}
|
2021-08-26 16:18:54 -07:00
|
|
|
}
|
2021-09-14 18:31:35 -07:00
|
|
|
response.on('aborted', () => reject(new Error('aborted')));
|
2021-08-26 16:18:54 -07:00
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
let body: Readable = response;
|
|
|
|
let transform: Transform | undefined;
|
|
|
|
const encoding = response.headers['content-encoding'];
|
|
|
|
if (encoding === 'gzip' || encoding === 'x-gzip') {
|
|
|
|
transform = zlib.createGunzip({
|
|
|
|
flush: zlib.constants.Z_SYNC_FLUSH,
|
|
|
|
finishFlush: zlib.constants.Z_SYNC_FLUSH
|
|
|
|
});
|
|
|
|
} else if (encoding === 'br') {
|
|
|
|
transform = zlib.createBrotliDecompress();
|
|
|
|
} else if (encoding === 'deflate') {
|
|
|
|
transform = zlib.createInflate();
|
2021-08-26 16:18:54 -07:00
|
|
|
}
|
2021-09-14 18:31:35 -07:00
|
|
|
if (transform) {
|
|
|
|
body = pipeline(response, transform, e => {
|
|
|
|
if (e)
|
|
|
|
reject(new Error(`failed to decompress '${encoding}' encoding: ${e}`));
|
|
|
|
});
|
2021-08-27 23:47:33 -07:00
|
|
|
}
|
2021-08-31 09:34:58 -07:00
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
const chunks: Buffer[] = [];
|
|
|
|
body.on('data', chunk => chunks.push(chunk));
|
|
|
|
body.on('end', () => {
|
|
|
|
const body = Buffer.concat(chunks);
|
|
|
|
fulfill({
|
|
|
|
url: response.url || url.toString(),
|
|
|
|
status: response.statusCode || 0,
|
|
|
|
statusText: response.statusMessage || '',
|
|
|
|
headers: toHeadersArray(response.rawHeaders),
|
|
|
|
body
|
|
|
|
});
|
2021-08-26 16:18:54 -07:00
|
|
|
});
|
2021-09-14 18:31:35 -07:00
|
|
|
body.on('error',reject);
|
2021-08-26 16:18:54 -07:00
|
|
|
});
|
2021-09-14 18:31:35 -07:00
|
|
|
request.on('error', reject);
|
2021-09-22 19:30:56 +02:00
|
|
|
|
|
|
|
if (options.deadline) {
|
|
|
|
const rejectOnTimeout = () => {
|
|
|
|
reject(new Error(`Request timed out after ${options.timeout}ms`));
|
|
|
|
request.abort();
|
|
|
|
};
|
|
|
|
const remaining = options.deadline - monotonicTime();
|
|
|
|
if (remaining <= 0) {
|
|
|
|
rejectOnTimeout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request.setTimeout(remaining, rejectOnTimeout);
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
2021-09-22 19:30:56 +02:00
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
if (postData)
|
|
|
|
request.write(postData);
|
|
|
|
request.end();
|
2021-08-26 16:18:54 -07:00
|
|
|
});
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BrowserContextFetchRequest extends FetchRequest {
|
|
|
|
private readonly _context: BrowserContext;
|
|
|
|
|
|
|
|
constructor(context: BrowserContext) {
|
|
|
|
super(context);
|
|
|
|
this._context = context;
|
2021-09-15 14:02:55 -07:00
|
|
|
context.once(BrowserContext.Events.Close, () => this._disposeImpl());
|
|
|
|
}
|
|
|
|
|
|
|
|
override dispose() {
|
|
|
|
this.fetchResponses.clear();
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_defaultOptions(): FetchRequestOptions {
|
|
|
|
return {
|
|
|
|
userAgent: this._context._options.userAgent || this._context._browser.userAgent(),
|
|
|
|
extraHTTPHeaders: this._context._options.extraHTTPHeaders,
|
|
|
|
httpCredentials: this._context._options.httpCredentials,
|
|
|
|
proxy: this._context._options.proxy || this._context._browser.options.proxy,
|
|
|
|
timeoutSettings: this._context._timeoutSettings,
|
|
|
|
ignoreHTTPSErrors: this._context._options.ignoreHTTPSErrors,
|
|
|
|
baseURL: this._context._options.baseURL,
|
2021-09-08 10:01:40 -07:00
|
|
|
};
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async _addCookies(cookies: types.SetNetworkCookieParam[]): Promise<void> {
|
|
|
|
await this._context.addCookies(cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _cookies(url: string): Promise<types.NetworkCookie[]> {
|
|
|
|
return await this._context.cookies(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class GlobalFetchRequest extends FetchRequest {
|
2021-09-22 12:44:22 -07:00
|
|
|
private readonly _options: FetchRequestOptions;
|
|
|
|
constructor(playwright: Playwright, options: Omit<NewRequestOptions, 'extraHTTPHeaders'> & { extraHTTPHeaders?: NameValue[] }) {
|
2021-09-14 18:31:35 -07:00
|
|
|
super(playwright);
|
2021-09-22 12:44:22 -07:00
|
|
|
const timeoutSettings = new TimeoutSettings();
|
|
|
|
if (options.timeout !== undefined)
|
|
|
|
timeoutSettings.setDefaultTimeout(options.timeout);
|
|
|
|
const proxy = options.proxy;
|
|
|
|
if (proxy?.server) {
|
|
|
|
let url = proxy?.server.trim();
|
|
|
|
if (!/^\w+:\/\//.test(url))
|
|
|
|
url = 'http://' + url;
|
|
|
|
proxy.server = url;
|
|
|
|
}
|
|
|
|
this._options = {
|
|
|
|
baseURL: options.baseURL,
|
|
|
|
userAgent: options.userAgent || '',
|
|
|
|
extraHTTPHeaders: options.extraHTTPHeaders,
|
|
|
|
ignoreHTTPSErrors: !!options.ignoreHTTPSErrors,
|
|
|
|
httpCredentials: options.httpCredentials,
|
|
|
|
proxy,
|
|
|
|
timeoutSettings,
|
|
|
|
};
|
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:02:55 -07:00
|
|
|
override dispose() {
|
|
|
|
this._disposeImpl();
|
|
|
|
}
|
|
|
|
|
2021-09-14 18:31:35 -07:00
|
|
|
_defaultOptions(): FetchRequestOptions {
|
2021-09-22 12:44:22 -07:00
|
|
|
return this._options;
|
2021-09-14 18:31:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async _addCookies(cookies: types.SetNetworkCookieParam[]): Promise<void> {
|
|
|
|
}
|
|
|
|
|
|
|
|
async _cookies(url: string): Promise<types.NetworkCookie[]> {
|
|
|
|
return [];
|
|
|
|
}
|
2021-08-26 16:18:54 -07:00
|
|
|
}
|
|
|
|
|
2021-09-10 14:03:56 -07:00
|
|
|
function toHeadersArray(rawHeaders: string[]): types.HeadersArray {
|
2021-08-26 16:18:54 -07:00
|
|
|
const result: types.HeadersArray = [];
|
2021-09-10 14:03:56 -07:00
|
|
|
for (let i = 0; i < rawHeaders.length; i += 2)
|
|
|
|
result.push({ name: rawHeaders[i], value: rawHeaders[i + 1] });
|
2021-08-26 16:18:54 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const redirectStatus = [301, 302, 303, 307, 308];
|
|
|
|
|
2021-08-24 14:29:04 -07:00
|
|
|
function canSetCookie(cookieDomain: string, hostname: string) {
|
|
|
|
// TODO: check public suffix list?
|
|
|
|
hostname = '.' + hostname;
|
|
|
|
if (!cookieDomain.startsWith('.'))
|
|
|
|
cookieDomain = '.' + cookieDomain;
|
|
|
|
return hostname.endsWith(cookieDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseCookie(header: string) {
|
|
|
|
const pairs = header.split(';').filter(s => s.trim().length > 0).map(p => p.split('=').map(s => s.trim()));
|
|
|
|
if (!pairs.length)
|
|
|
|
return null;
|
|
|
|
const [name, value] = pairs[0];
|
|
|
|
const cookie: types.NetworkCookie = {
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
domain: '',
|
|
|
|
path: '',
|
|
|
|
expires: -1,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
sameSite: 'Lax' // None for non-chromium
|
|
|
|
};
|
|
|
|
for (let i = 1; i < pairs.length; i++) {
|
|
|
|
const [name, value] = pairs[i];
|
|
|
|
switch (name.toLowerCase()) {
|
|
|
|
case 'expires':
|
|
|
|
const expiresMs = (+new Date(value));
|
|
|
|
if (isFinite(expiresMs))
|
|
|
|
cookie.expires = expiresMs / 1000;
|
|
|
|
break;
|
|
|
|
case 'max-age':
|
|
|
|
const maxAgeSec = parseInt(value, 10);
|
|
|
|
if (isFinite(maxAgeSec))
|
|
|
|
cookie.expires = Date.now() / 1000 + maxAgeSec;
|
|
|
|
break;
|
|
|
|
case 'domain':
|
|
|
|
cookie.domain = value || '';
|
|
|
|
break;
|
|
|
|
case 'path':
|
|
|
|
cookie.path = value || '';
|
|
|
|
break;
|
|
|
|
case 'secure':
|
|
|
|
cookie.secure = true;
|
|
|
|
break;
|
|
|
|
case 'httponly':
|
|
|
|
cookie.httpOnly = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cookie;
|
|
|
|
}
|
2021-09-16 17:48:43 -07:00
|
|
|
|
2021-09-17 12:43:00 -07:00
|
|
|
function serializeFormData(data: any, headers: { [name: string]: string }): Buffer {
|
2021-09-16 17:48:43 -07:00
|
|
|
const contentType = headers['content-type'] || 'application/json';
|
|
|
|
if (contentType === 'application/json') {
|
|
|
|
const json = JSON.stringify(data);
|
|
|
|
headers['content-type'] ??= contentType;
|
|
|
|
return Buffer.from(json, 'utf8');
|
|
|
|
} else if (contentType === 'application/x-www-form-urlencoded') {
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
for (const [name, value] of Object.entries(data))
|
|
|
|
searchParams.append(name, String(value));
|
|
|
|
return Buffer.from(searchParams.toString(), 'utf8');
|
|
|
|
} else if (contentType === 'multipart/form-data') {
|
|
|
|
const formData = new MultipartFormData();
|
|
|
|
for (const [name, value] of Object.entries(data)) {
|
|
|
|
if (isFilePayload(value)) {
|
|
|
|
const payload = value as types.FilePayload;
|
|
|
|
formData.addFileField(name, payload);
|
|
|
|
} else if (value !== undefined) {
|
|
|
|
formData.addField(name, String(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
headers['content-type'] = formData.contentTypeHeader();
|
|
|
|
return formData.finish();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Cannot serialize data using content type: ${contentType}`);
|
|
|
|
}
|
|
|
|
}
|