From b20df563ce7aec76432d4c791822cc60dcf24ca0 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 24 May 2022 16:27:47 +0200 Subject: [PATCH] Buffer short-circuit (onlky convert when non-Uint8Array) (#1499) --- packages/util/src/compact/fromU8a.spec.ts | 8 ++++++++ packages/util/src/u8a/toString.ts | 6 +++--- packages/util/src/u8a/toU8a.spec.ts | 18 ++++-------------- packages/util/src/u8a/toU8a.ts | 7 ++----- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/packages/util/src/compact/fromU8a.spec.ts b/packages/util/src/compact/fromU8a.spec.ts index d005853cb..6ea634831 100644 --- a/packages/util/src/compact/fromU8a.spec.ts +++ b/packages/util/src/compact/fromU8a.spec.ts @@ -57,6 +57,14 @@ describe('compactFromU8a', (): void => { ).toEqual([7, new BN('5af3107a4000', 16)]); }); + it('decodes an actual value (Buffer)', (): void => { + expect( + compactFromU8a( + Buffer.from('0b00407a10f35a', 'hex') + ) + ).toEqual([7, new BN('5af3107a4000', 16)]); + }); + it('decodes an actual value', (): void => { expect( compactFromU8a( diff --git a/packages/util/src/u8a/toString.ts b/packages/util/src/u8a/toString.ts index 438f80d98..eb0efd2c4 100644 --- a/packages/util/src/u8a/toString.ts +++ b/packages/util/src/u8a/toString.ts @@ -20,7 +20,7 @@ const decoder = new TextDecoder('utf-8'); * ``` */ export function u8aToString (value?: Uint8Array | null): string { - return !value?.length - ? '' - : decoder.decode(value); + return value + ? decoder.decode(value) + : ''; } diff --git a/packages/util/src/u8a/toU8a.spec.ts b/packages/util/src/u8a/toU8a.spec.ts index c29ad8f74..49945ba98 100644 --- a/packages/util/src/u8a/toU8a.spec.ts +++ b/packages/util/src/u8a/toU8a.spec.ts @@ -39,28 +39,18 @@ describe('u8aToU8a', (): void => { }); it('creates from a Buffer', (): void => { - const input = Buffer.from([1, 2, 3, 128, 0, 10, 11, 12]); - const output = u8aToU8a(input); - expect( - typeof input.readDoubleLE - ).toEqual('function'); - expect( - typeof (output as Buffer).readDoubleLE - ).toEqual('undefined'); - - expect( - output + u8aToU8a(Buffer.from([1, 2, 3, 128, 0, 10, 11, 12])).buffer ).toEqual( - new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12]) + new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12]).buffer ); }); it('creates from a Buffer (hex)', (): void => { expect( - u8aToU8a(Buffer.from('80000a', 'hex')) + u8aToU8a(Buffer.from('80000a', 'hex')).buffer ).toEqual( - new Uint8Array([128, 0, 10]) + new Uint8Array([128, 0, 10]).buffer ); }); diff --git a/packages/util/src/u8a/toU8a.ts b/packages/util/src/u8a/toU8a.ts index b95c432e2..4d7a000ce 100644 --- a/packages/util/src/u8a/toU8a.ts +++ b/packages/util/src/u8a/toU8a.ts @@ -26,13 +26,10 @@ import { stringToU8a } from '../string/toU8a'; */ export function u8aToU8a (value?: U8aLike | null): Uint8Array { return isU8a(value) - // under Node, Buffer implements Uint8Array, check it first - ? isBuffer(value) - ? new Uint8Array(value) - : value + ? value : isHex(value) ? hexToU8a(value) - : Array.isArray(value) + : isBuffer(value) || Array.isArray(value) ? new Uint8Array(value) : stringToU8a(value); }