Closed petebacondarwin closed 1 month ago
What's getRuntimeBytes()
? Did you mean crypto.getRandomValues()
?
Assuming so, it has the same behavior in Chrome:
To be more concrete. Using nodejs_compat
(not v2, so no polyfills) running this Worker:
import { getBuiltinModule } from "node:process";
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const results: string[] = [];
const nodeCrypto = getBuiltinModule("crypto");
const webcrypto = nodeCrypto.webcrypto;
const getRandomValues = webcrypto.getRandomValues;
results.push(crypto.getRandomValues(new Uint8Array(6)).toString()); // global
results.push(webcrypto.getRandomValues(new Uint8Array(6)).toString()); // webcrypto
results.push(nodeCrypto.getRandomValues(new Uint8Array(6)).toString()); // namespace import
results.push(getRandomValues(new Uint8Array(6)).toString()); // free standing
return Response.json(results);
},
};
Results in exceptions on these lines:
nodeCrypto.getRandomValues(new Uint8Array(6)).toString(); // namespace import
getRandomValues(new Uint8Array(6)).toString(); // free standing
I could accept that the bare getRandomValues()
call might not work. But I would expect the nodeCrypto.getRandomValues()
call to work, which is the point of this issue.
Also given that this is for Node.js compatibility, I tested it on vanilla Node.js (18):
> const crypto = require("crypto")
undefined
> crypto.getRandomValues(new Uint8Array(6))
Uint8Array(6) [ 56, 123, 166, 65, 220, 221 ]
> crypto.webcrypto.getRandomValues(new Uint8Array(6))
Uint8Array(6) [ 89, 169, 107, 187, 129, 83 ]
> const getRandomValues = crypto.getRandomValues
undefined
> getRandomValues(new Uint8Array(6))
Uint8Array(6) [ 82, 122, 19, 15, 222, 155 ]
The
getRandomBytes()
function will fail when called if it is not bound to thewebcrypto
object. See https://github.com/cloudflare/workers-sdk/pull/6721 for a test case. Currently the calls fail with:TypeError: Illegal invocation: function called with incorrect
thisreference.
.This has been fixed in unenv via https://github.com/unjs/unenv/pull/309 but I think that the
getRandomBytes()
function should not require any particularthis
to work.