Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-fileblob-utf8-stream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vercel/build-utils': patch
---

Preserve 4-byte UTF-8 characters when streaming string `FileBlob` contents (fixes Edge Function corruption during `vercel build`).
10 changes: 9 additions & 1 deletion packages/build-utils/src/file-blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ export default class FileBlob implements FileBase {
}

toStream(): NodeJS.ReadableStream {
return intoStream(this.data);
// Encode strings before streaming. into-stream@5 slices strings with
// String#slice at the ~16KiB highWaterMark; a UTF-16 surrogate pair
// (any 4-byte UTF-8 character) on that boundary is split and each half
// becomes U+FFFD when re-encoded, corrupting Edge Function bundles.
const data =
typeof this.data === 'string'
? Buffer.from(this.data, 'utf8')
: this.data;
return intoStream(data);
}
}
28 changes: 28 additions & 0 deletions packages/build-utils/test/unit.download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,34 @@ describe('download()', () => {
strictEqual(linkTarget, 'b.txt');
});

it('should preserve 4-byte UTF-8 characters at the 16KiB stream boundary', async () => {
// into-stream@5 slices string FileBlob data at highWaterMark (~16384
// UTF-16 code units). A surrogate pair starting at offset 16383 is
// split across chunks and each half becomes U+FFFD when encoded.
const astral = '\u{10437}';
const source = `${'a'.repeat(16383)}${astral}b`;
const expected = Buffer.from(source, 'utf8');
expect(expected.includes(Buffer.from('efbfbd', 'hex'))).toBe(false);

const outDir = path.join(__dirname, 'utf8-stream-out');
await fs.remove(outDir);

await download(
{
'index.js': new FileBlob({
mode: S_IFREG,
contentType: 'application/javascript',
data: source,
}),
},
outDir
);

const written = await fs.readFile(path.join(outDir, 'index.js'));
expect(written.equals(expected)).toBe(true);
expect(written.includes(Buffer.from('efbfbd', 'hex'))).toBe(false);
});

it('should create empty directory entries', async () => {
const outDir = path.join(__dirname, 'symlinks-out');
await fs.remove(outDir);
Expand Down
Loading