Skip to content

Commit

Permalink
fix: call init when the module is imported and not when we call the…
Browse files Browse the repository at this point in the history
… `csp` function

this avoids a potential race condition where multiple calls to `init` have happened, which ends up causes memory out of bounds errors
  • Loading branch information
JakeChampion committed Nov 21, 2024
1 parent 03889b4 commit c590b96
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
Binary file modified pkg/html_rewriter_bg.wasm
Binary file not shown.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import init from "../pkg/html_rewriter.js";
import { HTMLRewriter } from "./html_rewriter_wrapper.ts";
import { Element } from "./types.d.ts";

import { default as init } from "../pkg/html_rewriter.js";
await init();
type Params = {
/**
* When true, uses the `Content-Security-Policy-Report-Only` header instead
Expand Down Expand Up @@ -59,7 +60,7 @@ function uInt8ArrayToBase64String(input: Uint8Array): string {
return btoa(res);
}

export async function csp(originalResponse: Response, params?: Params) {
export function csp(originalResponse: Response, params?: Params) {
const isHTMLResponse = originalResponse.headers.get("content-type")
?.startsWith(
"text/html",
Expand Down Expand Up @@ -146,7 +147,6 @@ export async function csp(originalResponse: Response, params?: Params) {
}

const querySelectors = ["script", 'link[rel="preload"][as="script"]'];
await init();
return new HTMLRewriter()
.on(querySelectors.join(","), {
element(element: Element) {
Expand Down

0 comments on commit c590b96

Please sign in to comment.