@common.js/node-fetch 3.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/body.js ADDED
@@ -0,0 +1,902 @@
1
+ /**
2
+ * Body.js
3
+ *
4
+ * Body interface provides common methods for Request and Response
5
+ */ "use strict";
6
+ Object.defineProperty(exports, "__esModule", {
7
+ value: true
8
+ });
9
+ function _export(target, all) {
10
+ for(var name in all)Object.defineProperty(target, name, {
11
+ enumerable: true,
12
+ get: all[name]
13
+ });
14
+ }
15
+ _export(exports, {
16
+ default: function() {
17
+ return Body;
18
+ },
19
+ clone: function() {
20
+ return clone;
21
+ },
22
+ extractContentType: function() {
23
+ return extractContentType;
24
+ },
25
+ getTotalBytes: function() {
26
+ return getTotalBytes;
27
+ },
28
+ writeToStream: function() {
29
+ return writeToStream;
30
+ }
31
+ });
32
+ var _nodeStream = /*#__PURE__*/ _interopRequireWildcard(require("node:stream"));
33
+ var _nodeUtil = require("node:util");
34
+ var _nodeBuffer = require("node:buffer");
35
+ var _fetchBlob = /*#__PURE__*/ _interopRequireDefault(require("fetch-blob"));
36
+ var _esmMinJs = require("formdata-polyfill/esm.min.js");
37
+ var _fetchErrorJs = require("./errors/fetch-error.js");
38
+ var _baseJs = require("./errors/base.js");
39
+ var _isJs = require("./utils/is.js");
40
+ function _arrayLikeToArray(arr, len) {
41
+ if (len == null || len > arr.length) len = arr.length;
42
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
43
+ return arr2;
44
+ }
45
+ function _arrayWithHoles(arr) {
46
+ if (Array.isArray(arr)) return arr;
47
+ }
48
+ function _asyncIterator(iterable) {
49
+ var method, async, sync, retry = 2;
50
+ for("undefined" != typeof Symbol && (async = Symbol.asyncIterator, sync = Symbol.iterator); retry--;){
51
+ if (async && null != (method = iterable[async])) return method.call(iterable);
52
+ if (sync && null != (method = iterable[sync])) return new AsyncFromSyncIterator(method.call(iterable));
53
+ async = "@@asyncIterator", sync = "@@iterator";
54
+ }
55
+ throw new TypeError("Object is not async iterable");
56
+ }
57
+ function AsyncFromSyncIterator(s) {
58
+ function AsyncFromSyncIteratorContinuation(r) {
59
+ if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object."));
60
+ var done = r.done;
61
+ return Promise.resolve(r.value).then(function(value) {
62
+ return {
63
+ value: value,
64
+ done: done
65
+ };
66
+ });
67
+ }
68
+ return AsyncFromSyncIterator = function(s) {
69
+ this.s = s, this.n = s.next;
70
+ }, AsyncFromSyncIterator.prototype = {
71
+ s: null,
72
+ n: null,
73
+ next: function() {
74
+ return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments));
75
+ },
76
+ return: function(value) {
77
+ var ret = this.s.return;
78
+ return void 0 === ret ? Promise.resolve({
79
+ value: value,
80
+ done: !0
81
+ }) : AsyncFromSyncIteratorContinuation(ret.apply(this.s, arguments));
82
+ },
83
+ throw: function(value) {
84
+ var thr = this.s.return;
85
+ return void 0 === thr ? Promise.reject(value) : AsyncFromSyncIteratorContinuation(thr.apply(this.s, arguments));
86
+ }
87
+ }, new AsyncFromSyncIterator(s);
88
+ }
89
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
90
+ try {
91
+ var info = gen[key](arg);
92
+ var value = info.value;
93
+ } catch (error) {
94
+ reject(error);
95
+ return;
96
+ }
97
+ if (info.done) {
98
+ resolve(value);
99
+ } else {
100
+ Promise.resolve(value).then(_next, _throw);
101
+ }
102
+ }
103
+ function _asyncToGenerator(fn) {
104
+ return function() {
105
+ var self = this, args = arguments;
106
+ return new Promise(function(resolve, reject) {
107
+ var gen = fn.apply(self, args);
108
+ function _next(value) {
109
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
110
+ }
111
+ function _throw(err) {
112
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
113
+ }
114
+ _next(undefined);
115
+ });
116
+ };
117
+ }
118
+ function _classCallCheck(instance, Constructor) {
119
+ if (!(instance instanceof Constructor)) {
120
+ throw new TypeError("Cannot call a class as a function");
121
+ }
122
+ }
123
+ function _defineProperties(target, props) {
124
+ for(var i = 0; i < props.length; i++){
125
+ var descriptor = props[i];
126
+ descriptor.enumerable = descriptor.enumerable || false;
127
+ descriptor.configurable = true;
128
+ if ("value" in descriptor) descriptor.writable = true;
129
+ Object.defineProperty(target, descriptor.key, descriptor);
130
+ }
131
+ }
132
+ function _createClass(Constructor, protoProps, staticProps) {
133
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
134
+ if (staticProps) _defineProperties(Constructor, staticProps);
135
+ return Constructor;
136
+ }
137
+ function _instanceof(left, right) {
138
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
139
+ return !!right[Symbol.hasInstance](left);
140
+ } else {
141
+ return left instanceof right;
142
+ }
143
+ }
144
+ function _interopRequireDefault(obj) {
145
+ return obj && obj.__esModule ? obj : {
146
+ default: obj
147
+ };
148
+ }
149
+ function _getRequireWildcardCache(nodeInterop) {
150
+ if (typeof WeakMap !== "function") return null;
151
+ var cacheBabelInterop = new WeakMap();
152
+ var cacheNodeInterop = new WeakMap();
153
+ return (_getRequireWildcardCache = function(nodeInterop) {
154
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
155
+ })(nodeInterop);
156
+ }
157
+ function _interopRequireWildcard(obj, nodeInterop) {
158
+ if (!nodeInterop && obj && obj.__esModule) {
159
+ return obj;
160
+ }
161
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
162
+ return {
163
+ default: obj
164
+ };
165
+ }
166
+ var cache = _getRequireWildcardCache(nodeInterop);
167
+ if (cache && cache.has(obj)) {
168
+ return cache.get(obj);
169
+ }
170
+ var newObj = {};
171
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
172
+ for(var key in obj){
173
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
174
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
175
+ if (desc && (desc.get || desc.set)) {
176
+ Object.defineProperty(newObj, key, desc);
177
+ } else {
178
+ newObj[key] = obj[key];
179
+ }
180
+ }
181
+ }
182
+ newObj.default = obj;
183
+ if (cache) {
184
+ cache.set(obj, newObj);
185
+ }
186
+ return newObj;
187
+ }
188
+ function _iterableToArrayLimit(arr, i) {
189
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
190
+ if (_i == null) return;
191
+ var _arr = [];
192
+ var _n = true;
193
+ var _d = false;
194
+ var _s, _e;
195
+ try {
196
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
197
+ _arr.push(_s.value);
198
+ if (i && _arr.length === i) break;
199
+ }
200
+ } catch (err) {
201
+ _d = true;
202
+ _e = err;
203
+ } finally{
204
+ try {
205
+ if (!_n && _i["return"] != null) _i["return"]();
206
+ } finally{
207
+ if (_d) throw _e;
208
+ }
209
+ }
210
+ return _arr;
211
+ }
212
+ function _nonIterableRest() {
213
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
214
+ }
215
+ function _slicedToArray(arr, i) {
216
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
217
+ }
218
+ function _unsupportedIterableToArray(o, minLen) {
219
+ if (!o) return;
220
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
221
+ var n = Object.prototype.toString.call(o).slice(8, -1);
222
+ if (n === "Object" && o.constructor) n = o.constructor.name;
223
+ if (n === "Map" || n === "Set") return Array.from(n);
224
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
225
+ }
226
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
227
+ var f, y, t, g, _ = {
228
+ label: 0,
229
+ sent: function() {
230
+ if (t[0] & 1) throw t[1];
231
+ return t[1];
232
+ },
233
+ trys: [],
234
+ ops: []
235
+ };
236
+ return g = {
237
+ next: verb(0),
238
+ "throw": verb(1),
239
+ "return": verb(2)
240
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
241
+ return this;
242
+ }), g;
243
+ function verb(n) {
244
+ return function(v) {
245
+ return step([
246
+ n,
247
+ v
248
+ ]);
249
+ };
250
+ }
251
+ function step(op) {
252
+ if (f) throw new TypeError("Generator is already executing.");
253
+ while(_)try {
254
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
255
+ if (y = 0, t) op = [
256
+ op[0] & 2,
257
+ t.value
258
+ ];
259
+ switch(op[0]){
260
+ case 0:
261
+ case 1:
262
+ t = op;
263
+ break;
264
+ case 4:
265
+ _.label++;
266
+ return {
267
+ value: op[1],
268
+ done: false
269
+ };
270
+ case 5:
271
+ _.label++;
272
+ y = op[1];
273
+ op = [
274
+ 0
275
+ ];
276
+ continue;
277
+ case 7:
278
+ op = _.ops.pop();
279
+ _.trys.pop();
280
+ continue;
281
+ default:
282
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
283
+ _ = 0;
284
+ continue;
285
+ }
286
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
287
+ _.label = op[1];
288
+ break;
289
+ }
290
+ if (op[0] === 6 && _.label < t[1]) {
291
+ _.label = t[1];
292
+ t = op;
293
+ break;
294
+ }
295
+ if (t && _.label < t[2]) {
296
+ _.label = t[2];
297
+ _.ops.push(op);
298
+ break;
299
+ }
300
+ if (t[2]) _.ops.pop();
301
+ _.trys.pop();
302
+ continue;
303
+ }
304
+ op = body.call(thisArg, _);
305
+ } catch (e) {
306
+ op = [
307
+ 6,
308
+ e
309
+ ];
310
+ y = 0;
311
+ } finally{
312
+ f = t = 0;
313
+ }
314
+ if (op[0] & 5) throw op[1];
315
+ return {
316
+ value: op[0] ? op[1] : void 0,
317
+ done: true
318
+ };
319
+ }
320
+ };
321
+ var pipeline = (0, _nodeUtil.promisify)(_nodeStream.default.pipeline);
322
+ var INTERNALS = Symbol("Body internals");
323
+ var Body = /*#__PURE__*/ function() {
324
+ "use strict";
325
+ function Body(body) {
326
+ var ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, _size = ref.size, size = _size === void 0 ? 0 : _size;
327
+ var _this = this;
328
+ _classCallCheck(this, Body);
329
+ var boundary = null;
330
+ if (body === null) {
331
+ // Body is undefined or null
332
+ body = null;
333
+ } else if ((0, _isJs.isURLSearchParameters)(body)) {
334
+ // Body is a URLSearchParams
335
+ body = _nodeBuffer.Buffer.from(body.toString());
336
+ } else if ((0, _isJs.isBlob)(body)) {
337
+ // Body is blob
338
+ } else if (_nodeBuffer.Buffer.isBuffer(body)) {
339
+ // Body is Buffer
340
+ } else if (_nodeUtil.types.isAnyArrayBuffer(body)) {
341
+ // Body is ArrayBuffer
342
+ body = _nodeBuffer.Buffer.from(body);
343
+ } else if (ArrayBuffer.isView(body)) {
344
+ // Body is ArrayBufferView
345
+ body = _nodeBuffer.Buffer.from(body.buffer, body.byteOffset, body.byteLength);
346
+ } else if (_instanceof(body, _nodeStream.default)) {
347
+ // Body is stream
348
+ } else if (_instanceof(body, _esmMinJs.FormData)) {
349
+ // Body is FormData
350
+ body = (0, _esmMinJs.formDataToBlob)(body);
351
+ boundary = body.type.split("=")[1];
352
+ } else {
353
+ // None of the above
354
+ // coerce to string then buffer
355
+ body = _nodeBuffer.Buffer.from(String(body));
356
+ }
357
+ var stream = body;
358
+ if (_nodeBuffer.Buffer.isBuffer(body)) {
359
+ stream = _nodeStream.default.Readable.from(body);
360
+ } else if ((0, _isJs.isBlob)(body)) {
361
+ stream = _nodeStream.default.Readable.from(body.stream());
362
+ }
363
+ this[INTERNALS] = {
364
+ body: body,
365
+ stream: stream,
366
+ boundary: boundary,
367
+ disturbed: false,
368
+ error: null
369
+ };
370
+ this.size = size;
371
+ if (_instanceof(body, _nodeStream.default)) {
372
+ body.on("error", function(error_) {
373
+ var error = _instanceof(error_, _baseJs.FetchBaseError) ? error_ : new _fetchErrorJs.FetchError("Invalid response body while trying to fetch ".concat(_this.url, ": ").concat(error_.message), "system", error_);
374
+ _this[INTERNALS].error = error;
375
+ });
376
+ }
377
+ }
378
+ _createClass(Body, [
379
+ {
380
+ key: "body",
381
+ get: function get() {
382
+ return this[INTERNALS].stream;
383
+ }
384
+ },
385
+ {
386
+ key: "bodyUsed",
387
+ get: function get() {
388
+ return this[INTERNALS].disturbed;
389
+ }
390
+ },
391
+ {
392
+ key: "arrayBuffer",
393
+ value: /**
394
+ * Decode response as ArrayBuffer
395
+ *
396
+ * @return Promise
397
+ */ function arrayBuffer() {
398
+ var _this = this;
399
+ return _asyncToGenerator(function() {
400
+ var ref, buffer, byteOffset, byteLength;
401
+ return __generator(this, function(_state) {
402
+ switch(_state.label){
403
+ case 0:
404
+ return [
405
+ 4,
406
+ consumeBody(_this)
407
+ ];
408
+ case 1:
409
+ ref = _state.sent(), buffer = ref.buffer, byteOffset = ref.byteOffset, byteLength = ref.byteLength;
410
+ return [
411
+ 2,
412
+ buffer.slice(byteOffset, byteOffset + byteLength)
413
+ ];
414
+ }
415
+ });
416
+ })();
417
+ }
418
+ },
419
+ {
420
+ key: "formData",
421
+ value: function formData() {
422
+ var _this = this;
423
+ return _asyncToGenerator(function() {
424
+ var ct, formData, parameters, _, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, name, value, toFormData;
425
+ return __generator(this, function(_state) {
426
+ switch(_state.label){
427
+ case 0:
428
+ ct = _this.headers.get("content-type");
429
+ if (!ct.startsWith("application/x-www-form-urlencoded")) return [
430
+ 3,
431
+ 2
432
+ ];
433
+ formData = new _esmMinJs.FormData();
434
+ _ = URLSearchParams.bind;
435
+ return [
436
+ 4,
437
+ _this.text()
438
+ ];
439
+ case 1:
440
+ parameters = new (_.apply(URLSearchParams, [
441
+ void 0,
442
+ _state.sent()
443
+ ]));
444
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
445
+ try {
446
+ for(_iterator = parameters[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
447
+ _value = _slicedToArray(_step.value, 2), name = _value[0], value = _value[1];
448
+ formData.append(name, value);
449
+ }
450
+ } catch (err) {
451
+ _didIteratorError = true;
452
+ _iteratorError = err;
453
+ } finally{
454
+ try {
455
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
456
+ _iterator.return();
457
+ }
458
+ } finally{
459
+ if (_didIteratorError) {
460
+ throw _iteratorError;
461
+ }
462
+ }
463
+ }
464
+ return [
465
+ 2,
466
+ formData
467
+ ];
468
+ case 2:
469
+ return [
470
+ 4,
471
+ Promise.resolve().then(function() {
472
+ return /*#__PURE__*/ _interopRequireWildcard(require("./utils/multipart-parser.js"));
473
+ })
474
+ ];
475
+ case 3:
476
+ toFormData = _state.sent().toFormData;
477
+ return [
478
+ 2,
479
+ toFormData(_this.body, ct)
480
+ ];
481
+ }
482
+ });
483
+ })();
484
+ }
485
+ },
486
+ {
487
+ key: "blob",
488
+ value: /**
489
+ * Return raw response as Blob
490
+ *
491
+ * @return Promise
492
+ */ function blob() {
493
+ var _this = this;
494
+ return _asyncToGenerator(function() {
495
+ var ct, buf;
496
+ return __generator(this, function(_state) {
497
+ switch(_state.label){
498
+ case 0:
499
+ ct = _this.headers && _this.headers.get("content-type") || _this[INTERNALS].body && _this[INTERNALS].body.type || "";
500
+ return [
501
+ 4,
502
+ _this.arrayBuffer()
503
+ ];
504
+ case 1:
505
+ buf = _state.sent();
506
+ return [
507
+ 2,
508
+ new _fetchBlob.default([
509
+ buf
510
+ ], {
511
+ type: ct
512
+ })
513
+ ];
514
+ }
515
+ });
516
+ })();
517
+ }
518
+ },
519
+ {
520
+ key: "json",
521
+ value: /**
522
+ * Decode response as json
523
+ *
524
+ * @return Promise
525
+ */ function json() {
526
+ var _this = this;
527
+ return _asyncToGenerator(function() {
528
+ var text;
529
+ return __generator(this, function(_state) {
530
+ switch(_state.label){
531
+ case 0:
532
+ return [
533
+ 4,
534
+ _this.text()
535
+ ];
536
+ case 1:
537
+ text = _state.sent();
538
+ return [
539
+ 2,
540
+ JSON.parse(text)
541
+ ];
542
+ }
543
+ });
544
+ })();
545
+ }
546
+ },
547
+ {
548
+ key: "text",
549
+ value: /**
550
+ * Decode response as text
551
+ *
552
+ * @return Promise
553
+ */ function text() {
554
+ var _this = this;
555
+ return _asyncToGenerator(function() {
556
+ var buffer;
557
+ return __generator(this, function(_state) {
558
+ switch(_state.label){
559
+ case 0:
560
+ return [
561
+ 4,
562
+ consumeBody(_this)
563
+ ];
564
+ case 1:
565
+ buffer = _state.sent();
566
+ return [
567
+ 2,
568
+ new TextDecoder().decode(buffer)
569
+ ];
570
+ }
571
+ });
572
+ })();
573
+ }
574
+ },
575
+ {
576
+ /**
577
+ * Decode response as buffer (non-spec api)
578
+ *
579
+ * @return Promise
580
+ */ key: "buffer",
581
+ value: function buffer() {
582
+ return consumeBody(this);
583
+ }
584
+ }
585
+ ]);
586
+ return Body;
587
+ }();
588
+ Body.prototype.buffer = (0, _nodeUtil.deprecate)(Body.prototype.buffer, "Please use 'response.arrayBuffer()' instead of 'response.buffer()'", "node-fetch#buffer");
589
+ // In browsers, all properties are enumerable.
590
+ Object.defineProperties(Body.prototype, {
591
+ body: {
592
+ enumerable: true
593
+ },
594
+ bodyUsed: {
595
+ enumerable: true
596
+ },
597
+ arrayBuffer: {
598
+ enumerable: true
599
+ },
600
+ blob: {
601
+ enumerable: true
602
+ },
603
+ json: {
604
+ enumerable: true
605
+ },
606
+ text: {
607
+ enumerable: true
608
+ },
609
+ data: {
610
+ get: (0, _nodeUtil.deprecate)(function() {}, "data doesn't exist, use json(), text(), arrayBuffer(), or body instead", "https://github.com/node-fetch/node-fetch/issues/1000 (response)")
611
+ }
612
+ });
613
+ function consumeBody(data) {
614
+ return _consumeBody.apply(this, arguments);
615
+ }
616
+ function _consumeBody() {
617
+ _consumeBody = /**
618
+ * Consume and convert an entire Body to a Buffer.
619
+ *
620
+ * Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
621
+ *
622
+ * @return Promise
623
+ */ _asyncToGenerator(function(data) {
624
+ var body, accum, accumBytes, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, _$error, err, error, error_;
625
+ return __generator(this, function(_state) {
626
+ switch(_state.label){
627
+ case 0:
628
+ if (data[INTERNALS].disturbed) {
629
+ throw new TypeError("body used already for: ".concat(data.url));
630
+ }
631
+ data[INTERNALS].disturbed = true;
632
+ if (data[INTERNALS].error) {
633
+ throw data[INTERNALS].error;
634
+ }
635
+ body = data.body;
636
+ // Body is null
637
+ if (body === null) {
638
+ return [
639
+ 2,
640
+ _nodeBuffer.Buffer.alloc(0)
641
+ ];
642
+ }
643
+ /* c8 ignore next 3 */ if (!_instanceof(body, _nodeStream.default)) {
644
+ return [
645
+ 2,
646
+ _nodeBuffer.Buffer.alloc(0)
647
+ ];
648
+ }
649
+ accum = [];
650
+ accumBytes = 0;
651
+ _state.label = 1;
652
+ case 1:
653
+ _state.trys.push([
654
+ 1,
655
+ 14,
656
+ ,
657
+ 15
658
+ ]);
659
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
660
+ _state.label = 2;
661
+ case 2:
662
+ _state.trys.push([
663
+ 2,
664
+ 7,
665
+ 8,
666
+ 13
667
+ ]);
668
+ _iterator = _asyncIterator(body);
669
+ _state.label = 3;
670
+ case 3:
671
+ return [
672
+ 4,
673
+ _iterator.next()
674
+ ];
675
+ case 4:
676
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
677
+ 3,
678
+ 6
679
+ ];
680
+ _value = _step.value;
681
+ chunk = _value;
682
+ if (data.size > 0 && accumBytes + chunk.length > data.size) {
683
+ _$error = new _fetchErrorJs.FetchError("content size at ".concat(data.url, " over limit: ").concat(data.size), "max-size");
684
+ body.destroy(_$error);
685
+ throw _$error;
686
+ }
687
+ accumBytes += chunk.length;
688
+ accum.push(chunk);
689
+ _state.label = 5;
690
+ case 5:
691
+ _iteratorAbruptCompletion = false;
692
+ return [
693
+ 3,
694
+ 3
695
+ ];
696
+ case 6:
697
+ return [
698
+ 3,
699
+ 13
700
+ ];
701
+ case 7:
702
+ err = _state.sent();
703
+ _didIteratorError = true;
704
+ _iteratorError = err;
705
+ return [
706
+ 3,
707
+ 13
708
+ ];
709
+ case 8:
710
+ _state.trys.push([
711
+ 8,
712
+ ,
713
+ 11,
714
+ 12
715
+ ]);
716
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
717
+ 3,
718
+ 10
719
+ ];
720
+ return [
721
+ 4,
722
+ _iterator.return()
723
+ ];
724
+ case 9:
725
+ _state.sent();
726
+ _state.label = 10;
727
+ case 10:
728
+ return [
729
+ 3,
730
+ 12
731
+ ];
732
+ case 11:
733
+ if (_didIteratorError) {
734
+ throw _iteratorError;
735
+ }
736
+ return [
737
+ 7
738
+ ];
739
+ case 12:
740
+ return [
741
+ 7
742
+ ];
743
+ case 13:
744
+ return [
745
+ 3,
746
+ 15
747
+ ];
748
+ case 14:
749
+ error = _state.sent();
750
+ error_ = _instanceof(error, _baseJs.FetchBaseError) ? error : new _fetchErrorJs.FetchError("Invalid response body while trying to fetch ".concat(data.url, ": ").concat(error.message), "system", error);
751
+ throw error_;
752
+ case 15:
753
+ if (body.readableEnded === true || body._readableState.ended === true) {
754
+ try {
755
+ if (accum.every(function(c) {
756
+ return typeof c === "string";
757
+ })) {
758
+ return [
759
+ 2,
760
+ _nodeBuffer.Buffer.from(accum.join(""))
761
+ ];
762
+ }
763
+ return [
764
+ 2,
765
+ _nodeBuffer.Buffer.concat(accum, accumBytes)
766
+ ];
767
+ } catch (error1) {
768
+ throw new _fetchErrorJs.FetchError("Could not create Buffer from response body for ".concat(data.url, ": ").concat(error1.message), "system", error1);
769
+ }
770
+ } else {
771
+ throw new _fetchErrorJs.FetchError("Premature close of server response while trying to fetch ".concat(data.url));
772
+ }
773
+ return [
774
+ 2
775
+ ];
776
+ }
777
+ });
778
+ });
779
+ return _consumeBody.apply(this, arguments);
780
+ }
781
+ var clone = function(instance, highWaterMark) {
782
+ var p1;
783
+ var p2;
784
+ var body = instance[INTERNALS].body;
785
+ // Don't allow cloning a used body
786
+ if (instance.bodyUsed) {
787
+ throw new Error("cannot clone body after it is used");
788
+ }
789
+ // Check that body is a stream and not form-data object
790
+ // note: we can't clone the form-data object without having it as a dependency
791
+ if (_instanceof(body, _nodeStream.default) && typeof body.getBoundary !== "function") {
792
+ // Tee instance body
793
+ p1 = new _nodeStream.PassThrough({
794
+ highWaterMark: highWaterMark
795
+ });
796
+ p2 = new _nodeStream.PassThrough({
797
+ highWaterMark: highWaterMark
798
+ });
799
+ body.pipe(p1);
800
+ body.pipe(p2);
801
+ // Set instance body to teed body and return the other teed body
802
+ instance[INTERNALS].stream = p1;
803
+ body = p2;
804
+ }
805
+ return body;
806
+ };
807
+ var getNonSpecFormDataBoundary = (0, _nodeUtil.deprecate)(function(body) {
808
+ return body.getBoundary();
809
+ }, "form-data doesn't follow the spec and requires special treatment. Use alternative package", "https://github.com/node-fetch/node-fetch/issues/1167");
810
+ var extractContentType = function(body, request) {
811
+ // Body is null or undefined
812
+ if (body === null) {
813
+ return null;
814
+ }
815
+ // Body is string
816
+ if (typeof body === "string") {
817
+ return "text/plain;charset=UTF-8";
818
+ }
819
+ // Body is a URLSearchParams
820
+ if ((0, _isJs.isURLSearchParameters)(body)) {
821
+ return "application/x-www-form-urlencoded;charset=UTF-8";
822
+ }
823
+ // Body is blob
824
+ if ((0, _isJs.isBlob)(body)) {
825
+ return body.type || null;
826
+ }
827
+ // Body is a Buffer (Buffer, ArrayBuffer or ArrayBufferView)
828
+ if (_nodeBuffer.Buffer.isBuffer(body) || _nodeUtil.types.isAnyArrayBuffer(body) || ArrayBuffer.isView(body)) {
829
+ return null;
830
+ }
831
+ if (_instanceof(body, _esmMinJs.FormData)) {
832
+ return "multipart/form-data; boundary=".concat(request[INTERNALS].boundary);
833
+ }
834
+ // Detect form data input from form-data module
835
+ if (body && typeof body.getBoundary === "function") {
836
+ return "multipart/form-data;boundary=".concat(getNonSpecFormDataBoundary(body));
837
+ }
838
+ // Body is stream - can't really do much about this
839
+ if (_instanceof(body, _nodeStream.default)) {
840
+ return null;
841
+ }
842
+ // Body constructor defaults other things to string
843
+ return "text/plain;charset=UTF-8";
844
+ };
845
+ var getTotalBytes = function(request) {
846
+ var body = request[INTERNALS].body;
847
+ // Body is null or undefined
848
+ if (body === null) {
849
+ return 0;
850
+ }
851
+ // Body is Blob
852
+ if ((0, _isJs.isBlob)(body)) {
853
+ return body.size;
854
+ }
855
+ // Body is Buffer
856
+ if (_nodeBuffer.Buffer.isBuffer(body)) {
857
+ return body.length;
858
+ }
859
+ // Detect form data input from form-data module
860
+ if (body && typeof body.getLengthSync === "function") {
861
+ return body.hasKnownLength && body.hasKnownLength() ? body.getLengthSync() : null;
862
+ }
863
+ // Body is stream
864
+ return null;
865
+ };
866
+ var writeToStream = function() {
867
+ var _ref = _asyncToGenerator(function(dest, param) {
868
+ var body;
869
+ return __generator(this, function(_state) {
870
+ switch(_state.label){
871
+ case 0:
872
+ body = param.body;
873
+ if (!(body === null)) return [
874
+ 3,
875
+ 1
876
+ ];
877
+ // Body is null
878
+ dest.end();
879
+ return [
880
+ 3,
881
+ 3
882
+ ];
883
+ case 1:
884
+ // Body is stream
885
+ return [
886
+ 4,
887
+ pipeline(body, dest)
888
+ ];
889
+ case 2:
890
+ _state.sent();
891
+ _state.label = 3;
892
+ case 3:
893
+ return [
894
+ 2
895
+ ];
896
+ }
897
+ });
898
+ });
899
+ return function writeToStream(dest, _) {
900
+ return _ref.apply(this, arguments);
901
+ };
902
+ }();