Merge pull request #23289 from RedlineTriad/fix-fetch-for-binary-files

fix!: avoid binary file corruption in fetch action
This commit is contained in:
Fredrik Adelöw
2024-02-27 16:51:17 +01:00
committed by GitHub
3 changed files with 30 additions and 3 deletions
@@ -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);
}
}