Skip to content

Commit

Permalink
fix: free the underlying HTMLRewriter if the TransformStream has been…
Browse files Browse the repository at this point in the history
… cancelled
  • Loading branch information
JakeChampion committed Jan 20, 2025
1 parent ec2a920 commit f0ea6e0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/html_rewriter_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class HTMLRewriter {
response = new Response(response.body, response);
}

let rewriter: RawHTMLRewriter;
let rewriter: RawHTMLRewriter | undefined;
const transformStream = new TransformStream<Uint8Array, Uint8Array>({
start: (controller) => {
// Create a rewriter instance for this transformation that writes its
Expand All @@ -39,10 +39,21 @@ export class HTMLRewriter {
rewriter.on(selector, handlers);
}
},
transform: (chunk) => rewriter.write(chunk),
transform: (chunk) => rewriter?.write(chunk),
// deno-lint-ignore require-await
async cancel() {
if (rewriter) {
rewriter.end();
rewriter.free();
rewriter = undefined;
}
},
flush: () => {
rewriter.end();
rewriter.free();
if (rewriter) {
rewriter.end();
rewriter.free();
rewriter = undefined;
}
},
});

Expand Down

0 comments on commit f0ea6e0

Please sign in to comment.