diff options
author | Mayank Kumar <103447831+mayank-Pareek@users.noreply.github.com> | 2024-10-26 13:42:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-26 19:42:14 +0200 |
commit | 793b155cd31bfc79e4aac4447bd250c9a6d31f6b (patch) | |
tree | 820b945b4be8511316db0234c26c38068cb08c20 /ext/node | |
parent | f0f476e58453d502ab5c118fc91d1475e6829967 (diff) |
fix(ext/node): use primordials in ext\node\polyfills\internal\crypto\_randomInt.ts (#26534)
Towards #24236
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/polyfills/internal/crypto/_randomInt.ts | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/ext/node/polyfills/internal/crypto/_randomInt.ts b/ext/node/polyfills/internal/crypto/_randomInt.ts index 7f4d703ad..e08b3e963 100644 --- a/ext/node/polyfills/internal/crypto/_randomInt.ts +++ b/ext/node/polyfills/internal/crypto/_randomInt.ts @@ -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) { |