Rewrite urls in the response body

I am looking to rewrite the domain of urls in the body of all responses before the are returned to the browser.
Currently I have:
var html = await response.text()
// fix paths in body
html = replaceDomainInResponse(html)
var newResponse = new Response(html, response, { })
return newResponse

But I think I am being caught out by responses that are returned in chunks? (the response body is corrupt) How can I achieve the same thing with the “streaming model”? Thanks so much in advance!
### Response Buffering
Enable or disable buffering of responses from the origin server
Requires Enterprise or higher