From 3cd17070aba4d0f18d6b7b8ce76c76dbb3e05b01 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Sat, 15 Mar 2025 00:50:57 -0500 Subject: [PATCH] fix strict mode sourcemap handling --- src/worker/fetch.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index 60e80cb..e0e568c 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -366,9 +366,16 @@ async function rewriteBody( workertype === "module" ); if (flagEnabled("sourcemaps", meta.base) && map) { - js = - `${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");` + - (js instanceof Uint8Array ? new TextDecoder().decode(js) : js); + if (js instanceof Uint8Array) { + js = new TextDecoder().decode(js); + } + const sourcemapfn = `${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");`; + const strictMode = /^\s*(['"])use strict\1;?/; + if (strictMode.test(js)) { + js = js.replace(strictMode, `$&\n${sourcemapfn}`); + } else { + js = `${sourcemapfn}\n${js}`; + } } return js as unknown as ArrayBuffer;