twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

index.mjs (40866B)


      1 import Stream from 'stream';
      2 import http from 'http';
      3 import Url from 'url';
      4 import https from 'https';
      5 import zlib from 'zlib';
      6 
      7 // Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js
      8 
      9 // fix for "Readable" isn't a named export issue
     10 const Readable = Stream.Readable;
     11 
     12 const BUFFER = Symbol('buffer');
     13 const TYPE = Symbol('type');
     14 
     15 class Blob {
     16 	constructor() {
     17 		this[TYPE] = '';
     18 
     19 		const blobParts = arguments[0];
     20 		const options = arguments[1];
     21 
     22 		const buffers = [];
     23 		let size = 0;
     24 
     25 		if (blobParts) {
     26 			const a = blobParts;
     27 			const length = Number(a.length);
     28 			for (let i = 0; i < length; i++) {
     29 				const element = a[i];
     30 				let buffer;
     31 				if (element instanceof Buffer) {
     32 					buffer = element;
     33 				} else if (ArrayBuffer.isView(element)) {
     34 					buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
     35 				} else if (element instanceof ArrayBuffer) {
     36 					buffer = Buffer.from(element);
     37 				} else if (element instanceof Blob) {
     38 					buffer = element[BUFFER];
     39 				} else {
     40 					buffer = Buffer.from(typeof element === 'string' ? element : String(element));
     41 				}
     42 				size += buffer.length;
     43 				buffers.push(buffer);
     44 			}
     45 		}
     46 
     47 		this[BUFFER] = Buffer.concat(buffers);
     48 
     49 		let type = options && options.type !== undefined && String(options.type).toLowerCase();
     50 		if (type && !/[^\u0020-\u007E]/.test(type)) {
     51 			this[TYPE] = type;
     52 		}
     53 	}
     54 	get size() {
     55 		return this[BUFFER].length;
     56 	}
     57 	get type() {
     58 		return this[TYPE];
     59 	}
     60 	text() {
     61 		return Promise.resolve(this[BUFFER].toString());
     62 	}
     63 	arrayBuffer() {
     64 		const buf = this[BUFFER];
     65 		const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
     66 		return Promise.resolve(ab);
     67 	}
     68 	stream() {
     69 		const readable = new Readable();
     70 		readable._read = function () {};
     71 		readable.push(this[BUFFER]);
     72 		readable.push(null);
     73 		return readable;
     74 	}
     75 	toString() {
     76 		return '[object Blob]';
     77 	}
     78 	slice() {
     79 		const size = this.size;
     80 
     81 		const start = arguments[0];
     82 		const end = arguments[1];
     83 		let relativeStart, relativeEnd;
     84 		if (start === undefined) {
     85 			relativeStart = 0;
     86 		} else if (start < 0) {
     87 			relativeStart = Math.max(size + start, 0);
     88 		} else {
     89 			relativeStart = Math.min(start, size);
     90 		}
     91 		if (end === undefined) {
     92 			relativeEnd = size;
     93 		} else if (end < 0) {
     94 			relativeEnd = Math.max(size + end, 0);
     95 		} else {
     96 			relativeEnd = Math.min(end, size);
     97 		}
     98 		const span = Math.max(relativeEnd - relativeStart, 0);
     99 
    100 		const buffer = this[BUFFER];
    101 		const slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
    102 		const blob = new Blob([], { type: arguments[2] });
    103 		blob[BUFFER] = slicedBuffer;
    104 		return blob;
    105 	}
    106 }
    107 
    108 Object.defineProperties(Blob.prototype, {
    109 	size: { enumerable: true },
    110 	type: { enumerable: true },
    111 	slice: { enumerable: true }
    112 });
    113 
    114 Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
    115 	value: 'Blob',
    116 	writable: false,
    117 	enumerable: false,
    118 	configurable: true
    119 });
    120 
    121 /**
    122  * fetch-error.js
    123  *
    124  * FetchError interface for operational errors
    125  */
    126 
    127 /**
    128  * Create FetchError instance
    129  *
    130  * @param   String      message      Error message for human
    131  * @param   String      type         Error type for machine
    132  * @param   String      systemError  For Node.js system error
    133  * @return  FetchError
    134  */
    135 function FetchError(message, type, systemError) {
    136   Error.call(this, message);
    137 
    138   this.message = message;
    139   this.type = type;
    140 
    141   // when err.type is `system`, err.code contains system error code
    142   if (systemError) {
    143     this.code = this.errno = systemError.code;
    144   }
    145 
    146   // hide custom error implementation details from end-users
    147   Error.captureStackTrace(this, this.constructor);
    148 }
    149 
    150 FetchError.prototype = Object.create(Error.prototype);
    151 FetchError.prototype.constructor = FetchError;
    152 FetchError.prototype.name = 'FetchError';
    153 
    154 let convert;
    155 try {
    156 	convert = require('encoding').convert;
    157 } catch (e) {}
    158 
    159 const INTERNALS = Symbol('Body internals');
    160 
    161 // fix an issue where "PassThrough" isn't a named export for node <10
    162 const PassThrough = Stream.PassThrough;
    163 
    164 /**
    165  * Body mixin
    166  *
    167  * Ref: https://fetch.spec.whatwg.org/#body
    168  *
    169  * @param   Stream  body  Readable stream
    170  * @param   Object  opts  Response options
    171  * @return  Void
    172  */
    173 function Body(body) {
    174 	var _this = this;
    175 
    176 	var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    177 	    _ref$size = _ref.size;
    178 
    179 	let size = _ref$size === undefined ? 0 : _ref$size;
    180 	var _ref$timeout = _ref.timeout;
    181 	let timeout = _ref$timeout === undefined ? 0 : _ref$timeout;
    182 
    183 	if (body == null) {
    184 		// body is undefined or null
    185 		body = null;
    186 	} else if (isURLSearchParams(body)) {
    187 		// body is a URLSearchParams
    188 		body = Buffer.from(body.toString());
    189 	} else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
    190 		// body is ArrayBuffer
    191 		body = Buffer.from(body);
    192 	} else if (ArrayBuffer.isView(body)) {
    193 		// body is ArrayBufferView
    194 		body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
    195 	} else if (body instanceof Stream) ; else {
    196 		// none of the above
    197 		// coerce to string then buffer
    198 		body = Buffer.from(String(body));
    199 	}
    200 	this[INTERNALS] = {
    201 		body,
    202 		disturbed: false,
    203 		error: null
    204 	};
    205 	this.size = size;
    206 	this.timeout = timeout;
    207 
    208 	if (body instanceof Stream) {
    209 		body.on('error', function (err) {
    210 			const error = err.name === 'AbortError' ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, 'system', err);
    211 			_this[INTERNALS].error = error;
    212 		});
    213 	}
    214 }
    215 
    216 Body.prototype = {
    217 	get body() {
    218 		return this[INTERNALS].body;
    219 	},
    220 
    221 	get bodyUsed() {
    222 		return this[INTERNALS].disturbed;
    223 	},
    224 
    225 	/**
    226   * Decode response as ArrayBuffer
    227   *
    228   * @return  Promise
    229   */
    230 	arrayBuffer() {
    231 		return consumeBody.call(this).then(function (buf) {
    232 			return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
    233 		});
    234 	},
    235 
    236 	/**
    237   * Return raw response as Blob
    238   *
    239   * @return Promise
    240   */
    241 	blob() {
    242 		let ct = this.headers && this.headers.get('content-type') || '';
    243 		return consumeBody.call(this).then(function (buf) {
    244 			return Object.assign(
    245 			// Prevent copying
    246 			new Blob([], {
    247 				type: ct.toLowerCase()
    248 			}), {
    249 				[BUFFER]: buf
    250 			});
    251 		});
    252 	},
    253 
    254 	/**
    255   * Decode response as json
    256   *
    257   * @return  Promise
    258   */
    259 	json() {
    260 		var _this2 = this;
    261 
    262 		return consumeBody.call(this).then(function (buffer) {
    263 			try {
    264 				return JSON.parse(buffer.toString());
    265 			} catch (err) {
    266 				return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
    267 			}
    268 		});
    269 	},
    270 
    271 	/**
    272   * Decode response as text
    273   *
    274   * @return  Promise
    275   */
    276 	text() {
    277 		return consumeBody.call(this).then(function (buffer) {
    278 			return buffer.toString();
    279 		});
    280 	},
    281 
    282 	/**
    283   * Decode response as buffer (non-spec api)
    284   *
    285   * @return  Promise
    286   */
    287 	buffer() {
    288 		return consumeBody.call(this);
    289 	},
    290 
    291 	/**
    292   * Decode response as text, while automatically detecting the encoding and
    293   * trying to decode to UTF-8 (non-spec api)
    294   *
    295   * @return  Promise
    296   */
    297 	textConverted() {
    298 		var _this3 = this;
    299 
    300 		return consumeBody.call(this).then(function (buffer) {
    301 			return convertBody(buffer, _this3.headers);
    302 		});
    303 	}
    304 };
    305 
    306 // In browsers, all properties are enumerable.
    307 Object.defineProperties(Body.prototype, {
    308 	body: { enumerable: true },
    309 	bodyUsed: { enumerable: true },
    310 	arrayBuffer: { enumerable: true },
    311 	blob: { enumerable: true },
    312 	json: { enumerable: true },
    313 	text: { enumerable: true }
    314 });
    315 
    316 Body.mixIn = function (proto) {
    317 	for (const name of Object.getOwnPropertyNames(Body.prototype)) {
    318 		// istanbul ignore else: future proof
    319 		if (!(name in proto)) {
    320 			const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
    321 			Object.defineProperty(proto, name, desc);
    322 		}
    323 	}
    324 };
    325 
    326 /**
    327  * Consume and convert an entire Body to a Buffer.
    328  *
    329  * Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
    330  *
    331  * @return  Promise
    332  */
    333 function consumeBody() {
    334 	var _this4 = this;
    335 
    336 	if (this[INTERNALS].disturbed) {
    337 		return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
    338 	}
    339 
    340 	this[INTERNALS].disturbed = true;
    341 
    342 	if (this[INTERNALS].error) {
    343 		return Body.Promise.reject(this[INTERNALS].error);
    344 	}
    345 
    346 	let body = this.body;
    347 
    348 	// body is null
    349 	if (body === null) {
    350 		return Body.Promise.resolve(Buffer.alloc(0));
    351 	}
    352 
    353 	// body is blob
    354 	if (isBlob(body)) {
    355 		body = body.stream();
    356 	}
    357 
    358 	// body is buffer
    359 	if (Buffer.isBuffer(body)) {
    360 		return Body.Promise.resolve(body);
    361 	}
    362 
    363 	// istanbul ignore if: should never happen
    364 	if (!(body instanceof Stream)) {
    365 		return Body.Promise.resolve(Buffer.alloc(0));
    366 	}
    367 
    368 	// body is stream
    369 	// get ready to actually consume the body
    370 	let accum = [];
    371 	let accumBytes = 0;
    372 	let abort = false;
    373 
    374 	return new Body.Promise(function (resolve, reject) {
    375 		let resTimeout;
    376 
    377 		// allow timeout on slow response body
    378 		if (_this4.timeout) {
    379 			resTimeout = setTimeout(function () {
    380 				abort = true;
    381 				reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, 'body-timeout'));
    382 			}, _this4.timeout);
    383 		}
    384 
    385 		// handle stream errors
    386 		body.on('error', function (err) {
    387 			if (err.name === 'AbortError') {
    388 				// if the request was aborted, reject with this Error
    389 				abort = true;
    390 				reject(err);
    391 			} else {
    392 				// other errors, such as incorrect content-encoding
    393 				reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
    394 			}
    395 		});
    396 
    397 		body.on('data', function (chunk) {
    398 			if (abort || chunk === null) {
    399 				return;
    400 			}
    401 
    402 			if (_this4.size && accumBytes + chunk.length > _this4.size) {
    403 				abort = true;
    404 				reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, 'max-size'));
    405 				return;
    406 			}
    407 
    408 			accumBytes += chunk.length;
    409 			accum.push(chunk);
    410 		});
    411 
    412 		body.on('end', function () {
    413 			if (abort) {
    414 				return;
    415 			}
    416 
    417 			clearTimeout(resTimeout);
    418 
    419 			try {
    420 				resolve(Buffer.concat(accum, accumBytes));
    421 			} catch (err) {
    422 				// handle streams that have accumulated too much data (issue #414)
    423 				reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, 'system', err));
    424 			}
    425 		});
    426 	});
    427 }
    428 
    429 /**
    430  * Detect buffer encoding and convert to target encoding
    431  * ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding
    432  *
    433  * @param   Buffer  buffer    Incoming buffer
    434  * @param   String  encoding  Target encoding
    435  * @return  String
    436  */
    437 function convertBody(buffer, headers) {
    438 	if (typeof convert !== 'function') {
    439 		throw new Error('The package `encoding` must be installed to use the textConverted() function');
    440 	}
    441 
    442 	const ct = headers.get('content-type');
    443 	let charset = 'utf-8';
    444 	let res, str;
    445 
    446 	// header
    447 	if (ct) {
    448 		res = /charset=([^;]*)/i.exec(ct);
    449 	}
    450 
    451 	// no charset in content type, peek at response body for at most 1024 bytes
    452 	str = buffer.slice(0, 1024).toString();
    453 
    454 	// html5
    455 	if (!res && str) {
    456 		res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
    457 	}
    458 
    459 	// html4
    460 	if (!res && str) {
    461 		res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
    462 
    463 		if (res) {
    464 			res = /charset=(.*)/i.exec(res.pop());
    465 		}
    466 	}
    467 
    468 	// xml
    469 	if (!res && str) {
    470 		res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str);
    471 	}
    472 
    473 	// found charset
    474 	if (res) {
    475 		charset = res.pop();
    476 
    477 		// prevent decode issues when sites use incorrect encoding
    478 		// ref: https://hsivonen.fi/encoding-menu/
    479 		if (charset === 'gb2312' || charset === 'gbk') {
    480 			charset = 'gb18030';
    481 		}
    482 	}
    483 
    484 	// turn raw buffers into a single utf-8 buffer
    485 	return convert(buffer, 'UTF-8', charset).toString();
    486 }
    487 
    488 /**
    489  * Detect a URLSearchParams object
    490  * ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
    491  *
    492  * @param   Object  obj     Object to detect by type or brand
    493  * @return  String
    494  */
    495 function isURLSearchParams(obj) {
    496 	// Duck-typing as a necessary condition.
    497 	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') {
    498 		return false;
    499 	}
    500 
    501 	// Brand-checking and more duck-typing as optional condition.
    502 	return obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function';
    503 }
    504 
    505 /**
    506  * Check if `obj` is a W3C `Blob` object (which `File` inherits from)
    507  * @param  {*} obj
    508  * @return {boolean}
    509  */
    510 function isBlob(obj) {
    511 	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]);
    512 }
    513 
    514 /**
    515  * Clone body given Res/Req instance
    516  *
    517  * @param   Mixed  instance  Response or Request instance
    518  * @return  Mixed
    519  */
    520 function clone(instance) {
    521 	let p1, p2;
    522 	let body = instance.body;
    523 
    524 	// don't allow cloning a used body
    525 	if (instance.bodyUsed) {
    526 		throw new Error('cannot clone body after it is used');
    527 	}
    528 
    529 	// check that body is a stream and not form-data object
    530 	// note: we can't clone the form-data object without having it as a dependency
    531 	if (body instanceof Stream && typeof body.getBoundary !== 'function') {
    532 		// tee instance body
    533 		p1 = new PassThrough();
    534 		p2 = new PassThrough();
    535 		body.pipe(p1);
    536 		body.pipe(p2);
    537 		// set instance body to teed body and return the other teed body
    538 		instance[INTERNALS].body = p1;
    539 		body = p2;
    540 	}
    541 
    542 	return body;
    543 }
    544 
    545 /**
    546  * Performs the operation "extract a `Content-Type` value from |object|" as
    547  * specified in the specification:
    548  * https://fetch.spec.whatwg.org/#concept-bodyinit-extract
    549  *
    550  * This function assumes that instance.body is present.
    551  *
    552  * @param   Mixed  instance  Any options.body input
    553  */
    554 function extractContentType(body) {
    555 	if (body === null) {
    556 		// body is null
    557 		return null;
    558 	} else if (typeof body === 'string') {
    559 		// body is string
    560 		return 'text/plain;charset=UTF-8';
    561 	} else if (isURLSearchParams(body)) {
    562 		// body is a URLSearchParams
    563 		return 'application/x-www-form-urlencoded;charset=UTF-8';
    564 	} else if (isBlob(body)) {
    565 		// body is blob
    566 		return body.type || null;
    567 	} else if (Buffer.isBuffer(body)) {
    568 		// body is buffer
    569 		return null;
    570 	} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
    571 		// body is ArrayBuffer
    572 		return null;
    573 	} else if (ArrayBuffer.isView(body)) {
    574 		// body is ArrayBufferView
    575 		return null;
    576 	} else if (typeof body.getBoundary === 'function') {
    577 		// detect form data input from form-data module
    578 		return `multipart/form-data;boundary=${body.getBoundary()}`;
    579 	} else if (body instanceof Stream) {
    580 		// body is stream
    581 		// can't really do much about this
    582 		return null;
    583 	} else {
    584 		// Body constructor defaults other things to string
    585 		return 'text/plain;charset=UTF-8';
    586 	}
    587 }
    588 
    589 /**
    590  * The Fetch Standard treats this as if "total bytes" is a property on the body.
    591  * For us, we have to explicitly get it with a function.
    592  *
    593  * ref: https://fetch.spec.whatwg.org/#concept-body-total-bytes
    594  *
    595  * @param   Body    instance   Instance of Body
    596  * @return  Number?            Number of bytes, or null if not possible
    597  */
    598 function getTotalBytes(instance) {
    599 	const body = instance.body;
    600 
    601 
    602 	if (body === null) {
    603 		// body is null
    604 		return 0;
    605 	} else if (isBlob(body)) {
    606 		return body.size;
    607 	} else if (Buffer.isBuffer(body)) {
    608 		// body is buffer
    609 		return body.length;
    610 	} else if (body && typeof body.getLengthSync === 'function') {
    611 		// detect form data input from form-data module
    612 		if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
    613 		body.hasKnownLength && body.hasKnownLength()) {
    614 			// 2.x
    615 			return body.getLengthSync();
    616 		}
    617 		return null;
    618 	} else {
    619 		// body is stream
    620 		return null;
    621 	}
    622 }
    623 
    624 /**
    625  * Write a Body to a Node.js WritableStream (e.g. http.Request) object.
    626  *
    627  * @param   Body    instance   Instance of Body
    628  * @return  Void
    629  */
    630 function writeToStream(dest, instance) {
    631 	const body = instance.body;
    632 
    633 
    634 	if (body === null) {
    635 		// body is null
    636 		dest.end();
    637 	} else if (isBlob(body)) {
    638 		body.stream().pipe(dest);
    639 	} else if (Buffer.isBuffer(body)) {
    640 		// body is buffer
    641 		dest.write(body);
    642 		dest.end();
    643 	} else {
    644 		// body is stream
    645 		body.pipe(dest);
    646 	}
    647 }
    648 
    649 // expose Promise
    650 Body.Promise = global.Promise;
    651 
    652 /**
    653  * headers.js
    654  *
    655  * Headers class offers convenient helpers
    656  */
    657 
    658 const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
    659 const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
    660 
    661 function validateName(name) {
    662 	name = `${name}`;
    663 	if (invalidTokenRegex.test(name) || name === '') {
    664 		throw new TypeError(`${name} is not a legal HTTP header name`);
    665 	}
    666 }
    667 
    668 function validateValue(value) {
    669 	value = `${value}`;
    670 	if (invalidHeaderCharRegex.test(value)) {
    671 		throw new TypeError(`${value} is not a legal HTTP header value`);
    672 	}
    673 }
    674 
    675 /**
    676  * Find the key in the map object given a header name.
    677  *
    678  * Returns undefined if not found.
    679  *
    680  * @param   String  name  Header name
    681  * @return  String|Undefined
    682  */
    683 function find(map, name) {
    684 	name = name.toLowerCase();
    685 	for (const key in map) {
    686 		if (key.toLowerCase() === name) {
    687 			return key;
    688 		}
    689 	}
    690 	return undefined;
    691 }
    692 
    693 const MAP = Symbol('map');
    694 class Headers {
    695 	/**
    696   * Headers class
    697   *
    698   * @param   Object  headers  Response headers
    699   * @return  Void
    700   */
    701 	constructor() {
    702 		let init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
    703 
    704 		this[MAP] = Object.create(null);
    705 
    706 		if (init instanceof Headers) {
    707 			const rawHeaders = init.raw();
    708 			const headerNames = Object.keys(rawHeaders);
    709 
    710 			for (const headerName of headerNames) {
    711 				for (const value of rawHeaders[headerName]) {
    712 					this.append(headerName, value);
    713 				}
    714 			}
    715 
    716 			return;
    717 		}
    718 
    719 		// We don't worry about converting prop to ByteString here as append()
    720 		// will handle it.
    721 		if (init == null) ; else if (typeof init === 'object') {
    722 			const method = init[Symbol.iterator];
    723 			if (method != null) {
    724 				if (typeof method !== 'function') {
    725 					throw new TypeError('Header pairs must be iterable');
    726 				}
    727 
    728 				// sequence<sequence<ByteString>>
    729 				// Note: per spec we have to first exhaust the lists then process them
    730 				const pairs = [];
    731 				for (const pair of init) {
    732 					if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') {
    733 						throw new TypeError('Each header pair must be iterable');
    734 					}
    735 					pairs.push(Array.from(pair));
    736 				}
    737 
    738 				for (const pair of pairs) {
    739 					if (pair.length !== 2) {
    740 						throw new TypeError('Each header pair must be a name/value tuple');
    741 					}
    742 					this.append(pair[0], pair[1]);
    743 				}
    744 			} else {
    745 				// record<ByteString, ByteString>
    746 				for (const key of Object.keys(init)) {
    747 					const value = init[key];
    748 					this.append(key, value);
    749 				}
    750 			}
    751 		} else {
    752 			throw new TypeError('Provided initializer must be an object');
    753 		}
    754 	}
    755 
    756 	/**
    757   * Return combined header value given name
    758   *
    759   * @param   String  name  Header name
    760   * @return  Mixed
    761   */
    762 	get(name) {
    763 		name = `${name}`;
    764 		validateName(name);
    765 		const key = find(this[MAP], name);
    766 		if (key === undefined) {
    767 			return null;
    768 		}
    769 
    770 		return this[MAP][key].join(', ');
    771 	}
    772 
    773 	/**
    774   * Iterate over all headers
    775   *
    776   * @param   Function  callback  Executed for each item with parameters (value, name, thisArg)
    777   * @param   Boolean   thisArg   `this` context for callback function
    778   * @return  Void
    779   */
    780 	forEach(callback) {
    781 		let thisArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
    782 
    783 		let pairs = getHeaders(this);
    784 		let i = 0;
    785 		while (i < pairs.length) {
    786 			var _pairs$i = pairs[i];
    787 			const name = _pairs$i[0],
    788 			      value = _pairs$i[1];
    789 
    790 			callback.call(thisArg, value, name, this);
    791 			pairs = getHeaders(this);
    792 			i++;
    793 		}
    794 	}
    795 
    796 	/**
    797   * Overwrite header values given name
    798   *
    799   * @param   String  name   Header name
    800   * @param   String  value  Header value
    801   * @return  Void
    802   */
    803 	set(name, value) {
    804 		name = `${name}`;
    805 		value = `${value}`;
    806 		validateName(name);
    807 		validateValue(value);
    808 		const key = find(this[MAP], name);
    809 		this[MAP][key !== undefined ? key : name] = [value];
    810 	}
    811 
    812 	/**
    813   * Append a value onto existing header
    814   *
    815   * @param   String  name   Header name
    816   * @param   String  value  Header value
    817   * @return  Void
    818   */
    819 	append(name, value) {
    820 		name = `${name}`;
    821 		value = `${value}`;
    822 		validateName(name);
    823 		validateValue(value);
    824 		const key = find(this[MAP], name);
    825 		if (key !== undefined) {
    826 			this[MAP][key].push(value);
    827 		} else {
    828 			this[MAP][name] = [value];
    829 		}
    830 	}
    831 
    832 	/**
    833   * Check for header name existence
    834   *
    835   * @param   String   name  Header name
    836   * @return  Boolean
    837   */
    838 	has(name) {
    839 		name = `${name}`;
    840 		validateName(name);
    841 		return find(this[MAP], name) !== undefined;
    842 	}
    843 
    844 	/**
    845   * Delete all header values given name
    846   *
    847   * @param   String  name  Header name
    848   * @return  Void
    849   */
    850 	delete(name) {
    851 		name = `${name}`;
    852 		validateName(name);
    853 		const key = find(this[MAP], name);
    854 		if (key !== undefined) {
    855 			delete this[MAP][key];
    856 		}
    857 	}
    858 
    859 	/**
    860   * Return raw headers (non-spec api)
    861   *
    862   * @return  Object
    863   */
    864 	raw() {
    865 		return this[MAP];
    866 	}
    867 
    868 	/**
    869   * Get an iterator on keys.
    870   *
    871   * @return  Iterator
    872   */
    873 	keys() {
    874 		return createHeadersIterator(this, 'key');
    875 	}
    876 
    877 	/**
    878   * Get an iterator on values.
    879   *
    880   * @return  Iterator
    881   */
    882 	values() {
    883 		return createHeadersIterator(this, 'value');
    884 	}
    885 
    886 	/**
    887   * Get an iterator on entries.
    888   *
    889   * This is the default iterator of the Headers object.
    890   *
    891   * @return  Iterator
    892   */
    893 	[Symbol.iterator]() {
    894 		return createHeadersIterator(this, 'key+value');
    895 	}
    896 }
    897 Headers.prototype.entries = Headers.prototype[Symbol.iterator];
    898 
    899 Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
    900 	value: 'Headers',
    901 	writable: false,
    902 	enumerable: false,
    903 	configurable: true
    904 });
    905 
    906 Object.defineProperties(Headers.prototype, {
    907 	get: { enumerable: true },
    908 	forEach: { enumerable: true },
    909 	set: { enumerable: true },
    910 	append: { enumerable: true },
    911 	has: { enumerable: true },
    912 	delete: { enumerable: true },
    913 	keys: { enumerable: true },
    914 	values: { enumerable: true },
    915 	entries: { enumerable: true }
    916 });
    917 
    918 function getHeaders(headers) {
    919 	let kind = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'key+value';
    920 
    921 	const keys = Object.keys(headers[MAP]).sort();
    922 	return keys.map(kind === 'key' ? function (k) {
    923 		return k.toLowerCase();
    924 	} : kind === 'value' ? function (k) {
    925 		return headers[MAP][k].join(', ');
    926 	} : function (k) {
    927 		return [k.toLowerCase(), headers[MAP][k].join(', ')];
    928 	});
    929 }
    930 
    931 const INTERNAL = Symbol('internal');
    932 
    933 function createHeadersIterator(target, kind) {
    934 	const iterator = Object.create(HeadersIteratorPrototype);
    935 	iterator[INTERNAL] = {
    936 		target,
    937 		kind,
    938 		index: 0
    939 	};
    940 	return iterator;
    941 }
    942 
    943 const HeadersIteratorPrototype = Object.setPrototypeOf({
    944 	next() {
    945 		// istanbul ignore if
    946 		if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
    947 			throw new TypeError('Value of `this` is not a HeadersIterator');
    948 		}
    949 
    950 		var _INTERNAL = this[INTERNAL];
    951 		const target = _INTERNAL.target,
    952 		      kind = _INTERNAL.kind,
    953 		      index = _INTERNAL.index;
    954 
    955 		const values = getHeaders(target, kind);
    956 		const len = values.length;
    957 		if (index >= len) {
    958 			return {
    959 				value: undefined,
    960 				done: true
    961 			};
    962 		}
    963 
    964 		this[INTERNAL].index = index + 1;
    965 
    966 		return {
    967 			value: values[index],
    968 			done: false
    969 		};
    970 	}
    971 }, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())));
    972 
    973 Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
    974 	value: 'HeadersIterator',
    975 	writable: false,
    976 	enumerable: false,
    977 	configurable: true
    978 });
    979 
    980 /**
    981  * Export the Headers object in a form that Node.js can consume.
    982  *
    983  * @param   Headers  headers
    984  * @return  Object
    985  */
    986 function exportNodeCompatibleHeaders(headers) {
    987 	const obj = Object.assign({ __proto__: null }, headers[MAP]);
    988 
    989 	// http.request() only supports string as Host header. This hack makes
    990 	// specifying custom Host header possible.
    991 	const hostHeaderKey = find(headers[MAP], 'Host');
    992 	if (hostHeaderKey !== undefined) {
    993 		obj[hostHeaderKey] = obj[hostHeaderKey][0];
    994 	}
    995 
    996 	return obj;
    997 }
    998 
    999 /**
   1000  * Create a Headers object from an object of headers, ignoring those that do
   1001  * not conform to HTTP grammar productions.
   1002  *
   1003  * @param   Object  obj  Object of headers
   1004  * @return  Headers
   1005  */
   1006 function createHeadersLenient(obj) {
   1007 	const headers = new Headers();
   1008 	for (const name of Object.keys(obj)) {
   1009 		if (invalidTokenRegex.test(name)) {
   1010 			continue;
   1011 		}
   1012 		if (Array.isArray(obj[name])) {
   1013 			for (const val of obj[name]) {
   1014 				if (invalidHeaderCharRegex.test(val)) {
   1015 					continue;
   1016 				}
   1017 				if (headers[MAP][name] === undefined) {
   1018 					headers[MAP][name] = [val];
   1019 				} else {
   1020 					headers[MAP][name].push(val);
   1021 				}
   1022 			}
   1023 		} else if (!invalidHeaderCharRegex.test(obj[name])) {
   1024 			headers[MAP][name] = [obj[name]];
   1025 		}
   1026 	}
   1027 	return headers;
   1028 }
   1029 
   1030 const INTERNALS$1 = Symbol('Response internals');
   1031 
   1032 // fix an issue where "STATUS_CODES" aren't a named export for node <10
   1033 const STATUS_CODES = http.STATUS_CODES;
   1034 
   1035 /**
   1036  * Response class
   1037  *
   1038  * @param   Stream  body  Readable stream
   1039  * @param   Object  opts  Response options
   1040  * @return  Void
   1041  */
   1042 class Response {
   1043 	constructor() {
   1044 		let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
   1045 		let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
   1046 
   1047 		Body.call(this, body, opts);
   1048 
   1049 		const status = opts.status || 200;
   1050 		const headers = new Headers(opts.headers);
   1051 
   1052 		if (body != null && !headers.has('Content-Type')) {
   1053 			const contentType = extractContentType(body);
   1054 			if (contentType) {
   1055 				headers.append('Content-Type', contentType);
   1056 			}
   1057 		}
   1058 
   1059 		this[INTERNALS$1] = {
   1060 			url: opts.url,
   1061 			status,
   1062 			statusText: opts.statusText || STATUS_CODES[status],
   1063 			headers,
   1064 			counter: opts.counter
   1065 		};
   1066 	}
   1067 
   1068 	get url() {
   1069 		return this[INTERNALS$1].url || '';
   1070 	}
   1071 
   1072 	get status() {
   1073 		return this[INTERNALS$1].status;
   1074 	}
   1075 
   1076 	/**
   1077   * Convenience property representing if the request ended normally
   1078   */
   1079 	get ok() {
   1080 		return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300;
   1081 	}
   1082 
   1083 	get redirected() {
   1084 		return this[INTERNALS$1].counter > 0;
   1085 	}
   1086 
   1087 	get statusText() {
   1088 		return this[INTERNALS$1].statusText;
   1089 	}
   1090 
   1091 	get headers() {
   1092 		return this[INTERNALS$1].headers;
   1093 	}
   1094 
   1095 	/**
   1096   * Clone this response
   1097   *
   1098   * @return  Response
   1099   */
   1100 	clone() {
   1101 		return new Response(clone(this), {
   1102 			url: this.url,
   1103 			status: this.status,
   1104 			statusText: this.statusText,
   1105 			headers: this.headers,
   1106 			ok: this.ok,
   1107 			redirected: this.redirected
   1108 		});
   1109 	}
   1110 }
   1111 
   1112 Body.mixIn(Response.prototype);
   1113 
   1114 Object.defineProperties(Response.prototype, {
   1115 	url: { enumerable: true },
   1116 	status: { enumerable: true },
   1117 	ok: { enumerable: true },
   1118 	redirected: { enumerable: true },
   1119 	statusText: { enumerable: true },
   1120 	headers: { enumerable: true },
   1121 	clone: { enumerable: true }
   1122 });
   1123 
   1124 Object.defineProperty(Response.prototype, Symbol.toStringTag, {
   1125 	value: 'Response',
   1126 	writable: false,
   1127 	enumerable: false,
   1128 	configurable: true
   1129 });
   1130 
   1131 const INTERNALS$2 = Symbol('Request internals');
   1132 
   1133 // fix an issue where "format", "parse" aren't a named export for node <10
   1134 const parse_url = Url.parse;
   1135 const format_url = Url.format;
   1136 
   1137 const streamDestructionSupported = 'destroy' in Stream.Readable.prototype;
   1138 
   1139 /**
   1140  * Check if a value is an instance of Request.
   1141  *
   1142  * @param   Mixed   input
   1143  * @return  Boolean
   1144  */
   1145 function isRequest(input) {
   1146 	return typeof input === 'object' && typeof input[INTERNALS$2] === 'object';
   1147 }
   1148 
   1149 function isAbortSignal(signal) {
   1150 	const proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
   1151 	return !!(proto && proto.constructor.name === 'AbortSignal');
   1152 }
   1153 
   1154 /**
   1155  * Request class
   1156  *
   1157  * @param   Mixed   input  Url or Request instance
   1158  * @param   Object  init   Custom options
   1159  * @return  Void
   1160  */
   1161 class Request {
   1162 	constructor(input) {
   1163 		let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
   1164 
   1165 		let parsedURL;
   1166 
   1167 		// normalize input
   1168 		if (!isRequest(input)) {
   1169 			if (input && input.href) {
   1170 				// in order to support Node.js' Url objects; though WHATWG's URL objects
   1171 				// will fall into this branch also (since their `toString()` will return
   1172 				// `href` property anyway)
   1173 				parsedURL = parse_url(input.href);
   1174 			} else {
   1175 				// coerce input to a string before attempting to parse
   1176 				parsedURL = parse_url(`${input}`);
   1177 			}
   1178 			input = {};
   1179 		} else {
   1180 			parsedURL = parse_url(input.url);
   1181 		}
   1182 
   1183 		let method = init.method || input.method || 'GET';
   1184 		method = method.toUpperCase();
   1185 
   1186 		if ((init.body != null || isRequest(input) && input.body !== null) && (method === 'GET' || method === 'HEAD')) {
   1187 			throw new TypeError('Request with GET/HEAD method cannot have body');
   1188 		}
   1189 
   1190 		let inputBody = init.body != null ? init.body : isRequest(input) && input.body !== null ? clone(input) : null;
   1191 
   1192 		Body.call(this, inputBody, {
   1193 			timeout: init.timeout || input.timeout || 0,
   1194 			size: init.size || input.size || 0
   1195 		});
   1196 
   1197 		const headers = new Headers(init.headers || input.headers || {});
   1198 
   1199 		if (inputBody != null && !headers.has('Content-Type')) {
   1200 			const contentType = extractContentType(inputBody);
   1201 			if (contentType) {
   1202 				headers.append('Content-Type', contentType);
   1203 			}
   1204 		}
   1205 
   1206 		let signal = isRequest(input) ? input.signal : null;
   1207 		if ('signal' in init) signal = init.signal;
   1208 
   1209 		if (signal != null && !isAbortSignal(signal)) {
   1210 			throw new TypeError('Expected signal to be an instanceof AbortSignal');
   1211 		}
   1212 
   1213 		this[INTERNALS$2] = {
   1214 			method,
   1215 			redirect: init.redirect || input.redirect || 'follow',
   1216 			headers,
   1217 			parsedURL,
   1218 			signal
   1219 		};
   1220 
   1221 		// node-fetch-only options
   1222 		this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20;
   1223 		this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true;
   1224 		this.counter = init.counter || input.counter || 0;
   1225 		this.agent = init.agent || input.agent;
   1226 	}
   1227 
   1228 	get method() {
   1229 		return this[INTERNALS$2].method;
   1230 	}
   1231 
   1232 	get url() {
   1233 		return format_url(this[INTERNALS$2].parsedURL);
   1234 	}
   1235 
   1236 	get headers() {
   1237 		return this[INTERNALS$2].headers;
   1238 	}
   1239 
   1240 	get redirect() {
   1241 		return this[INTERNALS$2].redirect;
   1242 	}
   1243 
   1244 	get signal() {
   1245 		return this[INTERNALS$2].signal;
   1246 	}
   1247 
   1248 	/**
   1249   * Clone this request
   1250   *
   1251   * @return  Request
   1252   */
   1253 	clone() {
   1254 		return new Request(this);
   1255 	}
   1256 }
   1257 
   1258 Body.mixIn(Request.prototype);
   1259 
   1260 Object.defineProperty(Request.prototype, Symbol.toStringTag, {
   1261 	value: 'Request',
   1262 	writable: false,
   1263 	enumerable: false,
   1264 	configurable: true
   1265 });
   1266 
   1267 Object.defineProperties(Request.prototype, {
   1268 	method: { enumerable: true },
   1269 	url: { enumerable: true },
   1270 	headers: { enumerable: true },
   1271 	redirect: { enumerable: true },
   1272 	clone: { enumerable: true },
   1273 	signal: { enumerable: true }
   1274 });
   1275 
   1276 /**
   1277  * Convert a Request to Node.js http request options.
   1278  *
   1279  * @param   Request  A Request instance
   1280  * @return  Object   The options object to be passed to http.request
   1281  */
   1282 function getNodeRequestOptions(request) {
   1283 	const parsedURL = request[INTERNALS$2].parsedURL;
   1284 	const headers = new Headers(request[INTERNALS$2].headers);
   1285 
   1286 	// fetch step 1.3
   1287 	if (!headers.has('Accept')) {
   1288 		headers.set('Accept', '*/*');
   1289 	}
   1290 
   1291 	// Basic fetch
   1292 	if (!parsedURL.protocol || !parsedURL.hostname) {
   1293 		throw new TypeError('Only absolute URLs are supported');
   1294 	}
   1295 
   1296 	if (!/^https?:$/.test(parsedURL.protocol)) {
   1297 		throw new TypeError('Only HTTP(S) protocols are supported');
   1298 	}
   1299 
   1300 	if (request.signal && request.body instanceof Stream.Readable && !streamDestructionSupported) {
   1301 		throw new Error('Cancellation of streamed requests with AbortSignal is not supported in node < 8');
   1302 	}
   1303 
   1304 	// HTTP-network-or-cache fetch steps 2.4-2.7
   1305 	let contentLengthValue = null;
   1306 	if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
   1307 		contentLengthValue = '0';
   1308 	}
   1309 	if (request.body != null) {
   1310 		const totalBytes = getTotalBytes(request);
   1311 		if (typeof totalBytes === 'number') {
   1312 			contentLengthValue = String(totalBytes);
   1313 		}
   1314 	}
   1315 	if (contentLengthValue) {
   1316 		headers.set('Content-Length', contentLengthValue);
   1317 	}
   1318 
   1319 	// HTTP-network-or-cache fetch step 2.11
   1320 	if (!headers.has('User-Agent')) {
   1321 		headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
   1322 	}
   1323 
   1324 	// HTTP-network-or-cache fetch step 2.15
   1325 	if (request.compress && !headers.has('Accept-Encoding')) {
   1326 		headers.set('Accept-Encoding', 'gzip,deflate');
   1327 	}
   1328 
   1329 	let agent = request.agent;
   1330 	if (typeof agent === 'function') {
   1331 		agent = agent(parsedURL);
   1332 	}
   1333 
   1334 	if (!headers.has('Connection') && !agent) {
   1335 		headers.set('Connection', 'close');
   1336 	}
   1337 
   1338 	// HTTP-network fetch step 4.2
   1339 	// chunked encoding is handled by Node.js
   1340 
   1341 	return Object.assign({}, parsedURL, {
   1342 		method: request.method,
   1343 		headers: exportNodeCompatibleHeaders(headers),
   1344 		agent
   1345 	});
   1346 }
   1347 
   1348 /**
   1349  * abort-error.js
   1350  *
   1351  * AbortError interface for cancelled requests
   1352  */
   1353 
   1354 /**
   1355  * Create AbortError instance
   1356  *
   1357  * @param   String      message      Error message for human
   1358  * @return  AbortError
   1359  */
   1360 function AbortError(message) {
   1361   Error.call(this, message);
   1362 
   1363   this.type = 'aborted';
   1364   this.message = message;
   1365 
   1366   // hide custom error implementation details from end-users
   1367   Error.captureStackTrace(this, this.constructor);
   1368 }
   1369 
   1370 AbortError.prototype = Object.create(Error.prototype);
   1371 AbortError.prototype.constructor = AbortError;
   1372 AbortError.prototype.name = 'AbortError';
   1373 
   1374 // fix an issue where "PassThrough", "resolve" aren't a named export for node <10
   1375 const PassThrough$1 = Stream.PassThrough;
   1376 const resolve_url = Url.resolve;
   1377 
   1378 /**
   1379  * Fetch function
   1380  *
   1381  * @param   Mixed    url   Absolute url or Request instance
   1382  * @param   Object   opts  Fetch options
   1383  * @return  Promise
   1384  */
   1385 function fetch(url, opts) {
   1386 
   1387 	// allow custom promise
   1388 	if (!fetch.Promise) {
   1389 		throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
   1390 	}
   1391 
   1392 	Body.Promise = fetch.Promise;
   1393 
   1394 	// wrap http.request into fetch
   1395 	return new fetch.Promise(function (resolve, reject) {
   1396 		// build request object
   1397 		const request = new Request(url, opts);
   1398 		const options = getNodeRequestOptions(request);
   1399 
   1400 		const send = (options.protocol === 'https:' ? https : http).request;
   1401 		const signal = request.signal;
   1402 
   1403 		let response = null;
   1404 
   1405 		const abort = function abort() {
   1406 			let error = new AbortError('The user aborted a request.');
   1407 			reject(error);
   1408 			if (request.body && request.body instanceof Stream.Readable) {
   1409 				request.body.destroy(error);
   1410 			}
   1411 			if (!response || !response.body) return;
   1412 			response.body.emit('error', error);
   1413 		};
   1414 
   1415 		if (signal && signal.aborted) {
   1416 			abort();
   1417 			return;
   1418 		}
   1419 
   1420 		const abortAndFinalize = function abortAndFinalize() {
   1421 			abort();
   1422 			finalize();
   1423 		};
   1424 
   1425 		// send request
   1426 		const req = send(options);
   1427 		let reqTimeout;
   1428 
   1429 		if (signal) {
   1430 			signal.addEventListener('abort', abortAndFinalize);
   1431 		}
   1432 
   1433 		function finalize() {
   1434 			req.abort();
   1435 			if (signal) signal.removeEventListener('abort', abortAndFinalize);
   1436 			clearTimeout(reqTimeout);
   1437 		}
   1438 
   1439 		if (request.timeout) {
   1440 			req.once('socket', function (socket) {
   1441 				reqTimeout = setTimeout(function () {
   1442 					reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
   1443 					finalize();
   1444 				}, request.timeout);
   1445 			});
   1446 		}
   1447 
   1448 		req.on('error', function (err) {
   1449 			reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
   1450 			finalize();
   1451 		});
   1452 
   1453 		req.on('response', function (res) {
   1454 			clearTimeout(reqTimeout);
   1455 
   1456 			const headers = createHeadersLenient(res.headers);
   1457 
   1458 			// HTTP fetch step 5
   1459 			if (fetch.isRedirect(res.statusCode)) {
   1460 				// HTTP fetch step 5.2
   1461 				const location = headers.get('Location');
   1462 
   1463 				// HTTP fetch step 5.3
   1464 				const locationURL = location === null ? null : resolve_url(request.url, location);
   1465 
   1466 				// HTTP fetch step 5.5
   1467 				switch (request.redirect) {
   1468 					case 'error':
   1469 						reject(new FetchError(`redirect mode is set to error: ${request.url}`, 'no-redirect'));
   1470 						finalize();
   1471 						return;
   1472 					case 'manual':
   1473 						// node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
   1474 						if (locationURL !== null) {
   1475 							// handle corrupted header
   1476 							try {
   1477 								headers.set('Location', locationURL);
   1478 							} catch (err) {
   1479 								// istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
   1480 								reject(err);
   1481 							}
   1482 						}
   1483 						break;
   1484 					case 'follow':
   1485 						// HTTP-redirect fetch step 2
   1486 						if (locationURL === null) {
   1487 							break;
   1488 						}
   1489 
   1490 						// HTTP-redirect fetch step 5
   1491 						if (request.counter >= request.follow) {
   1492 							reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
   1493 							finalize();
   1494 							return;
   1495 						}
   1496 
   1497 						// HTTP-redirect fetch step 6 (counter increment)
   1498 						// Create a new Request object.
   1499 						const requestOpts = {
   1500 							headers: new Headers(request.headers),
   1501 							follow: request.follow,
   1502 							counter: request.counter + 1,
   1503 							agent: request.agent,
   1504 							compress: request.compress,
   1505 							method: request.method,
   1506 							body: request.body,
   1507 							signal: request.signal,
   1508 							timeout: request.timeout
   1509 						};
   1510 
   1511 						// HTTP-redirect fetch step 9
   1512 						if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
   1513 							reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
   1514 							finalize();
   1515 							return;
   1516 						}
   1517 
   1518 						// HTTP-redirect fetch step 11
   1519 						if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
   1520 							requestOpts.method = 'GET';
   1521 							requestOpts.body = undefined;
   1522 							requestOpts.headers.delete('content-length');
   1523 						}
   1524 
   1525 						// HTTP-redirect fetch step 15
   1526 						resolve(fetch(new Request(locationURL, requestOpts)));
   1527 						finalize();
   1528 						return;
   1529 				}
   1530 			}
   1531 
   1532 			// prepare response
   1533 			res.once('end', function () {
   1534 				if (signal) signal.removeEventListener('abort', abortAndFinalize);
   1535 			});
   1536 			let body = res.pipe(new PassThrough$1());
   1537 
   1538 			const response_options = {
   1539 				url: request.url,
   1540 				status: res.statusCode,
   1541 				statusText: res.statusMessage,
   1542 				headers: headers,
   1543 				size: request.size,
   1544 				timeout: request.timeout,
   1545 				counter: request.counter
   1546 			};
   1547 
   1548 			// HTTP-network fetch step 12.1.1.3
   1549 			const codings = headers.get('Content-Encoding');
   1550 
   1551 			// HTTP-network fetch step 12.1.1.4: handle content codings
   1552 
   1553 			// in following scenarios we ignore compression support
   1554 			// 1. compression support is disabled
   1555 			// 2. HEAD request
   1556 			// 3. no Content-Encoding header
   1557 			// 4. no content response (204)
   1558 			// 5. content not modified response (304)
   1559 			if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
   1560 				response = new Response(body, response_options);
   1561 				resolve(response);
   1562 				return;
   1563 			}
   1564 
   1565 			// For Node v6+
   1566 			// Be less strict when decoding compressed responses, since sometimes
   1567 			// servers send slightly invalid responses that are still accepted
   1568 			// by common browsers.
   1569 			// Always using Z_SYNC_FLUSH is what cURL does.
   1570 			const zlibOptions = {
   1571 				flush: zlib.Z_SYNC_FLUSH,
   1572 				finishFlush: zlib.Z_SYNC_FLUSH
   1573 			};
   1574 
   1575 			// for gzip
   1576 			if (codings == 'gzip' || codings == 'x-gzip') {
   1577 				body = body.pipe(zlib.createGunzip(zlibOptions));
   1578 				response = new Response(body, response_options);
   1579 				resolve(response);
   1580 				return;
   1581 			}
   1582 
   1583 			// for deflate
   1584 			if (codings == 'deflate' || codings == 'x-deflate') {
   1585 				// handle the infamous raw deflate response from old servers
   1586 				// a hack for old IIS and Apache servers
   1587 				const raw = res.pipe(new PassThrough$1());
   1588 				raw.once('data', function (chunk) {
   1589 					// see http://stackoverflow.com/questions/37519828
   1590 					if ((chunk[0] & 0x0F) === 0x08) {
   1591 						body = body.pipe(zlib.createInflate());
   1592 					} else {
   1593 						body = body.pipe(zlib.createInflateRaw());
   1594 					}
   1595 					response = new Response(body, response_options);
   1596 					resolve(response);
   1597 				});
   1598 				return;
   1599 			}
   1600 
   1601 			// for br
   1602 			if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
   1603 				body = body.pipe(zlib.createBrotliDecompress());
   1604 				response = new Response(body, response_options);
   1605 				resolve(response);
   1606 				return;
   1607 			}
   1608 
   1609 			// otherwise, use response as-is
   1610 			response = new Response(body, response_options);
   1611 			resolve(response);
   1612 		});
   1613 
   1614 		writeToStream(req, request);
   1615 	});
   1616 }
   1617 /**
   1618  * Redirect code matching
   1619  *
   1620  * @param   Number   code  Status code
   1621  * @return  Boolean
   1622  */
   1623 fetch.isRedirect = function (code) {
   1624 	return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
   1625 };
   1626 
   1627 // expose Promise
   1628 fetch.Promise = global.Promise;
   1629 
   1630 export default fetch;
   1631 export { Headers, Request, Response, FetchError };