From 54f643b04c296af22df1176397389dd110380791 Mon Sep 17 00:00:00 2001 From: Dimitri Date: Wed, 30 Mar 2022 07:57:45 +0200 Subject: [PATCH] fix(gatsby-source-filesystem): use correct hash when using createFileNodeFromBuffer (#35243) --- packages/gatsby-source-filesystem/README.md | 2 + .../__tests__/create-file-node-from-buffer.js | 39 +++++++++++++++++++ .../src/create-file-node-from-buffer.js | 7 +++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-source-filesystem/README.md b/packages/gatsby-source-filesystem/README.md index 41d3a45538bb3..7b14776de2589 100644 --- a/packages/gatsby-source-filesystem/README.md +++ b/packages/gatsby-source-filesystem/README.md @@ -261,6 +261,8 @@ When working with data that isn't already stored in a file, such as when queryin The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to disk, and creates a file node that points to it. +The name of the file can be passed to the `createFileNodeFromBuffer` helper. If no name is given, the content hash will be used to determine the name. + ## Example usage The following example is adapted from the source of [`gatsby-source-mysql`](https://github.com/malcolm-kee/gatsby-source-mysql): diff --git a/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js b/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js index 3bc46d58ee265..44961e9bbf77a 100644 --- a/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js +++ b/packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js @@ -18,6 +18,7 @@ jest.mock(`../create-file-node`, () => { }) const { ensureDir, writeFile } = require(`fs-extra`) +const { createContentDigest } = require(`gatsby-core-utils`) const { createFileNode } = require(`../create-file-node`) const createFileNodeFromBuffer = require(`../create-file-node-from-buffer`) @@ -118,6 +119,44 @@ describe(`create-file-node-from-buffer`, () => { expect.any(Object) ) }) + + it(`uses hash as filename when no name is provided`, async () => { + expect.assertions(1) + + let outputFilename + writeFile.mockImplementationOnce((filename, buf, cb) => { + outputFilename = filename + cb() + }) + + const buffer = createMockBuffer(`buffer-content`) + await setup({ + hash: `a-given-hash`, + buffer: buffer, + getCache: () => createMockCache(), + }) + + expect(outputFilename).toContain(`a-given-hash.bin`) + }) + + it(`uses generated hash as filename when no name or hash is provided`, async () => { + expect.assertions(1) + + let outputFilename + writeFile.mockImplementationOnce((filename, buf, cb) => { + outputFilename = filename + cb() + }) + + const buffer = createMockBuffer(`buffer-content`) + const expectedHash = createContentDigest(buffer) + await setup({ + buffer: buffer, + getCache: () => createMockCache(), + }) + + expect(outputFilename).toContain(`${expectedHash}.bin`) + }) }) describe(`validation`, () => { diff --git a/packages/gatsby-source-filesystem/src/create-file-node-from-buffer.js b/packages/gatsby-source-filesystem/src/create-file-node-from-buffer.js index 2e42dc40b7a61..f585520dc84c2 100644 --- a/packages/gatsby-source-filesystem/src/create-file-node-from-buffer.js +++ b/packages/gatsby-source-filesystem/src/create-file-node-from-buffer.js @@ -72,7 +72,6 @@ async function processBufferNode({ const filetype = await fileType.fromBuffer(buffer) ext = filetype ? `.${filetype.ext}` : `.bin` } - filename = createFilePath(path.join(pluginCacheDir, hash), name, ext) await fs.ensureDir(path.dirname(filename)) @@ -125,7 +124,7 @@ module.exports = ({ parentNodeId = null, createNodeId, ext, - name = hash, + name, }) => { // validation of the input // without this it's notoriously easy to pass in the wrong `createNodeId` @@ -156,6 +155,10 @@ module.exports = ({ hash = createContentDigest(buffer) } + if (!name) { + name = hash + } + // Check if we already requested node for this remote file // and return stored promise if we did. if (processingCache[hash]) {