From c91d110807d900bc6b8aeb629ff319fb8e5b9a49 Mon Sep 17 00:00:00 2001
From: Samuel Cha <sunchipssam@gmail.com>
Date: Tue, 23 Apr 2019 07:20:47 -0500
Subject: [PATCH] chore(*): Use new createContentDigest helper (#8992)

* add createContentDigest helper for gatsby-transformer-remark

* add createContentDigest helper for gatsby-source-filesystem

* Added createContentDigest in test

* Added missing createContentDigest param

* update createContentDigest helper tests for gatsby-transformer-remark

* update createContentDigest helper tests for gatsby-source-filesystem

* update createContentDigest helper tests for gatsby-transformer-csv

* add createContentDigest helper for internal-plugins/internal-data-bridge

* add createContentDigest helper for gatsby-source-hacker-news

* add createContentDigest helper for gatsby-transformer-excel

* add createContentDigest helper for gatsby-source-mongodb

* add createContentDigest helper for gatsby-source-lever

* add createContentDigest helper for internal-plugins/query-runner

* Updated gatsby dep

gatsby-source-hacker-news
gatsby-source-lever
gatsby-source-mongodb
gatsby-transformer-excel

* add createContentDigest helper for gatsby-source-contentful

* add createContentDigest helper for gatsby-transformer-hjson

* add createContentDigest helper for gatsby-transformer-sqip

* add createContentDigest helper for gatsby-source-npm-package-search

* add createContentDigest helper for gatsby-transformer-toml

* update createContentDigest helper for gatsby-source-mongodb

remove direct import

* update createContentDigest helper for gatsby-source-lever

could not find import for createGraphQLNode, added helper anyways

* update createContentDigest helper for internal-plugins/query-runner

modify src/bootstrap (imports writeRedirects)

* update createContentDigest helper for gatsby-source-npm-package-search

* update createContentDigest helper for gatsby-source-contentful*

TODO add ccd (createContentDigest) in cache-image

* update createContentDigest helper for gatsby-transformer-sqip*

TODO add ccd (createContentDigest) in generate-sqip

* update createContentDigest helper for gatsby-transformer-sqip

removed ccd from tests

* use caret version selector

* no need to import it - it's passed to `sourceNodes`

* merge again

* update to pass tests

* update hash to use createContentDigest

* move createContentDigest to import

* move createContentDigest to import for gatsby-source-contentful

* remove createContentDigest import in gatsby-source-filesystem test

* rereplace createContentDigest with crypto in gatsby-source-filesystem

* revert createContentDigest to crypto for create-file-node in gatsby-source-filesystem

* revert createContentDigest for gatsby-plugin-sharp

* update crypto misspell

* revert gatsby-remark-contentful and gatsby-source-contentful

* fix old code

* fix old code v2

* update gatsby pkg

* revert gatsby-transformer-remark

* add export for gatsby utils

* update gatsby-source-filesystem

* revert gatsby-source-graphql

* revert gatsby-source-graphql

* revert gatsby-source-hacker-news

* revert gatsby-source-lever

* revert gatsby-source-medium

* revert gatsby-source-mongodb

* revert gatsby-source-mongodb mapping

* revert gatsby-source-npm-package-search

* revert gatsby-source-wikipedia

* revert gatsby-source-wordpress

* revert gatsby-transformer-csv

* revert gatsby-transformer-documentationjs

* revert gatsby-transformer-excel

* revert gatsby-transformer-hjson

* revert other packages

* revert some pkgs

* add gatsby

* move gatsby to peerdeps
---
 .../gatsby-source-filesystem/package.json     |  2 +-
 .../src/create-file-node.js                   | 12 +++++-----
 .../src/create-remote-file-node.js            | 22 ++-----------------
 .../src/__tests__/extend-node.js              |  2 ++
 .../src/__tests__/on-node-create.js           |  7 ++++++
 .../src/on-node-create.js                     | 15 ++++++++-----
 packages/gatsby/utils.js                      |  1 +
 7 files changed, 27 insertions(+), 34 deletions(-)
 create mode 100644 packages/gatsby/utils.js

diff --git a/packages/gatsby-source-filesystem/package.json b/packages/gatsby-source-filesystem/package.json
index 67f842e7da265..317dc597b5406 100644
--- a/packages/gatsby-source-filesystem/package.json
+++ b/packages/gatsby-source-filesystem/package.json
@@ -36,7 +36,7 @@
   ],
   "license": "MIT",
   "peerDependencies": {
-    "gatsby": "^2.0.0"
+    "gatsby": "^2.0.15"
   },
   "repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem",
   "scripts": {
diff --git a/packages/gatsby-source-filesystem/src/create-file-node.js b/packages/gatsby-source-filesystem/src/create-file-node.js
index 3db6fcadd8483..a11add1b72cf4 100644
--- a/packages/gatsby-source-filesystem/src/create-file-node.js
+++ b/packages/gatsby-source-filesystem/src/create-file-node.js
@@ -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(`gatsby/utils`)
 
 exports.createFileNode = async (
   pathToFile,
@@ -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`,
diff --git a/packages/gatsby-source-filesystem/src/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/create-remote-file-node.js
index a7be7d1431868..c4332e4c87f0c 100644
--- a/packages/gatsby-source-filesystem/src/create-remote-file-node.js
+++ b/packages/gatsby-source-filesystem/src/create-remote-file-node.js
@@ -1,6 +1,6 @@
 const fs = require(`fs-extra`)
 const got = require(`got`)
-const crypto = require(`crypto`)
+const { createContentDigest } = require(`gatsby/utils`)
 const path = require(`path`)
 const { isWebUri } = require(`valid-url`)
 const Queue = require(`better-queue`)
@@ -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`
 
@@ -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)
   }
diff --git a/packages/gatsby-transformer-remark/src/__tests__/extend-node.js b/packages/gatsby-transformer-remark/src/__tests__/extend-node.js
index 11fbf583080b3..7933d6f9aeec7 100644
--- a/packages/gatsby-transformer-remark/src/__tests__/extend-node.js
+++ b/packages/gatsby-transformer-remark/src/__tests__/extend-node.js
@@ -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(
@@ -104,6 +105,7 @@ const bootstrapTest = (
         loadNodeContent,
         actions,
         createNodeId,
+        createContentDigest,
       },
       { ...additionalParameters, ...pluginOptions }
     )
diff --git a/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js b/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js
index 73b25046f29fb..712a9d0d50908 100644
--- a/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js
+++ b/packages/gatsby-transformer-remark/src/__tests__/on-node-create.js
@@ -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
@@ -38,6 +40,7 @@ Where oh where is my little pony?
         loadNodeContent,
         actions,
         createNodeId,
+        createContentDigest,
       }).then(() => {
         expect(actions.createNode.mock.calls).toMatchSnapshot()
         expect(
@@ -75,6 +78,7 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
           loadNodeContent,
           actions,
           createNodeId,
+          createContentDigest,
         },
         { excerpt_separator: `<!-- end -->` }
       ).then(() => {
@@ -106,6 +110,7 @@ yadda yadda
         actions,
         createNodeId,
         loadNodeContent,
+        createContentDigest,
       })
 
       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
           loadNodeContent,
           actions,
           createNodeId,
+          createContentDigest,
         },
         { excerpt_separator: `<!-- end -->` }
       )
@@ -261,6 +267,7 @@ Sed bibendum sem iaculis, pellentesque leo sed, imperdiet ante. Sed consequat ma
         loadNodeContent,
         actions,
         createNodeId,
+        createContentDigest,
       })
     })
   })
diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js
index c2937b08595da..115b9daef9999 100644
--- a/packages/gatsby-transformer-remark/src/on-node-create.js
+++ b/packages/gatsby-transformer-remark/src/on-node-create.js
@@ -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
@@ -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 })
diff --git a/packages/gatsby/utils.js b/packages/gatsby/utils.js
new file mode 100644
index 0000000000000..cb2c70625a460
--- /dev/null
+++ b/packages/gatsby/utils.js
@@ -0,0 +1 @@
+exports.createContentDigest = require(`./dist/utils/create-content-digest`)