React native support, strengthen TextEncoder/Decoder (#273)

This commit is contained in:
Jaco Greeff
2018-12-17 21:24:02 +01:00
committed by GitHub
parent 928d9bcff5
commit 30bf357d5d
6 changed files with 73 additions and 29 deletions
+4 -1
View File
@@ -35,11 +35,14 @@
"@types/ip-regex": "^2.0.0",
"@types/xxhashjs": "^0.1.1",
"bn.js": "^4.11.8",
"buffer": "^5.2.1",
"camelcase": "^5.0.0",
"chalk": "^2.4.1",
"core-js": "^2.5.7",
"deasync": "^0.1.14",
"ip-regex": "^3.0.0",
"moment": "^2.22.2"
"moment": "^2.22.2",
"stream": "^0.0.2",
"util": "^0.11.1"
}
}
@@ -5,17 +5,9 @@
// tslint:disable-next-line
if (typeof TextDecoder === 'undefined') {
try {
// @ts-ignore For the Node.js case
// @ts-ignore
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);
}, '');
}
};
// noop
}
}
@@ -8,18 +8,6 @@ if (typeof TextEncoder === 'undefined') {
// @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;
}
};
// noop
}
}
+16 -2
View File
@@ -4,7 +4,13 @@
import './polyfill/textEncoder';
const encoder = new TextEncoder();
let encoder: TextEncoder | undefined;
try {
encoder = new TextEncoder();
} catch (error) {
// noop
}
/**
* @name stringToU8a
@@ -24,7 +30,15 @@ const encoder = new TextEncoder();
export default function stringToU8a (value?: string): Uint8Array {
if (!value) {
return new Uint8Array([]);
} else if (encoder) {
return encoder.encode(value);
}
return encoder.encode(value);
const u8a = new Uint8Array(value.length);
for (let i = 0; i < value.length; i++) {
u8a[i] = value.charCodeAt(i);
}
return u8a;
}
+12 -2
View File
@@ -4,7 +4,13 @@
import '../string/polyfill/textDecoder';
const decoder = new TextDecoder('utf-8');
let decoder: TextDecoder | undefined;
try {
decoder = new TextDecoder('utf-8');
} catch (error) {
// ignore
}
/**
* @name u8aToString
@@ -24,7 +30,11 @@ const decoder = new TextDecoder('utf-8');
export default function u8aToString (value?: Uint8Array): string {
if (!value || !value.length) {
return '';
} else if (decoder) {
return decoder.decode(value);
}
return decoder.decode(value);
return value.reduce((str, code) => {
return str + String.fromCharCode(code);
}, '');
}
+38 -1
View File
@@ -2023,6 +2023,11 @@ base-x@^3.0.2:
dependencies:
safe-buffer "^5.0.1"
base64-js@^1.0.2:
version "1.3.0"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.3.0.tgz#cab1e6118f051095e58b5281aea8c1cd22bfc0e3"
integrity sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==
base@^0.11.1:
version "0.11.2"
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
@@ -2243,6 +2248,14 @@ buffer-shims@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-shims/-/buffer-shims-1.0.0.tgz#9978ce317388c649ad8793028c3477ef044a8b51"
integrity sha1-mXjOMXOIxkmth5MCjDR37wRKi1E=
buffer@^5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.2.1.tgz#dd57fa0f109ac59c602479044dca7b8b3d0b71d6"
integrity sha512-c+Ko0loDaFfuPWiL02ls9Xd3GO3cPVmUobQ6t3rXNUk304u6hGq+8N/kFi+QEIKhzK3uwolVhLzszmfLmMLnqg==
dependencies:
base64-js "^1.0.2"
ieee754 "^1.1.4"
builtin-modules@^1.0.0, builtin-modules@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
@@ -3302,6 +3315,11 @@ electron-to-chromium@^1.3.86:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.88.tgz#f36ab32634f49ef2b0fdc1e82e2d1cc17feb29e7"
integrity sha512-UPV4NuQMKeUh1S0OWRvwg0PI8ASHN9kBC8yDTk1ROXLC85W5GnhTRu/MZu3Teqx3JjlQYuckuHYXSUSgtb3J+A==
emitter-component@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/emitter-component/-/emitter-component-1.1.1.tgz#065e2dbed6959bf470679edabeaf7981d1003ab6"
integrity sha1-Bl4tvtaVm/RwZ57avq95gdEAOrY=
encoding@^0.1.11:
version "0.1.12"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
@@ -4486,6 +4504,11 @@ iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@^0.4.4, iconv-lite@~0.4.13:
dependencies:
safer-buffer ">= 2.1.2 < 3"
ieee754@^1.1.4:
version "1.1.12"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.12.tgz#50bf24e5b9c8bb98af4964c941cdb0918da7b60b"
integrity sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==
iferr@^0.1.5, iferr@~0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/iferr/-/iferr-0.1.5.tgz#c60eed69e6d8fdb6b3104a1fcbca1c192dc5b501"
@@ -4541,7 +4564,7 @@ inflight@^1.0.4, inflight@~1.0.4, inflight@~1.0.6:
once "^1.3.0"
wrappy "1"
inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1, inherits@~2.0.3:
inherits@2, inherits@2.0.3, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1, inherits@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=
@@ -8630,6 +8653,13 @@ stream-shift@^1.0.0:
resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.0.tgz#d5c752825e5367e786f78e18e445ea223a155952"
integrity sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI=
stream@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/stream/-/stream-0.0.2.tgz#7f5363f057f6592c5595f00bc80a27f5cec1f0ef"
integrity sha1-f1Nj8Ff2WSxVlfALyAon9c7B8O8=
dependencies:
emitter-component "^1.1.1"
string-length@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/string-length/-/string-length-2.0.0.tgz#d40dbb686a3ace960c1cffca562bf2c45f8363ed"
@@ -9320,6 +9350,13 @@ util.promisify@^1.0.0:
define-properties "^1.1.2"
object.getownpropertydescriptors "^2.0.3"
util@^0.11.1:
version "0.11.1"
resolved "https://registry.yarnpkg.com/util/-/util-0.11.1.tgz#3236733720ec64bb27f6e26f421aaa2e1b588d61"
integrity sha512-HShAsny+zS2TZfaXxD9tYj4HQGlBezXZMZuM/S5PKLLoZkShZiGk9o5CzukI1LVHZvjdvZ2Sj1aW/Ndn2NB/HQ==
dependencies:
inherits "2.0.3"
uuid@^3.0.0, uuid@^3.0.1, uuid@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"