prunelle 0.1.1 → 0.5.3
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 +23 -19
- package/package.json +8 -2
- package/src/a11y/declaration.ts +335 -0
- package/src/a11y/index.ts +446 -0
- package/src/a11y/rgaa-map.ts +184 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +80 -36
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +8 -4
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +11 -9
- package/src/build/ecoindex.ts +229 -0
- package/src/build/index.ts +174 -108
- package/src/build/islands.ts +2 -2
- package/src/build/manifest.ts +66 -5
- package/src/build/output.ts +2 -2
- package/src/build/plugin-host.ts +91 -33
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +128 -46
- package/src/build/report.ts +23 -2
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static.ts +137 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +561 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check.ts +75 -20
- package/src/cli/commands.ts +85 -4
- package/src/cli/docs.ts +402 -0
- package/src/cli/inspect.ts +103 -14
- package/src/cli/mcp-tools.ts +490 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +2 -2
- package/src/cli/render.ts +421 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/analyze.ts +42 -9
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability.ts +636 -0
- package/src/compiler/compliance-rgaa.ts +508 -0
- package/src/compiler/compliance.ts +241 -289
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +167 -57
- package/src/compiler/mistakes.ts +4 -1
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +40 -12
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +218 -87
- package/src/dev/error-log.ts +151 -0
- package/src/dev/index.ts +84 -34
- package/src/dev/open-editor.ts +204 -0
- package/src/dev/protocol.ts +1 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +42 -25
- package/src/dev/watchers.ts +47 -11
- package/src/dom/action.ts +43 -20
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +28 -12
- package/src/dom/index.ts +26 -8
- package/src/dom/island.ts +35 -28
- package/src/dom/loop.ts +10 -9
- package/src/plugins/config.ts +8 -7
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +26 -7
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +5 -2
- package/src/plugins/services.ts +62 -0
- package/src/plugins/types.ts +32 -0
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +45 -13
- package/src/server/action-protect.ts +198 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +82 -27
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +91 -9
- package/src/server/document.ts +25 -10
- package/src/server/errors.ts +303 -65
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +128 -31
- package/src/server/index.ts +230 -260
- package/src/server/loader.ts +5 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +123 -88
- package/src/server/public.ts +3 -3
- package/src/server/rate-limit.ts +58 -35
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +423 -0
- package/src/server/route-table.ts +339 -8
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +196 -63
- package/src/server/security.ts +208 -32
- package/src/server/serve-handlers.ts +21 -7
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +61 -8
- package/src/server/stream.ts +41 -22
- package/src/ssr/context.ts +85 -3
- package/src/ssr/index.ts +130 -3
- package/src/ssr/region.ts +31 -19
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
- package/ts-plugin.cjs +4277 -0
package/src/dev/watchers.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import type { FSWatcher } from 'node:fs'
|
|
4
4
|
import type { loadConfig } from '../plugins/index'
|
|
5
5
|
import { existsSync, readFileSync, realpathSync, statSync, watch } from 'node:fs'
|
|
6
|
-
import { dirname, isAbsolute, join, resolve } from 'node:path'
|
|
6
|
+
import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
|
|
7
7
|
import { isInNodeModules } from '../build/files'
|
|
8
8
|
import { isInside } from '../build/is-inside'
|
|
9
9
|
import { createAppContext, resolvePlugins, runWithAppContext } from '../plugins/index'
|
|
@@ -54,6 +54,33 @@ export function localImports(entry: string): string[] {
|
|
|
54
54
|
|
|
55
55
|
const isInsideDir = (parent: string, target: string): boolean => isInside(parent, target, { orSame: true })
|
|
56
56
|
|
|
57
|
+
/** Watches files through their parent directory (filtering by basename) so atomic rename saves on Linux keep firing. */
|
|
58
|
+
function watchFilesByParentDir(files: Iterable<string>, onMatch: (filename: string) => void, out: FSWatcher[]): void {
|
|
59
|
+
const byDir = new Map<string, Set<string>>()
|
|
60
|
+
for (const file of files) {
|
|
61
|
+
const dir = dirname(file)
|
|
62
|
+
if (!existsSync(dir))
|
|
63
|
+
continue
|
|
64
|
+
let names = byDir.get(dir)
|
|
65
|
+
if (names === undefined) {
|
|
66
|
+
names = new Set()
|
|
67
|
+
byDir.set(dir, names)
|
|
68
|
+
}
|
|
69
|
+
names.add(basename(file))
|
|
70
|
+
}
|
|
71
|
+
for (const [dir, names] of byDir) {
|
|
72
|
+
try {
|
|
73
|
+
out.push(watch(dir, (_ev, filename) => {
|
|
74
|
+
if (typeof filename === 'string' && names.has(filename))
|
|
75
|
+
onMatch(filename)
|
|
76
|
+
}))
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.warn(`[prunelle dev] could not watch ${dir}: ${err instanceof Error ? err.message : String(err)}`)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
57
84
|
export interface DevWatcherOptions {
|
|
58
85
|
root: string
|
|
59
86
|
devRoot: string
|
|
@@ -67,6 +94,7 @@ export function createDevWatchers({ root, devRoot, srcDir, routesDir, schedule }
|
|
|
67
94
|
const dirWatchers = new Map<string, FSWatcher>()
|
|
68
95
|
const pluginWatchers: FSWatcher[] = []
|
|
69
96
|
const graphWatchers = new Map<string, FSWatcher>()
|
|
97
|
+
const rootGraphFiles = new Set<string>()
|
|
70
98
|
let activeRoutesDir = resolve(root, routesDir ?? 'src/routes')
|
|
71
99
|
let activePublicDir = resolve(root, 'public')
|
|
72
100
|
let builtPublic = new Set<string>()
|
|
@@ -120,6 +148,7 @@ export function createDevWatchers({ root, devRoot, srcDir, routesDir, schedule }
|
|
|
120
148
|
w.close()
|
|
121
149
|
pluginWatchers.length = 0
|
|
122
150
|
const seen = new Set<string>(dirWatchers.keys())
|
|
151
|
+
const watchFiles: string[] = []
|
|
123
152
|
const watchCtx = createAppContext({ root, outDir: devRoot, publicDir: activePublicDir, assetPrefix, site: cfg.site, dev: true })
|
|
124
153
|
for (const plugin of resolvePlugins(cfg.plugins)) {
|
|
125
154
|
const paths = runWithAppContext(watchCtx, () =>
|
|
@@ -129,14 +158,18 @@ export function createDevWatchers({ root, devRoot, srcDir, routesDir, schedule }
|
|
|
129
158
|
if (seen.has(abs) || !existsSync(abs))
|
|
130
159
|
continue
|
|
131
160
|
seen.add(abs)
|
|
132
|
-
|
|
133
|
-
|
|
161
|
+
if (statSync(abs).isDirectory())
|
|
162
|
+
pluginWatchers.push(watch(abs, { recursive: true }, () => schedule('full')))
|
|
163
|
+
else
|
|
164
|
+
watchFiles.push(abs)
|
|
134
165
|
}
|
|
135
166
|
}
|
|
167
|
+
watchFilesByParentDir(watchFiles, () => schedule('full'), pluginWatchers)
|
|
136
168
|
},
|
|
137
169
|
|
|
138
170
|
syncBuild(watchedFiles: ReadonlySet<string>, publicFiles: readonly string[]): void {
|
|
139
171
|
builtPublic = new Set(publicFiles)
|
|
172
|
+
rootGraphFiles.clear()
|
|
140
173
|
const nextDirs = new Set<string>()
|
|
141
174
|
let realRoot: string
|
|
142
175
|
try {
|
|
@@ -151,10 +184,11 @@ export function createDevWatchers({ root, devRoot, srcDir, routesDir, schedule }
|
|
|
151
184
|
if (isInsideDir(devRoot, file) || isInsideDir(join(root, '.prunelle-cache'), file))
|
|
152
185
|
continue
|
|
153
186
|
const dir = dirname(file)
|
|
154
|
-
//
|
|
187
|
+
// Never watch unfiltered project root (which would fire on .prunelle-dev, .prunelle-cache, lockfiles); filter to tracked root filenames in watchRoot.
|
|
155
188
|
if (dir === root || dir === realRoot) {
|
|
156
|
-
if (file
|
|
157
|
-
|
|
189
|
+
if (file !== join(root, 'prunelle.config.ts') && file !== join(realRoot, 'prunelle.config.ts'))
|
|
190
|
+
rootGraphFiles.add(basename(file))
|
|
191
|
+
continue
|
|
158
192
|
}
|
|
159
193
|
if (isCoveredByStaticWatchers(dir))
|
|
160
194
|
continue
|
|
@@ -181,17 +215,19 @@ export function createDevWatchers({ root, devRoot, srcDir, routesDir, schedule }
|
|
|
181
215
|
}
|
|
182
216
|
},
|
|
183
217
|
|
|
184
|
-
// The root, non-recursively: prunelle.config.ts, and a public, routes or src folder created after startup; with `configImports`, the local files prunelle.config.ts imports too.
|
|
218
|
+
// The root, non-recursively: prunelle.config.ts, tracked root graph files, and a public, routes or src folder created after startup; with `configImports`, the local files prunelle.config.ts imports too.
|
|
185
219
|
watchRoot(currentConfig: () => PrunelleConfig, configImports: boolean): void {
|
|
186
|
-
if (configImports)
|
|
187
|
-
|
|
188
|
-
rootWatchers.push(watch(file, () => schedule('config')))
|
|
189
|
-
}
|
|
220
|
+
if (configImports)
|
|
221
|
+
watchFilesByParentDir(localImports(join(root, 'prunelle.config.ts')), () => schedule('config'), rootWatchers)
|
|
190
222
|
rootWatchers.push(watch(root, (_ev, filename) => {
|
|
191
223
|
if (filename === 'prunelle.config.ts') {
|
|
192
224
|
schedule('config')
|
|
193
225
|
return
|
|
194
226
|
}
|
|
227
|
+
if (typeof filename === 'string' && rootGraphFiles.has(filename)) {
|
|
228
|
+
schedule(srcChangeKind(filename))
|
|
229
|
+
return
|
|
230
|
+
}
|
|
195
231
|
const changed = typeof filename === 'string' && filename !== '' ? resolve(root, filename) : undefined
|
|
196
232
|
for (const dir of [activePublicDir, activeRoutesDir, srcDir]) {
|
|
197
233
|
if (!dirWatchers.has(dir) && existsSync(dir) && (changed === undefined || isInsideDir(changed, dir))) {
|
package/src/dom/action.ts
CHANGED
|
@@ -58,28 +58,26 @@ export function useAction<Input, Output>(
|
|
|
58
58
|
if (pending.peek())
|
|
59
59
|
return undefined
|
|
60
60
|
set(true)
|
|
61
|
+
let res: ActionResult<Output>
|
|
61
62
|
try {
|
|
62
|
-
|
|
63
|
-
if (!res.ok) {
|
|
64
|
-
set(false, res.error, res.fieldErrors)
|
|
65
|
-
options.onError?.(res)
|
|
66
|
-
return res
|
|
67
|
-
}
|
|
68
|
-
batch(() => {
|
|
69
|
-
data.value = res.data
|
|
70
|
-
pending.value = false
|
|
71
|
-
})
|
|
72
|
-
if (options.reset && formEl)
|
|
73
|
-
formEl.reset()
|
|
74
|
-
await options.onSuccess?.(res.data, formEl)
|
|
75
|
-
return res
|
|
63
|
+
res = await (action as ClientAction<Input, Output>)(input)
|
|
76
64
|
}
|
|
77
65
|
catch (err) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
res = fail(0, errMsg(err))
|
|
67
|
+
}
|
|
68
|
+
if (!res.ok) {
|
|
69
|
+
set(false, res.error, res.fieldErrors)
|
|
70
|
+
options.onError?.(res)
|
|
71
|
+
return res
|
|
82
72
|
}
|
|
73
|
+
batch(() => {
|
|
74
|
+
data.value = res.data
|
|
75
|
+
pending.value = false
|
|
76
|
+
})
|
|
77
|
+
if (options.reset && formEl)
|
|
78
|
+
formEl.reset()
|
|
79
|
+
await options.onSuccess?.(res.data, formEl)
|
|
80
|
+
return res
|
|
83
81
|
}
|
|
84
82
|
|
|
85
83
|
return {
|
|
@@ -88,7 +86,10 @@ export function useAction<Input, Output>(
|
|
|
88
86
|
fieldErrors,
|
|
89
87
|
data,
|
|
90
88
|
get action() {
|
|
91
|
-
|
|
89
|
+
const u = action.url || String(action)
|
|
90
|
+
if ('__pruAction' in action)
|
|
91
|
+
(globalThis as { __pruAct?: (url: string, act: unknown) => void }).__pruAct?.(u, action)
|
|
92
|
+
return u
|
|
92
93
|
},
|
|
93
94
|
onSubmit(event) {
|
|
94
95
|
event.preventDefault()
|
|
@@ -105,6 +106,21 @@ export function useAction<Input, Output>(
|
|
|
105
106
|
}
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
function isSameDocHash(to: string): boolean {
|
|
110
|
+
if (to.startsWith('#'))
|
|
111
|
+
return true
|
|
112
|
+
if (!to.includes('#') || typeof location.href !== 'string')
|
|
113
|
+
return false
|
|
114
|
+
try {
|
|
115
|
+
const cur = new URL(location.href)
|
|
116
|
+
const next = new URL(to, cur)
|
|
117
|
+
return next.origin === cur.origin && next.pathname === cur.pathname && next.search === cur.search
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
108
124
|
export function actionStub<Input = unknown, Output = unknown>(url: string): ClientAction<Input, Output> {
|
|
109
125
|
const call = (async (input: Input | FormData): Promise<ActionResult<Output>> => {
|
|
110
126
|
const isForm = typeof FormData !== 'undefined' && input instanceof FormData
|
|
@@ -131,7 +147,14 @@ export function actionStub<Input = unknown, Output = unknown>(url: string): Clie
|
|
|
131
147
|
const to = h.get('x-pru-redirect') || (res.status > 299 && res.status < 400 && h.get('location')) || (res.redirected && res.url)
|
|
132
148
|
if (to) {
|
|
133
149
|
if (typeof location !== 'undefined') {
|
|
134
|
-
|
|
150
|
+
try {
|
|
151
|
+
location.assign(to)
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
return fail(0, errMsg(err))
|
|
155
|
+
}
|
|
156
|
+
if (isSameDocHash(to))
|
|
157
|
+
return { ok: true, data: undefined as Output }
|
|
135
158
|
return new Promise<never>(() => {})
|
|
136
159
|
}
|
|
137
160
|
return fail(res.status || 302, to)
|
package/src/dom/attrs.ts
CHANGED
|
@@ -24,13 +24,22 @@ export function setClass(el: Element, value: unknown): void {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
const XLINK_NS = 'http://www.w3.org/1999/xlink'
|
|
28
|
+
|
|
27
29
|
export function setAttr(el: Element, name: string, value: unknown): void {
|
|
28
30
|
if (isBlockedAttr(name))
|
|
29
31
|
return
|
|
30
32
|
if (process.env.NODE_ENV !== 'production')
|
|
31
33
|
assertRenderable(value, name)
|
|
34
|
+
const isXlink = name === 'xlink:href'
|
|
35
|
+
const remove = (): void => {
|
|
36
|
+
if (isXlink)
|
|
37
|
+
el.removeAttributeNS(XLINK_NS, 'href')
|
|
38
|
+
else
|
|
39
|
+
el.removeAttribute(name)
|
|
40
|
+
}
|
|
32
41
|
if (value == null) {
|
|
33
|
-
|
|
42
|
+
remove()
|
|
34
43
|
}
|
|
35
44
|
else if (typeof value === 'boolean') {
|
|
36
45
|
if (isStringBoolAttr(name))
|
|
@@ -38,12 +47,15 @@ export function setAttr(el: Element, name: string, value: unknown): void {
|
|
|
38
47
|
else if (value)
|
|
39
48
|
el.setAttribute(name, '')
|
|
40
49
|
else
|
|
41
|
-
|
|
50
|
+
remove()
|
|
42
51
|
}
|
|
43
52
|
else if (isUnsafeUrlAttr(name, value, el.tagName)) {
|
|
44
53
|
if (process.env.NODE_ENV !== 'production')
|
|
45
54
|
console.warn(`prunelle: dropped unsafe URL on <${el.tagName.toLowerCase()} ${name}>.`)
|
|
46
|
-
|
|
55
|
+
remove()
|
|
56
|
+
}
|
|
57
|
+
else if (isXlink) {
|
|
58
|
+
el.setAttributeNS(XLINK_NS, name, String(value))
|
|
47
59
|
}
|
|
48
60
|
else {
|
|
49
61
|
el.setAttribute(name, String(value))
|
package/src/dom/extras.ts
CHANGED
|
@@ -61,7 +61,7 @@ export function propDefault<T>(fn: () => T, now: boolean): () => T {
|
|
|
61
61
|
let done = false
|
|
62
62
|
const get = (): T => {
|
|
63
63
|
if (!done) {
|
|
64
|
-
value = fn
|
|
64
|
+
value = untrack(fn)
|
|
65
65
|
done = true
|
|
66
66
|
}
|
|
67
67
|
return value
|
|
@@ -89,7 +89,17 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
89
89
|
let lastRef: unknown
|
|
90
90
|
const elRec = el as unknown as Record<string, unknown>
|
|
91
91
|
// Delegated events share document-level dispatch with compiled handlers so stopPropagation() runs child->parent.
|
|
92
|
-
|
|
92
|
+
// Stores [written handler, handler present before spread] so static handlers are restored or left intact.
|
|
93
|
+
const delegated = new Map<string, [unknown, unknown]>()
|
|
94
|
+
const releaseDelegated = (type: string, entry: [unknown, unknown]): void => {
|
|
95
|
+
const key = `$$${type}`
|
|
96
|
+
if (elRec[key] === entry[0]) {
|
|
97
|
+
if (entry[1] !== undefined)
|
|
98
|
+
elRec[key] = entry[1]
|
|
99
|
+
else
|
|
100
|
+
delete elRec[key]
|
|
101
|
+
}
|
|
102
|
+
}
|
|
93
103
|
// One listener per non-delegated event type, calling whichever handler the props hold now.
|
|
94
104
|
const handlers = new Map<string, (e: Event) => void>()
|
|
95
105
|
const listen = (e: Event): void => {
|
|
@@ -102,9 +112,9 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
102
112
|
for (const type of handlers.keys())
|
|
103
113
|
el.removeEventListener(type, listen)
|
|
104
114
|
handlers.clear()
|
|
105
|
-
for (const type of
|
|
106
|
-
|
|
107
|
-
|
|
115
|
+
for (const [type, entry] of delegated)
|
|
116
|
+
releaseDelegated(type, entry)
|
|
117
|
+
delegated.clear()
|
|
108
118
|
})
|
|
109
119
|
|
|
110
120
|
effect(() => {
|
|
@@ -127,7 +137,7 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
127
137
|
if (!isBlockedAttr(name)) {
|
|
128
138
|
if (DOM_PROPS.has(name))
|
|
129
139
|
setProp(el, name, name === 'value' ? '' : false)
|
|
130
|
-
el
|
|
140
|
+
setAttr(el, name, null)
|
|
131
141
|
}
|
|
132
142
|
}
|
|
133
143
|
}
|
|
@@ -151,11 +161,17 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
151
161
|
const type = event[1]!.toLowerCase()
|
|
152
162
|
types.add(type)
|
|
153
163
|
if (DELEGATED_EVENTS.has(type)) {
|
|
154
|
-
|
|
155
|
-
|
|
164
|
+
const key = `$$${type}`
|
|
165
|
+
const entry = delegated.get(type)
|
|
166
|
+
if (entry === undefined) {
|
|
167
|
+
delegated.set(type, [v, elRec[key]])
|
|
156
168
|
delegate([type])
|
|
169
|
+
elRec[key] = v
|
|
170
|
+
}
|
|
171
|
+
else if (elRec[key] === entry[0]) {
|
|
172
|
+
elRec[key] = v
|
|
173
|
+
entry[0] = v
|
|
157
174
|
}
|
|
158
|
-
elRec[`$$${type}`] = v
|
|
159
175
|
}
|
|
160
176
|
else {
|
|
161
177
|
if (!handlers.has(type))
|
|
@@ -177,10 +193,10 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
|
|
|
177
193
|
setAttr(el, name, v)
|
|
178
194
|
}
|
|
179
195
|
|
|
180
|
-
for (const type of
|
|
196
|
+
for (const [type, entry] of delegated) {
|
|
181
197
|
if (!types.has(type)) {
|
|
182
|
-
|
|
183
|
-
|
|
198
|
+
delegated.delete(type)
|
|
199
|
+
releaseDelegated(type, entry)
|
|
184
200
|
}
|
|
185
201
|
}
|
|
186
202
|
for (const type of handlers.keys()) {
|
package/src/dom/index.ts
CHANGED
|
@@ -42,9 +42,9 @@ export function hydrate(container: Element, render: () => Node | DocumentFragmen
|
|
|
42
42
|
hydrateRoot = container
|
|
43
43
|
|
|
44
44
|
try {
|
|
45
|
-
runWithOwner(owner,
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
const node = runWithOwner(owner, render)
|
|
46
|
+
if (node !== null && node.parentNode === null)
|
|
47
|
+
container.appendChild(node)
|
|
48
48
|
}
|
|
49
49
|
catch (err) {
|
|
50
50
|
disposeOwner(owner)
|
|
@@ -165,12 +165,30 @@ export function fragText(data: string, dynamic?: boolean): Text {
|
|
|
165
165
|
return document.createTextNode(data)
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
export function frag(...nodes: Node[]): Node | null {
|
|
169
|
-
|
|
168
|
+
export function frag(...nodes: (Node | null)[]): Node | null {
|
|
169
|
+
const first = nodes.find(n => n?.parentNode && n.parentNode.nodeType !== 11)
|
|
170
|
+
if (first) {
|
|
171
|
+
const parent = first.parentNode!
|
|
172
|
+
let prev: Node | null = null
|
|
173
|
+
for (const n of nodes) {
|
|
174
|
+
if (!n)
|
|
175
|
+
continue
|
|
176
|
+
if (n.parentNode === parent) {
|
|
177
|
+
prev = n
|
|
178
|
+
continue
|
|
179
|
+
}
|
|
180
|
+
const tail = n.nodeType === 11 ? n.lastChild : n
|
|
181
|
+
parent.insertBefore(n, prev ? prev.nextSibling : first)
|
|
182
|
+
if (prev && tail)
|
|
183
|
+
prev = tail
|
|
184
|
+
}
|
|
170
185
|
return null
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
186
|
+
}
|
|
187
|
+
let f: DocumentFragment | null = null
|
|
188
|
+
for (const n of nodes) {
|
|
189
|
+
if (n)
|
|
190
|
+
(f ??= document.createDocumentFragment()).appendChild(n)
|
|
191
|
+
}
|
|
174
192
|
return f
|
|
175
193
|
}
|
|
176
194
|
|
package/src/dom/island.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// Native <pru-island> Custom Element: the build defines which strategies an app uses, so the loader drops the code of the others.
|
|
2
2
|
|
|
3
|
-
import { hydrate, mount } from './index'
|
|
4
|
-
|
|
3
|
+
import type { hydrate, mount } from './index'
|
|
5
4
|
|
|
6
5
|
declare const PRUNELLE_VISIBLE: boolean | undefined
|
|
7
6
|
declare const PRUNELLE_IDLE: boolean | undefined
|
|
@@ -14,7 +13,8 @@ declare const PRUNELLE_REVIVE: string | undefined
|
|
|
14
13
|
// Only props holding a Date, Map, Set or another tagged value load the revivers: plain JSON parses with no extra file.
|
|
15
14
|
const reviver = (): Promise<typeof import('./revive')> => typeof PRUNELLE_REVIVE === 'undefined' ? import('./revive') : import(PRUNELLE_REVIVE)
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
// An island chunk also re-exports the DOM runtime it imports (`writeClientEntries`), `mount` only in an app with a client-only island.
|
|
17
|
+
type IslandModule = { default: (props: Record<string, unknown>) => Node | DocumentFragment | null, hydrate: typeof hydrate, mount?: typeof mount }
|
|
18
18
|
|
|
19
19
|
/** Finds this island's own `<script type="application/json" data-pru-props>` direct child. */
|
|
20
20
|
function findOwnPropsScript(root: Element): Element | null {
|
|
@@ -90,19 +90,43 @@ function replayEvent(root: Element, ev: Event, target: EventTarget | null): void
|
|
|
90
90
|
target.dispatchEvent(clone)
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
function watchInteraction(root: Element, onTrigger: (after: () => void) => void): () => void {
|
|
94
|
+
const evs = ['pointerdown', 'click', 'focusin', 'keydown']
|
|
95
|
+
const queued: { ev: Event, target: EventTarget | null }[] = []
|
|
96
|
+
let loading = false
|
|
97
|
+
const go = (ev: Event) => {
|
|
98
|
+
// Prevent native toggle/submit/navigation before hydration so replaying the click triggers it once with handlers attached.
|
|
99
|
+
if (ev.type === 'click')
|
|
100
|
+
ev.preventDefault()
|
|
101
|
+
queued.push({ ev, target: ev.target })
|
|
102
|
+
if (!loading) {
|
|
103
|
+
loading = true
|
|
104
|
+
onTrigger(() => {
|
|
105
|
+
for (let i = 0; i < queued.length; i++)
|
|
106
|
+
replayEvent(root, queued[i]!.ev, queued[i]!.target)
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
for (const e of evs) root.addEventListener(e, go, true)
|
|
111
|
+
return () => {
|
|
112
|
+
for (const e of evs) root.removeEventListener(e, go, true)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
93
116
|
function isAllowedChunkUrl(chunk: string): boolean {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return chunk[1] !== '/' && chunk[1] !== '\\'
|
|
117
|
+
if (/[\u0000-\u0020\u007F]/.test(chunk))
|
|
118
|
+
return false
|
|
97
119
|
if (typeof location !== 'undefined' && location.origin && location.origin !== 'null') {
|
|
98
120
|
try {
|
|
99
|
-
|
|
100
|
-
return parsed.origin === location.origin
|
|
121
|
+
return new URL(chunk, location.href).origin === location.origin
|
|
101
122
|
}
|
|
102
123
|
catch {
|
|
103
124
|
return false
|
|
104
125
|
}
|
|
105
126
|
}
|
|
127
|
+
// A browser reads `//host` and `/\host` as another site.
|
|
128
|
+
if (chunk[0] === '/')
|
|
129
|
+
return chunk[1] !== '/' && chunk[1] !== '\\'
|
|
106
130
|
return !/^(?:javascript|data|vbscript):/i.test(chunk)
|
|
107
131
|
}
|
|
108
132
|
|
|
@@ -175,24 +199,7 @@ export function defineIslandElement(): void {
|
|
|
175
199
|
return
|
|
176
200
|
}
|
|
177
201
|
if ((typeof PRUNELLE_INTERACTION === 'undefined' || PRUNELLE_INTERACTION) && h === 'interaction') {
|
|
178
|
-
|
|
179
|
-
const queued: { ev: Event, target: EventTarget | null }[] = []
|
|
180
|
-
let loading = false
|
|
181
|
-
const stop = () => {
|
|
182
|
-
for (const e of evs) this.removeEventListener(e, go, true)
|
|
183
|
-
}
|
|
184
|
-
const go = (ev: Event) => {
|
|
185
|
-
queued.push({ ev, target: ev.target })
|
|
186
|
-
if (!loading) {
|
|
187
|
-
loading = true
|
|
188
|
-
void this._l(false, token, true, () => {
|
|
189
|
-
for (let i = 0; i < queued.length; i++)
|
|
190
|
-
replayEvent(this, queued[i]!.ev, queued[i]!.target)
|
|
191
|
-
})
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
for (const e of evs) this.addEventListener(e, go, true)
|
|
195
|
-
this._c = stop
|
|
202
|
+
this._c = watchInteraction(this, after => void this._l(false, token, true, after))
|
|
196
203
|
return
|
|
197
204
|
}
|
|
198
205
|
if ((typeof PRUNELLE_MEDIA === 'undefined' || PRUNELLE_MEDIA) && h !== null && h.startsWith('media(')) {
|
|
@@ -262,8 +269,8 @@ export function defineIslandElement(): void {
|
|
|
262
269
|
const idPrefix = this.getAttribute('i') ?? `c${clientIslandIdSeq++}-`
|
|
263
270
|
const render = () => loaded.default(props)
|
|
264
271
|
dispose = (typeof PRUNELLE_CLIENT_ONLY === 'undefined' || PRUNELLE_CLIENT_ONLY) && clientOnly
|
|
265
|
-
? mount(this, render, idPrefix)
|
|
266
|
-
: hydrate(this, render, idPrefix)
|
|
272
|
+
? loaded.mount!(this, render, idPrefix)
|
|
273
|
+
: loaded.hydrate(this, render, idPrefix)
|
|
267
274
|
if (!this.isConnected || token !== this._t) {
|
|
268
275
|
dispose()
|
|
269
276
|
return
|
package/src/dom/loop.ts
CHANGED
|
@@ -49,11 +49,10 @@ function insertRow<T>(parent: Node, row: Row<T>, anchor: Node | null): void {
|
|
|
49
49
|
parent.insertBefore(list[i]!, anchor)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
function removeRow<T>(
|
|
52
|
+
function removeRow<T>(row: Row<T>): void {
|
|
53
53
|
const list = row.nodes
|
|
54
54
|
if (list === undefined) {
|
|
55
|
-
|
|
56
|
-
parent.removeChild(row.node)
|
|
55
|
+
row.node?.parentNode?.removeChild(row.node)
|
|
57
56
|
return
|
|
58
57
|
}
|
|
59
58
|
const first = list[0]!
|
|
@@ -85,11 +84,12 @@ type RenderRow<T> = (item: Signal<T>, index?: Signal<number>) => Node | Document
|
|
|
85
84
|
|
|
86
85
|
|
|
87
86
|
// While hydrating, a row that rendered no node of its own takes the server nodes the cursor moved over since `before`.
|
|
88
|
-
function adoptRowNodes<T>(parent: Node, marker: Node | null, row: Row<T>, before: Node | null): void {
|
|
87
|
+
function adoptRowNodes<T>(parent: Node, marker: Node | null, row: Row<T>, before: Node | null, prevSib: Node | null): void {
|
|
89
88
|
const afterCursor = hydrateCursor()
|
|
90
89
|
if (row.node === null && before !== null && before !== afterCursor) {
|
|
90
|
+
const start = before.parentNode === parent ? before : prevSib !== null ? prevSib.nextSibling : parent.firstChild
|
|
91
91
|
const claimed: Node[] = []
|
|
92
|
-
for (let n: Node | null =
|
|
92
|
+
for (let n: Node | null = start; n !== null && n !== afterCursor && n !== marker; n = n.nextSibling) {
|
|
93
93
|
if (n.nodeType === 8 && (n as Comment).data === 'k]')
|
|
94
94
|
break
|
|
95
95
|
claimed.push(n)
|
|
@@ -129,8 +129,9 @@ function createRows<T>(parent: Node, marker: Node | null, items: readonly T[], n
|
|
|
129
129
|
if (claimLoop(parent, () => {
|
|
130
130
|
for (let i = 0; i < items.length; i++) {
|
|
131
131
|
const before = hydrateCursor()
|
|
132
|
+
const prevSib = before?.previousSibling ?? null
|
|
132
133
|
const row = newRow(items[i]!, i)
|
|
133
|
-
adoptRowNodes(parent, marker, row, before)
|
|
134
|
+
adoptRowNodes(parent, marker, row, before, prevSib)
|
|
134
135
|
created[i] = row
|
|
135
136
|
}
|
|
136
137
|
end = hydrateCursor()
|
|
@@ -239,7 +240,7 @@ export function loop<T>(
|
|
|
239
240
|
}
|
|
240
241
|
else {
|
|
241
242
|
for (let i = 0; i < prevLen; i++)
|
|
242
|
-
removeRow(
|
|
243
|
+
removeRow(rows[i]!)
|
|
243
244
|
}
|
|
244
245
|
rows = []
|
|
245
246
|
return
|
|
@@ -309,7 +310,7 @@ export function loop<T>(
|
|
|
309
310
|
for (let i = start; i < oldEnd; i++) {
|
|
310
311
|
const row = rows[i]!
|
|
311
312
|
disposeOwner(row.owner)
|
|
312
|
-
removeRow(
|
|
313
|
+
removeRow(row)
|
|
313
314
|
}
|
|
314
315
|
rows = keepEnds(rows, start, oldEnd, newEnd, nextLen)
|
|
315
316
|
return
|
|
@@ -364,7 +365,7 @@ export function loop<T>(
|
|
|
364
365
|
if (used[i - start] === 0) {
|
|
365
366
|
const row = rows[i]!
|
|
366
367
|
disposeOwner(row.owner)
|
|
367
|
-
removeRow(
|
|
368
|
+
removeRow(row)
|
|
368
369
|
}
|
|
369
370
|
}
|
|
370
371
|
|
package/src/plugins/config.ts
CHANGED
|
@@ -6,17 +6,17 @@ export function defineConfig(config: PrunelleConfig): PrunelleConfig {
|
|
|
6
6
|
return config
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
/** Forwards every runtime key from `prunelle.config.ts`, stripping build/CLI-only keys (`routesDir`, `outDir`, `publicDir`, `port`, `compliance`). */
|
|
9
|
+
/** Forwards every runtime key from `prunelle.config.ts`, stripping build/CLI-only keys (`routesDir`, `outDir`, `publicDir`, `port`, `compliance`, `autoPrerender`). */
|
|
10
10
|
export function pickRuntimeConfig(config: PrunelleConfig | undefined): ServerRuntimeConfig {
|
|
11
11
|
if (config === undefined)
|
|
12
12
|
return {}
|
|
13
|
-
const { routesDir: _r, outDir: _o, publicDir: _p, port: _pt, compliance: _c, ...runtime } = config
|
|
13
|
+
const { routesDir: _r, outDir: _o, publicDir: _p, port: _pt, compliance: _c, autoPrerender: _ap, ...runtime } = config
|
|
14
14
|
return runtime
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
// Typed as records so that a key added to the config types without being listed here fails the typecheck.
|
|
18
|
-
const RUNTIME_KEYS: Record<keyof ServerRuntimeConfig, true> = { site: true, lang: true, assetPrefix: true, security: true, trustedOrigins: true, trustProxy: true, maxRequestBodySize: true, navigation: true, streamTimeout: true, consent: true, compress: true, onError: true, plugins: true }
|
|
19
|
-
const BUILD_KEYS: Record<Exclude<keyof PrunelleConfig, keyof ServerRuntimeConfig>, true> = { port: true, routesDir: true, outDir: true, publicDir: true, compliance: true }
|
|
18
|
+
const RUNTIME_KEYS: Record<keyof ServerRuntimeConfig, true> = { site: true, lang: true, assetPrefix: true, security: true, trustedOrigins: true, trustProxy: true, maxRequestBodySize: true, navigation: true, streamTimeout: true, consent: true, compress: true, routeCache: true, healthCheck: true, onError: true, plugins: true }
|
|
19
|
+
const BUILD_KEYS: Record<Exclude<keyof PrunelleConfig, keyof ServerRuntimeConfig>, true> = { port: true, routesDir: true, outDir: true, publicDir: true, compliance: true, autoPrerender: true }
|
|
20
20
|
const RUNTIME_CONFIG_KEYS = Object.keys(RUNTIME_KEYS) as (keyof ServerRuntimeConfig)[]
|
|
21
21
|
const CONFIG_KEYS = [...RUNTIME_CONFIG_KEYS, ...Object.keys(BUILD_KEYS)]
|
|
22
22
|
|
|
@@ -38,13 +38,14 @@ function editDistance(a: string, b: string): number {
|
|
|
38
38
|
return previous[b.length]!
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
/** The name of `names` within two edits of `name` (one
|
|
41
|
+
/** The name of `names` within two edits of `name` (one when either name is under four letters), the nearest first: the one a typo meant. */
|
|
42
42
|
export function closestName(name: string, names: readonly string[]): string | undefined {
|
|
43
43
|
let best: string | undefined
|
|
44
|
-
let bestDistance =
|
|
44
|
+
let bestDistance = 3
|
|
45
45
|
for (const known of names) {
|
|
46
|
+
const maxDistance = Math.min(name.length, known.length) < 4 ? 1 : 2
|
|
46
47
|
const far = editDistance(name, known)
|
|
47
|
-
if (far < bestDistance) {
|
|
48
|
+
if (far <= maxDistance && far < bestDistance) {
|
|
48
49
|
best = known
|
|
49
50
|
bestDistance = far
|
|
50
51
|
}
|
package/src/plugins/index.ts
CHANGED
|
@@ -7,8 +7,8 @@ export { createAppContext, getAppContext, runWithAppContext } from './context'
|
|
|
7
7
|
export type { AppRuntimeContext, CreateAppContextOptions } from './context'
|
|
8
8
|
export { loadConfig } from './load'
|
|
9
9
|
export { resolvePlugins } from './order'
|
|
10
|
-
export { CONTENT_IMAGE_RESOLVER_SERVICE, I18N_CONFIG_SERVICE, serviceKey } from './services'
|
|
11
|
-
export type { ActiveI18nConfig, ContentImageResolver, ContentImageResult, ServiceKey } from './services'
|
|
10
|
+
export { CMS_INLINE_ENABLED_SERVICE, CONTENT_COLLECTIONS_SERVICE, CONTENT_IMAGE_RESOLVER_SERVICE, I18N_CONFIG_SERVICE, JOBS_SERVICE, serviceKey } from './services'
|
|
11
|
+
export type { AccessedContentEntry, ActiveI18nConfig, ContentCollectionDescriptor, ContentCollectionEntrySummary, ContentCollectionsService, ContentImageResolver, ContentImageResult, JobsEnqueueOptions, JobsQueueOptions, JobsService, ServiceKey } from './services'
|
|
12
12
|
export type { ComplianceConfig, ComplianceRule } from '../compiler/compliance'
|
|
13
13
|
export type { ConsentConfig } from '../server/consent'
|
|
14
14
|
export type { BuildPluginContext, BuildPluginTarget, CheckPluginContext, DocumentHeadContext, FinalizeBuildContext, PrunelleConfig, PrunellePlugin, MiddlewareOptions, PluginRouteOptions, RequiresPluginContext, ServerErrorInfo, ServerPluginAPI, ServerRuntimeConfig } from './types'
|
package/src/plugins/load.ts
CHANGED
|
@@ -2,20 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
import type { AppRuntimeContext } from './context'
|
|
4
4
|
import type { PrunelleConfig } from './types'
|
|
5
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
5
6
|
import { existsSync, readFileSync } from 'node:fs'
|
|
6
|
-
import { join, relative, resolve } from 'node:path'
|
|
7
|
+
import { join, relative, resolve, sep } from 'node:path'
|
|
7
8
|
import { assertKnownConfigKeys } from './config'
|
|
8
9
|
import { createAppContext, getAppContext, runWithAppContext } from './context'
|
|
9
10
|
|
|
10
11
|
let configReloadSeq = 0
|
|
12
|
+
const PRUNELLE_SRC_DIR = resolve(import.meta.dir, '..')
|
|
13
|
+
const freshConfigStorage = new AsyncLocalStorage<boolean>()
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
function
|
|
15
|
+
/** Runs `fn` with every nested `loadConfig()` bypassing Bun's module cache (used by long-lived processes such as `prunelle mcp`). */
|
|
16
|
+
export function runWithFreshConfig<T>(fn: () => T): T {
|
|
17
|
+
return freshConfigStorage.run(true, fn)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function evictLocalConfigImports(absRoot: string): void {
|
|
21
|
+
const rootPrefix = absRoot.endsWith(sep) ? absRoot : `${absRoot}${sep}`
|
|
22
|
+
const pruPrefix = PRUNELLE_SRC_DIR.endsWith(sep) ? PRUNELLE_SRC_DIR : `${PRUNELLE_SRC_DIR}${sep}`
|
|
23
|
+
const nmSegment = `${sep}node_modules${sep}`
|
|
24
|
+
for (const cached of Object.keys(require.cache)) {
|
|
25
|
+
if (cached.startsWith(rootPrefix) && !cached.includes(nmSegment) && !cached.startsWith(pruPrefix))
|
|
26
|
+
delete require.cache[cached]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Query follows file content so unchanged configs reuse loaded plugin instances while edits reload immediately; `fresh` forces a new evaluation.
|
|
31
|
+
function configSpecifier(configPath: string, fresh: boolean): string {
|
|
14
32
|
if (fresh)
|
|
15
33
|
return `${configPath}?v=fresh-${++configReloadSeq}`
|
|
16
|
-
|
|
17
|
-
return `${configPath}?v=${Bun.hash(readFileSync(configPath)).toString(36)}`
|
|
18
|
-
return configPath
|
|
34
|
+
return `${configPath}?v=${Bun.hash(readFileSync(configPath)).toString(36)}`
|
|
19
35
|
}
|
|
20
36
|
|
|
21
37
|
export async function loadConfig(
|
|
@@ -29,7 +45,10 @@ export async function loadConfig(
|
|
|
29
45
|
const active = options?.appContext ?? getAppContext()
|
|
30
46
|
const isDev = options?.dev ?? active?.dev ?? (process.env.PRUNELLE_DEV === '1')
|
|
31
47
|
const resolvedNodeEnv = options?.nodeEnv ?? active?.nodeEnv
|
|
32
|
-
const
|
|
48
|
+
const isFresh = options?.fresh ?? (freshConfigStorage.getStore() === true)
|
|
49
|
+
if (isFresh)
|
|
50
|
+
evictLocalConfigImports(absRoot)
|
|
51
|
+
const spec = configSpecifier(configPath, isFresh)
|
|
33
52
|
const ctx = active !== undefined && active.root === absRoot
|
|
34
53
|
? active
|
|
35
54
|
: createAppContext({
|