Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions packages/fetch-http-handler/src/stream-collector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ describe("streamCollector", () => {
done();
});
});

it("returns a Uint8Array when blob is empty", done => {
const expected = new Uint8Array(0);

const dataPromise = new Response(expected.buffer)
.blob()
.then(blob => streamCollector(blob));
dataPromise.then((data: any) => {
expect(data).toEqual(Uint8Array.from([]));
done();
});
});
});
5 changes: 4 additions & 1 deletion packages/fetch-http-handler/src/stream-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ function readToBase64(blob: Blob): Promise<string> {
return reject(new Error("Reader aborted too early"));
}
const result = reader.result as string;
const dataOffset = result.indexOf(",") + 1;
// Response can include only 'data:' for empty blob, return empty string in this case.
// Otherwise, return the string after ','
const commaIndex = result.indexOf(",");
const dataOffset = commaIndex > -1 ? commaIndex + 1 : result.length;
resolve(result.substring(dataOffset));
};
reader.onabort = () => reject(new Error("Read aborted"));
Expand Down