From 75ad8cff4a3dae8ab06cfc50f6be32fcf81ea926 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Mon, 1 Apr 2019 09:41:54 +0200 Subject: [PATCH] don't introduce a breaking change --- packages/gatsby-transformer-toml/package.json | 3 +- .../__snapshots__/gatsby-node.js.snap | 104 ++++++++++++++++++ .../src/__tests__/gatsby-node.js | 48 ++++++++ .../src/create-content-digest.js | 9 ++ .../src/gatsby-node.js | 4 +- 5 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 packages/gatsby-transformer-toml/src/create-content-digest.js diff --git a/packages/gatsby-transformer-toml/package.json b/packages/gatsby-transformer-toml/package.json index de1d67f38ec57..90a6949ab8942 100644 --- a/packages/gatsby-transformer-toml/package.json +++ b/packages/gatsby-transformer-toml/package.json @@ -8,7 +8,6 @@ }, "dependencies": { "@babel/runtime": "^7.0.0", - "bluebird": "^3.5.0", "toml": "^2.3.2" }, "devDependencies": { @@ -25,7 +24,7 @@ ], "license": "MIT", "peerDependencies": { - "gatsby": "^2.0.15" + "gatsby": "^2.0.0" }, "repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-toml", "scripts": { diff --git a/packages/gatsby-transformer-toml/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-transformer-toml/src/__tests__/__snapshots__/gatsby-node.js.snap index 2b6a9c42a2f9b..7c9151dafe4c0 100644 --- a/packages/gatsby-transformer-toml/src/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-transformer-toml/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -103,3 +103,107 @@ Array [ ], ] `; + +exports[`Process TOML nodes correctly fallsback to crypto when createDigestContent is not available 1`] = ` +Array [ + Array [ + Object { + "children": Array [], + "id": "uuid-from-gatsby", + "internal": Object { + "contentDigest": "contentDigest fallback", + "type": "", + }, + "parent": "whatever", + "the": Object { + "hard": Object { + "another_test_string": " Same thing, but with a string #", + "bit#": Object { + "multi_line_array": Array [ + "]", + ], + "what?": "You don't think some user won't do that?", + }, + "harder_test_string": " And when \\"'s are in the string, along with # \\"", + "test_array": Array [ + "] ", + " # ", + ], + "test_array2": Array [ + "Test #11 ]proved that", + "Experiment #9 was a success", + ], + }, + "test_string": "You'll hate me after this - #", + }, + }, + ], +] +`; + +exports[`Process TOML nodes correctly fallsback to crypto when createDigestContent is not available 2`] = ` +Array [ + Array [ + Object { + "child": Object { + "children": Array [], + "id": "uuid-from-gatsby", + "internal": Object { + "contentDigest": "contentDigest fallback", + "type": "", + }, + "parent": "whatever", + "the": Object { + "hard": Object { + "another_test_string": " Same thing, but with a string #", + "bit#": Object { + "multi_line_array": Array [ + "]", + ], + "what?": "You don't think some user won't do that?", + }, + "harder_test_string": " And when \\"'s are in the string, along with # \\"", + "test_array": Array [ + "] ", + " # ", + ], + "test_array2": Array [ + "Test #11 ]proved that", + "Experiment #9 was a success", + ], + }, + "test_string": "You'll hate me after this - #", + }, + }, + "parent": Object { + "children": Array [], + "content": " + [the] + test_string = \\"You'll hate me after this - #\\" + + [the.hard] + test_array = [ \\"] \\", \\" # \\"] # ] + test_array2 = [ \\"Test #11 ]proved that\\", \\"Experiment #9 was a success\\" ] + another_test_string = \\" Same thing, but with a string #\\" + harder_test_string = \\" And when /\\"'s are in the string, along with # /\\"\\" + # Things will get harder + + [the.hard.\\"bit#\\"] + \\"what?\\" = \\"You don't think some user won't do that?\\" + multi_line_array = [ + \\"]\\", + # ] Oh yes I did + ] + ", + "extension": "toml", + "id": "whatever", + "internal": Object { + "contentDigest": "whatever", + }, + "name": "test", + "parent": null, + }, + }, + ], +] +`; diff --git a/packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js index 4a763a4d0d320..f070ed1de0fb0 100644 --- a/packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js @@ -43,6 +43,8 @@ describe(`Process TOML nodes correctly`, () => { const createNodeId = jest.fn() createNodeId.mockReturnValue(`uuid-from-gatsby`) const createDigestContent = jest.fn().mockReturnValue(`contentDigest`) + jest.mock(`../create-content-digest`) + const createDigestContentFallback = require(`../create-content-digest`) await onCreateNode({ node, @@ -50,6 +52,52 @@ describe(`Process TOML nodes correctly`, () => { actions, createNodeId, createDigestContent, + }).then(() => { + expect(createNode.mock.calls).toMatchSnapshot() + expect(createParentChildLink.mock.calls).toMatchSnapshot() + expect(createNode).toHaveBeenCalledTimes(1) + expect(createParentChildLink).toHaveBeenCalledTimes(1) + expect(createDigestContent).toHaveBeenCalledTimes(1) + expect(createDigestContentFallback).toHaveBeenCalledTimes(0) + }) + }) + + it(`fallsback to crypto when createDigestContent is not available`, async () => { + // Unfortunately due to TOML limitations no JSON -> TOML convertors exist, + // which means that we are stuck with JS template literals. + node.content = ` + [the] + test_string = "You'll hate me after this - #" + + [the.hard] + test_array = [ "] ", " # "] # ] + test_array2 = [ "Test #11 ]proved that", "Experiment #9 was a success" ] + another_test_string = " Same thing, but with a string #" + harder_test_string = " And when \\"'s are in the string, along with # \\"" + # Things will get harder + + [the.hard."bit#"] + "what?" = "You don't think some user won't do that?" + multi_line_array = [ + "]", + # ] Oh yes I did + ] + ` + + const createNode = jest.fn() + const createParentChildLink = jest.fn() + const actions = { createNode, createParentChildLink } + const createNodeId = jest.fn() + createNodeId.mockReturnValue(`uuid-from-gatsby`) + jest.mock(`../create-content-digest`) + const createDigestContent = require(`../create-content-digest`) + createDigestContent.mockImplementation(() => `contentDigest fallback`) + + await onCreateNode({ + node, + loadNodeContent, + actions, + createNodeId, }).then(() => { expect(createNode.mock.calls).toMatchSnapshot() expect(createParentChildLink.mock.calls).toMatchSnapshot() diff --git a/packages/gatsby-transformer-toml/src/create-content-digest.js b/packages/gatsby-transformer-toml/src/create-content-digest.js new file mode 100644 index 0000000000000..2a9ad3ac65836 --- /dev/null +++ b/packages/gatsby-transformer-toml/src/create-content-digest.js @@ -0,0 +1,9 @@ +// TODO remove in gatsby v3 +// keeps old behaviour in tact when createDigestContent is not available +const crypto = require(`crypto`) + +module.exports = content => + crypto + .createHash(`md5`) + .update(content) + .digest(`hex`) diff --git a/packages/gatsby-transformer-toml/src/gatsby-node.js b/packages/gatsby-transformer-toml/src/gatsby-node.js index 847ee026807be..e232cecbc9ad4 100644 --- a/packages/gatsby-transformer-toml/src/gatsby-node.js +++ b/packages/gatsby-transformer-toml/src/gatsby-node.js @@ -24,7 +24,9 @@ async function onCreateNode({ // 1) More TOML files -> more types // 2) Different files with the same name creating conflicts const parsedContentStr = JSON.stringify(parsedContent) - const contentDigest = createDigestContent(parsedContentStr) + const contentDigest = createDigestContent + ? createDigestContent(parsedContentStr) + : require(`./create-content-digest`)(parsedContentStr) const newNode = { ...parsedContent,