1
1
import { StreamCollector } from "@smithy/types" ;
2
2
import { Readable } from "stream" ;
3
+ import type { ReadableStream as IReadableStream } from "stream/web" ;
3
4
4
5
import { Collector } from "./collector" ;
5
6
6
- export const streamCollector : StreamCollector = ( stream : Readable | ReadableStream ) : Promise < Uint8Array > => {
7
+ export const streamCollector : StreamCollector = ( stream : Readable | IReadableStream ) : Promise < Uint8Array > => {
7
8
if ( isReadableStreamInstance ( stream ) ) {
8
- // web stream in Node.js indicates user has overridden requestHandler with FetchHttpHandler.
9
+ // Web stream API in Node.js
9
10
return collectReadableStream ( stream ) ;
10
11
}
11
12
return new Promise ( ( resolve , reject ) => {
@@ -24,10 +25,16 @@ export const streamCollector: StreamCollector = (stream: Readable | ReadableStre
24
25
} ) ;
25
26
} ;
26
27
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 =>
28
35
typeof ReadableStream === "function" && stream instanceof ReadableStream ;
29
36
30
- async function collectReadableStream ( stream : ReadableStream ) : Promise < Uint8Array > {
37
+ async function collectReadableStream ( stream : IReadableStream ) : Promise < Uint8Array > {
31
38
let res = new Uint8Array ( 0 ) ;
32
39
const reader = stream . getReader ( ) ;
33
40
let isDone = false ;
0 commit comments