Skip to content

Commit 37350d0

Browse files
committed
types: narrow EntropyFunction to discriminated union
1 parent 1876e56 commit 37350d0

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

src/lib/bits.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ const fillEntropy = (entropyOffset: number, entropyBuffer: ArrayBuffer, entropyF
8888
const nEntropyBytes = entropyBytes.length
8989
const nEntropyBits = 8 * nEntropyBytes
9090

91-
const [byValues, entropySource] = entropyFunction
91+
const { byValues, source } = entropyFunction
9292

9393
if (entropyOffset === nEntropyBits) {
9494
// No carry
9595
if (byValues) {
96-
entropyByValues(0, entropyBuffer, entropySource as EntropyByValues)
96+
entropyByValues(0, entropyBuffer, source)
9797
} else {
98-
entropyByBytes(0, entropyBuffer, entropySource as EntropyByBytes)
98+
entropyByBytes(0, entropyBuffer, source)
9999
}
100100
} else {
101101
// Handle carry
@@ -110,9 +110,9 @@ const fillEntropy = (entropyOffset: number, entropyBuffer: ArrayBuffer, entropyF
110110

111111
// Fill right bytes with new random values
112112
if (byValues) {
113-
entropyByValues(nUnusedBytes, entropyBuffer, entropySource as EntropyByValues)
113+
entropyByValues(nUnusedBytes, entropyBuffer, source)
114114
} else {
115-
entropyByBytes(nUnusedBytes, entropyBuffer, entropySource as EntropyByBytes)
115+
entropyByBytes(nUnusedBytes, entropyBuffer, source)
116116
}
117117
}
118118

src/lib/puid.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { randomBytes } from 'node:crypto'
22

3-
import { EntropyByBytes, EntropyByValues, EntropyFunction, Puid, PuidConfig, PuidResult } from '../types/puid'
3+
import { EntropyFunction, Puid, PuidConfig, PuidResult } from '../types/puid'
44

55
import muncher from './bits'
66
import { Chars, charsName, validChars } from './chars'
@@ -12,17 +12,17 @@ const round2 = (f: number): number => round(f * 100) / 100
1212
const { ceil, round } = Math
1313

1414
const selectEntropyFunction = (puidConfig: PuidConfig): EntropyFunction => {
15-
if (puidConfig.entropyValues) return [true, puidConfig.entropyValues as EntropyByValues]
16-
if (puidConfig.entropyBytes) return [false, puidConfig.entropyBytes as EntropyByBytes]
15+
if (puidConfig.entropyValues) return { byValues: true, source: puidConfig.entropyValues }
16+
if (puidConfig.entropyBytes) return { byValues: false, source: puidConfig.entropyBytes }
1717

1818
// Prefer Web Crypto in environments where it's available
1919
type CryptoLike = { getRandomValues?: (b: Uint8Array) => void }
2020
const cryptoObj = (globalThis as { crypto?: CryptoLike }).crypto
2121
const gv = cryptoObj?.getRandomValues?.bind(cryptoObj) as ((b: Uint8Array) => void) | undefined
22-
if (gv) return [true, gv]
22+
if (gv) return { byValues: true, source: gv }
2323

2424
// Fallback to Node's randomBytes
25-
return [false, randomBytes]
25+
return { byValues: false, source: randomBytes }
2626
}
2727

2828
/**

src/lib/puid.web.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EntropyByBytes, EntropyByValues, EntropyFunction, Puid, PuidConfig, PuidResult } from '../types/puid'
1+
import { EntropyFunction, Puid, PuidConfig, PuidResult } from '../types/puid'
22

33
import muncher from './bits'
44
import { Chars, charsName, validChars } from './chars'
@@ -10,13 +10,13 @@ const round2 = (f: number): number => round(f * 100) / 100
1010
const { ceil, round } = Math
1111

1212
const selectEntropyFunction = (puidConfig: PuidConfig): EntropyFunction => {
13-
if (puidConfig.entropyValues) return [true, puidConfig.entropyValues as EntropyByValues]
14-
if (puidConfig.entropyBytes) return [false, puidConfig.entropyBytes as EntropyByBytes]
13+
if (puidConfig.entropyValues) return { byValues: true, source: puidConfig.entropyValues }
14+
if (puidConfig.entropyBytes) return { byValues: false, source: puidConfig.entropyBytes }
1515

1616
type CryptoLike = { getRandomValues?: (b: Uint8Array) => void }
1717
const cryptoObj = (globalThis as { crypto?: CryptoLike }).crypto
1818
const gv = cryptoObj?.getRandomValues?.bind(cryptoObj) as ((b: Uint8Array) => void) | undefined
19-
if (gv) return [true, gv]
19+
if (gv) return { byValues: true, source: gv }
2020

2121
// No Node randomBytes fallback in web entry
2222
throw new Error('No entropy source available: provide entropyValues or ensure Web Crypto is available')

src/types/puid.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export type PuidBitsSlicer = (puidBits: PuidBits) => number[]
2525
export type PuidEncoder = (n: number) => number
2626

2727
export type EntropySource = EntropyByBytes | EntropyByValues
28-
export type EntropyFunction = [byValues: boolean, source: EntropySource]
28+
export type EntropyFunction =
29+
| { byValues: true; source: EntropyByValues }
30+
| { byValues: false; source: EntropyByBytes }
2931

3032
export type PuidInfo = {
3133
readonly bits: number

0 commit comments

Comments
 (0)