(randomBytes: (size: number) => Uint8Array)
| 220 | * @since 4.0.0 |
| 221 | */ |
| 222 | export const make = (randomBytes: (size: number) => Uint8Array): SecureRandom["Service"] => { |
| 223 | const randomBytesChecked = (size: number): Uint8Array => { |
| 224 | checkSize(size) |
| 225 | return randomBytes(size) |
| 226 | } |
| 227 | const draw = () => randomBytes(7) |
| 228 | const uuidV7 = Uuid.makeV7Monotonic() |
| 229 | const ulid = Ulid.makeMonotonic() |
| 230 | const ulidRandomBytes = () => randomBytes(10) |
| 231 | return { |
| 232 | randomBytes: (size) => Effect.sync(() => randomBytesChecked(size)), |
| 233 | intBetween: (min, max, options) => Effect.sync(() => intBetweenUnsafe(min, max, options?.halfOpen ?? false, draw)), |
| 234 | shuffle: <A>(elements: Iterable<A>) => |
| 235 | Effect.sync(() => { |
| 236 | const buffer = Array.from(elements) |
| 237 | for (let i = buffer.length - 1; i >= 1; i--) { |
| 238 | const index = intBetweenUnsafe(0, i, false, draw) |
| 239 | const value = buffer[i]! |
| 240 | buffer[i] = buffer[index]! |
| 241 | buffer[index] = value |
| 242 | } |
| 243 | return buffer |
| 244 | }), |
| 245 | uuidV4: |
| 246 | ((options?: { readonly format?: "string" | "bytes" | undefined }) => |
| 247 | Effect.sync(() => makeUuid(Uuid.v4Bytes, randomBytes, options?.format))) as UuidGenerator, |
| 248 | uuidV7: |
| 249 | ((options?: { readonly format?: "string" | "bytes" | undefined }) => |
| 250 | Effect.clockWith((clock) => |
| 251 | Effect.sync(() => |
| 252 | makeUuid((bytes) => uuidV7(clock.currentTimeMillisUnsafe(), bytes), randomBytes, options?.format) |
| 253 | ) |
| 254 | )) as UuidGenerator, |
| 255 | ulid: Effect.clockWith((clock) => Effect.sync(() => ulid(clock.currentTimeMillisUnsafe(), ulidRandomBytes))) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // `getRandomValues` throws a `QuotaExceededError` above 65,536 bytes. |
| 260 | const fillRandom = (crypto: Crypto, size: number): Uint8Array => { |
no test coverage detected