Skip to content

Commit

Permalink
fix: Add fallback for createContentDigest (gatsbyjs#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 cdd491b commit c356849
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
12 changes: 5 additions & 7 deletions 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 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 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`)
}
}

0 comments on commit c356849

Please sign in to comment.