From 9a53b70cbb8f439d36cf1f2c91eaa774f6680819 Mon Sep 17 00:00:00 2001 From: jake champion Date: Thu, 21 Nov 2024 14:50:15 +0000 Subject: [PATCH] fix: call `init` when the module is imported and not when we call the `csp` function this avoids a potential race condition where multiple calls to `init` have happened, which ends up causes memory out of bounds errors --- pkg/html_rewriter_bg.wasm | Bin 536422 -> 536422 bytes src/index.ts | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/html_rewriter_bg.wasm b/pkg/html_rewriter_bg.wasm index 2401fb2992cc37404d4e9fc8a9758f6145e1397f..3c21811f8513663a69b77410583222b51cfa22e3 100644 GIT binary patch delta 35 qcmaEMPvO};g@zW!7N!>F7M3ln)B6}1+h_E#0x{e68GY>M761SfE)Mzt delta 35 qcmaEMPvO};g@zW!7N!>F7M3ln)B6}X+Gq5!0x{e68GY>M761Sfmk$8| diff --git a/src/index.ts b/src/index.ts index c675879..a9e546a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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", @@ -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) {