Skip to content

Commit 093f1f2

Browse files
samschasidharthachatterjee
authored andcommitted
fix: Add fallback for createContentDigest (#13584)
1 parent b488a05 commit 093f1f2

File tree

8 files changed

+43
-34
lines changed

8 files changed

+43
-34
lines changed

packages/gatsby-source-filesystem/src/create-file-node.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const mime = require(`mime`)
55
const prettyBytes = require(`pretty-bytes`)
66

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

1010
exports.createFileNode = async (
1111
pathToFile,
@@ -27,12 +27,10 @@ exports.createFileNode = async (
2727
const stats = await fs.stat(slashedFile.absolutePath)
2828
let internal
2929
if (stats.isDirectory()) {
30-
const contentDigest = crypto
31-
.createHash(`md5`)
32-
.update(
33-
JSON.stringify({ stats: stats, absolutePath: slashedFile.absolutePath })
34-
)
35-
.digest(`hex`)
30+
const contentDigest = createContentDigest({
31+
stats: stats,
32+
absolutePath: slashedFile.absolutePath,
33+
})
3634
internal = {
3735
contentDigest,
3836
type: `Directory`,

packages/gatsby-source-filesystem/src/create-remote-file-node.js

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require(`fs-extra`)
22
const got = require(`got`)
3-
const crypto = require(`crypto`)
3+
const { createContentDigest } = require(`./fallback`)
44
const path = require(`path`)
55
const { isWebUri } = require(`valid-url`)
66
const Queue = require(`better-queue`)
@@ -53,24 +53,6 @@ const bar = new ProgressBar(
5353
* @param {Auth} [options.auth]
5454
*/
5555

56-
/*********
57-
* utils *
58-
*********/
59-
60-
/**
61-
* createHash
62-
* --
63-
*
64-
* Create an md5 hash of the given str
65-
* @param {Stringq} str
66-
* @return {String}
67-
*/
68-
const createHash = str =>
69-
crypto
70-
.createHash(`md5`)
71-
.update(str)
72-
.digest(`hex`)
73-
7456
const CACHE_DIR = `.cache`
7557
const FS_PLUGIN_DIR = `gatsby-source-filesystem`
7658

@@ -215,7 +197,7 @@ async function processRemoteNode({
215197
}
216198

217199
// Create the temp and permanent file names for the url.
218-
const digest = createHash(url)
200+
const digest = createContentDigest(url)
219201
if (!name) {
220202
name = getRemoteFileName(url)
221203
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TODO: remove in gatsby v3
2+
exports.createContentDigest = input => {
3+
try {
4+
const { createContentDigest } = require(`gatsby/utils`)
5+
6+
return createContentDigest(input)
7+
} catch (e) {
8+
const content = typeof input !== `string` ? JSON.stringify(input) : input
9+
10+
return require(`crypto`)
11+
.createHash(`md5`)
12+
.update(content)
13+
.digest(`hex`)
14+
}
15+
}

packages/gatsby-transformer-remark/src/__tests__/extend-node.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { graphql } = require(`gatsby/graphql`)
22
const { onCreateNode } = require(`../gatsby-node`)
33
const extendNodeType = require(`../extend-node-type`)
4+
const { createContentDigest } = require(`gatsby/utils`)
45

56
// given a set of nodes and a query, return the result of the query
67
async function queryResult(
@@ -104,6 +105,7 @@ const bootstrapTest = (
104105
loadNodeContent,
105106
actions,
106107
createNodeId,
108+
createContentDigest,
107109
},
108110
{ ...additionalParameters, ...pluginOptions }
109111
)

packages/gatsby-transformer-remark/src/__tests__/on-node-create.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const _ = require(`lodash`)
33
const onCreateNode = require(`../on-node-create`)
44
const { graphql } = require(`gatsby/graphql`)
55

6+
const { createContentDigest } = require(`gatsby/utils`)
7+
68
let node
79
let actions
810
let createNodeId
@@ -38,6 +40,7 @@ Where oh where is my little pony?
3840
loadNodeContent,
3941
actions,
4042
createNodeId,
43+
createContentDigest,
4144
}).then(() => {
4245
expect(actions.createNode.mock.calls).toMatchSnapshot()
4346
expect(
@@ -75,6 +78,7 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
7578
loadNodeContent,
7679
actions,
7780
createNodeId,
81+
createContentDigest,
7882
},
7983
{ excerpt_separator: `<!-- end -->` }
8084
).then(() => {
@@ -106,6 +110,7 @@ yadda yadda
106110
actions,
107111
createNodeId,
108112
loadNodeContent,
113+
createContentDigest,
109114
})
110115

111116
expect(parsed.frontmatter.date).toEqual(new Date(date).toJSON())
@@ -207,6 +212,7 @@ In quis lectus sed eros efficitur luctus. Morbi tempor, nisl eget feugiat tincid
207212
loadNodeContent,
208213
actions,
209214
createNodeId,
215+
createContentDigest,
210216
},
211217
{ excerpt_separator: `<!-- end -->` }
212218
)
@@ -261,6 +267,7 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
261267
loadNodeContent,
262268
actions,
263269
createNodeId,
270+
createContentDigest,
264271
})
265272
})
266273
})

packages/gatsby-transformer-remark/src/on-node-create.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const grayMatter = require(`gray-matter`)
2-
const crypto = require(`crypto`)
32
const _ = require(`lodash`)
43

54
module.exports = async function onCreateNode(
6-
{ node, loadNodeContent, actions, createNodeId, reporter },
5+
{
6+
node,
7+
loadNodeContent,
8+
actions,
9+
createNodeId,
10+
reporter,
11+
createContentDigest,
12+
},
713
pluginOptions
814
) {
915
const { createNode, createParentChildLink } = actions
@@ -53,10 +59,7 @@ module.exports = async function onCreateNode(
5359
markdownNode.fileAbsolutePath = node.absolutePath
5460
}
5561

56-
markdownNode.internal.contentDigest = crypto
57-
.createHash(`md5`)
58-
.update(JSON.stringify(markdownNode))
59-
.digest(`hex`)
62+
markdownNode.internal.contentDigest = createContentDigest(markdownNode)
6063

6164
createNode(markdownNode)
6265
createParentChildLink({ parent: node, child: markdownNode })

packages/gatsby/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@
148148
"cache-dir",
149149
"dist",
150150
"graphql.js",
151-
"index.d.ts"
151+
"index.d.ts",
152+
"utils.js"
152153
],
153154
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby#readme",
154155
"keywords": [

packages/gatsby/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.createContentDigest = require(`./dist/utils/create-content-digest`)

0 commit comments

Comments
 (0)