From cd7f64486b8e9311996824bd4965ae135792740d Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 17 Dec 2018 14:12:52 +0100 Subject: [PATCH] Fallback fallback ascii-only encoder/decoder (#271) --- package.json | 2 +- .../util/src/string/polyfill/textDecoder.ts | 16 +++++++++++++-- .../util/src/string/polyfill/textEncoder.ts | 20 +++++++++++++++++-- yarn.lock | 8 ++++---- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index dfd192afa..09b298ad3 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ }, "devDependencies": { "@babel/core": "^7.2.0", - "@polkadot/dev": "^0.22.11", + "@polkadot/dev": "^0.22.12", "@polkadot/ts": "^0.1.43" } } diff --git a/packages/util/src/string/polyfill/textDecoder.ts b/packages/util/src/string/polyfill/textDecoder.ts index 94055ca57..6816f9017 100644 --- a/packages/util/src/string/polyfill/textDecoder.ts +++ b/packages/util/src/string/polyfill/textDecoder.ts @@ -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); + }, ''); + } + }; + } } diff --git a/packages/util/src/string/polyfill/textEncoder.ts b/packages/util/src/string/polyfill/textEncoder.ts index 2c737b34f..d4e8f9f22 100644 --- a/packages/util/src/string/polyfill/textEncoder.ts +++ b/packages/util/src/string/polyfill/textEncoder.ts @@ -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; + } + }; + } } diff --git a/yarn.lock b/yarn.lock index 742107a38..a0f1c8423 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1327,10 +1327,10 @@ resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.2.tgz#54c5a964462be3d4d78af631363c18d6fa91ac26" integrity sha512-yprFYuno9FtNsSHVlSWd+nRlmGoAbqbeCwOryP6sC/zoCjhpArcRMYp19EvpSUSizJAlsXEwJv+wcWS9XaXdMw== -"@polkadot/dev@^0.22.11": - version "0.22.11" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.22.11.tgz#fcacaba21cb7f72490ad67e228171ce6cf5903f7" - integrity sha512-xYyAknjwqNZqDpxNgbH137R5m3jACQjnJi7/rz3uR8GFW107rFb7sGTtvtX3TYaS7CP69Eiy+mCRCE+3bdKHsw== +"@polkadot/dev@^0.22.12": + version "0.22.12" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.22.12.tgz#f0eaf211efb3d6b70869dab63a022858a110de85" + integrity sha512-dF4LJHrDOLbn7AqBm2ZL8o9YqUsm0+00OED8XCI0n2jy7jvMd37PwPn3gib0Ip7xwSZiikJbHvNkvmyGUUZjpw== dependencies: "@babel/cli" "^7.2.0" "@babel/core" "^7.2.0"