keryx 0.39.1 → 0.39.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/util/mcpAppBundler.ts +29 -2
package/package.json
CHANGED
package/util/mcpAppBundler.ts
CHANGED
|
@@ -35,6 +35,30 @@ const COMMENT_PLACEHOLDER = "/* MCP_APP_CLIENT */";
|
|
|
35
35
|
const THEME_PLACEHOLDER = "/* MCP_APP_THEME */";
|
|
36
36
|
/** Matches an empty `<script type="module">` tag (the preferred, declarative injection point). */
|
|
37
37
|
const EMPTY_MODULE_SCRIPT = /<script\s+type=["']module["']>\s*<\/script>/;
|
|
38
|
+
/**
|
|
39
|
+
* Matches the sequences that let bundled JS prematurely terminate (or corrupt) the inline
|
|
40
|
+
* `<script>` element it is embedded in: a closing `</script` tag (case-insensitive) and an
|
|
41
|
+
* `<!--` comment opener (which flips the HTML parser into "script data escaped" state).
|
|
42
|
+
*/
|
|
43
|
+
const SCRIPT_BREAKOUT = /<(\/script|!--)/gi;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Make bundled JS safe to inline inside an HTML `<script>` element. HTML parsers end a
|
|
47
|
+
* script at the first literal `</script>` (and `<!--` can start an escaped state that hides
|
|
48
|
+
* a later real `</script>`), so an unescaped bundle terminates the tag early and the rest
|
|
49
|
+
* renders as page text. Backslash-escaping the `<` neutralizes both without changing JS
|
|
50
|
+
* semantics: `<\/script` is identical to `</script` in every JS context, and `<\!--` is an
|
|
51
|
+
* identity escape inside the string literals where `<!--` appears in minified bundles.
|
|
52
|
+
*
|
|
53
|
+
* @param script - The bundled client JS to embed.
|
|
54
|
+
* @returns The JS with `</script`/`<!--` sequences backslash-escaped.
|
|
55
|
+
*/
|
|
56
|
+
export function escapeScriptForInlineHtml(script: string): string {
|
|
57
|
+
return script.replace(
|
|
58
|
+
SCRIPT_BREAKOUT,
|
|
59
|
+
(_match, tail: string) => `<\\${tail}`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
38
62
|
|
|
39
63
|
/** Resolved HTML per UI action, computed once at boot by {@link resolveMcpAppUiResources}. */
|
|
40
64
|
const resolvedUiHtml = new Map<Action, string>();
|
|
@@ -67,9 +91,12 @@ export async function bundleMcpAppClient(
|
|
|
67
91
|
* `<script type="module"></script>`, then a `MCP_APP_CLIENT` placeholder comment,
|
|
68
92
|
* then just before `</body>`, and finally appends as a last resort.
|
|
69
93
|
*
|
|
70
|
-
*
|
|
94
|
+
* The bundle is HTML-escaped ({@link escapeScriptForInlineHtml}) first so `</script>`
|
|
95
|
+
* sequences inside the JS cannot close the module tag early. Uses replacer functions so
|
|
96
|
+
* `$`-sequences in the bundled JS are inserted literally.
|
|
71
97
|
*/
|
|
72
|
-
function injectClientScript(shell: string,
|
|
98
|
+
function injectClientScript(shell: string, rawScript: string): string {
|
|
99
|
+
const script = escapeScriptForInlineHtml(rawScript);
|
|
73
100
|
if (EMPTY_MODULE_SCRIPT.test(shell)) {
|
|
74
101
|
return shell.replace(
|
|
75
102
|
EMPTY_MODULE_SCRIPT,
|