Skip to content

Commit

Permalink
fix(gatsby-source-filesystem): use correct hash when using createFile…
Browse files Browse the repository at this point in the history
…NodeFromBuffer (#35243)
  • Loading branch information
g00glen00b committed Mar 30, 2022
1 parent 6328c9d commit 54f643b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby-source-filesystem/README.md
Expand Up @@ -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):
Expand Down
Expand Up @@ -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`)

Expand Down Expand Up @@ -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`, () => {
Expand Down
Expand Up @@ -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))

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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]) {
Expand Down

0 comments on commit 54f643b

Please sign in to comment.