Skip to content

Commit 54f643b

Browse files
authored
fix(gatsby-source-filesystem): use correct hash when using createFileNodeFromBuffer (#35243)
1 parent 6328c9d commit 54f643b

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

packages/gatsby-source-filesystem/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ When working with data that isn't already stored in a file, such as when queryin
261261

262262
The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to disk, and creates a file node that points to it.
263263

264+
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.
265+
264266
## Example usage
265267

266268
The following example is adapted from the source of [`gatsby-source-mysql`](https://github.com/malcolm-kee/gatsby-source-mysql):

packages/gatsby-source-filesystem/src/__tests__/create-file-node-from-buffer.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jest.mock(`../create-file-node`, () => {
1818
})
1919

2020
const { ensureDir, writeFile } = require(`fs-extra`)
21+
const { createContentDigest } = require(`gatsby-core-utils`)
2122
const { createFileNode } = require(`../create-file-node`)
2223
const createFileNodeFromBuffer = require(`../create-file-node-from-buffer`)
2324

@@ -118,6 +119,44 @@ describe(`create-file-node-from-buffer`, () => {
118119
expect.any(Object)
119120
)
120121
})
122+
123+
it(`uses hash as filename when no name is provided`, async () => {
124+
expect.assertions(1)
125+
126+
let outputFilename
127+
writeFile.mockImplementationOnce((filename, buf, cb) => {
128+
outputFilename = filename
129+
cb()
130+
})
131+
132+
const buffer = createMockBuffer(`buffer-content`)
133+
await setup({
134+
hash: `a-given-hash`,
135+
buffer: buffer,
136+
getCache: () => createMockCache(),
137+
})
138+
139+
expect(outputFilename).toContain(`a-given-hash.bin`)
140+
})
141+
142+
it(`uses generated hash as filename when no name or hash is provided`, async () => {
143+
expect.assertions(1)
144+
145+
let outputFilename
146+
writeFile.mockImplementationOnce((filename, buf, cb) => {
147+
outputFilename = filename
148+
cb()
149+
})
150+
151+
const buffer = createMockBuffer(`buffer-content`)
152+
const expectedHash = createContentDigest(buffer)
153+
await setup({
154+
buffer: buffer,
155+
getCache: () => createMockCache(),
156+
})
157+
158+
expect(outputFilename).toContain(`${expectedHash}.bin`)
159+
})
121160
})
122161

123162
describe(`validation`, () => {

packages/gatsby-source-filesystem/src/create-file-node-from-buffer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ async function processBufferNode({
7272
const filetype = await fileType.fromBuffer(buffer)
7373
ext = filetype ? `.${filetype.ext}` : `.bin`
7474
}
75-
7675
filename = createFilePath(path.join(pluginCacheDir, hash), name, ext)
7776
await fs.ensureDir(path.dirname(filename))
7877

@@ -125,7 +124,7 @@ module.exports = ({
125124
parentNodeId = null,
126125
createNodeId,
127126
ext,
128-
name = hash,
127+
name,
129128
}) => {
130129
// validation of the input
131130
// without this it's notoriously easy to pass in the wrong `createNodeId`
@@ -156,6 +155,10 @@ module.exports = ({
156155
hash = createContentDigest(buffer)
157156
}
158157

158+
if (!name) {
159+
name = hash
160+
}
161+
159162
// Check if we already requested node for this remote file
160163
// and return stored promise if we did.
161164
if (processingCache[hash]) {

0 commit comments

Comments
 (0)