-
Notifications
You must be signed in to change notification settings - Fork 14
"browser" field in package.json #57
Comments
I published a really small shim as a workaround until browsers are supported here. I do have a few questions: What are the supported browsers? Note: IE11 doesn't support elliptic curve or SHA512-based algos What's the testing strategy? |
There was never any intention to use this directly in browsers. Most browsers already support W3C Web Cryptography API sufficiently for our purposes (excepting algs like Ed25519, etc. which is apparently coming in 2.0). Where they don't, shims exist? It's Node.js' maintainers that (to the detriment of the ecosystem, in my opinion) declined to consider implementing the standard. We wrote this package so that we could write webcrypto dependent code that runs both in the browser and on the server. In particular for packages such as these: https://github.com/anvilresearch/jose The support for webpack and browsers vs node is handled there ^, not in this package. There is apparently a comprehensive official W3C test suite for browsers (tightly coupled to that environment) and we've considered porting it here. Bandwidth is currently an issue. |
Just to make my proposal very clear, I do not intend to run any of the code here in the browser. What I want is to have a few packages intended for publishing to npm to be able to function both in browsers that have WebCrypto support and on Node.js. Currently I would have to do something like this: let webcrypto
if (typeof window === 'object' && typeof window.crypto === 'object') {
webcrypto = window.crypto
} else {
webcrypto = require('webcrypto')
}
// ... and webpack/browserify would still probably not be able to understand that the What I want to be able to do is to just write diff --git a/browser.js b/browser.js
new file mode 100644
index 0000000..a836487
--- /dev/null
+++ b/browser.js
@@ -0,0 +1 @@
+module.export = window.crypto
diff --git a/package.json b/package.json
index 9cdaaf6..33d1ca8 100644
--- a/package.json
+++ b/package.json
@@ -3,6 +3,7 @@
"version": "0.8.3",
"description": "WebCrypto API for Node.js",
"main": "src/index.js",
+ "browser": "browser.js",
"directories": {
"test": "test"
}, This will instruct browserify and webpack that this module is not intended for the browser, and that the result of requiring this module inside of the browser should just be I hope this clears up this issue, the intention is not to try and port anything to the browser. But merely adding a small hint to bundlers that "hey, this package is only for Node.js, use |
How about adding a
"browser"
field to the package.json, and a small browser shim that simply re-exportswindow.crypto
? That way you could write code that works in Node.js, and also seamlessly works with webpack/browserify/rollup in the browser.I would be happy to submit a PR 🚀
The text was updated successfully, but these errors were encountered: