| 48 | * when any of them is closed, all other streams will be closed as well. |
| 49 | */ |
| 50 | export class DuplexStreamFactory<R, W> { |
| 51 | #readableControllers: ReadableStreamDefaultController<R>[] = []; |
| 52 | #writers: WritableStreamDefaultWriter<W>[] = []; |
| 53 | |
| 54 | #writableClosed = false; |
| 55 | get writableClosed() { |
| 56 | return this.#writableClosed; |
| 57 | } |
| 58 | |
| 59 | #closed = new PromiseResolver<void>(); |
| 60 | get closed() { |
| 61 | return this.#closed.promise; |
| 62 | } |
| 63 | |
| 64 | readonly #options: DuplexStreamFactoryOptions; |
| 65 | |
| 66 | constructor(options?: DuplexStreamFactoryOptions) { |
| 67 | this.#options = options ?? {}; |
| 68 | } |
| 69 | |
| 70 | wrapReadable( |
| 71 | readable: ReadableStream<R>, |
| 72 | strategy?: QueuingStrategy<R>, |
| 73 | ): WrapReadableStream<R> { |
| 74 | return new WrapReadableStream<R>( |
| 75 | { |
| 76 | start: (controller) => { |
| 77 | this.#readableControllers.push(controller); |
| 78 | return readable; |
| 79 | }, |
| 80 | cancel: async () => { |
| 81 | // cancel means the local peer wants to close the connection. |
| 82 | await this.close(); |
| 83 | }, |
| 84 | close: async () => { |
| 85 | // stream end means the remote peer closed the connection first. |
| 86 | await this.dispose(); |
| 87 | }, |
| 88 | }, |
| 89 | strategy, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | createWritable(stream: WritableStream<W>): WritableStream<W> { |
| 94 | const writer = stream.getWriter(); |
| 95 | this.#writers.push(writer); |
| 96 | |
| 97 | // `WritableStream` has no way to tell if the remote peer has closed the connection. |
| 98 | // So it only triggers `close`. |
| 99 | return new WritableStream<W>({ |
| 100 | write: async (chunk) => { |
| 101 | await writer.write(chunk); |
| 102 | }, |
| 103 | abort: async (reason) => { |
| 104 | await writer.abort(reason); |
| 105 | await this.close(); |
| 106 | }, |
| 107 | close: async () => { |
nothing calls this directly
no outgoing calls
no test coverage detected