12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', { value: true });
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
- var Stream = _interopDefault(require('stream'));
- var http = _interopDefault(require('http'));
- var Url = _interopDefault(require('url'));
- var whatwgUrl = _interopDefault(require('whatwg-url'));
- var https = _interopDefault(require('https'));
- var zlib = _interopDefault(require('zlib'));
- const Readable = Stream.Readable;
- const BUFFER = Symbol('buffer');
- const TYPE = Symbol('type');
- class Blob {
- constructor() {
- this[TYPE] = '';
- const blobParts = arguments[0];
- const options = arguments[1];
- const buffers = [];
- let size = 0;
- if (blobParts) {
- const a = blobParts;
- const length = Number(a.length);
- for (let i = 0; i < length; i++) {
- const element = a[i];
- let buffer;
- if (element instanceof Buffer) {
- buffer = element;
- } else if (ArrayBuffer.isView(element)) {
- buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
- } else if (element instanceof ArrayBuffer) {
- buffer = Buffer.from(element);
- } else if (element instanceof Blob) {
- buffer = element[BUFFER];
- } else {
- buffer = Buffer.from(typeof element === 'string' ? element : String(element));
- }
- size += buffer.length;
- buffers.push(buffer);
- }
- }
- this[BUFFER] = Buffer.concat(buffers);
- let type = options && options.type !== undefined && String(options.type).toLowerCase();
- if (type && !/[^\u0020-\u007E]/.test(type)) {
- this[TYPE] = type;
- }
- }
- get size() {
- return this[BUFFER].length;
- }
- get type() {
- return this[TYPE];
- }
- text() {
- return Promise.resolve(this[BUFFER].toString());
- }
- arrayBuffer() {
- const buf = this[BUFFER];
- const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
- return Promise.resolve(ab);
- }
- stream() {
- const readable = new Readable();
- readable._read = function () {};
- readable.push(this[BUFFER]);
- readable.push(null);
- return readable;
- }
- toString() {
- return '[object Blob]';
- }
- slice() {
- const size = this.size;
- const start = arguments[0];
- const end = arguments[1];
- let relativeStart, relativeEnd;
- if (start === undefined) {
- relativeStart = 0;
- } else if (start < 0) {
- relativeStart = Math.max(size + start, 0);
- } else {
- relativeStart = Math.min(start, size);
- }
- if (end === undefined) {
- relativeEnd = size;
- } else if (end < 0) {
- relativeEnd = Math.max(size + end, 0);
- } else {
- relativeEnd = Math.min(end, size);
- }
- const span = Math.max(relativeEnd - relativeStart, 0);
- const buffer = this[BUFFER];
- const slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
- const blob = new Blob([], { type: arguments[2] });
- blob[BUFFER] = slicedBuffer;
- return blob;
- }
- }
- Object.defineProperties(Blob.prototype, {
- size: { enumerable: true },
- type: { enumerable: true },
- slice: { enumerable: true }
- });
- Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
- value: 'Blob',
- writable: false,
- enumerable: false,
- configurable: true
- });
- function FetchError(message, type, systemError) {
- Error.call(this, message);
- this.message = message;
- this.type = type;
-
- if (systemError) {
- this.code = this.errno = systemError.code;
- }
-
- Error.captureStackTrace(this, this.constructor);
- }
- FetchError.prototype = Object.create(Error.prototype);
- FetchError.prototype.constructor = FetchError;
- FetchError.prototype.name = 'FetchError';
- let convert;
- try {
- convert = require('encoding').convert;
- } catch (e) {}
- const INTERNALS = Symbol('Body internals');
- const PassThrough = Stream.PassThrough;
- function Body(body) {
- var _this = this;
- var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
- _ref$size = _ref.size;
- let size = _ref$size === undefined ? 0 : _ref$size;
- var _ref$timeout = _ref.timeout;
- let timeout = _ref$timeout === undefined ? 0 : _ref$timeout;
- if (body == null) {
-
- body = null;
- } else if (isURLSearchParams(body)) {
-
- body = Buffer.from(body.toString());
- } else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
-
- body = Buffer.from(body);
- } else if (ArrayBuffer.isView(body)) {
-
- body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
- } else if (body instanceof Stream) ; else {
-
-
- body = Buffer.from(String(body));
- }
- this[INTERNALS] = {
- body,
- disturbed: false,
- error: null
- };
- this.size = size;
- this.timeout = timeout;
- if (body instanceof Stream) {
- body.on('error', function (err) {
- const error = err.name === 'AbortError' ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, 'system', err);
- _this[INTERNALS].error = error;
- });
- }
- }
- Body.prototype = {
- get body() {
- return this[INTERNALS].body;
- },
- get bodyUsed() {
- return this[INTERNALS].disturbed;
- },
-
- arrayBuffer() {
- return consumeBody.call(this).then(function (buf) {
- return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
- });
- },
-
- blob() {
- let ct = this.headers && this.headers.get('content-type') || '';
- return consumeBody.call(this).then(function (buf) {
- return Object.assign(
-
- new Blob([], {
- type: ct.toLowerCase()
- }), {
- [BUFFER]: buf
- });
- });
- },
-
- json() {
- var _this2 = this;
- return consumeBody.call(this).then(function (buffer) {
- try {
- return JSON.parse(buffer.toString());
- } catch (err) {
- return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
- }
- });
- },
-
- text() {
- return consumeBody.call(this).then(function (buffer) {
- return buffer.toString();
- });
- },
-
- buffer() {
- return consumeBody.call(this);
- },
-
- textConverted() {
- var _this3 = this;
- return consumeBody.call(this).then(function (buffer) {
- return convertBody(buffer, _this3.headers);
- });
- }
- };
- Object.defineProperties(Body.prototype, {
- body: { enumerable: true },
- bodyUsed: { enumerable: true },
- arrayBuffer: { enumerable: true },
- blob: { enumerable: true },
- json: { enumerable: true },
- text: { enumerable: true }
- });
- Body.mixIn = function (proto) {
- for (const name of Object.getOwnPropertyNames(Body.prototype)) {
-
- if (!(name in proto)) {
- const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
- Object.defineProperty(proto, name, desc);
- }
- }
- };
- function consumeBody() {
- var _this4 = this;
- if (this[INTERNALS].disturbed) {
- return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
- }
- this[INTERNALS].disturbed = true;
- if (this[INTERNALS].error) {
- return Body.Promise.reject(this[INTERNALS].error);
- }
- let body = this.body;
-
- if (body === null) {
- return Body.Promise.resolve(Buffer.alloc(0));
- }
-
- if (isBlob(body)) {
- body = body.stream();
- }
-
- if (Buffer.isBuffer(body)) {
- return Body.Promise.resolve(body);
- }
-
- if (!(body instanceof Stream)) {
- return Body.Promise.resolve(Buffer.alloc(0));
- }
-
-
- let accum = [];
- let accumBytes = 0;
- let abort = false;
- return new Body.Promise(function (resolve, reject) {
- let resTimeout;
-
- if (_this4.timeout) {
- resTimeout = setTimeout(function () {
- abort = true;
- reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, 'body-timeout'));
- }, _this4.timeout);
- }
-
- body.on('error', function (err) {
- if (err.name === 'AbortError') {
-
- abort = true;
- reject(err);
- } else {
-
- reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
- }
- });
- body.on('data', function (chunk) {
- if (abort || chunk === null) {
- return;
- }
- if (_this4.size && accumBytes + chunk.length > _this4.size) {
- abort = true;
- reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, 'max-size'));
- return;
- }
- accumBytes += chunk.length;
- accum.push(chunk);
- });
- body.on('end', function () {
- if (abort) {
- return;
- }
- clearTimeout(resTimeout);
- try {
- resolve(Buffer.concat(accum, accumBytes));
- } catch (err) {
-
- reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, 'system', err));
- }
- });
- });
- }
- function convertBody(buffer, headers) {
- if (typeof convert !== 'function') {
- throw new Error('The package `encoding` must be installed to use the textConverted() function');
- }
- const ct = headers.get('content-type');
- let charset = 'utf-8';
- let res, str;
-
- if (ct) {
- res = /charset=([^;]*)/i.exec(ct);
- }
-
- str = buffer.slice(0, 1024).toString();
-
- if (!res && str) {
- res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
- }
-
- if (!res && str) {
- res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
- if (!res) {
- res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
- if (res) {
- res.pop();
- }
- }
- if (res) {
- res = /charset=(.*)/i.exec(res.pop());
- }
- }
-
- if (!res && str) {
- res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str);
- }
-
- if (res) {
- charset = res.pop();
-
-
- if (charset === 'gb2312' || charset === 'gbk') {
- charset = 'gb18030';
- }
- }
-
- return convert(buffer, 'UTF-8', charset).toString();
- }
- function isURLSearchParams(obj) {
-
- 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') {
- return false;
- }
-
- return obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function';
- }
- function isBlob(obj) {
- 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]);
- }
- function clone(instance) {
- let p1, p2;
- let body = instance.body;
-
- if (instance.bodyUsed) {
- throw new Error('cannot clone body after it is used');
- }
-
-
- if (body instanceof Stream && typeof body.getBoundary !== 'function') {
-
- p1 = new PassThrough();
- p2 = new PassThrough();
- body.pipe(p1);
- body.pipe(p2);
-
- instance[INTERNALS].body = p1;
- body = p2;
- }
- return body;
- }
- function extractContentType(body) {
- if (body === null) {
-
- return null;
- } else if (typeof body === 'string') {
-
- return 'text/plain;charset=UTF-8';
- } else if (isURLSearchParams(body)) {
-
- return 'application/x-www-form-urlencoded;charset=UTF-8';
- } else if (isBlob(body)) {
-
- return body.type || null;
- } else if (Buffer.isBuffer(body)) {
-
- return null;
- } else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
-
- return null;
- } else if (ArrayBuffer.isView(body)) {
-
- return null;
- } else if (typeof body.getBoundary === 'function') {
-
- return `multipart/form-data;boundary=${body.getBoundary()}`;
- } else if (body instanceof Stream) {
-
-
- return null;
- } else {
-
- return 'text/plain;charset=UTF-8';
- }
- }
- function getTotalBytes(instance) {
- const body = instance.body;
- if (body === null) {
-
- return 0;
- } else if (isBlob(body)) {
- return body.size;
- } else if (Buffer.isBuffer(body)) {
-
- return body.length;
- } else if (body && typeof body.getLengthSync === 'function') {
-
- if (body._lengthRetrievers && body._lengthRetrievers.length == 0 ||
- body.hasKnownLength && body.hasKnownLength()) {
-
- return body.getLengthSync();
- }
- return null;
- } else {
-
- return null;
- }
- }
- function writeToStream(dest, instance) {
- const body = instance.body;
- if (body === null) {
-
- dest.end();
- } else if (isBlob(body)) {
- body.stream().pipe(dest);
- } else if (Buffer.isBuffer(body)) {
-
- dest.write(body);
- dest.end();
- } else {
-
- body.pipe(dest);
- }
- }
- Body.Promise = global.Promise;
- const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
- const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
- function validateName(name) {
- name = `${name}`;
- if (invalidTokenRegex.test(name) || name === '') {
- throw new TypeError(`${name} is not a legal HTTP header name`);
- }
- }
- function validateValue(value) {
- value = `${value}`;
- if (invalidHeaderCharRegex.test(value)) {
- throw new TypeError(`${value} is not a legal HTTP header value`);
- }
- }
- function find(map, name) {
- name = name.toLowerCase();
- for (const key in map) {
- if (key.toLowerCase() === name) {
- return key;
- }
- }
- return undefined;
- }
- const MAP = Symbol('map');
- class Headers {
-
- constructor() {
- let init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
- this[MAP] = Object.create(null);
- if (init instanceof Headers) {
- const rawHeaders = init.raw();
- const headerNames = Object.keys(rawHeaders);
- for (const headerName of headerNames) {
- for (const value of rawHeaders[headerName]) {
- this.append(headerName, value);
- }
- }
- return;
- }
-
-
- if (init == null) ; else if (typeof init === 'object') {
- const method = init[Symbol.iterator];
- if (method != null) {
- if (typeof method !== 'function') {
- throw new TypeError('Header pairs must be iterable');
- }
-
-
- const pairs = [];
- for (const pair of init) {
- if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') {
- throw new TypeError('Each header pair must be iterable');
- }
- pairs.push(Array.from(pair));
- }
- for (const pair of pairs) {
- if (pair.length !== 2) {
- throw new TypeError('Each header pair must be a name/value tuple');
- }
- this.append(pair[0], pair[1]);
- }
- } else {
-
- for (const key of Object.keys(init)) {
- const value = init[key];
- this.append(key, value);
- }
- }
- } else {
- throw new TypeError('Provided initializer must be an object');
- }
- }
-
- get(name) {
- name = `${name}`;
- validateName(name);
- const key = find(this[MAP], name);
- if (key === undefined) {
- return null;
- }
- return this[MAP][key].join(', ');
- }
-
- forEach(callback) {
- let thisArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
- let pairs = getHeaders(this);
- let i = 0;
- while (i < pairs.length) {
- var _pairs$i = pairs[i];
- const name = _pairs$i[0],
- value = _pairs$i[1];
- callback.call(thisArg, value, name, this);
- pairs = getHeaders(this);
- i++;
- }
- }
-
- set(name, value) {
- name = `${name}`;
- value = `${value}`;
- validateName(name);
- validateValue(value);
- const key = find(this[MAP], name);
- this[MAP][key !== undefined ? key : name] = [value];
- }
-
- append(name, value) {
- name = `${name}`;
- value = `${value}`;
- validateName(name);
- validateValue(value);
- const key = find(this[MAP], name);
- if (key !== undefined) {
- this[MAP][key].push(value);
- } else {
- this[MAP][name] = [value];
- }
- }
-
- has(name) {
- name = `${name}`;
- validateName(name);
- return find(this[MAP], name) !== undefined;
- }
-
- delete(name) {
- name = `${name}`;
- validateName(name);
- const key = find(this[MAP], name);
- if (key !== undefined) {
- delete this[MAP][key];
- }
- }
-
- raw() {
- return this[MAP];
- }
-
- keys() {
- return createHeadersIterator(this, 'key');
- }
-
- values() {
- return createHeadersIterator(this, 'value');
- }
-
- [Symbol.iterator]() {
- return createHeadersIterator(this, 'key+value');
- }
- }
- Headers.prototype.entries = Headers.prototype[Symbol.iterator];
- Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
- value: 'Headers',
- writable: false,
- enumerable: false,
- configurable: true
- });
- Object.defineProperties(Headers.prototype, {
- get: { enumerable: true },
- forEach: { enumerable: true },
- set: { enumerable: true },
- append: { enumerable: true },
- has: { enumerable: true },
- delete: { enumerable: true },
- keys: { enumerable: true },
- values: { enumerable: true },
- entries: { enumerable: true }
- });
- function getHeaders(headers) {
- let kind = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'key+value';
- const keys = Object.keys(headers[MAP]).sort();
- return keys.map(kind === 'key' ? function (k) {
- return k.toLowerCase();
- } : kind === 'value' ? function (k) {
- return headers[MAP][k].join(', ');
- } : function (k) {
- return [k.toLowerCase(), headers[MAP][k].join(', ')];
- });
- }
- const INTERNAL = Symbol('internal');
- function createHeadersIterator(target, kind) {
- const iterator = Object.create(HeadersIteratorPrototype);
- iterator[INTERNAL] = {
- target,
- kind,
- index: 0
- };
- return iterator;
- }
- const HeadersIteratorPrototype = Object.setPrototypeOf({
- next() {
-
- if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
- throw new TypeError('Value of `this` is not a HeadersIterator');
- }
- var _INTERNAL = this[INTERNAL];
- const target = _INTERNAL.target,
- kind = _INTERNAL.kind,
- index = _INTERNAL.index;
- const values = getHeaders(target, kind);
- const len = values.length;
- if (index >= len) {
- return {
- value: undefined,
- done: true
- };
- }
- this[INTERNAL].index = index + 1;
- return {
- value: values[index],
- done: false
- };
- }
- }, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())));
- Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
- value: 'HeadersIterator',
- writable: false,
- enumerable: false,
- configurable: true
- });
- function exportNodeCompatibleHeaders(headers) {
- const obj = Object.assign({ __proto__: null }, headers[MAP]);
-
-
- const hostHeaderKey = find(headers[MAP], 'Host');
- if (hostHeaderKey !== undefined) {
- obj[hostHeaderKey] = obj[hostHeaderKey][0];
- }
- return obj;
- }
- function createHeadersLenient(obj) {
- const headers = new Headers();
- for (const name of Object.keys(obj)) {
- if (invalidTokenRegex.test(name)) {
- continue;
- }
- if (Array.isArray(obj[name])) {
- for (const val of obj[name]) {
- if (invalidHeaderCharRegex.test(val)) {
- continue;
- }
- if (headers[MAP][name] === undefined) {
- headers[MAP][name] = [val];
- } else {
- headers[MAP][name].push(val);
- }
- }
- } else if (!invalidHeaderCharRegex.test(obj[name])) {
- headers[MAP][name] = [obj[name]];
- }
- }
- return headers;
- }
- const INTERNALS$1 = Symbol('Response internals');
- const STATUS_CODES = http.STATUS_CODES;
- class Response {
- constructor() {
- let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
- let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- Body.call(this, body, opts);
- const status = opts.status || 200;
- const headers = new Headers(opts.headers);
- if (body != null && !headers.has('Content-Type')) {
- const contentType = extractContentType(body);
- if (contentType) {
- headers.append('Content-Type', contentType);
- }
- }
- this[INTERNALS$1] = {
- url: opts.url,
- status,
- statusText: opts.statusText || STATUS_CODES[status],
- headers,
- counter: opts.counter
- };
- }
- get url() {
- return this[INTERNALS$1].url || '';
- }
- get status() {
- return this[INTERNALS$1].status;
- }
-
- get ok() {
- return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300;
- }
- get redirected() {
- return this[INTERNALS$1].counter > 0;
- }
- get statusText() {
- return this[INTERNALS$1].statusText;
- }
- get headers() {
- return this[INTERNALS$1].headers;
- }
-
- clone() {
- return new Response(clone(this), {
- url: this.url,
- status: this.status,
- statusText: this.statusText,
- headers: this.headers,
- ok: this.ok,
- redirected: this.redirected
- });
- }
- }
- Body.mixIn(Response.prototype);
- Object.defineProperties(Response.prototype, {
- url: { enumerable: true },
- status: { enumerable: true },
- ok: { enumerable: true },
- redirected: { enumerable: true },
- statusText: { enumerable: true },
- headers: { enumerable: true },
- clone: { enumerable: true }
- });
- Object.defineProperty(Response.prototype, Symbol.toStringTag, {
- value: 'Response',
- writable: false,
- enumerable: false,
- configurable: true
- });
- const INTERNALS$2 = Symbol('Request internals');
- const URL = Url.URL || whatwgUrl.URL;
- const parse_url = Url.parse;
- const format_url = Url.format;
- function parseURL(urlStr) {
-
- if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.exec(urlStr)) {
- urlStr = new URL(urlStr).toString();
- }
-
- return parse_url(urlStr);
- }
- const streamDestructionSupported = 'destroy' in Stream.Readable.prototype;
- function isRequest(input) {
- return typeof input === 'object' && typeof input[INTERNALS$2] === 'object';
- }
- function isAbortSignal(signal) {
- const proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
- return !!(proto && proto.constructor.name === 'AbortSignal');
- }
- class Request {
- constructor(input) {
- let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- let parsedURL;
-
- if (!isRequest(input)) {
- if (input && input.href) {
-
-
-
- parsedURL = parseURL(input.href);
- } else {
-
- parsedURL = parseURL(`${input}`);
- }
- input = {};
- } else {
- parsedURL = parseURL(input.url);
- }
- let method = init.method || input.method || 'GET';
- method = method.toUpperCase();
- if ((init.body != null || isRequest(input) && input.body !== null) && (method === 'GET' || method === 'HEAD')) {
- throw new TypeError('Request with GET/HEAD method cannot have body');
- }
- let inputBody = init.body != null ? init.body : isRequest(input) && input.body !== null ? clone(input) : null;
- Body.call(this, inputBody, {
- timeout: init.timeout || input.timeout || 0,
- size: init.size || input.size || 0
- });
- const headers = new Headers(init.headers || input.headers || {});
- if (inputBody != null && !headers.has('Content-Type')) {
- const contentType = extractContentType(inputBody);
- if (contentType) {
- headers.append('Content-Type', contentType);
- }
- }
- let signal = isRequest(input) ? input.signal : null;
- if ('signal' in init) signal = init.signal;
- if (signal != null && !isAbortSignal(signal)) {
- throw new TypeError('Expected signal to be an instanceof AbortSignal');
- }
- this[INTERNALS$2] = {
- method,
- redirect: init.redirect || input.redirect || 'follow',
- headers,
- parsedURL,
- signal
- };
-
- this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20;
- this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true;
- this.counter = init.counter || input.counter || 0;
- this.agent = init.agent || input.agent;
- }
- get method() {
- return this[INTERNALS$2].method;
- }
- get url() {
- return format_url(this[INTERNALS$2].parsedURL);
- }
- get headers() {
- return this[INTERNALS$2].headers;
- }
- get redirect() {
- return this[INTERNALS$2].redirect;
- }
- get signal() {
- return this[INTERNALS$2].signal;
- }
-
- clone() {
- return new Request(this);
- }
- }
- Body.mixIn(Request.prototype);
- Object.defineProperty(Request.prototype, Symbol.toStringTag, {
- value: 'Request',
- writable: false,
- enumerable: false,
- configurable: true
- });
- Object.defineProperties(Request.prototype, {
- method: { enumerable: true },
- url: { enumerable: true },
- headers: { enumerable: true },
- redirect: { enumerable: true },
- clone: { enumerable: true },
- signal: { enumerable: true }
- });
- function getNodeRequestOptions(request) {
- const parsedURL = request[INTERNALS$2].parsedURL;
- const headers = new Headers(request[INTERNALS$2].headers);
-
- if (!headers.has('Accept')) {
- headers.set('Accept', '*/*');
- }
-
- if (!parsedURL.protocol || !parsedURL.hostname) {
- throw new TypeError('Only absolute URLs are supported');
- }
- if (!/^https?:$/.test(parsedURL.protocol)) {
- throw new TypeError('Only HTTP(S) protocols are supported');
- }
- if (request.signal && request.body instanceof Stream.Readable && !streamDestructionSupported) {
- throw new Error('Cancellation of streamed requests with AbortSignal is not supported in node < 8');
- }
-
- let contentLengthValue = null;
- if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
- contentLengthValue = '0';
- }
- if (request.body != null) {
- const totalBytes = getTotalBytes(request);
- if (typeof totalBytes === 'number') {
- contentLengthValue = String(totalBytes);
- }
- }
- if (contentLengthValue) {
- headers.set('Content-Length', contentLengthValue);
- }
-
- if (!headers.has('User-Agent')) {
- headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
- }
-
- if (request.compress && !headers.has('Accept-Encoding')) {
- headers.set('Accept-Encoding', 'gzip,deflate');
- }
- let agent = request.agent;
- if (typeof agent === 'function') {
- agent = agent(parsedURL);
- }
-
-
- return Object.assign({}, parsedURL, {
- method: request.method,
- headers: exportNodeCompatibleHeaders(headers),
- agent
- });
- }
- function AbortError(message) {
- Error.call(this, message);
- this.type = 'aborted';
- this.message = message;
-
- Error.captureStackTrace(this, this.constructor);
- }
- AbortError.prototype = Object.create(Error.prototype);
- AbortError.prototype.constructor = AbortError;
- AbortError.prototype.name = 'AbortError';
- const URL$1 = Url.URL || whatwgUrl.URL;
- const PassThrough$1 = Stream.PassThrough;
- const isDomainOrSubdomain = function isDomainOrSubdomain(destination, original) {
- const orig = new URL$1(original).hostname;
- const dest = new URL$1(destination).hostname;
- return orig === dest || orig[orig.length - dest.length - 1] === '.' && orig.endsWith(dest);
- };
- const isSameProtocol = function isSameProtocol(destination, original) {
- const orig = new URL$1(original).protocol;
- const dest = new URL$1(destination).protocol;
- return orig === dest;
- };
- function fetch(url, opts) {
-
- if (!fetch.Promise) {
- throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
- }
- Body.Promise = fetch.Promise;
-
- return new fetch.Promise(function (resolve, reject) {
-
- const request = new Request(url, opts);
- const options = getNodeRequestOptions(request);
- const send = (options.protocol === 'https:' ? https : http).request;
- const signal = request.signal;
- let response = null;
- const abort = function abort() {
- let error = new AbortError('The user aborted a request.');
- reject(error);
- if (request.body && request.body instanceof Stream.Readable) {
- destroyStream(request.body, error);
- }
- if (!response || !response.body) return;
- response.body.emit('error', error);
- };
- if (signal && signal.aborted) {
- abort();
- return;
- }
- const abortAndFinalize = function abortAndFinalize() {
- abort();
- finalize();
- };
-
- const req = send(options);
- let reqTimeout;
- if (signal) {
- signal.addEventListener('abort', abortAndFinalize);
- }
- function finalize() {
- req.abort();
- if (signal) signal.removeEventListener('abort', abortAndFinalize);
- clearTimeout(reqTimeout);
- }
- if (request.timeout) {
- req.once('socket', function (socket) {
- reqTimeout = setTimeout(function () {
- reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
- finalize();
- }, request.timeout);
- });
- }
- req.on('error', function (err) {
- reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
- if (response && response.body) {
- destroyStream(response.body, err);
- }
- finalize();
- });
- fixResponseChunkedTransferBadEnding(req, function (err) {
- if (signal && signal.aborted) {
- return;
- }
- if (response && response.body) {
- destroyStream(response.body, err);
- }
- });
-
- if (parseInt(process.version.substring(1)) < 14) {
-
-
- req.on('socket', function (s) {
- s.addListener('close', function (hadError) {
-
- const hasDataListener = s.listenerCount('data') > 0;
-
- if (response && hasDataListener && !hadError && !(signal && signal.aborted)) {
- const err = new Error('Premature close');
- err.code = 'ERR_STREAM_PREMATURE_CLOSE';
- response.body.emit('error', err);
- }
- });
- });
- }
- req.on('response', function (res) {
- clearTimeout(reqTimeout);
- const headers = createHeadersLenient(res.headers);
-
- if (fetch.isRedirect(res.statusCode)) {
-
- const location = headers.get('Location');
-
- let locationURL = null;
- try {
- locationURL = location === null ? null : new URL$1(location, request.url).toString();
- } catch (err) {
-
-
-
- if (request.redirect !== 'manual') {
- reject(new FetchError(`uri requested responds with an invalid redirect URL: ${location}`, 'invalid-redirect'));
- finalize();
- return;
- }
- }
-
- switch (request.redirect) {
- case 'error':
- reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
- finalize();
- return;
- case 'manual':
-
- if (locationURL !== null) {
-
- try {
- headers.set('Location', locationURL);
- } catch (err) {
-
- reject(err);
- }
- }
- break;
- case 'follow':
-
- if (locationURL === null) {
- break;
- }
-
- if (request.counter >= request.follow) {
- reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
- finalize();
- return;
- }
-
-
- const requestOpts = {
- headers: new Headers(request.headers),
- follow: request.follow,
- counter: request.counter + 1,
- agent: request.agent,
- compress: request.compress,
- method: request.method,
- body: request.body,
- signal: request.signal,
- timeout: request.timeout,
- size: request.size
- };
- if (!isDomainOrSubdomain(request.url, locationURL) || !isSameProtocol(request.url, locationURL)) {
- for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) {
- requestOpts.headers.delete(name);
- }
- }
-
- if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
- reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
- finalize();
- return;
- }
-
- if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
- requestOpts.method = 'GET';
- requestOpts.body = undefined;
- requestOpts.headers.delete('content-length');
- }
-
- resolve(fetch(new Request(locationURL, requestOpts)));
- finalize();
- return;
- }
- }
-
- res.once('end', function () {
- if (signal) signal.removeEventListener('abort', abortAndFinalize);
- });
- let body = res.pipe(new PassThrough$1());
- const response_options = {
- url: request.url,
- status: res.statusCode,
- statusText: res.statusMessage,
- headers: headers,
- size: request.size,
- timeout: request.timeout,
- counter: request.counter
- };
-
- const codings = headers.get('Content-Encoding');
-
-
-
-
-
-
-
- if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
- response = new Response(body, response_options);
- resolve(response);
- return;
- }
-
-
-
-
-
- const zlibOptions = {
- flush: zlib.Z_SYNC_FLUSH,
- finishFlush: zlib.Z_SYNC_FLUSH
- };
-
- if (codings == 'gzip' || codings == 'x-gzip') {
- body = body.pipe(zlib.createGunzip(zlibOptions));
- response = new Response(body, response_options);
- resolve(response);
- return;
- }
-
- if (codings == 'deflate' || codings == 'x-deflate') {
-
-
- const raw = res.pipe(new PassThrough$1());
- raw.once('data', function (chunk) {
-
- if ((chunk[0] & 0x0F) === 0x08) {
- body = body.pipe(zlib.createInflate());
- } else {
- body = body.pipe(zlib.createInflateRaw());
- }
- response = new Response(body, response_options);
- resolve(response);
- });
- raw.on('end', function () {
-
- if (!response) {
- response = new Response(body, response_options);
- resolve(response);
- }
- });
- return;
- }
-
- if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
- body = body.pipe(zlib.createBrotliDecompress());
- response = new Response(body, response_options);
- resolve(response);
- return;
- }
-
- response = new Response(body, response_options);
- resolve(response);
- });
- writeToStream(req, request);
- });
- }
- function fixResponseChunkedTransferBadEnding(request, errorCallback) {
- let socket;
- request.on('socket', function (s) {
- socket = s;
- });
- request.on('response', function (response) {
- const headers = response.headers;
- if (headers['transfer-encoding'] === 'chunked' && !headers['content-length']) {
- response.once('close', function (hadError) {
-
-
-
-
- const hasDataListener = socket && socket.listenerCount('data') > 0;
- if (hasDataListener && !hadError) {
- const err = new Error('Premature close');
- err.code = 'ERR_STREAM_PREMATURE_CLOSE';
- errorCallback(err);
- }
- });
- }
- });
- }
- function destroyStream(stream, err) {
- if (stream.destroy) {
- stream.destroy(err);
- } else {
-
- stream.emit('error', err);
- stream.end();
- }
- }
- fetch.isRedirect = function (code) {
- return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
- };
- fetch.Promise = global.Promise;
- module.exports = exports = fetch;
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.default = exports;
- exports.Headers = Headers;
- exports.Request = Request;
- exports.Response = Response;
- exports.FetchError = FetchError;
- exports.AbortError = AbortError;
|