-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-core-utils): make createContentDigest deterministic (#19832)
* always return a string * convert content-digest to node-object-hash * remove array sort * update snapshots * use crypto for primitives * fix snapshots
- Loading branch information
Showing
7 changed files
with
102 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
packages/gatsby-core-utils/src/__tests__/__snapshots__/create-content-digest.js.snap
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
const crypto = require(`crypto`) | ||
const objectHash = require(`node-object-hash`) | ||
|
||
const hasher = objectHash({ | ||
coerce: false, | ||
alg: `md5`, | ||
enc: `hex`, | ||
sort: { | ||
map: true, | ||
object: true, | ||
array: false, | ||
set: false, | ||
}, | ||
}) | ||
|
||
const hashPrimitive = input => | ||
crypto | ||
.createHash(`md5`) | ||
.update(input) | ||
.digest(`hex`) | ||
|
||
/** | ||
* @type {import('../index').createContentDigest} | ||
*/ | ||
const createContentDigest = input => { | ||
const content = typeof input !== `string` ? JSON.stringify(input) : input | ||
if (typeof input === `object`) { | ||
return hasher.hash(input) | ||
} | ||
|
||
return crypto | ||
.createHash(`md5`) | ||
.update(content) | ||
.digest(`hex`) | ||
return hashPrimitive(input) | ||
} | ||
|
||
module.exports = createContentDigest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters