MCPcopy Index your code
hub / github.com/nodejs/node / randomFill

Function randomFill

lib/internal/crypto/random.js:154–196  ·  view source on GitHub ↗
(buf, offset, size, callback)

Source from the content-addressed store, hash-verified

152}
153
154function randomFill(buf, offset, size, callback) {
155 if (!isAnyArrayBuffer(buf) && !isArrayBufferView(buf)) {
156 throw new ERR_INVALID_ARG_TYPE(
157 'buf',
158 ['ArrayBuffer', 'ArrayBufferView'],
159 buf);
160 }
161
162 const elementSize = buf.BYTES_PER_ELEMENT || 1;
163
164 if (typeof offset === 'function') {
165 callback = offset;
166 offset = 0;
167 // Size is a length here, assertSize() call turns it into a number of bytes
168 size = buf.length;
169 } else if (typeof size === 'function') {
170 callback = size;
171 size = buf.length - offset;
172 } else {
173 validateFunction(callback, 'callback');
174 }
175
176 offset = assertOffset(offset, elementSize, buf.byteLength);
177
178 if (size === undefined) {
179 size = buf.byteLength - offset;
180 } else {
181 size = assertSize(size, elementSize, offset, buf.byteLength);
182 }
183
184 if (size === 0) {
185 callback(null, buf);
186 return;
187 }
188
189 const job = new RandomBytesJob(
190 kCryptoJobAsync,
191 buf,
192 offset,
193 size);
194 job.ondone = FunctionPrototypeBind(onJobDone, job, buf, callback);
195 job.run();
196}
197
198// Largest integer we can read from a buffer.
199// e.g.: Buffer.from("ff".repeat(6), "hex").readUIntBE(0, 6);

Callers 2

randomBytesFunction · 0.85

Calls 4

assertOffsetFunction · 0.85
assertSizeFunction · 0.70
callbackFunction · 0.50
runMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…