lol-html
lol-html copied to clipboard
Awaiting transform of empty response never resolves
Not sure if this is the best place to raise. We are using HTMLRewriter in a Cloudflare and encountered an issue which was difficult to track down.
The issue appears to be that HTMLRewriter when called with a response that has no body returns a new response but the methods to resolve the new response body never appear to resolve. Instead they cause the worker to exception with "... your Worker's code had hung and would never generate a response."
The below shows a simple reproduction example:
export default {
async fetch(request, env, ctx): Promise<Response> {
const rewriter = new HTMLRewriter();
rewriter.on('div', {
element: (el) => {
el.setInnerContent('rewritten content');
}
});
// Never resolves, causes:
// The Workers runtime canceled this request because it detected that your Worker's code had hung and would never generate a response.
// Refer to: https://developers.cloudflare.com/workers/observability/errors/
const rewrittenResponse = await rewriter.transform(new Response('')).text();
// Resolves
const rewrittenResponse = await rewriter.transform(new Response('<div>initial content</div>')).text();
return new Response(rewrittenResponse, {
headers: {
'content-type': 'text/html'
}
});
},
} satisfies ExportedHandler<Env>;
It feels like it would be more consistent for await'ing to resolve with empty content or at least throwing in some way so the cause of the error is clearer.