diff --git a/.changeset/eighty-suits-admire.md b/.changeset/eighty-suits-admire.md new file mode 100644 index 0000000000..5e4b065b2a --- /dev/null +++ b/.changeset/eighty-suits-admire.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-node': minor +--- + +**BREAKING** Fixed file corruption for non utf-8 data in fetch contents diff --git a/plugins/scaffolder-node/src/actions/fetch.test.ts b/plugins/scaffolder-node/src/actions/fetch.test.ts index 42f75d02b4..ea7f458fde 100644 --- a/plugins/scaffolder-node/src/actions/fetch.test.ts +++ b/plugins/scaffolder-node/src/actions/fetch.test.ts @@ -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 () => { diff --git a/plugins/scaffolder-node/src/actions/fetch.ts b/plugins/scaffolder-node/src/actions/fetch.ts index 2ddc2b1ae6..6b3e08bc7e 100644 --- a/plugins/scaffolder-node/src/actions/fetch.ts +++ b/plugins/scaffolder-node/src/actions/fetch.ts @@ -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); } }