Skip to content

Commit

Permalink
Merge pull request #3 from lambdalisue/pipe_from
Browse files Browse the repository at this point in the history
Add `pipeThroughFrom` function to transform writable stream like `pipeThrough` method of readable streams
  • Loading branch information
lambdalisue authored May 13, 2023
2 parents 7ba38c3 + 36086dc commit e86b337
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ This is a [Deno][deno] module that provides utilities for handling Streams API.

## Usage

### pipeThroughFrom

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"]
```

### channel

`channel` creates a new channel, which is a pair of a readable and writable
Expand All @@ -20,7 +44,7 @@ import { channel } from "./channel.ts";
import { push } from "./push.ts";
import { pop } from "./pop.ts";

const [reader, writer] = channel<number>();
const { reader, writer } = channel<number>();

await push(writer, 1);
await push(writer, 2);
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tasks": {
"test": "deno test --no-lock --unstable -A --parallel",
"test": "deno test --no-lock --unstable -A --parallel --doc",
"check": "deno check --no-lock --unstable $(find . -name '*.ts')",
"upgrade": "deno run --no-lock -A https://deno.land/x/udd/main.ts $(find . -name '*.ts')"
}
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./channel.ts";
export * from "./collect.ts";
export * from "./pipe_through_from.ts";
export * from "./pop.ts";
export * from "./provide.ts";
export * from "./push.ts";
Expand Down
36 changes: 36 additions & 0 deletions pipe_through_from.ts
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;
}
22 changes: 22 additions & 0 deletions pipe_through_from_test.ts
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"]);
},
);
});

0 comments on commit e86b337

Please sign in to comment.