-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe_through_from.ts
34 lines (34 loc) · 1.17 KB
/
pipe_through_from.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Pipes the readable side of a TransformStream to a WritableStream.
* Returns the writable side of the TransformStream for further piping.
*
* ```ts
* import { channel } from "@core/streamutil/channel";
* import { collect } from "@core/streamutil/collect";
* import { pipeThroughFrom } from "@core/streamutil/pipe-through-from";
*
* const encoder = new TextEncoder();
* const output = channel<string>();
* const stream = pipeThroughFrom(output.writer, new TextDecoderStream());
* const writer = stream.getWriter();
*
* await writer.write(encoder.encode("Hello"));
* await writer.write(encoder.encode("World"));
* await writer.close();
* writer.releaseLock();
*
* const result = await collect(output.reader);
* console.log(result); // ["Hello", "World"]
* ```
*
* @param stream The destination WritableStream to pipe the data into.
* @param transform The TransformStream that transforms the input data.
* @returns The writable side of the TransformStream for further piping.
*/
export function pipeThroughFrom<I, O>(
stream: WritableStream<O>,
transform: TransformStream<I, O>,
): WritableStream<I> {
transform.readable.pipeTo(stream);
return transform.writable;
}