Skip to content

Commit

Permalink
♻️Use new createContentDigest helper in gatsby-source-medium, gatsby-…
Browse files Browse the repository at this point in the history
…transformer-toml and gatsby-source-drupal (#12973)

<!--
  Have any questions? Check out the contributing docs at https://gatsby.dev/contribute, or
  ask in this Pull Request and a Gatsby maintainer will be happy to help :)
-->

## Description

Hi 👋

First of all thanks for your work: I'm using gatsby everyday at work! 👏

This PR makes use of the new createContentDigest helper in the following plugins:

* gatsby-source-medium
* gatsby-transformer-toml
* gatsby-source-drupal

## Related Issues

<!--
  Link to the issue that is fixed by this PR (if there is one)
  e.g. Fixes #1234, Addresses #1234, Related to #1234, etc.
-->

Related to #8805
  • Loading branch information
charlyx authored and wardpeet committed Apr 1, 2019
1 parent 0c9a9af commit 980fa88
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-source-drupal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-drupal",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions packages/gatsby-source-drupal/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ describe(`gatsby-source-drupal`, () => {
const nodes = {}
const createNodeId = id => `generated-id-${id}`
const baseUrl = `http://fixture`
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
const { objectContaining } = expect

beforeAll(async () => {
const args = {
createNodeId,
createContentDigest,
actions: {
createNode: jest.fn(node => (nodes[node.id] = node)),
},
Expand All @@ -48,6 +51,24 @@ describe(`gatsby-source-drupal`, () => {
expect(nodes[createNodeId(`article-3`)]).toBeDefined()
})

it(`Nodes contain contentDigest`, () => {
expect(nodes[createNodeId(`file-1`)]).toEqual(
objectContaining({
internal: objectContaining({ contentDigest: `contentDigest` }),
})
)
expect(nodes[createNodeId(`article-2`)]).toEqual(
objectContaining({
internal: objectContaining({ contentDigest: `contentDigest` }),
})
)
expect(nodes[createNodeId(`tag-1`)]).toEqual(
objectContaining({
internal: objectContaining({ contentDigest: `contentDigest` }),
})
)
})

it(`Nodes contain attributes data`, () => {
expect(nodes[createNodeId(`file-1`)].filename).toEqual(`main-image.png`)
expect(nodes[createNodeId(`article-2`)].title).toEqual(`Article #2`)
Expand Down
18 changes: 9 additions & 9 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const axios = require(`axios`)
const crypto = require(`crypto`)
const _ = require(`lodash`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const { URL } = require(`url`)
const { nodeFromData } = require(`./normalize`)

// Get content digest of node.
const createContentDigest = obj =>
crypto
.createHash(`md5`)
.update(JSON.stringify(obj))
.digest(`hex`)

exports.sourceNodes = async (
{ actions, getNode, hasNodeChanged, store, cache, createNodeId },
{
actions,
getNode,
hasNodeChanged,
store,
cache,
createNodeId,
createContentDigest,
},
{ baseUrl, apiBase, basicAuth, filters }
) => {
const { createNode } = actions
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-medium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-medium",
"scripts": {
Expand Down
10 changes: 3 additions & 7 deletions packages/gatsby-source-medium/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 fetch = (username, limit = 100) => {
const url = `https://medium.com/${username}/latest?format=json&limit=${limit}`
Expand All @@ -26,7 +25,7 @@ const convertTimestamps = (nextObj, prevObj, prevKey) => {
const strip = payload => payload.replace(prefix, ``)

exports.sourceNodes = async (
{ actions, createNodeId },
{ actions, createNodeId, createContentDigest },
{ username, limit }
) => {
const { createNode } = actions
Expand Down Expand Up @@ -77,10 +76,7 @@ exports.sourceNodes = async (
resources.map(resource => {
convertTimestamps(resource)

const digest = crypto
.createHash(`md5`)
.update(JSON.stringify(resource))
.digest(`hex`)
const contentDigest = createContentDigest(resource)

const links =
resource.type === `Post`
Expand All @@ -106,7 +102,7 @@ exports.sourceNodes = async (
children: [],
internal: {
type: `Medium${resource.type}`,
contentDigest: digest,
contentDigest,
},
},
links
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-toml/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-transformer-toml",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "9ce7919cb3f607a0542bac4aa46136b6",
"contentDigest": "contentDigest",
"type": "",
},
"parent": "whatever",
Expand Down Expand Up @@ -45,7 +45,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "9ce7919cb3f607a0542bac4aa46136b6",
"contentDigest": "contentDigest",
"type": "",
},
"parent": "whatever",
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ describe(`Process TOML nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createDigestContent = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
createDigestContent,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(createParentChildLink).toHaveBeenCalledTimes(1)
expect(createDigestContent).toHaveBeenCalledTimes(1)
})
})

Expand Down
15 changes: 8 additions & 7 deletions packages/gatsby-transformer-toml/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const toml = require(`toml`)
const _ = require(`lodash`)
const crypto = require(`crypto`)

async function onCreateNode({ node, actions, loadNodeContent, createNodeId }) {
async function onCreateNode({
node,
actions,
loadNodeContent,
createNodeId,
createDigestContent,
}) {
const { createNode, createParentChildLink } = actions
// Filter out non-toml content
// Currently TOML files are considered null in 'mime-db'
Expand All @@ -18,11 +23,7 @@ async function onCreateNode({ node, actions, loadNodeContent, createNodeId }) {
// This version suffers from:
// 1) More TOML files -> more types
// 2) Different files with the same name creating conflicts
const parsedContentStr = JSON.stringify(parsedContent)
const contentDigest = crypto
.createHash(`md5`)
.update(parsedContentStr)
.digest(`hex`)
const contentDigest = createDigestContent(parsedContent)

const newNode = {
...parsedContent,
Expand Down

0 comments on commit 980fa88

Please sign in to comment.