Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(*): Use new createContentDigest helper #12973

Merged
merged 8 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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