Skip to content

Commit 2631b7f

Browse files
kuhetrivikr
andcommitted
Update packages/node-http-handler/src/stream-collector/index.ts
Co-authored-by: Trivikram Kamat <[email protected]>
1 parent 6630230 commit 2631b7f

File tree

1 file changed

+11
-4
lines changed
  • packages/node-http-handler/src/stream-collector

1 file changed

+11
-4
lines changed

packages/node-http-handler/src/stream-collector/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { StreamCollector } from "@smithy/types";
22
import { Readable } from "stream";
3+
import type { ReadableStream as IReadableStream } from "stream/web";
34

45
import { Collector } from "./collector";
56

6-
export const streamCollector: StreamCollector = (stream: Readable | ReadableStream): Promise<Uint8Array> => {
7+
export const streamCollector: StreamCollector = (stream: Readable | IReadableStream): Promise<Uint8Array> => {
78
if (isReadableStreamInstance(stream)) {
8-
// web stream in Node.js indicates user has overridden requestHandler with FetchHttpHandler.
9+
// Web stream API in Node.js
910
return collectReadableStream(stream);
1011
}
1112
return new Promise((resolve, reject) => {
@@ -24,10 +25,16 @@ export const streamCollector: StreamCollector = (stream: Readable | ReadableStre
2425
});
2526
};
2627

27-
const isReadableStreamInstance = (stream: unknown): stream is ReadableStream =>
28+
/**
29+
* Note: the global.ReadableStream object is marked experimental, and was added in v18.0.0 of Node.js.
30+
* The importable version was added in v16.5.0. We only test for the global version so as not to
31+
* enforce an import on a Node.js version that may not have it, and import
32+
* only the type from stream/web.
33+
*/
34+
const isReadableStreamInstance = (stream: unknown): stream is IReadableStream =>
2835
typeof ReadableStream === "function" && stream instanceof ReadableStream;
2936

30-
async function collectReadableStream(stream: ReadableStream): Promise<Uint8Array> {
37+
async function collectReadableStream(stream: IReadableStream): Promise<Uint8Array> {
3138
let res = new Uint8Array(0);
3239
const reader = stream.getReader();
3340
let isDone = false;

0 commit comments

Comments
 (0)