prunelle 0.8.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.
- package/llms.txt +68 -51
- package/package.json +7 -2
- package/src/a11y/declaration.ts +10 -17
- package/src/a11y/index.ts +69 -125
- package/src/animate.ts +127 -0
- package/src/build/asides.ts +51 -0
- package/src/build/bundle.ts +94 -136
- package/src/build/client-entries.ts +6 -6
- package/src/build/client-outputs.ts +75 -0
- package/src/build/client-plugin.ts +9 -4
- package/src/build/ecoindex.ts +4 -2
- package/src/build/files.ts +18 -0
- package/src/build/graph.ts +14 -6
- package/src/build/index.ts +23 -11
- package/src/build/island-promote.ts +42 -2
- package/src/build/islands.ts +63 -9
- package/src/build/key-types.ts +31 -0
- package/src/build/manifest.ts +117 -1
- package/src/build/plugin-host.ts +13 -1
- package/src/build/prerender.ts +8 -3
- package/src/build/reach.ts +18 -4
- package/src/build/report.ts +1 -1
- package/src/build/resolve.ts +5 -1
- package/src/build/route-types.ts +16 -0
- package/src/build/routes.ts +42 -15
- package/src/build/seed-ids.ts +65 -0
- package/src/build/seed-writes.ts +111 -0
- package/src/build/static-headers.ts +2 -2
- package/src/build/static.ts +1 -1
- package/src/cli/check-cache.ts +51 -0
- package/src/cli/check-consent.ts +95 -0
- package/src/cli/check-forms.ts +10 -2
- package/src/cli/check-links-patterns.ts +101 -0
- package/src/cli/check-links.ts +30 -105
- package/src/cli/check-regions.ts +185 -0
- package/src/cli/check-security.ts +50 -6
- package/src/cli/check.ts +49 -21
- package/src/cli/commands.ts +138 -16
- package/src/cli/docs.ts +21 -2
- package/src/cli/form-opaque-children.ts +40 -0
- package/src/cli/index.ts +1 -1
- package/src/cli/inspect.ts +2 -0
- package/src/cli/mcp-tools.ts +2 -2
- package/src/cli/new.ts +9 -5
- package/src/cli/upgrade-names.ts +14 -2
- package/src/cli/upgrade.ts +3 -0
- package/src/compiler/analyze.ts +22 -16
- package/src/compiler/cacheability-private.ts +13 -3
- package/src/compiler/cacheability.ts +24 -2
- package/src/compiler/compliance-contrast.ts +322 -0
- package/src/compiler/compliance-empty.ts +24 -0
- package/src/compiler/compliance-rgaa-content.ts +95 -0
- package/src/compiler/compliance-rgaa-criteria.ts +45 -0
- package/src/compiler/compliance-rgaa-pages.ts +140 -0
- package/src/compiler/compliance-rgaa.ts +19 -0
- package/src/compiler/compliance-rules.ts +72 -0
- package/src/compiler/compliance-third-party.ts +59 -0
- package/src/compiler/compliance-urls.ts +52 -0
- package/src/compiler/compliance.ts +61 -93
- package/src/compiler/cookie-inventory.ts +119 -0
- package/src/compiler/dom-attrs.ts +178 -22
- package/src/compiler/dom.ts +31 -44
- package/src/compiler/emit.ts +17 -6
- package/src/compiler/expr-context.ts +260 -207
- package/src/compiler/foreign-imports.ts +5 -3
- package/src/compiler/mistakes.ts +18 -9
- package/src/compiler/props-body.ts +263 -21
- package/src/compiler/props-pattern.ts +211 -0
- package/src/compiler/props.ts +324 -343
- package/src/compiler/signal-reads.ts +142 -29
- package/src/compiler/spread-handlers.ts +25 -13
- package/src/compiler/ssr-emit.ts +510 -0
- package/src/compiler/ssr-helpers.ts +165 -121
- package/src/compiler/ssr.ts +28 -441
- package/src/context.ts +14 -1
- package/src/dev/child.ts +1 -1
- package/src/dev/host-check.ts +2 -1
- package/src/dev/hot-islands.ts +71 -0
- package/src/dev/index.ts +31 -18
- package/src/dev/protocol.ts +1 -1
- package/src/dev/queue.ts +3 -2
- package/src/dev/reload.ts +5 -2
- package/src/dev/supervisor.ts +3 -1
- package/src/dev/watchers.ts +6 -2
- package/src/dom/action.ts +97 -14
- package/src/dom/attrs.ts +93 -25
- package/src/dom/extras.ts +91 -50
- package/src/dom/field-marks.ts +32 -0
- package/src/dom/hot.ts +9 -0
- package/src/dom/index.ts +44 -18
- package/src/dom/island-hot.ts +29 -0
- package/src/dom/island.ts +13 -2
- package/src/dom/loop.ts +259 -188
- package/src/dom/prefetch.ts +135 -0
- package/src/dom/regions.ts +515 -0
- package/src/dom/swap.ts +246 -0
- package/src/dom/trusted.ts +22 -0
- package/src/dom/upload.ts +44 -0
- package/src/eco/declaration.ts +44 -41
- package/src/eco/index.ts +8 -5
- package/src/href.ts +31 -0
- package/src/index.ts +12 -5
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +7 -1
- package/src/plugins/daily.ts +27 -0
- package/src/plugins/index.ts +3 -3
- package/src/plugins/order.ts +1 -1
- package/src/plugins/public.ts +12 -5
- package/src/plugins/services.ts +88 -14
- package/src/plugins/types.ts +128 -10
- package/src/reactivity/graph.ts +244 -41
- package/src/reactivity/hmr.ts +101 -0
- package/src/reactivity/index.ts +243 -239
- package/src/reactivity/resource.ts +129 -0
- package/src/reactivity/seed.ts +141 -0
- package/src/reactivity/selector.ts +85 -47
- package/src/reactivity/server-seed.ts +202 -0
- package/src/reactivity/store.ts +331 -97
- package/src/rgpd/draft.ts +171 -0
- package/src/rgpd/forms.ts +85 -0
- package/src/rgpd/index.ts +57 -0
- package/src/server/action-flash.ts +1 -0
- package/src/server/action-protect.ts +3 -0
- package/src/server/action-registry.ts +11 -9
- package/src/server/action.ts +51 -19
- package/src/server/app-secret.ts +14 -8
- package/src/server/cache-invalidation.ts +97 -0
- package/src/server/compress.ts +55 -18
- package/src/server/consent.ts +21 -5
- package/src/server/cookie-inventory.ts +78 -0
- package/src/server/cookies.ts +77 -8
- package/src/server/cors.ts +98 -0
- package/src/server/csp-report.ts +90 -0
- package/src/server/discard.ts +13 -0
- package/src/server/document.ts +2 -2
- package/src/server/env.ts +1 -1
- package/src/server/errors.ts +9 -2
- package/src/server/field-errors.ts +91 -0
- package/src/server/form-schema.ts +16 -11
- package/src/server/health.ts +41 -11
- package/src/server/http.ts +28 -11
- package/src/server/index.ts +122 -307
- package/src/server/loader.ts +2 -0
- package/src/server/page.ts +3 -9
- package/src/server/plugin-host.ts +66 -16
- package/src/server/plugin-route-shadow.ts +24 -0
- package/src/server/public.ts +14 -3
- package/src/server/rate-limit.ts +49 -10
- package/src/server/region.ts +22 -2
- package/src/server/request-handler.ts +406 -0
- package/src/server/request-log.ts +94 -0
- package/src/server/route-cache.ts +55 -8
- package/src/server/route-input.ts +54 -0
- package/src/server/route-table.ts +53 -27
- package/src/server/router.ts +2 -1
- package/src/server/routing.ts +104 -45
- package/src/server/sanitize-html.ts +4 -2
- package/src/server/security.ts +38 -8
- package/src/server/serve-handlers.ts +16 -6
- package/src/server/served-file.ts +19 -14
- package/src/server/shared-store.ts +323 -0
- package/src/server/start.ts +12 -5
- package/src/server/static-dir.ts +34 -12
- package/src/server/stream.ts +2 -1
- package/src/server/surface.ts +39 -0
- package/src/server/webhook.ts +50 -0
- package/src/server/workers.ts +168 -0
- package/src/ssr/consent-banner.ts +20 -16
- package/src/ssr/consent-texts.ts +40 -0
- package/src/ssr/context.ts +174 -53
- package/src/ssr/cookie-table.ts +75 -0
- package/src/ssr/index.ts +63 -58
- package/src/ssr/navigation.ts +161 -14
- package/src/ssr/region.ts +9 -3
- package/src/style.ts +12 -5
- package/src/test/browser.ts +53 -23
- package/src/test/build-reuse.ts +85 -0
- package/src/test/index.ts +51 -8
- package/src/test/mount.ts +53 -0
- package/src/test/outside.ts +94 -0
- package/src/ts-plugin/diagnostics.ts +56 -18
- package/templates/_shared/AGENTS.md +1 -1
- package/templates/_shared/Dockerfile +22 -0
- package/templates/_shared/dockerignore +9 -0
- package/templates/_shared/gitignore +2 -0
- package/templates/app/.env.example +3 -1
- package/templates/app/compose.yaml +32 -0
- package/templates/app/db/migrations/001_notes.sql +7 -0
- package/templates/app/db/schema.ts +11 -0
- package/templates/app/package.json +1 -0
- package/templates/app/prunelle.config.ts +6 -0
- package/templates/app/src/actions/notes.ts +26 -0
- package/templates/app/src/auth.ts +4 -2
- package/templates/app/src/db.ts +5 -0
- package/templates/app/src/routes/dashboard/index.tsx +26 -1
- package/templates/app/src/routes/privacy.tsx +6 -2
- package/templates/app/tests/app.test.ts +14 -3
- package/templates/plugin/AGENTS.md +11 -0
- package/templates/plugin/CLAUDE.md +1 -0
- package/templates/plugin/README.md +27 -0
- package/templates/plugin/gitignore +2 -0
- package/templates/plugin/llms.txt +19 -0
- package/templates/plugin/package.json +33 -0
- package/templates/plugin/src/index.ts +40 -0
- package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
- package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
- package/templates/plugin/tests/plugin.test.ts +42 -0
- package/templates/plugin/tsconfig.json +19 -0
- package/ts-plugin.cjs +4058 -2246
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// `resource()`: async data in an island, fetched again when its source changes, the run it replaces aborted.
|
|
2
|
+
|
|
3
|
+
import type { ActionResult } from '../server/action'
|
|
4
|
+
import type { ReadonlySignal } from './graph'
|
|
5
|
+
import { batch, effect, signal, untrack } from './index'
|
|
6
|
+
|
|
7
|
+
export interface ResourceOptions<T> {
|
|
8
|
+
/** The value to start from, such as one `load()` gave the page: the server renders it, and the browser fetches only once the source changes. */
|
|
9
|
+
initial?: T | undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Reads as the last value fetched, `undefined` until one arrives. */
|
|
13
|
+
export interface Resource<T> extends ReadonlySignal<T | undefined> {
|
|
14
|
+
readonly loading: ReadonlySignal<boolean>
|
|
15
|
+
readonly error: ReadonlySignal<Error | undefined>
|
|
16
|
+
/** Runs the fetcher again with the source as it is. */
|
|
17
|
+
refetch(): void
|
|
18
|
+
/** Updates the resource value directly without refetching. */
|
|
19
|
+
mutate(next: T | undefined | ((prev: T | undefined) => T | undefined)): void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ResourceRun {
|
|
23
|
+
/** Aborted once the run is replaced or the island goes away: pass it to `fetch`. */
|
|
24
|
+
readonly signal: AbortSignal
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type ActionCall<Input, Output> = ((input: Input, run?: ResourceRun) => Promise<ActionResult<Output>>) & { readonly url: string }
|
|
28
|
+
|
|
29
|
+
export function resource<T>(fetcher: (run: ResourceRun) => Promise<T>, options?: ResourceOptions<T>): Resource<T>
|
|
30
|
+
export function resource<S, Output>(source: () => S | undefined, action: ActionCall<S, Output>, options?: ResourceOptions<Output>): Resource<Output>
|
|
31
|
+
export function resource<S, T>(source: () => S | undefined, fetcher: (source: S, run: ResourceRun) => Promise<T>, options?: ResourceOptions<T>): Resource<T>
|
|
32
|
+
export function resource(
|
|
33
|
+
first: (() => unknown) | ((run: ResourceRun) => Promise<unknown>),
|
|
34
|
+
second?: ((source: unknown, run: ResourceRun) => Promise<unknown>) | ResourceOptions<unknown>,
|
|
35
|
+
third?: ResourceOptions<unknown>,
|
|
36
|
+
): Resource<unknown> {
|
|
37
|
+
const alone = typeof second !== 'function'
|
|
38
|
+
// A fetcher given alone has no source: it runs once, and what it reads is not tracked.
|
|
39
|
+
const source = alone ? () => true : first as () => unknown
|
|
40
|
+
const fetcher = alone ? (_: unknown, run: ResourceRun) => (first as (run: ResourceRun) => Promise<unknown>)(run) : second
|
|
41
|
+
const options = alone ? second : third
|
|
42
|
+
const isAction = typeof (fetcher as { url?: unknown }).url === 'string'
|
|
43
|
+
const value = signal(options?.initial)
|
|
44
|
+
const loading = signal(false)
|
|
45
|
+
const error = signal<Error | undefined>(undefined)
|
|
46
|
+
let controller: AbortController | undefined
|
|
47
|
+
let input: unknown
|
|
48
|
+
|
|
49
|
+
function settle(mine: AbortController, apply: () => void): void {
|
|
50
|
+
if (controller !== mine)
|
|
51
|
+
return
|
|
52
|
+
controller = undefined
|
|
53
|
+
batch(() => {
|
|
54
|
+
apply()
|
|
55
|
+
loading.value = false
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function run(): void {
|
|
60
|
+
controller?.abort()
|
|
61
|
+
controller = undefined
|
|
62
|
+
if (input === undefined) {
|
|
63
|
+
batch(() => {
|
|
64
|
+
loading.value = false
|
|
65
|
+
error.value = undefined
|
|
66
|
+
})
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
const mine = controller = new AbortController()
|
|
70
|
+
batch(() => {
|
|
71
|
+
loading.value = true
|
|
72
|
+
error.value = undefined
|
|
73
|
+
})
|
|
74
|
+
let pending: Promise<unknown>
|
|
75
|
+
try {
|
|
76
|
+
pending = fetcher(input, { signal: mine.signal })
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
pending = Promise.reject(err)
|
|
80
|
+
}
|
|
81
|
+
pending.then((result) => {
|
|
82
|
+
settle(mine, () => {
|
|
83
|
+
const failed = isAction && !(result as ActionResult<unknown>).ok
|
|
84
|
+
error.value = failed ? new Error((result as Extract<ActionResult<unknown>, { ok: false }>).error) : undefined
|
|
85
|
+
if (!failed)
|
|
86
|
+
value.value = isAction ? (result as Extract<ActionResult<unknown>, { ok: true }>).data : result
|
|
87
|
+
})
|
|
88
|
+
}, (err: unknown) => {
|
|
89
|
+
settle(mine, () => {
|
|
90
|
+
error.value = err instanceof Error ? err : new Error(String(err))
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The value given stands for the first run, so the browser does not fetch what the server already rendered.
|
|
96
|
+
let given = options?.initial !== undefined
|
|
97
|
+
let ran = false
|
|
98
|
+
effect(() => {
|
|
99
|
+
ran = true
|
|
100
|
+
input = source()
|
|
101
|
+
const skip = given
|
|
102
|
+
given = false
|
|
103
|
+
if (!skip)
|
|
104
|
+
untrack(run)
|
|
105
|
+
return () => {
|
|
106
|
+
controller?.abort()
|
|
107
|
+
controller = undefined
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
// During SSR, `effect()` is skipped: reflect `loading` only when the resource has a defined source and no initial value.
|
|
111
|
+
if (!ran && options?.initial === undefined && (alone || untrack(source) !== undefined))
|
|
112
|
+
loading.value = true
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
get value() {
|
|
116
|
+
return value.value
|
|
117
|
+
},
|
|
118
|
+
peek: () => value.peek(),
|
|
119
|
+
loading,
|
|
120
|
+
error,
|
|
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
|
+
},
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
|
|
3
|
+
import type { Signal } from './index'
|
|
4
|
+
import { batch, nameSeed } from './index'
|
|
5
|
+
|
|
6
|
+
declare const PRUNELLE_BROWSER: boolean | undefined
|
|
7
|
+
|
|
8
|
+
type Entry = [id: string, path: string[], value?: unknown, deleted?: 1]
|
|
9
|
+
|
|
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
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function seedSignal<T>(node: Signal<T>, id: string, label?: string): Signal<T> {
|
|
127
|
+
if (typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER)
|
|
128
|
+
nameSeed(node, id, label)
|
|
129
|
+
else
|
|
130
|
+
register(id, e => { node.value = e[2] as T })
|
|
131
|
+
return node
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Each written key is set where it was, so what the browser's store holds that the server's copy had changed since (state `load()` made) stays. The walk takes own keys only and never sets `__proto__`, so no path the page carries reaches a prototype (#375).
|
|
135
|
+
export function seedStore<T extends object>(node: T, id: string, label?: string): T {
|
|
136
|
+
if (typeof PRUNELLE_BROWSER === 'undefined' || !PRUNELLE_BROWSER)
|
|
137
|
+
nameSeed(node, id, label)
|
|
138
|
+
else
|
|
139
|
+
register(id, e => applyStoreEntry(node, e))
|
|
140
|
+
return node
|
|
141
|
+
}
|
|
@@ -1,75 +1,113 @@
|
|
|
1
1
|
// O(1) row selection primitive: zero WeakSet overhead per row.
|
|
2
2
|
|
|
3
|
-
import
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
37
|
+
domEffect(() => {
|
|
28
38
|
const next = source()
|
|
29
39
|
latest = next
|
|
30
|
-
checked = writeCount()
|
|
31
40
|
if (pending !== undefined) {
|
|
32
|
-
|
|
41
|
+
const seen = pending
|
|
42
|
+
pending = undefined
|
|
43
|
+
for (const [k, observed] of seen) {
|
|
33
44
|
const s = subs.get(k)
|
|
34
|
-
if (s
|
|
35
|
-
|
|
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
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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:
|
|
81
|
+
is(key: U): boolean {
|
|
57
82
|
if (!active)
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
63
|
-
|
|
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 =
|
|
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
|
|
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
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// What an island's server render shares with the request around it: the module state it may write, kept per request and handed to the browser.
|
|
2
|
+
|
|
3
|
+
let serverRenders = 0
|
|
4
|
+
let replaying = false
|
|
5
|
+
|
|
6
|
+
/** Internal, server only: an island is rendering, where `effect` and `onMount` do nothing. */
|
|
7
|
+
export function inServerRender(): boolean {
|
|
8
|
+
return serverRenders > 0
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Internal, server only: what the islands of one request wrote to state every request shares, in order, so each island renders with its predecessors' writes. */
|
|
12
|
+
export interface RequestSeeds {
|
|
13
|
+
ops: SharedWrite[]
|
|
14
|
+
/** What the island that rendered last wrote, for the browser: `[id, path, value]`, `[id, path]` for undefined, `[id, path, 0, 1]` for a deleted key. */
|
|
15
|
+
out: unknown[][] | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface SharedWrite {
|
|
19
|
+
/** The signal or store and the path in it, which a later write of the same place replaces in `out`. */
|
|
20
|
+
key: string
|
|
21
|
+
entry: unknown[]
|
|
22
|
+
redo: () => void
|
|
23
|
+
undo: () => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function replay(ops: readonly SharedWrite[], to: number, step: 'redo' | 'undo'): void {
|
|
27
|
+
replaying = true
|
|
28
|
+
try {
|
|
29
|
+
if (step === 'redo') {
|
|
30
|
+
for (let i = 0; i < to; i++)
|
|
31
|
+
ops[i]!.redo()
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
for (let i = to - 1; i >= 0; i--)
|
|
35
|
+
ops[i]!.undo()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
replaying = false
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Internal: runs an island's server render, during which `effect` and `onMount` do nothing.
|
|
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
|
+
*/
|
|
47
|
+
export function renderOnServer<T>(fn: () => T, seeds?: RequestSeeds, holder?: { seeds: RequestSeeds | undefined }): T {
|
|
48
|
+
if (serverRenders > 0) {
|
|
49
|
+
serverRenders++
|
|
50
|
+
try {
|
|
51
|
+
return fn()
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
serverRenders--
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const start = seeds !== undefined ? seeds.ops.length : 0
|
|
58
|
+
if (seeds !== undefined)
|
|
59
|
+
seeds.out = undefined
|
|
60
|
+
serverRenders++
|
|
61
|
+
seedsInUse = seeds
|
|
62
|
+
let ok = false
|
|
63
|
+
try {
|
|
64
|
+
if (seeds !== undefined && start > 0)
|
|
65
|
+
replay(seeds.ops, start, 'redo')
|
|
66
|
+
const result = fn()
|
|
67
|
+
ok = true
|
|
68
|
+
return result
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
serverRenders--
|
|
72
|
+
const mine = seedsInUse
|
|
73
|
+
seedsInUse = undefined
|
|
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
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let seedsInUse: RequestSeeds | undefined
|
|
101
|
+
// The signals and store roots the compiler named: the id the browser seeds each by, and `file#name` for errors.
|
|
102
|
+
const seedIds = new WeakMap<object, { id: string, label: string }>()
|
|
103
|
+
|
|
104
|
+
/** Internal, server only: gives a module-level signal or store the id the browser seeds it by. */
|
|
105
|
+
export function nameSeed(node: object, id: string, label = id): void {
|
|
106
|
+
seedIds.set(node, { id, label })
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// On the server, the signals and store objects made outside an island's render: at a module's top level, where every request of the process reads the same one.
|
|
110
|
+
const shared = new WeakSet<object>()
|
|
111
|
+
// A store object inside a shared store, to the object and key it was last read from.
|
|
112
|
+
const parents = new WeakMap<object, { parent: object, key: PropertyKey }>()
|
|
113
|
+
|
|
114
|
+
/** Internal, server only: marks `node` as shared by every request when it is made outside an island's render, or sits in a store object that is. */
|
|
115
|
+
export function shareOutsideRender(node: object, parent?: object, key?: PropertyKey): void {
|
|
116
|
+
if (parent !== undefined && shared.has(parent)) {
|
|
117
|
+
shared.add(node)
|
|
118
|
+
const at = parents.get(node)
|
|
119
|
+
if (node !== parent && (at === undefined || at.parent !== parent || at.key !== key))
|
|
120
|
+
parents.set(node, { parent, key: key! })
|
|
121
|
+
}
|
|
122
|
+
else if (serverRenders === 0) {
|
|
123
|
+
shared.add(node)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Internal, server only: the root store object `node` hangs from, and the keys down to it. */
|
|
128
|
+
export function sharedPath(node: object): { root: object, path: PropertyKey[] } {
|
|
129
|
+
const path: PropertyKey[] = []
|
|
130
|
+
let root = node
|
|
131
|
+
for (let at = parents.get(root); at !== undefined && path.length < 256; at = parents.get(root)) {
|
|
132
|
+
path.push(at.key)
|
|
133
|
+
root = at.parent
|
|
134
|
+
}
|
|
135
|
+
return { root, path: path.reverse() }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Internal, server only: true when an island renders on the server and `node` is state every request shares. */
|
|
139
|
+
export function isSharedWrite(node: object): boolean {
|
|
140
|
+
return serverRenders > 0 && !replaying && shared.has(node)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function describe(value: unknown): string {
|
|
144
|
+
switch (typeof value) {
|
|
145
|
+
case 'bigint': return 'a BigInt'
|
|
146
|
+
case 'function': return 'a function'
|
|
147
|
+
case 'symbol': return 'a symbol'
|
|
148
|
+
case 'number': return String(value)
|
|
149
|
+
case 'object': return `a ${Object.getPrototypeOf(value)?.constructor?.name ?? 'object'}`
|
|
150
|
+
default: return 'undefined'
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A copy of `value` the browser can be handed as JSON; throws naming the first thing in it that JSON cannot carry. */
|
|
155
|
+
function carry(value: unknown, at: string, owner: string, up: object[] = []): unknown {
|
|
156
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value)))
|
|
157
|
+
return value
|
|
158
|
+
let what = describe(value)
|
|
159
|
+
if (typeof value === 'object') {
|
|
160
|
+
const proto = Object.getPrototypeOf(value)
|
|
161
|
+
if (up.includes(value)) {
|
|
162
|
+
what = 'a cycle'
|
|
163
|
+
}
|
|
164
|
+
else if (Array.isArray(value)) {
|
|
165
|
+
up.push(value)
|
|
166
|
+
const copy = value.map((item, i) => carry(item, `${at}.${i}`, owner, up))
|
|
167
|
+
up.pop()
|
|
168
|
+
return copy
|
|
169
|
+
}
|
|
170
|
+
else if (proto === Object.prototype || proto === null) {
|
|
171
|
+
up.push(value)
|
|
172
|
+
const copy: Record<string, unknown> = {}
|
|
173
|
+
for (const [k, v] of Object.entries(value))
|
|
174
|
+
copy[k] = carry(v, `${at}.${k}`, owner, up)
|
|
175
|
+
up.pop()
|
|
176
|
+
return copy
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
throw new Error(`prunelle: an island wrote ${what} to ${owner}${at === '' ? '' : ` (at ${at.slice(1)})`} while it rendered on the server, and ${what} cannot reach the browser as JSON. Write a number, string, boolean, null, plain object or array (a BigInt as a string, a Date as its ISO string), or write it in onMount() or an event handler.`)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Internal, server only: an island's write to state every request shares stays in this request, as the writes undone after the render, and reaches the browser by the id of `owner`.
|
|
184
|
+
* `owner` is the signal or the store's root proxy, `path` the keys down to what was written, `value` what it became and `deleted` a key removed.
|
|
185
|
+
*/
|
|
186
|
+
export function recordSharedWrite(owner: object, path: readonly PropertyKey[], redo: () => void, undo: () => void, value: unknown, deleted = false): void {
|
|
187
|
+
const named = seedIds.get(owner)
|
|
188
|
+
if (named === undefined || serverRenders === 0)
|
|
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.')
|
|
190
|
+
const [file, name = named.label] = named.label.split('#') as [string, string?]
|
|
191
|
+
const owned = name === named.label ? name : `${name} in ${file}`
|
|
192
|
+
const keys = path.map((k) => {
|
|
193
|
+
if (typeof k === 'symbol')
|
|
194
|
+
throw new Error(`prunelle: an island wrote a symbol key of ${owned} while it rendered on the server, and a symbol key cannot reach the browser as JSON. Use string keys, or write it in onMount() or an event handler.`)
|
|
195
|
+
if (k === '__proto__')
|
|
196
|
+
throw new Error(`prunelle: an island wrote the key __proto__ of ${owned} while it rendered on the server, which would change a prototype in the browser. Prefix keys that come from the request (\`user:\${name}\`), keep them in a Map, or write it in onMount() or an event handler.`)
|
|
197
|
+
return k
|
|
198
|
+
})
|
|
199
|
+
const entry = deleted ? [named.id, keys, 0, 1] : value === undefined ? [named.id, keys] : [named.id, keys, carry(value, '', owned)]
|
|
200
|
+
;(seedsInUse ??= { ops: [], out: undefined }).ops.push({ key: JSON.stringify([named.id, keys]), entry, redo, undo })
|
|
201
|
+
}
|
|
202
|
+
|