diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-09-23 13:34:55 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-23 10:04:55 +0200 |
commit | 75a724890d94267a02bd431f98d3d7d5866d95e7 (patch) | |
tree | eff10e783aaeb9e4dca6af76e1872ed877413d78 /ext/node | |
parent | 365d1ac7c2c6e81cbc32135ed6a099f3caaa6110 (diff) |
fix(node): supported arguments to `randomFillSync` (#20637)
Fixes https://github.com/denoland/deno/issues/20634
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/lib.rs | 2 | ||||
-rw-r--r-- | ext/node/polyfills/internal/crypto/_randomFill.mjs | 90 | ||||
-rw-r--r-- | ext/node/polyfills/internal/crypto/_randomFill.ts | 94 | ||||
-rw-r--r-- | ext/node/polyfills/internal/crypto/random.ts | 4 |
4 files changed, 93 insertions, 97 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs index fbc1c9ffd..c7ca2ca72 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -388,7 +388,7 @@ deno_core::extension!(deno_node, "internal/constants.ts", "internal/crypto/_keys.ts", "internal/crypto/_randomBytes.ts", - "internal/crypto/_randomFill.ts", + "internal/crypto/_randomFill.mjs", "internal/crypto/_randomInt.ts", "internal/crypto/certificate.ts", "internal/crypto/cipher.ts", diff --git a/ext/node/polyfills/internal/crypto/_randomFill.mjs b/ext/node/polyfills/internal/crypto/_randomFill.mjs new file mode 100644 index 000000000..6afc654b4 --- /dev/null +++ b/ext/node/polyfills/internal/crypto/_randomFill.mjs @@ -0,0 +1,90 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +// TODO(petamoriken): enable prefer-primordials for node polyfills +// deno-lint-ignore-file prefer-primordials + +import { + MAX_SIZE as kMaxUint32, +} from "ext:deno_node/internal/crypto/_randomBytes.ts"; +import { Buffer } from "node:buffer"; +import { isAnyArrayBuffer, isArrayBufferView } from "node:util/types"; +import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts"; +const { core } = globalThis.__bootstrap; +const { ops } = core; + +const kBufferMaxLength = 0x7fffffff; + +function assertOffset(offset, length) { + if (offset > kMaxUint32 || offset < 0) { + throw new TypeError("offset must be a uint32"); + } + + if (offset > kBufferMaxLength || offset > length) { + throw new RangeError("offset out of range"); + } +} + +function assertSize(size, offset, length) { + if (size > kMaxUint32 || size < 0) { + throw new TypeError("size must be a uint32"); + } + + if (size + offset > length || size > kBufferMaxLength) { + throw new RangeError("buffer too small"); + } +} + +export default function randomFill( + buf, + offset, + size, + cb, +) { + if (typeof offset === "function") { + cb = offset; + offset = 0; + size = buf.length; + } else if (typeof size === "function") { + cb = size; + size = buf.length - Number(offset); + } + + assertOffset(offset, buf.length); + assertSize(size, offset, buf.length); + + core.opAsync("op_node_generate_secret_async", Math.floor(size)) + .then( + (randomData) => { + const randomBuf = Buffer.from(randomData.buffer); + randomBuf.copy(buf, offset, 0, size); + cb(null, buf); + }, + ); +} + +export function randomFillSync(buf, offset = 0, size) { + if (!isAnyArrayBuffer(buf) && !isArrayBufferView(buf)) { + throw new ERR_INVALID_ARG_TYPE( + "buf", + ["ArrayBuffer", "ArrayBufferView"], + buf, + ); + } + + assertOffset(offset, buf.byteLength); + + if (size === undefined) { + size = buf.byteLength - offset; + } else { + assertSize(size, offset, buf.byteLength); + } + + if (size === 0) { + return buf; + } + + const bytes = new Uint8Array(buf.buffer ? buf.buffer : buf, offset, size); + ops.op_node_generate_secret(bytes); + + return buf; +} diff --git a/ext/node/polyfills/internal/crypto/_randomFill.ts b/ext/node/polyfills/internal/crypto/_randomFill.ts deleted file mode 100644 index 927acaf8d..000000000 --- a/ext/node/polyfills/internal/crypto/_randomFill.ts +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. - -// TODO(petamoriken): enable prefer-primordials for node polyfills -// deno-lint-ignore-file prefer-primordials - -import { - MAX_SIZE as kMaxUint32, -} from "ext:deno_node/internal/crypto/_randomBytes.ts"; -import { Buffer } from "node:buffer"; -const { core } = globalThis.__bootstrap; -const { ops } = core; - -const kBufferMaxLength = 0x7fffffff; - -function assertOffset(offset: number, length: number) { - if (offset > kMaxUint32 || offset < 0) { - throw new TypeError("offset must be a uint32"); - } - - if (offset > kBufferMaxLength || offset > length) { - throw new RangeError("offset out of range"); - } -} - -function assertSize(size: number, offset: number, length: number) { - if (size > kMaxUint32 || size < 0) { - throw new TypeError("size must be a uint32"); - } - - if (size + offset > length || size > kBufferMaxLength) { - throw new RangeError("buffer too small"); - } -} - -export default function randomFill( - buf: Buffer, - cb: (err: Error | null, buf: Buffer) => void, -): void; - -export default function randomFill( - buf: Buffer, - offset: number, - cb: (err: Error | null, buf: Buffer) => void, -): void; - -export default function randomFill( - buf: Buffer, - offset: number, - size: number, - cb: (err: Error | null, buf: Buffer) => void, -): void; - -export default function randomFill( - buf: Buffer, - offset?: number | ((err: Error | null, buf: Buffer) => void), - size?: number | ((err: Error | null, buf: Buffer) => void), - cb?: (err: Error | null, buf: Buffer) => void, -) { - if (typeof offset === "function") { - cb = offset; - offset = 0; - size = buf.length; - } else if (typeof size === "function") { - cb = size; - size = buf.length - Number(offset as number); - } - - assertOffset(offset as number, buf.length); - assertSize(size as number, offset as number, buf.length); - - core.opAsync("op_node_generate_secret_async", Math.floor(size as number)) - .then( - (randomData: Uint8Array) => { - const randomBuf = Buffer.from(randomData.buffer); - randomBuf.copy(buf, offset as number, 0, size as number); - cb!(null, buf); - }, - ); -} - -export function randomFillSync(buf: Buffer, offset = 0, size?: number) { - assertOffset(offset, buf.length); - - if (size === undefined) size = buf.length - offset; - - assertSize(size, offset, buf.length); - - const bytes: Uint8Array = new Uint8Array(Math.floor(size)); - ops.op_node_generate_secret(bytes); - const bytesBuf: Buffer = Buffer.from(bytes.buffer); - bytesBuf.copy(buf, offset, 0, size); - - return buf; -} diff --git a/ext/node/polyfills/internal/crypto/random.ts b/ext/node/polyfills/internal/crypto/random.ts index a02d232e8..e8776cdf0 100644 --- a/ext/node/polyfills/internal/crypto/random.ts +++ b/ext/node/polyfills/internal/crypto/random.ts @@ -8,7 +8,7 @@ import { notImplemented } from "ext:deno_node/_utils.ts"; import randomBytes from "ext:deno_node/internal/crypto/_randomBytes.ts"; import randomFill, { randomFillSync, -} from "ext:deno_node/internal/crypto/_randomFill.ts"; +} from "ext:deno_node/internal/crypto/_randomFill.mjs"; import randomInt from "ext:deno_node/internal/crypto/_randomInt.ts"; import { validateBoolean, @@ -29,7 +29,7 @@ export { default as randomBytes } from "ext:deno_node/internal/crypto/_randomByt export { default as randomFill, randomFillSync, -} from "ext:deno_node/internal/crypto/_randomFill.ts"; +} from "ext:deno_node/internal/crypto/_randomFill.mjs"; export { default as randomInt } from "ext:deno_node/internal/crypto/_randomInt.ts"; const primordials = globalThis.__bootstrap.primordials; |