-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lambdalisue/pipe_from
Add `pipeThroughFrom` function to transform writable stream like `pipeThrough` method of readable streams
- Loading branch information
Showing
5 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Pipes the readable side of a TransformStream to a WritableStream. | ||
* Returns the writable side of the TransformStream for further piping. | ||
* | ||
* ```ts | ||
* import { channel } from "./channel.ts"; | ||
* import { collect } from "./collect.ts"; | ||
* import { pipeThroughFrom } from "./pipe_through_from.ts"; | ||
* | ||
* 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"] | ||
* ``` | ||
* | ||
* @template I The type of data that the readable side of the TransformStream accepts. | ||
* @template O The type of data that the TransformStream transforms the input data into. | ||
* @param {WritableStream<O>} stream The destination WritableStream to pipe the data into. | ||
* @param {TransformStream<I, O>} transform The TransformStream that transforms the input data. | ||
* @returns {WritableStream<I>} 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { collect } from "./collect.ts"; | ||
import { channel } from "./channel.ts"; | ||
import { pipeThroughFrom } from "./pipe_through_from.ts"; | ||
|
||
Deno.test("pipeThroughFrom", async (t) => { | ||
await t.step( | ||
"returns a writable stream that pipe through to the specified writable stream", | ||
async () => { | ||
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); | ||
assertEquals(result, ["Hello", "World"]); | ||
}, | ||
); | ||
}); |