Skip to content

Commit

Permalink
fix: allow blobs in Node.js splitStream fn (#1439)
Browse files Browse the repository at this point in the history
* fix: allow blobs in Node.js splitStream fn

* linting
  • Loading branch information
kuhe authored Oct 23, 2024
1 parent ef24835 commit c8d257b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-fans-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/util-stream": patch
---

allow Blob in node.js splitStream
15 changes: 15 additions & 0 deletions packages/util-stream/src/splitStream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ describe(splitStream.name, () => {
const bytes1 = await webStreamCollector(a);
const bytes2 = await webStreamCollector(b);

expect(bytes1).toEqual(new Uint8Array([97, 98, 99, 100]));
expect(bytes1).toEqual(bytes2);
}
});
it("should split a web:Blob", async () => {
if (typeof Blob !== "undefined") {
const inputChunks = [97, 98, 99, 100];

const myBlob = new Blob([new Uint8Array(inputChunks)]);

const [a, b] = await splitWebStream(myBlob);

const bytes1 = await webStreamCollector(a);
const bytes2 = await webStreamCollector(b);

expect(bytes1).toEqual(new Uint8Array([97, 98, 99, 100]));
expect(bytes1).toEqual(bytes2);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/util-stream/src/splitStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Readable } from "stream";
import { PassThrough } from "stream";

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

/**
* @param stream
Expand All @@ -13,7 +13,7 @@ export async function splitStream(stream: ReadableStream): Promise<[ReadableStre
export async function splitStream(
stream: Readable | ReadableStream
): Promise<[Readable | ReadableStream, Readable | ReadableStream]> {
if (isReadableStream(stream)) {
if (isReadableStream(stream) || isBlob(stream)) {
return splitWebStream(stream);
}
const stream1 = new PassThrough();
Expand Down
7 changes: 7 additions & 0 deletions packages/util-stream/src/stream-type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ type ReadableStreamType = ReadableStream;
export const isReadableStream = (stream: unknown): stream is ReadableStreamType =>
typeof ReadableStream === "function" &&
(stream?.constructor?.name === ReadableStream.name || stream instanceof ReadableStream);

/**
* @internal
*/
export const isBlob = (blob: unknown): blob is Blob => {
return typeof Blob === "function" && (blob?.constructor?.name === Blob.name || blob instanceof Blob);
};

0 comments on commit c8d257b

Please sign in to comment.