Skip to content

Commit

Permalink
Merge branch 'main' into sg/open-codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrove committed Aug 4, 2022
2 parents ec87389 + feb8989 commit d70a8e4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netlify-onegraph-internal",
"version": "0.4.0",
"version": "0.4.2",
"description": "Internal tools for use by Netlify",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -18,6 +18,7 @@
"dependencies": {
"graphql": "16.5.0",
"node-fetch": "^2.6.0",
"rusha": "^0.8.14",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * as CodegenHelpers from "./codegen/codegenHelpers";
export * as CliEventHelper from "./cliEventHelpers";
export * as GraphQLHelpers from "./graphqlHelpers";
export * as NetlifyGraphJsonConfig from "./netlifyGraphJsonConfig";
export * as NetlifyGraphLockfile from "./lockfile";
36 changes: 36 additions & 0 deletions src/lockfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Rusha gives us a SHA1 (good enough for file content hashing) and was already
// present in the Netlify React UI JS bundle when we introduced lockfiles
import { createHash } from "rusha";

//TODO add schema content hash too?
export type V0_format = {
version: "v0";
locked: {
schemaId: string;
operationsHash: string;
};
};

export const defaultLockFileName = "netlifyGraph.lock";

export const hashOperations = (operationsDoc: string) => {
return createHash().update(operationsDoc).digest("hex");
};

export const createLockfile = ({
schemaId,
operationsFileContents,
}: {
version?: string;
schemaId: string;
operationsFileContents: string;
}): V0_format => {
const operationsHash = hashOperations(operationsFileContents);
return {
version: "v0",
locked: {
schemaId,
operationsHash,
},
};
};
2 changes: 1 addition & 1 deletion src/netlifyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ ${out(
const encodedVariables = encodeURIComponent(input.variables || "null");
const url = 'https://graph.netlify.com/graphql?app_id=' + input.siteId + '&doc_id=' + input.docId + (input.operationName ? ('&operationName=' + input.operationName) : '') + (schemaId ? ('&schemaId=' + schemaId) : '') + '&variables=' + encodedVariables;
const respBody = []
return new Promise((resolve, reject) => {
Expand Down
6 changes: 6 additions & 0 deletions src/oneGraphClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const __netlifyGraphJwt = async ({
}
};

const atob =
globalThis.Buffer != null
? (x) => globalThis.Buffer.from(x, "base64").toString()
: globalThis.atob;

export function getGraphJwtForSite({
siteId,
nfToken,
Expand All @@ -78,6 +83,7 @@ export function getGraphJwtForSite({
// url-safe -> ordinary base64
.replace(/_/g, "/")
.replace(/-/g, "+");

const payload = JSON.parse(atob(base64Payload));
const expirationTicks = payload.exp - payload.iat;
// use performance.now in case the browser's clock is way off
Expand Down

0 comments on commit d70a8e4

Please sign in to comment.