Skip to content

Commit

Permalink
DRY isReadableStream typeguard
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jul 12, 2024
1 parent 6010af3 commit 2073c8c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .changeset/metal-snakes-remember.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@smithy/util-stream": minor
---

add stream splitting to sdkStreamMixin
add splitStream and headStream utilities
5 changes: 1 addition & 4 deletions packages/util-stream/src/headStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable, Writable } from "stream";

import { headStream as headWebStream } from "./headStream.browser";
import { isReadableStreamInstance } from "./isReadableStream";

/**
* @internal
Expand All @@ -11,7 +12,6 @@ import { headStream as headWebStream } from "./headStream.browser";
*/
export const headStream = (stream: Readable | ReadableStream, bytes: number): Promise<Uint8Array> => {
if (isReadableStreamInstance(stream)) {
// Web stream API in Node.js
return headWebStream(stream, bytes);
}
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -47,6 +47,3 @@ class Collector extends Writable {
callback();
}
}

const isReadableStreamInstance = (stream: unknown): stream is ReadableStream =>
typeof ReadableStream === "function" && stream instanceof ReadableStream;
5 changes: 5 additions & 0 deletions packages/util-stream/src/isReadableStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @internal
*/
export const isReadableStreamInstance = (stream: unknown): stream is ReadableStream =>
typeof ReadableStream === "function" && stream?.constructor?.name === ReadableStream.name;
5 changes: 2 additions & 3 deletions packages/util-stream/src/sdk-stream-mixin.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { toBase64 } from "@smithy/util-base64";
import { toHex } from "@smithy/util-hex-encoding";
import { toUtf8 } from "@smithy/util-utf8";

import { isReadableStreamInstance } from "./isReadableStream";

const ERR_MSG_STREAM_HAS_BEEN_TRANSFORMED = "The stream has already been transformed.";

/**
Expand Down Expand Up @@ -74,6 +76,3 @@ export const sdkStreamMixin = (stream: unknown): SdkStream<ReadableStream | Blob
};

const isBlobInstance = (stream: unknown): stream is Blob => typeof Blob === "function" && stream instanceof Blob;

const isReadableStreamInstance = (stream: unknown): stream is ReadableStream =>
typeof ReadableStream === "function" && stream instanceof ReadableStream;
14 changes: 12 additions & 2 deletions packages/util-stream/src/splitStream.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import type { Readable as IReadable } from "stream";
import type { Readable } from "stream";
import { PassThrough } from "stream";

import { isReadableStreamInstance } from "./isReadableStream";
import { splitStream as splitWebStream } from "./splitStream.browser";

/**
* @param stream
* @returns stream split into two identical streams.
*/
export async function splitStream(stream: IReadable): Promise<[IReadable, IReadable]> {
export async function splitStream(stream: Readable): Promise<[Readable, Readable]>;
export async function splitStream(stream: ReadableStream): Promise<[ReadableStream, ReadableStream]>;
export async function splitStream(
stream: Readable | ReadableStream
): Promise<[Readable | ReadableStream, Readable | ReadableStream]> {
if (isReadableStreamInstance(stream)) {
return splitWebStream(stream);
}
const stream1 = new PassThrough();
const stream2 = new PassThrough();
stream.pipe(stream1);
Expand Down

0 comments on commit 2073c8c

Please sign in to comment.