Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-source-filesystem): use correct hash when using createFileNodeFromBuffer #35243

Merged
merged 3 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/gatsby-source-filesystem/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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