@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10

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.
Files changed (219) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +496 -91
  22. package/dist/dom.d.ts +4 -0
  23. package/dist/dom.js +62 -16
  24. package/dist/effects.js +43 -10
  25. package/dist/events.d.ts +2 -0
  26. package/dist/events.js +18 -0
  27. package/dist/flight-props.d.ts +7 -0
  28. package/dist/flight-props.js +105 -0
  29. package/dist/handler-html.js +4 -4
  30. package/dist/handler-routing.d.ts +2 -23
  31. package/dist/handler-routing.js +9 -190
  32. package/dist/handler-segments.d.ts +6 -0
  33. package/dist/handler-segments.js +18 -4
  34. package/dist/head-tags.d.ts +9 -0
  35. package/dist/head-tags.js +93 -6
  36. package/dist/highlight-bind.js +1 -1
  37. package/dist/highlight.d.ts +3 -2
  38. package/dist/highlight.js +17 -12
  39. package/dist/hints.d.ts +25 -2
  40. package/dist/hints.js +147 -74
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +3 -0
  43. package/dist/island-swap.d.ts +47 -0
  44. package/dist/island-swap.js +635 -0
  45. package/dist/jsx-dev-runtime.d.ts +1 -1
  46. package/dist/jsx-types.d.ts +99 -3
  47. package/dist/krate-core.js +1 -1
  48. package/dist/krate-dev.js +407 -80
  49. package/dist/krate-f-actions.js +1 -1
  50. package/dist/krate-f-classes.js +1 -1
  51. package/dist/krate-f-highlight.js +1 -1
  52. package/dist/krate-f-list.js +1 -1
  53. package/dist/krate-f-navigation.js +1 -1
  54. package/dist/krate-f-router-lite.js +1 -1
  55. package/dist/krate-f-router.js +1 -1
  56. package/dist/krate-features.json +455 -0
  57. package/dist/krate-hydrate-lite.js +5 -5
  58. package/dist/krate-hydrate.js +5 -5
  59. package/dist/krate-island-swap.js +1 -0
  60. package/dist/krate-runtime.js +5 -5
  61. package/dist/krate-suspense.js +1 -1
  62. package/dist/list.d.ts +14 -4
  63. package/dist/list.js +30 -23
  64. package/dist/metadata.js +5 -2
  65. package/dist/navigation-react.d.ts +13 -0
  66. package/dist/navigation-react.js +86 -0
  67. package/dist/navigation.d.ts +19 -0
  68. package/dist/navigation.js +35 -0
  69. package/dist/node-server.js +3 -3
  70. package/dist/og.js +1 -1
  71. package/dist/platform.d.ts +52 -0
  72. package/dist/platform.js +57 -0
  73. package/dist/react-compat.d.ts +39 -0
  74. package/dist/react-compat.js +359 -0
  75. package/dist/react-server.d.ts +63 -0
  76. package/dist/react-server.js +125 -0
  77. package/dist/region-wire.d.ts +10 -0
  78. package/dist/region-wire.js +154 -0
  79. package/dist/request-context.js +1 -1
  80. package/dist/request-extras.d.ts +4 -0
  81. package/dist/request-extras.js +51 -0
  82. package/dist/resp.js +1 -1
  83. package/dist/route-source.d.ts +39 -0
  84. package/dist/route-source.js +532 -0
  85. package/dist/router-lite.js +3 -9
  86. package/dist/router.d.ts +1 -1
  87. package/dist/router.js +675 -100
  88. package/dist/server-handler.d.ts +69 -1
  89. package/dist/server-handler.js +515 -96
  90. package/dist/server-renderer.js +16 -7
  91. package/dist/server.d.ts +73 -16
  92. package/dist/server.js +619 -102
  93. package/dist/sha256.js +9 -9
  94. package/dist/signal.js +47 -20
  95. package/dist/site-config.d.ts +15 -0
  96. package/dist/site-config.js +28 -0
  97. package/dist/slot.d.ts +12 -0
  98. package/dist/slot.js +23 -0
  99. package/dist/ssr-core.d.ts +2 -0
  100. package/dist/ssr-core.js +12 -4
  101. package/dist/suspense.js +34 -2
  102. package/dist/testing.d.ts +62 -0
  103. package/dist/testing.js +90 -0
  104. package/dist/use-id.js +1 -1
  105. package/package.json +28 -3
  106. package/dist/action-context.test.d.ts +0 -1
  107. package/dist/action-context.test.js +0 -72
  108. package/dist/actions.test.d.ts +0 -1
  109. package/dist/actions.test.js +0 -477
  110. package/dist/cache-adapter.test.d.ts +0 -1
  111. package/dist/cache-adapter.test.js +0 -244
  112. package/dist/cache.test.d.ts +0 -1
  113. package/dist/cache.test.js +0 -233
  114. package/dist/class-component.test.d.ts +0 -1
  115. package/dist/class-component.test.js +0 -462
  116. package/dist/class-helpers.test.d.ts +0 -1
  117. package/dist/class-helpers.test.js +0 -169
  118. package/dist/context.test.d.ts +0 -1
  119. package/dist/context.test.js +0 -57
  120. package/dist/derived.test.d.ts +0 -1
  121. package/dist/derived.test.js +0 -140
  122. package/dist/dev-overlay.test.d.ts +0 -1
  123. package/dist/dev-overlay.test.js +0 -152
  124. package/dist/discrete.test.d.ts +0 -1
  125. package/dist/discrete.test.js +0 -100
  126. package/dist/dom.test.d.ts +0 -1
  127. package/dist/dom.test.js +0 -31
  128. package/dist/effect-errors.test.d.ts +0 -1
  129. package/dist/effect-errors.test.js +0 -163
  130. package/dist/effects.test.d.ts +0 -1
  131. package/dist/effects.test.js +0 -485
  132. package/dist/events.test.d.ts +0 -1
  133. package/dist/events.test.js +0 -447
  134. package/dist/handler-html.test.d.ts +0 -1
  135. package/dist/handler-html.test.js +0 -256
  136. package/dist/handler-routing.test.d.ts +0 -1
  137. package/dist/handler-routing.test.js +0 -99
  138. package/dist/handler-segments.test.d.ts +0 -1
  139. package/dist/handler-segments.test.js +0 -217
  140. package/dist/head-tags.test.d.ts +0 -1
  141. package/dist/head-tags.test.js +0 -131
  142. package/dist/highlight-bind.test.d.ts +0 -1
  143. package/dist/highlight-bind.test.js +0 -124
  144. package/dist/highlight.test.d.ts +0 -1
  145. package/dist/highlight.test.js +0 -56
  146. package/dist/lanes.test.d.ts +0 -1
  147. package/dist/lanes.test.js +0 -59
  148. package/dist/list.test.d.ts +0 -1
  149. package/dist/list.test.js +0 -344
  150. package/dist/metadata.test.d.ts +0 -1
  151. package/dist/metadata.test.js +0 -167
  152. package/dist/navigation.test.d.ts +0 -1
  153. package/dist/navigation.test.js +0 -130
  154. package/dist/node-server.test.d.ts +0 -1
  155. package/dist/node-server.test.js +0 -492
  156. package/dist/optimistic-lanes.test.d.ts +0 -1
  157. package/dist/optimistic-lanes.test.js +0 -45
  158. package/dist/patterns.test.d.ts +0 -1
  159. package/dist/patterns.test.js +0 -98
  160. package/dist/portal.test.d.ts +0 -1
  161. package/dist/portal.test.js +0 -99
  162. package/dist/react-effects.test.d.ts +0 -1
  163. package/dist/react-effects.test.js +0 -96
  164. package/dist/react-suspense.test.d.ts +0 -1
  165. package/dist/react-suspense.test.js +0 -194
  166. package/dist/react.test.d.ts +0 -1
  167. package/dist/react.test.js +0 -60
  168. package/dist/react19.test.d.ts +0 -1
  169. package/dist/react19.test.js +0 -176
  170. package/dist/reactivity-core.test.d.ts +0 -1
  171. package/dist/reactivity-core.test.js +0 -152
  172. package/dist/reconcile.test.d.ts +0 -1
  173. package/dist/reconcile.test.js +0 -110
  174. package/dist/refs.test.d.ts +0 -1
  175. package/dist/refs.test.js +0 -257
  176. package/dist/router-layout.test.d.ts +0 -1
  177. package/dist/router-layout.test.js +0 -249
  178. package/dist/router-nav.test.d.ts +0 -1
  179. package/dist/router-nav.test.js +0 -464
  180. package/dist/router-slots.test.d.ts +0 -1
  181. package/dist/router-slots.test.js +0 -144
  182. package/dist/router-speculation.test.d.ts +0 -1
  183. package/dist/router-speculation.test.js +0 -25
  184. package/dist/router-styles.test.d.ts +0 -1
  185. package/dist/router-styles.test.js +0 -98
  186. package/dist/router.test.d.ts +0 -1
  187. package/dist/router.test.js +0 -36
  188. package/dist/server-async.test.d.ts +0 -1
  189. package/dist/server-async.test.js +0 -216
  190. package/dist/server-handler.test.d.ts +0 -1
  191. package/dist/server-handler.test.js +0 -535
  192. package/dist/server-highlight.test.d.ts +0 -1
  193. package/dist/server-highlight.test.js +0 -32
  194. package/dist/server-renderer.test.d.ts +0 -1
  195. package/dist/server-renderer.test.js +0 -266
  196. package/dist/server.test.d.ts +0 -1
  197. package/dist/server.test.js +0 -88
  198. package/dist/sha256.test.d.ts +0 -1
  199. package/dist/sha256.test.js +0 -13
  200. package/dist/signal-lanes.test.d.ts +0 -1
  201. package/dist/signal-lanes.test.js +0 -32
  202. package/dist/signal.test.d.ts +0 -1
  203. package/dist/signal.test.js +0 -68
  204. package/dist/slots.test.d.ts +0 -1
  205. package/dist/slots.test.js +0 -169
  206. package/dist/snapshots.test.d.ts +0 -1
  207. package/dist/snapshots.test.js +0 -208
  208. package/dist/ssr-core-behavior.test.d.ts +0 -1
  209. package/dist/ssr-core-behavior.test.js +0 -620
  210. package/dist/ssr-core.test.d.ts +0 -1
  211. package/dist/ssr-core.test.js +0 -324
  212. package/dist/static-memory.test.d.ts +0 -1
  213. package/dist/static-memory.test.js +0 -26
  214. package/dist/storage.test.d.ts +0 -1
  215. package/dist/storage.test.js +0 -46
  216. package/dist/suspense-actions.test.d.ts +0 -1
  217. package/dist/suspense-actions.test.js +0 -72
  218. package/dist/suspense.test.d.ts +0 -1
  219. package/dist/suspense.test.js +0 -518
@@ -127,7 +127,7 @@ export function baseName(p) {
127
127
  return "/";
128
128
  const s = p.slice(0, end);
129
129
  const i = s.lastIndexOf("/");
130
- return i >= 0 ? s.slice(i + 1) : s;
130
+ return i !== -1 ? s.slice(i + 1) : s;
131
131
  }
132
132
  export function isHTMLDocRequest(urlPath) {
133
133
  return urlPath === "" || urlPath.endsWith("/") || urlPath.endsWith(".html");
@@ -178,6 +178,9 @@ export function isInternalOutputPath(urlPath) {
178
178
  const p = cleanPath("/" + decodePath(urlPath).replace(/\\/g, "/"));
179
179
  if (p === "/.krate" || p.startsWith("/.krate/"))
180
180
  return true;
181
+ // The shell of a segment page that renders per request is not a page of the site.
182
+ if (/(^|\/)__(401|403|404|500)(\/(index\.html)?)?$/.test(p))
183
+ return true;
181
184
  return p === "/server-manifest.json" || p === "/api-node" || p.startsWith("/api-node/");
182
185
  }
183
186
  export function isHiddenName(name) {
@@ -188,193 +191,7 @@ export function isHiddenPath(urlPath) {
188
191
  const decoded = decodePath(urlPath);
189
192
  return decoded.split(/[/\\]/).filter(Boolean).some(isHiddenName);
190
193
  }
191
- // Redirect and rewrite rules
192
- function splitURLPath(p) {
193
- const t = trimSlashes(p);
194
- return t === "" ? [] : t.split("/");
195
- }
196
- function splitParamModifier(name) {
197
- const n = name.length;
198
- if (n > 0) {
199
- const c = name[n - 1];
200
- if (c === "*" || c === "+" || c === "?")
201
- return [name.slice(0, n - 1), c];
202
- }
203
- return [name, ""];
204
- }
205
- /**
206
- * Matches a request path against a redirect/rewrite source: exact, a trailing
207
- * "/*" wildcard (param "splat"), or named segments with the "*", "+" and "?"
208
- * modifiers.
209
- */
210
- export function matchRedirectParamsMods(path, source) {
211
- const params = {};
212
- const mods = {};
213
- if (source === path)
214
- return { params, mods };
215
- if (!/[*:]/.test(source))
216
- return null;
217
- if (source.endsWith("/*")) {
218
- const prefix = source.slice(0, -2);
219
- if (path === prefix) {
220
- params.splat = "";
221
- return { params, mods };
222
- }
223
- if (!path.startsWith(prefix + "/"))
224
- return null;
225
- params.splat = path.slice(prefix.length).replace(/^\//, "");
226
- return { params, mods };
227
- }
228
- const sourceParts = splitURLPath(source);
229
- const pathParts = splitURLPath(path);
230
- let pi = 0;
231
- for (const part of sourceParts) {
232
- if (part.startsWith(":")) {
233
- const [name, mod] = splitParamModifier(part.slice(1));
234
- mods[name] = mod;
235
- switch (mod) {
236
- case "*":
237
- params[name] = pathParts.slice(pi).join("/");
238
- return { params, mods };
239
- case "+":
240
- if (pi >= pathParts.length)
241
- return null;
242
- params[name] = pathParts.slice(pi).join("/");
243
- return { params, mods };
244
- case "?":
245
- if (pi < pathParts.length) {
246
- params[name] = pathParts[pi];
247
- pi++;
248
- }
249
- else {
250
- params[name] = "";
251
- }
252
- continue;
253
- }
254
- if (pi >= pathParts.length)
255
- return null;
256
- params[name] = pathParts[pi];
257
- pi++;
258
- continue;
259
- }
260
- if (pi >= pathParts.length || part !== pathParts[pi])
261
- return null;
262
- pi++;
263
- }
264
- if (pi !== pathParts.length)
265
- return null;
266
- return { params, mods };
267
- }
268
- export function matchRedirect(path, source) {
269
- return matchRedirectParamsMods(path, source) !== null;
270
- }
271
- /** url.PathEscape of Go: segment-escapes everything but unreserved and $&+=:@. */
272
- export function pathEscape(s) {
273
- return encodeURIComponent(s)
274
- .replace(/[!'()*]/g, (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase())
275
- .replace(/%(24|26|2B|3D|3A|40)/g, (_m, h) => String.fromCharCode(parseInt(h, 16)));
276
- }
277
- /** Percent-encodes a matched (decoded) value segment by segment, keeping "/" between segments. */
278
- export function escapeRedirectValue(v) {
279
- if (v === "")
280
- return "";
281
- return v.split("/").map(pathEscape).join("/");
282
- }
283
- function replaceDestParams(destination, params, mods) {
284
- const re = /:([A-Za-z_][A-Za-z0-9_]*)/g;
285
- let out = "";
286
- let last = 0;
287
- let m;
288
- while ((m = re.exec(destination)) !== null) {
289
- const name = m[1];
290
- if (!Object.prototype.hasOwnProperty.call(params, name))
291
- continue;
292
- let end = m.index + m[0].length;
293
- const mod = mods[name];
294
- if (mod && end < destination.length && destination[end] === mod)
295
- end++;
296
- out += destination.slice(last, m.index) + escapeRedirectValue(params[name]);
297
- last = end;
298
- re.lastIndex = end;
299
- }
300
- return out + destination.slice(last);
301
- }
302
- /** Keeps a substituted path destination on-site (no "//host" or "/\\host" results). */
303
- export function sanitizeRedirectTarget(configured, out) {
304
- if (!configured.startsWith("/") || configured.startsWith("//") || configured.startsWith("/\\"))
305
- return out;
306
- out = out.replace(/[\u0000-\u001f\u007f]/g, (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0"));
307
- const trimmed = out.replace(/^[/\\]+/, "");
308
- return trimmed !== out ? "/" + trimmed : out;
309
- }
310
- /** Fills ":splat" and ":name" placeholders of a destination from the matched path. */
311
- export function rewriteDestination(path, source, destination) {
312
- const m = matchRedirectParamsMods(path, source);
313
- if (!m)
314
- return destination;
315
- let out;
316
- if (Object.prototype.hasOwnProperty.call(m.params, "splat")) {
317
- const splat = m.params.splat;
318
- if (destination.includes(":splat"))
319
- out = destination.split(":splat").join(escapeRedirectValue(splat));
320
- else if (splat !== "")
321
- out = destination.replace(/\/+$/, "") + "/" + escapeRedirectValue(splat);
322
- else
323
- out = destination;
324
- }
325
- else {
326
- out = replaceDestParams(destination, m.params, m.mods);
327
- }
328
- return sanitizeRedirectTarget(destination, out);
329
- }
330
- // Header rules
331
- /** path.Match for slash paths: "*" and "?" do not cross "/", "[...]" classes. */
332
- export function pathMatch(pattern, name) {
333
- let re = "^";
334
- for (let i = 0; i < pattern.length; i++) {
335
- const c = pattern[i];
336
- if (c === "*")
337
- re += "[^/]*";
338
- else if (c === "?")
339
- re += "[^/]";
340
- else if (c === "[") {
341
- const end = pattern.indexOf("]", i + 1);
342
- if (end < 0)
343
- return false;
344
- let cls = pattern.slice(i + 1, end);
345
- let neg = false;
346
- if (cls.startsWith("^")) {
347
- neg = true;
348
- cls = cls.slice(1);
349
- }
350
- re += "[" + (neg ? "^" : "") + cls.replace(/\\/g, "\\\\") + "]";
351
- i = end;
352
- }
353
- else if (c === "\\" && i + 1 < pattern.length) {
354
- re += pattern[++i].replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
355
- }
356
- else {
357
- re += c.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
358
- }
359
- }
360
- try {
361
- return new RegExp(re + "$").test(name);
362
- }
363
- catch {
364
- return false;
365
- }
366
- }
367
- /** Exact path, trailing "*" prefix, or path.Match glob (config `headers[].source`). */
368
- export function headerSourceMatches(pattern, p) {
369
- pattern = pattern.trim();
370
- if (pattern === "")
371
- return false;
372
- if (!pattern.includes("*"))
373
- return p === pattern;
374
- if (pattern.endsWith("*"))
375
- return p.startsWith(pattern.slice(0, -1));
376
- return pathMatch(pattern, p);
377
- }
194
+ // Redirect, rewrite and header rule matching lives in route-source.ts.
378
195
  // Dynamic SSG templates
379
196
  export const DYNAMIC_PARAM_PREFIX = "__KRATE_PARAM_";
380
197
  export function dynamicParamSentinel(name) {
@@ -396,7 +213,7 @@ export function escapeHTML(s) {
396
213
  export function applyDynamicRouteParams(templateHTML, params) {
397
214
  const injectScript = "<script>window.__KRATE_PARAMS__=" + goJSON(params) + "</script>";
398
215
  const at = templateHTML.indexOf("</head>");
399
- let out = at < 0 ? templateHTML : templateHTML.slice(0, at) + injectScript + templateHTML.slice(at);
216
+ let out = at === -1 ? templateHTML : templateHTML.slice(0, at) + injectScript + templateHTML.slice(at);
400
217
  for (const [name, value] of Object.entries(params)) {
401
218
  out = out.split(dynamicParamSentinel(name)).join(goHTMLEscape(value));
402
219
  }
@@ -414,9 +231,11 @@ export function goJSON(obj) {
414
231
  return "{" + keys.map((k) => enc(k) + ":" + enc(obj[k])).join(",") + "}";
415
232
  }
416
233
  // Hashed assets
234
+ /** Cache-Control of a content-hashed asset; a user headers rule never replaces it. */
235
+ export const IMMUTABLE_CACHE_CONTROL = "public, max-age=31536000, immutable";
417
236
  export function staticCacheControl(urlPath, hashed) {
418
237
  if (hashed.has(urlPath))
419
- return "public, max-age=31536000, immutable";
238
+ return IMMUTABLE_CACHE_CONTROL;
420
239
  return "no-cache";
421
240
  }
422
241
  // Middleware matcher (path-to-regexp subset), shared with the Go runtimes
@@ -10,6 +10,8 @@ export interface SegmentEntry {
10
10
  error?: string;
11
11
  forbidden?: string;
12
12
  unauthorized?: string;
13
+ /** Roles whose page renders per request (its file holds the baked shell). */
14
+ dynamic?: SegmentKind[];
13
15
  }
14
16
  export interface SegmentMatch {
15
17
  /** Page path relative to the output dir ("blog/404.html"). */
@@ -17,7 +19,11 @@ export interface SegmentMatch {
17
19
  key: string;
18
20
  /** Dynamic-segment values when matched against a URL, else undefined. */
19
21
  params?: Record<string, string>;
22
+ /** The renderer's route for a page that renders per request (build/segments.go SegmentRoute). */
23
+ route?: string;
20
24
  }
25
+ /** The route the renderer knows a request-time segment page by ("/__404", "/blog/__404"). */
26
+ export declare function segmentRoute(key: string, kind: SegmentKind): string;
21
27
  /**
22
28
  * Returns the nearest segment page of a kind. matched: the request matched a
23
29
  * route whose source directory is segDir, so the lookup walks up from it.
@@ -9,6 +9,20 @@ export const SEGMENT_KIND_OF_STATUS = {
9
9
  404: "notFound",
10
10
  500: "error",
11
11
  };
12
+ const KIND_STATUS = { notFound: 404, error: 500, forbidden: 403, unauthorized: 401 };
13
+ /** The route the renderer knows a request-time segment page by ("/__404", "/blog/__404"). */
14
+ export function segmentRoute(key, kind) {
15
+ const dir = "__" + KIND_STATUS[kind];
16
+ return key === "" ? "/" + dir : "/" + key + "/" + dir;
17
+ }
18
+ function matchOf(e, kind, params) {
19
+ const m = { file: entryPage(e, kind), key: e.key };
20
+ if (params)
21
+ m.params = params;
22
+ if (e.dynamic && e.dynamic.includes(kind))
23
+ m.route = segmentRoute(e.key, kind);
24
+ return m;
25
+ }
12
26
  function entryPage(e, kind) {
13
27
  switch (kind) {
14
28
  case "error":
@@ -36,11 +50,11 @@ export function lookupSegmentPage(entries, kind, urlPath, matched, segDir) {
36
50
  for (;;) {
37
51
  const e = byKey.get(dir);
38
52
  if (e && entryPage(e, kind) !== "")
39
- return { file: entryPage(e, kind), key: e.key };
53
+ return matchOf(e, kind);
40
54
  if (dir === "")
41
55
  return null;
42
56
  const i = dir.lastIndexOf("/");
43
- dir = i >= 0 ? dir.slice(0, i) : "";
57
+ dir = i !== -1 ? dir.slice(0, i) : "";
44
58
  }
45
59
  }
46
60
  let best = null;
@@ -58,7 +72,7 @@ export function lookupSegmentPage(entries, kind, urlPath, matched, segDir) {
58
72
  }
59
73
  if (!best)
60
74
  return null;
61
- return { file: entryPage(best, kind), key: best.key, params: bestParams };
75
+ return matchOf(best, kind, bestParams);
62
76
  }
63
77
  /** Matches the leading segments of urlPath; a trailing catch-all takes the rest. */
64
78
  function matchRoutePrefix(urlPath, pattern) {
@@ -102,7 +116,7 @@ export function fillServerError(page, e) {
102
116
  page = page.split(sentinel).join(goHTMLEscape(val));
103
117
  const block = '<script type="application/json" id="__krate_error">' + escapeJSONForScript(JSON.stringify(e)) + "</script>";
104
118
  const at = page.indexOf("</head>");
105
- return at >= 0 ? page.slice(0, at) + block + page.slice(at) : block + page;
119
+ return at !== -1 ? page.slice(0, at) + block + page.slice(at) : block + page;
106
120
  }
107
121
  /** A segment page filled for one response (dynamic dir params, then the error). */
108
122
  export function renderSegmentPage(html, m, err) {
@@ -47,5 +47,14 @@ export declare function parseHeadTags(fragment: string): HeadTag[];
47
47
  export declare function mergeHeadSection(head: string, title: string, fragment: string): string;
48
48
  /** Index where the head section of a document ends (</head>, else <body), or -1. */
49
49
  export declare function headEndOf(doc: string): number;
50
+ export type DocAttrs = Map<string, string>;
51
+ /** Removes the <html> and <body> elements from a head fragment and returns their attributes. */
52
+ export declare function splitDocTags(fragment: string): {
53
+ rest: string;
54
+ html: DocAttrs;
55
+ body: DocAttrs;
56
+ };
57
+ /** Sets attrs on the first start tag of `name` in doc, keeping the attributes it already has. */
58
+ export declare function setStartTagAttrs(doc: string, name: string, attrs: DocAttrs): string;
50
59
  /** Merges the title and head tags into the head of a whole document. */
51
60
  export declare function mergeHead(doc: string, title: string, fragment: string): string;
package/dist/head-tags.js CHANGED
@@ -25,7 +25,7 @@ export const HEAD_CLOSE = "<!--/krate-head-->";
25
25
  * markers. Both are removed from the returned html.
26
26
  */
27
27
  export function takeHead(html) {
28
- const hasHead = html.indexOf(HEAD_OPEN) !== -1;
28
+ const hasHead = html.includes(HEAD_OPEN);
29
29
  const hasTitle = /<title[\s>]/i.test(html);
30
30
  if (!hasHead && !hasTitle)
31
31
  return { title: "", head: "", html };
@@ -83,7 +83,7 @@ export function takeHead(html) {
83
83
  };
84
84
  }
85
85
  function takeTitles(html) {
86
- if (html.indexOf("<title") === -1 && html.indexOf("<TITLE") === -1)
86
+ if (!html.includes("<title") && !html.includes("<TITLE"))
87
87
  return { title: "", html };
88
88
  const re = /<title(?:\s[^>]*)?>([\s\S]*?)<\/title>/gi;
89
89
  const found = [];
@@ -107,7 +107,7 @@ function takeTitles(html) {
107
107
  function insideSVG(html, pos) {
108
108
  const before = html.slice(0, pos).toLowerCase();
109
109
  let depth = 0;
110
- const re = /<(\/?)svg[\s>\/]/g;
110
+ const re = /<(\/?)svg[\s>/]/g;
111
111
  let m;
112
112
  while ((m = re.exec(before)))
113
113
  depth += m[1] ? -1 : 1;
@@ -218,8 +218,11 @@ export function headKey(el) {
218
218
  if (el.name === "meta") {
219
219
  if (a.has("charset"))
220
220
  return "meta:charset";
221
- if (a.has("name"))
222
- return "meta:name:" + (a.get("name") || "").toLowerCase();
221
+ if (a.has("name")) {
222
+ // A media-qualified tag (light and dark theme-color) is a different tag.
223
+ const media = (a.get("media") || "").toLowerCase();
224
+ return "meta:name:" + (a.get("name") || "").toLowerCase() + (media ? "|" + media : "");
225
+ }
223
226
  if (a.has("property"))
224
227
  return "meta:property:" + (a.get("property") || "").toLowerCase();
225
228
  if (a.has("http-equiv"))
@@ -318,6 +321,87 @@ export function headEndOf(doc) {
318
321
  return h;
319
322
  return lower.indexOf("<body");
320
323
  }
324
+ const ATTR_NAME = /^[A-Za-z_:][-A-Za-z0-9_:.]*$/;
325
+ function decodeAttr(v) {
326
+ return v.replace(/&(quot|amp|lt|gt|apos|#34|#39);/g, (_m, e) => {
327
+ switch (e) {
328
+ case "quot":
329
+ case "#34":
330
+ return '"';
331
+ case "lt":
332
+ return "<";
333
+ case "gt":
334
+ return ">";
335
+ case "apos":
336
+ case "#39":
337
+ return "'";
338
+ default:
339
+ return "&";
340
+ }
341
+ });
342
+ }
343
+ /** Removes the <html> and <body> elements from a head fragment and returns their attributes. */
344
+ export function splitDocTags(fragment) {
345
+ const html = new Map();
346
+ const body = new Map();
347
+ if (!/<(html|body)[\s>/]/i.test(fragment))
348
+ return { rest: fragment, html, body };
349
+ let rest = "";
350
+ let at = 0;
351
+ for (const el of scanHeadElements(fragment)) {
352
+ if (el.name !== "html" && el.name !== "body")
353
+ continue;
354
+ const target = el.name === "html" ? html : body;
355
+ for (const [k, v] of el.attrs)
356
+ if (ATTR_NAME.test(k))
357
+ target.set(k, decodeAttr(v));
358
+ rest += fragment.slice(at, el.start);
359
+ at = el.end;
360
+ }
361
+ rest += fragment.slice(at);
362
+ return { rest: rest.replace(/<\/(html|body)\s*>/gi, ""), html, body };
363
+ }
364
+ function renderAttrs(attrs) {
365
+ return [...attrs.entries()]
366
+ .filter(([k]) => ATTR_NAME.test(k))
367
+ .sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0))
368
+ .map(([k, v]) => (v === "" ? " " + k : " " + k + '="' + escapeAttr(v) + '"'))
369
+ .join("");
370
+ }
371
+ function escapeAttr(v) {
372
+ return v.replace(/[&<>"']/g, (c) => (c === '"' ? "&quot;" : c === "&" ? "&amp;" : c === "<" ? "&lt;" : c === ">" ? "&gt;" : "&#39;"));
373
+ }
374
+ /** Sets attrs on the first start tag of `name` in doc, keeping the attributes it already has. */
375
+ export function setStartTagAttrs(doc, name, attrs) {
376
+ if (attrs.size === 0)
377
+ return doc;
378
+ const re = new RegExp("<" + name + "(?=[\\s>/])", "i");
379
+ const m = re.exec(doc);
380
+ if (!m)
381
+ return doc;
382
+ let end = m.index + 1;
383
+ let quote = "";
384
+ for (; end < doc.length; end++) {
385
+ const c = doc[end];
386
+ if (quote) {
387
+ if (c === quote)
388
+ quote = "";
389
+ }
390
+ else if (c === '"' || c === "'")
391
+ quote = c;
392
+ else if (c === ">") {
393
+ end++;
394
+ break;
395
+ }
396
+ }
397
+ const merged = new Map();
398
+ for (const [k, v] of scanHeadElements(doc.slice(m.index, end))[0]?.attrs ?? [])
399
+ if (ATTR_NAME.test(k))
400
+ merged.set(k, decodeAttr(v));
401
+ for (const [k, v] of attrs)
402
+ merged.set(k, v);
403
+ return doc.slice(0, m.index) + "<" + name + renderAttrs(merged) + ">" + doc.slice(end);
404
+ }
321
405
  /** Merges the title and head tags into the head of a whole document. */
322
406
  export function mergeHead(doc, title, fragment) {
323
407
  if (title === "" && fragment === "")
@@ -325,5 +409,8 @@ export function mergeHead(doc, title, fragment) {
325
409
  const end = headEndOf(doc);
326
410
  if (end === -1)
327
411
  return doc;
328
- return mergeHeadSection(doc.slice(0, end), title, fragment) + doc.slice(end);
412
+ const { rest, html, body } = splitDocTags(fragment);
413
+ let out = mergeHeadSection(doc.slice(0, end), title, rest) + doc.slice(end);
414
+ out = setStartTagAttrs(out, "html", html);
415
+ return setStartTagAttrs(out, "body", body);
329
416
  }
@@ -126,7 +126,7 @@ function bindCode(code) {
126
126
  return;
127
127
  for (let n = node.nextSibling; n && n !== r.end;) {
128
128
  const next = n.nextSibling;
129
- n.parentNode && n.parentNode.removeChild(n);
129
+ n.parentNode?.removeChild(n);
130
130
  n = next;
131
131
  }
132
132
  const text = node.textContent || "";
@@ -4,8 +4,9 @@
4
4
  * (rule lists with JavaScript regular expressions); this file runs them. It
5
5
  * follows chroma's RegexLexer state machine and its HTML formatter, so for the
6
6
  * same code and language the markup is identical to what a build produces:
7
- * one `<span class="line"><span class="cl">` per line and one classed span per
8
- * token, coloured by the same stylesheet in light and dark.
7
+ * one classed span per token (wrapped in a `<span class="line hl">` only for
8
+ * lines marked for highlighting), coloured by the same stylesheet in light and
9
+ * dark.
9
10
  *
10
11
  * Nothing here touches the DOM or any global, so it runs unchanged on the
11
12
  * server (SSR sidecars) and in the browser.
package/dist/highlight.js CHANGED
@@ -4,8 +4,9 @@
4
4
  * (rule lists with JavaScript regular expressions); this file runs them. It
5
5
  * follows chroma's RegexLexer state machine and its HTML formatter, so for the
6
6
  * same code and language the markup is identical to what a build produces:
7
- * one `<span class="line"><span class="cl">` per line and one classed span per
8
- * token, coloured by the same stylesheet in light and dark.
7
+ * one classed span per token (wrapped in a `<span class="line hl">` only for
8
+ * lines marked for highlighting), coloured by the same stylesheet in light and
9
+ * dark.
9
10
  *
10
11
  * Nothing here touches the DOM or any global, so it runs unchanged on the
11
12
  * server (SSR sidecars) and in the browser.
@@ -64,7 +65,7 @@ function lexerFor(name) {
64
65
  return key === undefined ? undefined : lexers.get(key);
65
66
  }
66
67
  function ensureLF(text) {
67
- if (text.indexOf("\r") < 0)
68
+ if (!text.includes("\r"))
68
69
  return text;
69
70
  return text.replace(/\r\n?/g, "\n");
70
71
  }
@@ -125,7 +126,7 @@ function lex(lx, text, startState, nested, out) {
125
126
  continue;
126
127
  }
127
128
  const cp = text.codePointAt(pos);
128
- const n = cp > 0xffff ? 2 : 1;
129
+ const n = cp > 0xFFFF ? 2 : 1;
129
130
  out.push(["err", text.substr(pos, n)]);
130
131
  pos += n;
131
132
  continue;
@@ -212,7 +213,6 @@ function emitTokens(lx, emit, groups, out) {
212
213
  emitTokens(lx, e, [group(groups, gi)], out);
213
214
  }
214
215
  }
215
- return;
216
216
  }
217
217
  }
218
218
  }
@@ -245,12 +245,12 @@ export function parseLineRanges(spec) {
245
245
  if (part === "")
246
246
  continue;
247
247
  const dash = part.indexOf("-");
248
- const lo = dash < 0 ? part : part.slice(0, dash);
248
+ const lo = dash === -1 ? part : part.slice(0, dash);
249
249
  const a = atoi(lo.trim());
250
250
  if (a === null || a < 1)
251
251
  continue;
252
252
  let b = a;
253
- if (dash >= 0) {
253
+ if (dash !== -1) {
254
254
  const hi = atoi(part.slice(dash + 1).trim());
255
255
  if (hi === null || hi < a)
256
256
  continue;
@@ -303,19 +303,24 @@ function shouldHighlight(ranges, idx, line) {
303
303
  /** Writes tokens as chroma's HTML formatter does (class mode, no line numbers). */
304
304
  function format(tokens, ranges) {
305
305
  const lines = splitLines(tokens);
306
+ // Line wrappers only carry the highlight class, so unmarked code omits them.
307
+ const wrap = ranges.length > 0;
306
308
  let html = "";
307
309
  let idx = 0;
308
310
  for (let i = 0; i < lines.length; i++) {
309
- const [hl, next] = shouldHighlight(ranges, idx, i + 1);
310
- idx = next;
311
- html += hl ? '<span class="line hl"><span class="cl">' : '<span class="line"><span class="cl">';
311
+ if (wrap) {
312
+ const [hl, next] = shouldHighlight(ranges, idx, i + 1);
313
+ idx = next;
314
+ html += hl ? '<span class="line hl"><span class="cl">' : '<span class="line"><span class="cl">';
315
+ }
312
316
  for (const [cls, value] of lines[i]) {
313
317
  if (cls === null)
314
318
  continue;
315
319
  const text = escapeHTML(value);
316
320
  html += cls === "" ? text : '<span class="' + cls + '">' + text + "</span>";
317
321
  }
318
- html += "</span></span>";
322
+ if (wrap)
323
+ html += "</span></span>";
319
324
  }
320
325
  return html;
321
326
  }
@@ -397,7 +402,7 @@ function splitFrontmatter(text) {
397
402
  return null;
398
403
  for (let pos = lineEnd + 1; pos < text.length;) {
399
404
  const next = text.indexOf("\n", pos);
400
- if (next < 0)
405
+ if (next === -1)
401
406
  break;
402
407
  lineEnd = next;
403
408
  const line = text.slice(pos, lineEnd).replace(/\r$/, "");
package/dist/hints.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- interface HintOptions {
1
+ export interface HintOptions {
2
2
  as?: string;
3
3
  crossOrigin?: string;
4
4
  integrity?: string;
@@ -9,11 +9,34 @@ interface HintOptions {
9
9
  referrerPolicy?: string;
10
10
  imageSrcSet?: string;
11
11
  imageSizes?: string;
12
+ precedence?: string;
12
13
  }
14
+ /**
15
+ * Records head tags the build collected for a component that the server does
16
+ * not render (a real-React client component). Server only; the browser
17
+ * already has them in the document.
18
+ */
19
+ export declare function krateHintTags(tags: readonly string[]): void;
13
20
  export declare function krateHintPreload(href: string, opts?: HintOptions): void;
14
21
  export declare function krateHintPreloadModule(href: string, opts?: HintOptions): void;
15
22
  export declare function krateHintPreconnect(href: string, opts?: HintOptions): void;
16
23
  export declare function krateHintPrefetchDNS(href: string): void;
17
24
  export declare function krateHintPreinit(href: string, opts?: HintOptions): void;
18
25
  export declare function krateHintPreinitModule(href: string, opts?: HintOptions): void;
19
- export {};
26
+ export declare const preload: typeof krateHintPreload;
27
+ export declare const preinit: typeof krateHintPreinit;
28
+ export declare const preconnect: typeof krateHintPreconnect;
29
+ export declare const prefetchDNS: typeof krateHintPrefetchDNS;
30
+ export declare const preloadModule: typeof krateHintPreloadModule;
31
+ export declare const preinitModule: typeof krateHintPreinitModule;
32
+ export declare function flushSync<T>(fn: () => T): T;
33
+ declare const _default: {
34
+ preload: typeof krateHintPreload;
35
+ preinit: typeof krateHintPreinit;
36
+ preconnect: typeof krateHintPreconnect;
37
+ prefetchDNS: typeof krateHintPrefetchDNS;
38
+ preloadModule: typeof krateHintPreloadModule;
39
+ preinitModule: typeof krateHintPreinitModule;
40
+ flushSync: typeof flushSync;
41
+ };
42
+ export default _default;