pipe-from.ts 878 B

123456789101112131415161718192021222324252627
  1. import { WritableStream, type ReadableWritablePair } from "./stream.js";
  2. /**
  3. * Create a new `WritableStream` that, when written to, will write that chunk to
  4. * `pair.writable`, when pipe `pair.readable` to `writable`.
  5. *
  6. * It's the opposite of `ReadableStream.pipeThrough`.
  7. *
  8. * @param writable The `WritableStream` to write to.
  9. * @param pair A `TransformStream` that converts chunks.
  10. * @returns A new `WritableStream`.
  11. */
  12. export function pipeFrom<W, T>(writable: WritableStream<W>, pair: ReadableWritablePair<W, T>) {
  13. const writer = pair.writable.getWriter();
  14. const pipe = pair.readable
  15. .pipeTo(writable);
  16. return new WritableStream<T>({
  17. async write(chunk) {
  18. await writer.ready;
  19. await writer.write(chunk);
  20. },
  21. async close() {
  22. await writer.close();
  23. await pipe;
  24. }
  25. });
  26. }