@kayjs/kay 2.0.0-alpha.8 → 2.0.0-beta.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/package.json +1 -1
- package/src/build/compile.ts +11 -2
- package/src/build/index.ts +233 -42
- package/src/cli/index.ts +27 -11
- package/src/cli/new.ts +15 -4
- package/src/compiler/dom.ts +141 -13
- package/src/compiler/ssr.ts +101 -22
- package/src/dev/index.ts +26 -7
- package/src/dom/extras.ts +79 -44
- package/src/dom/index.ts +90 -6
- package/src/dom/island.ts +67 -24
- package/src/dom/loop.ts +6 -1
- package/src/plugins/index.ts +8 -1
- package/src/reactivity/index.ts +80 -11
- package/src/reactivity/store.ts +1 -1
- package/src/server/action.ts +98 -26
- package/src/server/compress.ts +87 -8
- package/src/server/cookies.ts +15 -8
- package/src/server/http.ts +121 -6
- package/src/server/index.ts +446 -236
- package/src/server/router.ts +6 -1
- package/src/server/security.ts +76 -18
- package/src/ssr/context.ts +2 -1
- package/src/ssr/index.ts +88 -20
- package/src/test/index.ts +4 -0
package/src/dom/extras.ts
CHANGED
|
@@ -2,20 +2,35 @@
|
|
|
2
2
|
// so Bun.build({ splitting: true }) keeps them out of minimal islands that don't use them.
|
|
3
3
|
|
|
4
4
|
import type { Owner } from '../reactivity/index'
|
|
5
|
-
import {
|
|
6
|
-
import { claimInsert, setAttr, setClass, setProp, setStyle } from './index'
|
|
5
|
+
import { createOwner, disposeOwner, effect, getOwner, runWithOwner, untrack } from '../reactivity/index'
|
|
6
|
+
import { claimInsert, setAttr, setClass, setProp, setStyle, skipRange } from './index'
|
|
7
|
+
|
|
8
|
+
const RAW_HTML = Symbol.for('kay.html')
|
|
9
|
+
|
|
10
|
+
function isRawHtml(value: unknown): value is { __html: string } {
|
|
11
|
+
return typeof value === 'object' && value !== null && (value as Record<symbol, unknown>)[RAW_HTML] === true && '__html' in value
|
|
12
|
+
}
|
|
7
13
|
|
|
8
14
|
export function setInnerHtml(el: Element, value: unknown): void {
|
|
9
15
|
const html = value == null || value === false
|
|
10
16
|
? ''
|
|
11
|
-
: (
|
|
17
|
+
: isRawHtml(value)
|
|
18
|
+
? String(value.__html)
|
|
19
|
+
: String(value)
|
|
12
20
|
if (el.innerHTML !== html)
|
|
13
21
|
el.innerHTML = html
|
|
14
22
|
}
|
|
15
23
|
|
|
16
|
-
const
|
|
24
|
+
const VALID_ATTR_NAME = /^[A-Za-z_:][-\w:.]*$/
|
|
17
25
|
const SPREAD_DOM_PROPS = new Set(['value', 'checked', 'selected', 'muted', 'indeterminate'])
|
|
18
26
|
|
|
27
|
+
function isBlockedSpreadAttr(name: string): boolean {
|
|
28
|
+
if (!VALID_ATTR_NAME.test(name))
|
|
29
|
+
return true
|
|
30
|
+
const lower = name.toLowerCase()
|
|
31
|
+
return lower.startsWith('on') || lower === 'innerhtml' || lower === 'srcdoc'
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
export function omit(props: Record<string, unknown>, excluded: string[]): Record<string, unknown> {
|
|
20
35
|
const out: Record<string, unknown> = {}
|
|
21
36
|
const skip = new Set(excluded)
|
|
@@ -31,21 +46,6 @@ export function omit(props: Record<string, unknown>, excluded: string[]): Record
|
|
|
31
46
|
|
|
32
47
|
export function spread(el: Element, getProps: () => Record<string, unknown> | null | undefined): void {
|
|
33
48
|
let prevKeys: string[] = []
|
|
34
|
-
const listeners = new Map<string, (e: Event) => void>()
|
|
35
|
-
|
|
36
|
-
const removeListener = (ev: string) => {
|
|
37
|
-
const fn = listeners.get(ev)
|
|
38
|
-
if (fn !== undefined) {
|
|
39
|
-
el.removeEventListener(ev, fn)
|
|
40
|
-
listeners.delete(ev)
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
onCleanup(() => {
|
|
45
|
-
for (const [ev, fn] of listeners)
|
|
46
|
-
el.removeEventListener(ev, fn)
|
|
47
|
-
listeners.clear()
|
|
48
|
-
})
|
|
49
49
|
|
|
50
50
|
effect(() => {
|
|
51
51
|
const props = getProps() ?? {}
|
|
@@ -55,18 +55,16 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
55
55
|
const k = prevKeys[i]!
|
|
56
56
|
if (k in props)
|
|
57
57
|
continue
|
|
58
|
-
|
|
59
|
-
if (ev !== null) {
|
|
60
|
-
removeListener(ev[1]!.toLowerCase())
|
|
61
|
-
}
|
|
62
|
-
else if (k === 'class' || k === 'className') {
|
|
58
|
+
if (k === 'class' || k === 'className') {
|
|
63
59
|
setClass(el, '')
|
|
64
60
|
}
|
|
65
61
|
else if (k === 'style') {
|
|
66
62
|
setStyle(el as HTMLElement, null)
|
|
67
63
|
}
|
|
68
64
|
else if (k !== 'key' && k !== 'ref' && k !== '$hydrate' && k !== 'children') {
|
|
69
|
-
|
|
65
|
+
const name = k === 'htmlFor' ? 'for' : k
|
|
66
|
+
if (!isBlockedSpreadAttr(name))
|
|
67
|
+
el.removeAttribute(name)
|
|
70
68
|
}
|
|
71
69
|
}
|
|
72
70
|
|
|
@@ -80,21 +78,10 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
80
78
|
(v as (node: Element) => void)(el)
|
|
81
79
|
continue
|
|
82
80
|
}
|
|
83
|
-
const ev = SPREAD_EVENT_RE.exec(k)
|
|
84
|
-
if (ev !== null) {
|
|
85
|
-
const type = ev[1]!.toLowerCase()
|
|
86
|
-
removeListener(type)
|
|
87
|
-
if (typeof v === 'function') {
|
|
88
|
-
const wrapped = (e: Event) => batch(() => (v as (ev: Event) => void)(e))
|
|
89
|
-
listeners.set(type, wrapped)
|
|
90
|
-
el.addEventListener(type, wrapped)
|
|
91
|
-
}
|
|
92
|
-
continue
|
|
93
|
-
}
|
|
94
81
|
const name = k === 'className' ? 'class' : k === 'htmlFor' ? 'for' : k
|
|
95
|
-
if (name
|
|
96
|
-
|
|
97
|
-
|
|
82
|
+
if (isBlockedSpreadAttr(name))
|
|
83
|
+
continue
|
|
84
|
+
if (name === 'class')
|
|
98
85
|
setClass(el, v)
|
|
99
86
|
else if (name === 'style')
|
|
100
87
|
setStyle(el as HTMLElement, v)
|
|
@@ -131,22 +118,42 @@ function claimTextNode(claimed: Node | null, str: string): Text | undefined {
|
|
|
131
118
|
return undefined
|
|
132
119
|
}
|
|
133
120
|
|
|
134
|
-
export function insert(parent: Node, value: () => unknown, marker: Node | null = null): void {
|
|
121
|
+
export function insert(parent: Node, value: () => unknown, marker: Node | null = null, startMarker?: Node | null): void {
|
|
135
122
|
let current: Node | Node[] | null = null
|
|
136
123
|
let textNode: Text | undefined
|
|
124
|
+
let firstRun = true
|
|
137
125
|
|
|
138
126
|
effect(() => {
|
|
139
|
-
|
|
140
|
-
|
|
127
|
+
let res: { result: unknown, marker: Node | null, claimed: Node | null, nodes: Node[] | undefined }
|
|
128
|
+
if (firstRun) {
|
|
129
|
+
firstRun = false
|
|
130
|
+
let effMarker = marker
|
|
131
|
+
let effStart = startMarker
|
|
132
|
+
if (effStart === undefined && effMarker !== null && effMarker.nodeType === 8) {
|
|
133
|
+
const close = skipRange(effMarker)
|
|
134
|
+
if (close !== null) {
|
|
135
|
+
effStart = effMarker
|
|
136
|
+
effMarker = close
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
res = claimInsert(parent, effMarker, value, effStart)
|
|
140
|
+
marker = res.marker
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
res = { result: value(), marker, claimed: null, nodes: undefined }
|
|
144
|
+
}
|
|
141
145
|
const next = res.result
|
|
146
|
+
const ssrNodes = res.nodes
|
|
142
147
|
|
|
143
|
-
if (next == null &&
|
|
144
|
-
current =
|
|
148
|
+
if (next == null && ssrNodes !== undefined) {
|
|
149
|
+
current = ssrNodes
|
|
145
150
|
return
|
|
146
151
|
}
|
|
147
152
|
|
|
148
153
|
if (next == null || typeof next === 'boolean') {
|
|
149
154
|
clearInserted(current)
|
|
155
|
+
if (ssrNodes !== undefined)
|
|
156
|
+
clearInserted(ssrNodes)
|
|
150
157
|
current = null
|
|
151
158
|
if (textNode !== undefined)
|
|
152
159
|
textNode.data = ''
|
|
@@ -163,8 +170,16 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
|
|
|
163
170
|
const claimed = claimTextNode(res.claimed, str)
|
|
164
171
|
if (claimed !== undefined) {
|
|
165
172
|
textNode = claimed
|
|
173
|
+
if (ssrNodes !== undefined && ssrNodes.length > 1) {
|
|
174
|
+
for (let i = 0; i < ssrNodes.length; i++) {
|
|
175
|
+
if (ssrNodes[i] !== claimed)
|
|
176
|
+
ssrNodes[i]!.parentNode?.removeChild(ssrNodes[i]!)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
166
179
|
}
|
|
167
180
|
else {
|
|
181
|
+
if (ssrNodes !== undefined)
|
|
182
|
+
clearInserted(ssrNodes)
|
|
168
183
|
textNode = document.createTextNode(str)
|
|
169
184
|
parent.insertBefore(textNode, marker)
|
|
170
185
|
}
|
|
@@ -184,10 +199,18 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
|
|
|
184
199
|
if (current === next)
|
|
185
200
|
return
|
|
186
201
|
if (current === null && next.parentNode === parent) {
|
|
202
|
+
if (ssrNodes !== undefined && ssrNodes.length > 1) {
|
|
203
|
+
for (let i = 0; i < ssrNodes.length; i++) {
|
|
204
|
+
if (ssrNodes[i] !== next)
|
|
205
|
+
ssrNodes[i]!.parentNode?.removeChild(ssrNodes[i]!)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
187
208
|
current = next
|
|
188
209
|
return
|
|
189
210
|
}
|
|
190
211
|
clearInserted(current)
|
|
212
|
+
if (ssrNodes !== undefined)
|
|
213
|
+
clearInserted(ssrNodes)
|
|
191
214
|
if (next.nodeType === 11) {
|
|
192
215
|
const nodes = Array.from(next.childNodes)
|
|
193
216
|
parent.insertBefore(next, marker)
|
|
@@ -202,6 +225,18 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
|
|
|
202
225
|
|
|
203
226
|
if (Array.isArray(next)) {
|
|
204
227
|
clearInserted(current)
|
|
228
|
+
if (ssrNodes !== undefined) {
|
|
229
|
+
const kept = new Set<Node>()
|
|
230
|
+
for (let i = 0; i < next.length; i++) {
|
|
231
|
+
const item = next[i]
|
|
232
|
+
if (item instanceof Node && item.parentNode === parent)
|
|
233
|
+
kept.add(item)
|
|
234
|
+
}
|
|
235
|
+
for (let i = 0; i < ssrNodes.length; i++) {
|
|
236
|
+
if (!kept.has(ssrNodes[i]!))
|
|
237
|
+
ssrNodes[i]!.parentNode?.removeChild(ssrNodes[i]!)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
205
240
|
const f = document.createDocumentFragment()
|
|
206
241
|
const nodes: Node[] = []
|
|
207
242
|
for (let i = 0; i < next.length; i++) {
|
package/src/dom/index.ts
CHANGED
|
@@ -28,10 +28,33 @@ export function claimLoop(parent: Node, fn: () => void, skip = 0): boolean {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
// `nodes` is the SSR range the render claimed through template() (a fragment hydrates to null, so the caller learns its nodes here).
|
|
31
|
-
export function claimInsert<T>(
|
|
31
|
+
export function claimInsert<T>(
|
|
32
|
+
parent: Node,
|
|
33
|
+
marker: Node | null,
|
|
34
|
+
fn: () => T,
|
|
35
|
+
startMarker?: Node | null,
|
|
36
|
+
): { result: T, marker: Node | null, claimed: Node | null, nodes: Node[] | undefined } {
|
|
32
37
|
if (!hydrating)
|
|
33
38
|
return { result: fn(), marker, claimed: null, nodes: undefined }
|
|
34
39
|
|
|
40
|
+
if (startMarker !== undefined) {
|
|
41
|
+
const first = startMarker === null ? parent.firstChild : startMarker.nextSibling
|
|
42
|
+
const claimed = first === marker ? null : first
|
|
43
|
+
const prevNode = hydrateNode
|
|
44
|
+
hydrateNode = claimed
|
|
45
|
+
let result: T
|
|
46
|
+
try {
|
|
47
|
+
result = fn()
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
hydrateNode = prevNode
|
|
51
|
+
}
|
|
52
|
+
const nodes: Node[] = []
|
|
53
|
+
for (let n: Node | null = claimed; n !== null && n !== marker; n = n.nextSibling)
|
|
54
|
+
nodes.push(n)
|
|
55
|
+
return { result, marker, claimed, nodes: nodes.length > 0 ? nodes : undefined }
|
|
56
|
+
}
|
|
57
|
+
|
|
35
58
|
const claimed: Node | null = marker === null ? parent.lastChild : marker.nodeType === 8 ? null : marker
|
|
36
59
|
const prevNode = hydrateNode
|
|
37
60
|
hydrateNode = claimed
|
|
@@ -44,7 +67,16 @@ export function claimInsert<T>(parent: Node, marker: Node | null, fn: () => T):
|
|
|
44
67
|
finally {
|
|
45
68
|
hydrateNode = prevNode
|
|
46
69
|
}
|
|
47
|
-
if (marker === null
|
|
70
|
+
if (marker === null) {
|
|
71
|
+
let nodes: Node[] | undefined
|
|
72
|
+
if (claimed !== null && end !== claimed) {
|
|
73
|
+
nodes = []
|
|
74
|
+
for (let n: Node | null = claimed; n !== null && n !== end; n = n.nextSibling)
|
|
75
|
+
nodes.push(n)
|
|
76
|
+
}
|
|
77
|
+
return { result, marker: null, claimed, nodes }
|
|
78
|
+
}
|
|
79
|
+
if (claimed === null)
|
|
48
80
|
return { result, marker, claimed, nodes: undefined }
|
|
49
81
|
|
|
50
82
|
// The anchor goes after the claimed range; a text value or a nested insert moves no cursor and claims the one node.
|
|
@@ -73,6 +105,10 @@ export function hydrate(container: Element, render: () => Node | DocumentFragmen
|
|
|
73
105
|
render()
|
|
74
106
|
})
|
|
75
107
|
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
disposeOwner(owner)
|
|
110
|
+
throw err
|
|
111
|
+
}
|
|
76
112
|
finally {
|
|
77
113
|
hydrating = prevHydrating
|
|
78
114
|
hydrateNode = prevNode
|
|
@@ -91,6 +127,10 @@ export function mount(container: Element, render: () => Node | DocumentFragment
|
|
|
91
127
|
if (node !== null)
|
|
92
128
|
container.appendChild(node)
|
|
93
129
|
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
disposeOwner(owner)
|
|
132
|
+
throw err
|
|
133
|
+
}
|
|
94
134
|
finally {
|
|
95
135
|
hydrating = prevHydrating
|
|
96
136
|
}
|
|
@@ -170,6 +210,14 @@ export function skipText(marker: Node): Node | null {
|
|
|
170
210
|
return next !== null && next.nodeType === 3 ? next.nextSibling : next
|
|
171
211
|
}
|
|
172
212
|
|
|
213
|
+
/** Advance past an SSR-bracketed dynamic range (`<!---->...<!---->`) to its closing comment marker. */
|
|
214
|
+
export function skipRange(marker: Node): Node | null {
|
|
215
|
+
let cur = marker.nextSibling
|
|
216
|
+
while (cur !== null && cur.nodeType !== 8)
|
|
217
|
+
cur = cur.nextSibling
|
|
218
|
+
return cur
|
|
219
|
+
}
|
|
220
|
+
|
|
173
221
|
export function setText(node: Text, value: unknown): void {
|
|
174
222
|
const str = value == null || typeof value === 'boolean' ? '' : String(value)
|
|
175
223
|
if (node.data !== str)
|
|
@@ -194,13 +242,49 @@ export function setClass(el: Element, value: unknown): void {
|
|
|
194
242
|
}
|
|
195
243
|
}
|
|
196
244
|
|
|
245
|
+
const VALID_ATTR_NAME = /^[A-Za-z_:][-\w:.]*$/
|
|
246
|
+
const URL_ATTRS = new Set(['href', 'src', 'action', 'formaction', 'xlink:href'])
|
|
247
|
+
const UNSAFE_URL_RE = /^(?:javascript|vbscript):/i
|
|
248
|
+
const STRING_BOOL_ATTRS = new Set(['draggable', 'spellcheck', 'contenteditable'])
|
|
249
|
+
|
|
250
|
+
function isBlockedAttr(name: string): boolean {
|
|
251
|
+
if (!VALID_ATTR_NAME.test(name))
|
|
252
|
+
return true
|
|
253
|
+
const lower = name.toLowerCase()
|
|
254
|
+
return lower.startsWith('on') || lower === 'innerhtml' || lower === 'srcdoc'
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isStringBoolAttr(name: string): boolean {
|
|
258
|
+
const lower = name.toLowerCase()
|
|
259
|
+
return lower.startsWith('aria-') || STRING_BOOL_ATTRS.has(lower)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function isUnsafeUrlAttr(name: string, value: unknown): boolean {
|
|
263
|
+
if (!URL_ATTRS.has(name.toLowerCase()))
|
|
264
|
+
return false
|
|
265
|
+
return UNSAFE_URL_RE.test(String(value).replace(/[\u0000-\u0020]+/g, ''))
|
|
266
|
+
}
|
|
267
|
+
|
|
197
268
|
export function setAttr(el: Element, name: string, value: unknown): void {
|
|
198
|
-
if (
|
|
269
|
+
if (isBlockedAttr(name))
|
|
270
|
+
return
|
|
271
|
+
if (value == null) {
|
|
272
|
+
el.removeAttribute(name)
|
|
273
|
+
}
|
|
274
|
+
else if (typeof value === 'boolean') {
|
|
275
|
+
if (isStringBoolAttr(name))
|
|
276
|
+
el.setAttribute(name, value ? 'true' : 'false')
|
|
277
|
+
else if (value)
|
|
278
|
+
el.setAttribute(name, '')
|
|
279
|
+
else
|
|
280
|
+
el.removeAttribute(name)
|
|
281
|
+
}
|
|
282
|
+
else if (isUnsafeUrlAttr(name, value)) {
|
|
199
283
|
el.removeAttribute(name)
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
else
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
203
286
|
el.setAttribute(name, String(value))
|
|
287
|
+
}
|
|
204
288
|
}
|
|
205
289
|
|
|
206
290
|
export function setProp(el: Element, name: string, value: unknown): void {
|
package/src/dom/island.ts
CHANGED
|
@@ -33,12 +33,20 @@ export function defineIslandElement(): void {
|
|
|
33
33
|
|
|
34
34
|
class KayIsland extends HTMLElement {
|
|
35
35
|
_d: (() => void) | undefined
|
|
36
|
+
_c: (() => void) | undefined
|
|
36
37
|
_s = false
|
|
38
|
+
_t = 0
|
|
39
|
+
|
|
40
|
+
_clearStrategy(): void {
|
|
41
|
+
this._c?.()
|
|
42
|
+
this._c = undefined
|
|
43
|
+
}
|
|
37
44
|
|
|
38
45
|
connectedCallback(): void {
|
|
39
46
|
if (this._s)
|
|
40
47
|
return
|
|
41
48
|
this._s = true
|
|
49
|
+
const token = ++this._t
|
|
42
50
|
|
|
43
51
|
const h = this.getAttribute('h')
|
|
44
52
|
if (typeof KAY_DEFERRED === 'undefined' || KAY_DEFERRED) {
|
|
@@ -46,52 +54,74 @@ export function defineIslandElement(): void {
|
|
|
46
54
|
const target = this.firstElementChild ?? this
|
|
47
55
|
const io = new IntersectionObserver((entries) => {
|
|
48
56
|
if (entries.some(e => e.isIntersecting)) {
|
|
49
|
-
|
|
50
|
-
void this._l(false)
|
|
57
|
+
this._clearStrategy()
|
|
58
|
+
void this._l(false, token)
|
|
51
59
|
}
|
|
52
60
|
}, { rootMargin: '200px' })
|
|
61
|
+
this._c = () => io.disconnect()
|
|
53
62
|
io.observe(target)
|
|
54
63
|
return
|
|
55
64
|
}
|
|
56
65
|
if (h === 'idle') {
|
|
57
|
-
const
|
|
58
|
-
|
|
66
|
+
const g = globalThis as { requestIdleCallback?: (cb: () => void) => number, cancelIdleCallback?: (id: number) => void }
|
|
67
|
+
if (g.requestIdleCallback) {
|
|
68
|
+
const id = g.requestIdleCallback(() => {
|
|
69
|
+
this._c = undefined
|
|
70
|
+
void this._l(false, token)
|
|
71
|
+
})
|
|
72
|
+
this._c = () => g.cancelIdleCallback?.(id)
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const id = setTimeout(() => {
|
|
76
|
+
this._c = undefined
|
|
77
|
+
void this._l(false, token)
|
|
78
|
+
}, 1)
|
|
79
|
+
this._c = () => clearTimeout(id)
|
|
80
|
+
}
|
|
59
81
|
return
|
|
60
82
|
}
|
|
61
83
|
if (h === 'interaction') {
|
|
62
84
|
const evs = ['pointerdown', 'focusin', 'keydown']
|
|
63
|
-
const
|
|
85
|
+
const stop = () => {
|
|
64
86
|
for (const e of evs) this.removeEventListener(e, go, true)
|
|
65
|
-
|
|
87
|
+
}
|
|
88
|
+
const go = () => {
|
|
89
|
+
this._c = undefined
|
|
90
|
+
stop()
|
|
91
|
+
void this._l(false, token)
|
|
66
92
|
}
|
|
67
93
|
for (const e of evs) this.addEventListener(e, go, { capture: true, once: true })
|
|
94
|
+
this._c = stop
|
|
68
95
|
return
|
|
69
96
|
}
|
|
70
97
|
if (h !== null && h.startsWith('media(')) {
|
|
71
98
|
const mq = matchMedia(h.slice(6, -1))
|
|
72
99
|
if (mq.matches) {
|
|
73
|
-
void this._l(false)
|
|
100
|
+
void this._l(false, token)
|
|
74
101
|
}
|
|
75
102
|
else {
|
|
76
103
|
const go = () => {
|
|
77
104
|
if (mq.matches) {
|
|
78
|
-
|
|
79
|
-
void this._l(false)
|
|
105
|
+
this._clearStrategy()
|
|
106
|
+
void this._l(false, token)
|
|
80
107
|
}
|
|
81
108
|
}
|
|
82
109
|
mq.addEventListener('change', go)
|
|
110
|
+
this._c = () => mq.removeEventListener('change', go)
|
|
83
111
|
}
|
|
84
112
|
return
|
|
85
113
|
}
|
|
86
114
|
}
|
|
87
115
|
|
|
88
|
-
void this._l(h === 'client')
|
|
116
|
+
void this._l(h === 'client', token)
|
|
89
117
|
}
|
|
90
118
|
|
|
91
119
|
disconnectedCallback(): void {
|
|
92
120
|
// Defer disposal by a microtask so synchronous DOM moves (e.g. SPA persistent islands) stay alive!
|
|
93
121
|
queueMicrotask(() => {
|
|
94
122
|
if (!this.isConnected) {
|
|
123
|
+
this._t++
|
|
124
|
+
this._clearStrategy()
|
|
95
125
|
this._d?.()
|
|
96
126
|
this._d = undefined
|
|
97
127
|
this._s = false
|
|
@@ -99,24 +129,37 @@ export function defineIslandElement(): void {
|
|
|
99
129
|
})
|
|
100
130
|
}
|
|
101
131
|
|
|
102
|
-
async _l(clientOnly: boolean): Promise<void> {
|
|
132
|
+
async _l(clientOnly: boolean, token: number): Promise<void> {
|
|
133
|
+
this._clearStrategy()
|
|
103
134
|
const chunk = this.getAttribute('c')
|
|
104
|
-
if (!chunk)
|
|
135
|
+
if (!chunk || token !== this._t || !this.isConnected || this._d !== undefined)
|
|
105
136
|
return
|
|
106
137
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
138
|
+
let dispose: (() => void) | undefined
|
|
139
|
+
try {
|
|
140
|
+
const mod = await import(chunk) as IslandModule
|
|
141
|
+
if (!this.isConnected || token !== this._t || this._d !== undefined)
|
|
142
|
+
return
|
|
110
143
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
144
|
+
const p = this.getAttribute('p')
|
|
145
|
+
const props: Record<string, unknown> = p ? parseIslandProps(p) : {}
|
|
146
|
+
const slot = this.querySelector('kay-slot')
|
|
147
|
+
if (slot !== null)
|
|
148
|
+
props.children = slot
|
|
149
|
+
const render = () => mod.default(props)
|
|
150
|
+
dispose = (typeof KAY_CLIENT_ONLY === 'undefined' || KAY_CLIENT_ONLY) && clientOnly
|
|
151
|
+
? mount(this, render)
|
|
152
|
+
: hydrate(this, render)
|
|
153
|
+
if (!this.isConnected || token !== this._t) {
|
|
154
|
+
dispose()
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
this._d = dispose
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
dispose?.()
|
|
161
|
+
console.error(`[kay-island] failed to hydrate ${chunk}:`, err)
|
|
162
|
+
}
|
|
120
163
|
}
|
|
121
164
|
}
|
|
122
165
|
|
package/src/dom/loop.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// LIS minimal DOM moves (swap = 2 moves), and O(1) parent.textContent = '' clear.
|
|
3
3
|
|
|
4
4
|
import type { Owner, Signal } from '../reactivity/index'
|
|
5
|
-
import { createOwner, disposeOwner, effect, getOwner, runWithOwner, signal, untrack } from '../reactivity/index'
|
|
5
|
+
import { createOwner, disposeOwner, effect, getOwner, onCleanup, runWithOwner, signal, untrack } from '../reactivity/index'
|
|
6
6
|
import { claimLoop } from './index'
|
|
7
7
|
|
|
8
8
|
interface Row<T> {
|
|
@@ -27,6 +27,11 @@ export function loop<T>(
|
|
|
27
27
|
const parentOwner = getOwner()
|
|
28
28
|
const needsIndex = renderRow.length >= 2
|
|
29
29
|
let rows: Row<T>[] = []
|
|
30
|
+
if (parentOwner !== undefined) {
|
|
31
|
+
onCleanup(() => {
|
|
32
|
+
rows = []
|
|
33
|
+
})
|
|
34
|
+
}
|
|
30
35
|
|
|
31
36
|
const makeRow = (itemVal: T, idx: number, key: unknown): Row<T> => {
|
|
32
37
|
const owner = createOwner(parentOwner)
|
package/src/plugins/index.ts
CHANGED
|
@@ -36,11 +36,16 @@ export interface CheckPluginContext {
|
|
|
36
36
|
error(message: string): void
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
export interface PluginRouteOptions {
|
|
40
|
+
/** Enable CSRF Origin/Sec-Fetch-Site verification on unsafe methods (default: true). Set `false` to opt out (e.g. webhooks). */
|
|
41
|
+
csrf?: boolean | undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
39
44
|
export interface ServerPluginAPI {
|
|
40
45
|
/** Register global middleware that runs before route handlers. */
|
|
41
46
|
use(middleware: Middleware): void
|
|
42
47
|
/** Register a custom HTTP route (e.g. GET /sitemap.xml, POST /auth/callback). */
|
|
43
|
-
route(method: string, path: string, handler: (ctx: RequestContext) => Response | Promise<Response
|
|
48
|
+
route(method: string, path: string, handler: (ctx: RequestContext) => Response | Promise<Response>, options?: PluginRouteOptions): void
|
|
44
49
|
/** Rewrite an incoming pathname before route matching (e.g. strip /fr locale prefix). */
|
|
45
50
|
rewrite(fn: (pathname: string, ctx: RequestContext) => string | undefined): void
|
|
46
51
|
}
|
|
@@ -88,6 +93,8 @@ export interface KayConfig {
|
|
|
88
93
|
assetPrefix?: string | undefined
|
|
89
94
|
security?: SecurityHeadersOptions | false | undefined
|
|
90
95
|
trustedOrigins?: string[] | undefined
|
|
96
|
+
trustProxy?: boolean | undefined
|
|
97
|
+
maxRequestBodySize?: number | undefined
|
|
91
98
|
compress?: boolean | undefined
|
|
92
99
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
93
100
|
plugins?: KayPlugin[] | undefined
|