| 16 | const reader = body.getReader(); |
| 17 | const stream = new ReadableStream({ |
| 18 | async pull(controller) { |
| 19 | const { done, value } = await reader.read(); |
| 20 | |
| 21 | if (done) { |
| 22 | controller.close(); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | const encoder = new TextEncoder(); |
| 27 | const html = value instanceof Uint8Array ? new TextDecoder().decode(value) : String(value); |
| 28 | |
| 29 | if (html.includes(headClosingTag)) { |
| 30 | const modifiedHtml = html.replace(headClosingTag, `${getTraceMetaTags()}${headClosingTag}`); |
| 31 | |
| 32 | controller.enqueue(encoder.encode(modifiedHtml)); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | controller.enqueue(encoder.encode(html)); |
| 37 | }, |
| 38 | }); |
| 39 | |
| 40 | return stream; |