-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crypto: expose Web Crypto API under crypto/web #40462
Conversation
Personally I think it should be exported as |
1204ece
to
2972653
Compare
I personally would probably just deprecate the |
f5c5e35
to
703c072
Compare
703c072
to
2cc9d50
Compare
By the way, why not export directly the content of webcrypto in Currently, this is not really practical in my opinion in ESM in addition to not being consistent with other builtins exports. import { crypto } from 'crypto/web',
const { ... } = crypto;
Yes but most builtins export their submodules ( |
Oups, sorry |
if you mean why not using a "default" export, it would make it infeasible for exporting any additional functionality under that name if needed. if you mean the
the difference is that the |
cc @jasnell |
|
||
const { ObjectDefineProperty } = primordials; | ||
|
||
ObjectDefineProperty(module.exports, 'crypto', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would more expect it to directly export the crypto object...
import { subtle, randomUUID } from 'crypto/web'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my initial thought was the same, but I thought it would make it harder to export any additional (future) functionality under this export since it's a web api with any potential future additions.
named imports such as import { subtle, randomUUID } from 'crypto/web'
would (should) not work, as those methods sit on the prototype of crypto
, not on the module namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a developer I do not have the expectation of finding Web APIs in ${module}/web
and so I feel this addition doesn't do much.
This also rolls back on the use of import()
function in ESM examples previously discussed and added 1.
I think we should be thinking of advancing WebCrypto forward in terms of
deciding whether to keep all the proprietary algs or not 2(done)fixing or documenting (non)-conformance 3(done)- graduating from experimental status 4
- exposing crypto as a global for universal javascript libraries to use 5
Footnotes
-
https://github.com/nodejs/node/pull/37594#issuecomment-794021447 ↩
-
https://github.com/nodejs/node/pull/37969#issuecomment-810019310 ↩
-
Web Cryptography API compliance wrt. key import/export #39959 ↩
-
webcrypto: graduate from experimental, expose via
crypto
#37969 ↩ -
https://github.com/nodejs/node/pull/37969#issuecomment-809732176 ↩
I'd prefer to expose it as a global, like it is on browsers. However, until this happens,
In general web APIs don't let you destructure them and would throw |
Not just browsers, WinterCG compatible runtimes as well. Anyway, I agree.
What would be the improvement over this which works already? import { webcrypto as crypto } from 'node:crypto'; |
With |
this PR makes the
Web Crypto API
easier accessible undercrypto/web
, similar tofs/promises
and also consistent withstream/web
. I think it's also better and consistent with the naming ofcrypto
whennode.js
makes it globally accessible, otherwise I think it would add confusion.currently still missing:
Regarding the current docs, the Web Crypto API docs don't show examples for
cjs
andesm
as the other modules do. I'm wondering if the examples for cjs should really use the destructuring syntax, as the functionality is sitting on the prototype ofcrypto
(subtle, randomUUID, getRandomValues).I took the liberty to change the docs from:
to:
as opposed to: