prunelle 0.9.0 → 0.10.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.
Files changed (96) hide show
  1. package/llms.txt +18 -18
  2. package/package.json +2 -2
  3. package/src/a11y/declaration.ts +4 -7
  4. package/src/build/bundle.ts +6 -2
  5. package/src/build/index.ts +3 -1
  6. package/src/build/island-promote.ts +42 -2
  7. package/src/build/islands.ts +59 -8
  8. package/src/build/manifest.ts +98 -0
  9. package/src/build/plugin-host.ts +11 -1
  10. package/src/build/prerender.ts +6 -1
  11. package/src/build/reach.ts +4 -3
  12. package/src/build/resolve.ts +5 -1
  13. package/src/build/routes.ts +18 -0
  14. package/src/cli/check-forms.ts +8 -0
  15. package/src/cli/check-links.ts +14 -5
  16. package/src/cli/check.ts +12 -6
  17. package/src/cli/commands.ts +5 -3
  18. package/src/cli/form-opaque-children.ts +40 -0
  19. package/src/compiler/analyze.ts +20 -14
  20. package/src/compiler/cacheability-private.ts +13 -3
  21. package/src/compiler/compliance-contrast.ts +48 -10
  22. package/src/compiler/compliance-rules.ts +72 -0
  23. package/src/compiler/compliance.ts +6 -66
  24. package/src/compiler/dom-attrs.ts +178 -22
  25. package/src/compiler/dom.ts +31 -44
  26. package/src/compiler/emit.ts +17 -6
  27. package/src/compiler/expr-context.ts +260 -207
  28. package/src/compiler/props-body.ts +263 -21
  29. package/src/compiler/props.ts +155 -5
  30. package/src/compiler/signal-reads.ts +142 -29
  31. package/src/compiler/spread-handlers.ts +25 -13
  32. package/src/compiler/ssr-emit.ts +3 -3
  33. package/src/compiler/ssr-helpers.ts +0 -5
  34. package/src/compiler/ssr.ts +4 -2
  35. package/src/dev/host-check.ts +2 -1
  36. package/src/dev/reload.ts +4 -1
  37. package/src/dom/attrs.ts +93 -25
  38. package/src/dom/extras.ts +89 -49
  39. package/src/dom/field-marks.ts +9 -1
  40. package/src/dom/index.ts +19 -13
  41. package/src/dom/loop.ts +259 -188
  42. package/src/dom/prefetch.ts +4 -1
  43. package/src/dom/regions.ts +25 -8
  44. package/src/dom/swap.ts +11 -10
  45. package/src/dom/trusted.ts +4 -4
  46. package/src/eco/declaration.ts +5 -1
  47. package/src/eco/index.ts +3 -0
  48. package/src/index.ts +3 -2
  49. package/src/plugins/config.ts +1 -1
  50. package/src/plugins/public.ts +4 -0
  51. package/src/plugins/types.ts +8 -1
  52. package/src/reactivity/graph.ts +244 -41
  53. package/src/reactivity/hmr.ts +14 -3
  54. package/src/reactivity/index.ts +219 -235
  55. package/src/reactivity/resource.ts +17 -3
  56. package/src/reactivity/seed.ts +118 -16
  57. package/src/reactivity/selector.ts +85 -47
  58. package/src/reactivity/server-seed.ts +31 -23
  59. package/src/reactivity/store.ts +316 -136
  60. package/src/server/action-protect.ts +3 -0
  61. package/src/server/action-registry.ts +5 -1
  62. package/src/server/cache-invalidation.ts +4 -3
  63. package/src/server/compress.ts +52 -17
  64. package/src/server/consent.ts +5 -3
  65. package/src/server/cookies.ts +27 -8
  66. package/src/server/cors.ts +98 -0
  67. package/src/server/csp-report.ts +8 -4
  68. package/src/server/health.ts +9 -0
  69. package/src/server/http.ts +3 -1
  70. package/src/server/index.ts +25 -13
  71. package/src/server/loader.ts +1 -0
  72. package/src/server/plugin-host.ts +41 -9
  73. package/src/server/plugin-route-shadow.ts +24 -0
  74. package/src/server/rate-limit.ts +13 -3
  75. package/src/server/region.ts +19 -2
  76. package/src/server/request-handler.ts +58 -22
  77. package/src/server/request-log.ts +1 -1
  78. package/src/server/route-cache.ts +14 -2
  79. package/src/server/route-table.ts +21 -11
  80. package/src/server/router.ts +2 -1
  81. package/src/server/routing.ts +100 -42
  82. package/src/server/sanitize-html.ts +3 -1
  83. package/src/server/serve-handlers.ts +10 -4
  84. package/src/server/served-file.ts +19 -14
  85. package/src/server/shared-store.ts +10 -1
  86. package/src/server/static-dir.ts +21 -3
  87. package/src/server/webhook.ts +6 -4
  88. package/src/ssr/context.ts +149 -51
  89. package/src/ssr/index.ts +41 -46
  90. package/src/ssr/region.ts +9 -3
  91. package/src/style.ts +12 -5
  92. package/src/test/browser.ts +3 -1
  93. package/src/test/build-reuse.ts +22 -7
  94. package/src/test/index.ts +7 -3
  95. package/src/ts-plugin/diagnostics.ts +56 -18
  96. package/ts-plugin.cjs +465 -131
@@ -15,6 +15,8 @@ export interface Resource<T> extends ReadonlySignal<T | undefined> {
15
15
  readonly error: ReadonlySignal<Error | undefined>
16
16
  /** Runs the fetcher again with the source as it is. */
17
17
  refetch(): void
18
+ /** Updates the resource value directly without refetching. */
19
+ mutate(next: T | undefined | ((prev: T | undefined) => T | undefined)): void
18
20
  }
19
21
 
20
22
  export interface ResourceRun {
@@ -58,11 +60,17 @@ export function resource(
58
60
  controller?.abort()
59
61
  controller = undefined
60
62
  if (input === undefined) {
61
- loading.value = false
63
+ batch(() => {
64
+ loading.value = false
65
+ error.value = undefined
66
+ })
62
67
  return
63
68
  }
64
69
  const mine = controller = new AbortController()
65
- loading.value = true
70
+ batch(() => {
71
+ loading.value = true
72
+ error.value = undefined
73
+ })
66
74
  let pending: Promise<unknown>
67
75
  try {
68
76
  pending = fetcher(input, { signal: mine.signal })
@@ -110,6 +118,12 @@ export function resource(
110
118
  peek: () => value.peek(),
111
119
  loading,
112
120
  error,
113
- refetch: () => untrack(run),
121
+ refetch: () => untrack(() => {
122
+ input = source()
123
+ run()
124
+ }),
125
+ mutate(next: unknown) {
126
+ value.value = typeof next === 'function' ? (next as (prev: unknown) => unknown)(value.peek()) : next
127
+ },
114
128
  }
115
129
  }
@@ -1,22 +1,133 @@
1
1
  // What the compiler wraps a module's `signal()` and `store()` in: the server gives it the id the page hands the browser, and the browser starts from what the server rendered.
2
2
 
3
3
  import type { Signal } from './index'
4
- import { nameSeed } from './index'
4
+ import { batch, nameSeed } from './index'
5
5
 
6
6
  declare const PRUNELLE_BROWSER: boolean | undefined
7
7
 
8
8
  type Entry = [id: string, path: string[], value?: unknown, deleted?: 1]
9
9
 
10
- // What the islands of the page say the server wrote to `id`, in page order: read when the module first runs, so a page a navigation brought in has its own.
11
- function entriesOf(id: string): Entry[] {
12
- return Array.from(document.querySelectorAll('pru-island[s]')).flatMap(el => (JSON.parse(el.getAttribute('s')!) as Entry[]).filter(e => e[0] === id))
10
+ const RESEED_KEY = Symbol.for('pru.reseed')
11
+ const registry = new Map<string, (entry: Entry) => void>()
12
+ const applied = new WeakMap<Element, Set<string>>()
13
+
14
+ let initialCache: Map<string, [Element, Entry][]> | undefined
15
+ let initialDoc: ParentNode | undefined
16
+
17
+ function markApplied(el: Element, id: string): void {
18
+ let set = applied.get(el)
19
+ if (set === undefined) {
20
+ set = new Set()
21
+ applied.set(el, set)
22
+ }
23
+ set.add(id)
24
+ }
25
+
26
+ function collectIslands(root: ParentNode): [Element, Entry[]][] {
27
+ const out: [Element, Entry[]][] = []
28
+ const els = Array.from(root.querySelectorAll('pru-island[s]'))
29
+ if ('matches' in root && typeof (root as Element).matches === 'function' && (root as Element).matches('pru-island[s]'))
30
+ els.unshift(root as Element)
31
+ for (const el of els) {
32
+ const raw = el.getAttribute('s')
33
+ if (raw)
34
+ out.push([el, JSON.parse(raw) as Entry[]])
35
+ }
36
+ return out
37
+ }
38
+
39
+ // Parse `pru-island[s]` once per initial synchronous batch on the same document.
40
+ function initialEntriesFor(id: string): [Element, Entry][] | undefined {
41
+ const doc = typeof document !== 'undefined' ? document : undefined
42
+ if (!doc)
43
+ return undefined
44
+ if (!initialCache || initialDoc !== doc) {
45
+ const map = new Map<string, [Element, Entry][]>()
46
+ for (const [el, entries] of collectIslands(doc)) {
47
+ for (const entry of entries) {
48
+ let list = map.get(entry[0])
49
+ if (list === undefined) {
50
+ list = []
51
+ map.set(entry[0], list)
52
+ }
53
+ list.push([el, entry])
54
+ }
55
+ }
56
+ initialCache = map
57
+ initialDoc = doc
58
+ queueMicrotask(() => {
59
+ if (initialCache === map)
60
+ initialCache = undefined
61
+ })
62
+ }
63
+ return initialCache.get(id)
64
+ }
65
+
66
+ function register(id: string, apply: (entry: Entry) => void): void {
67
+ ;(globalThis as unknown as Record<symbol, unknown>)[RESEED_KEY] = reseed
68
+ registry.set(id, apply)
69
+ const items = initialEntriesFor(id)
70
+ if (items) {
71
+ let ranEls: Set<Element> | undefined
72
+ for (const [el, entry] of items) {
73
+ if (applied.get(el)?.has(id))
74
+ continue
75
+ apply(entry)
76
+ ;(ranEls ??= new Set()).add(el)
77
+ }
78
+ if (ranEls !== undefined) {
79
+ for (const el of ranEls)
80
+ markApplied(el, id)
81
+ }
82
+ }
83
+ }
84
+
85
+ function applyStoreEntry(node: object, [, path, value, deleted]: Entry): void {
86
+ const at = path.slice(0, -1).reduce<unknown>((o, k) => typeof o === 'object' && o !== null && Object.hasOwn(o, k) ? (o as Record<string, unknown>)[k] : undefined, node)
87
+ const last = path.at(-1)
88
+ if (typeof at === 'object' && at !== null && last !== undefined && last !== '__proto__') {
89
+ if (deleted)
90
+ delete (at as Record<string, unknown>)[last]
91
+ else
92
+ (at as Record<string, unknown>)[last] = value
93
+ }
94
+ }
95
+
96
+ export function reseed(root?: ParentNode): void {
97
+ initialCache = undefined
98
+ const target = root ?? (typeof document !== 'undefined' ? document : undefined)
99
+ if (!target)
100
+ return
101
+ const islands = collectIslands(target)
102
+ if (islands.length === 0)
103
+ return
104
+ batch(() => {
105
+ for (const [el, entries] of islands) {
106
+ const seen = applied.get(el)
107
+ let ran: Set<string> | undefined
108
+ for (const entry of entries) {
109
+ const id = entry[0]
110
+ if (seen?.has(id))
111
+ continue
112
+ const apply = registry.get(id)
113
+ if (apply !== undefined) {
114
+ apply(entry)
115
+ ;(ran ??= new Set()).add(id)
116
+ }
117
+ }
118
+ if (ran !== undefined) {
119
+ for (const id of ran)
120
+ markApplied(el, id)
121
+ }
122
+ }
123
+ })
13
124
  }
14
125
 
15
126
  export function seedSignal<T>(node: Signal<T>, id: string, label?: string): Signal<T> {
16
127
  if (typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER)
17
128
  nameSeed(node, id, label)
18
129
  else
19
- entriesOf(id).forEach(e => node.value = e[2] as T)
130
+ register(id, e => { node.value = e[2] as T })
20
131
  return node
21
132
  }
22
133
 
@@ -24,16 +135,7 @@ export function seedSignal<T>(node: Signal<T>, id: string, label?: string): Sign
24
135
  export function seedStore<T extends object>(node: T, id: string, label?: string): T {
25
136
  if (typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER)
26
137
  nameSeed(node, id, label)
27
- else {
28
- for (const [, path, value, deleted] of entriesOf(id)) {
29
- const at = path.slice(0, -1).reduce<unknown>((o, k) => typeof o === 'object' && o !== null && Object.hasOwn(o, k) ? (o as Record<string, unknown>)[k] : undefined, node)
30
- if (typeof at === 'object' && at !== null && path.at(-1) !== '__proto__') {
31
- if (deleted)
32
- delete (at as Record<string, unknown>)[path.at(-1)!]
33
- else
34
- (at as Record<string, unknown>)[path.at(-1)!] = value
35
- }
36
- }
37
- }
138
+ else
139
+ register(id, e => applyStoreEntry(node, e))
38
140
  return node
39
141
  }
@@ -1,75 +1,113 @@
1
1
  // O(1) row selection primitive: zero WeakSet overhead per row.
2
2
 
3
- import type { Signal } from './index'
4
- import { effect, isObserving, isTracking, untrack, watchedSignal, writeCount } from './index'
3
+ import { bumpWrites, domEffect, isObserving, isTracking, SignalNode, untrack, writeCount } from './index'
5
4
 
6
5
  export interface Selector<K> {
7
6
  is(key: K): boolean
8
7
  }
9
8
 
10
- export function selector<K>(source: () => K): Selector<K> {
11
- const subs = new Map<K, Signal<boolean>>()
12
- let current: K | undefined
13
- let active = false
14
- let checked = -1
15
- let latest: K | undefined
16
- let pending: Set<K> | undefined
9
+ class SelectorSub<K> extends SignalNode<boolean> {
10
+ _m: Map<K, SelectorSub<K>>
11
+ _k: K
17
12
 
18
- const readLatest = (): K => {
19
- const w = writeCount()
20
- if (checked !== w) {
21
- latest = untrack(source)
22
- checked = w
23
- }
24
- return latest as K
13
+ constructor(initial: boolean, map: Map<K, SelectorSub<K>>, key: K) {
14
+ super(initial)
15
+ this._m = map
16
+ this._k = key
17
+ }
18
+
19
+ u(): void {
20
+ this.v++
21
+ bumpWrites()
22
+ if (this._m.get(this._k) === this)
23
+ this._m.delete(this._k)
25
24
  }
25
+ }
26
+
27
+ export function selector<T, U = T>(source: () => T, fn?: (key: U, value: T) => boolean): Selector<U> {
28
+ const subs = new Map<U, SelectorSub<U>>()
29
+ let current: T | undefined
30
+ let active = false
31
+ let checked = -1
32
+ let lastWrite = -1
33
+ let latest: T | undefined
34
+ let pending: Map<U, boolean> | undefined
35
+ const match = (key: U, value: T): boolean => fn ? fn(key, value) : Object.is(key, value)
26
36
 
27
- effect(() => {
37
+ domEffect(() => {
28
38
  const next = source()
29
39
  latest = next
30
- checked = writeCount()
31
40
  if (pending !== undefined) {
32
- for (const k of pending) {
41
+ const seen = pending
42
+ pending = undefined
43
+ for (const [k, observed] of seen) {
33
44
  const s = subs.get(k)
34
- if (s !== undefined)
35
- s.value = Object.is(next, k)
45
+ if (s === undefined)
46
+ continue
47
+ const target = match(k, next)
48
+ if (s._v !== target) {
49
+ s.value = target
50
+ }
51
+ else if (target !== observed) {
52
+ s.value = !target
53
+ s.value = target
54
+ }
36
55
  }
37
- pending = undefined
38
56
  }
39
- if (active && Object.is(current, next))
40
- return
41
-
42
- if (active) {
43
- const prev = subs.get(current as K)
44
- if (prev !== undefined)
45
- prev.value = false
57
+ if (!active || !Object.is(current, next)) {
58
+ if (fn) {
59
+ active = true
60
+ current = next
61
+ for (const [k, s] of subs)
62
+ s.value = fn(k, next)
63
+ }
64
+ else {
65
+ if (active) {
66
+ const prev = subs.get(current as unknown as U)
67
+ if (prev !== undefined)
68
+ prev.value = false
69
+ }
70
+ active = true
71
+ current = next
72
+ const nextSub = subs.get(next as unknown as U)
73
+ if (nextSub !== undefined)
74
+ nextSub.value = true
75
+ }
46
76
  }
47
-
48
- active = true
49
- current = next
50
- const nextSub = subs.get(next)
51
- if (nextSub !== undefined)
52
- nextSub.value = true
77
+ lastWrite = checked = writeCount()
53
78
  })
54
79
 
55
80
  return {
56
- is(key: K): boolean {
81
+ is(key: U): boolean {
57
82
  if (!active)
58
- return Object.is(source(), key)
59
- if (!isObserving())
60
- return Object.is(isTracking() ? source() : readLatest(), key)
83
+ return match(key, source())
84
+ const w = writeCount()
85
+ if (!isObserving()) {
86
+ if (isTracking())
87
+ return match(key, source())
88
+ if (checked !== w) {
89
+ latest = untrack(source)
90
+ checked = w
91
+ }
92
+ return match(key, latest as T)
93
+ }
61
94
 
62
- const val = readLatest()
63
- const matched = Object.is(val, key)
95
+ if (checked !== w) {
96
+ latest = untrack(source)
97
+ checked = w
98
+ }
99
+ const val = latest as T
100
+ const matched = match(key, val)
64
101
  let sub = subs.get(key)
102
+ const unflushed = w !== lastWrite
65
103
  if (sub === undefined) {
66
- sub = watchedSignal(matched, () => {
67
- if (subs.get(key) === sub)
68
- subs.delete(key)
69
- })
104
+ sub = new SelectorSub(matched, subs, key)
70
105
  subs.set(key, sub)
71
- if (!Object.is(current, val))
72
- (pending ??= new Set()).add(key)
106
+ if (unflushed && !Object.is(current, val))
107
+ (pending ??= new Map()).set(key, matched)
108
+ }
109
+ else if (unflushed && sub._v !== matched) {
110
+ (pending ??= new Map()).set(key, matched)
73
111
  }
74
112
  sub.value
75
113
  return matched
@@ -44,7 +44,7 @@ function replay(ops: readonly SharedWrite[], to: number, step: 'redo' | 'undo'):
44
44
  * Internal: runs an island's server render, during which `effect` and `onMount` do nothing.
45
45
  * The writes the request's earlier islands made to module state are applied first and the ones this render makes are undone after it, so the next request sees the module's own value.
46
46
  */
47
- export function renderOnServer<T>(fn: () => T, seeds?: RequestSeeds): T {
47
+ export function renderOnServer<T>(fn: () => T, seeds?: RequestSeeds, holder?: { seeds: RequestSeeds | undefined }): T {
48
48
  if (serverRenders > 0) {
49
49
  serverRenders++
50
50
  try {
@@ -54,37 +54,45 @@ export function renderOnServer<T>(fn: () => T, seeds?: RequestSeeds): T {
54
54
  serverRenders--
55
55
  }
56
56
  }
57
- const mine = seeds ?? { ops: [], out: undefined }
58
- const start = mine.ops.length
59
- mine.out = undefined
57
+ const start = seeds !== undefined ? seeds.ops.length : 0
58
+ if (seeds !== undefined)
59
+ seeds.out = undefined
60
60
  serverRenders++
61
- seedsInUse = mine
61
+ seedsInUse = seeds
62
62
  let ok = false
63
63
  try {
64
- replay(mine.ops, start, 'redo')
64
+ if (seeds !== undefined && start > 0)
65
+ replay(seeds.ops, start, 'redo')
65
66
  const result = fn()
66
67
  ok = true
67
68
  return result
68
69
  }
69
70
  finally {
70
71
  serverRenders--
72
+ const mine = seedsInUse
71
73
  seedsInUse = undefined
72
- const end = mine.ops.length
73
- if (ok && end > start) {
74
- const out = new Map<string, unknown[]>()
75
- for (let i = start; i < end; i++) {
76
- out.delete(mine.ops[i]!.key)
77
- out.set(mine.ops[i]!.key, mine.ops[i]!.entry)
74
+ if (mine !== undefined) {
75
+ if (seeds === undefined && holder !== undefined)
76
+ holder.seeds = mine
77
+ const end = mine.ops.length
78
+ if (ok && end > start) {
79
+ const out = new Map<string, unknown[]>()
80
+ for (let i = start; i < end; i++) {
81
+ out.delete(mine.ops[i]!.key)
82
+ out.set(mine.ops[i]!.key, mine.ops[i]!.entry)
83
+ }
84
+ mine.out = [...out.values()]
85
+ }
86
+ // Every write undone in reverse leaves each place on its first value, also when a redo above threw halfway.
87
+ if (end > 0) {
88
+ try {
89
+ replay(mine.ops, end, 'undo')
90
+ }
91
+ finally {
92
+ if (!ok)
93
+ mine.ops.length = start
94
+ }
78
95
  }
79
- mine.out = [...out.values()]
80
- }
81
- // Every write undone in reverse leaves each place on its first value, also when a redo above threw halfway.
82
- try {
83
- replay(mine.ops, end, 'undo')
84
- }
85
- finally {
86
- if (!ok)
87
- mine.ops.length = start
88
96
  }
89
97
  }
90
98
  }
@@ -177,7 +185,7 @@ function carry(value: unknown, at: string, owner: string, up: object[] = []): un
177
185
  */
178
186
  export function recordSharedWrite(owner: object, path: readonly PropertyKey[], redo: () => void, undo: () => void, value: unknown, deleted = false): void {
179
187
  const named = seedIds.get(owner)
180
- if (named === undefined || seedsInUse === undefined)
188
+ if (named === undefined || serverRenders === 0)
181
189
  throw new Error('prunelle: an island wrote a signal or store made outside its own render, and prunelle cannot hand its value to the browser, while it rendered on the server, where the next visitor could see the value. Make it with `const cart = signal(0)` or `store({ … })` at a module\'s top level (not in load() or another function that runs per request), write it in onMount() or an event handler, or make the signal inside the component.')
182
190
  const [file, name = named.label] = named.label.split('#') as [string, string?]
183
191
  const owned = name === named.label ? name : `${name} in ${file}`
@@ -189,6 +197,6 @@ export function recordSharedWrite(owner: object, path: readonly PropertyKey[], r
189
197
  return k
190
198
  })
191
199
  const entry = deleted ? [named.id, keys, 0, 1] : value === undefined ? [named.id, keys] : [named.id, keys, carry(value, '', owned)]
192
- seedsInUse.ops.push({ key: JSON.stringify([named.id, keys]), entry, redo, undo })
200
+ ;(seedsInUse ??= { ops: [], out: undefined }).ops.push({ key: JSON.stringify([named.id, keys]), entry, redo, undo })
193
201
  }
194
202