wp/wp-includes/js/dist/vendor/wp-polyfill-fetch.js
changeset 19 3d72ae0968f4
parent 9 177826044cd9
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     2   typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
     2   typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
     3   typeof define === 'function' && define.amd ? define(['exports'], factory) :
     3   typeof define === 'function' && define.amd ? define(['exports'], factory) :
     4   (factory((global.WHATWGFetch = {})));
     4   (factory((global.WHATWGFetch = {})));
     5 }(this, (function (exports) { 'use strict';
     5 }(this, (function (exports) { 'use strict';
     6 
     6 
       
     7   var global =
       
     8     (typeof globalThis !== 'undefined' && globalThis) ||
       
     9     (typeof self !== 'undefined' && self) ||
       
    10     (typeof global !== 'undefined' && global);
       
    11 
     7   var support = {
    12   var support = {
     8     searchParams: 'URLSearchParams' in self,
    13     searchParams: 'URLSearchParams' in global,
     9     iterable: 'Symbol' in self && 'iterator' in Symbol,
    14     iterable: 'Symbol' in global && 'iterator' in Symbol,
    10     blob:
    15     blob:
    11       'FileReader' in self &&
    16       'FileReader' in global &&
    12       'Blob' in self &&
    17       'Blob' in global &&
    13       (function() {
    18       (function() {
    14         try {
    19         try {
    15           new Blob();
    20           new Blob();
    16           return true
    21           return true
    17         } catch (e) {
    22         } catch (e) {
    18           return false
    23           return false
    19         }
    24         }
    20       })(),
    25       })(),
    21     formData: 'FormData' in self,
    26     formData: 'FormData' in global,
    22     arrayBuffer: 'ArrayBuffer' in self
    27     arrayBuffer: 'ArrayBuffer' in global
    23   };
    28   };
    24 
    29 
    25   function isDataView(obj) {
    30   function isDataView(obj) {
    26     return obj && DataView.prototype.isPrototypeOf(obj)
    31     return obj && DataView.prototype.isPrototypeOf(obj)
    27   }
    32   }
    48 
    53 
    49   function normalizeName(name) {
    54   function normalizeName(name) {
    50     if (typeof name !== 'string') {
    55     if (typeof name !== 'string') {
    51       name = String(name);
    56       name = String(name);
    52     }
    57     }
    53     if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
    58     if (/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(name) || name === '') {
    54       throw new TypeError('Invalid character in header field name')
    59       throw new TypeError('Invalid character in header field name: "' + name + '"')
    55     }
    60     }
    56     return name.toLowerCase()
    61     return name.toLowerCase()
    57   }
    62   }
    58 
    63 
    59   function normalizeValue(value) {
    64   function normalizeValue(value) {
   213 
   218 
   214   function Body() {
   219   function Body() {
   215     this.bodyUsed = false;
   220     this.bodyUsed = false;
   216 
   221 
   217     this._initBody = function(body) {
   222     this._initBody = function(body) {
       
   223       /*
       
   224         fetch-mock wraps the Response object in an ES6 Proxy to
       
   225         provide useful test harness features such as flush. However, on
       
   226         ES5 browsers without fetch or Proxy support pollyfills must be used;
       
   227         the proxy-pollyfill is unable to proxy an attribute unless it exists
       
   228         on the object before the Proxy is created. This change ensures
       
   229         Response.bodyUsed exists on the instance, while maintaining the
       
   230         semantic of setting Request.bodyUsed in the constructor before
       
   231         _initBody is called.
       
   232       */
       
   233       this.bodyUsed = this.bodyUsed;
   218       this._bodyInit = body;
   234       this._bodyInit = body;
   219       if (!body) {
   235       if (!body) {
   220         this._bodyText = '';
   236         this._bodyText = '';
   221       } else if (typeof body === 'string') {
   237       } else if (typeof body === 'string') {
   222         this._bodyText = body;
   238         this._bodyText = body;
   265         }
   281         }
   266       };
   282       };
   267 
   283 
   268       this.arrayBuffer = function() {
   284       this.arrayBuffer = function() {
   269         if (this._bodyArrayBuffer) {
   285         if (this._bodyArrayBuffer) {
   270           return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
   286           var isConsumed = consumed(this);
       
   287           if (isConsumed) {
       
   288             return isConsumed
       
   289           }
       
   290           if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
       
   291             return Promise.resolve(
       
   292               this._bodyArrayBuffer.buffer.slice(
       
   293                 this._bodyArrayBuffer.byteOffset,
       
   294                 this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength
       
   295               )
       
   296             )
       
   297           } else {
       
   298             return Promise.resolve(this._bodyArrayBuffer)
       
   299           }
   271         } else {
   300         } else {
   272           return this.blob().then(readBlobAsArrayBuffer)
   301           return this.blob().then(readBlobAsArrayBuffer)
   273         }
   302         }
   274       };
   303       };
   275     }
   304     }
   311     var upcased = method.toUpperCase();
   340     var upcased = method.toUpperCase();
   312     return methods.indexOf(upcased) > -1 ? upcased : method
   341     return methods.indexOf(upcased) > -1 ? upcased : method
   313   }
   342   }
   314 
   343 
   315   function Request(input, options) {
   344   function Request(input, options) {
       
   345     if (!(this instanceof Request)) {
       
   346       throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
       
   347     }
       
   348 
   316     options = options || {};
   349     options = options || {};
   317     var body = options.body;
   350     var body = options.body;
   318 
   351 
   319     if (input instanceof Request) {
   352     if (input instanceof Request) {
   320       if (input.bodyUsed) {
   353       if (input.bodyUsed) {
   347 
   380 
   348     if ((this.method === 'GET' || this.method === 'HEAD') && body) {
   381     if ((this.method === 'GET' || this.method === 'HEAD') && body) {
   349       throw new TypeError('Body not allowed for GET or HEAD requests')
   382       throw new TypeError('Body not allowed for GET or HEAD requests')
   350     }
   383     }
   351     this._initBody(body);
   384     this._initBody(body);
       
   385 
       
   386     if (this.method === 'GET' || this.method === 'HEAD') {
       
   387       if (options.cache === 'no-store' || options.cache === 'no-cache') {
       
   388         // Search for a '_' parameter in the query string
       
   389         var reParamSearch = /([?&])_=[^&]*/;
       
   390         if (reParamSearch.test(this.url)) {
       
   391           // If it already exists then set the value with the current time
       
   392           this.url = this.url.replace(reParamSearch, '$1_=' + new Date().getTime());
       
   393         } else {
       
   394           // Otherwise add a new '_' parameter to the end with the current time
       
   395           var reQueryString = /\?/;
       
   396           this.url += (reQueryString.test(this.url) ? '&' : '?') + '_=' + new Date().getTime();
       
   397         }
       
   398       }
       
   399     }
   352   }
   400   }
   353 
   401 
   354   Request.prototype.clone = function() {
   402   Request.prototype.clone = function() {
   355     return new Request(this, {body: this._bodyInit})
   403     return new Request(this, {body: this._bodyInit})
   356   };
   404   };
   374   function parseHeaders(rawHeaders) {
   422   function parseHeaders(rawHeaders) {
   375     var headers = new Headers();
   423     var headers = new Headers();
   376     // Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space
   424     // Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space
   377     // https://tools.ietf.org/html/rfc7230#section-3.2
   425     // https://tools.ietf.org/html/rfc7230#section-3.2
   378     var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
   426     var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
   379     preProcessedHeaders.split(/\r?\n/).forEach(function(line) {
   427     // Avoiding split via regex to work around a common IE11 bug with the core-js 3.6.0 regex polyfill
   380       var parts = line.split(':');
   428     // https://github.com/github/fetch/issues/748
   381       var key = parts.shift().trim();
   429     // https://github.com/zloirock/core-js/issues/751
   382       if (key) {
   430     preProcessedHeaders
   383         var value = parts.join(':').trim();
   431       .split('\r')
   384         headers.append(key, value);
   432       .map(function(header) {
   385       }
   433         return header.indexOf('\n') === 0 ? header.substr(1, header.length) : header
   386     });
   434       })
       
   435       .forEach(function(line) {
       
   436         var parts = line.split(':');
       
   437         var key = parts.shift().trim();
       
   438         if (key) {
       
   439           var value = parts.join(':').trim();
       
   440           headers.append(key, value);
       
   441         }
       
   442       });
   387     return headers
   443     return headers
   388   }
   444   }
   389 
   445 
   390   Body.call(Request.prototype);
   446   Body.call(Request.prototype);
   391 
   447 
   392   function Response(bodyInit, options) {
   448   function Response(bodyInit, options) {
       
   449     if (!(this instanceof Response)) {
       
   450       throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
       
   451     }
   393     if (!options) {
   452     if (!options) {
   394       options = {};
   453       options = {};
   395     }
   454     }
   396 
   455 
   397     this.type = 'default';
   456     this.type = 'default';
   398     this.status = options.status === undefined ? 200 : options.status;
   457     this.status = options.status === undefined ? 200 : options.status;
   399     this.ok = this.status >= 200 && this.status < 300;
   458     this.ok = this.status >= 200 && this.status < 300;
   400     this.statusText = 'statusText' in options ? options.statusText : 'OK';
   459     this.statusText = options.statusText === undefined ? '' : '' + options.statusText;
   401     this.headers = new Headers(options.headers);
   460     this.headers = new Headers(options.headers);
   402     this.url = options.url || '';
   461     this.url = options.url || '';
   403     this._initBody(bodyInit);
   462     this._initBody(bodyInit);
   404   }
   463   }
   405 
   464 
   428     }
   487     }
   429 
   488 
   430     return new Response(null, {status: status, headers: {location: url}})
   489     return new Response(null, {status: status, headers: {location: url}})
   431   };
   490   };
   432 
   491 
   433   exports.DOMException = self.DOMException;
   492   exports.DOMException = global.DOMException;
   434   try {
   493   try {
   435     new exports.DOMException();
   494     new exports.DOMException();
   436   } catch (err) {
   495   } catch (err) {
   437     exports.DOMException = function(message, name) {
   496     exports.DOMException = function(message, name) {
   438       this.message = message;
   497       this.message = message;
   464           statusText: xhr.statusText,
   523           statusText: xhr.statusText,
   465           headers: parseHeaders(xhr.getAllResponseHeaders() || '')
   524           headers: parseHeaders(xhr.getAllResponseHeaders() || '')
   466         };
   525         };
   467         options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
   526         options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
   468         var body = 'response' in xhr ? xhr.response : xhr.responseText;
   527         var body = 'response' in xhr ? xhr.response : xhr.responseText;
   469         resolve(new Response(body, options));
   528         setTimeout(function() {
       
   529           resolve(new Response(body, options));
       
   530         }, 0);
   470       };
   531       };
   471 
   532 
   472       xhr.onerror = function() {
   533       xhr.onerror = function() {
   473         reject(new TypeError('Network request failed'));
   534         setTimeout(function() {
       
   535           reject(new TypeError('Network request failed'));
       
   536         }, 0);
   474       };
   537       };
   475 
   538 
   476       xhr.ontimeout = function() {
   539       xhr.ontimeout = function() {
   477         reject(new TypeError('Network request failed'));
   540         setTimeout(function() {
       
   541           reject(new TypeError('Network request failed'));
       
   542         }, 0);
   478       };
   543       };
   479 
   544 
   480       xhr.onabort = function() {
   545       xhr.onabort = function() {
   481         reject(new exports.DOMException('Aborted', 'AbortError'));
   546         setTimeout(function() {
   482       };
   547           reject(new exports.DOMException('Aborted', 'AbortError'));
   483 
   548         }, 0);
   484       xhr.open(request.method, request.url, true);
   549       };
       
   550 
       
   551       function fixUrl(url) {
       
   552         try {
       
   553           return url === '' && global.location.href ? global.location.href : url
       
   554         } catch (e) {
       
   555           return url
       
   556         }
       
   557       }
       
   558 
       
   559       xhr.open(request.method, fixUrl(request.url), true);
   485 
   560 
   486       if (request.credentials === 'include') {
   561       if (request.credentials === 'include') {
   487         xhr.withCredentials = true;
   562         xhr.withCredentials = true;
   488       } else if (request.credentials === 'omit') {
   563       } else if (request.credentials === 'omit') {
   489         xhr.withCredentials = false;
   564         xhr.withCredentials = false;
   490       }
   565       }
   491 
   566 
   492       if ('responseType' in xhr && support.blob) {
   567       if ('responseType' in xhr) {
   493         xhr.responseType = 'blob';
   568         if (support.blob) {
   494       }
   569           xhr.responseType = 'blob';
   495 
   570         } else if (
   496       request.headers.forEach(function(value, name) {
   571           support.arrayBuffer &&
   497         xhr.setRequestHeader(name, value);
   572           request.headers.get('Content-Type') &&
   498       });
   573           request.headers.get('Content-Type').indexOf('application/octet-stream') !== -1
       
   574         ) {
       
   575           xhr.responseType = 'arraybuffer';
       
   576         }
       
   577       }
       
   578 
       
   579       if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers)) {
       
   580         Object.getOwnPropertyNames(init.headers).forEach(function(name) {
       
   581           xhr.setRequestHeader(name, normalizeValue(init.headers[name]));
       
   582         });
       
   583       } else {
       
   584         request.headers.forEach(function(value, name) {
       
   585           xhr.setRequestHeader(name, value);
       
   586         });
       
   587       }
   499 
   588 
   500       if (request.signal) {
   589       if (request.signal) {
   501         request.signal.addEventListener('abort', abortXhr);
   590         request.signal.addEventListener('abort', abortXhr);
   502 
   591 
   503         xhr.onreadystatechange = function() {
   592         xhr.onreadystatechange = function() {
   512     })
   601     })
   513   }
   602   }
   514 
   603 
   515   fetch.polyfill = true;
   604   fetch.polyfill = true;
   516 
   605 
   517   if (!self.fetch) {
   606   if (!global.fetch) {
   518     self.fetch = fetch;
   607     global.fetch = fetch;
   519     self.Headers = Headers;
   608     global.Headers = Headers;
   520     self.Request = Request;
   609     global.Request = Request;
   521     self.Response = Response;
   610     global.Response = Response;
   522   }
   611   }
   523 
   612 
   524   exports.Headers = Headers;
   613   exports.Headers = Headers;
   525   exports.Request = Request;
   614   exports.Request = Request;
   526   exports.Response = Response;
   615   exports.Response = Response;