Closed simylein closed 10 months ago
headers also https://github.com/oven-sh/bun/issues/8081
😢
Node.js crypto
is essentially not capable of being polyfilled - because Node.js uses an internal implementation. Use webcrypto
property of node:crypto
for subtle
.
[AskJS] Why is the internal crypto module so difficult to bundle as a standalone portable script?
The crypto module is not entirely Javascript. Some of its implementation uses native code which only runs in the nodejs environment and some of its implementation uses nodejs internals (like the thread pool).
This
import { createHash, getRandomValues, subtle } from 'crypto';
should be something like
import * as crypto from "node:crypto";
const { webcrypto } = crypto;
const { subtle } = webcrypto;
FYI node
's KeyObject
https://nodejs.org/api/crypto.html#class-keyobject is not the same as Web Cryptography API's CryptoKey
, https://w3c.github.io/webcrypto/#cryptokey-interface. E.g., cf. https://github.com/WICG/webpackage/tree/main/js to https://github.com/guest271314/wbn-sign-webcrypto.
Oh right, thank you for the explanation. Then I guess it's a skill issue 😉. I think I can close this for now?
I would substitute Web Cryptography API for Node.js crypto
. Might be a tedious effort, though possible, and once completed you should be able to run the same code in Bun, Deno, and Node.js.
What version of Bun is running?
1.0.22+b400b36ca
What platform is your computer?
Darwin 23.2.0 arm64 arm
What steps can reproduce the bug?
What is the expected behavior?
with
@types/bun@1.0.0
type checking succeeds without failuresWhat do you see instead?
with
@types/bun@1.0.1
type checking fails withArgument of type 'import("crypto").webcrypto.CryptoKey' is not assignable to parameter of type 'CryptoKey'. Property 'prototype' is missing in type 'import("crypto").webcrypto.CryptoKey' but required in type 'CryptoKey'.ts(2345) lib.dom.d.ts(5887, 5): 'prototype' is declared here.
Argument of type 'import("crypto").webcrypto.CryptoKey' is not assignable to parameter of type 'CryptoKey'.ts(2345)
Additional information
The runtime works fine, it's just the type definition, can someone add the
typescript
label?