index.mjs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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. if (!headers.has('Connection') && !agent) {
  1181. headers.set('Connection', 'close');
  1182. }
  1183. // HTTP-network fetch step 4.2
  1184. // chunked encoding is handled by Node.js
  1185. return Object.assign({}, parsedURL, {
  1186. method: request.method,
  1187. headers: exportNodeCompatibleHeaders(headers),
  1188. agent
  1189. });
  1190. }
  1191. /**
  1192. * abort-error.js
  1193. *
  1194. * AbortError interface for cancelled requests
  1195. */
  1196. /**
  1197. * Create AbortError instance
  1198. *
  1199. * @param String message Error message for human
  1200. * @return AbortError
  1201. */
  1202. function AbortError(message) {
  1203. Error.call(this, message);
  1204. this.type = 'aborted';
  1205. this.message = message;
  1206. // hide custom error implementation details from end-users
  1207. Error.captureStackTrace(this, this.constructor);
  1208. }
  1209. AbortError.prototype = Object.create(Error.prototype);
  1210. AbortError.prototype.constructor = AbortError;
  1211. AbortError.prototype.name = 'AbortError';
  1212. const URL$1 = Url.URL || whatwgUrl.URL;
  1213. // fix an issue where "PassThrough", "resolve" aren't a named export for node <10
  1214. const PassThrough$1 = Stream.PassThrough;
  1215. const isDomainOrSubdomain = function isDomainOrSubdomain(destination, original) {
  1216. const orig = new URL$1(original).hostname;
  1217. const dest = new URL$1(destination).hostname;
  1218. return orig === dest || orig[orig.length - dest.length - 1] === '.' && orig.endsWith(dest);
  1219. };
  1220. /**
  1221. * Fetch function
  1222. *
  1223. * @param Mixed url Absolute url or Request instance
  1224. * @param Object opts Fetch options
  1225. * @return Promise
  1226. */
  1227. function fetch(url, opts) {
  1228. // allow custom promise
  1229. if (!fetch.Promise) {
  1230. throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
  1231. }
  1232. Body.Promise = fetch.Promise;
  1233. // wrap http.request into fetch
  1234. return new fetch.Promise(function (resolve, reject) {
  1235. // build request object
  1236. const request = new Request(url, opts);
  1237. const options = getNodeRequestOptions(request);
  1238. const send = (options.protocol === 'https:' ? https : http).request;
  1239. const signal = request.signal;
  1240. let response = null;
  1241. const abort = function abort() {
  1242. let error = new AbortError('The user aborted a request.');
  1243. reject(error);
  1244. if (request.body && request.body instanceof Stream.Readable) {
  1245. request.body.destroy(error);
  1246. }
  1247. if (!response || !response.body) return;
  1248. response.body.emit('error', error);
  1249. };
  1250. if (signal && signal.aborted) {
  1251. abort();
  1252. return;
  1253. }
  1254. const abortAndFinalize = function abortAndFinalize() {
  1255. abort();
  1256. finalize();
  1257. };
  1258. // send request
  1259. const req = send(options);
  1260. let reqTimeout;
  1261. if (signal) {
  1262. signal.addEventListener('abort', abortAndFinalize);
  1263. }
  1264. function finalize() {
  1265. req.abort();
  1266. if (signal) signal.removeEventListener('abort', abortAndFinalize);
  1267. clearTimeout(reqTimeout);
  1268. }
  1269. if (request.timeout) {
  1270. req.once('socket', function (socket) {
  1271. reqTimeout = setTimeout(function () {
  1272. reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
  1273. finalize();
  1274. }, request.timeout);
  1275. });
  1276. }
  1277. req.on('error', function (err) {
  1278. reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
  1279. finalize();
  1280. });
  1281. req.on('response', function (res) {
  1282. clearTimeout(reqTimeout);
  1283. const headers = createHeadersLenient(res.headers);
  1284. // HTTP fetch step 5
  1285. if (fetch.isRedirect(res.statusCode)) {
  1286. // HTTP fetch step 5.2
  1287. const location = headers.get('Location');
  1288. // HTTP fetch step 5.3
  1289. let locationURL = null;
  1290. try {
  1291. locationURL = location === null ? null : new URL$1(location, request.url).toString();
  1292. } catch (err) {
  1293. // error here can only be invalid URL in Location: header
  1294. // do not throw when options.redirect == manual
  1295. // let the user extract the errorneous redirect URL
  1296. if (request.redirect !== 'manual') {
  1297. reject(new FetchError(`uri requested responds with an invalid redirect URL: ${location}`, 'invalid-redirect'));
  1298. finalize();
  1299. return;
  1300. }
  1301. }
  1302. // HTTP fetch step 5.5
  1303. switch (request.redirect) {
  1304. case 'error':
  1305. reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
  1306. finalize();
  1307. return;
  1308. case 'manual':
  1309. // node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
  1310. if (locationURL !== null) {
  1311. // handle corrupted header
  1312. try {
  1313. headers.set('Location', locationURL);
  1314. } catch (err) {
  1315. // istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
  1316. reject(err);
  1317. }
  1318. }
  1319. break;
  1320. case 'follow':
  1321. // HTTP-redirect fetch step 2
  1322. if (locationURL === null) {
  1323. break;
  1324. }
  1325. // HTTP-redirect fetch step 5
  1326. if (request.counter >= request.follow) {
  1327. reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
  1328. finalize();
  1329. return;
  1330. }
  1331. // HTTP-redirect fetch step 6 (counter increment)
  1332. // Create a new Request object.
  1333. const requestOpts = {
  1334. headers: new Headers(request.headers),
  1335. follow: request.follow,
  1336. counter: request.counter + 1,
  1337. agent: request.agent,
  1338. compress: request.compress,
  1339. method: request.method,
  1340. body: request.body,
  1341. signal: request.signal,
  1342. timeout: request.timeout,
  1343. size: request.size
  1344. };
  1345. if (!isDomainOrSubdomain(request.url, locationURL)) {
  1346. for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) {
  1347. requestOpts.headers.delete(name);
  1348. }
  1349. }
  1350. // HTTP-redirect fetch step 9
  1351. if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
  1352. reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
  1353. finalize();
  1354. return;
  1355. }
  1356. // HTTP-redirect fetch step 11
  1357. if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
  1358. requestOpts.method = 'GET';
  1359. requestOpts.body = undefined;
  1360. requestOpts.headers.delete('content-length');
  1361. }
  1362. // HTTP-redirect fetch step 15
  1363. resolve(fetch(new Request(locationURL, requestOpts)));
  1364. finalize();
  1365. return;
  1366. }
  1367. }
  1368. // prepare response
  1369. res.once('end', function () {
  1370. if (signal) signal.removeEventListener('abort', abortAndFinalize);
  1371. });
  1372. let body = res.pipe(new PassThrough$1());
  1373. const response_options = {
  1374. url: request.url,
  1375. status: res.statusCode,
  1376. statusText: res.statusMessage,
  1377. headers: headers,
  1378. size: request.size,
  1379. timeout: request.timeout,
  1380. counter: request.counter
  1381. };
  1382. // HTTP-network fetch step 12.1.1.3
  1383. const codings = headers.get('Content-Encoding');
  1384. // HTTP-network fetch step 12.1.1.4: handle content codings
  1385. // in following scenarios we ignore compression support
  1386. // 1. compression support is disabled
  1387. // 2. HEAD request
  1388. // 3. no Content-Encoding header
  1389. // 4. no content response (204)
  1390. // 5. content not modified response (304)
  1391. if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
  1392. response = new Response(body, response_options);
  1393. resolve(response);
  1394. return;
  1395. }
  1396. // For Node v6+
  1397. // Be less strict when decoding compressed responses, since sometimes
  1398. // servers send slightly invalid responses that are still accepted
  1399. // by common browsers.
  1400. // Always using Z_SYNC_FLUSH is what cURL does.
  1401. const zlibOptions = {
  1402. flush: zlib.Z_SYNC_FLUSH,
  1403. finishFlush: zlib.Z_SYNC_FLUSH
  1404. };
  1405. // for gzip
  1406. if (codings == 'gzip' || codings == 'x-gzip') {
  1407. body = body.pipe(zlib.createGunzip(zlibOptions));
  1408. response = new Response(body, response_options);
  1409. resolve(response);
  1410. return;
  1411. }
  1412. // for deflate
  1413. if (codings == 'deflate' || codings == 'x-deflate') {
  1414. // handle the infamous raw deflate response from old servers
  1415. // a hack for old IIS and Apache servers
  1416. const raw = res.pipe(new PassThrough$1());
  1417. raw.once('data', function (chunk) {
  1418. // see http://stackoverflow.com/questions/37519828
  1419. if ((chunk[0] & 0x0F) === 0x08) {
  1420. body = body.pipe(zlib.createInflate());
  1421. } else {
  1422. body = body.pipe(zlib.createInflateRaw());
  1423. }
  1424. response = new Response(body, response_options);
  1425. resolve(response);
  1426. });
  1427. return;
  1428. }
  1429. // for br
  1430. if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
  1431. body = body.pipe(zlib.createBrotliDecompress());
  1432. response = new Response(body, response_options);
  1433. resolve(response);
  1434. return;
  1435. }
  1436. // otherwise, use response as-is
  1437. response = new Response(body, response_options);
  1438. resolve(response);
  1439. });
  1440. writeToStream(req, request);
  1441. });
  1442. }
  1443. /**
  1444. * Redirect code matching
  1445. *
  1446. * @param Number code Status code
  1447. * @return Boolean
  1448. */
  1449. fetch.isRedirect = function (code) {
  1450. return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
  1451. };
  1452. // expose Promise
  1453. fetch.Promise = global.Promise;
  1454. export default fetch;
  1455. export { Headers, Request, Response, FetchError };