deno/ext/node/polyfills/internal/crypto/_randomInt.ts
Garret Thompson 038d5a5331
Some checks are pending
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
fix(ext/node): avoid panic when crypto.randomInt has no arguments (#30314)
Closes https://github.com/denoland/deno/issues/30313
2025-08-05 17:09:39 +02:00

69 lines
1.6 KiB
TypeScript

// Copyright 2018-2025 the Deno authors. MIT license.
import { op_node_random_int } from "ext:core/ops";
import { primordials } from "ext:core/mod.js";
import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
const {
Error,
MathCeil,
MathFloor,
MathPow,
NumberIsSafeInteger,
RangeError,
} = primordials;
export default function randomInt(max: number): number;
export default function randomInt(min: number, max: number): number;
export default function randomInt(
max: number,
cb: (err: Error | null, n?: number) => void,
): void;
export default function randomInt(
min: number,
max: number,
cb: (err: Error | null, n?: number) => void,
): void;
export default function randomInt(
max: number,
min?: ((err: Error | null, n?: number) => void) | number,
cb?: (err: Error | null, n?: number) => void,
): number | void {
if (typeof max === "number" && typeof min === "number") {
const temp = max;
max = min;
min = temp;
}
if (min === undefined) min = 0;
else if (typeof min === "function") {
cb = min;
min = 0;
}
if (!NumberIsSafeInteger(min)) {
throw new ERR_INVALID_ARG_TYPE("min", "a safe integer", min);
}
if (!NumberIsSafeInteger(max)) {
throw new ERR_INVALID_ARG_TYPE("max", "a safe integer", max);
}
if (max - min > MathPow(2, 48)) {
throw new RangeError("max - min should be less than 2^48!");
}
if (min >= max) {
throw new Error("Min is bigger than Max!");
}
min = MathCeil(min);
max = MathFloor(max);
const result = op_node_random_int(min, max);
if (cb) {
cb(null, result);
return;
}
return result;
}