-
Notifications
You must be signed in to change notification settings - Fork 182
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
Rework JS feature detection #284
Conversation
Now we look for the standard Web Cryptography API before attempting to check for Node.js support. This allows Node.js ES6 module users to add a polyfill like: ```js import {webcrypto} from 'crypto' globalThis.crypto = webcrypto ``` as described in #256 (comment) Signed-off-by: Joe Richey <joerichey@google.com>
This allows users to get an actionable error message about this particular problem. We also add detection for this problem. Signed-off-by: Joe Richey <joerichey@google.com>
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.
Looks fine to me, but note that I am not familiar enough with the JS targets to judge quality of this solution.
I'm not so sure about the implementation quality myself, I did try changing around some of the names of functions to make sure errors occurred when they "should". The main feedback I was looking to get was mostly on the documentation, and error messages. |
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'm not an expert for this either but from what I can see this looks like a nice solution.
I left some comments on the related issue. In short, there's a lingering problem in wasm-pack's |
Both look good to me. |
Is this going to be merged soon? I could really use this in my project 🙂 |
Yes, I just need to run a few more checks that we are correctly handling errors when in a Node.js ES module. |
Signed-off-by: Joe Richey <joerichey@google.com>
This call throws an exception if module isn't defined. Signed-off-by: Joe Richey <joerichey@google.com>
Signed-off-by: Joe Richey <joerichey@google.com>
I added e0c93b1 which makes the error @josephg pointed out in #256 (comment) much nicer. Now, if we try to use an ES module from Node.js, we get an error like:
Then if we apply the workaround in our docs, everything works!! This should hopefully allow users to (a) figure out what's going wrong, and (b) figure out how to fix the error. |
Awesome! Is this going to be published as 0.2.8 ? |
See #294 |
This PR tries to incorporate some of the suggestions from #256
The first commit changes our detection logic to first look for the Web Cryptography API before attempting to check for Node.js support. This allows Node.js ES6 module users to add a polyfill like:
as described in #256 (comment)
We also rename
BroswerCrypto
toWebCrypto
and update some of the outdated error messages and package comments.The Second commit adds better detection for why we failed to call
module.require("crypto")
, returning a different error code ifmodule.require
is not a function,I think this fixes #256 about as well as we can.
Signed-off-by: Joe Richey joerichey@google.com