| 143 | } |
| 144 | |
| 145 | export function renderToWebStream( |
| 146 | input: App | VNode, |
| 147 | context: SSRContext = {}, |
| 148 | ): ReadableStream { |
| 149 | if (typeof ReadableStream !== 'function') { |
| 150 | throw new Error( |
| 151 | `ReadableStream constructor is not available in the global scope. ` + |
| 152 | `If the target environment does support web streams, consider using ` + |
| 153 | `pipeToWebWritable() with an existing WritableStream instance instead.`, |
| 154 | ) |
| 155 | } |
| 156 | |
| 157 | const encoder = new TextEncoder() |
| 158 | let cancelled = false |
| 159 | |
| 160 | return new ReadableStream({ |
| 161 | start(controller) { |
| 162 | renderToSimpleStream(input, context, { |
| 163 | push(content) { |
| 164 | if (cancelled) return |
| 165 | if (content != null) { |
| 166 | controller.enqueue(encoder.encode(content)) |
| 167 | } else { |
| 168 | controller.close() |
| 169 | } |
| 170 | }, |
| 171 | destroy(err) { |
| 172 | controller.error(err) |
| 173 | }, |
| 174 | }) |
| 175 | }, |
| 176 | cancel() { |
| 177 | cancelled = true |
| 178 | }, |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | export function pipeToWebWritable( |
| 183 | input: App | VNode, |