Skip to content

Commit c6a40d2

Browse files
committed
fromUint8Super -> fromTypedArray
1 parent e65a27d commit c6a40d2

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

base64.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert, assertUint8 } from './assert.js'
2-
import { fromUint8Super } from './convert.js'
2+
import { fromTypedArray } from './convert.js'
33

44
// See https://datatracker.ietf.org/doc/html/rfc4648
55

@@ -54,7 +54,7 @@ export function fromBase64(arg, format = 'uint8') {
5454
}
5555

5656
assert(!/[^0-9a-z=+/]/ui.test(arg), 'Invalid character in base64 input')
57-
return fromUint8Super(fromBase64common(arg, false), format)
57+
return fromTypedArray(fromBase64common(arg, false), format)
5858
}
5959

6060
export function fromBase64url(arg, format = 'uint8') {
@@ -65,7 +65,7 @@ export function fromBase64url(arg, format = 'uint8') {
6565
assert(!arg.includes('='), 'Did not expect padding in base64url input')
6666

6767
assert(!/[^0-9a-z_-]/ui.test(arg), 'Invalid character in base64url input')
68-
return fromUint8Super(fromBase64common(arg, true), format)
68+
return fromTypedArray(fromBase64common(arg, true), format)
6969
}
7070

7171
function checkLastBase64Chunk(s, arr, isBase64url = false) {

convert.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { assert, assertUint8 } from './assert.js'
1+
import { assert } from './assert.js'
22

33
const { Buffer } = globalThis // Buffer is optional, only used when native
44
const haveNativeBuffer = Buffer && !Buffer.TYPED_ARRAY_SUPPORT
55

66
let hexArray
77
let dehexArray
88

9-
// From Uint8Array or a Buffer, defaults to uint8
10-
export function fromUint8Super(arr, format = 'uint8') {
11-
assertUint8(arr)
9+
const TypedArray = Object.getPrototypeOf(Uint8Array)
10+
11+
export function fromTypedArray(arr, format = 'uint8') {
12+
assert(arr instanceof TypedArray, 'Expected a TypedArray instance')
1213
switch (format) {
1314
case 'uint8':
1415
if (arr.constructor === Uint8Array) return arr // fast path
@@ -17,14 +18,15 @@ export function fromUint8Super(arr, format = 'uint8') {
1718
if (arr.constructor === Buffer && Buffer.isBuffer(arr)) return arr
1819
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
1920
case 'hex':
20-
if (Uint8Array.prototype.toHex && arr.toHex === Uint8Array.prototype.toHex) return arr.toHex()
21+
const u8 = arr instanceof Uint8Array ? arr : new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength)
22+
if (Uint8Array.prototype.toHex && u8.toHex === Uint8Array.prototype.toHex) return u8.toHex()
2123
if (haveNativeBuffer) {
2224
if (arr.constructor === Buffer && Buffer.isBuffer(arr)) return arr.toString('hex')
2325
return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength).toString('hex')
2426
}
2527
if (!hexArray) hexArray = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'))
2628
let out = ''
27-
for (let i = 0; i < arr.length; i++) out += hexArray[arr[i]]
29+
for (let i = 0; i < arr.length; i++) out += hexArray[u8[i]]
2830
return out
2931
}
3032

@@ -33,12 +35,12 @@ export function fromUint8Super(arr, format = 'uint8') {
3335

3436
// Unlike Buffer.from(), throws on invalid input
3537
export function fromHex(arg, format = 'uint8') {
36-
if (Uint8Array.fromHex) return fromUint8Super(Uint8Array.fromHex(arg), format)
38+
if (Uint8Array.fromHex) return fromTypedArray(Uint8Array.fromHex(arg), format)
3739
if (typeof arg !== 'string') throw new TypeError('Input is not a string')
3840
assert(arg.length % 2 === 0, 'Input is not a hex string')
3941
if (haveNativeBuffer) {
4042
assert(!/[^0-9a-f]/ui.test(arg), 'Input is not a hex string')
41-
return fromUint8Super(Buffer.from(arg, 'hex'), format)
43+
return fromTypedArray(Buffer.from(arg, 'hex'), format)
4244
}
4345

4446
if (!dehexArray) {
@@ -57,5 +59,5 @@ export function fromHex(arg, format = 'uint8') {
5759
arr[i] = a
5860
}
5961

60-
return fromUint8Super(arr, format)
62+
return fromTypedArray(arr, format)
6163
}

tests/convert.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromUint8Super, fromHex } from '@exodus/bytes/convert.js'
1+
import { fromTypedArray, fromHex } from '@exodus/bytes/convert.js'
22
import { describe, test } from 'node:test'
33

44
const raw = [new Uint8Array(), new Uint8Array([0]), new Uint8Array([1]), new Uint8Array([255])]
@@ -12,22 +12,22 @@ const pool = raw.map((uint8) => {
1212
return { uint8, buffer, hex: buffer.toString('hex') }
1313
})
1414

15-
describe('fromUint8Super', () => {
15+
describe('fromTypedArray', () => {
1616
test('invalid input', (t) => {
17-
for (const input of [null, undefined, [], [1,2], new Uint16Array(1), 'string']) {
18-
t.assert.throws(() => fromUint8Super(input))
17+
for (const input of [null, undefined, [], [1,2], 'string']) {
18+
t.assert.throws(() => fromTypedArray(input))
1919
for (const form of ['uint8', 'buffer', 'hex']) {
20-
t.assert.throws(() => fromUint8Super(input, form))
20+
t.assert.throws(() => fromTypedArray(input, form))
2121
}
2222
}
2323
})
2424

2525
test('uint8', (t) => {
2626
for (const { buffer, uint8 } of pool) {
27-
t.assert.strictEqual(fromUint8Super(uint8), uint8)
28-
t.assert.strictEqual(fromUint8Super(uint8, 'uint8'), uint8)
29-
const a = fromUint8Super(buffer)
30-
const b = fromUint8Super(buffer, 'uint8')
27+
t.assert.strictEqual(fromTypedArray(uint8), uint8)
28+
t.assert.strictEqual(fromTypedArray(uint8, 'uint8'), uint8)
29+
const a = fromTypedArray(buffer)
30+
const b = fromTypedArray(buffer, 'uint8')
3131
t.assert.deepStrictEqual(a, uint8)
3232
t.assert.strictEqual(a.buffer, buffer.buffer)
3333
t.assert.deepStrictEqual(b, uint8)
@@ -37,17 +37,17 @@ describe('fromUint8Super', () => {
3737

3838
test('buffer', (t) => {
3939
for (const { uint8, buffer } of pool) {
40-
t.assert.strictEqual(fromUint8Super(buffer, 'buffer'), buffer)
41-
const a = fromUint8Super(uint8, 'buffer')
40+
t.assert.strictEqual(fromTypedArray(buffer, 'buffer'), buffer)
41+
const a = fromTypedArray(uint8, 'buffer')
4242
t.assert.deepStrictEqual(a, buffer)
4343
t.assert.strictEqual(a.buffer, uint8.buffer)
4444
}
4545
})
4646

4747
test('hex', (t) => {
4848
for (const { uint8, buffer, hex} of pool) {
49-
t.assert.strictEqual(fromUint8Super(uint8, 'hex'), hex)
50-
t.assert.strictEqual(fromUint8Super(buffer, 'hex'), hex)
49+
t.assert.strictEqual(fromTypedArray(uint8, 'hex'), hex)
50+
t.assert.strictEqual(fromTypedArray(buffer, 'hex'), hex)
5151
}
5252
})
5353
})

0 commit comments

Comments
 (0)