mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
fix(webview): rewrite dynamic imports only inside script bodies
The root-absolute import("/...") rewrite ran over the whole document, so
visible text such as <pre>import("/x")</pre> was rewritten too. Run the
specifier rewrite per <script> body and leave markup text untouched.
This commit is contained in:
1 parent
cc413afdd1
commit
06100a5506
4 files changed
+63
-18
No files matched your search
@@ -2,4 +2,4 @@
|
||||
"aicodeman": patch
|
||||
---
|
||||
|
||||
Web tabs whose dashboard ships a relative `<base href>` render again. The proxy rewrites the document URL to strip its `/webview/<cap>/` prefix, and a relative base is resolved against that URL — so `./` (what Vite builds emit, DeepSeek Harness among them) pointed at Codeman's root: every module script 404'd and the page painted its background with nothing on it. The rewrite now resolves such a base against the document's own directory inside the proxy prefix before the mask runs (a page at `/webview/<cap>/app/page.html` gets `/webview/<cap>/app/`, not the proxy root); a cross-origin base is left exactly as the author wrote it.
|
||||
Web tabs whose dashboard ships a relative `<base href>` render again. The proxy rewrites the document URL to strip its `/webview/<cap>/` prefix, and a relative base is resolved against that URL — so `./` (what Vite builds emit, DeepSeek Harness among them) pointed at Codeman's root: every module script 404'd and the page painted its background with nothing on it. The rewrite now resolves such a base against the document's own directory inside the proxy prefix before the mask runs (a page at `/webview/<cap>/app/page.html` gets `/webview/<cap>/app/`, not the proxy root); a cross-origin base is left exactly as the author wrote it. The root-absolute `import("/...")` rewrite is confined to `<script>` bodies, so a `<pre>import("/x")</pre>` example is no longer rewritten as if it were a loader.
|
||||
+7
-5
@@ -152,11 +152,13 @@ layers cooperate so a dashboard talking to its own backend just works:
|
||||
resolved against that URL, so `./` would otherwise point at Codeman's root and
|
||||
404 every module script behind an otherwise-painted page.
|
||||
2. Attribute rewriting handles root-absolute `src`/`href`/`action` in the page the
|
||||
proxy serves — and root-absolute dynamic `import("/...")` specifiers in the
|
||||
initial HTML. The import is the one URL no other layer can reach: `<base>` does
|
||||
not apply to a `/`-rooted specifier and it is not a `fetch()`. SvelteKit's inline
|
||||
bootstrap is exactly this shape; without the rewrite the module loads from
|
||||
Codeman's root, 404s, and the app sits on its own loading spinner forever.
|
||||
proxy serves — and root-absolute dynamic `import("/...")` specifiers inside
|
||||
`<script>` bodies. The import is the one URL no other layer can reach: `<base>`
|
||||
does not apply to a `/`-rooted specifier and it is not a `fetch()`. SvelteKit's
|
||||
inline bootstrap is exactly this shape; without the rewrite the module loads
|
||||
from Codeman's root, 404s, and the app sits on its own loading spinner forever.
|
||||
Visible text is left alone: a `<pre>import("/x")</pre>` example is not a
|
||||
loader and is not rewritten.
|
||||
3. A small injected script rebases URLs built at **runtime**, which the first two
|
||||
cannot see: `fetch('/api/data')` and `new WebSocket('/live')`, but equally
|
||||
`card.innerHTML = '<img src="/api/hero">'`, `img.src = '/api/slide'`, and
|
||||
|
||||
+34
-12
@@ -781,6 +781,34 @@ function rebaseRelativeBase(html: string, prefix: string, documentPath: string):
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite root-absolute specifiers of dynamic `import()` calls in `<script>`
|
||||
* bodies.
|
||||
*
|
||||
* A dynamic import is the one URL no other layer can reach: it is not an
|
||||
* attribute, so `<base>` does not apply to it; it is not `fetch()`, so the runtime
|
||||
* shim cannot rewrite it; and the browser resolves it against the ORIGIN root, not
|
||||
* the document. SvelteKit's inline bootstrap is exactly this shape
|
||||
* (`import("/_app/…")`), and the failure mode is silent: the module 404s against
|
||||
* Codeman and the app's own loading spinner never resolves.
|
||||
*
|
||||
* ⚠️ Scoped to script bodies on purpose. Running the same regex over the whole
|
||||
* document also rewrote visible text — `<pre>import("/x")</pre>` became a broken
|
||||
* example — which is a rendering bug, not a loader the browser was going to run.
|
||||
*
|
||||
* @param html - document with attributes already rebased.
|
||||
* @param prefix - proxy prefix to put in front of a `/`-rooted specifier.
|
||||
*/
|
||||
function rewriteScriptImports(html: string, prefix: string): string {
|
||||
return html.replace(
|
||||
/(<script\b[^>]*>)([^]*?)(<\/script>)/gi,
|
||||
(_match: string, open: string, body: string, close: string) =>
|
||||
open +
|
||||
body.replace(/\bimport\s*\(\s*(["'])\/(?!\/)/g, (_import: string, quote: string) => `import(${quote}${prefix}`) +
|
||||
close
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject `<base href="/webview/<cap>/">` plus the runtime URL shim, and rebase
|
||||
* root-absolute `src`/`href`/`action` attributes, which `<base>` alone does not
|
||||
@@ -801,18 +829,12 @@ export function rewriteHtml(html: string, capability: string, basePath = '', doc
|
||||
|
||||
// Fresh regexes per call: module-level /g patterns carry `lastIndex` between calls.
|
||||
const rebased = rebaseRelativeBase(
|
||||
html
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*")\/(?!\/)/gi, `$1${prefix}`)
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*')\/(?!\/)/gi, `$1${prefix}`)
|
||||
// A root-absolute dynamic `import()` is the one URL no other layer can
|
||||
// reach: it is not an attribute, so `<base>` does not apply to it; it is
|
||||
// not `fetch()`, so the runtime shim cannot rewrite it; and the browser
|
||||
// resolves it against the ORIGIN root, not the document. SvelteKit's
|
||||
// inline bootstrap is exactly this shape (`import("/_app/…")`), and the
|
||||
// failure mode is silent: the module 404s against Codeman and the app's
|
||||
// own loading spinner never resolves. Rewriting the specifier here is the
|
||||
// only layer that can.
|
||||
.replace(/\bimport\s*\(\s*(["'])\/(?!\/)/g, (_match, quote: string) => `import(${quote}${prefix}`),
|
||||
rewriteScriptImports(
|
||||
html
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*")\/(?!\/)/gi, `$1${prefix}`)
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*')\/(?!\/)/gi, `$1${prefix}`),
|
||||
prefix
|
||||
),
|
||||
prefix,
|
||||
documentPath
|
||||
);
|
||||
|
||||
@@ -447,6 +447,27 @@ describe('rewriteHtml', () => {
|
||||
expect(out).toContain('import("https://cdn.example/c.js")');
|
||||
});
|
||||
|
||||
// The rewrite is for specifiers a loader will execute, not for the word
|
||||
// appearing in the page a reader sees.
|
||||
it('leaves import() in visible text alone', () => {
|
||||
const out = rewriteHtml(
|
||||
`<html><body><pre>import("/example.js")</pre><code>import('/other.js')</code></body></html>`,
|
||||
CAP
|
||||
);
|
||||
expect(out).toContain('<pre>import("/example.js")</pre>');
|
||||
expect(out).toContain("<code>import('/other.js')</code>");
|
||||
expect(out).not.toContain(`${PREFIX}example.js`);
|
||||
});
|
||||
|
||||
it('rewrites imports in every script body, including one with attributes', () => {
|
||||
const out = rewriteHtml(
|
||||
`<head><script type="module">import("/_app/a.js")</script><script defer>import('/b.js')</script></head>`,
|
||||
CAP
|
||||
);
|
||||
expect(out).toContain(`import("${PREFIX}_app/a.js")`);
|
||||
expect(out).toContain(`import('${PREFIX}b.js')`);
|
||||
});
|
||||
|
||||
it('rebases root-absolute src/href/action, which <base> cannot fix', () => {
|
||||
const out = rewriteHtml(
|
||||
`<head></head><body><script src="/static/app.js"></script><link href='/s.css'><form action="/login"></form></body>`,
|
||||
|
||||
Reference in new issue
Block a user