@krate/runtime 0.5.0-beta.1 → 0.5.0-beta.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/dist/actions.js +2 -1
- package/dist/assets-static.d.ts +5 -0
- package/dist/assets-static.js +31 -0
- package/dist/boundary.d.ts +12 -0
- package/dist/boundary.js +53 -13
- package/dist/cache-adapter.d.ts +113 -0
- package/dist/cache-adapter.js +217 -0
- package/dist/cache-adapter.test.d.ts +1 -0
- package/dist/cache-adapter.test.js +244 -0
- package/dist/cache-redis.d.ts +27 -0
- package/dist/cache-redis.js +110 -0
- package/dist/cache-scope.d.ts +25 -9
- package/dist/cache-scope.js +60 -12
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +3 -3
- package/dist/class-component.js +6 -0
- package/dist/class-component.test.js +53 -2
- package/dist/dev-overlay.js +10 -10
- package/dist/dom.d.ts +8 -0
- package/dist/dom.js +28 -0
- package/dist/effect-errors.test.d.ts +1 -0
- package/dist/effect-errors.test.js +163 -0
- package/dist/effects.d.ts +8 -0
- package/dist/effects.js +13 -0
- package/dist/error-overlay.js +4 -4
- package/dist/handler-html.d.ts +63 -0
- package/dist/handler-html.js +285 -0
- package/dist/handler-html.test.d.ts +1 -0
- package/dist/handler-html.test.js +256 -0
- package/dist/handler-routing.d.ts +83 -0
- package/dist/handler-routing.js +579 -0
- package/dist/handler-routing.test.d.ts +1 -0
- package/dist/handler-routing.test.js +99 -0
- package/dist/handler-segments.d.ts +43 -0
- package/dist/handler-segments.js +115 -0
- package/dist/handler-segments.test.d.ts +1 -0
- package/dist/handler-segments.test.js +217 -0
- package/dist/head-tags.d.ts +51 -0
- package/dist/head-tags.js +329 -0
- package/dist/head-tags.test.d.ts +1 -0
- package/dist/head-tags.test.js +131 -0
- package/dist/highlight-bind.d.ts +42 -0
- package/dist/highlight-bind.js +176 -0
- package/dist/highlight-bind.test.d.ts +1 -0
- package/dist/highlight-bind.test.js +124 -0
- package/dist/highlight.d.ts +93 -0
- package/dist/highlight.js +426 -0
- package/dist/highlight.test.d.ts +1 -0
- package/dist/highlight.test.js +56 -0
- package/dist/hints.d.ts +19 -0
- package/dist/hints.js +97 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/jsx-types.d.ts +11 -0
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +10 -10
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +5 -5
- package/dist/krate-f-highlight.js +2 -0
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +2 -0
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +254 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/list.js +4 -2
- package/dist/metadata-routes.d.ts +75 -0
- package/dist/metadata-routes.js +76 -0
- package/dist/metadata.d.ts +222 -0
- package/dist/metadata.js +452 -0
- package/dist/metadata.test.d.ts +1 -0
- package/dist/metadata.test.js +167 -0
- package/dist/navigation-server.d.ts +27 -0
- package/dist/navigation-server.js +90 -0
- package/dist/navigation.d.ts +37 -0
- package/dist/navigation.js +195 -0
- package/dist/navigation.test.d.ts +1 -0
- package/dist/navigation.test.js +130 -0
- package/dist/node-server.d.ts +22 -0
- package/dist/node-server.js +436 -0
- package/dist/node-server.test.d.ts +1 -0
- package/dist/node-server.test.js +492 -0
- package/dist/og-files.d.ts +2 -0
- package/dist/og-files.js +19 -0
- package/dist/og.d.ts +56 -0
- package/dist/og.js +156 -0
- package/dist/react.d.ts +47 -0
- package/dist/react.js +59 -1
- package/dist/react19.test.d.ts +1 -0
- package/dist/react19.test.js +176 -0
- package/dist/request-context.d.ts +23 -0
- package/dist/request-context.js +45 -0
- package/dist/resp.d.ts +55 -0
- package/dist/resp.js +239 -0
- package/dist/router-layout.test.js +70 -0
- package/dist/router-nav.test.js +27 -0
- package/dist/router.js +59 -4
- package/dist/segmented-lru.d.ts +19 -0
- package/dist/segmented-lru.js +87 -0
- package/dist/server-async.test.js +23 -1
- package/dist/server-handler.d.ts +165 -0
- package/dist/server-handler.js +1826 -0
- package/dist/server-handler.test.d.ts +1 -0
- package/dist/server-handler.test.js +522 -0
- package/dist/server-highlight.test.d.ts +1 -0
- package/dist/server-highlight.test.js +32 -0
- package/dist/server-renderer.js +146 -788
- package/dist/server-renderer.test.d.ts +1 -0
- package/dist/server-renderer.test.js +266 -0
- package/dist/server.d.ts +12 -1
- package/dist/server.js +42 -6
- package/dist/sha256.d.ts +1 -0
- package/dist/sha256.js +66 -0
- package/dist/sha256.test.d.ts +1 -0
- package/dist/sha256.test.js +13 -0
- package/dist/shared-isr.d.ts +31 -0
- package/dist/shared-isr.js +142 -0
- package/dist/signal.js +63 -8
- package/dist/ssr-core-behavior.test.d.ts +1 -0
- package/dist/ssr-core-behavior.test.js +620 -0
- package/dist/ssr-core.d.ts +175 -0
- package/dist/ssr-core.js +940 -0
- package/dist/ssr-core.test.d.ts +1 -0
- package/dist/ssr-core.test.js +324 -0
- package/dist/static-memory.d.ts +3 -0
- package/dist/static-memory.js +47 -0
- package/dist/static-memory.test.d.ts +1 -0
- package/dist/static-memory.test.js +26 -0
- package/dist/test-redis.d.ts +12 -0
- package/dist/test-redis.js +131 -0
- package/dist/use-id.d.ts +8 -0
- package/dist/use-id.js +40 -0
- package/package.json +29 -1
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// HTML-level helpers of the portable server handler: baked-shell splice markers,
|
|
2
|
+
// title swaps, out-of-order stream scripts and the no-JS flash notice. Ports of
|
|
3
|
+
// internal/build/serve.go, region_stream.go and serve_flash.go.
|
|
4
|
+
import { escapeHTML } from "./handler-routing.js";
|
|
5
|
+
/** Extracts the nested marker tree of a baked shell. */
|
|
6
|
+
export function parseRegionShell(shell) {
|
|
7
|
+
const e = { shell, roots: [], all: [], ok: false };
|
|
8
|
+
const re = /<!--(suspense|region):([^>]+?)-->/g;
|
|
9
|
+
const opens = [];
|
|
10
|
+
let m;
|
|
11
|
+
while ((m = re.exec(shell)) !== null)
|
|
12
|
+
opens.push(m);
|
|
13
|
+
if (opens.length === 0)
|
|
14
|
+
return e;
|
|
15
|
+
e.ok = true;
|
|
16
|
+
const stack = [];
|
|
17
|
+
for (const o of opens) {
|
|
18
|
+
const kind = o[1];
|
|
19
|
+
const id = o[2];
|
|
20
|
+
const openEnd = o.index + o[0].length;
|
|
21
|
+
const rel = shell.indexOf("<!--/" + kind + ":" + id + "-->", openEnd);
|
|
22
|
+
if (rel < 0) {
|
|
23
|
+
e.ok = false;
|
|
24
|
+
e.roots = [];
|
|
25
|
+
e.all = [];
|
|
26
|
+
return e;
|
|
27
|
+
}
|
|
28
|
+
const b = { openStart: o.index, openEnd, fbEnd: rel, id, kind, children: [] };
|
|
29
|
+
while (stack.length > 0 && stack[stack.length - 1].fbEnd <= b.openStart)
|
|
30
|
+
stack.pop();
|
|
31
|
+
if (stack.length > 0)
|
|
32
|
+
stack[stack.length - 1].children.push(b);
|
|
33
|
+
else
|
|
34
|
+
e.roots.push(b);
|
|
35
|
+
stack.push(b);
|
|
36
|
+
e.all.push(b);
|
|
37
|
+
}
|
|
38
|
+
return e;
|
|
39
|
+
}
|
|
40
|
+
/** Sibling regions only: a coarse page region or nested boundaries stay ordered. */
|
|
41
|
+
export function canStreamOutOfOrder(all) {
|
|
42
|
+
if (all.length === 0)
|
|
43
|
+
return false;
|
|
44
|
+
return all.every((b) => b.id !== "page" && b.children.length === 0);
|
|
45
|
+
}
|
|
46
|
+
export function allSuspense(all) {
|
|
47
|
+
return all.every((b) => b.kind === "suspense");
|
|
48
|
+
}
|
|
49
|
+
/** Keeps document markup in the streamed prefix and defers page scripts to the tail. */
|
|
50
|
+
export function splitBodyScripts(shell) {
|
|
51
|
+
const lower = shell.toLowerCase();
|
|
52
|
+
const body = lower.indexOf("<body");
|
|
53
|
+
if (body >= 0) {
|
|
54
|
+
const rel = lower.indexOf("<script", body);
|
|
55
|
+
if (rel >= 0)
|
|
56
|
+
return [shell.slice(0, rel), shell.slice(rel)];
|
|
57
|
+
}
|
|
58
|
+
const at = lower.lastIndexOf("</body>");
|
|
59
|
+
if (at >= 0)
|
|
60
|
+
return [shell.slice(0, at), shell.slice(at)];
|
|
61
|
+
return [shell, ""];
|
|
62
|
+
}
|
|
63
|
+
/** Swaps the baked <title> content of a shell. */
|
|
64
|
+
export function replaceTitle(shell, title) {
|
|
65
|
+
const m = /<title[^>]*>[\s\S]*?<\/title>/i.exec(shell);
|
|
66
|
+
if (!m)
|
|
67
|
+
return shell;
|
|
68
|
+
return shell.slice(0, m.index) + "<title>" + title + "</title>" + shell.slice(m.index + m[0].length);
|
|
69
|
+
}
|
|
70
|
+
function scriptJSON(v) {
|
|
71
|
+
return JSON.stringify(v)
|
|
72
|
+
.replace(/</g, "\\u003c")
|
|
73
|
+
.replace(/>/g, "\\u003e")
|
|
74
|
+
.replace(/&/g, "\\u0026")
|
|
75
|
+
.replace(/\u2028/g, "\\u2028")
|
|
76
|
+
.replace(/\u2029/g, "\\u2029");
|
|
77
|
+
}
|
|
78
|
+
// The client half of a streamed region's <Head>: the head is already on the
|
|
79
|
+
// wire, so the tags are applied by script. A same-key meta/canonical replaces
|
|
80
|
+
// the one in the document; scripts are recreated so they run.
|
|
81
|
+
const HEAD_APPLY = 'if(tt){var d=document.createElement("textarea");d.innerHTML=tt;document.title=d.value;}' +
|
|
82
|
+
'if(hd){var ht=document.createElement("template");ht.innerHTML=hd;[].slice.call(ht.content.children).forEach(function(e){' +
|
|
83
|
+
'var k=e.tagName,q="";' +
|
|
84
|
+
'if(k==="META"){if(e.hasAttribute("name"))q=\'meta[name="\'+e.getAttribute("name")+\'"]\';else if(e.hasAttribute("http-equiv"))q=\'meta[http-equiv="\'+e.getAttribute("http-equiv")+\'"]\';}' +
|
|
85
|
+
'else if(k==="LINK"&&(e.getAttribute("rel")||"").toLowerCase()==="canonical")q=\'link[rel="canonical"]\';' +
|
|
86
|
+
'if(q){try{var o=document.head.querySelector(q);if(o)o.remove();}catch(x){}}' +
|
|
87
|
+
'var n=e;if(k==="SCRIPT"){n=document.createElement("script");[].slice.call(e.attributes).forEach(function(a){n.setAttribute(a.name,a.value);});n.text=e.textContent;}' +
|
|
88
|
+
'document.head.appendChild(n);});}';
|
|
89
|
+
/**
|
|
90
|
+
* Inline script that inserts one region chunk at its marker pair. head and
|
|
91
|
+
* title are the region's <Head> content, applied to the document head.
|
|
92
|
+
*/
|
|
93
|
+
export function regionReplaceScript(id, html, head = "", title = "") {
|
|
94
|
+
const payload = scriptJSON(html);
|
|
95
|
+
const headPart = head || title ? "var hd=" + scriptJSON(head) + ",tt=" + scriptJSON(title) + ";" + HEAD_APPLY : "";
|
|
96
|
+
return ("<script data-krate-stream>(function(){var id=" +
|
|
97
|
+
JSON.stringify(id) +
|
|
98
|
+
";var html=" +
|
|
99
|
+
payload +
|
|
100
|
+
';var kinds=["suspense:","region:"];var open=null,kind="";var w=document.createTreeWalker(document.documentElement,NodeFilter.SHOW_COMMENT,null,false);while(w.nextNode()){var v=w.currentNode.nodeValue||"";for(var i=0;i<kinds.length;i++){if(v===kinds[i]+id){open=w.currentNode;kind=kinds[i];break;}}if(open)break;}if(!open)return;var close=null,n=open.nextSibling;while(n){if(n.nodeType===8&&n.nodeValue==="/"+kind+id){close=n;break;}n=n.nextSibling;}if(!close)return;var r=document.createRange();r.setStartAfter(open);r.setEndBefore(close);r.deleteContents();var t=document.createElement("template");t.innerHTML=html;r.insertNode(t.content);' + headPart + '})();</script>');
|
|
101
|
+
}
|
|
102
|
+
/** Writes a shell, replacing each marker block's inner content with its region HTML. */
|
|
103
|
+
export function spliceShell(shell, roots, regionHTML, emit) {
|
|
104
|
+
const writeSpan = (lo, hi, children) => {
|
|
105
|
+
let cursor = lo;
|
|
106
|
+
for (const b of children) {
|
|
107
|
+
emit(shell.slice(cursor, b.openEnd));
|
|
108
|
+
const html = regionHTML.get(b.id);
|
|
109
|
+
if (html !== undefined)
|
|
110
|
+
emit(html);
|
|
111
|
+
else if (b.kind === "suspense")
|
|
112
|
+
writeSpan(b.openEnd, b.fbEnd, b.children);
|
|
113
|
+
cursor = b.fbEnd;
|
|
114
|
+
}
|
|
115
|
+
emit(shell.slice(cursor, hi));
|
|
116
|
+
};
|
|
117
|
+
writeSpan(0, shell.length, roots);
|
|
118
|
+
}
|
|
119
|
+
// Flash state (no-JS form feedback)
|
|
120
|
+
export const FLASH_COOKIE = "krate_flash";
|
|
121
|
+
function b64urlDecode(s) {
|
|
122
|
+
try {
|
|
123
|
+
const b64 = s.replace(/=+$/, "").replace(/-/g, "+").replace(/_/g, "/");
|
|
124
|
+
const bin = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
|
|
125
|
+
const bytes = new Uint8Array(bin.length);
|
|
126
|
+
for (let i = 0; i < bin.length; i++)
|
|
127
|
+
bytes[i] = bin.charCodeAt(i);
|
|
128
|
+
return new TextDecoder().decode(bytes);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Flash cookies are signed with HMAC-SHA256 through Web Crypto, which every
|
|
135
|
+
// target has (Workers, Vercel, Netlify, Node), so instances that share
|
|
136
|
+
// KRATE_FLASH_SECRET accept each other's cookies. The format matches the Go
|
|
137
|
+
// host (serve_flash.go signFlashValue): payload + "." + base64url(HMAC(secret,
|
|
138
|
+
// "krate-flash\0" + payload)), without padding.
|
|
139
|
+
const flashKeys = new Map();
|
|
140
|
+
function flashKey(secret) {
|
|
141
|
+
let key = flashKeys.get(secret);
|
|
142
|
+
if (!key) {
|
|
143
|
+
key = crypto.subtle.importKey("raw", new TextEncoder().encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"]);
|
|
144
|
+
if (flashKeys.size > 8)
|
|
145
|
+
flashKeys.clear();
|
|
146
|
+
flashKeys.set(secret, key);
|
|
147
|
+
}
|
|
148
|
+
return key;
|
|
149
|
+
}
|
|
150
|
+
function b64urlEncodeBytes(bytes) {
|
|
151
|
+
let bin = "";
|
|
152
|
+
for (const b of bytes)
|
|
153
|
+
bin += String.fromCharCode(b);
|
|
154
|
+
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
155
|
+
}
|
|
156
|
+
function b64urlDecodeBytes(s) {
|
|
157
|
+
try {
|
|
158
|
+
const b64 = s.replace(/=+$/, "").replace(/-/g, "+").replace(/_/g, "/");
|
|
159
|
+
const bin = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
|
|
160
|
+
const bytes = new Uint8Array(bin.length);
|
|
161
|
+
for (let i = 0; i < bin.length; i++)
|
|
162
|
+
bytes[i] = bin.charCodeAt(i);
|
|
163
|
+
return bytes;
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** The cookie value for a flash payload (already base64url): payload.signature. */
|
|
170
|
+
export async function signFlash(secret, payload) {
|
|
171
|
+
const mac = await crypto.subtle.sign("HMAC", await flashKey(secret), new TextEncoder().encode("krate-flash\0" + payload));
|
|
172
|
+
return payload + "." + b64urlEncodeBytes(new Uint8Array(mac));
|
|
173
|
+
}
|
|
174
|
+
/** The payload of a signed flash cookie value, or null when the signature does not verify. */
|
|
175
|
+
export async function verifyFlash(secret, value) {
|
|
176
|
+
const dot = value.indexOf(".");
|
|
177
|
+
if (dot <= 0)
|
|
178
|
+
return null;
|
|
179
|
+
const payload = value.slice(0, dot);
|
|
180
|
+
const sig = b64urlDecodeBytes(value.slice(dot + 1));
|
|
181
|
+
if (!sig || sig.length !== 32)
|
|
182
|
+
return null;
|
|
183
|
+
const ok = await crypto.subtle.verify("HMAC", await flashKey(secret), sig, new TextEncoder().encode("krate-flash\0" + payload));
|
|
184
|
+
return ok ? payload : null;
|
|
185
|
+
}
|
|
186
|
+
/** base64url of the flash JSON, the payload that gets signed. */
|
|
187
|
+
export function flashPayloadOf(action, result) {
|
|
188
|
+
const bytes = new TextEncoder().encode(JSON.stringify({ a: action, r: result }));
|
|
189
|
+
return b64urlEncodeBytes(bytes);
|
|
190
|
+
}
|
|
191
|
+
function flashCookieValue(cookieHeader) {
|
|
192
|
+
for (const part of cookieHeader.split(";")) {
|
|
193
|
+
const i = part.indexOf("=");
|
|
194
|
+
if (i <= 0)
|
|
195
|
+
continue;
|
|
196
|
+
if (part.slice(0, i).trim() === FLASH_COOKIE) {
|
|
197
|
+
let raw = part.slice(i + 1).trim();
|
|
198
|
+
if (raw.startsWith('"') && raw.endsWith('"'))
|
|
199
|
+
raw = raw.slice(1, -1);
|
|
200
|
+
return raw;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return "";
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Whether a Cookie header carries a flash cookie, signed or not. A static layer
|
|
207
|
+
* uses it to serve the page uncompressed so the notice can be rendered into it;
|
|
208
|
+
* only requestFlash decides whether the cookie is believed.
|
|
209
|
+
*/
|
|
210
|
+
export function hasFlashCookie(cookieHeader) {
|
|
211
|
+
return !!cookieHeader && flashCookieValue(cookieHeader) !== "";
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Decodes the flash cookie of a Cookie header. The cookie must carry a valid
|
|
215
|
+
* signature for secret: an unsigned or forged value (a sibling subdomain can
|
|
216
|
+
* set cookies for this host) is never trusted. Without a secret nothing is.
|
|
217
|
+
*/
|
|
218
|
+
export async function requestFlash(cookieHeader, secret) {
|
|
219
|
+
if (!cookieHeader || !secret)
|
|
220
|
+
return null;
|
|
221
|
+
const raw = flashCookieValue(cookieHeader);
|
|
222
|
+
if (raw === "")
|
|
223
|
+
return null;
|
|
224
|
+
const payloadPart = await verifyFlash(secret, raw);
|
|
225
|
+
if (payloadPart === null)
|
|
226
|
+
return null;
|
|
227
|
+
const text = b64urlDecode(payloadPart);
|
|
228
|
+
if (text === null)
|
|
229
|
+
return null;
|
|
230
|
+
try {
|
|
231
|
+
const p = JSON.parse(text);
|
|
232
|
+
if (!p || typeof p.a !== "string" || p.a === "")
|
|
233
|
+
return null;
|
|
234
|
+
return { raw: payloadPart, payload: p };
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/** The human-readable strings of an action's returned state, in a stable order. */
|
|
241
|
+
export function flashMessages(value) {
|
|
242
|
+
const out = [];
|
|
243
|
+
const walk = (v, depth) => {
|
|
244
|
+
if (depth > 3)
|
|
245
|
+
return;
|
|
246
|
+
if (typeof v === "string") {
|
|
247
|
+
const s = v.trim();
|
|
248
|
+
if (s !== "")
|
|
249
|
+
out.push(s);
|
|
250
|
+
}
|
|
251
|
+
else if (Array.isArray(v)) {
|
|
252
|
+
for (const x of v)
|
|
253
|
+
walk(x, depth + 1);
|
|
254
|
+
}
|
|
255
|
+
else if (v && typeof v === "object") {
|
|
256
|
+
const obj = v;
|
|
257
|
+
for (const k of Object.keys(obj).sort()) {
|
|
258
|
+
const x = obj[k];
|
|
259
|
+
if (typeof x === "string" || Array.isArray(x) || (x && typeof x === "object"))
|
|
260
|
+
walk(x, depth + 1);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
walk(value, 0);
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
function regexpQuote(s) {
|
|
268
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
269
|
+
}
|
|
270
|
+
/** Inserts the notice after the opening tag of the form that produced the flash. */
|
|
271
|
+
export function injectFlashNotice(page, cookieValue, p) {
|
|
272
|
+
const msgs = flashMessages(p.r);
|
|
273
|
+
if (msgs.length === 0)
|
|
274
|
+
return null;
|
|
275
|
+
const re = new RegExp("<form\\b[^>]*?\\baction\\s*=\\s*[\"']?[^\"'\\s>]*/__krate/action/" + regexpQuote(p.a) + "[^>]*>", "i");
|
|
276
|
+
const m = re.exec(page);
|
|
277
|
+
if (!m)
|
|
278
|
+
return null;
|
|
279
|
+
const end = m.index + m[0].length;
|
|
280
|
+
let n = '<div class="krate-flash" role="alert" data-krate-action="' + escapeHTML(p.a) + '" data-krate-flash="' + escapeHTML(cookieValue) + '">';
|
|
281
|
+
for (const msg of msgs)
|
|
282
|
+
n += "<p>" + escapeHTML(msg) + "</p>";
|
|
283
|
+
n += "</div>";
|
|
284
|
+
return page.slice(0, end) + n + page.slice(end);
|
|
285
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { allSuspense, canStreamOutOfOrder, flashMessages, injectFlashNotice, parseRegionShell, regionReplaceScript, replaceTitle, requestFlash, signFlash, verifyFlash, flashPayloadOf, splitBodyScripts, spliceShell, } from "./handler-html.js";
|
|
4
|
+
const suspense = (id, inner = "") => `<!--suspense:${id}-->${inner}<!--/suspense:${id}-->`;
|
|
5
|
+
const region = (id, inner = "") => `<!--region:${id}-->${inner}<!--/region:${id}-->`;
|
|
6
|
+
function splice(shell, html) {
|
|
7
|
+
const parsed = parseRegionShell(shell);
|
|
8
|
+
expect(parsed.ok).toBe(true);
|
|
9
|
+
const out = [];
|
|
10
|
+
spliceShell(shell, parsed.roots, new Map(Object.entries(html)), (c) => out.push(c));
|
|
11
|
+
return out.join("");
|
|
12
|
+
}
|
|
13
|
+
describe("parseRegionShell", () => {
|
|
14
|
+
it("reports a shell without markers as not ok", () => {
|
|
15
|
+
const p = parseRegionShell("<html><body>static</body></html>");
|
|
16
|
+
expect(p.ok).toBe(false);
|
|
17
|
+
expect(p.roots).toEqual([]);
|
|
18
|
+
});
|
|
19
|
+
it("finds sibling and nested markers in document order", () => {
|
|
20
|
+
const shell = `<main>${suspense("a", "fb-a")}${region("b", suspense("c", "fb-c"))}</main>`;
|
|
21
|
+
const p = parseRegionShell(shell);
|
|
22
|
+
expect(p.ok).toBe(true);
|
|
23
|
+
expect(p.all.map((b) => b.id)).toEqual(["a", "b", "c"]);
|
|
24
|
+
expect(p.roots.map((b) => b.id)).toEqual(["a", "b"]);
|
|
25
|
+
expect(p.roots[1].children.map((b) => b.id)).toEqual(["c"]);
|
|
26
|
+
const a = p.all[0];
|
|
27
|
+
expect(shell.slice(a.openEnd, a.fbEnd)).toBe("fb-a");
|
|
28
|
+
});
|
|
29
|
+
it("rejects a marker whose closing comment is missing, dropping partial results", () => {
|
|
30
|
+
const p = parseRegionShell(`${suspense("a", "x")}<!--suspense:b-->never closed`);
|
|
31
|
+
expect(p.ok).toBe(false);
|
|
32
|
+
expect(p.roots).toEqual([]);
|
|
33
|
+
expect(p.all).toEqual([]);
|
|
34
|
+
});
|
|
35
|
+
it("does not confuse a region and a suspense marker that share an id", () => {
|
|
36
|
+
const p = parseRegionShell(`<!--suspense:x-->s<!--/region:x-->`);
|
|
37
|
+
expect(p.ok).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe("streaming mode", () => {
|
|
41
|
+
it("streams siblings out of order, but not the coarse page region or nested markers", () => {
|
|
42
|
+
expect(canStreamOutOfOrder(parseRegionShell(suspense("a") + suspense("b")).all)).toBe(true);
|
|
43
|
+
expect(canStreamOutOfOrder(parseRegionShell(suspense("page")).all)).toBe(false);
|
|
44
|
+
expect(canStreamOutOfOrder(parseRegionShell(suspense("a", suspense("b"))).all)).toBe(false);
|
|
45
|
+
expect(canStreamOutOfOrder([])).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
it("allSuspense is false as soon as one region marker is present", () => {
|
|
48
|
+
expect(allSuspense(parseRegionShell(suspense("a") + suspense("b")).all)).toBe(true);
|
|
49
|
+
expect(allSuspense(parseRegionShell(suspense("a") + region("b")).all)).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe("splitBodyScripts", () => {
|
|
53
|
+
it("defers the first body script and everything after it", () => {
|
|
54
|
+
const [head, tail] = splitBodyScripts("<html><body><p>x</p><SCRIPT src=/a.js></SCRIPT></body></html>");
|
|
55
|
+
expect(head).toBe("<html><body><p>x</p>");
|
|
56
|
+
expect(tail).toBe("<SCRIPT src=/a.js></SCRIPT></body></html>");
|
|
57
|
+
});
|
|
58
|
+
it("ignores a script in <head>", () => {
|
|
59
|
+
const shell = "<html><head><script>1</script></head><body><p>x</p></body></html>";
|
|
60
|
+
const [head, tail] = splitBodyScripts(shell);
|
|
61
|
+
expect(head).toBe("<html><head><script>1</script></head><body><p>x</p>");
|
|
62
|
+
expect(tail).toBe("</body></html>");
|
|
63
|
+
});
|
|
64
|
+
it("returns the whole shell when there is no body", () => {
|
|
65
|
+
expect(splitBodyScripts("<p>fragment</p>")).toEqual(["<p>fragment</p>", ""]);
|
|
66
|
+
});
|
|
67
|
+
it("round-trips: head + tail is the original", () => {
|
|
68
|
+
const shell = "<body>a<script>b</script>c</body>";
|
|
69
|
+
const [h, t] = splitBodyScripts(shell);
|
|
70
|
+
expect(h + t).toBe(shell);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe("replaceTitle", () => {
|
|
74
|
+
it("swaps the content and drops title attributes", () => {
|
|
75
|
+
expect(replaceTitle('<head><title data-x="1">Old</title></head>', "New")).toBe("<head><title>New</title></head>");
|
|
76
|
+
});
|
|
77
|
+
it("leaves a shell without a title alone", () => {
|
|
78
|
+
expect(replaceTitle("<head></head>", "New")).toBe("<head></head>");
|
|
79
|
+
});
|
|
80
|
+
it("does not interpret replacement patterns in the new title", () => {
|
|
81
|
+
expect(replaceTitle("<title>Old</title>", "Cost $& $1 $$")).toBe("<title>Cost $& $1 $$</title>");
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe("regionReplaceScript", () => {
|
|
85
|
+
const payloadOf = (script) => {
|
|
86
|
+
const m = /var html=(".*?");var kinds=/s.exec(script);
|
|
87
|
+
expect(m).not.toBeNull();
|
|
88
|
+
return JSON.parse(m[1]);
|
|
89
|
+
};
|
|
90
|
+
it("embeds the HTML so it cannot close the script element or break the literal", () => {
|
|
91
|
+
const html = `</script><script>alert(1)</script> & "q" \u2028 \u2029 <!-- x -->`;
|
|
92
|
+
const script = regionReplaceScript("r1", html);
|
|
93
|
+
// Only the wrapper's own tags may appear: the payload has no raw < or >.
|
|
94
|
+
expect(script.match(/<\/script>/gi)).toHaveLength(1);
|
|
95
|
+
expect(script.match(/<script/gi)).toHaveLength(1);
|
|
96
|
+
expect(script).not.toContain("\u2028");
|
|
97
|
+
expect(script).not.toContain("\u2029");
|
|
98
|
+
expect(payloadOf(script)).toBe(html);
|
|
99
|
+
});
|
|
100
|
+
it("quotes the id as a JSON string", () => {
|
|
101
|
+
const script = regionReplaceScript("a\"b'c\\d", "x");
|
|
102
|
+
expect(script).toContain('var id="a\\"b\'c\\\\d";');
|
|
103
|
+
});
|
|
104
|
+
it("executes against a minimal DOM and replaces only the marked block", () => {
|
|
105
|
+
// Run the generated script with stub DOM objects to check the splice logic.
|
|
106
|
+
const comments = [
|
|
107
|
+
{ nodeType: 8, nodeValue: "suspense:r1", nextSibling: null },
|
|
108
|
+
{ nodeType: 8, nodeValue: "/suspense:r1", nextSibling: null },
|
|
109
|
+
];
|
|
110
|
+
comments[0].nextSibling = { nodeType: 3, nodeValue: "fallback", nextSibling: comments[1] };
|
|
111
|
+
const calls = [];
|
|
112
|
+
let idx = -1;
|
|
113
|
+
const doc = {
|
|
114
|
+
documentElement: {},
|
|
115
|
+
createTreeWalker: () => ({
|
|
116
|
+
get currentNode() {
|
|
117
|
+
return comments[idx];
|
|
118
|
+
},
|
|
119
|
+
nextNode: () => ++idx < comments.length,
|
|
120
|
+
}),
|
|
121
|
+
createRange: () => ({
|
|
122
|
+
setStartAfter: (n) => calls.push("start:" + n.nodeValue),
|
|
123
|
+
setEndBefore: (n) => calls.push("end:" + n.nodeValue),
|
|
124
|
+
deleteContents: () => calls.push("delete"),
|
|
125
|
+
insertNode: (n) => calls.push("insert:" + n),
|
|
126
|
+
}),
|
|
127
|
+
createElement: () => {
|
|
128
|
+
const t = { content: "<c>", set innerHTML(v) { t.content = v; } };
|
|
129
|
+
return t;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
const code = regionReplaceScript("r1", "<b>done</b>").replace(/^<script[^>]*>/, "").replace(/<\/script>$/, "");
|
|
133
|
+
new Function("document", "NodeFilter", code)(doc, { SHOW_COMMENT: 128 });
|
|
134
|
+
expect(calls).toEqual(["start:suspense:r1", "end:/suspense:r1", "delete", "insert:<b>done</b>"]);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe("spliceShell", () => {
|
|
138
|
+
it("replaces the baked content of a marker and keeps the markers and the rest of the shell", () => {
|
|
139
|
+
const shell = `<main>${suspense("a", "loading")}<footer>f</footer></main>`;
|
|
140
|
+
expect(splice(shell, { a: "<p>done</p>" })).toBe(`<main>${suspense("a", "<p>done</p>")}<footer>f</footer></main>`);
|
|
141
|
+
});
|
|
142
|
+
it("keeps the baked fallback of a region that produced no HTML", () => {
|
|
143
|
+
const shell = `${suspense("a", "fb-a")}|${suspense("b", "fb-b")}`;
|
|
144
|
+
expect(splice(shell, { b: "B" })).toBe(`${suspense("a", "fb-a")}|${suspense("b", "B")}`);
|
|
145
|
+
});
|
|
146
|
+
it("descends into a suspense fallback to fill nested regions", () => {
|
|
147
|
+
const shell = suspense("outer", `shell ${region("inner", "baked-inner")} end`);
|
|
148
|
+
expect(splice(shell, { inner: "NEW" })).toBe(suspense("outer", `shell ${region("inner", "NEW")} end`));
|
|
149
|
+
});
|
|
150
|
+
it("lets the outer region win over nested ones when both have HTML", () => {
|
|
151
|
+
const shell = suspense("outer", region("inner", "baked"));
|
|
152
|
+
expect(splice(shell, { outer: "OUT", inner: "IN" })).toBe(suspense("outer", "OUT"));
|
|
153
|
+
});
|
|
154
|
+
it("leaves a standalone runtime region empty when it produced no HTML (same as the Go host)", () => {
|
|
155
|
+
const shell = `a${region("r", suspense("s", "baked"))}b`;
|
|
156
|
+
expect(splice(shell, { s: "IGNORED" })).toBe(`a${region("r")}b`);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
const SECRET = "krate-test-flash-secret-0123456789";
|
|
160
|
+
// The same vector is pinned in internal/build/serve_flash_test.go: what the Go
|
|
161
|
+
// host signs verifies here, and what is signed here verifies there.
|
|
162
|
+
const VECTOR_PAYLOAD = "eyJhIjoiYWN0MSIsInIiOnsiZXJyb3IiOiJOYW1lIGlzIHJlcXVpcmVkIn19";
|
|
163
|
+
const VECTOR_COOKIE = VECTOR_PAYLOAD + ".zLQd73rhp1HN8J0vNvSeLqvlCqquygUX-mXuuN24dlY";
|
|
164
|
+
async function flashCookie(payload, secret = SECRET) {
|
|
165
|
+
const b64 = Buffer.from(JSON.stringify(payload), "utf-8").toString("base64url");
|
|
166
|
+
return secret === null ? b64 : signFlash(secret, b64);
|
|
167
|
+
}
|
|
168
|
+
describe("requestFlash", () => {
|
|
169
|
+
const payload = { a: "act1", r: { error: "Name is required" } };
|
|
170
|
+
it("reads the signed flash cookie among other cookies", async () => {
|
|
171
|
+
const got = await requestFlash(`theme=dark; krate_flash=${await flashCookie(payload)}; x=1`, SECRET);
|
|
172
|
+
expect(got?.payload).toEqual(payload);
|
|
173
|
+
});
|
|
174
|
+
it("accepts a quoted value and returns the payload part as raw", async () => {
|
|
175
|
+
const got = await requestFlash(`krate_flash="${await flashCookie(payload)}"`, SECRET);
|
|
176
|
+
expect(got?.payload).toEqual(payload);
|
|
177
|
+
expect(got?.raw).toBe(VECTOR_PAYLOAD);
|
|
178
|
+
});
|
|
179
|
+
it("signs and verifies the same bytes as the Go host", async () => {
|
|
180
|
+
expect(await signFlash(SECRET, VECTOR_PAYLOAD)).toBe(VECTOR_COOKIE);
|
|
181
|
+
expect(await verifyFlash(SECRET, VECTOR_COOKIE)).toBe(VECTOR_PAYLOAD);
|
|
182
|
+
expect(flashPayloadOf("act1", { error: "Name is required" })).toBe(VECTOR_PAYLOAD);
|
|
183
|
+
});
|
|
184
|
+
it("never trusts an unsigned, forged or foreign-signed cookie", async () => {
|
|
185
|
+
const unsigned = await flashCookie(payload, null);
|
|
186
|
+
expect(await requestFlash(`krate_flash=${unsigned}`, SECRET)).toBeNull();
|
|
187
|
+
expect(await requestFlash(`krate_flash=${unsigned}.`, SECRET)).toBeNull();
|
|
188
|
+
expect(await requestFlash(`krate_flash=${unsigned}.deadbeef`, SECRET)).toBeNull();
|
|
189
|
+
expect(await requestFlash(`krate_flash=${await flashCookie(payload, "another-secret-another-secret")}`, SECRET)).toBeNull();
|
|
190
|
+
// A valid signature does not carry over to a different payload.
|
|
191
|
+
const sig = VECTOR_COOKIE.slice(VECTOR_COOKIE.indexOf(".") + 1);
|
|
192
|
+
const other = Buffer.from(JSON.stringify({ a: "act1", r: { error: "Injected notice" } })).toString("base64url");
|
|
193
|
+
expect(await requestFlash(`krate_flash=${other}.${sig}`, SECRET)).toBeNull();
|
|
194
|
+
// Without a secret nothing is trusted, signed or not.
|
|
195
|
+
expect(await requestFlash(`krate_flash=${VECTOR_COOKIE}`, null)).toBeNull();
|
|
196
|
+
expect(await requestFlash(`krate_flash=${VECTOR_COOKIE}`, "")).toBeNull();
|
|
197
|
+
});
|
|
198
|
+
it("does not match a cookie that merely ends with the name", async () => {
|
|
199
|
+
expect(await requestFlash(`other_krate_flash=${await flashCookie(payload)}`, SECRET)).toBeNull();
|
|
200
|
+
});
|
|
201
|
+
it("returns null for no header, an empty value, bad base64, bad JSON or no action id", async () => {
|
|
202
|
+
expect(await requestFlash(null, SECRET)).toBeNull();
|
|
203
|
+
expect(await requestFlash("krate_flash=", SECRET)).toBeNull();
|
|
204
|
+
expect(await requestFlash("krate_flash=%%%", SECRET)).toBeNull();
|
|
205
|
+
expect(await requestFlash(`krate_flash=${await signFlash(SECRET, Buffer.from("not json").toString("base64url"))}`, SECRET)).toBeNull();
|
|
206
|
+
expect(await requestFlash(`krate_flash=${await flashCookie({ a: "", r: "x" })}`, SECRET)).toBeNull();
|
|
207
|
+
expect(await requestFlash(`krate_flash=${await flashCookie({ r: "x" })}`, SECRET)).toBeNull();
|
|
208
|
+
});
|
|
209
|
+
it("decodes multi-byte text", async () => {
|
|
210
|
+
const got = await requestFlash(`krate_flash=${await flashCookie({ a: "a", r: "café €" })}`, SECRET);
|
|
211
|
+
expect(got?.payload.r).toBe("café €");
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
describe("flashMessages", () => {
|
|
215
|
+
it("collects strings in a stable order (sorted keys, array order), trimmed", () => {
|
|
216
|
+
expect(flashMessages({ b: " second ", a: "first", c: ["x", "", " ", "y"], n: 5, ok: true })).toEqual(["first", "second", "x", "y"]);
|
|
217
|
+
});
|
|
218
|
+
it("stops descending after a few levels", () => {
|
|
219
|
+
expect(flashMessages({ a: { b: { c: { d: "too deep" } } } })).toEqual([]);
|
|
220
|
+
expect(flashMessages({ a: { b: { c: "kept" } } })).toEqual(["kept"]);
|
|
221
|
+
});
|
|
222
|
+
it("handles a bare string and empty values", () => {
|
|
223
|
+
expect(flashMessages("hi")).toEqual(["hi"]);
|
|
224
|
+
expect(flashMessages(null)).toEqual([]);
|
|
225
|
+
expect(flashMessages(undefined)).toEqual([]);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
describe("injectFlashNotice", () => {
|
|
229
|
+
const page = `<body><form method="post" action="/__krate/action/save"><input name="n"></form><form action="/__krate/action/other"></form></body>`;
|
|
230
|
+
it("inserts the notice right after the opening tag of the form that produced it", () => {
|
|
231
|
+
const out = injectFlashNotice(page, "RAW", { a: "save", r: { error: "Bad <name>" } });
|
|
232
|
+
expect(out).not.toBeNull();
|
|
233
|
+
const first = out.indexOf("krate-flash");
|
|
234
|
+
expect(first).toBeGreaterThan(out.indexOf('action="/__krate/action/save"'));
|
|
235
|
+
expect(first).toBeLessThan(out.indexOf('<input name="n">'));
|
|
236
|
+
// the other form is untouched
|
|
237
|
+
expect(out.match(/krate-flash"/g)).toHaveLength(1);
|
|
238
|
+
});
|
|
239
|
+
it("escapes the message, the action id and the cookie value", () => {
|
|
240
|
+
const out = injectFlashNotice(`<form action="/__krate/action/a"b"></form>`, 'v"><img src=x onerror=alert(1)>', { a: 'a"b', r: "<script>alert(1)</script>" });
|
|
241
|
+
expect(out).not.toContain("<script>");
|
|
242
|
+
expect(out).not.toContain("<img src=x");
|
|
243
|
+
expect(out).toContain("<script>");
|
|
244
|
+
expect(out).toContain('role="alert"');
|
|
245
|
+
});
|
|
246
|
+
it("matches an action id that contains regex characters literally", () => {
|
|
247
|
+
const html = `<form action="/__krate/action/a.b+c"></form><form action="/__krate/action/aXb+c"></form>`;
|
|
248
|
+
const out = injectFlashNotice(html, "v", { a: "a.b+c", r: "msg" });
|
|
249
|
+
expect(out.indexOf("krate-flash")).toBeLessThan(out.indexOf("aXb+c"));
|
|
250
|
+
expect(out.match(/krate-flash"/g)).toHaveLength(1);
|
|
251
|
+
});
|
|
252
|
+
it("returns null when there is nothing to show or no matching form", () => {
|
|
253
|
+
expect(injectFlashNotice(page, "v", { a: "save", r: 7 })).toBeNull();
|
|
254
|
+
expect(injectFlashNotice(page, "v", { a: "missing", r: "msg" })).toBeNull();
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export type RouteSegKind = 0 | 1 | 2 | 3;
|
|
2
|
+
export interface RouteSeg {
|
|
3
|
+
kind: RouteSegKind;
|
|
4
|
+
text: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function parseRoutePattern(pattern: string): RouteSeg[];
|
|
7
|
+
export declare function trimSlashes(s: string): string;
|
|
8
|
+
export declare function isDynamicPattern(pattern: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Matches a URL path against a route pattern. A param matches one non-empty
|
|
11
|
+
* segment; a catch-all one or more remaining segments, an optional catch-all
|
|
12
|
+
* zero or more. Catch-all values are the matched segments joined with "/".
|
|
13
|
+
*/
|
|
14
|
+
export declare function matchRoutePattern(urlPath: string, pattern: string): Record<string, string> | null;
|
|
15
|
+
/** Negative when a is more specific than b: static > param > catch-all > optional catch-all. */
|
|
16
|
+
export declare function compareRouteSpecificity(a: string, b: string): number;
|
|
17
|
+
export declare function normalizeRoutePattern(p: string): string;
|
|
18
|
+
/** The route form used by the manifest: no trailing slash, "/" for the root. */
|
|
19
|
+
export declare function requestRoute(urlPath: string): string;
|
|
20
|
+
export declare function isFileLikePath(p: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* The internal path a rewrite serves. Page URLs are canonical with a trailing
|
|
23
|
+
* slash and the slashless spelling answers 301, so a rewrite to "/feedback" would
|
|
24
|
+
* change the visible URL; the canonical form keeps it invisible. API routes,
|
|
25
|
+
* internal paths and file-like paths are left alone.
|
|
26
|
+
*/
|
|
27
|
+
export declare function rewriteTargetPath(p: string): string;
|
|
28
|
+
/** path.Base for slash paths. */
|
|
29
|
+
export declare function baseName(p: string): string;
|
|
30
|
+
export declare function isHTMLDocRequest(urlPath: string): boolean;
|
|
31
|
+
/** path.Ext for slash paths. */
|
|
32
|
+
export declare function extName(p: string): string;
|
|
33
|
+
/** path.Clean for slash paths. */
|
|
34
|
+
export declare function cleanPath(p: string): string;
|
|
35
|
+
/** True for requests aimed at Krate's server-only output directory. */
|
|
36
|
+
export declare function isInternalOutputPath(urlPath: string): boolean;
|
|
37
|
+
export declare function isHiddenName(name: string): boolean;
|
|
38
|
+
/** True when any segment of a (decoded) URL path is a dotfile name. */
|
|
39
|
+
export declare function isHiddenPath(urlPath: string): boolean;
|
|
40
|
+
export interface RuleMatch {
|
|
41
|
+
params: Record<string, string>;
|
|
42
|
+
mods: Record<string, string>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Matches a request path against a redirect/rewrite source: exact, a trailing
|
|
46
|
+
* "/*" wildcard (param "splat"), or named segments with the "*", "+" and "?"
|
|
47
|
+
* modifiers.
|
|
48
|
+
*/
|
|
49
|
+
export declare function matchRedirectParamsMods(path: string, source: string): RuleMatch | null;
|
|
50
|
+
export declare function matchRedirect(path: string, source: string): boolean;
|
|
51
|
+
/** url.PathEscape of Go: segment-escapes everything but unreserved and $&+=:@. */
|
|
52
|
+
export declare function pathEscape(s: string): string;
|
|
53
|
+
/** Percent-encodes a matched (decoded) value segment by segment, keeping "/" between segments. */
|
|
54
|
+
export declare function escapeRedirectValue(v: string): string;
|
|
55
|
+
/** Keeps a substituted path destination on-site (no "//host" or "/\\host" results). */
|
|
56
|
+
export declare function sanitizeRedirectTarget(configured: string, out: string): string;
|
|
57
|
+
/** Fills ":splat" and ":name" placeholders of a destination from the matched path. */
|
|
58
|
+
export declare function rewriteDestination(path: string, source: string, destination: string): string;
|
|
59
|
+
/** path.Match for slash paths: "*" and "?" do not cross "/", "[...]" classes. */
|
|
60
|
+
export declare function pathMatch(pattern: string, name: string): boolean;
|
|
61
|
+
/** Exact path, trailing "*" prefix, or path.Match glob (config `headers[].source`). */
|
|
62
|
+
export declare function headerSourceMatches(pattern: string, p: string): boolean;
|
|
63
|
+
export declare const DYNAMIC_PARAM_PREFIX = "__KRATE_PARAM_";
|
|
64
|
+
export declare function dynamicParamSentinel(name: string): string;
|
|
65
|
+
/** Go's html.EscapeString (used for dynamic template params). */
|
|
66
|
+
export declare function goHTMLEscape(s: string): string;
|
|
67
|
+
/** escape.HTML of the compiler: & < > " ' */
|
|
68
|
+
export declare function escapeHTML(s: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* Renders a dynamic-route template for one request: injects the matched params
|
|
71
|
+
* for client-side access and replaces every build-time sentinel.
|
|
72
|
+
*/
|
|
73
|
+
export declare function applyDynamicRouteParams(templateHTML: string, params: Record<string, string>): string;
|
|
74
|
+
/** json.Marshal of a string map: sorted keys, HTML-significant characters escaped. */
|
|
75
|
+
export declare function goJSON(obj: Record<string, string>): string;
|
|
76
|
+
export declare function staticCacheControl(urlPath: string, hashed: ReadonlySet<string>): string;
|
|
77
|
+
export declare function compileMatcher(src: unknown): RegExp;
|
|
78
|
+
/**
|
|
79
|
+
* Builds the response http.Redirect writes: a relative target is resolved
|
|
80
|
+
* against the request path and cleaned, Location is escaped, and a GET gets a
|
|
81
|
+
* small HTML body.
|
|
82
|
+
*/
|
|
83
|
+
export declare function goRedirect(target: string, requestPath: string, method: string, status: number, extra?: Headers): Response;
|