fix!: avoid binary file corruption in fetch action

The `.toString()` caused all non utf-8 data to be corrupted on disk.
This caused issues when downloading binary files such as zip archives.

Fixes #22899

Signed-off-by: RedlineTriad <39059512+RedlineTriad@users.noreply.github.com>
This commit is contained in:
RedlineTriad
2024-02-27 16:23:01 +01:00
parent 94c4fe2782
commit 85f4723b1b
3 changed files with 30 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-node': minor
---
**BREAKING** Fixed file corruption for non utf-8 data in fetch contents
@@ -210,7 +210,26 @@ describe('fetchContents helper', () => {
fetchUrl: 'https://github.com/backstage/foo',
});
expect(fs.ensureDir).toHaveBeenCalledWith('.');
expect(fs.outputFile).toHaveBeenCalledWith('foo', 'test');
expect(fs.outputFile).toHaveBeenCalledWith(
'foo',
Buffer.from([116, 101, 115, 116]),
);
});
it('should fetch binary content from url', async () => {
readUrl.mockResolvedValue({
buffer: () => Buffer.from([0, 1, 2, 3, 255, 254, 253, 252]),
});
await fetchFile({
...options,
outputPath: 'foo',
fetchUrl: 'https://github.com/backstage/foo',
});
expect(fs.ensureDir).toHaveBeenCalledWith('.');
expect(fs.outputFile).toHaveBeenCalledWith(
'foo',
Buffer.from([0, 1, 2, 3, 255, 254, 253, 252]),
);
});
it('should fetch content from url into directory', async () => {
@@ -223,7 +242,10 @@ describe('fetchContents helper', () => {
fetchUrl: 'https://github.com/backstage/foo',
});
expect(fs.ensureDir).toHaveBeenCalledWith('mydir');
expect(fs.outputFile).toHaveBeenCalledWith('mydir/foo', 'test');
expect(fs.outputFile).toHaveBeenCalledWith(
'mydir/foo',
Buffer.from([116, 101, 115, 116]),
);
});
it('should pass through the token provided through to the URL reader', async () => {
+1 -1
View File
@@ -95,7 +95,7 @@ export async function fetchFile(options: {
const res = await reader.readUrl(readUrl, { token });
await fs.ensureDir(path.dirname(outputPath));
const buffer = await res.buffer();
await fs.outputFile(outputPath, buffer.toString());
await fs.outputFile(outputPath, buffer);
}
}