Skip to content

Commit

Permalink
fix: Add fallback for createContentDigest (#13584)
Browse files Browse the repository at this point in the history
  • Loading branch information
samscha authored and sidharthachatterjee committed Apr 24, 2019
1 parent b488a05 commit 093f1f2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 34 deletions.
12 changes: 5 additions & 7 deletions packages/gatsby-source-filesystem/src/create-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const mime = require(`mime`)
const prettyBytes = require(`pretty-bytes`)

const md5File = require(`bluebird`).promisify(require(`md5-file`))
const crypto = require(`crypto`)
const { createContentDigest } = require(`./fallback`)

exports.createFileNode = async (
pathToFile,
Expand All @@ -27,12 +27,10 @@ exports.createFileNode = async (
const stats = await fs.stat(slashedFile.absolutePath)
let internal
if (stats.isDirectory()) {
const contentDigest = crypto
.createHash(`md5`)
.update(
JSON.stringify({ stats: stats, absolutePath: slashedFile.absolutePath })
)
.digest(`hex`)
const contentDigest = createContentDigest({
stats: stats,
absolutePath: slashedFile.absolutePath,
})
internal = {
contentDigest,
type: `Directory`,
Expand Down
22 changes: 2 additions & 20 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require(`fs-extra`)
const got = require(`got`)
const crypto = require(`crypto`)
const { createContentDigest } = require(`./fallback`)
const path = require(`path`)
const { isWebUri } = require(`valid-url`)
const Queue = require(`better-queue`)
Expand Down Expand Up @@ -53,24 +53,6 @@ const bar = new ProgressBar(
* @param {Auth} [options.auth]
*/

/*********
* utils *
*********/

/**
* createHash
* --
*
* Create an md5 hash of the given str
* @param {Stringq} str
* @return {String}
*/
const createHash = str =>
crypto
.createHash(`md5`)
.update(str)
.digest(`hex`)

const CACHE_DIR = `.cache`
const FS_PLUGIN_DIR = `gatsby-source-filesystem`

Expand Down Expand Up @@ -215,7 +197,7 @@ async function processRemoteNode({
}

// Create the temp and permanent file names for the url.
const digest = createHash(url)
const digest = createContentDigest(url)
if (!name) {
name = getRemoteFileName(url)
}
Expand Down
15 changes: 15 additions & 0 deletions packages/gatsby-source-filesystem/src/fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TODO: remove in gatsby v3
exports.createContentDigest = input => {
try {
const { createContentDigest } = require(`gatsby/utils`)

return createContentDigest(input)
} catch (e) {
const content = typeof input !== `string` ? JSON.stringify(input) : input

return require(`crypto`)
.createHash(`md5`)
.update(content)
.digest(`hex`)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { graphql } = require(`gatsby/graphql`)
const { onCreateNode } = require(`../gatsby-node`)
const extendNodeType = require(`../extend-node-type`)
const { createContentDigest } = require(`gatsby/utils`)

// given a set of nodes and a query, return the result of the query
async function queryResult(
Expand Down Expand Up @@ -104,6 +105,7 @@ const bootstrapTest = (
loadNodeContent,
actions,
createNodeId,
createContentDigest,
},
{ ...additionalParameters, ...pluginOptions }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const _ = require(`lodash`)
const onCreateNode = require(`../on-node-create`)
const { graphql } = require(`gatsby/graphql`)

const { createContentDigest } = require(`gatsby/utils`)

let node
let actions
let createNodeId
Expand Down Expand Up @@ -38,6 +40,7 @@ Where oh where is my little pony?
loadNodeContent,
actions,
createNodeId,
createContentDigest,
}).then(() => {
expect(actions.createNode.mock.calls).toMatchSnapshot()
expect(
Expand Down Expand Up @@ -75,6 +78,7 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
loadNodeContent,
actions,
createNodeId,
createContentDigest,
},
{ excerpt_separator: `<!-- end -->` }
).then(() => {
Expand Down Expand Up @@ -106,6 +110,7 @@ yadda yadda
actions,
createNodeId,
loadNodeContent,
createContentDigest,
})

expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON())
Expand Down Expand Up @@ -207,6 +212,7 @@ In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincid
loadNodeContent,
actions,
createNodeId,
createContentDigest,
},
{ excerpt_separator: `<!-- end -->` }
)
Expand Down Expand Up @@ -261,6 +267,7 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
loadNodeContent,
actions,
createNodeId,
createContentDigest,
})
})
})
Expand Down
15 changes: 9 additions & 6 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const grayMatter = require(`gray-matter`)
const crypto = require(`crypto`)
const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, loadNodeContent, actions, createNodeId, reporter },
{
node,
loadNodeContent,
actions,
createNodeId,
reporter,
createContentDigest,
},
pluginOptions
) {
const { createNode, createParentChildLink } = actions
Expand Down Expand Up @@ -53,10 +59,7 @@ module.exports = async function onCreateNode(
markdownNode.fileAbsolutePath = node.absolutePath
}

markdownNode.internal.contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(markdownNode))
.digest(`hex`)
markdownNode.internal.contentDigest = createContentDigest(markdownNode)

createNode(markdownNode)
createParentChildLink({ parent: node, child: markdownNode })
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
"cache-dir",
"dist",
"graphql.js",
"index.d.ts"
"index.d.ts",
"utils.js"
],
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby#readme",
"keywords": [
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.createContentDigest = require(`./dist/utils/create-content-digest`)

0 comments on commit 093f1f2

Please sign in to comment.