Skip to content

Commit

Permalink
chore(*): Use new createContentDigest helper (#13000)
Browse files Browse the repository at this point in the history
## Description
this PR makes use of the new createContentDigest helper in the following plugins:
- gatsby-transformer-excel
- gatsby-source-hacker-news
- gatsby-source-wikipedia
- gatsby-source-wordpress
- gatsby-transformer-javascript-frontmatter
- gatsby-source-graphql
- gatsby-transformer-hjson
- gatsby-source-npm-package-search
- gatsby-source-lever
- gatsby-transformer-javascript-static-exports
- gatsby-transformer-react-docgen
## Related Issues

Related to #8805
  • Loading branch information
frinyvonnick authored and wardpeet committed Apr 8, 2019
1 parent 01670e1 commit 8166aff
Show file tree
Hide file tree
Showing 32 changed files with 199 additions and 181 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-source-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-graphql",
"scripts": {
Expand Down
16 changes: 9 additions & 7 deletions packages/gatsby-source-graphql/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const crypto = require(`crypto`)
const uuidv4 = require(`uuid/v4`)
const { buildSchema, printSchema } = require(`graphql`)
const {
Expand Down Expand Up @@ -29,6 +28,7 @@ exports.sourceNodes = async (
createLink,
createSchema,
refetchInterval,
createContentDigest,
} = options

invariant(
Expand Down Expand Up @@ -80,7 +80,12 @@ exports.sourceNodes = async (
})

const nodeId = createNodeId(`gatsby-source-graphql-${typeName}`)
const node = createSchemaNode({ id: nodeId, typeName, fieldName })
const node = createSchemaNode({
id: nodeId,
typeName,
fieldName,
createContentDigest,
})
createNode(node)

const resolver = (parent, args, context) => {
Expand Down Expand Up @@ -112,12 +117,9 @@ exports.sourceNodes = async (
}
}

function createSchemaNode({ id, typeName, fieldName }) {
function createSchemaNode({ id, typeName, fieldName, createContentDigest }) {
const nodeContent = uuidv4()
const nodeContentDigest = crypto
.createHash(`md5`)
.update(nodeContent)
.digest(`hex`)
const nodeContentDigest = createContentDigest(nodeContent)
return {
id,
typeName: typeName,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-hacker-news/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-hacker-news",
"scripts": {
Expand Down
14 changes: 3 additions & 11 deletions packages/gatsby-source-hacker-news/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const axios = require(`axios`)
const crypto = require(`crypto`)
const url = require(`url`)
const _ = require(`lodash`)

Expand All @@ -13,6 +12,7 @@ exports.sourceNodes = async ({
getNode,
createNodeId,
hasNodeChanged,
createContentDigest,
}) => {
const { createNode } = actions

Expand Down Expand Up @@ -117,11 +117,7 @@ fragment commentsFragment on HackerNewsItem {
storyNode.by = storyNode.by.id

// Get content digest of node.
const contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(storyNode))
.digest(`hex`)

const contentDigest = createContentDigest(storyNode)
storyNode.internal.contentDigest = contentDigest
createNode(storyNode)

Expand All @@ -147,11 +143,7 @@ fragment commentsFragment on HackerNewsItem {
const nodeStr = JSON.stringify(commentNode)

// Get content digest of comment node.
const contentDigest = crypto
.createHash(`md5`)
.update(nodeStr)
.digest(`hex`)

const contentDigest = createContentDigest(nodeStr)
commentNode.internal.contentDigest = contentDigest
commentNode.internal.content = nodeStr

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-lever/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-lever",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2749,7 +2749,7 @@ Lever’s Sales organization is a tight-knit team with diverse backgrounds--we
"hostedUrl": "https://jobs.lever.co/lever/4432ed35-6f71-4ea4-b802-f99af29d71b6",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "6afa157457057c1a22bdec0e27495a3b",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "4432ed35-6f71-4ea4-b802-f99af29d71b6",
Expand Down Expand Up @@ -2798,7 +2798,7 @@ JavaScript, Node.js, MongoDB, Redis, Elasticsearch, DerbyJS, ShareDB, IMAP, SMTP
"hostedUrl": "https://jobs.lever.co/lever/7ab138b0-b5c8-425d-93ae-2fc2b051bc0e",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "9aa12a00f412e84623d60e8e3c9a892b",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "7ab138b0-b5c8-425d-93ae-2fc2b051bc0e",
Expand Down Expand Up @@ -2859,7 +2859,7 @@ Lever’s Sales organization is a tight-knit team with diverse backgrounds--we
"hostedUrl": "https://jobs.lever.co/lever/182706ef-d98a-46ad-92bb-b7cddeb58419",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "6b8dea31eee4d6d49b08bddeb2ecbba0",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "182706ef-d98a-46ad-92bb-b7cddeb58419",
Expand Down Expand Up @@ -2921,7 +2921,7 @@ Please note, this role is on-site and located in San Francisco, California.
"hostedUrl": "https://jobs.lever.co/lever/8ef9f55b-1ca0-4c2b-b85d-56613d397098",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "ba7357d3b7205aab95fea87f486a0b8b",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "8ef9f55b-1ca0-4c2b-b85d-56613d397098",
Expand Down Expand Up @@ -2978,7 +2978,7 @@ Our next coworker is hands-on, collaborative, self-motivated, analytical, and pa
"hostedUrl": "https://jobs.lever.co/lever/b8093a5d-46e5-4470-be07-16759ccaae14",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "0211574c85e985208d118544e4921e69",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "b8093a5d-46e5-4470-be07-16759ccaae14",
Expand Down Expand Up @@ -3031,7 +3031,7 @@ You’re a fantastic listener, solutions oriented, and a problem-solver at heart
"hostedUrl": "https://jobs.lever.co/lever/4d23b480-37c7-4f87-890e-13bde50c52a2",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "84aab21e7d760d53bed9de4433698945",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "4d23b480-37c7-4f87-890e-13bde50c52a2",
Expand Down Expand Up @@ -3087,7 +3087,7 @@ As an engineering manager, you’ll lead a team of product developers as they bu
"hostedUrl": "https://jobs.lever.co/lever/37c2fb0d-03ef-4c7d-a6b0-cac02488f53b",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "a89df3d73afb0f6f65a6f690ae30c777",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "37c2fb0d-03ef-4c7d-a6b0-cac02488f53b",
Expand Down Expand Up @@ -3146,7 +3146,7 @@ Lever’s Sales team is a tight-knit and rapidly growing team with diverse backg
"hostedUrl": "https://jobs.lever.co/lever/6e55f2fc-7a4e-42c9-ab5e-775fbc7b126a",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f6fb7d85df782b4eff5085a7496bbe4e",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "6e55f2fc-7a4e-42c9-ab5e-775fbc7b126a",
Expand Down Expand Up @@ -3191,7 +3191,7 @@ This role is perfect for someone that enjoys challenging tasks, working on many
"hostedUrl": "https://jobs.lever.co/lever/62fbd830-e891-45fc-a8d6-8849a888b697",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "cbe4c334650ce99b5813094d6e80a006",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "62fbd830-e891-45fc-a8d6-8849a888b697",
Expand Down Expand Up @@ -3246,7 +3246,7 @@ Lever’s Sales team is a tight-knit team with diverse backgrounds--we’re seas
"hostedUrl": "https://jobs.lever.co/lever/4c5c5337-6a44-4c7c-80ae-3aeceb4603a0",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "c4d6b23a78da7bbe549ecec92e8459d4",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "4c5c5337-6a44-4c7c-80ae-3aeceb4603a0",
Expand Down Expand Up @@ -3312,7 +3312,7 @@ Learn more about Lever’s interview process and read about what our engineering
"hostedUrl": "https://jobs.lever.co/lever/befa0df5-da1f-4c11-adc2-d3eb2fa189da",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "68a44303a59c8e2e6296c959af1e314a",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "befa0df5-da1f-4c11-adc2-d3eb2fa189da",
Expand Down Expand Up @@ -3368,7 +3368,7 @@ This is an opportunity for someone who wants to be part of something big and tra
"hostedUrl": "https://jobs.lever.co/lever/f366f610-1af4-43c1-9213-9c39830a715f",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "96729eb3a7c40a9a30137fe1adbab65a",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "f366f610-1af4-43c1-9213-9c39830a715f",
Expand Down Expand Up @@ -3429,7 +3429,7 @@ Lever’s Sales team is a tight-knit and rapidly growing team with diverse backg
"hostedUrl": "https://jobs.lever.co/lever/0a28373c-4b92-4c6e-a6d1-d4654a89329e",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f912357bbad60de2ca4a4dbfb4302263",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "0a28373c-4b92-4c6e-a6d1-d4654a89329e",
Expand Down Expand Up @@ -3475,7 +3475,7 @@ We’re building a world-class product-first organization and assembling the rig
"hostedUrl": "https://jobs.lever.co/lever/85df11e0-5aad-4fa4-9238-d07b506f9a07",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "ca1af348b856a922e3938df88d11575f",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "85df11e0-5aad-4fa4-9238-d07b506f9a07",
Expand Down Expand Up @@ -3534,7 +3534,7 @@ From day one as a Leveroo, you'll be an active contributor to the Lever culture
"hostedUrl": "https://jobs.lever.co/lever/c4b1635e-b308-4c8b-b2ca-1b536a7726ad",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "d674452420791401cd90cc2e44675dd3",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "c4b1635e-b308-4c8b-b2ca-1b536a7726ad",
Expand Down Expand Up @@ -3596,7 +3596,7 @@ Lever’s Sales Development team is a tight-knit team with diverse backgrounds--
"hostedUrl": "https://jobs.lever.co/lever/bc6bd95d-262a-460c-b6dc-6b4d46cf6927",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "9a856a9d4a078a3fef16bf2af5df66ba",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "bc6bd95d-262a-460c-b6dc-6b4d46cf6927",
Expand Down Expand Up @@ -3646,7 +3646,7 @@ Lever’s Sales Development team is a tight-knit team with diverse backgrounds--
"hostedUrl": "https://jobs.lever.co/lever/ba77c5e6-2f6c-4ee7-b37b-09dc02b3395f",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "b0445f8ba1213c5f845587958da3545a",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "ba77c5e6-2f6c-4ee7-b37b-09dc02b3395f",
Expand Down Expand Up @@ -3695,7 +3695,7 @@ In terms of what we’re looking for, past tenure at a hyper-growth startup is i
"hostedUrl": "https://jobs.lever.co/lever/7f673706-37a2-4386-bee5-bd2892bc0cfc",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f8b43fa0ad69c3201c5ab2b1669999f5",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "7f673706-37a2-4386-bee5-bd2892bc0cfc",
Expand Down Expand Up @@ -3749,7 +3749,7 @@ We’re looking for you to be as maniacal about Finance as we are about hiring (
"hostedUrl": "https://jobs.lever.co/lever/0a47a57c-a05a-48b3-90ec-2caca6d2807b",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "6a0ba5304335497cd7056fdcd0bc568d",
"contentDigest": "contentDigest",
"type": "lever",
},
"lever_id": "0a47a57c-a05a-48b3-90ec-2caca6d2807b",
Expand Down
7 changes: 6 additions & 1 deletion packages/gatsby-source-lever/src/__tests__/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ describe(`Process Lever data`, () => {
})
it(`creates nodes for each entry`, () => {
const createNode = jest.fn()
normalize.createNodesFromEntities({ entities, createNode })
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
normalize.createNodesFromEntities({
entities,
createNode,
createContentDigest,
})
expect(createNode.mock.calls).toMatchSnapshot()
})
})
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-source-lever/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const normalize = require(`./normalize`)
const typePrefix = `lever__`

exports.sourceNodes = async (
{ actions, getNode, store, cache, createNodeId },
{ actions, getNode, store, cache, createNodeId, createContentDigest },
{ site, verboseOutput }
) => {
const { createNode } = actions
Expand All @@ -30,7 +30,11 @@ exports.sourceNodes = async (
entities = normalize.createGatsbyIds(createNodeId, entities)

// creates nodes for each entry
normalize.createNodesFromEntities({ entities, createNode })
normalize.createNodesFromEntities({
entities,
createNode,
createContentDigest,
})

return
}
32 changes: 15 additions & 17 deletions packages/gatsby-source-lever/src/normalize.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
const crypto = require(`crypto`)
const deepMapKeys = require(`deep-map-keys`)
const stringify = require(`json-stringify-safe`)

const conflictFieldPrefix = `lever_`
// restrictedNodeFields from here https://www.gatsbyjs.org/docs/node-interface/
const restrictedNodeFields = [`id`, `children`, `parent`, `fields`, `internal`]

/**
* Encrypts a String using md5 hash of hexadecimal digest.
*
* @param {any} str
*/
const digest = str =>
crypto
.createHash(`md5`)
.update(str)
.digest(`hex`)

/**
* Create the Graph QL Node
*
* @param {any} ent
* @param {any} type
* @param {any} createNode
*/
async function createGraphQLNode(ent, type, createNode, store, cache) {
async function createGraphQLNode(
ent,
type,
createNode,
store,
cache,
createContentDigest
) {
let id = !ent.id ? (!ent.ID ? 0 : ent.ID) : ent.id
let node = {
id: `${type}_${id.toString()}`,
Expand All @@ -36,7 +30,7 @@ async function createGraphQLNode(ent, type, createNode, store, cache) {
}
node = recursiveAddFields(ent, node)
node.internal.content = JSON.stringify(node)
node.internal.contentDigest = digest(stringify(node))
node.internal.contentDigest = createContentDigest(node)
createNode(node)
}
exports.createGraphQLNode = createGraphQLNode
Expand Down Expand Up @@ -133,7 +127,11 @@ exports.createGatsbyIds = (createNodeId, entities) =>
return e
})

exports.createNodesFromEntities = ({ entities, createNode }) => {
exports.createNodesFromEntities = ({
entities,
createNode,
createContentDigest,
}) => {
entities.forEach(e => {
let { ...entity } = e
let node = {
Expand All @@ -142,7 +140,7 @@ exports.createNodesFromEntities = ({ entities, createNode }) => {
children: [],
internal: {
type: `lever`,
contentDigest: digest(JSON.stringify(entity)),
contentDigest: createContentDigest(entity),
},
}
createNode(node)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-npm-package-search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
Expand Down
Loading

0 comments on commit 8166aff

Please sign in to comment.