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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed `streamToString` corrupting multi-byte UTF-8 characters that span a stream chunk boundary.
9 changes: 6 additions & 3 deletions src/streamUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ export function streamToString(s?: NodeJS.ReadableStream | null): Promise<string
return Promise.resolve("");
}
return new Promise((resolve, reject) => {
let b = "";
const chunks: Buffer[] = [];
s.on("error", reject);
s.on("data", (d: Buffer | string) => {
b += d.toString();
// Buffer the raw chunks and decode once at the end. Decoding each chunk
// on its own corrupts any multi-byte character that straddles a chunk
// boundary, turning it into replacement characters.
chunks.push(Buffer.isBuffer(d) ? d : Buffer.from(String(d), "utf8"));
});
Comment on lines +28 to 35

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If the stream emits Uint8Array chunks (which is common in modern Node.js and web-compatible streams), Buffer.isBuffer(d) will return false. This falls back to Buffer.from(String(d), "utf8"). Calling String() on a Uint8Array converts it to a comma-separated string of numbers (e.g., "104,101,108,108,111"), which will corrupt the decoded string.

Since Buffer.concat natively accepts both Buffer and Uint8Array instances, we can safely push them directly. We should explicitly check for Uint8Array (which also covers Buffer since Buffer inherits from Uint8Array in Node.js) and string to handle all cases correctly.

    const chunks: Uint8Array[] = [];
    s.on("error", reject);
    s.on("data", (d: Buffer | string | Uint8Array) => {
      // Buffer the raw chunks and decode once at the end. Decoding each chunk
      // on its own corrupts any multi-byte character that straddles a chunk
      // boundary, turning it into replacement characters.
      if (d instanceof Uint8Array) {
        chunks.push(d);
      } else if (typeof d === "string") {
        chunks.push(Buffer.from(d, "utf8"));
      } else {
        chunks.push(Buffer.from(String(d), "utf8"));
      }
    });

s.once("end", () => resolve(b));
s.once("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
}
14 changes: 14 additions & 0 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as sinon from "sinon";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { Readable } from "stream";

import * as utils from "./utils";
import { FirebaseError } from "./error";
Expand Down Expand Up @@ -294,6 +295,19 @@ describe("utils", () => {
await expect(utils.streamToString(undefined)).to.eventually.equal("");
await expect(utils.streamToString(null)).to.eventually.equal("");
});

it("should decode a multi-byte character split across chunks", async () => {
// "café 中文 😀" holds 2-, 3- and 4-byte UTF-8 characters.
const text = "caf\u00e9 \u4e2d\u6587 \u{1F600}";
const buf = Buffer.from(text, "utf8");
const cut = buf.indexOf(Buffer.from("\u00e9", "utf8")) + 1;
const stream = new Readable();
stream.push(buf.subarray(0, cut));
stream.push(buf.subarray(cut));
stream.push(null);

await expect(utils.streamToString(stream)).to.eventually.equal(text);
});
});

describe("allSettled", () => {
Expand Down