Avoid importing entire crypto dependency tree if not in Node.js. #2304
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
apollo-server-core
package uses Node's built-incrypto
module only to create SHA-256 and -512 hashes.When we're actually running in Node, the native
crypto
library is clearly the best way to create these hashes, not least because we can assume it will be available without having to bundle it first.Outside of Node (such as in React Native apps), bundlers tend to fall back on the
crypto-browserify
polyfill, which comprises more than a hundred separate modules. Importing this polyfill at runtime (likely during application startup) takes precious time and memory, even though almost all of it is unused.Since we only need to create SHA hashes, we can import the much smaller
sha.js
library in non-Node environments, which happens to be whatcrypto-browserify
uses for SHA hashing.