Skip to content

Commit 38f75f9

Browse files
committed
Refactor
Consolidate puid config checks
1 parent 5be930e commit 38f75f9

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

src/lib/bits.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
22-
import crypto from 'crypto'
23-
2422
import encoder from './encoder'
2523
import { entropyBitsPerChar } from './entropy'
2624

@@ -140,22 +138,7 @@ const fillEntropy = (entropyBits: EntropyBits, entropyFunction: EntropyFunction)
140138
}
141139
}
142140

143-
const selectEntropyFunction = (puidConfig: PuidConfig): EntropyFunction => {
144-
if (puidConfig.entropyValues) {
145-
return [true, puidConfig.entropyValues as EntropyByValues]
146-
} else if (puidConfig.entropyBytes) {
147-
return [false, puidConfig.entropyBytes as EntropyByBytes]
148-
} else {
149-
return [false, crypto.randomBytes]
150-
// [true, crypto.getRandomValues]
151-
}
152-
}
153-
154-
export default (puidLen: number, puidChars: string, puidConfig: PuidConfig): PuidBitsMuncherResult => {
155-
if (puidConfig.entropyBytes && puidConfig.entropyValues) {
156-
return { error: new Error('Cannot specify both entropyBytes and entropyValues functions') }
157-
}
158-
141+
export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunction): PuidBitsMuncherResult => {
159142
const nBitsPerChar = bitsPerChar(puidChars)
160143
const nBitsPerPuid = nBitsPerChar * puidLen
161144
const nBytesPerPuid = ceil(nBitsPerPuid / 8)
@@ -164,12 +147,13 @@ export default (puidLen: number, puidChars: string, puidConfig: PuidConfig): Pui
164147
const entropyBuffer = new ArrayBuffer(bufferLen)
165148
const entropyBits: EntropyBits = [8 * bufferLen, entropyBuffer]
166149
const entropyBytes = new Uint8Array(entropyBuffer)
167-
const entropyFunction = selectEntropyFunction(puidConfig)
168150

169151
const charsEncoder = encoder(puidChars)
170152
const puidEncoded = (value: number) => String.fromCharCode(charsEncoder(value))
171153

172-
if (isPow2(puidChars.length)) {
154+
const nChars = puidChars.length
155+
156+
if (isPow2(nChars)) {
173157
// When chars count is a power of 2, sliced bits always yield a valid char
174158
const mapper = new Array(puidLen).fill(0).map((zero, ndx) => zero + ndx)
175159

@@ -187,7 +171,7 @@ export default (puidLen: number, puidChars: string, puidConfig: PuidConfig): Pui
187171
return { success: bitsMuncher }
188172
}
189173

190-
const nChars = puidChars.length
174+
191175
const nEntropyBits = 8 * entropyBytes.length
192176
const puidShifts = bitShifts(puidChars)
193177

src/lib/puid.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
import crypto from 'crypto'
24+
2325
import muncher from './bits'
2426
import { Chars, charsName, validChars } from './chars'
2527
import { entropyBits, entropyBitsPerChar } from './entropy'
@@ -28,6 +30,17 @@ const round2 = (f: number): number => round(f * 100) / 100
2830

2931
const { ceil, round } = Math
3032

33+
const selectEntropyFunction = (puidConfig: PuidConfig): EntropyFunction => {
34+
if (puidConfig.entropyValues) {
35+
return [true, puidConfig.entropyValues as EntropyByValues]
36+
} else if (puidConfig.entropyBytes) {
37+
return [false, puidConfig.entropyBytes as EntropyByBytes]
38+
} else {
39+
return [false, crypto.randomBytes]
40+
// [true, crypto.getRandomValues]
41+
}
42+
}
43+
3144
/**
3245
* Create `puid` generating function
3346
*
@@ -88,6 +101,10 @@ export default (puidConfig: PuidConfig = {}): PuidResult => {
88101
if (!puidConfig.total && puidConfig.risk) return { error: new Error('Total and risk must be specified together') }
89102
if (puidConfig.total && puidConfig.bits) return { error: new Error('Cannot specify both total/risk and bits') }
90103

104+
if (puidConfig.entropyBytes && puidConfig.entropyValues) {
105+
return { error: new Error('Cannot specify both entropyBytes and entropyValues functions') }
106+
}
107+
91108
const puidEntropyBits =
92109
puidConfig.total && puidConfig.risk
93110
? entropyBits(puidConfig.total, puidConfig.risk)
@@ -96,7 +113,7 @@ export default (puidConfig: PuidConfig = {}): PuidResult => {
96113
const puidLen = round(ceil(puidEntropyBits / puidBitsPerChar))
97114
const ere = (puidBitsPerChar * puidChars.length) / (8 * Buffer.byteLength(puidChars))
98115

99-
const { error: bitsMuncherError, success: bitsMuncher } = muncher(puidLen, puidChars, puidConfig)
116+
const { error: bitsMuncherError, success: bitsMuncher } = muncher(puidLen, puidChars, selectEntropyFunction(puidConfig))
100117
if (bitsMuncherError) return { error: bitsMuncherError }
101118

102119
const puid: Puid = (): string => bitsMuncher()

0 commit comments

Comments
 (0)