TextEncoder allows non-Uint8Array Buffers (#596)

* TextEncoder allows non-Uint8Array Buffers

* Add eslintrc to eslintignore
This commit is contained in:
Jaco Greeff
2020-05-11 09:57:00 +02:00
committed by GitHub
parent d3583dc432
commit d1de30907f
17 changed files with 1106 additions and 953 deletions

View File

@@ -1,3 +1,4 @@
.eslintrc.js
**/build/*
**/coverage/*
**/node_modules/*

View File

@@ -1,3 +1,7 @@
# 2.10.0-beta.x
- Make the `TextEncoder` polyfill handle non-compliant Buffer implementations (newer versions of Jest)
# 2.9.1 Apr 30, 2020
- Add support for ECDSA keypairs (Thanks to https://github.com/akru)

View File

@@ -22,8 +22,8 @@
},
"devDependencies": {
"@babel/core": "^7.9.6",
"@polkadot/dev": "^0.52.12",
"@polkadot/ts": "^0.3.19",
"@polkadot/dev": "^0.52.17",
"@polkadot/ts": "^0.3.20",
"@types/jest": "^25.2.1"
}
}

View File

@@ -10,7 +10,7 @@ describe('bnToU8a', (): void => {
it('converts null values to 0x00', (): void => {
expect(
bnToU8a(null, -1, false)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts null values to 0x00000000 (bitLength)', (): void => {

View File

@@ -43,7 +43,7 @@ function bnToU8a (value: BN | BigInt | number | null, arg1: number | Options = {
if (!value) {
return _options.bitLength === -1
? new Uint8Array([])
? new Uint8Array()
: new Uint8Array(byteLength);
}

View File

@@ -8,7 +8,7 @@ describe('bufferToU8a', (): void => {
it('returns an empty buffer when null provided', (): void => {
expect(
bufferToU8a(null)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('returns a Uint8Buffer with the correct values', (): void => {

View File

@@ -17,9 +17,5 @@
* ```
*/
export default function bufferToU8a (buffer?: Buffer | number[] | null): Uint8Array {
if (!buffer) {
return new Uint8Array([]);
}
return new Uint8Array(buffer);
return new Uint8Array(buffer || []);
}

View File

@@ -23,7 +23,7 @@ import hexStripPrefix from './stripPrefix';
*/
export default function hexToU8a (_value?: string | null, bitLength = -1): Uint8Array {
if (!_value) {
return new Uint8Array([]);
return new Uint8Array();
}
assert(isHex(_value), `Expected hex value to convert, found '${_value}'`);

View File

@@ -19,7 +19,7 @@ describe('isUint8Array', (): void => {
it('returns true on Uint8Array values', (): void => {
expect(
isU8a(new Uint8Array([]))
isU8a(new Uint8Array())
).toEqual(true);
});
});

View File

@@ -8,25 +8,25 @@ describe('numberToU8a', (): void => {
it('converts 0 to empty', (): void => {
expect(
numberToU8a(0)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts undefined to empty', (): void => {
expect(
numberToU8a()
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts null to empty', (): void => {
expect(
numberToU8a(null)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts NaN to empty', (): void => {
expect(
numberToU8a(NaN)
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('converts values to the u8a', (): void => {

View File

@@ -21,7 +21,7 @@ import numberToHex from './toHex';
*/
export default function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
if (!value || isNaN(value)) {
return new Uint8Array([]);
return new Uint8Array();
}
return hexToU8a(

View File

@@ -4,8 +4,23 @@
if (typeof TextEncoder === 'undefined') {
try {
const UTE = require('util').TextEncoder;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).TextEncoder = require('util').TextEncoder;
(global as any).TextEncoder = class TextEncoder {
#encoder: TextEncoder;
constructor () {
this.#encoder = new UTE();
}
// For a Jest 26.0.1 environment, Buffer !== Uint8Array
encode (value: string): Uint8Array {
const encoded = this.#encoder.encode(value);
return Uint8Array.from(encoded);
}
};
} catch (error) {
// noop
}

View File

@@ -8,7 +8,7 @@ describe('stringToU8a', (): void => {
it('decodes to an empty string for undefined', (): void => {
expect(
stringToU8a()
).toEqual(new Uint8Array([]));
).toEqual(new Uint8Array());
});
it('encodes the string correctly', (): void => {

View File

@@ -39,5 +39,5 @@ try {
export default function stringToU8a (value?: string): Uint8Array {
return value
? encoder.encode(value)
: new Uint8Array([]);
: new Uint8Array();
}

View File

@@ -33,7 +33,7 @@ describe('u8aToBn', (): void => {
it('converts empty', (): void => {
expect(
u8aToBn(
new Uint8Array([]),
new Uint8Array(),
{ isLe: true }
).toString(16)
).toBe('0');

View File

@@ -13,7 +13,7 @@ describe('u8aToString', (): void => {
it('decodes to an empty string for empty buffer', (): void => {
expect(
u8aToString(new Uint8Array([]))
u8aToString(new Uint8Array())
).toEqual('');
});

1999
yarn.lock

File diff suppressed because it is too large Load Diff