Skip to content
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

Merged
merged 5 commits into from Oct 6, 2022
Merged

Rework JS feature detection #284

merged 5 commits into from Oct 6, 2022

Conversation

josephlr
Copy link
Member

@josephlr josephlr commented Sep 13, 2022

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:

import {webcrypto} from 'crypto'
globalThis.crypto = webcrypto

as described in #256 (comment)

We also rename BroswerCrypto to WebCrypto 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 if module.require is not a function,

I think this fixes #256 about as well as we can.

Signed-off-by: Joe Richey joerichey@google.com

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>
Copy link
Member

@newpavlov newpavlov left a 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.

@josephlr
Copy link
Member Author

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.

Copy link

@maccesch maccesch left a 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.

@josephg
Copy link

josephg commented Sep 15, 2022

I left some comments on the related issue. In short, there's a lingering problem in wasm-pack's bundler mode which we could fix or ignore. Either way it looks good to me!

@newpavlov
Copy link
Member

@josephlr

The main feedback I was looking to get was mostly on the documentation, and error messages.

Both look good to me.

@maccesch
Copy link

maccesch commented Oct 2, 2022

Is this going to be merged soon? I could really use this in my project 🙂

@josephlr
Copy link
Member Author

josephlr commented Oct 6, 2022

Is this going to be merged soon? I could really use this in my project slightly_smiling_face

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>
@josephlr
Copy link
Member Author

josephlr commented Oct 6, 2022

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:

panicked at 'getrandom failed: Error { internal_code: 2147483662, description: "Node.js ES modules are not directly supported, see https://docs.rs/getrandom#nodejs-es-module-support" }', src/lib.rs:9:31

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.

@josephlr josephlr merged commit cfdad53 into master Oct 6, 2022
@josephlr josephlr deleted the webcrypto branch October 6, 2022 22:06
@maccesch
Copy link

maccesch commented Oct 6, 2022

Awesome! Is this going to be published as 0.2.8 ?

@josephlr
Copy link
Member Author

Awesome! Is this going to be published as 0.2.8 ?

See #294

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Javascript code does not work in ESM context
4 participants