foldkit-ssr 0.9.0 → 0.11.0
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/README.md +402 -36
- package/dist/{client-F16QT0sc.d.ts → client-qmdYY-Eb.d.ts} +15 -5
- package/dist/client-qmdYY-Eb.d.ts.map +1 -0
- package/dist/client.d.ts +5 -5
- package/dist/client.js +126 -54
- package/dist/client.js.map +1 -1
- package/dist/context.d.ts +1 -1
- package/dist/context.js +12 -0
- package/dist/context.js.map +1 -1
- package/dist/diagnostics.d.ts +2 -2
- package/dist/diagnostics.js +8 -1
- package/dist/diagnostics.js.map +1 -1
- package/dist/index.d.ts +127 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +317 -59
- package/dist/index.js.map +1 -1
- package/dist/keep.d.ts +36 -0
- package/dist/keep.d.ts.map +1 -0
- package/dist/keep.js +109 -0
- package/dist/keep.js.map +1 -0
- package/dist/{listen-zNd0IIAT.d.ts → listen-DaUPRKVR.d.ts} +4 -2
- package/dist/listen-DaUPRKVR.d.ts.map +1 -0
- package/dist/listen.d.ts +2 -2
- package/dist/listen.js +31 -23
- package/dist/listen.js.map +1 -1
- package/dist/pageCache-DO0pOgnc.d.ts +80 -0
- package/dist/pageCache-DO0pOgnc.d.ts.map +1 -0
- package/dist/pageCache.d.ts +2 -0
- package/dist/pageCache.js +112 -0
- package/dist/pageCache.js.map +1 -0
- package/dist/{requestContext-D9nsbGSJ.d.ts → requestContext-DmwmQeci.d.ts} +12 -2
- package/dist/requestContext-DmwmQeci.d.ts.map +1 -0
- package/dist/requestContext.d.ts +2 -2
- package/dist/requestContext.js +30 -13
- package/dist/requestContext.js.map +1 -1
- package/dist/{resumable-BWItJsxF.d.ts → resumable-BPN_qQHU.d.ts} +33 -12
- package/dist/resumable-BPN_qQHU.d.ts.map +1 -0
- package/dist/resumable.d.ts +2 -2
- package/dist/resumable.js +51 -8
- package/dist/resumable.js.map +1 -1
- package/dist/{shared-v1u-OEQI.d.ts → shared-CJN-m58Z.d.ts} +37 -10
- package/dist/shared-CJN-m58Z.d.ts.map +1 -0
- package/dist/shared.d.ts +2 -2
- package/dist/shared.js +31 -7
- package/dist/shared.js.map +1 -1
- package/dist/vite.d.ts +1 -1
- package/package.json +9 -9
- package/dist/client-F16QT0sc.d.ts.map +0 -1
- package/dist/listen-zNd0IIAT.d.ts.map +0 -1
- package/dist/requestContext-D9nsbGSJ.d.ts.map +0 -1
- package/dist/resumable-BWItJsxF.d.ts.map +0 -1
- package/dist/shared-v1u-OEQI.d.ts.map +0 -1
package/dist/keep.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { KEEP_ATTRIBUTE } from "./resumable.js";
|
|
2
|
+
import { typedMessage } from "./listen.js";
|
|
3
|
+
import { Result } from "effect";
|
|
4
|
+
// src/keep.ts
|
|
5
|
+
/**
|
|
6
|
+
* What is typed before the runtime starts, read from the fields themselves.
|
|
7
|
+
*
|
|
8
|
+
* A field the view marks with `KeepTyped` keeps its typing in the browser's
|
|
9
|
+
* own state (its value, its selection) until the runtime adopts it, and
|
|
10
|
+
* hydration then re-asserts the value the server rendered. The page reads
|
|
11
|
+
* the marked fields when its script runs, turns each changed one into its
|
|
12
|
+
* binding's Message, and after the first render puts the value and the
|
|
13
|
+
* selection back, so the Message that follows finds the field already
|
|
14
|
+
* holding its value and writes nothing.
|
|
15
|
+
*/
|
|
16
|
+
const isField = (element) => element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement;
|
|
17
|
+
/**
|
|
18
|
+
* The fields in `root` marked to keep their typing, each with its binding.
|
|
19
|
+
* Refused, with the reason, when a marker names no `OnInput` or `OnChange`
|
|
20
|
+
* binding, or sits on an element that is not a field: the server writes
|
|
21
|
+
* neither, so the page was altered.
|
|
22
|
+
*/
|
|
23
|
+
const keptFields = (root, bindings) => {
|
|
24
|
+
const kept = [];
|
|
25
|
+
for (const element of Array.from(root.querySelectorAll(`[${KEEP_ATTRIBUTE}]`))) {
|
|
26
|
+
const token = element.getAttribute("data-foldkit-plus-keep") ?? "";
|
|
27
|
+
const binding = bindings[Number(token)];
|
|
28
|
+
if (!/^\d+$/.test(token) || binding === void 0 || binding.event !== "input" && binding.event !== "change") return Result.fail(`the marker ${KEEP_ATTRIBUTE}="${token}" names no input or change binding the page carries`);
|
|
29
|
+
if (!isField(element)) return Result.fail(`the marker ${KEEP_ATTRIBUTE} is on a ${element.localName}, not a field`);
|
|
30
|
+
kept.push({
|
|
31
|
+
element,
|
|
32
|
+
binding
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return Result.succeed(kept);
|
|
36
|
+
};
|
|
37
|
+
/** Whether an option is disabled by its own attribute or by a disabled `optgroup` around it. */
|
|
38
|
+
const disabledOption = (option) => option.disabled || (option.closest("optgroup")?.disabled ?? false);
|
|
39
|
+
/** A select's display size: its `size` when it parses above zero, else 4 for `multiple` and 1 otherwise. */
|
|
40
|
+
const displaySize = (select) => {
|
|
41
|
+
const size = select.getAttribute("size");
|
|
42
|
+
const parsed = size === null ? null : /^\s*(\d+)\s*$/.exec(size);
|
|
43
|
+
if (parsed !== null && Number(parsed[1]) > 0) return Number(parsed[1]);
|
|
44
|
+
return select.multiple ? 4 : 1;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The value the server rendered into a field: its default, which typing
|
|
48
|
+
* leaves alone. A `select`'s follows the HTML reset algorithm: the last option
|
|
49
|
+
* marked `selected` wins, else a single-line select takes its first option
|
|
50
|
+
* that is not disabled, through an `optgroup` included.
|
|
51
|
+
*/
|
|
52
|
+
const servedValue = (element) => {
|
|
53
|
+
if (!(element instanceof HTMLSelectElement)) return element.defaultValue;
|
|
54
|
+
const options = Array.from(element.options);
|
|
55
|
+
return (options.filter((option) => option.defaultSelected).at(-1) ?? (displaySize(element) === 1 ? options.find((option) => !disabledOption(option)) : void 0))?.value ?? "";
|
|
56
|
+
};
|
|
57
|
+
/** The indices of a `select`'s options that `chosen` picks, in document order. */
|
|
58
|
+
const indicesOf = (select, chosen) => Array.from(select.options).flatMap((option, index) => chosen(option) ? [index] : []);
|
|
59
|
+
/** Whether the field holds something other than what the server rendered. */
|
|
60
|
+
const changed = (field) => {
|
|
61
|
+
const { element } = field;
|
|
62
|
+
if (element instanceof HTMLSelectElement && element.multiple) {
|
|
63
|
+
const selected = indicesOf(element, (option) => option.selected);
|
|
64
|
+
const served = indicesOf(element, (option) => option.defaultSelected);
|
|
65
|
+
return selected.length !== served.length || selected.some((index, at) => index !== served[at]);
|
|
66
|
+
}
|
|
67
|
+
return element.value !== servedValue(element);
|
|
68
|
+
};
|
|
69
|
+
/** The Messages that carry each changed field's typing, in document order. */
|
|
70
|
+
const typedMessages = (fields) => fields.filter(changed).map((field) => typedMessage(field.binding, field.element.value));
|
|
71
|
+
/** Each changed field's state, to put back once hydration has re-asserted the served value. */
|
|
72
|
+
const hold = (fields) => fields.filter(changed).map(({ element }) => {
|
|
73
|
+
if (element instanceof HTMLSelectElement) return element.multiple ? {
|
|
74
|
+
element,
|
|
75
|
+
value: element.value,
|
|
76
|
+
selected: indicesOf(element, (option) => option.selected)
|
|
77
|
+
} : {
|
|
78
|
+
element,
|
|
79
|
+
value: element.value
|
|
80
|
+
};
|
|
81
|
+
const { selectionStart: start, selectionEnd: end } = element;
|
|
82
|
+
return {
|
|
83
|
+
element,
|
|
84
|
+
value: element.value,
|
|
85
|
+
...start === null || end === null ? {} : { selection: [
|
|
86
|
+
start,
|
|
87
|
+
end,
|
|
88
|
+
element.selectionDirection ?? "none"
|
|
89
|
+
] }
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
/**
|
|
93
|
+
* Puts each held field back as it was. The first render adopted it, so it is
|
|
94
|
+
* the same element, which the person may still be typing into.
|
|
95
|
+
*/
|
|
96
|
+
const restore = (held) => {
|
|
97
|
+
for (const { element, value, selected, selection } of held) {
|
|
98
|
+
if (element instanceof HTMLSelectElement && selected !== void 0) {
|
|
99
|
+
const chosen = new Set(selected);
|
|
100
|
+
Array.from(element.options).forEach((option, index) => {
|
|
101
|
+
option.selected = chosen.has(index);
|
|
102
|
+
});
|
|
103
|
+
} else element.value = value;
|
|
104
|
+
if (selection !== void 0 && !(element instanceof HTMLSelectElement)) element.setSelectionRange(...selection);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
export { changed, hold, keptFields, restore, typedMessages };
|
|
108
|
+
|
|
109
|
+
//# sourceMappingURL=keep.js.map
|
package/dist/keep.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keep.js","names":[],"sources":["../src/keep.ts"],"sourcesContent":["/**\n * What is typed before the runtime starts, read from the fields themselves.\n *\n * A field the view marks with `KeepTyped` keeps its typing in the browser's\n * own state (its value, its selection) until the runtime adopts it, and\n * hydration then re-asserts the value the server rendered. The page reads\n * the marked fields when its script runs, turns each changed one into its\n * binding's Message, and after the first render puts the value and the\n * selection back, so the Message that follows finds the field already\n * holding its value and writes nothing.\n */\nimport { Result } from 'effect'\nimport { typedMessage, type DecodedBinding } from './listen.js'\nimport { KEEP_ATTRIBUTE } from './resumable.js'\n\ntype Field = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement\n\n/** A marked field and the binding whose Message replays its typing. */\nexport interface KeptField {\n readonly element: Field\n readonly binding: DecodedBinding\n}\n\nconst isField = (element: Element): element is Field =>\n element instanceof HTMLInputElement ||\n element instanceof HTMLTextAreaElement ||\n element instanceof HTMLSelectElement\n\n/**\n * The fields in `root` marked to keep their typing, each with its binding.\n * Refused, with the reason, when a marker names no `OnInput` or `OnChange`\n * binding, or sits on an element that is not a field: the server writes\n * neither, so the page was altered.\n */\nexport const keptFields = (\n root: Element,\n bindings: ReadonlyArray<DecodedBinding>,\n): Result.Result<ReadonlyArray<KeptField>, string> => {\n const kept: Array<KeptField> = []\n for (const element of Array.from(root.querySelectorAll(`[${KEEP_ATTRIBUTE}]`))) {\n const token = element.getAttribute(KEEP_ATTRIBUTE) ?? ''\n const binding = bindings[Number(token)]\n // A binding of either event has a hole: a closure is no binding at all.\n if (\n !/^\\d+$/.test(token) ||\n binding === undefined ||\n (binding.event !== 'input' && binding.event !== 'change')\n ) {\n return Result.fail(\n `the marker ${KEEP_ATTRIBUTE}=\"${token}\" names no input or change binding the page carries`,\n )\n }\n if (!isField(element)) {\n return Result.fail(`the marker ${KEEP_ATTRIBUTE} is on a ${element.localName}, not a field`)\n }\n kept.push({ element, binding })\n }\n return Result.succeed(kept)\n}\n\n/** Whether an option is disabled by its own attribute or by a disabled `optgroup` around it. */\nconst disabledOption = (option: HTMLOptionElement): boolean =>\n option.disabled || (option.closest('optgroup')?.disabled ?? false)\n\n/** A select's display size: its `size` when it parses above zero, else 4 for `multiple` and 1 otherwise. */\nconst displaySize = (select: HTMLSelectElement): number => {\n const size = select.getAttribute('size')\n const parsed = size === null ? null : /^\\s*(\\d+)\\s*$/.exec(size)\n if (parsed !== null && Number(parsed[1]) > 0) return Number(parsed[1])\n return select.multiple ? 4 : 1\n}\n\n/**\n * The value the server rendered into a field: its default, which typing\n * leaves alone. A `select`'s follows the HTML reset algorithm: the last option\n * marked `selected` wins, else a single-line select takes its first option\n * that is not disabled, through an `optgroup` included.\n */\nconst servedValue = (element: Field): string => {\n if (!(element instanceof HTMLSelectElement)) return element.defaultValue\n const options = Array.from(element.options)\n const chosen =\n options.filter(option => option.defaultSelected).at(-1) ??\n (displaySize(element) === 1 ? options.find(option => !disabledOption(option)) : undefined)\n return chosen?.value ?? ''\n}\n\n/** The indices of a `select`'s options that `chosen` picks, in document order. */\nconst indicesOf = (\n select: HTMLSelectElement,\n chosen: (option: HTMLOptionElement) => boolean,\n): ReadonlyArray<number> =>\n Array.from(select.options).flatMap((option, index) => (chosen(option) ? [index] : []))\n\n/** Whether the field holds something other than what the server rendered. */\nexport const changed = (field: KeptField): boolean => {\n const { element } = field\n // A multiple select holds a set of selections, which its `value`, the first\n // of them, cannot tell apart: compare the whole set with the served one.\n if (element instanceof HTMLSelectElement && element.multiple) {\n const selected = indicesOf(element, option => option.selected)\n const served = indicesOf(element, option => option.defaultSelected)\n return selected.length !== served.length || selected.some((index, at) => index !== served[at])\n }\n return element.value !== servedValue(element)\n}\n\n/** The Messages that carry each changed field's typing, in document order. */\nexport const typedMessages = (fields: ReadonlyArray<KeptField>): ReadonlyArray<unknown> =>\n fields.filter(changed).map(field => typedMessage(field.binding, field.element.value))\n\n/** A changed field's value and selection, as they were just before the runtime started. */\nexport interface Held {\n readonly element: Field\n readonly value: string\n /** A `multiple` select's selected option indices: its `value` names only the first. */\n readonly selected?: readonly number[] | undefined\n readonly selection?: readonly [number, number, 'forward' | 'backward' | 'none'] | undefined\n}\n\n/** Each changed field's state, to put back once hydration has re-asserted the served value. */\nexport const hold = (fields: ReadonlyArray<KeptField>): ReadonlyArray<Held> =>\n fields.filter(changed).map(({ element }) => {\n if (element instanceof HTMLSelectElement) {\n return element.multiple\n ? {\n element,\n value: element.value,\n selected: indicesOf(element, option => option.selected),\n }\n : { element, value: element.value }\n }\n // An input type with no selection (`email`, `number`) reads null.\n const { selectionStart: start, selectionEnd: end } = element\n return {\n element,\n value: element.value,\n ...(start === null || end === null\n ? {}\n : { selection: [start, end, element.selectionDirection ?? 'none'] as const }),\n }\n })\n\n/**\n * Puts each held field back as it was. The first render adopted it, so it is\n * the same element, which the person may still be typing into.\n */\nexport const restore = (held: ReadonlyArray<Held>): void => {\n for (const { element, value, selected, selection } of held) {\n if (element instanceof HTMLSelectElement && selected !== undefined) {\n const chosen = new Set(selected)\n Array.from(element.options).forEach((option, index) => {\n option.selected = chosen.has(index)\n })\n } else {\n element.value = value\n }\n if (selection !== undefined && !(element instanceof HTMLSelectElement)) {\n element.setSelectionRange(...selection)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAuBA,MAAM,WAAW,YACf,mBAAmB,oBACnB,mBAAmB,uBACnB,mBAAmB;;;;;;;AAQrB,MAAa,cACX,MACA,aACoD;CACpD,MAAM,OAAyB,CAA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AE,mBAAmB,oBAClD,QAAQ,kBAAkB,GAAG,SAAS;CAE1C;AACF"}
|
|
@@ -38,6 +38,8 @@ interface DecodedBinding {
|
|
|
38
38
|
* Messages, is refused whole, with the reason.
|
|
39
39
|
*/
|
|
40
40
|
declare const decodeBindings: (Message: Schema.Top | undefined, encoded: ReadonlyArray<EncodedBinding>, root: ParentNode, allowed: ReadonlySet<string>) => Result.Result<ReadonlyArray<DecodedBinding>, string>;
|
|
41
|
+
/** The Message an `OnInput` or `OnChange` hole binding dispatches for a field holding `value`. */
|
|
42
|
+
declare const typedMessage: (binding: DecodedBinding, value: string) => unknown;
|
|
41
43
|
/**
|
|
42
44
|
* What the markers make of one event: the Messages its bindings dispatch, in
|
|
43
45
|
* the order the live page would, and, when the walk met a handler the page
|
|
@@ -65,5 +67,5 @@ interface ListenOptions {
|
|
|
65
67
|
* the markers. Returns what removes the listeners.
|
|
66
68
|
*/
|
|
67
69
|
declare const listen: (root: Element, options: ListenOptions) => (() => void);
|
|
68
|
-
export { decodeBindings as a, ListenOptions as i, DecodedBinding as n, listen as o, EncodedBinding as r, readBindings as s, Answer as t };
|
|
69
|
-
//# sourceMappingURL=listen-
|
|
70
|
+
export { decodeBindings as a, typedMessage as c, ListenOptions as i, DecodedBinding as n, listen as o, EncodedBinding as r, readBindings as s, Answer as t };
|
|
71
|
+
//# sourceMappingURL=listen-DaUPRKVR.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listen-DaUPRKVR.d.ts","names":[],"sources":["../src/listen.ts"],"mappings":";;;;;;;;;UA0BiB;WACN;WACA;WACA,OAAO;WACP;WACA;;;;;;;;;cAaE,eAAY,mBAEtB,OAAO,OAAO,cAAc;;UA+Bd;WACN;WACA;WACA;WACA,OAAO;WACP;WACA;;;;;;;;cASE,iBAAc,SAChB,OAAO,iBAAe,SACtB,cAAc,iBAAe,MAChC,YAAU,SACP,wBACR,OAAO,OAAO,cAAc;;cA+FlB,eAAY,SAAa,gBAAc;;;;;;;UAgCnC;WACN,OAAO;WACP,UAAU;WACV,UAAU;;UAGJ;WACN,UAAU,cAAc;WACxB,WAAW,QAAQ;;;;;;;WAOnB,SAAS;;;;;;cAOP,SAAM,MAAU,SAAO,SAAW"}
|
package/dist/listen.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as decodeBindings, i as ListenOptions, n as DecodedBinding, o as listen, r as EncodedBinding, s as readBindings, t as Answer } from "./listen-
|
|
2
|
-
export { Answer, DecodedBinding, EncodedBinding, ListenOptions, decodeBindings, listen, readBindings };
|
|
1
|
+
import { a as decodeBindings, c as typedMessage, i as ListenOptions, n as DecodedBinding, o as listen, r as EncodedBinding, s as readBindings, t as Answer } from "./listen-DaUPRKVR.js";
|
|
2
|
+
export { Answer, DecodedBinding, EncodedBinding, ListenOptions, decodeBindings, listen, readBindings, typedMessage };
|
package/dist/listen.js
CHANGED
|
@@ -97,48 +97,56 @@ const modifiersOf = (event) => ({
|
|
|
97
97
|
altKey: event.altKey,
|
|
98
98
|
metaKey: event.metaKey
|
|
99
99
|
});
|
|
100
|
-
/**
|
|
101
|
-
const
|
|
102
|
-
if (binding.hole === void 0) return binding.message;
|
|
103
|
-
const fill = (template) => {
|
|
104
|
-
if (binding.event === "input" || binding.event === "change") {
|
|
105
|
-
const [field] = binding.hole ?? [];
|
|
106
|
-
return {
|
|
107
|
-
...template,
|
|
108
|
-
[field ?? "value"]: inputValue(event.target)
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
if (event instanceof KeyboardEvent) return {
|
|
112
|
-
...template,
|
|
113
|
-
key: event.key,
|
|
114
|
-
modifiers: modifiersOf(event)
|
|
115
|
-
};
|
|
116
|
-
return template;
|
|
117
|
-
};
|
|
100
|
+
/** A binding's Message with its hole filled by `fill`, inside each placement wrapper's `message`. */
|
|
101
|
+
const filled = (binding, fill) => {
|
|
118
102
|
const inside = (template, depth) => depth === 0 ? fill(template) : {
|
|
119
103
|
...template,
|
|
120
104
|
message: inside(template.message, depth - 1)
|
|
121
105
|
};
|
|
122
106
|
return inside(binding.message, binding.depth ?? 0);
|
|
123
107
|
};
|
|
108
|
+
/** The Message an `OnInput` or `OnChange` hole binding dispatches for a field holding `value`. */
|
|
109
|
+
const typedMessage = (binding, value) => filled(binding, (template) => {
|
|
110
|
+
const [field] = binding.hole ?? [];
|
|
111
|
+
return {
|
|
112
|
+
...template,
|
|
113
|
+
[field ?? "value"]: value
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
/** The Message a binding dispatches for this event: its own, with the hole filled. */
|
|
117
|
+
const messageFor = (binding, event) => {
|
|
118
|
+
if (binding.hole === void 0) return binding.message;
|
|
119
|
+
if (binding.event === "input" || binding.event === "change") return typedMessage(binding, inputValue(event.target));
|
|
120
|
+
return filled(binding, (template) => event instanceof KeyboardEvent ? {
|
|
121
|
+
...template,
|
|
122
|
+
key: event.key,
|
|
123
|
+
modifiers: modifiersOf(event)
|
|
124
|
+
} : template);
|
|
125
|
+
};
|
|
124
126
|
/**
|
|
125
127
|
* Listens at `root` for every event the bindings name, and answers each from
|
|
126
128
|
* the markers. Returns what removes the listeners.
|
|
127
129
|
*/
|
|
128
130
|
const listen = (root, options) => {
|
|
131
|
+
const markers = /* @__PURE__ */ new Map();
|
|
132
|
+
for (const element of [root, ...Array.from(root.querySelectorAll("*"))]) for (const name of element.getAttributeNames()) {
|
|
133
|
+
if (!name.startsWith("data-foldkit-plus-on-")) continue;
|
|
134
|
+
const held = markers.get(element) ?? /* @__PURE__ */ new Map();
|
|
135
|
+
held.set(name.slice(BINDING_ATTRIBUTE.length), element.getAttribute(name) ?? "");
|
|
136
|
+
markers.set(element, held);
|
|
137
|
+
}
|
|
129
138
|
const events = new Set(options.bindings.map((binding) => binding.event));
|
|
130
139
|
if (options.events !== void 0) for (const event of options.events) events.add(event);
|
|
131
|
-
else for (const
|
|
140
|
+
else for (const held of markers.values()) for (const event of held.keys()) events.add(event);
|
|
132
141
|
const handler = (event) => {
|
|
133
142
|
const type = event.type;
|
|
134
|
-
const attribute = `${BINDING_ATTRIBUTE}${type}`;
|
|
135
143
|
const messages = [];
|
|
136
144
|
const path = event.bubbles ? event.composedPath() : [event.target];
|
|
137
145
|
for (const node of path) {
|
|
138
146
|
if (!(node instanceof Element)) continue;
|
|
139
|
-
const tokens =
|
|
147
|
+
const tokens = markers.get(node)?.get(type);
|
|
140
148
|
let stopped = false;
|
|
141
|
-
if (tokens !==
|
|
149
|
+
if (tokens !== void 0) for (const token of tokens.split(" ")) {
|
|
142
150
|
if (token === "*") {
|
|
143
151
|
options.onAnswer({
|
|
144
152
|
event,
|
|
@@ -174,6 +182,6 @@ const listen = (root, options) => {
|
|
|
174
182
|
for (const type of events) root.removeEventListener(type, handler, true);
|
|
175
183
|
};
|
|
176
184
|
};
|
|
177
|
-
export { decodeBindings, listen, readBindings };
|
|
185
|
+
export { decodeBindings, listen, readBindings, typedMessage };
|
|
178
186
|
|
|
179
187
|
//# sourceMappingURL=listen.js.map
|
package/dist/listen.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listen.js","names":[],"sources":["../src/listen.ts"],"sourcesContent":["/**\n * Delegated dispatch: the page answers an event from its markers, before the\n * application has booted.\n *\n * One capture-phase listener per event type sits at the root, so events that\n * do not bubble (`focus`, `blur`) are caught, and it runs before anything\n * Foldkit attaches later. On an event it walks from the target to the root\n * and, for every marker of that event on the way, collects each binding's\n * Message in order, the way Foldkit's own handler would dispatch it: a hole is\n * filled from the event, `OnClick`'s options are honoured, `OnSubmit`\n * prevents the default. A marker token the page could not name (`*`) means\n * the live page does something here the page cannot describe, so the walk\n * ends and the answer names that element.\n */\nimport { Predicate, Result, Schema } from 'effect'\nimport type { KeyboardModifiers } from 'foldkit/html'\nimport { BINDING_ATTRIBUTE, EVENT_OF, UNNAMED_HANDLER } from './resumable.js'\n\n/**\n * A binding as the envelope carries it: Foldkit's attribute (`OnSubmit`,\n * `OnBlur`), whose tag says what its handler does beside dispatching; the\n * Message, encoded through the application's Message Schema (for a hole, with\n * the hole filled by a placeholder); the fields the event fills, `depth`\n * placement wrappers down; and the attribute's options. Its ordinal is its\n * index.\n */\nexport interface EncodedBinding {\n readonly attribute: string\n readonly message: unknown\n readonly hole?: ReadonlyArray<string>\n readonly depth?: number\n readonly options?: unknown\n}\n\nconst isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\n/**\n * The page's list of bindings, read as untrusted input: each entry checked,\n * and only its known keys kept, or the reason the list is refused. Checked by\n * hand rather than through a Schema, which at a thousand rows took most of a\n * frame for a shape this plain; the Message inside each entry is still decoded\n * through the application's Schema by `decodeBindings`.\n */\nexport const readBindings = (\n value: unknown,\n): Result.Result<ReadonlyArray<EncodedBinding>, string> => {\n if (!Array.isArray(value)) return Result.fail('the list is not an array')\n const out: Array<EncodedBinding> = []\n for (const [index, entry] of value.entries()) {\n if (!isRecord(entry)) return Result.fail(`entry ${index} is not an object`)\n if (typeof entry.attribute !== 'string') {\n return Result.fail(`entry ${index} has no attribute`)\n }\n if (!Object.hasOwn(entry, 'message')) return Result.fail(`entry ${index} has no message`)\n const { hole, depth } = entry\n if (\n hole !== undefined &&\n !(Array.isArray(hole) && hole.every(field => typeof field === 'string'))\n ) {\n return Result.fail(`entry ${index} has a hole that is not a list of fields`)\n }\n if (depth !== undefined && !(Number.isInteger(depth) && (depth as number) >= 0)) {\n return Result.fail(`entry ${index} has a depth that is not a count`)\n }\n out.push({\n attribute: entry.attribute,\n message: entry.message,\n ...(hole === undefined ? {} : { hole: hole as ReadonlyArray<string> }),\n ...(depth === undefined ? {} : { depth: depth as number }),\n ...(Object.hasOwn(entry, 'options') ? { options: entry.options } : {}),\n })\n }\n return Result.succeed(out)\n}\n\n/** A binding decoded from the page: its Message is one of the application's. */\nexport interface DecodedBinding {\n readonly attribute: string\n readonly event: string\n readonly message: unknown\n readonly hole?: ReadonlyArray<string> | undefined\n readonly depth?: number | undefined\n readonly options?: unknown\n}\n\n/**\n * The page's bindings, decoded through the application's Message Schema, and\n * every marker in `root` checked against them. A page whose markers name a\n * binding it does not carry, or whose entry is not one of the application's\n * Messages, is refused whole, with the reason.\n */\nexport const decodeBindings = (\n Message: Schema.Top | undefined,\n encoded: ReadonlyArray<EncodedBinding>,\n root: ParentNode,\n allowed: ReadonlySet<string>,\n): Result.Result<ReadonlyArray<DecodedBinding>, string> => {\n if (encoded.length > 0 && Message === undefined) {\n return Result.fail(\n 'the page has bindings and the plan has no Message Schema to decode them with',\n )\n }\n const decode =\n Message === undefined\n ? undefined\n : Schema.decodeUnknownResult(Message as Schema.Codec<unknown, unknown>)\n const bindings: Array<DecodedBinding> = []\n for (const [index, entry] of encoded.entries()) {\n const event = EVENT_OF[entry.attribute]\n if (event === undefined) {\n return Result.fail(\n `binding ${index} is for \"${entry.attribute}\", which is no event attribute`,\n )\n }\n const message = decode!(entry.message)\n if (Result.isFailure(message)) {\n return Result.fail(\n `binding ${index} does not decode as a Message: ${message.failure.message}`,\n )\n }\n const tag = Predicate.hasProperty(message.success, '_tag') ? message.success._tag : undefined\n if (typeof tag !== 'string' || !allowed.has(tag)) {\n return Result.fail(\n `binding ${index} dispatches ${typeof tag === 'string' ? tag : 'an untagged Message'}, which no active Surface lists in its messages`,\n )\n }\n bindings.push({\n attribute: entry.attribute,\n event,\n message: message.success,\n ...(entry.hole === undefined ? {} : { hole: entry.hole }),\n ...(entry.depth === undefined ? {} : { depth: entry.depth }),\n ...(entry.options === undefined ? {} : { options: entry.options }),\n })\n }\n for (const element of Array.from(root.querySelectorAll('*'))) {\n for (const attribute of element.getAttributeNames()) {\n if (!attribute.startsWith(BINDING_ATTRIBUTE)) continue\n for (const token of (element.getAttribute(attribute) ?? '').split(' ')) {\n if (token === UNNAMED_HANDLER) continue\n const ordinal = Number(token)\n if (!Number.isInteger(ordinal) || bindings[ordinal] === undefined) {\n return Result.fail(`the marker ${attribute}=\"${token}\" names no binding the page carries`)\n }\n }\n }\n }\n return Result.succeed(bindings)\n}\n\n/** A string property of the target, if it has one. */\nconst stringProperty = (target: EventTarget | null, name: string): string | undefined => {\n if (target === null || !(name in target)) return undefined\n const value: unknown = Reflect.get(target, name)\n return typeof value === 'string' ? value : undefined\n}\n\n/**\n * Foldkit's reading of an input's value (`inputEventValue`): any target's\n * string `value`, a custom element's included, else its `innerText`, else its\n * `textContent`.\n */\nconst inputValue = (target: EventTarget | null): string =>\n stringProperty(target, 'value') ??\n stringProperty(target, 'innerText') ??\n stringProperty(target, 'textContent') ??\n ''\n\nconst modifiersOf = (event: KeyboardEvent): KeyboardModifiers => ({\n shiftKey: event.shiftKey,\n ctrlKey: event.ctrlKey,\n altKey: event.altKey,\n metaKey: event.metaKey,\n})\n\n/** The Message a binding dispatches for this event: its own, with the hole filled. */\nconst messageFor = (binding: DecodedBinding, event: Event): unknown => {\n if (binding.hole === undefined) return binding.message\n const fill = (template: Readonly<Record<string, unknown>>): unknown => {\n if (binding.event === 'input' || binding.event === 'change') {\n const [field] = binding.hole ?? []\n return { ...template, [field ?? 'value']: inputValue(event.target) }\n }\n if (event instanceof KeyboardEvent) {\n return { ...template, key: event.key, modifiers: modifiersOf(event) }\n }\n return template\n }\n // The hole's fields sit inside each placement wrapper's `message`.\n const inside = (template: Readonly<Record<string, unknown>>, depth: number): unknown =>\n depth === 0\n ? fill(template)\n : {\n ...template,\n message: inside(template.message as Readonly<Record<string, unknown>>, depth - 1),\n }\n return inside(binding.message as Readonly<Record<string, unknown>>, binding.depth ?? 0)\n}\n\n/** What `OnClick` declares beside its Message. */\ninterface ClickOptions {\n readonly defaultAction?: 'Allow' | 'Prevent'\n readonly propagation?: 'Bubble' | 'Stop'\n readonly focusSelector?: string\n}\n\n/**\n * What the markers make of one event: the Messages its bindings dispatch, in\n * the order the live page would, and, when the walk met a handler the page\n * could not name, that element. An answer with `unnamed` is incomplete: the\n * live page would do more than its Messages say.\n */\nexport interface Answer {\n readonly event: Event\n readonly messages: ReadonlyArray<unknown>\n readonly unnamed?: Element | undefined\n}\n\nexport interface ListenOptions {\n readonly bindings: ReadonlyArray<DecodedBinding>\n readonly onAnswer: (answer: Answer) => void\n /**\n * Every event the page's markers name, as the envelope lists them. Given,\n * `listen` needs no pass over the page to find the events a marker names\n * with `*` alone; without it, it reads every element's attributes, which on\n * a large page is most of its cost.\n */\n readonly events?: Iterable<string> | undefined\n}\n\n/**\n * Listens at `root` for every event the bindings name, and answers each from\n * the markers. Returns what removes the listeners.\n */\nexport const listen = (root: Element, options: ListenOptions): (() => void) => {\n // The bindings name most events; a marker whose handlers the page could not\n // name is `*` alone, so the markers themselves say the rest.\n const events = new Set(options.bindings.map(binding => binding.event))\n if (options.events !== undefined) {\n for (const event of options.events) events.add(event)\n } else {\n for (const element of [root, ...Array.from(root.querySelectorAll('*'))]) {\n for (const { name } of Array.from(element.attributes)) {\n if (name.startsWith(BINDING_ATTRIBUTE)) events.add(name.slice(BINDING_ATTRIBUTE.length))\n }\n }\n }\n const handler = (event: Event) => {\n const type = event.type\n const attribute = `${BINDING_ATTRIBUTE}${type}`\n const messages: Array<unknown> = []\n // An event that does not bubble (`focus`, `blur`, `mouseenter`) reaches only\n // its target's own handlers on the live page, so only those answer it here.\n const path = event.bubbles ? event.composedPath() : [event.target]\n for (const node of path) {\n if (!(node instanceof Element)) continue\n const tokens = node.getAttribute(attribute)\n // Foldkit chains an element's handlers in one listener, so a Stop among\n // them keeps the event from other elements, not from its siblings.\n let stopped = false\n if (tokens !== null) {\n for (const token of tokens.split(' ')) {\n if (token === UNNAMED_HANDLER) {\n options.onAnswer({ event, messages, unnamed: node })\n return\n }\n const binding = options.bindings[Number(token)]\n if (binding === undefined) continue\n if (binding.attribute === 'OnSubmit') event.preventDefault()\n if (binding.attribute === 'OnClick') {\n const click = (binding.options ?? {}) as ClickOptions\n if (click.defaultAction === 'Prevent') event.preventDefault()\n if (click.propagation === 'Stop') event.stopPropagation()\n if (click.focusSelector !== undefined) {\n const focusTarget = document.querySelector(click.focusSelector)\n if (focusTarget instanceof HTMLElement) focusTarget.focus()\n }\n if (click.propagation === 'Stop') stopped = true\n }\n messages.push(messageFor(binding, event))\n }\n }\n if (stopped || node === root) break\n }\n options.onAnswer({ event, messages })\n }\n for (const type of events) root.addEventListener(type, handler, true)\n return () => {\n for (const type of events) root.removeEventListener(type, handler, true)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkCA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;;;;;;;AASrE,MAAa,gBACX,UACyD;CACzD,IAAI,CAAC,MAAM,QAAQ,KAAK,GAAG,OAAO,OAAO,KAAK,0BAA0B;CACxE,MAAM,MAA6B,CAAC;CACpC,KAAK,MAAM,CAAC,OAAO,UAAU,MAAM,QAAQ,GAAG;EAC5C,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO,OAAO,KAAK,SAAS,MAAM,kBAAkB;EAC1E,IAAI,OAAO,MAAM,cAAc,UAC7B,OAAO,OAAO,KAAK,SAAS,MAAM,kBAAkB;EAEtD,IAAI,CAAC,OAAO,OAAO,OAAO,SAAS,GAAG,OAAO,OAAO,KAAK,SAAS,MAAM,gBAAgB;EACxF,MAAM,EAAE,MAAM,UAAU;EACxB,IACE,SAAS,KAAA,KACT,EAAE,MAAM,QAAQ,IAAI,KAAK,KAAK,OAAM,UAAS,OAAO,UAAU,QAAQ,IAEtE,OAAO,OAAO,KAAK,SAAS,MAAM,yCAAyC;EAE7E,IAAI,UAAU,KAAA,KAAa,EAAE,OAAO,UAAU,KAAK,KAAM,SAAoB,IAC3E,OAAO,OAAO,KAAK,SAAS,MAAM,iCAAiC;EAErE,IAAI,KAAK;GACP,WAAW,MAAM;GACjB,SAAS,MAAM;GACf,GAAI,SAAS,KAAA,IAAY,CAAC,IAAI,EAAQ,KAA8B;GACpE,GAAI,UAAU,KAAA,IAAY,CAAC,IAAI,EAAS,MAAgB;GACxD,GAAI,OAAO,OAAO,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AAI,MAAM,gBAAgB,QAAQ,MAAM,gBAAgB;KACxD,IAAI,MAAM,kBAAkB,KAAA,GAAW;MACrC,MAAM,cAAc,SAAS,cAAc,MAAM,aAAa;MAC9D,IAAI,uBAAuB,aAAa,YAAY,MAAM;KAC5D;KACA,IAAI,MAAM,gBAAgB,QAAQ,UAAU;IAC9C;IACA,SAAS,KAAK,WAAW,SAAS,KAAK,CAAC;GAC1C;GAEF,IAAI,WAAW,SAAS,MAAM;EAChC;EACA,QAAQ,SAAS;GAAE;GAAO;EAAS,CAAC;CACtC;CACA,KAAK,MAAM,QAAQ,QAAQ,KAAK,iBAAiB,MAAM,SAAS,IAAI;CACpE,aAAa;EACX,KAAK,MAAM,QAAQ,QAAQ,KAAK,oBAAoB,MAAM,SAAS,IAAI;CACzE;AACF"}
|
|
1
|
+
{"version":3,"file":"listen.js","names":[],"sources":["../src/listen.ts"],"sourcesContent":["/**\n * Delegated dispatch: the page answers an event from its markers, before the\n * application has booted.\n *\n * One capture-phase listener per event type sits at the root, so events that\n * do not bubble (`focus`, `blur`) are caught, and it runs before anything\n * Foldkit attaches later. On an event it walks from the target to the root\n * and, for every marker of that event on the way, collects each binding's\n * Message in order, the way Foldkit's own handler would dispatch it: a hole is\n * filled from the event, `OnClick`'s options are honoured, `OnSubmit`\n * prevents the default. A marker token the page could not name (`*`) means\n * the live page does something here the page cannot describe, so the walk\n * ends and the answer names that element.\n */\nimport { Predicate, Result, Schema } from 'effect'\nimport type { KeyboardModifiers } from 'foldkit/html'\nimport { BINDING_ATTRIBUTE, EVENT_OF, UNNAMED_HANDLER } from './resumable.js'\n\n/**\n * A binding as the envelope carries it: Foldkit's attribute (`OnSubmit`,\n * `OnBlur`), whose tag says what its handler does beside dispatching; the\n * Message, encoded through the application's Message Schema (for a hole, with\n * the hole filled by a placeholder); the fields the event fills, `depth`\n * placement wrappers down; and the attribute's options. Its ordinal is its\n * index.\n */\nexport interface EncodedBinding {\n readonly attribute: string\n readonly message: unknown\n readonly hole?: ReadonlyArray<string>\n readonly depth?: number\n readonly options?: unknown\n}\n\nconst isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\n/**\n * The page's list of bindings, read as untrusted input: each entry checked,\n * and only its known keys kept, or the reason the list is refused. Checked by\n * hand rather than through a Schema, which at a thousand rows took most of a\n * frame for a shape this plain; the Message inside each entry is still decoded\n * through the application's Schema by `decodeBindings`.\n */\nexport const readBindings = (\n value: unknown,\n): Result.Result<ReadonlyArray<EncodedBinding>, string> => {\n if (!Array.isArray(value)) return Result.fail('the list is not an array')\n const out: Array<EncodedBinding> = []\n for (const [index, entry] of value.entries()) {\n if (!isRecord(entry)) return Result.fail(`entry ${index} is not an object`)\n if (typeof entry.attribute !== 'string') {\n return Result.fail(`entry ${index} has no attribute`)\n }\n if (!Object.hasOwn(entry, 'message')) return Result.fail(`entry ${index} has no message`)\n const { hole, depth } = entry\n if (\n hole !== undefined &&\n !(Array.isArray(hole) && hole.every(field => typeof field === 'string'))\n ) {\n return Result.fail(`entry ${index} has a hole that is not a list of fields`)\n }\n if (depth !== undefined && !(Number.isInteger(depth) && (depth as number) >= 0)) {\n return Result.fail(`entry ${index} has a depth that is not a count`)\n }\n out.push({\n attribute: entry.attribute,\n message: entry.message,\n ...(hole === undefined ? {} : { hole: hole as ReadonlyArray<string> }),\n ...(depth === undefined ? {} : { depth: depth as number }),\n ...(Object.hasOwn(entry, 'options') ? { options: entry.options } : {}),\n })\n }\n return Result.succeed(out)\n}\n\n/** A binding decoded from the page: its Message is one of the application's. */\nexport interface DecodedBinding {\n readonly attribute: string\n readonly event: string\n readonly message: unknown\n readonly hole?: ReadonlyArray<string> | undefined\n readonly depth?: number | undefined\n readonly options?: unknown\n}\n\n/**\n * The page's bindings, decoded through the application's Message Schema, and\n * every marker in `root` checked against them. A page whose markers name a\n * binding it does not carry, or whose entry is not one of the application's\n * Messages, is refused whole, with the reason.\n */\nexport const decodeBindings = (\n Message: Schema.Top | undefined,\n encoded: ReadonlyArray<EncodedBinding>,\n root: ParentNode,\n allowed: ReadonlySet<string>,\n): Result.Result<ReadonlyArray<DecodedBinding>, string> => {\n if (encoded.length > 0 && Message === undefined) {\n return Result.fail(\n 'the page has bindings and the plan has no Message Schema to decode them with',\n )\n }\n const decode =\n Message === undefined\n ? undefined\n : Schema.decodeUnknownResult(Message as Schema.Codec<unknown, unknown>)\n const bindings: Array<DecodedBinding> = []\n for (const [index, entry] of encoded.entries()) {\n const event = EVENT_OF[entry.attribute]\n if (event === undefined) {\n return Result.fail(\n `binding ${index} is for \"${entry.attribute}\", which is no event attribute`,\n )\n }\n const message = decode!(entry.message)\n if (Result.isFailure(message)) {\n return Result.fail(\n `binding ${index} does not decode as a Message: ${message.failure.message}`,\n )\n }\n const tag = Predicate.hasProperty(message.success, '_tag') ? message.success._tag : undefined\n if (typeof tag !== 'string' || !allowed.has(tag)) {\n return Result.fail(\n `binding ${index} dispatches ${typeof tag === 'string' ? tag : 'an untagged Message'}, which no active Surface lists in its messages`,\n )\n }\n bindings.push({\n attribute: entry.attribute,\n event,\n message: message.success,\n ...(entry.hole === undefined ? {} : { hole: entry.hole }),\n ...(entry.depth === undefined ? {} : { depth: entry.depth }),\n ...(entry.options === undefined ? {} : { options: entry.options }),\n })\n }\n for (const element of Array.from(root.querySelectorAll('*'))) {\n for (const attribute of element.getAttributeNames()) {\n if (!attribute.startsWith(BINDING_ATTRIBUTE)) continue\n for (const token of (element.getAttribute(attribute) ?? '').split(' ')) {\n if (token === UNNAMED_HANDLER) continue\n const ordinal = Number(token)\n if (!Number.isInteger(ordinal) || bindings[ordinal] === undefined) {\n return Result.fail(`the marker ${attribute}=\"${token}\" names no binding the page carries`)\n }\n }\n }\n }\n return Result.succeed(bindings)\n}\n\n/** A string property of the target, if it has one. */\nconst stringProperty = (target: EventTarget | null, name: string): string | undefined => {\n if (target === null || !(name in target)) return undefined\n const value: unknown = Reflect.get(target, name)\n return typeof value === 'string' ? value : undefined\n}\n\n/**\n * Foldkit's reading of an input's value (`inputEventValue`): any target's\n * string `value`, a custom element's included, else its `innerText`, else its\n * `textContent`.\n */\nconst inputValue = (target: EventTarget | null): string =>\n stringProperty(target, 'value') ??\n stringProperty(target, 'innerText') ??\n stringProperty(target, 'textContent') ??\n ''\n\nconst modifiersOf = (event: KeyboardEvent): KeyboardModifiers => ({\n shiftKey: event.shiftKey,\n ctrlKey: event.ctrlKey,\n altKey: event.altKey,\n metaKey: event.metaKey,\n})\n\n/** A binding's Message with its hole filled by `fill`, inside each placement wrapper's `message`. */\nconst filled = (\n binding: DecodedBinding,\n fill: (template: Readonly<Record<string, unknown>>) => unknown,\n): unknown => {\n const inside = (template: Readonly<Record<string, unknown>>, depth: number): unknown =>\n depth === 0\n ? fill(template)\n : {\n ...template,\n message: inside(template.message as Readonly<Record<string, unknown>>, depth - 1),\n }\n return inside(binding.message as Readonly<Record<string, unknown>>, binding.depth ?? 0)\n}\n\n/** The Message an `OnInput` or `OnChange` hole binding dispatches for a field holding `value`. */\nexport const typedMessage = (binding: DecodedBinding, value: string): unknown =>\n filled(binding, template => {\n const [field] = binding.hole ?? []\n return { ...template, [field ?? 'value']: value }\n })\n\n/** The Message a binding dispatches for this event: its own, with the hole filled. */\nconst messageFor = (binding: DecodedBinding, event: Event): unknown => {\n if (binding.hole === undefined) return binding.message\n if (binding.event === 'input' || binding.event === 'change') {\n return typedMessage(binding, inputValue(event.target))\n }\n return filled(binding, template =>\n event instanceof KeyboardEvent\n ? { ...template, key: event.key, modifiers: modifiersOf(event) }\n : template,\n )\n}\n\n/** What `OnClick` declares beside its Message. */\ninterface ClickOptions {\n readonly defaultAction?: 'Allow' | 'Prevent'\n readonly propagation?: 'Bubble' | 'Stop'\n readonly focusSelector?: string\n}\n\n/**\n * What the markers make of one event: the Messages its bindings dispatch, in\n * the order the live page would, and, when the walk met a handler the page\n * could not name, that element. An answer with `unnamed` is incomplete: the\n * live page would do more than its Messages say.\n */\nexport interface Answer {\n readonly event: Event\n readonly messages: ReadonlyArray<unknown>\n readonly unnamed?: Element | undefined\n}\n\nexport interface ListenOptions {\n readonly bindings: ReadonlyArray<DecodedBinding>\n readonly onAnswer: (answer: Answer) => void\n /**\n * Every event the page's markers name, as the envelope lists them. Given,\n * `listen` needs no pass over the page to find the events a marker names\n * with `*` alone; without it, it reads every element's attributes, which on\n * a large page is most of its cost.\n */\n readonly events?: Iterable<string> | undefined\n}\n\n/**\n * Listens at `root` for every event the bindings name, and answers each from\n * the markers. Returns what removes the listeners.\n */\nexport const listen = (root: Element, options: ListenOptions): (() => void) => {\n // The markers as served, read once: the boot takes them off the page before\n // Foldkit adopts it, and an event landing before the first render is still\n // answered from them.\n const markers = new Map<Element, Map<string, string>>()\n for (const element of [root, ...Array.from(root.querySelectorAll('*'))]) {\n for (const name of element.getAttributeNames()) {\n if (!name.startsWith(BINDING_ATTRIBUTE)) continue\n const held = markers.get(element) ?? new Map<string, string>()\n held.set(name.slice(BINDING_ATTRIBUTE.length), element.getAttribute(name) ?? '')\n markers.set(element, held)\n }\n }\n // The bindings name most events; a marker whose handlers the page could not\n // name is `*` alone, so the markers themselves say the rest.\n const events = new Set(options.bindings.map(binding => binding.event))\n if (options.events !== undefined) {\n for (const event of options.events) events.add(event)\n } else {\n for (const held of markers.values()) for (const event of held.keys()) events.add(event)\n }\n const handler = (event: Event) => {\n const type = event.type\n const messages: Array<unknown> = []\n // An event that does not bubble (`focus`, `blur`, `mouseenter`) reaches only\n // its target's own handlers on the live page, so only those answer it here.\n const path = event.bubbles ? event.composedPath() : [event.target]\n for (const node of path) {\n if (!(node instanceof Element)) continue\n const tokens = markers.get(node)?.get(type)\n // Foldkit chains an element's handlers in one listener, so a Stop among\n // them keeps the event from other elements, not from its siblings.\n let stopped = false\n if (tokens !== undefined) {\n for (const token of tokens.split(' ')) {\n if (token === UNNAMED_HANDLER) {\n options.onAnswer({ event, messages, unnamed: node })\n return\n }\n const binding = options.bindings[Number(token)]\n if (binding === undefined) continue\n if (binding.attribute === 'OnSubmit') event.preventDefault()\n if (binding.attribute === 'OnClick') {\n const click = (binding.options ?? {}) as ClickOptions\n if (click.defaultAction === 'Prevent') event.preventDefault()\n if (click.propagation === 'Stop') event.stopPropagation()\n if (click.focusSelector !== undefined) {\n const focusTarget = document.querySelector(click.focusSelector)\n if (focusTarget instanceof HTMLElement) focusTarget.focus()\n }\n if (click.propagation === 'Stop') stopped = true\n }\n messages.push(messageFor(binding, event))\n }\n }\n if (stopped || node === root) break\n }\n options.onAnswer({ event, messages })\n }\n for (const type of events) root.addEventListener(type, handler, true)\n return () => {\n for (const type of events) root.removeEventListener(type, handler, true)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkCA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;;;;;;;AASrE,MAAa,gBACX,UACyD;CACzD,IAAI,CAAC,MAAM,QAAQ,KAAK,GAAG,OAAO,OAAO,KAAK,0BAA0B;CACxE,MAAM,MAA6B,CAAC;CACpC,KAAK,MAAM,CAAC,OAAO,UAAU,MAAM,QAAQ,GAAG;EAC5C,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO,OAAO,KAAK,SAAS,MAAM,kBAAkB;EAC1E,IAAI,OAAO,MAAM,cAAc,UAC7B,OAAO,OAAO,KAAK,SAAS,MAAM,kBAAkB;EAEtD,IAAI,CAAC,OAAO,OAAO,OAAO,SAAS,GAAG,OAAO,OAAO,KAAK,SAAS,MAAM,gBAAgB;EACxF,MAAM,EAAE,MAAM,UAAU;EACxB,IACE,SAAS,KAAA,KACT,EAAE,MAAM,QAAQ,IAAI,KAAK,KAAK,OAAM,UAAS,OAAO,UAAU,QAAQ,IAEtE,OAAO,OAAO,KAAK,SAAS,MAAM,yCAAyC;EAE7E,IAAI,UAAU,KAAA,KAAa,EAAE,OAAO,UAAU,KAAK,KAAM,SAAoB,IAC3E,OAAO,OAAO,KAAK,SAAS,MAAM,iCAAiC;EAErE,IAAI,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K,MAAM,QAAQ,MAAM;GACvB,IAAI,EAAE,gBAAgB,UAAU;GAChC,MAAM,SAAS,QAAQ,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI;GAG1C,IAAI,UAAU;GACd,IAAI,WAAW,KAAA,GACb,KAAK,MAAM,SAAS,OAAO,MAAM,GAAG,GAAG;IACrC,IAAI,UAAA,KAA2B;KAC7B,QAAQ,SAAS;MAAE;MAAO;MAAU,SAAS;KAAK,CAAC;KACnD;IACF;IACA,MAAM,UAAU,QAAQ,SAAS,OAAO,KAAK;IAC7C,IAAI,YAAY,KAAA,GAAW;IAC3B,IAAI,QAAQ,cAAc,YAAY,MAAM,eAAe;IAC3D,IAAI,QAAQ,cAAc,WAAW;KACnC,MAAM,QAAS,QAAQ,WAAW,CAAC;KACnC,IAAI,MAAM,kBAAkB,WAAW,MAAM,eAAe;KAC5D,IAAI,MAAM,gBAAgB,QAAQ,MAAM,gBAAgB;KACxD,IAAI,MAAM,kBAAkB,KAAA,GAAW;MACrC,MAAM,cAAc,SAAS,cAAc,MAAM,aAAa;MAC9D,IAAI,uBAAuB,aAAa,YAAY,MAAM;KAC5D;KACA,IAAI,MAAM,gBAAgB,QAAQ,UAAU;IAC9C;IACA,SAAS,KAAK,WAAW,SAAS,KAAK,CAAC;GAC1C;GAEF,IAAI,WAAW,SAAS,MAAM;EAChC;EACA,QAAQ,SAAS;GAAE;GAAO;EAAS,CAAC;CACtC;CACA,KAAK,MAAM,QAAQ,QAAQ,KAAK,iBAAiB,MAAM,SAAS,IAAI;CACpE,aAAa;EACX,KAAK,MAAM,QAAQ,QAAQ,KAAK,oBAAoB,MAAM,SAAS,IAAI;CACzE;AACF"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Effect, Option, Schema } from "effect";
|
|
2
|
+
import { RenderedApplication } from "foldkit/experimental/server";
|
|
3
|
+
/** What a render read of its request's headers: names, or every one. */
|
|
4
|
+
interface HeadersRead {
|
|
5
|
+
readonly names: Set<string>;
|
|
6
|
+
all: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* `request` as the render sees it, every header it reads recorded in `read`.
|
|
10
|
+
* A lookup by name records that name; enumerating the headers records all.
|
|
11
|
+
* Everything else is the request's own, so its getters keep their receiver.
|
|
12
|
+
* A request context read of it (`SSR.requestContext`) is the host's request's,
|
|
13
|
+
* and records all: what a host attaches may say who is asking.
|
|
14
|
+
*/
|
|
15
|
+
declare const tracked: (request: Request, read: HeadersRead) => Request;
|
|
16
|
+
/** Whether a page drawn from `read` serves `request`: it carries none of the headers read. */
|
|
17
|
+
declare const fits: (request: Request, vary: ReadonlyArray<string>) => boolean;
|
|
18
|
+
/** A kept page: what the host draws, its status and headers, and the headers it must lack. */
|
|
19
|
+
declare const KeptPage: Schema.Struct<{
|
|
20
|
+
readonly application: Schema.Struct<{
|
|
21
|
+
readonly html: Schema.String;
|
|
22
|
+
readonly title: Schema.String;
|
|
23
|
+
readonly lang: Schema.optionalKey<Schema.String>;
|
|
24
|
+
readonly dir: Schema.optionalKey<Schema.Literals<readonly ["ltr", "rtl", "auto"]>>;
|
|
25
|
+
readonly canonical: Schema.optionalKey<Schema.String>;
|
|
26
|
+
readonly ogUrl: Schema.optionalKey<Schema.String>;
|
|
27
|
+
}>;
|
|
28
|
+
readonly status: Schema.optionalKey<Schema.Number>;
|
|
29
|
+
readonly headers: Schema.$Array<Schema.Tuple<readonly [Schema.String, Schema.String]>>;
|
|
30
|
+
readonly vary: Schema.$Array<Schema.String>;
|
|
31
|
+
}>;
|
|
32
|
+
type KeptPage = typeof KeptPage.Type;
|
|
33
|
+
declare const decodeKept: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => Option.Option<{
|
|
34
|
+
readonly application: {
|
|
35
|
+
readonly html: string;
|
|
36
|
+
readonly title: string;
|
|
37
|
+
readonly lang?: string;
|
|
38
|
+
readonly dir?: "auto" | "ltr" | "rtl";
|
|
39
|
+
readonly canonical?: string;
|
|
40
|
+
readonly ogUrl?: string;
|
|
41
|
+
};
|
|
42
|
+
readonly status?: number;
|
|
43
|
+
readonly headers: readonly (readonly [string, string])[];
|
|
44
|
+
readonly vary: readonly string[];
|
|
45
|
+
}>;
|
|
46
|
+
/** Whether two renders drew the same page: what the host draws, and its status. */
|
|
47
|
+
declare const samePage: (a: {
|
|
48
|
+
readonly application: RenderedApplication;
|
|
49
|
+
readonly status?: number | undefined;
|
|
50
|
+
}, b: {
|
|
51
|
+
readonly application: RenderedApplication;
|
|
52
|
+
readonly status?: number | undefined;
|
|
53
|
+
}, now: number) => boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Where a page is kept: by its build and its full URL, under the request's
|
|
56
|
+
* origin, since the Workers Cache API keys by URL. A deploy's pages never
|
|
57
|
+
* meet another build's.
|
|
58
|
+
*/
|
|
59
|
+
declare const pageKey: (buildId: string, url: string) => Effect.Effect<string>;
|
|
60
|
+
/** The value a `Rendered` result's `headers` gives, none when absent. */
|
|
61
|
+
declare const headerOf: (headers: HeadersInit | undefined, name: string) => Option.Option<string>;
|
|
62
|
+
/** The pages `SSR.entry` serves from its cache, by the object it serves. */
|
|
63
|
+
declare const keptApplications: WeakSet<Readonly<{
|
|
64
|
+
html: string;
|
|
65
|
+
title: string;
|
|
66
|
+
lang?: string;
|
|
67
|
+
dir?: 'ltr' | 'rtl' | 'auto';
|
|
68
|
+
canonical?: string;
|
|
69
|
+
ogUrl?: string;
|
|
70
|
+
}>>;
|
|
71
|
+
/**
|
|
72
|
+
* `render`, remembering the document it drew for each kept page (one
|
|
73
|
+
* `SSR.entry` serves from its `cache`), by the page and the assets: the
|
|
74
|
+
* host draws a page into its document on every request, which parses the
|
|
75
|
+
* page again (`injectIntoTemplate`), so a kept page would cost that much
|
|
76
|
+
* every time it is served. Any other page is drawn as `render` draws it.
|
|
77
|
+
*/
|
|
78
|
+
declare const keptDocuments: <Assets>(render: (application: RenderedApplication, assets: Assets) => string) => (application: RenderedApplication, assets: Assets) => string;
|
|
79
|
+
export { headerOf as a, pageKey as c, fits as i, samePage as l, KeptPage as n, keptApplications as o, decodeKept as r, keptDocuments as s, HeadersRead as t, tracked as u };
|
|
80
|
+
//# sourceMappingURL=pageCache-DO0pOgnc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pageCache-DO0pOgnc.d.ts","names":[],"sources":["../src/pageCache.ts"],"mappings":";;;UAYiB;WACN,OAAO;EAChB;;;;;;;;;cAUW,UAAO,SAAa,SAAO,MAAQ,gBAAc;;cAkCjD,OAAI,SAAa,SAAO,MAAQ;;cAIhC,UAAQ,OAAA;;;;;;;;;;;;;KAaT,kBAAkB,SAAS;cAE1B,aAAU,gBAAA,qCAAA,iBAAA,OAAA;;;;;;;;;;;;;;cAcV,WAAQ;WACL,aAAa;WAA8B;GAA6B;WACxE,aAAa;WAA8B;GAA6B;;;;;;cAc3E,UAAO,iBAAmB,gBAAgB,OAAO;;cAMjD,WAAQ,SAAa,yBAAuB,iBAAiB,OAAO;;cAIpE,kBAAgB,QAAA;;;;;;;;;;;;;;;cAYhB,gBAAiB,QAAM,SACzB,aAAa,qBAAqB,QAAQ,uBAAiB,aAG/C,qBAAmB,QAAU"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as headerOf, c as pageKey, i as fits, l as samePage, n as KeptPage, o as keptApplications, r as decodeKept, s as keptDocuments, t as HeadersRead, u as tracked } from "./pageCache-DO0pOgnc.js";
|
|
2
|
+
export { HeadersRead, KeptPage, decodeKept, fits, headerOf, keptApplications, keptDocuments, pageKey, samePage, tracked };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { watchedOf } from "./requestContext.js";
|
|
2
|
+
import { Effect, Option, Schema } from "effect";
|
|
3
|
+
// src/pageCache.ts
|
|
4
|
+
/**
|
|
5
|
+
* What `SSR.entry` needs to keep a rendered page (#428): a request that
|
|
6
|
+
* records which of its headers the render read, and a page as a store keeps
|
|
7
|
+
* it. A page is kept only when every header the render read was absent, so
|
|
8
|
+
* it was drawn from the URL alone and serves every request that also lacks
|
|
9
|
+
* them; the names it read are its `Vary`.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* `request` as the render sees it, every header it reads recorded in `read`.
|
|
13
|
+
* A lookup by name records that name; enumerating the headers records all.
|
|
14
|
+
* Everything else is the request's own, so its getters keep their receiver.
|
|
15
|
+
* A request context read of it (`SSR.requestContext`) is the host's request's,
|
|
16
|
+
* and records all: what a host attaches may say who is asking.
|
|
17
|
+
*/
|
|
18
|
+
const tracked = (request, read) => {
|
|
19
|
+
const headers = new Proxy(request.headers, { get: (target, property) => {
|
|
20
|
+
const value = Reflect.get(target, property, target);
|
|
21
|
+
if (typeof value !== "function") return value;
|
|
22
|
+
if (property === "get" || property === "has") return (name) => {
|
|
23
|
+
read.names.add(name.toLowerCase());
|
|
24
|
+
return value.call(target, name);
|
|
25
|
+
};
|
|
26
|
+
read.all = true;
|
|
27
|
+
return value.bind(target);
|
|
28
|
+
} });
|
|
29
|
+
const watched = new Proxy(request, { get: (target, property) => {
|
|
30
|
+
if (property === "headers") return headers;
|
|
31
|
+
if (property === "clone") read.all = true;
|
|
32
|
+
const value = Reflect.get(target, property, target);
|
|
33
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
34
|
+
} });
|
|
35
|
+
watchedOf().set(watched, {
|
|
36
|
+
original: request,
|
|
37
|
+
readContext: () => {
|
|
38
|
+
read.all = true;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return watched;
|
|
42
|
+
};
|
|
43
|
+
/** Whether a page drawn from `read` serves `request`: it carries none of the headers read. */
|
|
44
|
+
const fits = (request, vary) => vary.every((name) => !request.headers.has(name));
|
|
45
|
+
/** A kept page: what the host draws, its status and headers, and the headers it must lack. */
|
|
46
|
+
const KeptPage = Schema.Struct({
|
|
47
|
+
application: Schema.Struct({
|
|
48
|
+
html: Schema.String,
|
|
49
|
+
title: Schema.String,
|
|
50
|
+
lang: Schema.optionalKey(Schema.String),
|
|
51
|
+
dir: Schema.optionalKey(Schema.Literals([
|
|
52
|
+
"ltr",
|
|
53
|
+
"rtl",
|
|
54
|
+
"auto"
|
|
55
|
+
])),
|
|
56
|
+
canonical: Schema.optionalKey(Schema.String),
|
|
57
|
+
ogUrl: Schema.optionalKey(Schema.String)
|
|
58
|
+
}),
|
|
59
|
+
status: Schema.optionalKey(Schema.Number),
|
|
60
|
+
headers: Schema.Array(Schema.Tuple([Schema.String, Schema.String])),
|
|
61
|
+
vary: Schema.Array(Schema.String)
|
|
62
|
+
});
|
|
63
|
+
const decodeKept = Schema.decodeUnknownOption(Schema.fromJsonString(KeptPage));
|
|
64
|
+
const DAY_MS = 864e5;
|
|
65
|
+
/**
|
|
66
|
+
* A page with each time stamped on it as `T`: a run of 13 digits within a
|
|
67
|
+
* day of `now`, as milliseconds since the epoch read now are (a read's
|
|
68
|
+
* `updatedAt` in the resumed Model). A page drawn a moment later is stamped
|
|
69
|
+
* later, and is still the same page.
|
|
70
|
+
*/
|
|
71
|
+
const unstamped = (text, now) => text.replace(/\b\d{13}\b/g, (digits) => Math.abs(Number(digits) - now) < DAY_MS ? "T" : digits);
|
|
72
|
+
/** Whether two renders drew the same page: what the host draws, and its status. */
|
|
73
|
+
const samePage = (a, b, now) => unstamped(JSON.stringify([a.application, a.status ?? 200]), now) === unstamped(JSON.stringify([b.application, b.status ?? 200]), now);
|
|
74
|
+
const hex = (bytes) => [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
75
|
+
/**
|
|
76
|
+
* Where a page is kept: by its build and its full URL, under the request's
|
|
77
|
+
* origin, since the Workers Cache API keys by URL. A deploy's pages never
|
|
78
|
+
* meet another build's.
|
|
79
|
+
*/
|
|
80
|
+
const pageKey = (buildId, url) => Effect.promise(() => crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${buildId}\n${url}`))).pipe(Effect.map((digest) => `${new URL(url).origin}/__foldkit/cache/page/${hex(digest)}`));
|
|
81
|
+
/** The value a `Rendered` result's `headers` gives, none when absent. */
|
|
82
|
+
const headerOf = (headers, name) => Option.fromNullishOr(headers === void 0 ? null : new Headers(headers).get(name));
|
|
83
|
+
/** The pages `SSR.entry` serves from its cache, by the object it serves. */
|
|
84
|
+
const keptApplications = /* @__PURE__ */ new WeakSet();
|
|
85
|
+
/** The most documents `keptDocuments` holds: each is a whole page's markup. */
|
|
86
|
+
const KEPT_DOCUMENTS = 32;
|
|
87
|
+
/**
|
|
88
|
+
* `render`, remembering the document it drew for each kept page (one
|
|
89
|
+
* `SSR.entry` serves from its `cache`), by the page and the assets: the
|
|
90
|
+
* host draws a page into its document on every request, which parses the
|
|
91
|
+
* page again (`injectIntoTemplate`), so a kept page would cost that much
|
|
92
|
+
* every time it is served. Any other page is drawn as `render` draws it.
|
|
93
|
+
*/
|
|
94
|
+
const keptDocuments = (render) => {
|
|
95
|
+
const drawn = /* @__PURE__ */ new Map();
|
|
96
|
+
return (application, assets) => {
|
|
97
|
+
if (!keptApplications.has(application)) return render(application, assets);
|
|
98
|
+
const key = JSON.stringify([application, assets]);
|
|
99
|
+
const held = drawn.get(key);
|
|
100
|
+
if (held !== void 0) return held;
|
|
101
|
+
const document = render(application, assets);
|
|
102
|
+
drawn.set(key, document);
|
|
103
|
+
for (const oldest of drawn.keys()) {
|
|
104
|
+
if (drawn.size <= KEPT_DOCUMENTS) break;
|
|
105
|
+
drawn.delete(oldest);
|
|
106
|
+
}
|
|
107
|
+
return document;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
export { KeptPage, decodeKept, fits, headerOf, keptApplications, keptDocuments, pageKey, samePage, tracked };
|
|
111
|
+
|
|
112
|
+
//# sourceMappingURL=pageCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pageCache.js","names":[],"sources":["../src/pageCache.ts"],"sourcesContent":["/**\n * What `SSR.entry` needs to keep a rendered page (#428): a request that\n * records which of its headers the render read, and a page as a store keeps\n * it. A page is kept only when every header the render read was absent, so\n * it was drawn from the URL alone and serves every request that also lacks\n * them; the names it read are its `Vary`.\n */\nimport { Effect, Option, Schema } from 'effect'\nimport type { RenderedApplication } from 'foldkit/experimental/server'\nimport { watchedOf } from './requestContext.js'\n\n/** What a render read of its request's headers: names, or every one. */\nexport interface HeadersRead {\n readonly names: Set<string>\n all: boolean\n}\n\n/**\n * `request` as the render sees it, every header it reads recorded in `read`.\n * A lookup by name records that name; enumerating the headers records all.\n * Everything else is the request's own, so its getters keep their receiver.\n * A request context read of it (`SSR.requestContext`) is the host's request's,\n * and records all: what a host attaches may say who is asking.\n */\nexport const tracked = (request: Request, read: HeadersRead): Request => {\n const headers = new Proxy(request.headers, {\n get: (target, property) => {\n const value: unknown = Reflect.get(target, property, target)\n if (typeof value !== 'function') return value\n if (property === 'get' || property === 'has') {\n return (name: string) => {\n read.names.add(name.toLowerCase())\n return value.call(target, name)\n }\n }\n read.all = true\n return value.bind(target)\n },\n })\n const watched = new Proxy(request, {\n get: (target, property) => {\n if (property === 'headers') return headers\n // `clone` would hand out a request this one cannot see into.\n if (property === 'clone') read.all = true\n const value: unknown = Reflect.get(target, property, target)\n return typeof value === 'function' ? value.bind(target) : value\n },\n })\n watchedOf().set(watched, {\n original: request,\n readContext: () => {\n read.all = true\n },\n })\n return watched\n}\n\n/** Whether a page drawn from `read` serves `request`: it carries none of the headers read. */\nexport const fits = (request: Request, vary: ReadonlyArray<string>): boolean =>\n vary.every(name => !request.headers.has(name))\n\n/** A kept page: what the host draws, its status and headers, and the headers it must lack. */\nexport const KeptPage = Schema.Struct({\n application: Schema.Struct({\n html: Schema.String,\n title: Schema.String,\n lang: Schema.optionalKey(Schema.String),\n dir: Schema.optionalKey(Schema.Literals(['ltr', 'rtl', 'auto'])),\n canonical: Schema.optionalKey(Schema.String),\n ogUrl: Schema.optionalKey(Schema.String),\n }),\n status: Schema.optionalKey(Schema.Number),\n headers: Schema.Array(Schema.Tuple([Schema.String, Schema.String])),\n vary: Schema.Array(Schema.String),\n})\nexport type KeptPage = typeof KeptPage.Type\n\nexport const decodeKept = Schema.decodeUnknownOption(Schema.fromJsonString(KeptPage))\n\nconst DAY_MS = 86_400_000\n\n/**\n * A page with each time stamped on it as `T`: a run of 13 digits within a\n * day of `now`, as milliseconds since the epoch read now are (a read's\n * `updatedAt` in the resumed Model). A page drawn a moment later is stamped\n * later, and is still the same page.\n */\nconst unstamped = (text: string, now: number): string =>\n text.replace(/\\b\\d{13}\\b/g, digits => (Math.abs(Number(digits) - now) < DAY_MS ? 'T' : digits))\n\n/** Whether two renders drew the same page: what the host draws, and its status. */\nexport const samePage = (\n a: { readonly application: RenderedApplication; readonly status?: number | undefined },\n b: { readonly application: RenderedApplication; readonly status?: number | undefined },\n now: number,\n): boolean =>\n unstamped(JSON.stringify([a.application, a.status ?? 200]), now) ===\n unstamped(JSON.stringify([b.application, b.status ?? 200]), now)\n\nconst hex = (bytes: ArrayBuffer): string =>\n [...new Uint8Array(bytes)].map(byte => byte.toString(16).padStart(2, '0')).join('')\n\n/**\n * Where a page is kept: by its build and its full URL, under the request's\n * origin, since the Workers Cache API keys by URL. A deploy's pages never\n * meet another build's.\n */\nexport const pageKey = (buildId: string, url: string): Effect.Effect<string> =>\n Effect.promise(() =>\n crypto.subtle.digest('SHA-256', new TextEncoder().encode(`${buildId}\\n${url}`)),\n ).pipe(Effect.map(digest => `${new URL(url).origin}/__foldkit/cache/page/${hex(digest)}`))\n\n/** The value a `Rendered` result's `headers` gives, none when absent. */\nexport const headerOf = (headers: HeadersInit | undefined, name: string): Option.Option<string> =>\n Option.fromNullishOr(headers === undefined ? null : new Headers(headers).get(name))\n\n/** The pages `SSR.entry` serves from its cache, by the object it serves. */\nexport const keptApplications = new WeakSet<RenderedApplication>()\n\n/** The most documents `keptDocuments` holds: each is a whole page's markup. */\nconst KEPT_DOCUMENTS = 32\n\n/**\n * `render`, remembering the document it drew for each kept page (one\n * `SSR.entry` serves from its `cache`), by the page and the assets: the\n * host draws a page into its document on every request, which parses the\n * page again (`injectIntoTemplate`), so a kept page would cost that much\n * every time it is served. Any other page is drawn as `render` draws it.\n */\nexport const keptDocuments = <Assets>(\n render: (application: RenderedApplication, assets: Assets) => string,\n) => {\n const drawn = new Map<string, string>()\n return (application: RenderedApplication, assets: Assets): string => {\n if (!keptApplications.has(application)) return render(application, assets)\n const key = JSON.stringify([application, assets])\n const held = drawn.get(key)\n if (held !== undefined) return held\n const document = render(application, assets)\n drawn.set(key, document)\n for (const oldest of drawn.keys()) {\n if (drawn.size <= KEPT_DOCUMENTS) break\n drawn.delete(oldest)\n }\n return document\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAwBA,MAAa,WAAW,SAAkB,SAA+B;CACvE,MAAM,UAAU,IAAI,MAAM,QAAQ,SAAS,EACzC,MAAM,QAAQ,aAAa;EACzB,MAAM,QAAiB,QAAQ,IAAI,QAAQ,UAAU,MAAM;EAC3D,IAAI,OAAO,UAAU,YAAY,OAAO;EACxC,IAAI,aAAa,SAAS,aAAa,OACrC,QAAQ,SAAiB;GACvB,KAAK,MAAM,IAAI,KAAK,YAAY,CAAC;GACjC,OAAO,MAAM,KAAK,QAAQ,IAAI;EAChC;EAEF,KAAK,MAAM;EACX,OAAO,MAAM,KAAK,MAAM;CAC1B,EACF,CAAC;CACD,MAAM,UAAU,IAAI,MAAM,SAAS,EACjC,MAAM,QAAQ,aAAa;EACzB,IAAI,aAAa,WAAW,OAAO;EAEnC,IAAI,aAAa,SAAS,KAAK,MAAM;EACrC,MAAM,QAAiB,QAAQ,IAAI,QAAQ,UAAU,MAAM;EAC3D,OAAO,OAAO,UAAU,aAAa,MAAM,KAAK,MAAM,IAAI;CAC5D,EACF,CAAC;CACD,UAAU,CAAC,CAAC,IAAI,SAAS;EACvB,UAAU;EACV,mBAAmB;GACjB,KAAK,MAAM;EACb;CACF,CAAC;CACD,OAAO;AACT;;AAGA,MAAa,QAAQ,SAAkB,SACrC,KAAK,OAAM,SAAQ,CAAC,QAAQ,QAAQ,IAAI,IAAI,CAAC;;A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MAAM,GAAG,CAAC;;AAG3F,MAAa,YAAY,SAAkC,SACzD,OAAO,cAAc,YAAY,KAAA,IAAY,OAAO,IAAI,QAAQ,OAAO,CAAC,CAAC,IAAI,IAAI,CAAC;;AAGpF,MAAa,mCAAmB,IAAI,QAA6B;;AAGjE,MAAM,iBAAiB;;;;;;;;AASvB,MAAa,iBACX,WACG;CACH,MAAM,wBAAQ,IAAI,IAAoB;CACtC,QAAQ,aAAkC,WAA2B;EACnE,IAAI,CAAC,iBAAiB,IAAI,WAAW,GAAG,OAAO,OAAO,aAAa,MAAM;EACzE,MAAM,MAAM,KAAK,UAAU,CAAC,aAAa,MAAM,CAAC;EAChD,MAAM,OAAO,MAAM,IAAI,GAAG;EAC1B,IAAI,SAAS,KAAA,GAAW,OAAO;EAC/B,MAAM,WAAW,OAAO,aAAa,MAAM;EAC3C,MAAM,IAAI,KAAK,QAAQ;EACvB,KAAK,MAAM,UAAU,MAAM,KAAK,GAAG;GACjC,IAAI,MAAM,QAAQ,gBAAgB;GAClC,MAAM,OAAO,MAAM;EACrB;EACA,OAAO;CACT;AACF"}
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { Option } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* The request a render that may be kept reads in place of the host's, to the
|
|
4
|
+
* host's, with what to record when the render reads a context of it. On
|
|
5
|
+
* `globalThis`, as the values are, since the Worker's copy of this module
|
|
6
|
+
* may be asked about a request the page bundle's `SSR.entry` made.
|
|
7
|
+
*/
|
|
8
|
+
declare const watchedOf: () => WeakMap<Request, {
|
|
9
|
+
readonly original: Request;
|
|
10
|
+
readonly readContext: () => void;
|
|
11
|
+
}>;
|
|
2
12
|
/** A value of type `C` the host attaches to a request, for its `renderPage` to read. */
|
|
3
13
|
interface RequestContext<C> {
|
|
4
14
|
readonly name: string;
|
|
@@ -17,5 +27,5 @@ interface RequestContext<C> {
|
|
|
17
27
|
* other's values by the name.
|
|
18
28
|
*/
|
|
19
29
|
declare const requestContext: <C>(name: string) => RequestContext<C>;
|
|
20
|
-
export { requestContext as n, RequestContext as t };
|
|
21
|
-
//# sourceMappingURL=requestContext-
|
|
30
|
+
export { requestContext as n, watchedOf as r, RequestContext as t };
|
|
31
|
+
//# sourceMappingURL=requestContext-DmwmQeci.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestContext-DmwmQeci.d.ts","names":[],"sources":["../src/requestContext.ts"],"mappings":";;;;;;;cAyCa,iBAAS,QAAA;WACD,UAAA;WACG;;;UAIP,eAAe;WACrB;;WAEA,SAAS,SAAS,SAAS,OAAO,MAAM;;;;;;WAMxC,KAAK,SAAS,YAAY,OAAO,OAAO;;;;;;;cAQtC,iBAAkB,GAAC,iBAAiB,eAAe"}
|
package/dist/requestContext.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as requestContext, t as RequestContext } from "./requestContext-
|
|
2
|
-
export { RequestContext, requestContext };
|
|
1
|
+
import { n as requestContext, r as watchedOf, t as RequestContext } from "./requestContext-DmwmQeci.js";
|
|
2
|
+
export { RequestContext, requestContext, watchedOf };
|