1 process.emitWarning("The .es.js file is deprecated. Use .mjs instead.");
3 import Stream from 'stream';
4 import http from 'http';
6 import whatwgUrl from 'whatwg-url';
7 import https from 'https';
8 import zlib from 'zlib';
10 // Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js
12 // fix for "Readable" isn't a named export issue
13 const Readable = Stream.Readable;
15 const BUFFER = Symbol('buffer');
16 const TYPE = Symbol('type');
22 const blobParts = arguments[0];
23 const options = arguments[1];
30 const length = Number(a.length);
31 for (let i = 0; i < length; i++) {
34 if (element instanceof Buffer) {
36 } else if (ArrayBuffer.isView(element)) {
37 buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
38 } else if (element instanceof ArrayBuffer) {
39 buffer = Buffer.from(element);
40 } else if (element instanceof Blob) {
41 buffer = element[BUFFER];
43 buffer = Buffer.from(typeof element === 'string' ? element : String(element));
45 size += buffer.length;
50 this[BUFFER] = Buffer.concat(buffers);
52 let type = options && options.type !== undefined && String(options.type).toLowerCase();
53 if (type && !/[^\u0020-\u007E]/.test(type)) {
58 return this[BUFFER].length;
64 return Promise.resolve(this[BUFFER].toString());
67 const buf = this[BUFFER];
68 const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
69 return Promise.resolve(ab);
72 const readable = new Readable();
73 readable._read = function () {};
74 readable.push(this[BUFFER]);
79 return '[object Blob]';
82 const size = this.size;
84 const start = arguments[0];
85 const end = arguments[1];
86 let relativeStart, relativeEnd;
87 if (start === undefined) {
89 } else if (start < 0) {
90 relativeStart = Math.max(size + start, 0);
92 relativeStart = Math.min(start, size);
94 if (end === undefined) {
97 relativeEnd = Math.max(size + end, 0);
99 relativeEnd = Math.min(end, size);
101 const span = Math.max(relativeEnd - relativeStart, 0);
103 const buffer = this[BUFFER];
104 const slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
105 const blob = new Blob([], { type: arguments[2] });
106 blob[BUFFER] = slicedBuffer;
111 Object.defineProperties(Blob.prototype, {
112 size: { enumerable: true },
113 type: { enumerable: true },
114 slice: { enumerable: true }
117 Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
127 * FetchError interface for operational errors
131 * Create FetchError instance
133 * @param String message Error message for human
134 * @param String type Error type for machine
135 * @param String systemError For Node.js system error
138 function FetchError(message, type, systemError) {
139 Error.call(this, message);
141 this.message = message;
144 // when err.type is `system`, err.code contains system error code
146 this.code = this.errno = systemError.code;
149 // hide custom error implementation details from end-users
150 Error.captureStackTrace(this, this.constructor);
153 FetchError.prototype = Object.create(Error.prototype);
154 FetchError.prototype.constructor = FetchError;
155 FetchError.prototype.name = 'FetchError';
159 convert = require('encoding').convert;
162 const INTERNALS = Symbol('Body internals');
164 // fix an issue where "PassThrough" isn't a named export for node <10
165 const PassThrough = Stream.PassThrough;
170 * Ref: https://fetch.spec.whatwg.org/#body
172 * @param Stream body Readable stream
173 * @param Object opts Response options
176 function Body(body) {
179 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
180 _ref$size = _ref.size;
182 let size = _ref$size === undefined ? 0 : _ref$size;
183 var _ref$timeout = _ref.timeout;
184 let timeout = _ref$timeout === undefined ? 0 : _ref$timeout;
187 // body is undefined or null
189 } else if (isURLSearchParams(body)) {
190 // body is a URLSearchParams
191 body = Buffer.from(body.toString());
192 } else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
193 // body is ArrayBuffer
194 body = Buffer.from(body);
195 } else if (ArrayBuffer.isView(body)) {
196 // body is ArrayBufferView
197 body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
198 } else if (body instanceof Stream) ; else {
200 // coerce to string then buffer
201 body = Buffer.from(String(body));
209 this.timeout = timeout;
211 if (body instanceof Stream) {
212 body.on('error', function (err) {
213 const error = err.name === 'AbortError' ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, 'system', err);
214 _this[INTERNALS].error = error;
221 return this[INTERNALS].body;
225 return this[INTERNALS].disturbed;
229 * Decode response as ArrayBuffer
234 return consumeBody.call(this).then(function (buf) {
235 return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
240 * Return raw response as Blob
245 let ct = this.headers && this.headers.get('content-type') || '';
246 return consumeBody.call(this).then(function (buf) {
247 return Object.assign(
250 type: ct.toLowerCase()
258 * Decode response as json
265 return consumeBody.call(this).then(function (buffer) {
267 return JSON.parse(buffer.toString());
269 return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
275 * Decode response as text
280 return consumeBody.call(this).then(function (buffer) {
281 return buffer.toString();
286 * Decode response as buffer (non-spec api)
291 return consumeBody.call(this);
295 * Decode response as text, while automatically detecting the encoding and
296 * trying to decode to UTF-8 (non-spec api)
303 return consumeBody.call(this).then(function (buffer) {
304 return convertBody(buffer, _this3.headers);
309 // In browsers, all properties are enumerable.
310 Object.defineProperties(Body.prototype, {
311 body: { enumerable: true },
312 bodyUsed: { enumerable: true },
313 arrayBuffer: { enumerable: true },
314 blob: { enumerable: true },
315 json: { enumerable: true },
316 text: { enumerable: true }
319 Body.mixIn = function (proto) {
320 for (const name of Object.getOwnPropertyNames(Body.prototype)) {
321 // istanbul ignore else: future proof
322 if (!(name in proto)) {
323 const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
324 Object.defineProperty(proto, name, desc);
330 * Consume and convert an entire Body to a Buffer.
332 * Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
336 function consumeBody() {
339 if (this[INTERNALS].disturbed) {
340 return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
343 this[INTERNALS].disturbed = true;
345 if (this[INTERNALS].error) {
346 return Body.Promise.reject(this[INTERNALS].error);
349 let body = this.body;
353 return Body.Promise.resolve(Buffer.alloc(0));
358 body = body.stream();
362 if (Buffer.isBuffer(body)) {
363 return Body.Promise.resolve(body);
366 // istanbul ignore if: should never happen
367 if (!(body instanceof Stream)) {
368 return Body.Promise.resolve(Buffer.alloc(0));
372 // get ready to actually consume the body
377 return new Body.Promise(function (resolve, reject) {
380 // allow timeout on slow response body
381 if (_this4.timeout) {
382 resTimeout = setTimeout(function () {
384 reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, 'body-timeout'));
388 // handle stream errors
389 body.on('error', function (err) {
390 if (err.name === 'AbortError') {
391 // if the request was aborted, reject with this Error
395 // other errors, such as incorrect content-encoding
396 reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
400 body.on('data', function (chunk) {
401 if (abort || chunk === null) {
405 if (_this4.size && accumBytes + chunk.length > _this4.size) {
407 reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, 'max-size'));
411 accumBytes += chunk.length;
415 body.on('end', function () {
420 clearTimeout(resTimeout);
423 resolve(Buffer.concat(accum, accumBytes));
425 // handle streams that have accumulated too much data (issue #414)
426 reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, 'system', err));
433 * Detect buffer encoding and convert to target encoding
434 * ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding
436 * @param Buffer buffer Incoming buffer
437 * @param String encoding Target encoding
440 function convertBody(buffer, headers) {
441 if (typeof convert !== 'function') {
442 throw new Error('The package `encoding` must be installed to use the textConverted() function');
445 const ct = headers.get('content-type');
446 let charset = 'utf-8';
451 res = /charset=([^;]*)/i.exec(ct);
454 // no charset in content type, peek at response body for at most 1024 bytes
455 str = buffer.slice(0, 1024).toString();
459 res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
464 res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
466 res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
468 res.pop(); // drop last quote
473 res = /charset=(.*)/i.exec(res.pop());
479 res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str);
486 // prevent decode issues when sites use incorrect encoding
487 // ref: https://hsivonen.fi/encoding-menu/
488 if (charset === 'gb2312' || charset === 'gbk') {
493 // turn raw buffers into a single utf-8 buffer
494 return convert(buffer, 'UTF-8', charset).toString();
498 * Detect a URLSearchParams object
499 * ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
501 * @param Object obj Object to detect by type or brand
504 function isURLSearchParams(obj) {
505 // Duck-typing as a necessary condition.
506 if (typeof obj !== 'object' || typeof obj.append !== 'function' || typeof obj.delete !== 'function' || typeof obj.get !== 'function' || typeof obj.getAll !== 'function' || typeof obj.has !== 'function' || typeof obj.set !== 'function') {
510 // Brand-checking and more duck-typing as optional condition.
511 return obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function';
515 * Check if `obj` is a W3C `Blob` object (which `File` inherits from)
519 function isBlob(obj) {
520 return typeof obj === 'object' && typeof obj.arrayBuffer === 'function' && typeof obj.type === 'string' && typeof obj.stream === 'function' && typeof obj.constructor === 'function' && typeof obj.constructor.name === 'string' && /^(Blob|File)$/.test(obj.constructor.name) && /^(Blob|File)$/.test(obj[Symbol.toStringTag]);
524 * Clone body given Res/Req instance
526 * @param Mixed instance Response or Request instance
529 function clone(instance) {
531 let body = instance.body;
533 // don't allow cloning a used body
534 if (instance.bodyUsed) {
535 throw new Error('cannot clone body after it is used');
538 // check that body is a stream and not form-data object
539 // note: we can't clone the form-data object without having it as a dependency
540 if (body instanceof Stream && typeof body.getBoundary !== 'function') {
542 p1 = new PassThrough();
543 p2 = new PassThrough();
546 // set instance body to teed body and return the other teed body
547 instance[INTERNALS].body = p1;
555 * Performs the operation "extract a `Content-Type` value from |object|" as
556 * specified in the specification:
557 * https://fetch.spec.whatwg.org/#concept-bodyinit-extract
559 * This function assumes that instance.body is present.
561 * @param Mixed instance Any options.body input
563 function extractContentType(body) {
567 } else if (typeof body === 'string') {
569 return 'text/plain;charset=UTF-8';
570 } else if (isURLSearchParams(body)) {
571 // body is a URLSearchParams
572 return 'application/x-www-form-urlencoded;charset=UTF-8';
573 } else if (isBlob(body)) {
575 return body.type || null;
576 } else if (Buffer.isBuffer(body)) {
579 } else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
580 // body is ArrayBuffer
582 } else if (ArrayBuffer.isView(body)) {
583 // body is ArrayBufferView
585 } else if (typeof body.getBoundary === 'function') {
586 // detect form data input from form-data module
587 return `multipart/form-data;boundary=${body.getBoundary()}`;
588 } else if (body instanceof Stream) {
590 // can't really do much about this
593 // Body constructor defaults other things to string
594 return 'text/plain;charset=UTF-8';
599 * The Fetch Standard treats this as if "total bytes" is a property on the body.
600 * For us, we have to explicitly get it with a function.
602 * ref: https://fetch.spec.whatwg.org/#concept-body-total-bytes
604 * @param Body instance Instance of Body
605 * @return Number? Number of bytes, or null if not possible
607 function getTotalBytes(instance) {
608 const body = instance.body;
614 } else if (isBlob(body)) {
616 } else if (Buffer.isBuffer(body)) {
619 } else if (body && typeof body.getLengthSync === 'function') {
620 // detect form data input from form-data module
621 if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
622 body.hasKnownLength && body.hasKnownLength()) {
624 return body.getLengthSync();
634 * Write a Body to a Node.js WritableStream (e.g. http.Request) object.
636 * @param Body instance Instance of Body
639 function writeToStream(dest, instance) {
640 const body = instance.body;
646 } else if (isBlob(body)) {
647 body.stream().pipe(dest);
648 } else if (Buffer.isBuffer(body)) {
659 Body.Promise = global.Promise;
664 * Headers class offers convenient helpers
667 const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
668 const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
670 function validateName(name) {
672 if (invalidTokenRegex.test(name) || name === '') {
673 throw new TypeError(`${name} is not a legal HTTP header name`);
677 function validateValue(value) {
679 if (invalidHeaderCharRegex.test(value)) {
680 throw new TypeError(`${value} is not a legal HTTP header value`);
685 * Find the key in the map object given a header name.
687 * Returns undefined if not found.
689 * @param String name Header name
690 * @return String|Undefined
692 function find(map, name) {
693 name = name.toLowerCase();
694 for (const key in map) {
695 if (key.toLowerCase() === name) {
702 const MAP = Symbol('map');
707 * @param Object headers Response headers
711 let init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
713 this[MAP] = Object.create(null);
715 if (init instanceof Headers) {
716 const rawHeaders = init.raw();
717 const headerNames = Object.keys(rawHeaders);
719 for (const headerName of headerNames) {
720 for (const value of rawHeaders[headerName]) {
721 this.append(headerName, value);
728 // We don't worry about converting prop to ByteString here as append()
730 if (init == null) ; else if (typeof init === 'object') {
731 const method = init[Symbol.iterator];
732 if (method != null) {
733 if (typeof method !== 'function') {
734 throw new TypeError('Header pairs must be iterable');
737 // sequence<sequence<ByteString>>
738 // Note: per spec we have to first exhaust the lists then process them
740 for (const pair of init) {
741 if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') {
742 throw new TypeError('Each header pair must be iterable');
744 pairs.push(Array.from(pair));
747 for (const pair of pairs) {
748 if (pair.length !== 2) {
749 throw new TypeError('Each header pair must be a name/value tuple');
751 this.append(pair[0], pair[1]);
754 // record<ByteString, ByteString>
755 for (const key of Object.keys(init)) {
756 const value = init[key];
757 this.append(key, value);
761 throw new TypeError('Provided initializer must be an object');
766 * Return combined header value given name
768 * @param String name Header name
774 const key = find(this[MAP], name);
775 if (key === undefined) {
779 return this[MAP][key].join(', ');
783 * Iterate over all headers
785 * @param Function callback Executed for each item with parameters (value, name, thisArg)
786 * @param Boolean thisArg `this` context for callback function
790 let thisArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
792 let pairs = getHeaders(this);
794 while (i < pairs.length) {
795 var _pairs$i = pairs[i];
796 const name = _pairs$i[0],
799 callback.call(thisArg, value, name, this);
800 pairs = getHeaders(this);
806 * Overwrite header values given name
808 * @param String name Header name
809 * @param String value Header value
816 validateValue(value);
817 const key = find(this[MAP], name);
818 this[MAP][key !== undefined ? key : name] = [value];
822 * Append a value onto existing header
824 * @param String name Header name
825 * @param String value Header value
828 append(name, value) {
832 validateValue(value);
833 const key = find(this[MAP], name);
834 if (key !== undefined) {
835 this[MAP][key].push(value);
837 this[MAP][name] = [value];
842 * Check for header name existence
844 * @param String name Header name
850 return find(this[MAP], name) !== undefined;
854 * Delete all header values given name
856 * @param String name Header name
862 const key = find(this[MAP], name);
863 if (key !== undefined) {
864 delete this[MAP][key];
869 * Return raw headers (non-spec api)
878 * Get an iterator on keys.
883 return createHeadersIterator(this, 'key');
887 * Get an iterator on values.
892 return createHeadersIterator(this, 'value');
896 * Get an iterator on entries.
898 * This is the default iterator of the Headers object.
902 [Symbol.iterator]() {
903 return createHeadersIterator(this, 'key+value');
906 Headers.prototype.entries = Headers.prototype[Symbol.iterator];
908 Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
915 Object.defineProperties(Headers.prototype, {
916 get: { enumerable: true },
917 forEach: { enumerable: true },
918 set: { enumerable: true },
919 append: { enumerable: true },
920 has: { enumerable: true },
921 delete: { enumerable: true },
922 keys: { enumerable: true },
923 values: { enumerable: true },
924 entries: { enumerable: true }
927 function getHeaders(headers) {
928 let kind = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'key+value';
930 const keys = Object.keys(headers[MAP]).sort();
931 return keys.map(kind === 'key' ? function (k) {
932 return k.toLowerCase();
933 } : kind === 'value' ? function (k) {
934 return headers[MAP][k].join(', ');
936 return [k.toLowerCase(), headers[MAP][k].join(', ')];
940 const INTERNAL = Symbol('internal');
942 function createHeadersIterator(target, kind) {
943 const iterator = Object.create(HeadersIteratorPrototype);
944 iterator[INTERNAL] = {
952 const HeadersIteratorPrototype = Object.setPrototypeOf({
954 // istanbul ignore if
955 if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
956 throw new TypeError('Value of `this` is not a HeadersIterator');
959 var _INTERNAL = this[INTERNAL];
960 const target = _INTERNAL.target,
961 kind = _INTERNAL.kind,
962 index = _INTERNAL.index;
964 const values = getHeaders(target, kind);
965 const len = values.length;
973 this[INTERNAL].index = index + 1;
976 value: values[index],
980 }, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())));
982 Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
983 value: 'HeadersIterator',
990 * Export the Headers object in a form that Node.js can consume.
992 * @param Headers headers
995 function exportNodeCompatibleHeaders(headers) {
996 const obj = Object.assign({ __proto__: null }, headers[MAP]);
998 // http.request() only supports string as Host header. This hack makes
999 // specifying custom Host header possible.
1000 const hostHeaderKey = find(headers[MAP], 'Host');
1001 if (hostHeaderKey !== undefined) {
1002 obj[hostHeaderKey] = obj[hostHeaderKey][0];
1009 * Create a Headers object from an object of headers, ignoring those that do
1010 * not conform to HTTP grammar productions.
1012 * @param Object obj Object of headers
1015 function createHeadersLenient(obj) {
1016 const headers = new Headers();
1017 for (const name of Object.keys(obj)) {
1018 if (invalidTokenRegex.test(name)) {
1021 if (Array.isArray(obj[name])) {
1022 for (const val of obj[name]) {
1023 if (invalidHeaderCharRegex.test(val)) {
1026 if (headers[MAP][name] === undefined) {
1027 headers[MAP][name] = [val];
1029 headers[MAP][name].push(val);
1032 } else if (!invalidHeaderCharRegex.test(obj[name])) {
1033 headers[MAP][name] = [obj[name]];
1039 const INTERNALS$1 = Symbol('Response internals');
1041 // fix an issue where "STATUS_CODES" aren't a named export for node <10
1042 const STATUS_CODES = http.STATUS_CODES;
1047 * @param Stream body Readable stream
1048 * @param Object opts Response options
1053 let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
1054 let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1056 Body.call(this, body, opts);
1058 const status = opts.status || 200;
1059 const headers = new Headers(opts.headers);
1061 if (body != null && !headers.has('Content-Type')) {
1062 const contentType = extractContentType(body);
1064 headers.append('Content-Type', contentType);
1068 this[INTERNALS$1] = {
1071 statusText: opts.statusText || STATUS_CODES[status],
1073 counter: opts.counter
1078 return this[INTERNALS$1].url || '';
1082 return this[INTERNALS$1].status;
1086 * Convenience property representing if the request ended normally
1089 return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300;
1093 return this[INTERNALS$1].counter > 0;
1097 return this[INTERNALS$1].statusText;
1101 return this[INTERNALS$1].headers;
1105 * Clone this response
1110 return new Response(clone(this), {
1112 status: this.status,
1113 statusText: this.statusText,
1114 headers: this.headers,
1116 redirected: this.redirected
1121 Body.mixIn(Response.prototype);
1123 Object.defineProperties(Response.prototype, {
1124 url: { enumerable: true },
1125 status: { enumerable: true },
1126 ok: { enumerable: true },
1127 redirected: { enumerable: true },
1128 statusText: { enumerable: true },
1129 headers: { enumerable: true },
1130 clone: { enumerable: true }
1133 Object.defineProperty(Response.prototype, Symbol.toStringTag, {
1140 const INTERNALS$2 = Symbol('Request internals');
1141 const URL = Url.URL || whatwgUrl.URL;
1143 // fix an issue where "format", "parse" aren't a named export for node <10
1144 const parse_url = Url.parse;
1145 const format_url = Url.format;
1148 * Wrapper around `new URL` to handle arbitrary URLs
1150 * @param {string} urlStr
1153 function parseURL(urlStr) {
1155 Check whether the URL is absolute or not
1156 Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
1157 Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
1159 if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.exec(urlStr)) {
1160 urlStr = new URL(urlStr).toString();
1163 // Fallback to old implementation for arbitrary URLs
1164 return parse_url(urlStr);
1167 const streamDestructionSupported = 'destroy' in Stream.Readable.prototype;
1170 * Check if a value is an instance of Request.
1172 * @param Mixed input
1175 function isRequest(input) {
1176 return typeof input === 'object' && typeof input[INTERNALS$2] === 'object';
1179 function isAbortSignal(signal) {
1180 const proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
1181 return !!(proto && proto.constructor.name === 'AbortSignal');
1187 * @param Mixed input Url or Request instance
1188 * @param Object init Custom options
1192 constructor(input) {
1193 let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1198 if (!isRequest(input)) {
1199 if (input && input.href) {
1200 // in order to support Node.js' Url objects; though WHATWG's URL objects
1201 // will fall into this branch also (since their `toString()` will return
1202 // `href` property anyway)
1203 parsedURL = parseURL(input.href);
1205 // coerce input to a string before attempting to parse
1206 parsedURL = parseURL(`${input}`);
1210 parsedURL = parseURL(input.url);
1213 let method = init.method || input.method || 'GET';
1214 method = method.toUpperCase();
1216 if ((init.body != null || isRequest(input) && input.body !== null) && (method === 'GET' || method === 'HEAD')) {
1217 throw new TypeError('Request with GET/HEAD method cannot have body');
1220 let inputBody = init.body != null ? init.body : isRequest(input) && input.body !== null ? clone(input) : null;
1222 Body.call(this, inputBody, {
1223 timeout: init.timeout || input.timeout || 0,
1224 size: init.size || input.size || 0
1227 const headers = new Headers(init.headers || input.headers || {});
1229 if (inputBody != null && !headers.has('Content-Type')) {
1230 const contentType = extractContentType(inputBody);
1232 headers.append('Content-Type', contentType);
1236 let signal = isRequest(input) ? input.signal : null;
1237 if ('signal' in init) signal = init.signal;
1239 if (signal != null && !isAbortSignal(signal)) {
1240 throw new TypeError('Expected signal to be an instanceof AbortSignal');
1243 this[INTERNALS$2] = {
1245 redirect: init.redirect || input.redirect || 'follow',
1251 // node-fetch-only options
1252 this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20;
1253 this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true;
1254 this.counter = init.counter || input.counter || 0;
1255 this.agent = init.agent || input.agent;
1259 return this[INTERNALS$2].method;
1263 return format_url(this[INTERNALS$2].parsedURL);
1267 return this[INTERNALS$2].headers;
1271 return this[INTERNALS$2].redirect;
1275 return this[INTERNALS$2].signal;
1279 * Clone this request
1284 return new Request(this);
1288 Body.mixIn(Request.prototype);
1290 Object.defineProperty(Request.prototype, Symbol.toStringTag, {
1297 Object.defineProperties(Request.prototype, {
1298 method: { enumerable: true },
1299 url: { enumerable: true },
1300 headers: { enumerable: true },
1301 redirect: { enumerable: true },
1302 clone: { enumerable: true },
1303 signal: { enumerable: true }
1307 * Convert a Request to Node.js http request options.
1309 * @param Request A Request instance
1310 * @return Object The options object to be passed to http.request
1312 function getNodeRequestOptions(request) {
1313 const parsedURL = request[INTERNALS$2].parsedURL;
1314 const headers = new Headers(request[INTERNALS$2].headers);
1317 if (!headers.has('Accept')) {
1318 headers.set('Accept', '*/*');
1322 if (!parsedURL.protocol || !parsedURL.hostname) {
1323 throw new TypeError('Only absolute URLs are supported');
1326 if (!/^https?:$/.test(parsedURL.protocol)) {
1327 throw new TypeError('Only HTTP(S) protocols are supported');
1330 if (request.signal && request.body instanceof Stream.Readable && !streamDestructionSupported) {
1331 throw new Error('Cancellation of streamed requests with AbortSignal is not supported in node < 8');
1334 // HTTP-network-or-cache fetch steps 2.4-2.7
1335 let contentLengthValue = null;
1336 if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
1337 contentLengthValue = '0';
1339 if (request.body != null) {
1340 const totalBytes = getTotalBytes(request);
1341 if (typeof totalBytes === 'number') {
1342 contentLengthValue = String(totalBytes);
1345 if (contentLengthValue) {
1346 headers.set('Content-Length', contentLengthValue);
1349 // HTTP-network-or-cache fetch step 2.11
1350 if (!headers.has('User-Agent')) {
1351 headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
1354 // HTTP-network-or-cache fetch step 2.15
1355 if (request.compress && !headers.has('Accept-Encoding')) {
1356 headers.set('Accept-Encoding', 'gzip,deflate');
1359 let agent = request.agent;
1360 if (typeof agent === 'function') {
1361 agent = agent(parsedURL);
1364 if (!headers.has('Connection') && !agent) {
1365 headers.set('Connection', 'close');
1368 // HTTP-network fetch step 4.2
1369 // chunked encoding is handled by Node.js
1371 return Object.assign({}, parsedURL, {
1372 method: request.method,
1373 headers: exportNodeCompatibleHeaders(headers),
1381 * AbortError interface for cancelled requests
1385 * Create AbortError instance
1387 * @param String message Error message for human
1388 * @return AbortError
1390 function AbortError(message) {
1391 Error.call(this, message);
1393 this.type = 'aborted';
1394 this.message = message;
1396 // hide custom error implementation details from end-users
1397 Error.captureStackTrace(this, this.constructor);
1400 AbortError.prototype = Object.create(Error.prototype);
1401 AbortError.prototype.constructor = AbortError;
1402 AbortError.prototype.name = 'AbortError';
1404 // fix an issue where "PassThrough", "resolve" aren't a named export for node <10
1405 const PassThrough$1 = Stream.PassThrough;
1406 const resolve_url = Url.resolve;
1411 * @param Mixed url Absolute url or Request instance
1412 * @param Object opts Fetch options
1415 function fetch(url, opts) {
1417 // allow custom promise
1418 if (!fetch.Promise) {
1419 throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
1422 Body.Promise = fetch.Promise;
1424 // wrap http.request into fetch
1425 return new fetch.Promise(function (resolve, reject) {
1426 // build request object
1427 const request = new Request(url, opts);
1428 const options = getNodeRequestOptions(request);
1430 const send = (options.protocol === 'https:' ? https : http).request;
1431 const signal = request.signal;
1433 let response = null;
1435 const abort = function abort() {
1436 let error = new AbortError('The user aborted a request.');
1438 if (request.body && request.body instanceof Stream.Readable) {
1439 request.body.destroy(error);
1441 if (!response || !response.body) return;
1442 response.body.emit('error', error);
1445 if (signal && signal.aborted) {
1450 const abortAndFinalize = function abortAndFinalize() {
1456 const req = send(options);
1460 signal.addEventListener('abort', abortAndFinalize);
1463 function finalize() {
1465 if (signal) signal.removeEventListener('abort', abortAndFinalize);
1466 clearTimeout(reqTimeout);
1469 if (request.timeout) {
1470 req.once('socket', function (socket) {
1471 reqTimeout = setTimeout(function () {
1472 reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
1474 }, request.timeout);
1478 req.on('error', function (err) {
1479 reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
1483 req.on('response', function (res) {
1484 clearTimeout(reqTimeout);
1486 const headers = createHeadersLenient(res.headers);
1488 // HTTP fetch step 5
1489 if (fetch.isRedirect(res.statusCode)) {
1490 // HTTP fetch step 5.2
1491 const location = headers.get('Location');
1493 // HTTP fetch step 5.3
1494 const locationURL = location === null ? null : resolve_url(request.url, location);
1496 // HTTP fetch step 5.5
1497 switch (request.redirect) {
1499 reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
1503 // node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
1504 if (locationURL !== null) {
1505 // handle corrupted header
1507 headers.set('Location', locationURL);
1509 // istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
1515 // HTTP-redirect fetch step 2
1516 if (locationURL === null) {
1520 // HTTP-redirect fetch step 5
1521 if (request.counter >= request.follow) {
1522 reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
1527 // HTTP-redirect fetch step 6 (counter increment)
1528 // Create a new Request object.
1529 const requestOpts = {
1530 headers: new Headers(request.headers),
1531 follow: request.follow,
1532 counter: request.counter + 1,
1533 agent: request.agent,
1534 compress: request.compress,
1535 method: request.method,
1537 signal: request.signal,
1538 timeout: request.timeout,
1542 // HTTP-redirect fetch step 9
1543 if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
1544 reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
1549 // HTTP-redirect fetch step 11
1550 if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
1551 requestOpts.method = 'GET';
1552 requestOpts.body = undefined;
1553 requestOpts.headers.delete('content-length');
1556 // HTTP-redirect fetch step 15
1557 resolve(fetch(new Request(locationURL, requestOpts)));
1564 res.once('end', function () {
1565 if (signal) signal.removeEventListener('abort', abortAndFinalize);
1567 let body = res.pipe(new PassThrough$1());
1569 const response_options = {
1571 status: res.statusCode,
1572 statusText: res.statusMessage,
1575 timeout: request.timeout,
1576 counter: request.counter
1579 // HTTP-network fetch step 12.1.1.3
1580 const codings = headers.get('Content-Encoding');
1582 // HTTP-network fetch step 12.1.1.4: handle content codings
1584 // in following scenarios we ignore compression support
1585 // 1. compression support is disabled
1587 // 3. no Content-Encoding header
1588 // 4. no content response (204)
1589 // 5. content not modified response (304)
1590 if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
1591 response = new Response(body, response_options);
1597 // Be less strict when decoding compressed responses, since sometimes
1598 // servers send slightly invalid responses that are still accepted
1599 // by common browsers.
1600 // Always using Z_SYNC_FLUSH is what cURL does.
1601 const zlibOptions = {
1602 flush: zlib.Z_SYNC_FLUSH,
1603 finishFlush: zlib.Z_SYNC_FLUSH
1607 if (codings == 'gzip' || codings == 'x-gzip') {
1608 body = body.pipe(zlib.createGunzip(zlibOptions));
1609 response = new Response(body, response_options);
1615 if (codings == 'deflate' || codings == 'x-deflate') {
1616 // handle the infamous raw deflate response from old servers
1617 // a hack for old IIS and Apache servers
1618 const raw = res.pipe(new PassThrough$1());
1619 raw.once('data', function (chunk) {
1620 // see http://stackoverflow.com/questions/37519828
1621 if ((chunk[0] & 0x0F) === 0x08) {
1622 body = body.pipe(zlib.createInflate());
1624 body = body.pipe(zlib.createInflateRaw());
1626 response = new Response(body, response_options);
1633 if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
1634 body = body.pipe(zlib.createBrotliDecompress());
1635 response = new Response(body, response_options);
1640 // otherwise, use response as-is
1641 response = new Response(body, response_options);
1645 writeToStream(req, request);
1649 * Redirect code matching
1651 * @param Number code Status code
1654 fetch.isRedirect = function (code) {
1655 return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
1659 fetch.Promise = global.Promise;
1661 export default fetch;
1662 export { Headers, Request, Response, FetchError };