Skip to content

Commit 9a8c06b

Browse files
RSNarafacebook-github-bot
authored andcommitted
Transform empty responses into empty Blobs
Summary: In `XMLHttpRequest`, if the responseType is `Blob`, but the response is an empty string, we return `null` from `XMLHttpRequest.prototype.response()`. Instead, we should return an empty Blob. This is the behaviour on the web. To demonstrate, run the following HTTP server with Node: ## server.js ``` const http = require('http'); const server = http.createServer(); server.on('request', (request, response) => { if (request.url.includes('img.png')) { console.log('sending image'); response.end(''); return; } response.end('Hello World!'); }); server.listen('9000'); ``` Then, open up a web browser to `http://localhost:9000`, and type the following in the console: ``` var oReq = new XMLHttpRequest(); oReq.open('GET', 'http://localhost:9000/img.png', true); oReq.responseType = 'blob'; oReq.onload = function(oEvent) { var blob = oReq.response; console.warn(blob); }; oReq.onerror = function(error) { console.warn('Error!'); }; oReq.send(); ``` This warns: ``` Blob {size: 0, type: "text/xml"} ``` Changelog: [Both][Fixed] - [RN][XMLHttpRequest] Transform empty responses into empty Blobs Reviewed By: sahrens Differential Revision: D19500607 fbshipit-source-id: ec35e534b32a507c8a94a29e955b7bc4c62902a0
1 parent aeaf286 commit 9a8c06b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Libraries/Blob/FileReader.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,15 @@ class FileReader extends (EventTarget(...READER_EVENTS): any) {
8585
throw new Error('FileReader.readAsArrayBuffer is not implemented');
8686
}
8787

88-
readAsDataURL(blob: Blob) {
88+
readAsDataURL(blob: ?Blob) {
8989
this._aborted = false;
9090

91+
if (blob == null) {
92+
throw new TypeError(
93+
"Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'",
94+
);
95+
}
96+
9197
NativeFileReaderModule.readAsDataURL(blob.data).then(
9298
(text: string) => {
9399
if (this._aborted) {
@@ -106,9 +112,15 @@ class FileReader extends (EventTarget(...READER_EVENTS): any) {
106112
);
107113
}
108114

109-
readAsText(blob: Blob, encoding: string = 'UTF-8') {
115+
readAsText(blob: ?Blob, encoding: string = 'UTF-8') {
110116
this._aborted = false;
111117

118+
if (blob == null) {
119+
throw new TypeError(
120+
"Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'",
121+
);
122+
}
123+
112124
NativeFileReaderModule.readAsText(blob.data, encoding).then(
113125
(text: string) => {
114126
if (this._aborted) {

Libraries/Network/XMLHttpRequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class XMLHttpRequest extends (EventTarget(...XHR_EVENTS): any) {
244244
if (typeof this._response === 'object' && this._response) {
245245
this._cachedResponse = BlobManager.createFromOptions(this._response);
246246
} else if (this._response === '') {
247-
this._cachedResponse = null;
247+
this._cachedResponse = BlobManager.createFromParts([]);
248248
} else {
249249
throw new Error(`Invalid response for blob: ${this._response}`);
250250
}

0 commit comments

Comments
 (0)