11 let original: undefined | typeof crypto.getRandomValues;
14 * Mock crypto.getRandomValues using the mocked implementation (if given). Otherwise,
15 * a deterministic function will fill the buffer with consecutive values from 0 up to 254.
17 export const initRandomMock = <T extends TypedArray>(mockedImplementation?: (buf: T) => T) => {
19 original = crypto.getRandomValues;
22 if (mockedImplementation) {
23 // @ts-ignore DataView not supported
24 crypto.getRandomValues = mockedImplementation;
26 const staticMockRandomValues = (buf: TypedArray) => {
27 for (let i = 0; i < Math.min(buf.length, 255); ++i) {
28 // values wrap around automatically based on the provided typed array
33 // @ts-ignore DataView not supported
34 crypto.getRandomValues = staticMockRandomValues;
38 export const disableRandomMock = () => {
40 throw new Error('mock was not initialized');
42 crypto.getRandomValues = original;