2020-05-18 17:22:48 +00:00
|
|
|
import * as url from 'url';
|
|
|
|
import { Readable, Writable } from 'stream';
|
2020-07-13 16:58:49 +00:00
|
|
|
import { app } from 'electron/main';
|
|
|
|
import type { ClientRequestConstructorOptions, UploadProgress } from 'electron/main';
|
2020-09-29 16:03:33 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const {
|
|
|
|
isValidHeaderName,
|
|
|
|
isValidHeaderValue,
|
|
|
|
createURLLoader
|
|
|
|
} = process._linkedBinding('electron_browser_net');
|
2016-09-19 09:21:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const kSupportedProtocols = new Set(['http:', 'https:']);
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-04-02 14:41:19 +00:00
|
|
|
// set of headers that Node.js discards duplicates for
|
|
|
|
// see https://nodejs.org/api/http.html#http_message_headers
|
|
|
|
const discardableDuplicateHeaders = new Set([
|
|
|
|
'content-type',
|
|
|
|
'content-length',
|
|
|
|
'user-agent',
|
|
|
|
'referer',
|
|
|
|
'host',
|
|
|
|
'authorization',
|
|
|
|
'proxy-authorization',
|
|
|
|
'if-modified-since',
|
|
|
|
'if-unmodified-since',
|
|
|
|
'from',
|
|
|
|
'location',
|
|
|
|
'max-forwards',
|
|
|
|
'retry-after',
|
|
|
|
'etag',
|
|
|
|
'last-modified',
|
|
|
|
'server',
|
|
|
|
'age',
|
|
|
|
'expires'
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-04-02 14:41:19 +00:00
|
|
|
|
2016-10-06 12:14:05 +00:00
|
|
|
class IncomingMessage extends Readable {
|
2020-05-18 17:22:48 +00:00
|
|
|
_shouldPush: boolean;
|
|
|
|
_data: (Buffer | null)[];
|
|
|
|
_responseHead: NodeJS.ResponseHead;
|
|
|
|
|
|
|
|
constructor (responseHead: NodeJS.ResponseHead) {
|
2020-03-20 20:28:31 +00:00
|
|
|
super();
|
|
|
|
this._shouldPush = false;
|
|
|
|
this._data = [];
|
|
|
|
this._responseHead = responseHead;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get statusCode () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._responseHead.statusCode;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get statusMessage () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._responseHead.statusMessage;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get headers () {
|
2020-05-18 17:22:48 +00:00
|
|
|
const filteredHeaders: Record<string, string | string[]> = {};
|
2020-03-20 20:28:31 +00:00
|
|
|
const { rawHeaders } = this._responseHead;
|
2020-01-14 17:36:08 +00:00
|
|
|
rawHeaders.forEach(header => {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(filteredHeaders, header.key) &&
|
|
|
|
discardableDuplicateHeaders.has(header.key)) {
|
2019-04-02 14:41:19 +00:00
|
|
|
// do nothing with discardable duplicate headers
|
|
|
|
} else {
|
2020-01-14 17:36:08 +00:00
|
|
|
if (header.key === 'set-cookie') {
|
2019-04-02 14:41:19 +00:00
|
|
|
// keep set-cookie as an array per Node.js rules
|
|
|
|
// see https://nodejs.org/api/http.html#http_message_headers
|
2020-01-14 17:36:08 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(filteredHeaders, header.key)) {
|
2020-05-18 17:22:48 +00:00
|
|
|
(filteredHeaders[header.key] as string[]).push(header.value);
|
2020-01-14 17:36:08 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
filteredHeaders[header.key] = [header.value];
|
2020-01-14 17:36:08 +00:00
|
|
|
}
|
2019-04-02 14:41:19 +00:00
|
|
|
} else {
|
|
|
|
// for non-cookie headers, the values are joined together with ', '
|
2020-01-14 17:36:08 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(filteredHeaders, header.key)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
filteredHeaders[header.key] += `, ${header.value}`;
|
2020-01-14 17:36:08 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
filteredHeaders[header.key] = header.value;
|
2020-01-14 17:36:08 +00:00
|
|
|
}
|
2019-04-02 14:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
return filteredHeaders;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersion () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return `${this.httpVersionMajor}.${this.httpVersionMinor}`;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersionMajor () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._responseHead.httpVersion.major;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
get httpVersionMinor () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return this._responseHead.httpVersion.minor;
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
get rawTrailers () {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('HTTP trailers are not supported');
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
get trailers () {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('HTTP trailers are not supported');
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
_storeInternalData (chunk: Buffer | null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._data.push(chunk);
|
|
|
|
this._pushInternalData();
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_pushInternalData () {
|
2019-11-27 01:01:13 +00:00
|
|
|
while (this._shouldPush && this._data.length > 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const chunk = this._data.shift();
|
|
|
|
this._shouldPush = this.push(chunk);
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:29:25 +00:00
|
|
|
_read () {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._shouldPush = true;
|
|
|
|
this._pushInternalData();
|
2016-10-06 12:14:05 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
/** Writable stream that buffers up everything written to it. */
|
|
|
|
class SlurpStream extends Writable {
|
2020-05-18 17:22:48 +00:00
|
|
|
_data: Buffer;
|
2019-11-27 01:01:13 +00:00
|
|
|
constructor () {
|
2020-03-20 20:28:31 +00:00
|
|
|
super();
|
|
|
|
this._data = Buffer.alloc(0);
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2020-03-20 15:12:18 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
_write (chunk: Buffer, encoding: string, callback: () => void) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._data = Buffer.concat([this._data, chunk]);
|
|
|
|
callback();
|
2016-10-04 15:12:17 +00:00
|
|
|
}
|
2020-07-09 17:18:49 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
data () { return this._data; }
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
class ChunkedBodyStream extends Writable {
|
2020-05-18 17:22:48 +00:00
|
|
|
_pendingChunk: Buffer | undefined;
|
|
|
|
_downstream?: NodeJS.DataPipe;
|
|
|
|
_pendingCallback?: (error?: Error) => void;
|
|
|
|
_clientRequest: ClientRequest;
|
|
|
|
|
|
|
|
constructor (clientRequest: ClientRequest) {
|
2020-03-20 20:28:31 +00:00
|
|
|
super();
|
|
|
|
this._clientRequest = clientRequest;
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
_write (chunk: Buffer, encoding: string, callback: () => void) {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._downstream) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._downstream.write(chunk).then(callback, callback);
|
2016-09-27 10:33:51 +00:00
|
|
|
} else {
|
2019-11-27 01:01:13 +00:00
|
|
|
// the contract of _write is that we won't be called again until we call
|
|
|
|
// the callback, so we're good to just save a single chunk.
|
2020-03-20 20:28:31 +00:00
|
|
|
this._pendingChunk = chunk;
|
|
|
|
this._pendingCallback = callback;
|
2019-11-27 01:01:13 +00:00
|
|
|
|
|
|
|
// The first write to a chunked body stream begins the request.
|
2020-03-20 20:28:31 +00:00
|
|
|
this._clientRequest._startRequest();
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
_final (callback: () => void) {
|
|
|
|
this._downstream!.done();
|
2020-03-20 20:28:31 +00:00
|
|
|
callback();
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
startReading (pipe: NodeJS.DataPipe) {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._downstream) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('two startReading calls???');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
this._downstream = pipe;
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._pendingChunk) {
|
2020-05-18 17:22:48 +00:00
|
|
|
const doneWriting = (maybeError: Error | void) => {
|
|
|
|
const cb = this._pendingCallback!;
|
2020-03-20 20:28:31 +00:00
|
|
|
delete this._pendingCallback;
|
|
|
|
delete this._pendingChunk;
|
2020-05-18 17:22:48 +00:00
|
|
|
cb(maybeError || undefined);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
this._downstream.write(this._pendingChunk).then(doneWriting, doneWriting);
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
type RedirectPolicy = 'manual' | 'follow' | 'error';
|
|
|
|
|
|
|
|
function parseOptions (optionsIn: ClientRequestConstructorOptions | string): NodeJS.CreateURLLoaderOptions & { redirectPolicy: RedirectPolicy, extraHeaders: Record<string, string> } {
|
|
|
|
const options: any = typeof optionsIn === 'string' ? url.parse(optionsIn) : { ...optionsIn };
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
let urlStr: string = options.url;
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!urlStr) {
|
2020-05-18 17:22:48 +00:00
|
|
|
const urlObj: url.UrlObject = {};
|
2020-03-20 20:28:31 +00:00
|
|
|
const protocol = options.protocol || 'http:';
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!kSupportedProtocols.has(protocol)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Protocol "' + protocol + '" not supported');
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
urlObj.protocol = protocol;
|
2017-03-23 19:37:54 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (options.host) {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlObj.host = options.host;
|
2019-11-27 01:01:13 +00:00
|
|
|
} else {
|
|
|
|
if (options.hostname) {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlObj.hostname = options.hostname;
|
2016-10-25 13:47:54 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlObj.hostname = 'localhost';
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
|
|
|
|
if (options.port) {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlObj.port = options.port;
|
2016-10-25 13:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (options.path && / /.test(options.path)) {
|
|
|
|
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
|
|
|
// with an additional rule for ignoring percentage-escaped characters
|
|
|
|
// but that's a) hard to capture in a regular expression that performs
|
|
|
|
// well, and b) possibly too restrictive for real-world usage. That's
|
|
|
|
// why it only scans for spaces because those are guaranteed to create
|
|
|
|
// an invalid request.
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new TypeError('Request path contains unescaped characters');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
const pathObj = url.parse(options.path || '/');
|
|
|
|
urlObj.pathname = pathObj.pathname;
|
|
|
|
urlObj.search = pathObj.search;
|
|
|
|
urlObj.hash = pathObj.hash;
|
|
|
|
urlStr = url.format(urlObj);
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const redirectPolicy = options.redirect || 'follow';
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!['follow', 'error', 'manual'].includes(redirectPolicy)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('redirect mode should be one of follow, error or manual');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (options.headers != null && typeof options.headers !== 'object') {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new TypeError('headers must be an object');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-10-10 14:21:12 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
const urlLoaderOptions: NodeJS.CreateURLLoaderOptions & { redirectPolicy: RedirectPolicy, extraHeaders: Record<string, string | string[]> } = {
|
|
|
|
method: (options.method || 'GET').toUpperCase(),
|
2019-11-27 01:01:13 +00:00
|
|
|
url: urlStr,
|
|
|
|
redirectPolicy,
|
2020-03-20 22:56:02 +00:00
|
|
|
extraHeaders: options.headers || {},
|
2020-05-18 17:22:48 +00:00
|
|
|
body: null as any,
|
2020-09-29 16:03:33 +00:00
|
|
|
useSessionCookies: options.useSessionCookies,
|
|
|
|
credentials: options.credentials
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2020-05-18 17:22:48 +00:00
|
|
|
for (const [name, value] of Object.entries(urlLoaderOptions.extraHeaders!)) {
|
2020-03-23 20:09:45 +00:00
|
|
|
if (!isValidHeaderName(name)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid header name: '${name}'`);
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
2020-03-23 20:09:45 +00:00
|
|
|
if (!isValidHeaderValue(value.toString())) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid value for header '${name}': '${value}'`);
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.session) {
|
2020-05-19 17:18:12 +00:00
|
|
|
// Weak check, but it should be enough to catch 99% of accidental misuses.
|
|
|
|
if (options.session.constructor && options.session.constructor.name === 'Session') {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlLoaderOptions.session = options.session;
|
2019-11-27 01:01:13 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new TypeError('`session` should be an instance of the Session class');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
} else if (options.partition) {
|
|
|
|
if (typeof options.partition === 'string') {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlLoaderOptions.partition = options.partition;
|
2019-11-27 01:01:13 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new TypeError('`partition` should be a string');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return urlLoaderOptions;
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-27 10:33:51 +00:00
|
|
|
|
2020-07-13 16:36:20 +00:00
|
|
|
export class ClientRequest extends Writable implements Electron.ClientRequest {
|
2020-05-18 17:22:48 +00:00
|
|
|
_started: boolean = false;
|
|
|
|
_firstWrite: boolean = false;
|
|
|
|
_aborted: boolean = false;
|
|
|
|
_chunkedEncoding: boolean | undefined;
|
|
|
|
_body: Writable | undefined;
|
|
|
|
_urlLoaderOptions: NodeJS.CreateURLLoaderOptions & { extraHeaders: Record<string, string> };
|
|
|
|
_redirectPolicy: RedirectPolicy;
|
|
|
|
_followRedirectCb?: () => void;
|
|
|
|
_uploadProgress?: { active: boolean, started: boolean, current: number, total: number };
|
|
|
|
_urlLoader?: NodeJS.URLLoader;
|
|
|
|
_response?: IncomingMessage;
|
|
|
|
|
|
|
|
constructor (options: ClientRequestConstructorOptions | string, callback?: (message: IncomingMessage) => void) {
|
2020-03-20 20:28:31 +00:00
|
|
|
super({ autoDestroy: true });
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!app.isReady()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('net module can only be used after app is ready');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-10-20 09:43:26 +00:00
|
|
|
|
2016-09-27 08:21:11 +00:00
|
|
|
if (callback) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.once('response', callback);
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { redirectPolicy, ...urlLoaderOptions } = parseOptions(options);
|
|
|
|
this._urlLoaderOptions = urlLoaderOptions;
|
|
|
|
this._redirectPolicy = redirectPolicy;
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 04:19:49 +00:00
|
|
|
get chunkedEncoding () {
|
|
|
|
return this._chunkedEncoding || false;
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
set chunkedEncoding (value: boolean) {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._started) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('chunkedEncoding can only be set before the request is started');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
if (typeof this._chunkedEncoding !== 'undefined') {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('chunkedEncoding can only be set once');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
this._chunkedEncoding = !!value;
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._chunkedEncoding) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._body = new ChunkedBodyStream(this);
|
2020-05-18 17:22:48 +00:00
|
|
|
this._urlLoaderOptions.body = (pipe: NodeJS.DataPipe) => {
|
|
|
|
(this._body! as ChunkedBodyStream).startReading(pipe);
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2016-09-27 10:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
setHeader (name: string, value: string) {
|
2016-09-29 13:24:28 +00:00
|
|
|
if (typeof name !== 'string') {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new TypeError('`name` should be a string in setHeader(name, value)');
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2017-03-30 16:24:40 +00:00
|
|
|
if (value == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('`value` required in setHeader("' + name + '", value)');
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._started || this._firstWrite) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Can\'t set headers after they are sent');
|
2016-09-29 13:24:28 +00:00
|
|
|
}
|
2020-03-23 20:09:45 +00:00
|
|
|
if (!isValidHeaderName(name)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid header name: '${name}'`);
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-23 20:09:45 +00:00
|
|
|
if (!isValidHeaderValue(value.toString())) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid value for header '${name}': '${value}'`);
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const key = name.toLowerCase();
|
|
|
|
this._urlLoaderOptions.extraHeaders[key] = value;
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
getHeader (name: string) {
|
2017-03-30 19:37:40 +00:00
|
|
|
if (name == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('`name` is required for getHeader(name)');
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const key = name.toLowerCase();
|
|
|
|
return this._urlLoaderOptions.extraHeaders[key];
|
2016-09-21 07:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
removeHeader (name: string) {
|
2017-03-30 19:37:40 +00:00
|
|
|
if (name == null) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('`name` is required for removeHeader(name)');
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._started || this._firstWrite) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Can\'t remove headers after they are sent');
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const key = name.toLowerCase();
|
|
|
|
delete this._urlLoaderOptions.extraHeaders[key];
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 22:08:57 +00:00
|
|
|
_write (chunk: Buffer, encoding: BufferEncoding, callback: () => void) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._firstWrite = true;
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!this._body) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._body = new SlurpStream();
|
2019-11-27 01:01:13 +00:00
|
|
|
this._body.on('finish', () => {
|
2020-05-18 17:22:48 +00:00
|
|
|
this._urlLoaderOptions.body = (this._body as SlurpStream).data();
|
2020-03-20 20:28:31 +00:00
|
|
|
this._startRequest();
|
|
|
|
});
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2019-11-27 01:01:13 +00:00
|
|
|
// TODO: is this the right way to forward to another stream?
|
2020-03-20 20:28:31 +00:00
|
|
|
this._body.write(chunk, encoding, callback);
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
_final (callback: () => void) {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._body) {
|
|
|
|
// TODO: is this the right way to forward to another stream?
|
2020-03-20 20:28:31 +00:00
|
|
|
this._body.end(callback);
|
2019-11-27 01:01:13 +00:00
|
|
|
} else {
|
|
|
|
// end() called without a body, go ahead and start the request
|
2020-03-20 20:28:31 +00:00
|
|
|
this._startRequest();
|
|
|
|
callback();
|
2016-09-27 08:21:11 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
_startRequest () {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._started = true;
|
2020-05-18 17:22:48 +00:00
|
|
|
const stringifyValues = (obj: Record<string, any>) => {
|
|
|
|
const ret: Record<string, string> = {};
|
2020-03-17 20:17:55 +00:00
|
|
|
for (const k of Object.keys(obj)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ret[k] = obj[k].toString();
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return ret;
|
|
|
|
};
|
2020-05-20 17:28:38 +00:00
|
|
|
this._urlLoaderOptions.referrer = this._urlLoaderOptions.extraHeaders.referer || '';
|
2020-03-20 20:28:31 +00:00
|
|
|
const opts = { ...this._urlLoaderOptions, extraHeaders: stringifyValues(this._urlLoaderOptions.extraHeaders) };
|
2020-03-23 20:09:45 +00:00
|
|
|
this._urlLoader = createURLLoader(opts);
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('response-started', (event, finalUrl, responseHead) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const response = this._response = new IncomingMessage(responseHead);
|
|
|
|
this.emit('response', response);
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('data', (event, data) => {
|
2020-05-18 17:22:48 +00:00
|
|
|
this._response!._storeInternalData(Buffer.from(data));
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('complete', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (this._response) { this._response._storeInternalData(null); }
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('error', (event, netErrorString) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const error = new Error(netErrorString);
|
|
|
|
if (this._response) this._response.destroy(error);
|
|
|
|
this._die(error);
|
|
|
|
});
|
2016-09-29 13:24:28 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('login', (event, authInfo, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const handled = this.emit('login', authInfo, callback);
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!handled) {
|
|
|
|
// If there were no listeners, cancel the authentication request.
|
2020-03-20 20:28:31 +00:00
|
|
|
callback();
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
|
|
|
|
this._urlLoader.on('redirect', (event, redirectInfo, headers) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { statusCode, newMethod, newUrl } = redirectInfo;
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._redirectPolicy === 'error') {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._die(new Error('Attempted to redirect, but redirect policy was \'error\''));
|
2019-11-27 01:01:13 +00:00
|
|
|
} else if (this._redirectPolicy === 'manual') {
|
2020-03-20 20:28:31 +00:00
|
|
|
let _followRedirect = false;
|
|
|
|
this._followRedirectCb = () => { _followRedirect = true; };
|
2019-11-27 01:01:13 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.emit('redirect', statusCode, newMethod, newUrl, headers);
|
2019-11-27 01:01:13 +00:00
|
|
|
} finally {
|
2020-05-18 17:22:48 +00:00
|
|
|
this._followRedirectCb = undefined;
|
2019-12-30 23:47:08 +00:00
|
|
|
if (!_followRedirect && !this._aborted) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._die(new Error('Redirect was cancelled'));
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (this._redirectPolicy === 'follow') {
|
|
|
|
// Calling followRedirect() when the redirect policy is 'follow' is
|
|
|
|
// allowed but does nothing. (Perhaps it should throw an error
|
|
|
|
// though...? Since the redirect will happen regardless.)
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._followRedirectCb = () => {};
|
|
|
|
this.emit('redirect', statusCode, newMethod, newUrl, headers);
|
2019-11-27 01:01:13 +00:00
|
|
|
} finally {
|
2020-05-18 17:22:48 +00:00
|
|
|
this._followRedirectCb = undefined;
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._die(new Error(`Unexpected redirect policy '${this._redirectPolicy}'`));
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('upload-progress', (event, position, total) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._uploadProgress = { active: true, started: true, current: position, total };
|
|
|
|
this.emit('upload-progress', position, total); // Undocumented, for now
|
|
|
|
});
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
this._urlLoader.on('download-progress', (event, current) => {
|
|
|
|
if (this._response) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._response.emit('download-progress', current); // Undocumented, for now
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-09-26 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:37:54 +00:00
|
|
|
followRedirect () {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._followRedirectCb) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._followRedirectCb();
|
2019-11-27 01:01:13 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('followRedirect() called, but was not waiting for a redirect');
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2017-03-23 19:37:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:24:28 +00:00
|
|
|
abort () {
|
2019-11-27 01:01:13 +00:00
|
|
|
if (!this._aborted) {
|
2020-03-20 20:28:31 +00:00
|
|
|
process.nextTick(() => { this.emit('abort'); });
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
this._aborted = true;
|
|
|
|
this._die();
|
2016-09-28 13:07:54 +00:00
|
|
|
}
|
2018-07-27 14:15:48 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
_die (err?: Error) {
|
2020-09-17 22:08:57 +00:00
|
|
|
// Node.js assumes that any stream which is ended is no longer capable of emitted events
|
|
|
|
// which is a faulty assumption for the case of an object that is acting like a stream
|
|
|
|
// (our urlRequest). If we don't emit here, this causes errors since we *do* expect
|
|
|
|
// that error events can be emitted after urlRequest.end().
|
|
|
|
if ((this as any)._writableState.destroyed && err) {
|
|
|
|
this.emit('error', err);
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
this.destroy(err);
|
2019-11-27 01:01:13 +00:00
|
|
|
if (this._urlLoader) {
|
2020-03-20 20:28:31 +00:00
|
|
|
this._urlLoader.cancel();
|
|
|
|
if (this._response) this._response.destroy(err);
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2018-07-27 14:15:48 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
|
2020-05-18 17:22:48 +00:00
|
|
|
getUploadProgress (): UploadProgress {
|
|
|
|
return this._uploadProgress ? { ...this._uploadProgress } : { active: false, started: false, current: 0, total: 0 };
|
2019-11-27 01:01:13 +00:00
|
|
|
}
|
2016-09-21 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 16:36:20 +00:00
|
|
|
export function request (options: ClientRequestConstructorOptions | string, callback?: (message: IncomingMessage) => void) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return new ClientRequest(options, callback);
|
2020-07-13 16:36:20 +00:00
|
|
|
}
|