Skip to content

Commit c8d257b

Browse files
authored
fix: allow blobs in Node.js splitStream fn (#1439)
* fix: allow blobs in Node.js splitStream fn * linting
1 parent ef24835 commit c8d257b

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

.changeset/rotten-fans-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/util-stream": patch
3+
---
4+
5+
allow Blob in node.js splitStream

packages/util-stream/src/splitStream.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ describe(splitStream.name, () => {
3636
const bytes1 = await webStreamCollector(a);
3737
const bytes2 = await webStreamCollector(b);
3838

39+
expect(bytes1).toEqual(new Uint8Array([97, 98, 99, 100]));
40+
expect(bytes1).toEqual(bytes2);
41+
}
42+
});
43+
it("should split a web:Blob", async () => {
44+
if (typeof Blob !== "undefined") {
45+
const inputChunks = [97, 98, 99, 100];
46+
47+
const myBlob = new Blob([new Uint8Array(inputChunks)]);
48+
49+
const [a, b] = await splitWebStream(myBlob);
50+
51+
const bytes1 = await webStreamCollector(a);
52+
const bytes2 = await webStreamCollector(b);
53+
3954
expect(bytes1).toEqual(new Uint8Array([97, 98, 99, 100]));
4055
expect(bytes1).toEqual(bytes2);
4156
}

packages/util-stream/src/splitStream.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Readable } from "stream";
22
import { PassThrough } from "stream";
33

44
import { splitStream as splitWebStream } from "./splitStream.browser";
5-
import { isReadableStream } from "./stream-type-check";
5+
import { isBlob, isReadableStream } from "./stream-type-check";
66

77
/**
88
* @param stream
@@ -13,7 +13,7 @@ export async function splitStream(stream: ReadableStream): Promise<[ReadableStre
1313
export async function splitStream(
1414
stream: Readable | ReadableStream
1515
): Promise<[Readable | ReadableStream, Readable | ReadableStream]> {
16-
if (isReadableStream(stream)) {
16+
if (isReadableStream(stream) || isBlob(stream)) {
1717
return splitWebStream(stream);
1818
}
1919
const stream1 = new PassThrough();

packages/util-stream/src/stream-type-check.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ type ReadableStreamType = ReadableStream;
1212
export const isReadableStream = (stream: unknown): stream is ReadableStreamType =>
1313
typeof ReadableStream === "function" &&
1414
(stream?.constructor?.name === ReadableStream.name || stream instanceof ReadableStream);
15+
16+
/**
17+
* @internal
18+
*/
19+
export const isBlob = (blob: unknown): blob is Blob => {
20+
return typeof Blob === "function" && (blob?.constructor?.name === Blob.name || blob instanceof Blob);
21+
};

0 commit comments

Comments
 (0)