[streams] pipe redirection (#325)

I started feeling that we've been too optimistic about use of specialized pipeTo algorithm given an identify transform between the pair of rs/ws.

In the discussion about the `Request` object, I've been saying that
- we should be able to `.write()` even before the `Request` gets attached to the final sink (https://github.com/yutakahirano/fetch-with-streams/issues/30#issuecomment-90882279).
- we can run specialized `.pipeTo()` internally even with existence of such a buffer.

The following are reduced example of what I said:

`id` is writable even before `id.readable.pipeTo()` happens.

```
id.writable.write("hello");
id.writable.write("world");
id.readable.pipeTo(ws);
```

In the following two examples, `rs.pipeTo(ws)` should happen eventually and internally after draining chunks queued in `id`.

```
rs.pipeTo(id.writable);
id.readable.pipeTo(ws);  // Eventually rs.pipeTo(ws)
```

```
rs.pipeTo(id.writable);
id.readable.pipeTo(ws);  // Eventually rs.pipeTo(ws)
```

Note that not whole of the `rs.pipeTo(ws)` algorithm should always happen. Closure of `rs` should result in making only `id` closed if `id.readable.pipeTo(ws)` is `id.readable.pipeTo(ws, { preventClose: true})`.

I'd call the main data transfer part of `.pipeTo()` as `.specialDataTransfer()` which excludes close/error signal.

So, let's think of what we should do for the following example:

```
id.writable.write("hello");
id.writable.write("world");
rs.pipeTo(id.writable);
id.readable.pipeTo(ws);
```

`id` already has two chunks queued in it. If `id.readable.pipeTo(ws)` algorithm is the one of `ReadableStream.pipeTo()` as-is, we cannot drain all the data from `id` until `ws` stop exerting back-pressure.

Without an ability to stop `rs.pipeTo(id.writable)`, we never be able to switch to rs.specialDataTransfer(ws).

I guess, the solution is:
- Identity transform flushes all the data queued in it when `id.readable.pipeTo(ws)` is called regardless of back-pressure of `ws`.
- Provide an (possibly private) API, say `.redirectTo`, on the `WritableStream` for `.pipeTo()` algorithm to redirect `.specialDataTransfer()` to `ws`. `ReadableStream.pipeTo()` is required to check `.redirectTo` before every `dest.write()` and if it's set, it must switch to use `rs.specialDataTransfer(dest.redirectTo)`.

Maybe this could be evolved to address #307, too.

---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/325

Received on Thursday, 9 April 2015 06:02:38 UTC