| 30 | } |
| 31 | |
| 32 | export class WrapWritableStream<T> extends WritableStream<T> { |
| 33 | writable!: WritableStream<T>; |
| 34 | |
| 35 | #writer!: WritableStreamDefaultWriter<T>; |
| 36 | |
| 37 | constructor( |
| 38 | start: |
| 39 | | WritableStream<T> |
| 40 | | WrapWritableStreamStart<T> |
| 41 | | WritableStreamWrapper<T>, |
| 42 | ) { |
| 43 | super({ |
| 44 | start: async () => { |
| 45 | const writable = await getWrappedWritableStream(start); |
| 46 | // `start` is called in `super()`, so can't use `this` synchronously. |
| 47 | // but it's fine after the first `await` |
| 48 | this.writable = writable; |
| 49 | this.#writer = this.writable.getWriter(); |
| 50 | }, |
| 51 | write: async (chunk) => { |
| 52 | await this.#writer.write(chunk); |
| 53 | }, |
| 54 | abort: async (reason) => { |
| 55 | await this.#writer.abort(reason); |
| 56 | if (start !== this.writable && "close" in start) { |
| 57 | await start.close?.(); |
| 58 | } |
| 59 | }, |
| 60 | close: async () => { |
| 61 | // Close the inner stream first. |
| 62 | // Usually the inner stream is a logical sub-stream over the outer stream, |
| 63 | // closing the outer stream first will make the inner stream incapable of |
| 64 | // sending data in its `close` handler. |
| 65 | await this.#writer.close(); |
| 66 | if (start !== this.writable && "close" in start) { |
| 67 | await start.close?.(); |
| 68 | } |
| 69 | }, |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | bePipedThroughFrom<U>(transformer: TransformStream<U, T>) { |
| 74 | let promise: Promise<void>; |
| 75 | return new WrapWritableStream<U>({ |
| 76 | start: () => { |
| 77 | promise = transformer.readable.pipeTo(this); |
| 78 | return transformer.writable; |
| 79 | }, |
| 80 | async close() { |
| 81 | await promise; |
| 82 | }, |
| 83 | }); |
| 84 | } |
| 85 | } |
nothing calls this directly
no outgoing calls
no test coverage detected