@merceas/cross-fetch 3.1.9

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.
@@ -0,0 +1,532 @@
1
+ (function(self) {
2
+
3
+ var irrelevant = (function (exports) {
4
+
5
+ var support = {
6
+ searchParams: 'URLSearchParams' in self,
7
+ iterable: 'Symbol' in self && 'iterator' in Symbol,
8
+ blob:
9
+ 'FileReader' in self &&
10
+ 'Blob' in self &&
11
+ (function() {
12
+ try {
13
+ new Blob();
14
+ return true
15
+ } catch (e) {
16
+ return false
17
+ }
18
+ })(),
19
+ formData: 'FormData' in self,
20
+ arrayBuffer: 'ArrayBuffer' in self
21
+ };
22
+
23
+ function isDataView(obj) {
24
+ return obj && DataView.prototype.isPrototypeOf(obj)
25
+ }
26
+
27
+ if (support.arrayBuffer) {
28
+ var viewClasses = [
29
+ '[object Int8Array]',
30
+ '[object Uint8Array]',
31
+ '[object Uint8ClampedArray]',
32
+ '[object Int16Array]',
33
+ '[object Uint16Array]',
34
+ '[object Int32Array]',
35
+ '[object Uint32Array]',
36
+ '[object Float32Array]',
37
+ '[object Float64Array]'
38
+ ];
39
+
40
+ var isArrayBufferView =
41
+ ArrayBuffer.isView ||
42
+ function(obj) {
43
+ return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
44
+ };
45
+ }
46
+
47
+ function normalizeName(name) {
48
+ if (typeof name !== 'string') {
49
+ name = String(name);
50
+ }
51
+ if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
52
+ throw new TypeError('Invalid character in header field name')
53
+ }
54
+ return name.toLowerCase()
55
+ }
56
+
57
+ function normalizeValue(value) {
58
+ if (typeof value !== 'string') {
59
+ value = String(value);
60
+ }
61
+ return value
62
+ }
63
+
64
+ // Build a destructive iterator for the value list
65
+ function iteratorFor(items) {
66
+ var iterator = {
67
+ next: function() {
68
+ var value = items.shift();
69
+ return {done: value === undefined, value: value}
70
+ }
71
+ };
72
+
73
+ if (support.iterable) {
74
+ iterator[Symbol.iterator] = function() {
75
+ return iterator
76
+ };
77
+ }
78
+
79
+ return iterator
80
+ }
81
+
82
+ function Headers(headers) {
83
+ this.map = {};
84
+
85
+ if (headers instanceof Headers) {
86
+ headers.forEach(function(value, name) {
87
+ this.append(name, value);
88
+ }, this);
89
+ } else if (Array.isArray(headers)) {
90
+ headers.forEach(function(header) {
91
+ this.append(header[0], header[1]);
92
+ }, this);
93
+ } else if (headers) {
94
+ Object.getOwnPropertyNames(headers).forEach(function(name) {
95
+ this.append(name, headers[name]);
96
+ }, this);
97
+ }
98
+ }
99
+
100
+ Headers.prototype.append = function(name, value) {
101
+ name = normalizeName(name);
102
+ value = normalizeValue(value);
103
+ var oldValue = this.map[name];
104
+ this.map[name] = oldValue ? oldValue + ', ' + value : value;
105
+ };
106
+
107
+ Headers.prototype['delete'] = function(name) {
108
+ delete this.map[normalizeName(name)];
109
+ };
110
+
111
+ Headers.prototype.get = function(name) {
112
+ name = normalizeName(name);
113
+ return this.has(name) ? this.map[name] : null
114
+ };
115
+
116
+ Headers.prototype.has = function(name) {
117
+ return this.map.hasOwnProperty(normalizeName(name))
118
+ };
119
+
120
+ Headers.prototype.set = function(name, value) {
121
+ this.map[normalizeName(name)] = normalizeValue(value);
122
+ };
123
+
124
+ Headers.prototype.forEach = function(callback, thisArg) {
125
+ for (var name in this.map) {
126
+ if (this.map.hasOwnProperty(name)) {
127
+ callback.call(thisArg, this.map[name], name, this);
128
+ }
129
+ }
130
+ };
131
+
132
+ Headers.prototype.keys = function() {
133
+ var items = [];
134
+ this.forEach(function(value, name) {
135
+ items.push(name);
136
+ });
137
+ return iteratorFor(items)
138
+ };
139
+
140
+ Headers.prototype.values = function() {
141
+ var items = [];
142
+ this.forEach(function(value) {
143
+ items.push(value);
144
+ });
145
+ return iteratorFor(items)
146
+ };
147
+
148
+ Headers.prototype.entries = function() {
149
+ var items = [];
150
+ this.forEach(function(value, name) {
151
+ items.push([name, value]);
152
+ });
153
+ return iteratorFor(items)
154
+ };
155
+
156
+ if (support.iterable) {
157
+ Headers.prototype[Symbol.iterator] = Headers.prototype.entries;
158
+ }
159
+
160
+ function consumed(body) {
161
+ if (body.bodyUsed) {
162
+ return Promise.reject(new TypeError('Already read'))
163
+ }
164
+ body.bodyUsed = true;
165
+ }
166
+
167
+ function fileReaderReady(reader) {
168
+ return new Promise(function(resolve, reject) {
169
+ reader.onload = function() {
170
+ resolve(reader.result);
171
+ };
172
+ reader.onerror = function() {
173
+ reject(reader.error);
174
+ };
175
+ })
176
+ }
177
+
178
+ function readBlobAsArrayBuffer(blob) {
179
+ var reader = new FileReader();
180
+ var promise = fileReaderReady(reader);
181
+ reader.readAsArrayBuffer(blob);
182
+ return promise
183
+ }
184
+
185
+ function readBlobAsText(blob) {
186
+ var reader = new FileReader();
187
+ var promise = fileReaderReady(reader);
188
+ reader.readAsText(blob);
189
+ return promise
190
+ }
191
+
192
+ function readArrayBufferAsText(buf) {
193
+ var view = new Uint8Array(buf);
194
+ var chars = new Array(view.length);
195
+
196
+ for (var i = 0; i < view.length; i++) {
197
+ chars[i] = String.fromCharCode(view[i]);
198
+ }
199
+ return chars.join('')
200
+ }
201
+
202
+ function bufferClone(buf) {
203
+ if (buf.slice) {
204
+ return buf.slice(0)
205
+ } else {
206
+ var view = new Uint8Array(buf.byteLength);
207
+ view.set(new Uint8Array(buf));
208
+ return view.buffer
209
+ }
210
+ }
211
+
212
+ function Body() {
213
+ this.bodyUsed = false;
214
+
215
+ this._initBody = function(body) {
216
+ this._bodyInit = body;
217
+ if (!body) {
218
+ this._bodyText = '';
219
+ } else if (typeof body === 'string') {
220
+ this._bodyText = body;
221
+ } else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
222
+ this._bodyBlob = body;
223
+ } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
224
+ this._bodyFormData = body;
225
+ } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
226
+ this._bodyText = body.toString();
227
+ } else if (support.arrayBuffer && support.blob && isDataView(body)) {
228
+ this._bodyArrayBuffer = bufferClone(body.buffer);
229
+ // IE 10-11 can't handle a DataView body.
230
+ this._bodyInit = new Blob([this._bodyArrayBuffer]);
231
+ } else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
232
+ this._bodyArrayBuffer = bufferClone(body);
233
+ } else {
234
+ this._bodyText = body = Object.prototype.toString.call(body);
235
+ }
236
+
237
+ if (!this.headers.get('content-type')) {
238
+ if (typeof body === 'string') {
239
+ this.headers.set('content-type', 'text/plain;charset=UTF-8');
240
+ } else if (this._bodyBlob && this._bodyBlob.type) {
241
+ this.headers.set('content-type', this._bodyBlob.type);
242
+ } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
243
+ this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
244
+ }
245
+ }
246
+ };
247
+
248
+ if (support.blob) {
249
+ this.blob = function() {
250
+ var rejected = consumed(this);
251
+ if (rejected) {
252
+ return rejected
253
+ }
254
+
255
+ if (this._bodyBlob) {
256
+ return Promise.resolve(this._bodyBlob)
257
+ } else if (this._bodyArrayBuffer) {
258
+ return Promise.resolve(new Blob([this._bodyArrayBuffer]))
259
+ } else if (this._bodyFormData) {
260
+ throw new Error('could not read FormData body as blob')
261
+ } else {
262
+ return Promise.resolve(new Blob([this._bodyText]))
263
+ }
264
+ };
265
+
266
+ this.arrayBuffer = function() {
267
+ if (this._bodyArrayBuffer) {
268
+ return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
269
+ } else {
270
+ return this.blob().then(readBlobAsArrayBuffer)
271
+ }
272
+ };
273
+ }
274
+
275
+ this.text = function() {
276
+ var rejected = consumed(this);
277
+ if (rejected) {
278
+ return rejected
279
+ }
280
+
281
+ if (this._bodyBlob) {
282
+ return readBlobAsText(this._bodyBlob)
283
+ } else if (this._bodyArrayBuffer) {
284
+ return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer))
285
+ } else if (this._bodyFormData) {
286
+ throw new Error('could not read FormData body as text')
287
+ } else {
288
+ return Promise.resolve(this._bodyText)
289
+ }
290
+ };
291
+
292
+ if (support.formData) {
293
+ this.formData = function() {
294
+ return this.text().then(decode)
295
+ };
296
+ }
297
+
298
+ this.json = function() {
299
+ return this.text().then(JSON.parse)
300
+ };
301
+
302
+ return this
303
+ }
304
+
305
+ // HTTP methods whose capitalization should be normalized
306
+ var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'];
307
+
308
+ function normalizeMethod(method) {
309
+ var upcased = method.toUpperCase();
310
+ return methods.indexOf(upcased) > -1 ? upcased : method
311
+ }
312
+
313
+ function Request(input, options) {
314
+ options = options || {};
315
+ var body = options.body;
316
+
317
+ if (input instanceof Request) {
318
+ if (input.bodyUsed) {
319
+ throw new TypeError('Already read')
320
+ }
321
+ this.url = input.url;
322
+ this.credentials = input.credentials;
323
+ if (!options.headers) {
324
+ this.headers = new Headers(input.headers);
325
+ }
326
+ this.method = input.method;
327
+ this.mode = input.mode;
328
+ this.signal = input.signal;
329
+ if (!body && input._bodyInit != null) {
330
+ body = input._bodyInit;
331
+ input.bodyUsed = true;
332
+ }
333
+ } else {
334
+ this.url = String(input);
335
+ }
336
+
337
+ this.credentials = options.credentials || this.credentials || 'same-origin';
338
+ if (options.headers || !this.headers) {
339
+ this.headers = new Headers(options.headers);
340
+ }
341
+ this.method = normalizeMethod(options.method || this.method || 'GET');
342
+ this.mode = options.mode || this.mode || null;
343
+ this.signal = options.signal || this.signal;
344
+ this.referrer = null;
345
+
346
+ if ((this.method === 'GET' || this.method === 'HEAD') && body) {
347
+ throw new TypeError('Body not allowed for GET or HEAD requests')
348
+ }
349
+ this._initBody(body);
350
+ }
351
+
352
+ Request.prototype.clone = function() {
353
+ return new Request(this, {body: this._bodyInit})
354
+ };
355
+
356
+ function decode(body) {
357
+ var form = new FormData();
358
+ body
359
+ .trim()
360
+ .split('&')
361
+ .forEach(function(bytes) {
362
+ if (bytes) {
363
+ var split = bytes.split('=');
364
+ var name = split.shift().replace(/\+/g, ' ');
365
+ var value = split.join('=').replace(/\+/g, ' ');
366
+ form.append(decodeURIComponent(name), decodeURIComponent(value));
367
+ }
368
+ });
369
+ return form
370
+ }
371
+
372
+ function parseHeaders(rawHeaders) {
373
+ var headers = new Headers();
374
+ // Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space
375
+ // https://tools.ietf.org/html/rfc7230#section-3.2
376
+ var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
377
+ preProcessedHeaders.split(/\r?\n/).forEach(function(line) {
378
+ var parts = line.split(':');
379
+ var key = parts.shift().trim();
380
+ if (key) {
381
+ var value = parts.join(':').trim();
382
+ headers.append(key, value);
383
+ }
384
+ });
385
+ return headers
386
+ }
387
+
388
+ Body.call(Request.prototype);
389
+
390
+ function Response(bodyInit, options) {
391
+ if (!options) {
392
+ options = {};
393
+ }
394
+
395
+ this.type = 'default';
396
+ this.status = options.status === undefined ? 200 : options.status;
397
+ this.ok = this.status >= 200 && this.status < 300;
398
+ this.statusText = 'statusText' in options ? options.statusText : 'OK';
399
+ this.headers = new Headers(options.headers);
400
+ this.url = options.url || '';
401
+ this._initBody(bodyInit);
402
+ }
403
+
404
+ Body.call(Response.prototype);
405
+
406
+ Response.prototype.clone = function() {
407
+ return new Response(this._bodyInit, {
408
+ status: this.status,
409
+ statusText: this.statusText,
410
+ headers: new Headers(this.headers),
411
+ url: this.url
412
+ })
413
+ };
414
+
415
+ Response.error = function() {
416
+ var response = new Response(null, {status: 0, statusText: ''});
417
+ response.type = 'error';
418
+ return response
419
+ };
420
+
421
+ var redirectStatuses = [301, 302, 303, 307, 308];
422
+
423
+ Response.redirect = function(url, status) {
424
+ if (redirectStatuses.indexOf(status) === -1) {
425
+ throw new RangeError('Invalid status code')
426
+ }
427
+
428
+ return new Response(null, {status: status, headers: {location: url}})
429
+ };
430
+
431
+ exports.DOMException = self.DOMException;
432
+ try {
433
+ new exports.DOMException();
434
+ } catch (err) {
435
+ exports.DOMException = function(message, name) {
436
+ this.message = message;
437
+ this.name = name;
438
+ var error = Error(message);
439
+ this.stack = error.stack;
440
+ };
441
+ exports.DOMException.prototype = Object.create(Error.prototype);
442
+ exports.DOMException.prototype.constructor = exports.DOMException;
443
+ }
444
+
445
+ function fetch(input, init) {
446
+ return new Promise(function(resolve, reject) {
447
+ var request = new Request(input, init);
448
+
449
+ if (request.signal && request.signal.aborted) {
450
+ return reject(new exports.DOMException('Aborted', 'AbortError'))
451
+ }
452
+
453
+ var xhr = new XMLHttpRequest();
454
+
455
+ function abortXhr() {
456
+ xhr.abort();
457
+ }
458
+
459
+ xhr.onload = function() {
460
+ var options = {
461
+ status: xhr.status,
462
+ statusText: xhr.statusText,
463
+ headers: parseHeaders(xhr.getAllResponseHeaders() || '')
464
+ };
465
+ options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
466
+ var body = 'response' in xhr ? xhr.response : xhr.responseText;
467
+ resolve(new Response(body, options));
468
+ };
469
+
470
+ xhr.onerror = function() {
471
+ reject(new TypeError('Network request failed'));
472
+ };
473
+
474
+ xhr.ontimeout = function() {
475
+ reject(new TypeError('Network request failed'));
476
+ };
477
+
478
+ xhr.onabort = function() {
479
+ reject(new exports.DOMException('Aborted', 'AbortError'));
480
+ };
481
+
482
+ xhr.open(request.method, request.url, true);
483
+
484
+ if (request.credentials === 'include') {
485
+ xhr.withCredentials = true;
486
+ } else if (request.credentials === 'omit') {
487
+ xhr.withCredentials = false;
488
+ }
489
+
490
+ if ('responseType' in xhr && support.blob) {
491
+ xhr.responseType = 'blob';
492
+ }
493
+
494
+ request.headers.forEach(function(value, name) {
495
+ xhr.setRequestHeader(name, value);
496
+ });
497
+
498
+ if (request.signal) {
499
+ request.signal.addEventListener('abort', abortXhr);
500
+
501
+ xhr.onreadystatechange = function() {
502
+ // DONE (success or failure)
503
+ if (xhr.readyState === 4) {
504
+ request.signal.removeEventListener('abort', abortXhr);
505
+ }
506
+ };
507
+ }
508
+
509
+ xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit);
510
+ })
511
+ }
512
+
513
+ fetch.polyfill = true;
514
+
515
+ if (!self.fetch) {
516
+ self.fetch = fetch;
517
+ self.Headers = Headers;
518
+ self.Request = Request;
519
+ self.Response = Response;
520
+ }
521
+
522
+ exports.Headers = Headers;
523
+ exports.Request = Request;
524
+ exports.Response = Response;
525
+ exports.fetch = fetch;
526
+
527
+ Object.defineProperty(exports, '__esModule', { value: true });
528
+
529
+ return exports;
530
+
531
+ })({});
532
+ })(typeof self !== 'undefined' ? self : this);