mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 16:49:18 -05:00
fix(ext/node): use primordials in ext\node\polyfills\internal\crypto\_randomInt.ts (#26534)
Towards #24236
This commit is contained in:
parent
f0f476e584
commit
793b155cd3
1 changed files with 17 additions and 9 deletions
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue