Skip to content

Commit

Permalink
Fix "decode" compatibility with react-native (samthor#22)
Browse files Browse the repository at this point in the history
React-Native runtime fails with [Error: Cannot create URL for blob]. Guess Blob differences from it browser api.
  • Loading branch information
Reeywhaar authored Jun 20, 2022
1 parent 19e4894 commit a8f00c4
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions text.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,21 @@ function decodeBuffer(bytes) {
* @return {string}
*/
function decodeSyncXHR(bytes) {
const b = new Blob([bytes], {type: 'text/plain;charset=UTF-8'});
const u = URL.createObjectURL(b);

let u;
// This hack will fail in non-Edgium Edge because sync XHRs are disabled (and
// possibly in other places), so ensure there's a fallback call.
try {
const b = new Blob([bytes], {type: 'text/plain;charset=UTF-8'});
u = URL.createObjectURL(b);

const x = new XMLHttpRequest();
x.open('GET', u, false);
x.send();
return x.responseText;
} catch (e) {
return decodeFallback(bytes);
} finally {
URL.revokeObjectURL(u);
if (u) URL.revokeObjectURL(u);
}
}

Expand Down

0 comments on commit a8f00c4

Please sign in to comment.