1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var Stream = _interopDefault(require('stream'));
|
8 | var http = _interopDefault(require('http'));
|
9 | var Url = _interopDefault(require('url'));
|
10 | var https = _interopDefault(require('https'));
|
11 | var zlib = _interopDefault(require('zlib'));
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const Readable = Stream.Readable;
|
17 |
|
18 | const BUFFER = Symbol('buffer');
|
19 | const TYPE = Symbol('type');
|
20 |
|
21 | class Blob {
|
22 | constructor() {
|
23 | this[TYPE] = '';
|
24 |
|
25 | const blobParts = arguments[0];
|
26 | const options = arguments[1];
|
27 |
|
28 | const buffers = [];
|
29 | let size = 0;
|
30 |
|
31 | if (blobParts) {
|
32 | const a = blobParts;
|
33 | const length = Number(a.length);
|
34 | for (let i = 0; i < length; i++) {
|
35 | const element = a[i];
|
36 | let buffer;
|
37 | if (element instanceof Buffer) {
|
38 | buffer = element;
|
39 | } else if (ArrayBuffer.isView(element)) {
|
40 | buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
|
41 | } else if (element instanceof ArrayBuffer) {
|
42 | buffer = Buffer.from(element);
|
43 | } else if (element instanceof Blob) {
|
44 | buffer = element[BUFFER];
|
45 | } else {
|
46 | buffer = Buffer.from(typeof element === 'string' ? element : String(element));
|
47 | }
|
48 | size += buffer.length;
|
49 | buffers.push(buffer);
|
50 | }
|
51 | }
|
52 |
|
53 | this[BUFFER] = Buffer.concat(buffers);
|
54 |
|
55 | let type = options && options.type !== undefined && String(options.type).toLowerCase();
|
56 | if (type && !/[^\u0020-\u007E]/.test(type)) {
|
57 | this[TYPE] = type;
|
58 | }
|
59 | }
|
60 | get size() {
|
61 | return this[BUFFER].length;
|
62 | }
|
63 | get type() {
|
64 | return this[TYPE];
|
65 | }
|
66 | text() {
|
67 | return Promise.resolve(this[BUFFER].toString());
|
68 | }
|
69 | arrayBuffer() {
|
70 | const buf = this[BUFFER];
|
71 | const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
72 | return Promise.resolve(ab);
|
73 | }
|
74 | stream() {
|
75 | const readable = new Readable();
|
76 | readable._read = function () {};
|
77 | readable.push(this[BUFFER]);
|
78 | readable.push(null);
|
79 | return readable;
|
80 | }
|
81 | toString() {
|
82 | return '[object Blob]';
|
83 | }
|
84 | slice() {
|
85 | const size = this.size;
|
86 |
|
87 | const start = arguments[0];
|
88 | const end = arguments[1];
|
89 | let relativeStart, relativeEnd;
|
90 | if (start === undefined) {
|
91 | relativeStart = 0;
|
92 | } else if (start < 0) {
|
93 | relativeStart = Math.max(size + start, 0);
|
94 | } else {
|
95 | relativeStart = Math.min(start, size);
|
96 | }
|
97 | if (end === undefined) {
|
98 | relativeEnd = size;
|
99 | } else if (end < 0) {
|
100 | relativeEnd = Math.max(size + end, 0);
|
101 | } else {
|
102 | relativeEnd = Math.min(end, size);
|
103 | }
|
104 | const span = Math.max(relativeEnd - relativeStart, 0);
|
105 |
|
106 | const buffer = this[BUFFER];
|
107 | const slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
|
108 | const blob = new Blob([], { type: arguments[2] });
|
109 | blob[BUFFER] = slicedBuffer;
|
110 | return blob;
|
111 | }
|
112 | }
|
113 |
|
114 | Object.defineProperties(Blob.prototype, {
|
115 | size: { enumerable: true },
|
116 | type: { enumerable: true },
|
117 | slice: { enumerable: true }
|
118 | });
|
119 |
|
120 | Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
|
121 | value: 'Blob',
|
122 | writable: false,
|
123 | enumerable: false,
|
124 | configurable: true
|
125 | });
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function FetchError(message, type, systemError) {
|
142 | Error.call(this, message);
|
143 |
|
144 | this.message = message;
|
145 | this.type = type;
|
146 |
|
147 |
|
148 | if (systemError) {
|
149 | this.code = this.errno = systemError.code;
|
150 | }
|
151 |
|
152 |
|
153 | Error.captureStackTrace(this, this.constructor);
|
154 | }
|
155 |
|
156 | FetchError.prototype = Object.create(Error.prototype);
|
157 | FetchError.prototype.constructor = FetchError;
|
158 | FetchError.prototype.name = 'FetchError';
|
159 |
|
160 | let convert;
|
161 | try {
|
162 | convert = require('encoding').convert;
|
163 | } catch (e) {}
|
164 |
|
165 | const INTERNALS = Symbol('Body internals');
|
166 |
|
167 |
|
168 | const PassThrough = Stream.PassThrough;
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | function Body(body) {
|
180 | var _this = this;
|
181 |
|
182 | var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
183 | _ref$size = _ref.size;
|
184 |
|
185 | let size = _ref$size === undefined ? 0 : _ref$size;
|
186 | var _ref$timeout = _ref.timeout;
|
187 | let timeout = _ref$timeout === undefined ? 0 : _ref$timeout;
|
188 |
|
189 | if (body == null) {
|
190 |
|
191 | body = null;
|
192 | } else if (isURLSearchParams(body)) {
|
193 |
|
194 | body = Buffer.from(body.toString());
|
195 | } else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
196 |
|
197 | body = Buffer.from(body);
|
198 | } else if (ArrayBuffer.isView(body)) {
|
199 |
|
200 | body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
201 | } else if (body instanceof Stream) ; else {
|
202 |
|
203 |
|
204 | body = Buffer.from(String(body));
|
205 | }
|
206 | this[INTERNALS] = {
|
207 | body,
|
208 | disturbed: false,
|
209 | error: null
|
210 | };
|
211 | this.size = size;
|
212 | this.timeout = timeout;
|
213 |
|
214 | if (body instanceof Stream) {
|
215 | body.on('error', function (err) {
|
216 | const error = err.name === 'AbortError' ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, 'system', err);
|
217 | _this[INTERNALS].error = error;
|
218 | });
|
219 | }
|
220 | }
|
221 |
|
222 | Body.prototype = {
|
223 | get body() {
|
224 | return this[INTERNALS].body;
|
225 | },
|
226 |
|
227 | get bodyUsed() {
|
228 | return this[INTERNALS].disturbed;
|
229 | },
|
230 |
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 | arrayBuffer() {
|
237 | return consumeBody.call(this).then(function (buf) {
|
238 | return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
239 | });
|
240 | },
|
241 |
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 | blob() {
|
248 | let ct = this.headers && this.headers.get('content-type') || '';
|
249 | return consumeBody.call(this).then(function (buf) {
|
250 | return Object.assign(
|
251 |
|
252 | new Blob([], {
|
253 | type: ct.toLowerCase()
|
254 | }), {
|
255 | [BUFFER]: buf
|
256 | });
|
257 | });
|
258 | },
|
259 |
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 | json() {
|
266 | var _this2 = this;
|
267 |
|
268 | return consumeBody.call(this).then(function (buffer) {
|
269 | try {
|
270 | return JSON.parse(buffer.toString());
|
271 | } catch (err) {
|
272 | return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
|
273 | }
|
274 | });
|
275 | },
|
276 |
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 | text() {
|
283 | return consumeBody.call(this).then(function (buffer) {
|
284 | return buffer.toString();
|
285 | });
|
286 | },
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 | buffer() {
|
294 | return consumeBody.call(this);
|
295 | },
|
296 |
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | textConverted() {
|
304 | var _this3 = this;
|
305 |
|
306 | return consumeBody.call(this).then(function (buffer) {
|
307 | return convertBody(buffer, _this3.headers);
|
308 | });
|
309 | }
|
310 | };
|
311 |
|
312 |
|
313 | Object.defineProperties(Body.prototype, {
|
314 | body: { enumerable: true },
|
315 | bodyUsed: { enumerable: true },
|
316 | arrayBuffer: { enumerable: true },
|
317 | blob: { enumerable: true },
|
318 | json: { enumerable: true },
|
319 | text: { enumerable: true }
|
320 | });
|
321 |
|
322 | Body.mixIn = function (proto) {
|
323 | for (const name of Object.getOwnPropertyNames(Body.prototype)) {
|
324 |
|
325 | if (!(name in proto)) {
|
326 | const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
|
327 | Object.defineProperty(proto, name, desc);
|
328 | }
|
329 | }
|
330 | };
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | function consumeBody() {
|
340 | var _this4 = this;
|
341 |
|
342 | if (this[INTERNALS].disturbed) {
|
343 | return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
|
344 | }
|
345 |
|
346 | this[INTERNALS].disturbed = true;
|
347 |
|
348 | if (this[INTERNALS].error) {
|
349 | return Body.Promise.reject(this[INTERNALS].error);
|
350 | }
|
351 |
|
352 | let body = this.body;
|
353 |
|
354 |
|
355 | if (body === null) {
|
356 | return Body.Promise.resolve(Buffer.alloc(0));
|
357 | }
|
358 |
|
359 |
|
360 | if (isBlob(body)) {
|
361 | body = body.stream();
|
362 | }
|
363 |
|
364 |
|
365 | if (Buffer.isBuffer(body)) {
|
366 | return Body.Promise.resolve(body);
|
367 | }
|
368 |
|
369 |
|
370 | if (!(body instanceof Stream)) {
|
371 | return Body.Promise.resolve(Buffer.alloc(0));
|
372 | }
|
373 |
|
374 |
|
375 |
|
376 | let accum = [];
|
377 | let accumBytes = 0;
|
378 | let abort = false;
|
379 |
|
380 | return new Body.Promise(function (resolve, reject) {
|
381 | let resTimeout;
|
382 |
|
383 |
|
384 | if (_this4.timeout) {
|
385 | resTimeout = setTimeout(function () {
|
386 | abort = true;
|
387 | reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, 'body-timeout'));
|
388 | }, _this4.timeout);
|
389 | }
|
390 |
|
391 |
|
392 | body.on('error', function (err) {
|
393 | if (err.name === 'AbortError') {
|
394 |
|
395 | abort = true;
|
396 | reject(err);
|
397 | } else {
|
398 |
|
399 | reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
|
400 | }
|
401 | });
|
402 |
|
403 | body.on('data', function (chunk) {
|
404 | if (abort || chunk === null) {
|
405 | return;
|
406 | }
|
407 |
|
408 | if (_this4.size && accumBytes + chunk.length > _this4.size) {
|
409 | abort = true;
|
410 | reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, 'max-size'));
|
411 | return;
|
412 | }
|
413 |
|
414 | accumBytes += chunk.length;
|
415 | accum.push(chunk);
|
416 | });
|
417 |
|
418 | body.on('end', function () {
|
419 | if (abort) {
|
420 | return;
|
421 | }
|
422 |
|
423 | clearTimeout(resTimeout);
|
424 |
|
425 | try {
|
426 | resolve(Buffer.concat(accum, accumBytes));
|
427 | } catch (err) {
|
428 |
|
429 | reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, 'system', err));
|
430 | }
|
431 | });
|
432 | });
|
433 | }
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 | function convertBody(buffer, headers) {
|
444 | if (typeof convert !== 'function') {
|
445 | throw new Error('The package `encoding` must be installed to use the textConverted() function');
|
446 | }
|
447 |
|
448 | const ct = headers.get('content-type');
|
449 | let charset = 'utf-8';
|
450 | let res, str;
|
451 |
|
452 |
|
453 | if (ct) {
|
454 | res = /charset=([^;]*)/i.exec(ct);
|
455 | }
|
456 |
|
457 |
|
458 | str = buffer.slice(0, 1024).toString();
|
459 |
|
460 |
|
461 | if (!res && str) {
|
462 | res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
|
463 | }
|
464 |
|
465 |
|
466 | if (!res && str) {
|
467 | res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
|
468 |
|
469 | if (res) {
|
470 | res = /charset=(.*)/i.exec(res.pop());
|
471 | }
|
472 | }
|
473 |
|
474 |
|
475 | if (!res && str) {
|
476 | res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str);
|
477 | }
|
478 |
|
479 |
|
480 | if (res) {
|
481 | charset = res.pop();
|
482 |
|
483 |
|
484 |
|
485 | if (charset === 'gb2312' || charset === 'gbk') {
|
486 | charset = 'gb18030';
|
487 | }
|
488 | }
|
489 |
|
490 |
|
491 | return convert(buffer, 'UTF-8', charset).toString();
|
492 | }
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | function isURLSearchParams(obj) {
|
502 |
|
503 | 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') {
|
504 | return false;
|
505 | }
|
506 |
|
507 |
|
508 | return obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function';
|
509 | }
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 | function isBlob(obj) {
|
517 | 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]);
|
518 | }
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 | function clone(instance) {
|
527 | let p1, p2;
|
528 | let body = instance.body;
|
529 |
|
530 |
|
531 | if (instance.bodyUsed) {
|
532 | throw new Error('cannot clone body after it is used');
|
533 | }
|
534 |
|
535 |
|
536 |
|
537 | if (body instanceof Stream && typeof body.getBoundary !== 'function') {
|
538 |
|
539 | p1 = new PassThrough();
|
540 | p2 = new PassThrough();
|
541 | body.pipe(p1);
|
542 | body.pipe(p2);
|
543 |
|
544 | instance[INTERNALS].body = p1;
|
545 | body = p2;
|
546 | }
|
547 |
|
548 | return body;
|
549 | }
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 | function extractContentType(body) {
|
561 | if (body === null) {
|
562 |
|
563 | return null;
|
564 | } else if (typeof body === 'string') {
|
565 |
|
566 | return 'text/plain;charset=UTF-8';
|
567 | } else if (isURLSearchParams(body)) {
|
568 |
|
569 | return 'application/x-www-form-urlencoded;charset=UTF-8';
|
570 | } else if (isBlob(body)) {
|
571 |
|
572 | return body.type || null;
|
573 | } else if (Buffer.isBuffer(body)) {
|
574 |
|
575 | return null;
|
576 | } else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
|
577 |
|
578 | return null;
|
579 | } else if (ArrayBuffer.isView(body)) {
|
580 |
|
581 | return null;
|
582 | } else if (typeof body.getBoundary === 'function') {
|
583 |
|
584 | return `multipart/form-data;boundary=${body.getBoundary()}`;
|
585 | } else if (body instanceof Stream) {
|
586 |
|
587 |
|
588 | return null;
|
589 | } else {
|
590 |
|
591 | return 'text/plain;charset=UTF-8';
|
592 | }
|
593 | }
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 |
|
604 | function getTotalBytes(instance) {
|
605 | const body = instance.body;
|
606 |
|
607 |
|
608 | if (body === null) {
|
609 |
|
610 | return 0;
|
611 | } else if (isBlob(body)) {
|
612 | return body.size;
|
613 | } else if (Buffer.isBuffer(body)) {
|
614 |
|
615 | return body.length;
|
616 | } else if (body && typeof body.getLengthSync === 'function') {
|
617 |
|
618 | if (body._lengthRetrievers && body._lengthRetrievers.length == 0 ||
|
619 | body.hasKnownLength && body.hasKnownLength()) {
|
620 |
|
621 | return body.getLengthSync();
|
622 | }
|
623 | return null;
|
624 | } else {
|
625 |
|
626 | return null;
|
627 | }
|
628 | }
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 | function writeToStream(dest, instance) {
|
637 | const body = instance.body;
|
638 |
|
639 |
|
640 | if (body === null) {
|
641 |
|
642 | dest.end();
|
643 | } else if (isBlob(body)) {
|
644 | body.stream().pipe(dest);
|
645 | } else if (Buffer.isBuffer(body)) {
|
646 |
|
647 | dest.write(body);
|
648 | dest.end();
|
649 | } else {
|
650 |
|
651 | body.pipe(dest);
|
652 | }
|
653 | }
|
654 |
|
655 |
|
656 | Body.Promise = global.Promise;
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 | const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
|
665 | const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
|
666 |
|
667 | function validateName(name) {
|
668 | name = `${name}`;
|
669 | if (invalidTokenRegex.test(name) || name === '') {
|
670 | throw new TypeError(`${name} is not a legal HTTP header name`);
|
671 | }
|
672 | }
|
673 |
|
674 | function validateValue(value) {
|
675 | value = `${value}`;
|
676 | if (invalidHeaderCharRegex.test(value)) {
|
677 | throw new TypeError(`${value} is not a legal HTTP header value`);
|
678 | }
|
679 | }
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 | function find(map, name) {
|
690 | name = name.toLowerCase();
|
691 | for (const key in map) {
|
692 | if (key.toLowerCase() === name) {
|
693 | return key;
|
694 | }
|
695 | }
|
696 | return undefined;
|
697 | }
|
698 |
|
699 | const MAP = Symbol('map');
|
700 | class Headers {
|
701 | |
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 | constructor() {
|
708 | let init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
|
709 |
|
710 | this[MAP] = Object.create(null);
|
711 |
|
712 | if (init instanceof Headers) {
|
713 | const rawHeaders = init.raw();
|
714 | const headerNames = Object.keys(rawHeaders);
|
715 |
|
716 | for (const headerName of headerNames) {
|
717 | for (const value of rawHeaders[headerName]) {
|
718 | this.append(headerName, value);
|
719 | }
|
720 | }
|
721 |
|
722 | return;
|
723 | }
|
724 |
|
725 |
|
726 |
|
727 | if (init == null) ; else if (typeof init === 'object') {
|
728 | const method = init[Symbol.iterator];
|
729 | if (method != null) {
|
730 | if (typeof method !== 'function') {
|
731 | throw new TypeError('Header pairs must be iterable');
|
732 | }
|
733 |
|
734 |
|
735 |
|
736 | const pairs = [];
|
737 | for (const pair of init) {
|
738 | if (typeof pair !== 'object' || typeof pair[Symbol.iterator] !== 'function') {
|
739 | throw new TypeError('Each header pair must be iterable');
|
740 | }
|
741 | pairs.push(Array.from(pair));
|
742 | }
|
743 |
|
744 | for (const pair of pairs) {
|
745 | if (pair.length !== 2) {
|
746 | throw new TypeError('Each header pair must be a name/value tuple');
|
747 | }
|
748 | this.append(pair[0], pair[1]);
|
749 | }
|
750 | } else {
|
751 |
|
752 | for (const key of Object.keys(init)) {
|
753 | const value = init[key];
|
754 | this.append(key, value);
|
755 | }
|
756 | }
|
757 | } else {
|
758 | throw new TypeError('Provided initializer must be an object');
|
759 | }
|
760 | }
|
761 |
|
762 | |
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 | get(name) {
|
769 | name = `${name}`;
|
770 | validateName(name);
|
771 | const key = find(this[MAP], name);
|
772 | if (key === undefined) {
|
773 | return null;
|
774 | }
|
775 |
|
776 | return this[MAP][key].join(', ');
|
777 | }
|
778 |
|
779 | |
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | forEach(callback) {
|
787 | let thisArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
|
788 |
|
789 | let pairs = getHeaders(this);
|
790 | let i = 0;
|
791 | while (i < pairs.length) {
|
792 | var _pairs$i = pairs[i];
|
793 | const name = _pairs$i[0],
|
794 | value = _pairs$i[1];
|
795 |
|
796 | callback.call(thisArg, value, name, this);
|
797 | pairs = getHeaders(this);
|
798 | i++;
|
799 | }
|
800 | }
|
801 |
|
802 | |
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 | set(name, value) {
|
810 | name = `${name}`;
|
811 | value = `${value}`;
|
812 | validateName(name);
|
813 | validateValue(value);
|
814 | const key = find(this[MAP], name);
|
815 | this[MAP][key !== undefined ? key : name] = [value];
|
816 | }
|
817 |
|
818 | |
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 | append(name, value) {
|
826 | name = `${name}`;
|
827 | value = `${value}`;
|
828 | validateName(name);
|
829 | validateValue(value);
|
830 | const key = find(this[MAP], name);
|
831 | if (key !== undefined) {
|
832 | this[MAP][key].push(value);
|
833 | } else {
|
834 | this[MAP][name] = [value];
|
835 | }
|
836 | }
|
837 |
|
838 | |
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 | has(name) {
|
845 | name = `${name}`;
|
846 | validateName(name);
|
847 | return find(this[MAP], name) !== undefined;
|
848 | }
|
849 |
|
850 | |
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 | delete(name) {
|
857 | name = `${name}`;
|
858 | validateName(name);
|
859 | const key = find(this[MAP], name);
|
860 | if (key !== undefined) {
|
861 | delete this[MAP][key];
|
862 | }
|
863 | }
|
864 |
|
865 | |
866 |
|
867 |
|
868 |
|
869 |
|
870 | raw() {
|
871 | return this[MAP];
|
872 | }
|
873 |
|
874 | |
875 |
|
876 |
|
877 |
|
878 |
|
879 | keys() {
|
880 | return createHeadersIterator(this, 'key');
|
881 | }
|
882 |
|
883 | |
884 |
|
885 |
|
886 |
|
887 |
|
888 | values() {
|
889 | return createHeadersIterator(this, 'value');
|
890 | }
|
891 |
|
892 | |
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 | [Symbol.iterator]() {
|
900 | return createHeadersIterator(this, 'key+value');
|
901 | }
|
902 | }
|
903 | Headers.prototype.entries = Headers.prototype[Symbol.iterator];
|
904 |
|
905 | Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
|
906 | value: 'Headers',
|
907 | writable: false,
|
908 | enumerable: false,
|
909 | configurable: true
|
910 | });
|
911 |
|
912 | Object.defineProperties(Headers.prototype, {
|
913 | get: { enumerable: true },
|
914 | forEach: { enumerable: true },
|
915 | set: { enumerable: true },
|
916 | append: { enumerable: true },
|
917 | has: { enumerable: true },
|
918 | delete: { enumerable: true },
|
919 | keys: { enumerable: true },
|
920 | values: { enumerable: true },
|
921 | entries: { enumerable: true }
|
922 | });
|
923 |
|
924 | function getHeaders(headers) {
|
925 | let kind = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'key+value';
|
926 |
|
927 | const keys = Object.keys(headers[MAP]).sort();
|
928 | return keys.map(kind === 'key' ? function (k) {
|
929 | return k.toLowerCase();
|
930 | } : kind === 'value' ? function (k) {
|
931 | return headers[MAP][k].join(', ');
|
932 | } : function (k) {
|
933 | return [k.toLowerCase(), headers[MAP][k].join(', ')];
|
934 | });
|
935 | }
|
936 |
|
937 | const INTERNAL = Symbol('internal');
|
938 |
|
939 | function createHeadersIterator(target, kind) {
|
940 | const iterator = Object.create(HeadersIteratorPrototype);
|
941 | iterator[INTERNAL] = {
|
942 | target,
|
943 | kind,
|
944 | index: 0
|
945 | };
|
946 | return iterator;
|
947 | }
|
948 |
|
949 | const HeadersIteratorPrototype = Object.setPrototypeOf({
|
950 | next() {
|
951 |
|
952 | if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
|
953 | throw new TypeError('Value of `this` is not a HeadersIterator');
|
954 | }
|
955 |
|
956 | var _INTERNAL = this[INTERNAL];
|
957 | const target = _INTERNAL.target,
|
958 | kind = _INTERNAL.kind,
|
959 | index = _INTERNAL.index;
|
960 |
|
961 | const values = getHeaders(target, kind);
|
962 | const len = values.length;
|
963 | if (index >= len) {
|
964 | return {
|
965 | value: undefined,
|
966 | done: true
|
967 | };
|
968 | }
|
969 |
|
970 | this[INTERNAL].index = index + 1;
|
971 |
|
972 | return {
|
973 | value: values[index],
|
974 | done: false
|
975 | };
|
976 | }
|
977 | }, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())));
|
978 |
|
979 | Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
|
980 | value: 'HeadersIterator',
|
981 | writable: false,
|
982 | enumerable: false,
|
983 | configurable: true
|
984 | });
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 | function exportNodeCompatibleHeaders(headers) {
|
993 | const obj = Object.assign({ __proto__: null }, headers[MAP]);
|
994 |
|
995 |
|
996 |
|
997 | const hostHeaderKey = find(headers[MAP], 'Host');
|
998 | if (hostHeaderKey !== undefined) {
|
999 | obj[hostHeaderKey] = obj[hostHeaderKey][0];
|
1000 | }
|
1001 |
|
1002 | return obj;
|
1003 | }
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 | function createHeadersLenient(obj) {
|
1013 | const headers = new Headers();
|
1014 | for (const name of Object.keys(obj)) {
|
1015 | if (invalidTokenRegex.test(name)) {
|
1016 | continue;
|
1017 | }
|
1018 | if (Array.isArray(obj[name])) {
|
1019 | for (const val of obj[name]) {
|
1020 | if (invalidHeaderCharRegex.test(val)) {
|
1021 | continue;
|
1022 | }
|
1023 | if (headers[MAP][name] === undefined) {
|
1024 | headers[MAP][name] = [val];
|
1025 | } else {
|
1026 | headers[MAP][name].push(val);
|
1027 | }
|
1028 | }
|
1029 | } else if (!invalidHeaderCharRegex.test(obj[name])) {
|
1030 | headers[MAP][name] = [obj[name]];
|
1031 | }
|
1032 | }
|
1033 | return headers;
|
1034 | }
|
1035 |
|
1036 | const INTERNALS$1 = Symbol('Response internals');
|
1037 |
|
1038 |
|
1039 | const STATUS_CODES = http.STATUS_CODES;
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 | class Response {
|
1049 | constructor() {
|
1050 | let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
1051 | let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
1052 |
|
1053 | Body.call(this, body, opts);
|
1054 |
|
1055 | const status = opts.status || 200;
|
1056 | const headers = new Headers(opts.headers);
|
1057 |
|
1058 | if (body != null && !headers.has('Content-Type')) {
|
1059 | const contentType = extractContentType(body);
|
1060 | if (contentType) {
|
1061 | headers.append('Content-Type', contentType);
|
1062 | }
|
1063 | }
|
1064 |
|
1065 | this[INTERNALS$1] = {
|
1066 | url: opts.url,
|
1067 | status,
|
1068 | statusText: opts.statusText || STATUS_CODES[status],
|
1069 | headers,
|
1070 | counter: opts.counter
|
1071 | };
|
1072 | }
|
1073 |
|
1074 | get url() {
|
1075 | return this[INTERNALS$1].url || '';
|
1076 | }
|
1077 |
|
1078 | get status() {
|
1079 | return this[INTERNALS$1].status;
|
1080 | }
|
1081 |
|
1082 | |
1083 |
|
1084 |
|
1085 | get ok() {
|
1086 | return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300;
|
1087 | }
|
1088 |
|
1089 | get redirected() {
|
1090 | return this[INTERNALS$1].counter > 0;
|
1091 | }
|
1092 |
|
1093 | get statusText() {
|
1094 | return this[INTERNALS$1].statusText;
|
1095 | }
|
1096 |
|
1097 | get headers() {
|
1098 | return this[INTERNALS$1].headers;
|
1099 | }
|
1100 |
|
1101 | |
1102 |
|
1103 |
|
1104 |
|
1105 |
|
1106 | clone() {
|
1107 | return new Response(clone(this), {
|
1108 | url: this.url,
|
1109 | status: this.status,
|
1110 | statusText: this.statusText,
|
1111 | headers: this.headers,
|
1112 | ok: this.ok,
|
1113 | redirected: this.redirected
|
1114 | });
|
1115 | }
|
1116 | }
|
1117 |
|
1118 | Body.mixIn(Response.prototype);
|
1119 |
|
1120 | Object.defineProperties(Response.prototype, {
|
1121 | url: { enumerable: true },
|
1122 | status: { enumerable: true },
|
1123 | ok: { enumerable: true },
|
1124 | redirected: { enumerable: true },
|
1125 | statusText: { enumerable: true },
|
1126 | headers: { enumerable: true },
|
1127 | clone: { enumerable: true }
|
1128 | });
|
1129 |
|
1130 | Object.defineProperty(Response.prototype, Symbol.toStringTag, {
|
1131 | value: 'Response',
|
1132 | writable: false,
|
1133 | enumerable: false,
|
1134 | configurable: true
|
1135 | });
|
1136 |
|
1137 | const INTERNALS$2 = Symbol('Request internals');
|
1138 |
|
1139 |
|
1140 | const parse_url = Url.parse;
|
1141 | const format_url = Url.format;
|
1142 |
|
1143 | const streamDestructionSupported = 'destroy' in Stream.Readable.prototype;
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 | function isRequest(input) {
|
1152 | return typeof input === 'object' && typeof input[INTERNALS$2] === 'object';
|
1153 | }
|
1154 |
|
1155 | function isAbortSignal(signal) {
|
1156 | const proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
|
1157 | return !!(proto && proto.constructor.name === 'AbortSignal');
|
1158 | }
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 |
|
1166 |
|
1167 | class Request {
|
1168 | constructor(input) {
|
1169 | let init = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
1170 |
|
1171 | let parsedURL;
|
1172 |
|
1173 |
|
1174 | if (!isRequest(input)) {
|
1175 | if (input && input.href) {
|
1176 |
|
1177 |
|
1178 |
|
1179 | parsedURL = parse_url(input.href);
|
1180 | } else {
|
1181 |
|
1182 | parsedURL = parse_url(`${input}`);
|
1183 | }
|
1184 | input = {};
|
1185 | } else {
|
1186 | parsedURL = parse_url(input.url);
|
1187 | }
|
1188 |
|
1189 | let method = init.method || input.method || 'GET';
|
1190 | method = method.toUpperCase();
|
1191 |
|
1192 | if ((init.body != null || isRequest(input) && input.body !== null) && (method === 'GET' || method === 'HEAD')) {
|
1193 | throw new TypeError('Request with GET/HEAD method cannot have body');
|
1194 | }
|
1195 |
|
1196 | let inputBody = init.body != null ? init.body : isRequest(input) && input.body !== null ? clone(input) : null;
|
1197 |
|
1198 | Body.call(this, inputBody, {
|
1199 | timeout: init.timeout || input.timeout || 0,
|
1200 | size: init.size || input.size || 0
|
1201 | });
|
1202 |
|
1203 | const headers = new Headers(init.headers || input.headers || {});
|
1204 |
|
1205 | if (inputBody != null && !headers.has('Content-Type')) {
|
1206 | const contentType = extractContentType(inputBody);
|
1207 | if (contentType) {
|
1208 | headers.append('Content-Type', contentType);
|
1209 | }
|
1210 | }
|
1211 |
|
1212 | let signal = isRequest(input) ? input.signal : null;
|
1213 | if ('signal' in init) signal = init.signal;
|
1214 |
|
1215 | if (signal != null && !isAbortSignal(signal)) {
|
1216 | throw new TypeError('Expected signal to be an instanceof AbortSignal');
|
1217 | }
|
1218 |
|
1219 | this[INTERNALS$2] = {
|
1220 | method,
|
1221 | redirect: init.redirect || input.redirect || 'follow',
|
1222 | headers,
|
1223 | parsedURL,
|
1224 | signal
|
1225 | };
|
1226 |
|
1227 |
|
1228 | this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20;
|
1229 | this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true;
|
1230 | this.counter = init.counter || input.counter || 0;
|
1231 | this.agent = init.agent || input.agent;
|
1232 | }
|
1233 |
|
1234 | get method() {
|
1235 | return this[INTERNALS$2].method;
|
1236 | }
|
1237 |
|
1238 | get url() {
|
1239 | return format_url(this[INTERNALS$2].parsedURL);
|
1240 | }
|
1241 |
|
1242 | get headers() {
|
1243 | return this[INTERNALS$2].headers;
|
1244 | }
|
1245 |
|
1246 | get redirect() {
|
1247 | return this[INTERNALS$2].redirect;
|
1248 | }
|
1249 |
|
1250 | get signal() {
|
1251 | return this[INTERNALS$2].signal;
|
1252 | }
|
1253 |
|
1254 | |
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 | clone() {
|
1260 | return new Request(this);
|
1261 | }
|
1262 | }
|
1263 |
|
1264 | Body.mixIn(Request.prototype);
|
1265 |
|
1266 | Object.defineProperty(Request.prototype, Symbol.toStringTag, {
|
1267 | value: 'Request',
|
1268 | writable: false,
|
1269 | enumerable: false,
|
1270 | configurable: true
|
1271 | });
|
1272 |
|
1273 | Object.defineProperties(Request.prototype, {
|
1274 | method: { enumerable: true },
|
1275 | url: { enumerable: true },
|
1276 | headers: { enumerable: true },
|
1277 | redirect: { enumerable: true },
|
1278 | clone: { enumerable: true },
|
1279 | signal: { enumerable: true }
|
1280 | });
|
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 | function getNodeRequestOptions(request) {
|
1289 | const parsedURL = request[INTERNALS$2].parsedURL;
|
1290 | const headers = new Headers(request[INTERNALS$2].headers);
|
1291 |
|
1292 |
|
1293 | if (!headers.has('Accept')) {
|
1294 | headers.set('Accept', '*/*');
|
1295 | }
|
1296 |
|
1297 |
|
1298 | if (!parsedURL.protocol || !parsedURL.hostname) {
|
1299 | throw new TypeError('Only absolute URLs are supported');
|
1300 | }
|
1301 |
|
1302 | if (!/^https?:$/.test(parsedURL.protocol)) {
|
1303 | throw new TypeError('Only HTTP(S) protocols are supported');
|
1304 | }
|
1305 |
|
1306 | if (request.signal && request.body instanceof Stream.Readable && !streamDestructionSupported) {
|
1307 | throw new Error('Cancellation of streamed requests with AbortSignal is not supported in node < 8');
|
1308 | }
|
1309 |
|
1310 |
|
1311 | let contentLengthValue = null;
|
1312 | if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
|
1313 | contentLengthValue = '0';
|
1314 | }
|
1315 | if (request.body != null) {
|
1316 | const totalBytes = getTotalBytes(request);
|
1317 | if (typeof totalBytes === 'number') {
|
1318 | contentLengthValue = String(totalBytes);
|
1319 | }
|
1320 | }
|
1321 | if (contentLengthValue) {
|
1322 | headers.set('Content-Length', contentLengthValue);
|
1323 | }
|
1324 |
|
1325 |
|
1326 | if (!headers.has('User-Agent')) {
|
1327 | headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
|
1328 | }
|
1329 |
|
1330 |
|
1331 | if (request.compress && !headers.has('Accept-Encoding')) {
|
1332 | headers.set('Accept-Encoding', 'gzip,deflate');
|
1333 | }
|
1334 |
|
1335 | let agent = request.agent;
|
1336 | if (typeof agent === 'function') {
|
1337 | agent = agent(parsedURL);
|
1338 | }
|
1339 |
|
1340 | if (!headers.has('Connection') && !agent) {
|
1341 | headers.set('Connection', 'close');
|
1342 | }
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 | return Object.assign({}, parsedURL, {
|
1348 | method: request.method,
|
1349 | headers: exportNodeCompatibleHeaders(headers),
|
1350 | agent
|
1351 | });
|
1352 | }
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 | function AbortError(message) {
|
1367 | Error.call(this, message);
|
1368 |
|
1369 | this.type = 'aborted';
|
1370 | this.message = message;
|
1371 |
|
1372 |
|
1373 | Error.captureStackTrace(this, this.constructor);
|
1374 | }
|
1375 |
|
1376 | AbortError.prototype = Object.create(Error.prototype);
|
1377 | AbortError.prototype.constructor = AbortError;
|
1378 | AbortError.prototype.name = 'AbortError';
|
1379 |
|
1380 |
|
1381 | const PassThrough$1 = Stream.PassThrough;
|
1382 | const resolve_url = Url.resolve;
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 | function fetch(url, opts) {
|
1392 |
|
1393 |
|
1394 | if (!fetch.Promise) {
|
1395 | throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
|
1396 | }
|
1397 |
|
1398 | Body.Promise = fetch.Promise;
|
1399 |
|
1400 |
|
1401 | return new fetch.Promise(function (resolve, reject) {
|
1402 |
|
1403 | const request = new Request(url, opts);
|
1404 | const options = getNodeRequestOptions(request);
|
1405 |
|
1406 | const send = (options.protocol === 'https:' ? https : http).request;
|
1407 | const signal = request.signal;
|
1408 |
|
1409 | let response = null;
|
1410 |
|
1411 | const abort = function abort() {
|
1412 | let error = new AbortError('The user aborted a request.');
|
1413 | reject(error);
|
1414 | if (request.body && request.body instanceof Stream.Readable) {
|
1415 | request.body.destroy(error);
|
1416 | }
|
1417 | if (!response || !response.body) return;
|
1418 | response.body.emit('error', error);
|
1419 | };
|
1420 |
|
1421 | if (signal && signal.aborted) {
|
1422 | abort();
|
1423 | return;
|
1424 | }
|
1425 |
|
1426 | const abortAndFinalize = function abortAndFinalize() {
|
1427 | abort();
|
1428 | finalize();
|
1429 | };
|
1430 |
|
1431 |
|
1432 | const req = send(options);
|
1433 | let reqTimeout;
|
1434 |
|
1435 | if (signal) {
|
1436 | signal.addEventListener('abort', abortAndFinalize);
|
1437 | }
|
1438 |
|
1439 | function finalize() {
|
1440 | req.abort();
|
1441 | if (signal) signal.removeEventListener('abort', abortAndFinalize);
|
1442 | clearTimeout(reqTimeout);
|
1443 | }
|
1444 |
|
1445 | if (request.timeout) {
|
1446 | req.once('socket', function (socket) {
|
1447 | reqTimeout = setTimeout(function () {
|
1448 | reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
|
1449 | finalize();
|
1450 | }, request.timeout);
|
1451 | });
|
1452 | }
|
1453 |
|
1454 | req.on('error', function (err) {
|
1455 | reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
|
1456 | finalize();
|
1457 | });
|
1458 |
|
1459 | req.on('response', function (res) {
|
1460 | clearTimeout(reqTimeout);
|
1461 |
|
1462 | const headers = createHeadersLenient(res.headers);
|
1463 |
|
1464 |
|
1465 | if (fetch.isRedirect(res.statusCode)) {
|
1466 |
|
1467 | const location = headers.get('Location');
|
1468 |
|
1469 |
|
1470 | const locationURL = location === null ? null : resolve_url(request.url, location);
|
1471 |
|
1472 |
|
1473 | switch (request.redirect) {
|
1474 | case 'error':
|
1475 | reject(new FetchError(`redirect mode is set to error: ${request.url}`, 'no-redirect'));
|
1476 | finalize();
|
1477 | return;
|
1478 | case 'manual':
|
1479 |
|
1480 | if (locationURL !== null) {
|
1481 |
|
1482 | try {
|
1483 | headers.set('Location', locationURL);
|
1484 | } catch (err) {
|
1485 |
|
1486 | reject(err);
|
1487 | }
|
1488 | }
|
1489 | break;
|
1490 | case 'follow':
|
1491 |
|
1492 | if (locationURL === null) {
|
1493 | break;
|
1494 | }
|
1495 |
|
1496 |
|
1497 | if (request.counter >= request.follow) {
|
1498 | reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
|
1499 | finalize();
|
1500 | return;
|
1501 | }
|
1502 |
|
1503 |
|
1504 |
|
1505 | const requestOpts = {
|
1506 | headers: new Headers(request.headers),
|
1507 | follow: request.follow,
|
1508 | counter: request.counter + 1,
|
1509 | agent: request.agent,
|
1510 | compress: request.compress,
|
1511 | method: request.method,
|
1512 | body: request.body,
|
1513 | signal: request.signal,
|
1514 | timeout: request.timeout
|
1515 | };
|
1516 |
|
1517 |
|
1518 | if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
|
1519 | reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
|
1520 | finalize();
|
1521 | return;
|
1522 | }
|
1523 |
|
1524 |
|
1525 | if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
|
1526 | requestOpts.method = 'GET';
|
1527 | requestOpts.body = undefined;
|
1528 | requestOpts.headers.delete('content-length');
|
1529 | }
|
1530 |
|
1531 |
|
1532 | resolve(fetch(new Request(locationURL, requestOpts)));
|
1533 | finalize();
|
1534 | return;
|
1535 | }
|
1536 | }
|
1537 |
|
1538 |
|
1539 | res.once('end', function () {
|
1540 | if (signal) signal.removeEventListener('abort', abortAndFinalize);
|
1541 | });
|
1542 | let body = res.pipe(new PassThrough$1());
|
1543 |
|
1544 | const response_options = {
|
1545 | url: request.url,
|
1546 | status: res.statusCode,
|
1547 | statusText: res.statusMessage,
|
1548 | headers: headers,
|
1549 | size: request.size,
|
1550 | timeout: request.timeout,
|
1551 | counter: request.counter
|
1552 | };
|
1553 |
|
1554 |
|
1555 | const codings = headers.get('Content-Encoding');
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 | if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
|
1566 | response = new Response(body, response_options);
|
1567 | resolve(response);
|
1568 | return;
|
1569 | }
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 |
|
1576 | const zlibOptions = {
|
1577 | flush: zlib.Z_SYNC_FLUSH,
|
1578 | finishFlush: zlib.Z_SYNC_FLUSH
|
1579 | };
|
1580 |
|
1581 |
|
1582 | if (codings == 'gzip' || codings == 'x-gzip') {
|
1583 | body = body.pipe(zlib.createGunzip(zlibOptions));
|
1584 | response = new Response(body, response_options);
|
1585 | resolve(response);
|
1586 | return;
|
1587 | }
|
1588 |
|
1589 |
|
1590 | if (codings == 'deflate' || codings == 'x-deflate') {
|
1591 |
|
1592 |
|
1593 | const raw = res.pipe(new PassThrough$1());
|
1594 | raw.once('data', function (chunk) {
|
1595 |
|
1596 | if ((chunk[0] & 0x0F) === 0x08) {
|
1597 | body = body.pipe(zlib.createInflate());
|
1598 | } else {
|
1599 | body = body.pipe(zlib.createInflateRaw());
|
1600 | }
|
1601 | response = new Response(body, response_options);
|
1602 | resolve(response);
|
1603 | });
|
1604 | return;
|
1605 | }
|
1606 |
|
1607 |
|
1608 | if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
|
1609 | body = body.pipe(zlib.createBrotliDecompress());
|
1610 | response = new Response(body, response_options);
|
1611 | resolve(response);
|
1612 | return;
|
1613 | }
|
1614 |
|
1615 |
|
1616 | response = new Response(body, response_options);
|
1617 | resolve(response);
|
1618 | });
|
1619 |
|
1620 | writeToStream(req, request);
|
1621 | });
|
1622 | }
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 |
|
1629 | fetch.isRedirect = function (code) {
|
1630 | return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
|
1631 | };
|
1632 |
|
1633 |
|
1634 | fetch.Promise = global.Promise;
|
1635 |
|
1636 | module.exports = exports = fetch;
|
1637 | Object.defineProperty(exports, "__esModule", { value: true });
|
1638 | exports.default = exports;
|
1639 | exports.Headers = Headers;
|
1640 | exports.Request = Request;
|
1641 | exports.Response = Response;
|
1642 | exports.FetchError = FetchError;
|