1 import Stream from 'stream';
2 import http from 'http';
4 import whatwgUrl from 'whatwg-url';
5 import https from 'https';
6 import zlib from 'zlib';
8 // Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js
10 // fix for "Readable" isn't a named export issue
11 const Readable = Stream.Readable;
13 const BUFFER = Symbol('buffer');
14 const TYPE = Symbol('type');
20 const blobParts = arguments[0];
21 const options = arguments[1];
28 const length = Number(a.length);
29 for (let i = 0; i < length; i++) {
32 if (element instanceof Buffer) {
34 } else if (ArrayBuffer.isView(element)) {
35 buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
36 } else if (element instanceof ArrayBuffer) {
37 buffer = Buffer.from(element);
38 } else if (element instanceof Blob) {
39 buffer = element[BUFFER];
41 buffer = Buffer.from(typeof element === 'string' ? element : String(element));
43 size += buffer.length;
48 this[BUFFER] = Buffer.concat(buffers);
50 let type = options && options.type !== undefined && String(options.type).toLowerCase();
51 if (type && !/[^\u0020-\u007E]/.test(type)) {
56 return this[BUFFER].length;
62 return Promise.resolve(this[BUFFER].toString());
65 const buf = this[BUFFER];
66 const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
67 return Promise.resolve(ab);
70 const readable = new Readable();
71 readable._read = function () {};
72 readable.push(this[BUFFER]);
77 return '[object Blob]';
80 const size = this.size;
82 const start = arguments[0];
83 const end = arguments[1];
84 let relativeStart, relativeEnd;
85 if (start === undefined) {
87 } else if (start < 0) {
88 relativeStart = Math.max(size + start, 0);
90 relativeStart = Math.min(start, size);
92 if (end === undefined) {
95 relativeEnd = Math.max(size + end, 0);
97 relativeEnd = Math.min(end, size);
99 const span = Math.max(relativeEnd - relativeStart, 0);
101 const buffer = this[BUFFER];
102 const slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
103 const blob = new Blob([], { type: arguments[2] });
104 blob[BUFFER] = slicedBuffer;
109 Object.defineProperties(Blob.prototype, {
110 size: { enumerable: true },
111 type: { enumerable: true },
112 slice: { enumerable: true }
115 Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
125 * FetchError interface for operational errors
129 * Create FetchError instance
131 * @param String message Error message for human
132 * @param String type Error type for machine
133 * @param String systemError For Node.js system error
136 function FetchError(message, type, systemError) {
137 Error.call(this, message);
139 this.message = message;
142 // when err.type is `system`, err.code contains system error code
144 this.code = this.errno = systemError.code;
147 // hide custom error implementation details from end-users
148 Error.captureStackTrace(this, this.constructor);
151 FetchError.prototype = Object.create(Error.prototype);
152 FetchError.prototype.constructor = FetchError;
153 FetchError.prototype.name = 'FetchError';
157 convert = require('encoding').convert;
160 const INTERNALS = Symbol('Body internals');
162 // fix an issue where "PassThrough" isn't a named export for node <10
163 const PassThrough = Stream.PassThrough;
168 * Ref: https://fetch.spec.whatwg.org/#body
170 * @param Stream body Readable stream
171 * @param Object opts Response options
174 function Body(body) {
177 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
178 _ref$size = _ref.size;
180 let size = _ref$size === undefined ? 0 : _ref$size;
181 var _ref$timeout = _ref.timeout;
182 let timeout = _ref$timeout === undefined ? 0 : _ref$timeout;
185 // body is undefined or null
187 } else if (isURLSearchParams(body)) {
188 // body is a URLSearchParams
189 body = Buffer.from(body.toString());
190 } else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
191 // body is ArrayBuffer
192 body = Buffer.from(body);
193 } else if (ArrayBuffer.isView(body)) {
194 // body is ArrayBufferView
195 body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
196 } else if (body instanceof Stream) ; else {
198 // coerce to string then buffer
199 body = Buffer.from(String(body));
207 this.timeout = timeout;
209 if (body instanceof Stream) {
210 body.on('error', function (err) {
211 const error = err.name === 'AbortError' ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, 'system', err);
212 _this[INTERNALS].error = error;
219 return this[INTERNALS].body;
223 return this[INTERNALS].disturbed;
227 * Decode response as ArrayBuffer
232 return consumeBody.call(this).then(function (buf) {
233 return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
238 * Return raw response as Blob
243 let ct = this.headers && this.headers.get('content-type') || '';
244 return consumeBody.call(this).then(function (buf) {
245 return Object.assign(
248 type: ct.toLowerCase()
256 * Decode response as json
263 return consumeBody.call(this).then(function (buffer) {
265 return JSON.parse(buffer.toString());
267 return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
273 * Decode response as text
278 return consumeBody.call(this).then(function (buffer) {
279 return buffer.toString();
284 * Decode response as buffer (non-spec api)
289 return consumeBody.call(this);
293 * Decode response as text, while automatically detecting the encoding and
294 * trying to decode to UTF-8 (non-spec api)
301 return consumeBody.call(this).then(function (buffer) {
302 return convertBody(buffer, _this3.headers);
307 // In browsers, all properties are enumerable.
308 Object.defineProperties(Body.prototype, {
309 body: { enumerable: true },
310 bodyUsed: { enumerable: true },
311 arrayBuffer: { enumerable: true },
312 blob: { enumerable: true },
313 json: { enumerable: true },
314 text: { enumerable: true }
317 Body.mixIn = function (proto) {
318 for (const name of Object.getOwnPropertyNames(Body.prototype)) {
319 // istanbul ignore else: future proof
320 if (!(name in proto)) {
321 const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
322 Object.defineProperty(proto, name, desc);
328 * Consume and convert an entire Body to a Buffer.
330 * Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
334 function consumeBody() {
337 if (this[INTERNALS].disturbed) {
338 return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
341 this[INTERNALS].disturbed = true;
343 if (this[INTERNALS].error) {
344 return Body.Promise.reject(this[INTERNALS].error);
347 let body = this.body;
351 return Body.Promise.resolve(Buffer.alloc(0));
356 body = body.stream();
360 if (Buffer.isBuffer(body)) {
361 return Body.Promise.resolve(body);
364 // istanbul ignore if: should never happen
365 if (!(body instanceof Stream)) {
366 return Body.Promise.resolve(Buffer.alloc(0));
370 // get ready to actually consume the body
375 return new Body.Promise(function (resolve, reject) {
378 // allow timeout on slow response body
379 if (_this4.timeout) {
380 resTimeout = setTimeout(function () {
382 reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, 'body-timeout'));
386 // handle stream errors
387 body.on('error', function (err) {
388 if (err.name === 'AbortError') {
389 // if the request was aborted, reject with this Error
393 // other errors, such as incorrect content-encoding
394 reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
398 body.on('data', function (chunk) {
399 if (abort || chunk === null) {
403 if (_this4.size && accumBytes + chunk.length > _this4.size) {
405 reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, 'max-size'));
409 accumBytes += chunk.length;
413 body.on('end', function () {
418 clearTimeout(resTimeout);
421 resolve(Buffer.concat(accum, accumBytes));
423 // handle streams that have accumulated too much data (issue #414)
424 reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, 'system', err));
431 * Detect buffer encoding and convert to target encoding
432 * ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding
434 * @param Buffer buffer Incoming buffer
435 * @param String encoding Target encoding
438 function convertBody(buffer, headers) {
439 if (typeof convert !== 'function') {
440 throw new Error('The package `encoding` must be installed to use the textConverted() function');
443 const ct = headers.get('content-type');
444 let charset = 'utf-8';
449 res = /charset=([^;]*)/i.exec(ct);
452 // no charset in content type, peek at response body for at most 1024 bytes
453 str = buffer.slice(0, 1024).toString();
457 res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
462 res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
464 res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
466 res.pop(); // drop last quote
471 res = /charset=(.*)/i.exec(res.pop());
477 res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str);
484 // prevent decode issues when sites use incorrect encoding
485 // ref: https://hsivonen.fi/encoding-menu/
486 if (charset === 'gb2312' || charset === 'gbk') {
491 // turn raw buffers into a single utf-8 buffer
492 return convert(buffer, 'UTF-8', charset).toString();
496 * Detect a URLSearchParams object
497 * ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
499 * @param Object obj Object to detect by type or brand
502 function isURLSearchParams(obj) {
503 // Duck-typing as a necessary condition.
504 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') {
508 // Brand-checking and more duck-typing as optional condition.
509 return obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function';
513 * Check if `obj` is a W3C `Blob` object (which `File` inherits from)
517 function isBlob(obj) {
518 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]);
522 * Clone body given Res/Req instance
524 * @param Mixed instance Response or Request instance
527 function clone(instance) {
529 let body = instance.body;
531 // don't allow cloning a used body
532 if (instance.bodyUsed) {
533 throw new Error('cannot clone body after it is used');
536 // check that body is a stream and not form-data object
537 // note: we can't clone the form-data object without having it as a dependency
538 if (body instanceof Stream && typeof body.getBoundary !== 'function') {
540 p1 = new PassThrough();
541 p2 = new PassThrough();
544 // set instance body to teed body and return the other teed body
545 instance[INTERNALS].body = p1;
553 * Performs the operation "extract a `Content-Type` value from |object|" as
554 * specified in the specification:
555 * https://fetch.spec.whatwg.org/#concept-bodyinit-extract
557 * This function assumes that instance.body is present.
559 * @param Mixed instance Any options.body input
561 function extractContentType(body) {
565 } else if (typeof body === 'string') {
567 return 'text/plain;charset=UTF-8';
568 } else if (isURLSearchParams(body)) {
569 // body is a URLSearchParams
570 return 'application/x-www-form-urlencoded;charset=UTF-8';
571 } else if (isBlob(body)) {
573 return body.type || null;
574 } else if (Buffer.isBuffer(body)) {
577 } else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
578 // body is ArrayBuffer
580 } else if (ArrayBuffer.isView(body)) {
581 // body is ArrayBufferView
583 } else if (typeof body.getBoundary === 'function') {
584 // detect form data input from form-data module
585 return `multipart/form-data;boundary=${body.getBoundary()}`;
586 } else if (body instanceof Stream) {
588 // can't really do much about this
591 // Body constructor defaults other things to string
592 return 'text/plain;charset=UTF-8';
597 * The Fetch Standard treats this as if "total bytes" is a property on the body.
598 * For us, we have to explicitly get it with a function.
600 * ref: https://fetch.spec.whatwg.org/#concept-body-total-bytes
602 * @param Body instance Instance of Body
603 * @return Number? Number of bytes, or null if not possible
605 function getTotalBytes(instance) {
606 const body = instance.body;
612 } else if (isBlob(body)) {
614 } else if (Buffer.isBuffer(body)) {
617 } else if (body && typeof body.getLengthSync === 'function') {
618 // detect form data input from form-data module
619 if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
620 body.hasKnownLength && body.hasKnownLength()) {
622 return body.getLengthSync();
632 * Write a Body to a Node.js WritableStream (e.g. http.Request) object.
634 * @param Body instance Instance of Body
637 function writeToStream(dest, instance) {
638 const body = instance.body;
644 } else if (isBlob(body)) {
645 body.stream().pipe(dest);
646 } else if (Buffer.isBuffer(body)) {
657 Body.Promise = global.Promise;
662 * Headers class offers convenient helpers
665 const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
666 const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
668 function validateName(name) {
670 if (invalidTokenRegex.test(name) || name === '') {
671 throw new TypeError(`${name} is not a legal HTTP header name`);
675 function validateValue(value) {
677 if (invalidHeaderCharRegex.test(value)) {
678 throw new TypeError(`${value} is not a legal HTTP header value`);
683 * Find the key in the map object given a header name.
685 * Returns undefined if not found.
687 * @param String name Header name
688 * @return String|Undefined
690 function find(map, name) {
691 name = name.toLowerCase();
692 for (const key in map) {
693 if (key.toLowerCase() === name) {
700 const MAP = Symbol('map');
705 * @param Object headers Response headers
709 let init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
711 this[MAP] = Object.create(null);
713 if (init instanceof Headers) {
714 const rawHeaders = init.raw();
715 const headerNames = Object.keys(rawHeaders);
717 for (const headerName of headerNames) {
718 for (const value of rawHeaders[headerName]) {
719 this.append(headerName, value);
726 // We don't worry about converting prop to ByteString here as append()
728 if (init == null) ; else if (typeof init === 'object') {
729 const method = init[Symbol.iterator];
730 if (method != null) {
731 if (typeof method !== 'function') {
732 throw new TypeError('Header pairs must be iterable');
735 // sequence<sequence<ByteString>>
736 // Note: per spec we have to first exhaust the lists then process them
738 for (const pair of init) {
739 if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') {
740 throw new TypeError('Each header pair must be iterable');
742 pairs.push(Array.from(pair));
745 for (const pair of pairs) {
746 if (pair.length !== 2) {
747 throw new TypeError('Each header pair must be a name/value tuple');
749 this.append(pair[0], pair[1]);
752 // record<ByteString, ByteString>
753 for (const key of Object.keys(init)) {
754 const value = init[key];
755 this.append(key, value);
759 throw new TypeError('Provided initializer must be an object');
764 * Return combined header value given name
766 * @param String name Header name
772 const key = find(this[MAP], name);
773 if (key === undefined) {
777 return this[MAP][key].join(', ');
781 * Iterate over all headers
783 * @param Function callback Executed for each item with parameters (value, name, thisArg)
784 * @param Boolean thisArg `this` context for callback function
788 let thisArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
790 let pairs = getHeaders(this);
792 while (i < pairs.length) {
793 var _pairs$i = pairs[i];
794 const name = _pairs$i[0],
797 callback.call(thisArg, value, name, this);
798 pairs = getHeaders(this);
804 * Overwrite header values given name
806 * @param String name Header name
807 * @param String value Header value
814 validateValue(value);
815 const key = find(this[MAP], name);
816 this[MAP][key !== undefined ? key : name] = [value];
820 * Append a value onto existing header
822 * @param String name Header name
823 * @param String value Header value
826 append(name, value) {
830 validateValue(value);
831 const key = find(this[MAP], name);
832 if (key !== undefined) {
833 this[MAP][key].push(value);
835 this[MAP][name] = [value];
840 * Check for header name existence
842 * @param String name Header name
848 return find(this[MAP], name) !== undefined;
852 * Delete all header values given name
854 * @param String name Header name
860 const key = find(this[MAP], name);
861 if (key !== undefined) {
862 delete this[MAP][key];
867 * Return raw headers (non-spec api)
876 * Get an iterator on keys.
881 return createHeadersIterator(this, 'key');
885 * Get an iterator on values.
890 return createHeadersIterator(this, 'value');
894 * Get an iterator on entries.
896 * This is the default iterator of the Headers object.
900 [Symbol.iterator]() {
901 return createHeadersIterator(this, 'key+value');
904 Headers.prototype.entries = Headers.prototype[Symbol.iterator];
906 Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
913 Object.defineProperties(Headers.prototype, {
914 get: { enumerable: true },
915 forEach: { enumerable: true },
916 set: { enumerable: true },
917 append: { enumerable: true },
918 has: { enumerable: true },
919 delete: { enumerable: true },
920 keys: { enumerable: true },
921 values: { enumerable: true },
922 entries: { enumerable: true }
925 function getHeaders(headers) {
926 let kind = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'key+value';
928 const keys = Object.keys(headers[MAP]).sort();
929 return keys.map(kind === 'key' ? function (k) {
930 return k.toLowerCase();
931 } : kind === 'value' ? function (k) {
932 return headers[MAP][k].join(', ');
934 return [k.toLowerCase(), headers[MAP][k].join(', ')];
938 const INTERNAL = Symbol('internal');
940 function createHeadersIterator(target, kind) {
941 const iterator = Object.create(HeadersIteratorPrototype);
942 iterator[INTERNAL] = {
950 const HeadersIteratorPrototype = Object.setPrototypeOf({
952 // istanbul ignore if
953 if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
954 throw new TypeError('Value of `this` is not a HeadersIterator');
957 var _INTERNAL = this[INTERNAL];
958 const target = _INTERNAL.target,
959 kind = _INTERNAL.kind,
960 index = _INTERNAL.index;
962 const values = getHeaders(target, kind);
963 const len = values.length;
971 this[INTERNAL].index = index + 1;
974 value: values[index],
978 }, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())));
980 Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
981 value: 'HeadersIterator',
988 * Export the Headers object in a form that Node.js can consume.
990 * @param Headers headers
993 function exportNodeCompatibleHeaders(headers) {
994 const obj = Object.assign({ __proto__: null }, headers[MAP]);
996 // http.request() only supports string as Host header. This hack makes
997 // specifying custom Host header possible.
998 const hostHeaderKey = find(headers[MAP], 'Host');
999 if (hostHeaderKey !== undefined) {
1000 obj[hostHeaderKey] = obj[hostHeaderKey][0];
1007 * Create a Headers object from an object of headers, ignoring those that do
1008 * not conform to HTTP grammar productions.
1010 * @param Object obj Object of headers
1013 function createHeadersLenient(obj) {
1014 const headers = new Headers();
1015 for (const name of Object.keys(obj)) {
1016 if (invalidTokenRegex.test(name)) {
1019 if (Array.isArray(obj[name])) {
1020 for (const val of obj[name]) {
1021 if (invalidHeaderCharRegex.test(val)) {
1024 if (headers[MAP][name] === undefined) {
1025 headers[MAP][name] = [val];
1027 headers[MAP][name].push(val);
1030 } else if (!invalidHeaderCharRegex.test(obj[name])) {
1031 headers[MAP][name] = [obj[name]];
1037 const INTERNALS$1 = Symbol('Response internals');
1039 // fix an issue where "STATUS_CODES" aren't a named export for node <10
1040 const STATUS_CODES = http.STATUS_CODES;
1045 * @param Stream body Readable stream
1046 * @param Object opts Response options
1051 let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
1052 let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1054 Body.call(this, body, opts);
1056 const status = opts.status || 200;
1057 const headers = new Headers(opts.headers);
1059 if (body != null && !headers.has('Content-Type')) {
1060 const contentType = extractContentType(body);
1062 headers.append('Content-Type', contentType);
1066 this[INTERNALS$1] = {
1069 statusText: opts.statusText || STATUS_CODES[status],
1071 counter: opts.counter
1076 return this[INTERNALS$1].url || '';
1080 return this[INTERNALS$1].status;
1084 * Convenience property representing if the request ended normally
1087 return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300;
1091 return this[INTERNALS$1].counter > 0;
1095 return this[INTERNALS$1].statusText;
1099 return this[INTERNALS$1].headers;
1103 * Clone this response
1108 return new Response(clone(this), {
1110 status: this.status,
1111 statusText: this.statusText,
1112 headers: this.headers,
1114 redirected: this.redirected
1119 Body.mixIn(Response.prototype);
1121 Object.defineProperties(Response.prototype, {
1122 url: { enumerable: true },
1123 status: { enumerable: true },
1124 ok: { enumerable: true },
1125 redirected: { enumerable: true },
1126 statusText: { enumerable: true },
1127 headers: { enumerable: true },
1128 clone: { enumerable: true }
1131 Object.defineProperty(Response.prototype, Symbol.toStringTag, {
1138 const INTERNALS$2 = Symbol('Request internals');
1139 const URL = Url.URL || whatwgUrl.URL;
1141 // fix an issue where "format", "parse" aren't a named export for node <10
1142 const parse_url = Url.parse;
1143 const format_url = Url.format;
1146 * Wrapper around `new URL` to handle arbitrary URLs
1148 * @param {string} urlStr
1151 function parseURL(urlStr) {
1153 Check whether the URL is absolute or not
1154 Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
1155 Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
1157 if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.exec(urlStr)) {
1158 urlStr = new URL(urlStr).toString();
1161 // Fallback to old implementation for arbitrary URLs
1162 return parse_url(urlStr);
1165 const streamDestructionSupported = 'destroy' in Stream.Readable.prototype;
1168 * Check if a value is an instance of Request.
1170 * @param Mixed input
1173 function isRequest(input) {
1174 return typeof input === 'object' && typeof input[INTERNALS$2] === 'object';
1177 function isAbortSignal(signal) {
1178 const proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
1179 return !!(proto && proto.constructor.name === 'AbortSignal');
1185 * @param Mixed input Url or Request instance
1186 * @param Object init Custom options
1190 constructor(input) {
1191 let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1196 if (!isRequest(input)) {
1197 if (input && input.href) {
1198 // in order to support Node.js' Url objects; though WHATWG's URL objects
1199 // will fall into this branch also (since their `toString()` will return
1200 // `href` property anyway)
1201 parsedURL = parseURL(input.href);
1203 // coerce input to a string before attempting to parse
1204 parsedURL = parseURL(`${input}`);
1208 parsedURL = parseURL(input.url);
1211 let method = init.method || input.method || 'GET';
1212 method = method.toUpperCase();
1214 if ((init.body != null || isRequest(input) && input.body !== null) && (method === 'GET' || method === 'HEAD')) {
1215 throw new TypeError('Request with GET/HEAD method cannot have body');
1218 let inputBody = init.body != null ? init.body : isRequest(input) && input.body !== null ? clone(input) : null;
1220 Body.call(this, inputBody, {
1221 timeout: init.timeout || input.timeout || 0,
1222 size: init.size || input.size || 0
1225 const headers = new Headers(init.headers || input.headers || {});
1227 if (inputBody != null && !headers.has('Content-Type')) {
1228 const contentType = extractContentType(inputBody);
1230 headers.append('Content-Type', contentType);
1234 let signal = isRequest(input) ? input.signal : null;
1235 if ('signal' in init) signal = init.signal;
1237 if (signal != null && !isAbortSignal(signal)) {
1238 throw new TypeError('Expected signal to be an instanceof AbortSignal');
1241 this[INTERNALS$2] = {
1243 redirect: init.redirect || input.redirect || 'follow',
1249 // node-fetch-only options
1250 this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20;
1251 this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true;
1252 this.counter = init.counter || input.counter || 0;
1253 this.agent = init.agent || input.agent;
1257 return this[INTERNALS$2].method;
1261 return format_url(this[INTERNALS$2].parsedURL);
1265 return this[INTERNALS$2].headers;
1269 return this[INTERNALS$2].redirect;
1273 return this[INTERNALS$2].signal;
1277 * Clone this request
1282 return new Request(this);
1286 Body.mixIn(Request.prototype);
1288 Object.defineProperty(Request.prototype, Symbol.toStringTag, {
1295 Object.defineProperties(Request.prototype, {
1296 method: { enumerable: true },
1297 url: { enumerable: true },
1298 headers: { enumerable: true },
1299 redirect: { enumerable: true },
1300 clone: { enumerable: true },
1301 signal: { enumerable: true }
1305 * Convert a Request to Node.js http request options.
1307 * @param Request A Request instance
1308 * @return Object The options object to be passed to http.request
1310 function getNodeRequestOptions(request) {
1311 const parsedURL = request[INTERNALS$2].parsedURL;
1312 const headers = new Headers(request[INTERNALS$2].headers);
1315 if (!headers.has('Accept')) {
1316 headers.set('Accept', '*/*');
1320 if (!parsedURL.protocol || !parsedURL.hostname) {
1321 throw new TypeError('Only absolute URLs are supported');
1324 if (!/^https?:$/.test(parsedURL.protocol)) {
1325 throw new TypeError('Only HTTP(S) protocols are supported');
1328 if (request.signal && request.body instanceof Stream.Readable && !streamDestructionSupported) {
1329 throw new Error('Cancellation of streamed requests with AbortSignal is not supported in node < 8');
1332 // HTTP-network-or-cache fetch steps 2.4-2.7
1333 let contentLengthValue = null;
1334 if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
1335 contentLengthValue = '0';
1337 if (request.body != null) {
1338 const totalBytes = getTotalBytes(request);
1339 if (typeof totalBytes === 'number') {
1340 contentLengthValue = String(totalBytes);
1343 if (contentLengthValue) {
1344 headers.set('Content-Length', contentLengthValue);
1347 // HTTP-network-or-cache fetch step 2.11
1348 if (!headers.has('User-Agent')) {
1349 headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
1352 // HTTP-network-or-cache fetch step 2.15
1353 if (request.compress && !headers.has('Accept-Encoding')) {
1354 headers.set('Accept-Encoding', 'gzip,deflate');
1357 let agent = request.agent;
1358 if (typeof agent === 'function') {
1359 agent = agent(parsedURL);
1362 if (!headers.has('Connection') && !agent) {
1363 headers.set('Connection', 'close');
1366 // HTTP-network fetch step 4.2
1367 // chunked encoding is handled by Node.js
1369 return Object.assign({}, parsedURL, {
1370 method: request.method,
1371 headers: exportNodeCompatibleHeaders(headers),
1379 * AbortError interface for cancelled requests
1383 * Create AbortError instance
1385 * @param String message Error message for human
1386 * @return AbortError
1388 function AbortError(message) {
1389 Error.call(this, message);
1391 this.type = 'aborted';
1392 this.message = message;
1394 // hide custom error implementation details from end-users
1395 Error.captureStackTrace(this, this.constructor);
1398 AbortError.prototype = Object.create(Error.prototype);
1399 AbortError.prototype.constructor = AbortError;
1400 AbortError.prototype.name = 'AbortError';
1402 // fix an issue where "PassThrough", "resolve" aren't a named export for node <10
1403 const PassThrough$1 = Stream.PassThrough;
1404 const resolve_url = Url.resolve;
1409 * @param Mixed url Absolute url or Request instance
1410 * @param Object opts Fetch options
1413 function fetch(url, opts) {
1415 // allow custom promise
1416 if (!fetch.Promise) {
1417 throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
1420 Body.Promise = fetch.Promise;
1422 // wrap http.request into fetch
1423 return new fetch.Promise(function (resolve, reject) {
1424 // build request object
1425 const request = new Request(url, opts);
1426 const options = getNodeRequestOptions(request);
1428 const send = (options.protocol === 'https:' ? https : http).request;
1429 const signal = request.signal;
1431 let response = null;
1433 const abort = function abort() {
1434 let error = new AbortError('The user aborted a request.');
1436 if (request.body && request.body instanceof Stream.Readable) {
1437 request.body.destroy(error);
1439 if (!response || !response.body) return;
1440 response.body.emit('error', error);
1443 if (signal && signal.aborted) {
1448 const abortAndFinalize = function abortAndFinalize() {
1454 const req = send(options);
1458 signal.addEventListener('abort', abortAndFinalize);
1461 function finalize() {
1463 if (signal) signal.removeEventListener('abort', abortAndFinalize);
1464 clearTimeout(reqTimeout);
1467 if (request.timeout) {
1468 req.once('socket', function (socket) {
1469 reqTimeout = setTimeout(function () {
1470 reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
1472 }, request.timeout);
1476 req.on('error', function (err) {
1477 reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
1481 req.on('response', function (res) {
1482 clearTimeout(reqTimeout);
1484 const headers = createHeadersLenient(res.headers);
1486 // HTTP fetch step 5
1487 if (fetch.isRedirect(res.statusCode)) {
1488 // HTTP fetch step 5.2
1489 const location = headers.get('Location');
1491 // HTTP fetch step 5.3
1492 const locationURL = location === null ? null : resolve_url(request.url, location);
1494 // HTTP fetch step 5.5
1495 switch (request.redirect) {
1497 reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
1501 // node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
1502 if (locationURL !== null) {
1503 // handle corrupted header
1505 headers.set('Location', locationURL);
1507 // istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
1513 // HTTP-redirect fetch step 2
1514 if (locationURL === null) {
1518 // HTTP-redirect fetch step 5
1519 if (request.counter >= request.follow) {
1520 reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
1525 // HTTP-redirect fetch step 6 (counter increment)
1526 // Create a new Request object.
1527 const requestOpts = {
1528 headers: new Headers(request.headers),
1529 follow: request.follow,
1530 counter: request.counter + 1,
1531 agent: request.agent,
1532 compress: request.compress,
1533 method: request.method,
1535 signal: request.signal,
1536 timeout: request.timeout,
1540 // HTTP-redirect fetch step 9
1541 if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
1542 reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
1547 // HTTP-redirect fetch step 11
1548 if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
1549 requestOpts.method = 'GET';
1550 requestOpts.body = undefined;
1551 requestOpts.headers.delete('content-length');
1554 // HTTP-redirect fetch step 15
1555 resolve(fetch(new Request(locationURL, requestOpts)));
1562 res.once('end', function () {
1563 if (signal) signal.removeEventListener('abort', abortAndFinalize);
1565 let body = res.pipe(new PassThrough$1());
1567 const response_options = {
1569 status: res.statusCode,
1570 statusText: res.statusMessage,
1573 timeout: request.timeout,
1574 counter: request.counter
1577 // HTTP-network fetch step 12.1.1.3
1578 const codings = headers.get('Content-Encoding');
1580 // HTTP-network fetch step 12.1.1.4: handle content codings
1582 // in following scenarios we ignore compression support
1583 // 1. compression support is disabled
1585 // 3. no Content-Encoding header
1586 // 4. no content response (204)
1587 // 5. content not modified response (304)
1588 if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
1589 response = new Response(body, response_options);
1595 // Be less strict when decoding compressed responses, since sometimes
1596 // servers send slightly invalid responses that are still accepted
1597 // by common browsers.
1598 // Always using Z_SYNC_FLUSH is what cURL does.
1599 const zlibOptions = {
1600 flush: zlib.Z_SYNC_FLUSH,
1601 finishFlush: zlib.Z_SYNC_FLUSH
1605 if (codings == 'gzip' || codings == 'x-gzip') {
1606 body = body.pipe(zlib.createGunzip(zlibOptions));
1607 response = new Response(body, response_options);
1613 if (codings == 'deflate' || codings == 'x-deflate') {
1614 // handle the infamous raw deflate response from old servers
1615 // a hack for old IIS and Apache servers
1616 const raw = res.pipe(new PassThrough$1());
1617 raw.once('data', function (chunk) {
1618 // see http://stackoverflow.com/questions/37519828
1619 if ((chunk[0] & 0x0F) === 0x08) {
1620 body = body.pipe(zlib.createInflate());
1622 body = body.pipe(zlib.createInflateRaw());
1624 response = new Response(body, response_options);
1631 if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
1632 body = body.pipe(zlib.createBrotliDecompress());
1633 response = new Response(body, response_options);
1638 // otherwise, use response as-is
1639 response = new Response(body, response_options);
1643 writeToStream(req, request);
1647 * Redirect code matching
1649 * @param Number code Status code
1652 fetch.isRedirect = function (code) {
1653 return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
1657 fetch.Promise = global.Promise;
1659 export default fetch;
1660 export { Headers, Request, Response, FetchError };