MCPcopy Create free account
hub / github.com/AlexErrant/Pentive / idFactory

Function idFactory

shared/src/binary.ts:66–81  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

64
65// inspired by https://github.com/ryan-mars/ulid-workers/blob/06689d9ddd894bfc608c6131f9bbd2a8b48bcbc8/src/ulid.ts#L147
66export function idFactory() {
67 let lastTime = -1
68 const lastId = new Uint8Array(idLength)
69 return () => {
70 const epochMs = Date.now()
71 if (epochMs > lastTime) {
72 lastTime = epochMs
73 crypto.getRandomValues(lastId)
74 prefixEpochToArray(epochMs, lastId)
75 return new Uint8Array(lastId)
76 } else {
77 incrementRandom(lastId)
78 return new Uint8Array(lastId)
79 }
80 }
81}
82
83// separate from idFactory because it's only used by tests and adds unnecessary complexity/conditionals to idFactory
84export const rawIdWithTime = (epochMs: number) => {

Callers 2

binary.test.tsFile · 0.90
binary.tsFile · 0.85

Calls 2

prefixEpochToArrayFunction · 0.85
incrementRandomFunction · 0.85

Tested by

no test coverage detected