Skip to content

Commit

Permalink
feat(gatsby) : add createContentDigest helper (#8687)
Browse files Browse the repository at this point in the history
This PR add a createContentDigest helper and pass it in to the Gatsby API runner.

Closes #8587
  • Loading branch information
rbadr authored and DSchau committed Oct 2, 2018
1 parent ec991a4 commit e8b2696
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 74 deletions.
24 changes: 8 additions & 16 deletions docs/docs/source-plugin-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ With the setup done, move on to adding the plugin's functionality.
Create a new file called `gatsby-node.js` in your `gatsby-source-pixabay` directory, and add the following:

```js:title=gatsby-node.js
const crypto = require("crypto")
const fetch = require("node-fetch")
const queryString = require("query-string")

exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions

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

```js
const crypto = require("crypto")
const fetch = require("node-fetch")
const queryString = require("query-string")
```

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:
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:

```js
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
```
You do some initial setup:
Expand Down Expand Up @@ -211,9 +209,8 @@ Update `gatsby-node.js` in your `plugins/gatsby-source-pixabay/` directory:
```js{11-30}:title=gatsby-node.js
const fetch = require("node-fetch")
const queryString = require("query-string")
const crypto = require("crypto")

exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions

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

exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions

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

const nodeData = Object.assign({}, photo, {
id: nodeId,
Expand All @@ -295,7 +287,7 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
internal: {
type: `PixabayPhoto`,
content: nodeContent,
contentDigest: nodeContentDigest,
contentDigest: createContentDigest(photo),
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Array [
"funny": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "5005aee6b2557974cae0aabc712e125a",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -22,7 +22,7 @@ Array [
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "0af87da7e572c2d6d8493bcd642c2c78",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -41,7 +41,7 @@ Array [
"funny": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "5005aee6b2557974cae0aabc712e125a",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down Expand Up @@ -70,7 +70,7 @@ false,\\"nope\\"",
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "0af87da7e572c2d6d8493bcd642c2c78",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down Expand Up @@ -103,7 +103,7 @@ Array [
"field2": "funny",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "49605f5e32a2250d7ca740c5d2e79784",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -116,7 +116,7 @@ Array [
"field2": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "2bf0bc0e8539ef5f6bbcf67c8b733233",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -129,7 +129,7 @@ Array [
"field2": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f5f617ce4243d639de924543dec5600e",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -148,7 +148,7 @@ Array [
"field2": "funny",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "49605f5e32a2250d7ca740c5d2e79784",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down Expand Up @@ -177,7 +177,7 @@ false,nope",
"field2": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "2bf0bc0e8539ef5f6bbcf67c8b733233",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down Expand Up @@ -206,7 +206,7 @@ false,nope",
"field2": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f5f617ce4243d639de924543dec5600e",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby-transformer-csv/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ describe(`Process nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
createContentDigest,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
Expand All @@ -52,13 +54,15 @@ describe(`Process nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
createContentDigest,
},
{ noheader: true }
).then(() => {
Expand Down
10 changes: 2 additions & 8 deletions packages/gatsby-transformer-csv/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const Promise = require(`bluebird`)
const csv = require(`csvtojson`)
const _ = require(`lodash`)
const crypto = require(`crypto`)

const convertToJson = (data, options) =>
new Promise((res, rej) => {
Expand All @@ -16,7 +15,7 @@ const convertToJson = (data, options) =>
})

async function onCreateNode(
{ node, actions, loadNodeContent, createNodeId },
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
options
) {
const { createNode, createParentChildLink } = actions
Expand All @@ -31,19 +30,14 @@ async function onCreateNode(

if (_.isArray(parsedContent)) {
const csvArray = parsedContent.map((obj, i) => {
const objStr = JSON.stringify(obj)
const contentDigest = crypto
.createHash(`md5`)
.update(objStr)
.digest(`hex`)

return {
...obj,
id: obj.id ? obj.id : createNodeId(`${node.id} [${i}] >>> CSV`),
children: [],
parent: node.id,
internal: {
contentDigest,
contentDigest: createContentDigest(obj),
// TODO make choosing the "type" a lot smarter. This assumes
// the parent node is a file.
// PascalCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "FooJson",
},
"parent": "whatever",
Expand All @@ -28,7 +28,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "FooJson",
},
"parent": "whatever",
Expand Down Expand Up @@ -60,7 +60,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"parent": "whatever",
Expand All @@ -79,7 +79,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"parent": "whatever",
Expand Down Expand Up @@ -109,7 +109,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "NodeNameJson",
},
"parent": "whatever",
Expand All @@ -122,7 +122,7 @@ Array [
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
"contentDigest": "contentDigest",
"type": "NodeNameJson",
},
"parent": "whatever",
Expand All @@ -141,7 +141,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "NodeNameJson",
},
"parent": "whatever",
Expand Down Expand Up @@ -169,7 +169,7 @@ Array [
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
"contentDigest": "contentDigest",
"type": "NodeNameJson",
},
"parent": "whatever",
Expand Down Expand Up @@ -201,7 +201,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"parent": "whatever",
Expand All @@ -214,7 +214,7 @@ Array [
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"parent": "whatever",
Expand All @@ -233,7 +233,7 @@ Array [
"funny": "yup",
"id": "foo",
"internal": Object {
"contentDigest": "8838e569ae02d98806532310fb2a577a",
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"parent": "whatever",
Expand All @@ -259,7 +259,7 @@ Array [
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"parent": "whatever",
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-transformer-json/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const bootstrapTest = async (node, pluginOptions = {}) => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)

return await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
createContentDigest,
},
pluginOptions
).then(() => {
Expand Down
10 changes: 2 additions & 8 deletions packages/gatsby-transformer-json/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const _ = require(`lodash`)
const crypto = require(`crypto`)
const path = require(`path`)

async function onCreateNode(
{ node, actions, loadNodeContent, createNodeId },
{ node, actions, loadNodeContent, createNodeId, createContentDigest },
pluginOptions
) {
function getType({ node, object, isArray }) {
Expand All @@ -21,18 +20,13 @@ async function onCreateNode(
}

function transformObject(obj, id, type) {
const objStr = JSON.stringify(obj)
const contentDigest = crypto
.createHash(`md5`)
.update(objStr)
.digest(`hex`)
const jsonNode = {
...obj,
id,
children: [],
parent: node.id,
internal: {
contentDigest,
contentDigest: createContentDigest(obj),
type,
},
}
Expand Down
Loading

0 comments on commit e8b2696

Please sign in to comment.