src/service/xhr-impl.js
/**
 * A minimal polyfill of Fetch API. It only polyfills what we currently use.
 *
 * See https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch
 *
 * Notice that the "fetch" method itself is not exported as that would require
 * us to immediately support a much wide API.
 *
 * @param {string} input
 * @param {!FetchInitDef} init
 * @return {!Promise<!FetchResponse>}
 * @private Visible for testing
 */
A minimal polyfill of Fetch API. It only polyfills what we currently use.

Notice that the "fetch" method itself is not exported as that would require
us to immediately support a much wide API.

@param {string} input
@param {!FetchInitDef} init
@return {!Promise}
@private Visible for testing
A minimal polyfill of Fetch API . It only polyfills what we currently use . Notice that the " fetch " method itself is not exported as that would require us to immediately support a much wide API . @param { string } input @param { !FetchInitDef } init @return { !Promise } @private Visible for testing
function fetchPolyfill(input, init) {
  return new Promise(function(resolve, reject) {
    const xhr = createXhrRequest(init.method || 'GET', input);
    if (init.credentials == 'include') {
      xhr.withCredentials = true;
    }
    if (init.responseType in allowedFetchTypes_) {
      xhr.responseType = init.responseType;
    }
    if (init.headers) {
      Object.keys(init.headers).forEach(function(header) {
        xhr.setRequestHeader(header, init.headers[header]);
      });
    }
    xhr.onreadystatechange = () => {
      if (xhr.readyState <  2) {
        return;
      }
      if (xhr.status < 100 || xhr.status > 599) {
        xhr.onreadystatechange = null;
        reject(user().createExpectedError(`Unknown HTTP status ${xhr.status}`));
        return;
      }
      if (xhr.readyState ==  4) {
        resolve(new FetchResponse(xhr));
      }
    };
    xhr.onerror = () => {
      reject(user().createExpectedError('Network failure'));
    };
    xhr.onabort = () => {
      reject(user().createExpectedError('Request aborted'));
    };
    if (init.method == 'POST') {
      xhr.send(init.body);
    } else {
      xhr.send();
    }
  });
}
function fetchPolyfill ( input , init ) { return new Promise ( function ( resolve , reject ) { const xhr = createXhrRequest ( init . method || 'GET' , input ) ; if ( init . credentials == 'include' ) { xhr . withCredentials = true ; } if ( init . responseType in allowedFetchTypes_ ) { xhr . responseType = init . responseType ; } if ( init . headers ) { Object . keys ( init . headers ) . forEach ( function ( header ) { xhr . setRequestHeader ( header , init . headers [ header ] ) ; } ) ; } xhr . onreadystatechange = ( ) => { if ( xhr . readyState < 2 ) { return ; } if ( xhr . status < 100 || xhr . status > 599 ) { xhr . onreadystatechange = null ; reject ( user ( ) . createExpectedError ( ` ${ xhr . status } ` ) ) ; return ; } if ( xhr . readyState == 4 ) { resolve ( new FetchResponse ( xhr ) ) ; } } ; xhr . onerror = ( ) => { reject ( user ( ) . createExpectedError ( 'Network failure' ) ) ; } ; xhr . onabort = ( ) => { reject ( user ( ) . createExpectedError ( 'Request aborted' ) ) ; } ; if ( init . method == 'POST' ) { xhr . send ( init . body ) ; } else { xhr . send ( ) ; } } ) ; }
A minimal polyfill of Fetch API.
A minimal polyfill of Fetch API .
/* STATUS_RECEIVED */ // TODO(dvoytenko): This is currently simplified: we will wait for the // whole document loading to complete. This is fine for the use cases // we have now, but may need to be reimplemented later. /* COMPLETE */