You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.es.js 44KB

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