-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
fix(amazon-cognito-identity-js): set crypto for Node #7136
Conversation
Codecov Report
@@ Coverage Diff @@
## main #7136 +/- ##
=======================================
Coverage 72.95% 72.95%
=======================================
Files 213 213
Lines 13302 13302
Branches 2606 2510 -96
=======================================
Hits 9705 9705
- Misses 3401 3432 +31
+ Partials 196 165 -31
Continue to review full report at Codecov.
|
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.
LGTM 🌮 NIT: can we add an additional test for this?
externals: { | ||
crypto: '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.
This is needed for the build:umd
to successfully build due to require('crypto')
. However, I'm not even sure if the UMD build is even used anymore since webpack outputs the build to dist
and we have "main": "lib/index.js"
in package.json
. With that said, removing the UMD build is out of scope for this PR.
// Native crypto import via require (NodeJS) | ||
if (!crypto && typeof require === 'function') { | ||
try { | ||
crypto = require('crypto'); | ||
} catch (err) {} | ||
} | ||
|
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.
This is based on how crypto-js
handles this case: https://github.com/brix/crypto-js/blob/develop/src/core.js#L25-L30
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 good to me! 🌮 🎉 🥇
Thanks @amhinson
* Set crypto for node * add crypto to webpack externals * add unit tests for crypto
guys, why crypto instead of crypto-js ? |
This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs. Looking for a help forum? We recommend joining the Amplify Community Discord server |
Issue #, if available:
#7098
Description of changes:
This allows
amazon-cognito-identity-js
to run in the Node environment without having to explicitly addglobal.crypto = require('crypto')
in your code.I tested this in React Native, Expo, React, & Node. Normally, using a dynamic
require
like this causes problems with React Native, but since this only exists incryptoSecureRandomInt.js
and notcryptoSecureRandomInt.native.js
, Metro bundler doesn't evaluate this particular file.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.