Skip to content

Commit bfeb541

Browse files
committed
Add environment awareness in order to use crypto/sha.js when appropriate
1 parent b947a84 commit bfeb541

8 files changed

Lines changed: 36 additions & 3 deletions

File tree

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/apollo-env/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"dependencies": {
2323
"core-js": "3.0.0-beta.13",
24-
"node-fetch": "^2.2.0"
24+
"node-fetch": "^2.2.0",
25+
"sha.js": "^2.4.11"
2526
}
2627
}

packages/apollo-env/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import "./polyfills";
22
import "./typescript-utility-types";
33

44
export * from "../lib/fetch";
5+
export * from "./utils";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { isNode } from "./isNode";
2+
3+
export function createHash(kind: string): import("crypto").Hash {
4+
if (isNode) {
5+
// Use module.require instead of just require to avoid bundling whatever
6+
// crypto polyfills a non-Node bundler might fall back to.
7+
return module.require("crypto").createHash(kind);
8+
}
9+
return require("sha.js")(kind);
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./createHash";
2+
export * from "./isNode";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const isNode =
2+
typeof process === "object" &&
3+
process &&
4+
process.release &&
5+
process.release.name === "node" &&
6+
process.versions &&
7+
typeof process.versions.node === "string";

packages/apollo-graphql/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"node": ">=6"
1212
},
1313
"dependencies": {
14+
"apollo-env": "file:../apollo-env",
1415
"lodash.sortby": "^4.7.0"
1516
},
1617
"peerDependencies": {

packages/apollo-graphql/src/signature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
// the server no longer needs to parse the signature or run its own signature
4444
// algorithm on it, and the details of the signature algorithm are now up to the
4545
// reporting agent.
46-
import { createHash } from "crypto";
4746
import { DocumentNode } from "graphql";
47+
import { createHash } from "apollo-env";
4848
import {
4949
printWithReducedWhitespace,
5050
dropUnusedDefinitions,

0 commit comments

Comments
 (0)