@krate/runtime 0.5.0-rc.6 → 0.5.0-rc.7

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.
@@ -23,4 +23,17 @@ export interface ReactCompatExtras {
23
23
  * one form-status context.
24
24
  */
25
25
  export declare function createReactCompatExtras(c: any, options: any): ReactCompatExtras;
26
+ /**
27
+ * React 18 flushes the passive effects of a render caused by a discrete event
28
+ * (a click, a key press, typing) synchronously at the end of its commit, and
29
+ * renders the updates those effects make in the same task. Preact runs them
30
+ * after the next frame, so a quick second event could render a component with
31
+ * a new prop and a state update from its previous effects at once: react-colorful
32
+ * then took its own color prop for user input and called onChange with a
33
+ * stale colour. While a discrete event's task runs, this flushes Preact's
34
+ * effect queue after every commit (Preact's render loop picks up the updates
35
+ * they make); otherwise effects keep running after paint. The compiler's
36
+ * client builds call it from their `react` module, once per Preact instance.
37
+ */
38
+ export declare function installDiscreteEffects(options: any): void;
26
39
  export {};
@@ -290,3 +290,70 @@ function build(c, options) {
290
290
  useTransition,
291
291
  };
292
292
  }
293
+ // Events React treats as discrete: the effects of a render they cause are
294
+ // flushed at the end of its commit, not after paint.
295
+ const DISCRETE_EVENTS = [
296
+ 'beforeinput', 'blur', 'change', 'click', 'compositionend', 'compositionstart', 'contextmenu', 'copy', 'cut',
297
+ 'dblclick', 'dragend', 'dragstart', 'drop', 'focusin', 'focusout', 'input', 'keydown', 'keypress', 'keyup',
298
+ 'mousedown', 'mouseup', 'paste', 'pointercancel', 'pointerdown', 'pointerup', 'reset', 'submit',
299
+ 'touchcancel', 'touchend', 'touchstart',
300
+ ];
301
+ /**
302
+ * React 18 flushes the passive effects of a render caused by a discrete event
303
+ * (a click, a key press, typing) synchronously at the end of its commit, and
304
+ * renders the updates those effects make in the same task. Preact runs them
305
+ * after the next frame, so a quick second event could render a component with
306
+ * a new prop and a state update from its previous effects at once: react-colorful
307
+ * then took its own color prop for user input and called onChange with a
308
+ * stale colour. While a discrete event's task runs, this flushes Preact's
309
+ * effect queue after every commit (Preact's render loop picks up the updates
310
+ * they make); otherwise effects keep running after paint. The compiler's
311
+ * client builds call it from their `react` module, once per Preact instance.
312
+ */
313
+ export function installDiscreteEffects(options) {
314
+ if (typeof window === 'undefined' || typeof document === 'undefined' || options.__krateDiscrete)
315
+ return;
316
+ options.__krateDiscrete = true;
317
+ let discrete = false;
318
+ let clearing = false;
319
+ let flushEffects = null;
320
+ const onEvent = () => {
321
+ discrete = true;
322
+ if (clearing)
323
+ return;
324
+ clearing = true;
325
+ setTimeout(() => {
326
+ discrete = false;
327
+ clearing = false;
328
+ }, 0);
329
+ };
330
+ for (const type of DISCRETE_EVENTS)
331
+ window.addEventListener(type, onEvent, true);
332
+ // Preact's default afterNextFrame, used outside discrete events. The flush
333
+ // function preact/hooks hands over is always the same one, so it is kept.
334
+ const prevRaf = options.requestAnimationFrame;
335
+ options.requestAnimationFrame = (cb) => {
336
+ flushEffects = cb;
337
+ if (prevRaf)
338
+ return prevRaf(cb);
339
+ let raf = 0;
340
+ const done = () => {
341
+ clearTimeout(timeout);
342
+ if (typeof cancelAnimationFrame === 'function')
343
+ cancelAnimationFrame(raf);
344
+ setTimeout(cb);
345
+ };
346
+ const timeout = setTimeout(done, 100);
347
+ if (typeof requestAnimationFrame === 'function')
348
+ raf = requestAnimationFrame(done);
349
+ };
350
+ // options._commit (mangled __c) runs after refs and, through preact/hooks'
351
+ // own wrapper, after layout effects.
352
+ const prevCommit = options.__c;
353
+ options.__c = (vnode, queue) => {
354
+ if (prevCommit)
355
+ prevCommit(vnode, queue);
356
+ if (discrete && flushEffects)
357
+ flushEffects();
358
+ };
359
+ }
@@ -0,0 +1,4 @@
1
+ export declare function requestCookiesOf(headers: Headers): Map<string, string>;
2
+ export declare function attachCookies(request: Request, basePath?: string): Request;
3
+ /** A URL carrying Next's NextURL extras: basePath (pathname never includes it) and clone(). */
4
+ export declare function makeNextUrl(href: string, basePath: string): URL;
@@ -0,0 +1,51 @@
1
+ // Next's request extras on a route handler's Request: a read-only
2
+ // request.cookies and request.nextUrl. Shared by the server handler and
3
+ // @krate/runtime/testing so tests see what the server passes.
4
+ export function requestCookiesOf(headers) {
5
+ const jar = new Map();
6
+ for (const part of String(headers.get("cookie") || "").split(";")) {
7
+ const i = part.indexOf("=");
8
+ if (i <= 0)
9
+ continue;
10
+ const name = part.slice(0, i).trim();
11
+ if (!name || jar.has(name))
12
+ continue;
13
+ // Same parsing as the action context and the Node sidecar: unquote, then percent-decode.
14
+ let value = part.slice(i + 1).trim();
15
+ if (value.startsWith('"') && value.endsWith('"'))
16
+ value = value.slice(1, -1);
17
+ try {
18
+ value = decodeURIComponent(value);
19
+ }
20
+ catch {
21
+ // keep the raw value
22
+ }
23
+ jar.set(name, value);
24
+ }
25
+ return jar;
26
+ }
27
+ export function attachCookies(request, basePath = "") {
28
+ try {
29
+ const jar = requestCookiesOf(request.headers);
30
+ Object.defineProperty(request, "cookies", {
31
+ value: {
32
+ get: (n) => (jar.has(String(n)) ? { name: String(n), value: jar.get(String(n)) } : undefined),
33
+ getAll: (n) => [...jar].filter(([k]) => n === undefined || k === String(n)).map(([name, value]) => ({ name, value })),
34
+ has: (n) => jar.has(String(n)),
35
+ },
36
+ enumerable: false,
37
+ });
38
+ Object.defineProperty(request, "nextUrl", { value: makeNextUrl(request.url, basePath), enumerable: false });
39
+ }
40
+ catch {
41
+ // frozen request objects keep working without the cookies accessor
42
+ }
43
+ return request;
44
+ }
45
+ /** A URL carrying Next's NextURL extras: basePath (pathname never includes it) and clone(). */
46
+ export function makeNextUrl(href, basePath) {
47
+ const u = new URL(href);
48
+ Object.defineProperty(u, "basePath", { value: basePath, enumerable: false });
49
+ Object.defineProperty(u, "clone", { value: () => makeNextUrl(u.href, basePath), enumerable: false });
50
+ return u;
51
+ }
@@ -0,0 +1,39 @@
1
+ export type SourceKind = "redirect" | "rewrite" | "header";
2
+ export interface RouteCondition {
3
+ type: "query" | "header" | "cookie" | "host" | string;
4
+ key?: string;
5
+ value?: string;
6
+ }
7
+ export interface MatchResult {
8
+ params: Record<string, string>;
9
+ mods: Record<string, string>;
10
+ }
11
+ export interface CompiledSource {
12
+ match(path: string): MatchResult | null;
13
+ }
14
+ /** Compiles a source. It throws when the source cannot be matched. */
15
+ export declare function compileSource(source: string, kind: SourceKind): CompiledSource;
16
+ export interface CondRequest {
17
+ headers: Headers;
18
+ url: URL;
19
+ }
20
+ export interface Conditions {
21
+ match(req: CondRequest): Record<string, string> | null;
22
+ }
23
+ /** Compiles has/missing; null when there are none. It throws on an invalid item. */
24
+ export declare function compileConditions(has?: RouteCondition[], missing?: RouteCondition[]): Conditions | null;
25
+ /** A compiled rule: source plus conditions. */
26
+ export interface RouteMatcher {
27
+ match(path: string, req: CondRequest): MatchResult | null;
28
+ }
29
+ export declare function compileMatcher(source: string, kind: SourceKind, has?: RouteCondition[], missing?: RouteCondition[]): RouteMatcher;
30
+ /** url.PathEscape of Go: segment-escapes everything but unreserved and $&+=:@. */
31
+ export declare function pathEscape(s: string): string;
32
+ /** Percent-encodes a matched (decoded) value segment by segment, keeping "/" between segments. */
33
+ export declare function escapeRedirectValue(v: string): string;
34
+ /** Keeps a substituted path destination on-site (no "//host" or "/\\host" results). */
35
+ export declare function sanitizeRedirectTarget(configured: string, out: string): string;
36
+ /** Fills ":splat" and ":name" placeholders of a destination from a match. */
37
+ export declare function buildDestination(destination: string, m: MatchResult): string;
38
+ /** Adds the request's query to the destination's own; the destination's keys win. */
39
+ export declare function mergeRequestQuery(dest: string, rawQuery: string): string;