Skip to content

Commit e8b2696

Browse files
rbadrDSchau
authored andcommitted
feat(gatsby) : add createContentDigest helper (#8687)
This PR add a createContentDigest helper and pass it in to the Gatsby API runner. Closes #8587
1 parent ec991a4 commit e8b2696

File tree

15 files changed

+95
-74
lines changed

15 files changed

+95
-74
lines changed

docs/docs/source-plugin-tutorial.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ With the setup done, move on to adding the plugin's functionality.
116116
Create a new file called `gatsby-node.js` in your `gatsby-source-pixabay` directory, and add the following:
117117

118118
```js:title=gatsby-node.js
119-
const crypto = require("crypto")
120119
const fetch = require("node-fetch")
121120
const queryString = require("query-string")
122121

123-
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
122+
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
124123
const { createNode } = actions
125124

126125
// Gatsby adds a configOption that's not needed for this plugin, delete it
@@ -136,15 +135,14 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
136135
What did you do by adding this code? You started by importing the dependencies that you added earlier (along with one built in dependency):
137136

138137
```js
139-
const crypto = require("crypto")
140138
const fetch = require("node-fetch")
141139
const queryString = require("query-string")
142140
```
143141

144-
Then you implemented Gatsby's [`sourceNodes` API](/docs/node-apis/#sourceNodes) which Gatsby will run as part of its bootstrap process. When Gatsby calls `sourceNodes`, it'll pass in some helper functions (`actions` and `createNodeId`) along with any config options that are provided in your project's `gatsby-config.js` file:
142+
Then you implemented Gatsby's [`sourceNodes` API](/docs/node-apis/#sourceNodes) which Gatsby will run as part of its bootstrap process. When Gatsby calls `sourceNodes`, it'll pass in some helper functions (`actions`, `createNodeId` and `createContentDigest`) along with any config options that are provided in your project's `gatsby-config.js` file:
145143

146144
```js
147-
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
145+
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
148146
```
149147
150148
You do some initial setup:
@@ -211,9 +209,8 @@ Update `gatsby-node.js` in your `plugins/gatsby-source-pixabay/` directory:
211209
```js{11-30}:title=gatsby-node.js
212210
const fetch = require("node-fetch")
213211
const queryString = require("query-string")
214-
const crypto = require("crypto")
215212

216-
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
213+
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
217214
const { createNode } = actions
218215

219216
// Gatsby adds a configOption that's not needed for this plugin, delete it
@@ -266,14 +263,13 @@ You're ready to add the final step of your plugin - converting this data into a
266263
267264
### Use `createNode` function
268265
269-
You're adding a helper function on lines 12 to 32 and processing the data into a node on lines 49 to 52:
266+
You're adding a helper function on lines 11 to 27 and processing the data into a node on lines 44 to 47:
270267
271-
```js{12-32,49-52}:title=gatsby-node.js
268+
```js{11-27,44-47}:title=gatsby-node.js
272269
const fetch = require("node-fetch")
273270
const queryString = require("query-string")
274-
const crypto = require("crypto")
275271

276-
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
272+
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
277273
const { createNode } = actions
278274

279275
// Gatsby adds a configOption that's not needed for this plugin, delete it
@@ -283,10 +279,6 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
283279
const processPhoto = photo => {
284280
const nodeId = createNodeId(`pixabay-photo-${photo.id}`)
285281
const nodeContent = JSON.stringify(photo)
286-
const nodeContentDigest = crypto
287-
.createHash("md5")
288-
.update(nodeContent)
289-
.digest("hex")
290282

291283
const nodeData = Object.assign({}, photo, {
292284
id: nodeId,
@@ -295,7 +287,7 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
295287
internal: {
296288
type: `PixabayPhoto`,
297289
content: nodeContent,
298-
contentDigest: nodeContentDigest,
290+
contentDigest: createContentDigest(photo),
299291
},
300292
})
301293

packages/gatsby-transformer-csv/src/__tests__/__snapshots__/gatsby-node.js.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Array [
99
"funny": "yup",
1010
"id": "uuid-from-gatsby",
1111
"internal": Object {
12-
"contentDigest": "5005aee6b2557974cae0aabc712e125a",
12+
"contentDigest": "contentDigest",
1313
"type": "TestCsv",
1414
},
1515
"parent": "whatever",
@@ -22,7 +22,7 @@ Array [
2222
"funny": "nope",
2323
"id": "uuid-from-gatsby",
2424
"internal": Object {
25-
"contentDigest": "0af87da7e572c2d6d8493bcd642c2c78",
25+
"contentDigest": "contentDigest",
2626
"type": "TestCsv",
2727
},
2828
"parent": "whatever",
@@ -41,7 +41,7 @@ Array [
4141
"funny": "yup",
4242
"id": "uuid-from-gatsby",
4343
"internal": Object {
44-
"contentDigest": "5005aee6b2557974cae0aabc712e125a",
44+
"contentDigest": "contentDigest",
4545
"type": "TestCsv",
4646
},
4747
"parent": "whatever",
@@ -70,7 +70,7 @@ false,\\"nope\\"",
7070
"funny": "nope",
7171
"id": "uuid-from-gatsby",
7272
"internal": Object {
73-
"contentDigest": "0af87da7e572c2d6d8493bcd642c2c78",
73+
"contentDigest": "contentDigest",
7474
"type": "TestCsv",
7575
},
7676
"parent": "whatever",
@@ -103,7 +103,7 @@ Array [
103103
"field2": "funny",
104104
"id": "uuid-from-gatsby",
105105
"internal": Object {
106-
"contentDigest": "49605f5e32a2250d7ca740c5d2e79784",
106+
"contentDigest": "contentDigest",
107107
"type": "TestCsv",
108108
},
109109
"parent": "whatever",
@@ -116,7 +116,7 @@ Array [
116116
"field2": "yup",
117117
"id": "uuid-from-gatsby",
118118
"internal": Object {
119-
"contentDigest": "2bf0bc0e8539ef5f6bbcf67c8b733233",
119+
"contentDigest": "contentDigest",
120120
"type": "TestCsv",
121121
},
122122
"parent": "whatever",
@@ -129,7 +129,7 @@ Array [
129129
"field2": "nope",
130130
"id": "uuid-from-gatsby",
131131
"internal": Object {
132-
"contentDigest": "f5f617ce4243d639de924543dec5600e",
132+
"contentDigest": "contentDigest",
133133
"type": "TestCsv",
134134
},
135135
"parent": "whatever",
@@ -148,7 +148,7 @@ Array [
148148
"field2": "funny",
149149
"id": "uuid-from-gatsby",
150150
"internal": Object {
151-
"contentDigest": "49605f5e32a2250d7ca740c5d2e79784",
151+
"contentDigest": "contentDigest",
152152
"type": "TestCsv",
153153
},
154154
"parent": "whatever",
@@ -177,7 +177,7 @@ false,nope",
177177
"field2": "yup",
178178
"id": "uuid-from-gatsby",
179179
"internal": Object {
180-
"contentDigest": "2bf0bc0e8539ef5f6bbcf67c8b733233",
180+
"contentDigest": "contentDigest",
181181
"type": "TestCsv",
182182
},
183183
"parent": "whatever",
@@ -206,7 +206,7 @@ false,nope",
206206
"field2": "nope",
207207
"id": "uuid-from-gatsby",
208208
"internal": Object {
209-
"contentDigest": "f5f617ce4243d639de924543dec5600e",
209+
"contentDigest": "contentDigest",
210210
"type": "TestCsv",
211211
},
212212
"parent": "whatever",

packages/gatsby-transformer-csv/src/__tests__/gatsby-node.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ describe(`Process nodes correctly`, () => {
3030
const actions = { createNode, createParentChildLink }
3131
const createNodeId = jest.fn()
3232
createNodeId.mockReturnValue(`uuid-from-gatsby`)
33+
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
3334

3435
await onCreateNode({
3536
node,
3637
loadNodeContent,
3738
actions,
3839
createNodeId,
40+
createContentDigest,
3941
}).then(() => {
4042
expect(createNode.mock.calls).toMatchSnapshot()
4143
expect(createParentChildLink.mock.calls).toMatchSnapshot()
@@ -52,13 +54,15 @@ describe(`Process nodes correctly`, () => {
5254
const actions = { createNode, createParentChildLink }
5355
const createNodeId = jest.fn()
5456
createNodeId.mockReturnValue(`uuid-from-gatsby`)
57+
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
5558

5659
await onCreateNode(
5760
{
5861
node,
5962
loadNodeContent,
6063
actions,
6164
createNodeId,
65+
createContentDigest,
6266
},
6367
{ noheader: true }
6468
).then(() => {

packages/gatsby-transformer-csv/src/gatsby-node.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const Promise = require(`bluebird`)
22
const csv = require(`csvtojson`)
33
const _ = require(`lodash`)
4-
const crypto = require(`crypto`)
54

65
const convertToJson = (data, options) =>
76
new Promise((res, rej) => {
@@ -16,7 +15,7 @@ const convertToJson = (data, options) =>
1615
})
1716

1817
async function onCreateNode(
19-
{ node, actions, loadNodeContent, createNodeId },
18+
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
2019
options
2120
) {
2221
const { createNode, createParentChildLink } = actions
@@ -31,19 +30,14 @@ async function onCreateNode(
3130

3231
if (_.isArray(parsedContent)) {
3332
const csvArray = parsedContent.map((obj, i) => {
34-
const objStr = JSON.stringify(obj)
35-
const contentDigest = crypto
36-
.createHash(`md5`)
37-
.update(objStr)
38-
.digest(`hex`)
3933

4034
return {
4135
...obj,
4236
id: obj.id ? obj.id : createNodeId(`${node.id} [${i}] >>> CSV`),
4337
children: [],
4438
parent: node.id,
4539
internal: {
46-
contentDigest,
40+
contentDigest: createContentDigest(obj),
4741
// TODO make choosing the "type" a lot smarter. This assumes
4842
// the parent node is a file.
4943
// PascalCase

packages/gatsby-transformer-json/src/__tests__/__snapshots__/gatsby-node.js.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Array [
99
"funny": "yup",
1010
"id": "foo",
1111
"internal": Object {
12-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
12+
"contentDigest": "contentDigest",
1313
"type": "FooJson",
1414
},
1515
"parent": "whatever",
@@ -28,7 +28,7 @@ Array [
2828
"funny": "yup",
2929
"id": "foo",
3030
"internal": Object {
31-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
31+
"contentDigest": "contentDigest",
3232
"type": "FooJson",
3333
},
3434
"parent": "whatever",
@@ -60,7 +60,7 @@ Array [
6060
"funny": "yup",
6161
"id": "foo",
6262
"internal": Object {
63-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
63+
"contentDigest": "contentDigest",
6464
"type": "NotFileJson",
6565
},
6666
"parent": "whatever",
@@ -79,7 +79,7 @@ Array [
7979
"funny": "yup",
8080
"id": "foo",
8181
"internal": Object {
82-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
82+
"contentDigest": "contentDigest",
8383
"type": "NotFileJson",
8484
},
8585
"parent": "whatever",
@@ -109,7 +109,7 @@ Array [
109109
"funny": "yup",
110110
"id": "foo",
111111
"internal": Object {
112-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
112+
"contentDigest": "contentDigest",
113113
"type": "NodeNameJson",
114114
},
115115
"parent": "whatever",
@@ -122,7 +122,7 @@ Array [
122122
"funny": "nope",
123123
"id": "uuid-from-gatsby",
124124
"internal": Object {
125-
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
125+
"contentDigest": "contentDigest",
126126
"type": "NodeNameJson",
127127
},
128128
"parent": "whatever",
@@ -141,7 +141,7 @@ Array [
141141
"funny": "yup",
142142
"id": "foo",
143143
"internal": Object {
144-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
144+
"contentDigest": "contentDigest",
145145
"type": "NodeNameJson",
146146
},
147147
"parent": "whatever",
@@ -169,7 +169,7 @@ Array [
169169
"funny": "nope",
170170
"id": "uuid-from-gatsby",
171171
"internal": Object {
172-
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
172+
"contentDigest": "contentDigest",
173173
"type": "NodeNameJson",
174174
},
175175
"parent": "whatever",
@@ -201,7 +201,7 @@ Array [
201201
"funny": "yup",
202202
"id": "foo",
203203
"internal": Object {
204-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
204+
"contentDigest": "contentDigest",
205205
"type": "NotFileJson",
206206
},
207207
"parent": "whatever",
@@ -214,7 +214,7 @@ Array [
214214
"funny": "nope",
215215
"id": "uuid-from-gatsby",
216216
"internal": Object {
217-
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
217+
"contentDigest": "contentDigest",
218218
"type": "NotFileJson",
219219
},
220220
"parent": "whatever",
@@ -233,7 +233,7 @@ Array [
233233
"funny": "yup",
234234
"id": "foo",
235235
"internal": Object {
236-
"contentDigest": "8838e569ae02d98806532310fb2a577a",
236+
"contentDigest": "contentDigest",
237237
"type": "NotFileJson",
238238
},
239239
"parent": "whatever",
@@ -259,7 +259,7 @@ Array [
259259
"funny": "nope",
260260
"id": "uuid-from-gatsby",
261261
"internal": Object {
262-
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
262+
"contentDigest": "contentDigest",
263263
"type": "NotFileJson",
264264
},
265265
"parent": "whatever",

packages/gatsby-transformer-json/src/__tests__/gatsby-node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ const bootstrapTest = async (node, pluginOptions = {}) => {
1111
const actions = { createNode, createParentChildLink }
1212
const createNodeId = jest.fn()
1313
createNodeId.mockReturnValue(`uuid-from-gatsby`)
14+
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
1415

1516
return await onCreateNode(
1617
{
1718
node,
1819
loadNodeContent,
1920
actions,
2021
createNodeId,
22+
createContentDigest,
2123
},
2224
pluginOptions
2325
).then(() => {

packages/gatsby-transformer-json/src/gatsby-node.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const _ = require(`lodash`)
2-
const crypto = require(`crypto`)
32
const path = require(`path`)
43

54
async function onCreateNode(
6-
{ node, actions, loadNodeContent, createNodeId },
5+
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
76
pluginOptions
87
) {
98
function getType({ node, object, isArray }) {
@@ -21,18 +20,13 @@ async function onCreateNode(
2120
}
2221

2322
function transformObject(obj, id, type) {
24-
const objStr = JSON.stringify(obj)
25-
const contentDigest = crypto
26-
.createHash(`md5`)
27-
.update(objStr)
28-
.digest(`hex`)
2923
const jsonNode = {
3024
...obj,
3125
id,
3226
children: [],
3327
parent: node.id,
3428
internal: {
35-
contentDigest,
29+
contentDigest: createContentDigest(obj),
3630
type,
3731
},
3832
}

0 commit comments

Comments
 (0)