Skip to content

Commit

Permalink
proposed fix for facebook#18223
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Humphreys committed Jun 5, 2018
1 parent 30320f1 commit 26c95bf
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions Libraries/Network/XMLHttpRequest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
Expand Down Expand Up @@ -181,7 +182,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
if (this._sent) {
throw new Error(
"Failed to set the 'responseType' property on 'XMLHttpRequest': The " +
'response type cannot be set after the request has been sent.',
'response type cannot be set after the request has been sent.',
);
}
if (!SUPPORTED_RESPONSE_TYPES.hasOwnProperty(responseType)) {
Expand Down Expand Up @@ -211,7 +212,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
if (this._responseType !== '' && this._responseType !== 'text') {
throw new Error(
"The 'responseText' property is only available if 'responseType' " +
`is set to '' or 'text', but it is '${this._responseType}'.`,
`is set to '' or 'text', but it is '${this._responseType}'.`,
);
}
if (this.readyState < LOADING) {
Expand All @@ -221,7 +222,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
}

get response(): Response {
const {responseType} = this;
const { responseType } = this;
if (responseType === '' || responseType === 'text') {
return this.readyState < LOADING || this._hasError ? '' : this._response;
}
Expand All @@ -247,7 +248,11 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
if (typeof this._response === 'object' && this._response) {
this._cachedResponse = BlobManager.createFromOptions(this._response);
} else {
throw new Error(`Invalid response for blob: ${this._response}`);
if (this._response === '') {
this._cachedResponse = null;
} else {
throw new Error(`Invalid response for blob: ${this._response}`);
}
}
break;

Expand Down Expand Up @@ -565,20 +570,20 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {

setReadyState(newState: number): void {
this.readyState = newState;
this.dispatchEvent({type: 'readystatechange'});
this.dispatchEvent({ type: 'readystatechange' });
if (newState === this.DONE) {
if (this._aborted) {
this.dispatchEvent({type: 'abort'});
this.dispatchEvent({ type: 'abort' });
} else if (this._hasError) {
if (this._timedOut) {
this.dispatchEvent({type: 'timeout'});
this.dispatchEvent({ type: 'timeout' });
} else {
this.dispatchEvent({type: 'error'});
this.dispatchEvent({ type: 'error' });
}
} else {
this.dispatchEvent({type: 'load'});
this.dispatchEvent({ type: 'load' });
}
this.dispatchEvent({type: 'loadend'});
this.dispatchEvent({ type: 'loadend' });
}
}

Expand All @@ -595,4 +600,4 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
}
}

module.exports = XMLHttpRequest;
module.exports = XMLHttpRequest;

0 comments on commit 26c95bf

Please sign in to comment.