Skip to content

Commit

Permalink
Add environment awareness in order to use crypto/sha.js when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Feb 27, 2019
1 parent b947a84 commit bfeb541
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/apollo-env/package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"core-js": "3.0.0-beta.13",
"node-fetch": "^2.2.0"
"node-fetch": "^2.2.0",
"sha.js": "^2.4.11"
}
}
1 change: 1 addition & 0 deletions packages/apollo-env/src/index.ts
Expand Up @@ -2,3 +2,4 @@ import "./polyfills";
import "./typescript-utility-types";

export * from "../lib/fetch";
export * from "./utils";
10 changes: 10 additions & 0 deletions packages/apollo-env/src/utils/createHash.ts
@@ -0,0 +1,10 @@
import { isNode } from "./isNode";

export function createHash(kind: string): import("crypto").Hash {
if (isNode) {
// Use module.require instead of just require to avoid bundling whatever
// crypto polyfills a non-Node bundler might fall back to.
return module.require("crypto").createHash(kind);
}
return require("sha.js")(kind);
}
2 changes: 2 additions & 0 deletions packages/apollo-env/src/utils/index.ts
@@ -0,0 +1,2 @@
export * from "./createHash";
export * from "./isNode";
7 changes: 7 additions & 0 deletions packages/apollo-env/src/utils/isNode.ts
@@ -0,0 +1,7 @@
export const isNode =
typeof process === "object" &&
process &&
process.release &&
process.release.name === "node" &&

This comment has been minimized.

Copy link
@abernix

abernix Mar 4, 2019

Member

@trevor-scheer I learned in apollographql/apollo-server#2357 that we should drop this line. (Also, this should be shared logic, but don't let that distract you right now.)

process.versions &&
typeof process.versions.node === "string";
1 change: 1 addition & 0 deletions packages/apollo-graphql/package.json
Expand Up @@ -11,6 +11,7 @@
"node": ">=6"
},
"dependencies": {
"apollo-env": "file:../apollo-env",
"lodash.sortby": "^4.7.0"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-graphql/src/signature.ts
Expand Up @@ -43,8 +43,8 @@
// the server no longer needs to parse the signature or run its own signature
// algorithm on it, and the details of the signature algorithm are now up to the
// reporting agent.
import { createHash } from "crypto";
import { DocumentNode } from "graphql";
import { createHash } from "apollo-env";
import {
printWithReducedWhitespace,
dropUnusedDefinitions,
Expand Down

0 comments on commit bfeb541

Please sign in to comment.