Fallback fallback ascii-only encoder/decoder (#271)

This commit is contained in:
Jaco Greeff
2018-12-17 14:12:52 +01:00
committed by GitHub
parent 67e1359fe1
commit cd7f64486b
4 changed files with 37 additions and 9 deletions
@@ -4,6 +4,18 @@
// tslint:disable-next-line
if (typeof TextDecoder === 'undefined') {
// @ts-ignore
global.TextDecoder = require('util').TextDecoder;
try {
// @ts-ignore For the Node.js case
global.TextDecoder = require('util').TextDecoder;
} catch (error) {
// @ts-ignore For the old browser case
global.TextDecoder = {
// Very naive ascii-only decoder
decode: (u8a: Uint8Array) => {
return u8a.reduce((str, code) => {
return str + String.fromCharCode(code);
}, '');
}
};
}
}
@@ -4,6 +4,22 @@
// tslint:disable-next-line
if (typeof TextEncoder === 'undefined') {
// @ts-ignore
global.TextEncoder = require('util').TextEncoder;
try {
// @ts-ignore For the Node.js case
global.TextEncoder = require('util').TextEncoder;
} catch (error) {
// @ts-ignore For the old browser case
global.TextEncoder = {
// Very naive ascii-only encoder
encode: (str: string) => {
const u8a = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
u8a[i] = str.charCodeAt(i);
}
return u8a;
}
};
}
}