Skip to content

Commit 2079985

Browse files
committed
refactor: simplify Uint8Array checks
1 parent 8cab29a commit 2079985

20 files changed

+49
-54
lines changed

base58.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { typedView } from './array.js'
2-
import { assertUint8 } from './assert.js'
3-
import { E_STRING } from './fallback/_utils.js'
2+
import { assertU8, E_STRING } from './fallback/_utils.js'
43
import { nativeDecoder, nativeEncoder, isHermes } from './fallback/platform.js'
54
import { encodeAscii, decodeAscii } from './fallback/latin1.js'
65

@@ -24,7 +23,7 @@ const E_CHAR = 'Invalid character in base58 input'
2423
const shouldUseBigIntFrom = isHermes // faster only on Hermes, numbers path beats it on normal engines
2524

2625
function toBase58core(arr, alphabet, codes) {
27-
assertUint8(arr)
26+
assertU8(arr)
2827
const length = arr.length
2928
if (length === 0) return ''
3029

base64.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { assertUint8, assertEmptyRest } from './assert.js'
1+
import { assertEmptyRest } from './assert.js'
22
import { typedView } from './array.js'
3-
import { E_STRING } from './fallback/_utils.js'
3+
import { assertU8, E_STRING } from './fallback/_utils.js'
44
import { isHermes } from './fallback/platform.js'
55
import { decodeLatin1, encodeLatin1 } from './fallback/latin1.js'
66
import * as js from './fallback/base64.js'
@@ -38,7 +38,7 @@ const toUrl = (x) => x.replaceAll('+', '-').replaceAll('/', '_')
3838
const haveWeb = (x) => web64 && x.toBase64 === web64
3939

4040
export function toBase64(x, { padding = true } = {}) {
41-
assertUint8(x)
41+
assertU8(x)
4242
if (haveWeb(x)) return padding ? x.toBase64() : x.toBase64({ omitPadding: !padding }) // Modern, optionless is slightly faster
4343
if (haveNativeBuffer) return maybeUnpad(toBuffer(x).base64Slice(0, x.byteLength), padding) // Older Node.js
4444
if (shouldUseBtoa) return maybeUnpad(btoa(decodeLatin1(x)), padding)
@@ -47,7 +47,7 @@ export function toBase64(x, { padding = true } = {}) {
4747

4848
// NOTE: base64url omits padding by default
4949
export function toBase64url(x, { padding = false } = {}) {
50-
assertUint8(x)
50+
assertU8(x)
5151
if (haveWeb(x)) return x.toBase64({ alphabet: 'base64url', omitPadding: !padding }) // Modern
5252
if (haveNativeBuffer) return maybePad(toBuffer(x).base64urlSlice(0, x.byteLength), padding) // Older Node.js
5353
if (shouldUseBtoa) return maybeUnpad(toUrl(btoa(decodeLatin1(x))), padding)

bech32.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { assertUint8 } from './assert.js'
2-
import { E_STRING } from './fallback/_utils.js'
1+
import { assertU8, E_STRING } from './fallback/_utils.js'
32
import { nativeEncoder } from './fallback/platform.js'
43
import { decodeAscii, encodeAscii, encodeLatin1 } from './fallback/latin1.js'
54

@@ -110,7 +109,7 @@ function pPrefix(prefix) {
110109
function toBech32enc(prefix, bytes, limit, encoding) {
111110
if (typeof prefix !== 'string' || !prefix) throw new TypeError(E_PREFIX)
112111
if (typeof limit !== 'number') throw new TypeError(E_SIZE)
113-
assertUint8(bytes)
112+
assertU8(bytes)
114113
const bytesLength = bytes.length
115114
const wordsLength = Math.ceil((bytesLength * 8) / 5)
116115
if (!(prefix.length + 7 + wordsLength <= limit)) throw new TypeError(E_SIZE)

fallback/_utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export function assert(condition, msg) {
66
if (!condition) throw new Error(msg)
77
}
88

9+
export function assertU8(arg) {
10+
if (!(arg instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
11+
}
12+
913
// On arrays in heap (<= 64) it's cheaper to copy into a pooled buffer than lazy-create the ArrayBuffer storage
1014
export const toBuf = (x) =>
1115
x.byteLength <= 64 && x.BYTES_PER_ELEMENT === 1

fallback/base32.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertUint8 } from '../assert.js'
1+
import { assertU8 } from './_utils.js'
22
import { nativeEncoder, nativeDecoder, isHermes } from './platform.js'
33
import { encodeAscii, decodeAscii } from './latin1.js'
44

@@ -18,7 +18,7 @@ const useTemplates = isHermes // Faster on Hermes and JSC, but we use it only on
1818

1919
// We construct output by concatenating chars, this seems to be fine enough on modern JS engines
2020
export function toBase32(arr, isBase32Hex, padding) {
21-
assertUint8(arr)
21+
assertU8(arr)
2222
const fullChunks = Math.floor(arr.length / 5)
2323
const fullChunksBytes = fullChunks * 5
2424
let o = ''

fallback/base58check.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { typedView } from '@exodus/bytes/array.js'
22
import { toBase58, fromBase58 } from '@exodus/bytes/base58.js'
3-
import { assertUint8 } from '../assert.js'
3+
import { assertU8 } from './_utils.js'
44

55
const E_CHECKSUM = 'Invalid checksum'
66

@@ -28,7 +28,7 @@ function assertChecksum(c, r) {
2828
export const makeBase58check = (hashAlgo, hashAlgoSync) => {
2929
const apis = {
3030
async encode(arr) {
31-
assertUint8(arr)
31+
assertU8(arr)
3232
return encodeWithChecksum(arr, await hashAlgo(arr))
3333
},
3434
async decode(str, format = 'uint8') {
@@ -41,7 +41,7 @@ export const makeBase58check = (hashAlgo, hashAlgoSync) => {
4141
return {
4242
...apis,
4343
encodeSync(arr) {
44-
assertUint8(arr)
44+
assertU8(arr)
4545
return encodeWithChecksum(arr, hashAlgoSync(arr))
4646
},
4747
decodeSync(str, format = 'uint8') {

fallback/base64.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { assertUint8 } from '../assert.js'
21
import { nativeEncoder, nativeDecoder } from './platform.js'
32
import { encodeAscii, decodeAscii } from './latin1.js'
43

@@ -15,8 +14,8 @@ export const E_LENGTH = 'Invalid base64 length'
1514
export const E_LAST = 'Invalid last chunk'
1615

1716
// We construct output by concatenating chars, this seems to be fine enough on modern JS engines
17+
// Expects a checked Uint8Array
1818
export function toBase64(arr, isURL, padding) {
19-
assertUint8(arr)
2019
const fullChunks = (arr.length / 3) | 0
2120
const fullChunksBytes = fullChunks * 3
2221
let o = ''

fallback/hex.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { assertUint8 } from '../assert.js'
21
import { E_STRING } from './_utils.js'
32
import { nativeDecoder, nativeEncoder, decode2string } from './platform.js'
43
import { encodeAscii, decodeAscii } from './latin1.js'
@@ -12,9 +11,8 @@ const allowed = '0123456789ABCDEFabcdef'
1211

1312
export const E_HEX = 'Input is not a hex string'
1413

14+
// Expects a checked Uint8Array
1515
export function toHex(arr) {
16-
assertUint8(arr)
17-
1816
if (!hexArray) hexArray = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'))
1917
const length = arr.length // this helps Hermes
2018

fallback/utf16.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { decodeUCS2, encodeCharcodes } from './latin1.js'
2-
import { E_STRING, E_STRICT_UNICODE } from './_utils.js'
2+
import { assertU8, E_STRING, E_STRICT_UNICODE } from './_utils.js'
33
import { nativeDecoder, isLE } from './platform.js'
44

55
export const E_STRICT = 'Input is not well-formed utf16'
@@ -38,7 +38,7 @@ export function decodeApiDecoders(input, loose, format) {
3838
if (format === 'uint16') {
3939
if (!(input instanceof Uint16Array)) throw new TypeError('Expected an Uint16Array')
4040
} else if (format === 'uint8-le' || format === 'uint8-be') {
41-
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
41+
assertU8(input)
4242
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
4343
} else {
4444
throw new TypeError('Unknown format')
@@ -56,12 +56,12 @@ export function decodeApiJS(input, loose, format) {
5656
u16 = input
5757
break
5858
case 'uint8-le':
59-
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
59+
assertU8(input)
6060
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
6161
u16 = to16input(input, true)
6262
break
6363
case 'uint8-be':
64-
if (!(input instanceof Uint8Array)) throw new TypeError('Expected an Uint8Array')
64+
assertU8(input)
6565
if (input.byteLength % 2 !== 0) throw new TypeError('Expected even number of bytes')
6666
u16 = to16input(input, false)
6767
break

hex.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { assertUint8 } from './assert.js'
21
import { typedView } from './array.js'
2+
import { assertU8 } from './fallback/_utils.js'
33
import * as js from './fallback/hex.js'
44

55
const { toHex: webHex } = Uint8Array.prototype // Modern engines have this
66

77
export function toHex(arr) {
8-
assertUint8(arr)
8+
assertU8(arr)
99
if (arr.length === 0) return ''
1010
if (webHex && arr.toHex === webHex) return arr.toHex()
1111
return js.toHex(arr)

0 commit comments

Comments
 (0)