1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

fix(ext/node): use primordials in ext\node\polyfills\internal\crypto\_randomInt.ts (#26534)

Towards #24236
This commit is contained in:
Mayank Kumar 2024-10-26 13:42:14 -04:00 committed by GitHub
parent f0f476e584
commit 793b155cd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,9 +1,15 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials
import { op_node_random_int } from "ext:core/ops";
import { primordials } from "ext:core/mod.js";
const {
Error,
MathCeil,
MathFloor,
MathPow,
NumberIsSafeInteger,
RangeError,
} = primordials;
export default function randomInt(max: number): number;
export default function randomInt(min: number, max: number): number;
@ -23,7 +29,9 @@ export default function randomInt(
cb?: (err: Error | null, n?: number) => void,
): number | void {
if (typeof max === "number" && typeof min === "number") {
[max, min] = [min, max];
const temp = max;
max = min;
min = temp;
}
if (min === undefined) min = 0;
else if (typeof min === "function") {
@ -32,13 +40,13 @@ export default function randomInt(
}
if (
!Number.isSafeInteger(min) ||
typeof max === "number" && !Number.isSafeInteger(max)
!NumberIsSafeInteger(min) ||
typeof max === "number" && !NumberIsSafeInteger(max)
) {
throw new Error("max or min is not a Safe Number");
}
if (max - min > Math.pow(2, 48)) {
if (max - min > MathPow(2, 48)) {
throw new RangeError("max - min should be less than 2^48!");
}
@ -46,8 +54,8 @@ export default function randomInt(
throw new Error("Min is bigger than Max!");
}
min = Math.ceil(min);
max = Math.floor(max);
min = MathCeil(min);
max = MathFloor(max);
const result = op_node_random_int(min, max);
if (cb) {