index.mjs 44 KB

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