()
| 103 | } |
| 104 | |
| 105 | public async toResponse() { |
| 106 | // If we haven't called `send` yet, wait for it to be called. |
| 107 | if (!this.sent) await this.sendPromise.promise |
| 108 | |
| 109 | const body = this.textBody ?? this.transformStream.readable |
| 110 | |
| 111 | let bodyInit: BodyInit = body |
| 112 | |
| 113 | // if the response is streaming, onClose() can still be called after this point. |
| 114 | const canAddListenersLater = typeof bodyInit !== 'string' |
| 115 | const shouldTrackBody = canAddListenersLater |
| 116 | ? true |
| 117 | : this.closeController.listeners > 0 |
| 118 | |
| 119 | if (shouldTrackBody) { |
| 120 | bodyInit = trackBodyConsumed(body, () => { |
| 121 | this.closeController.dispatchClose() |
| 122 | }) |
| 123 | } |
| 124 | |
| 125 | return new Response(bodyInit, { |
| 126 | headers: this.headers, |
| 127 | status: this.statusCode, |
| 128 | statusText: this.statusMessage, |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | public onClose(callback: () => void) { |
| 133 | if (this.closeController.isClosed) { |
no test coverage detected