()
| 64 | |
| 65 | // inspired by https://github.com/ryan-mars/ulid-workers/blob/06689d9ddd894bfc608c6131f9bbd2a8b48bcbc8/src/ulid.ts#L147 |
| 66 | export 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 |
| 84 | export const rawIdWithTime = (epochMs: number) => { |
no test coverage detected