index.mjs 40 KB

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