prunelle 0.0.0-stage → 0.7.2
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/LICENSE +21 -0
- package/llms.txt +245 -0
- package/package.json +62 -4
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +43 -0
- package/src/build/bundle.ts +661 -0
- package/src/build/byte-size.ts +11 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +175 -0
- package/src/build/client-prune.ts +168 -0
- package/src/build/compile.ts +130 -0
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +171 -0
- package/src/build/first-load.ts +235 -0
- package/src/build/graph.ts +237 -0
- package/src/build/index.ts +678 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +452 -0
- package/src/build/manifest.ts +204 -0
- package/src/build/output.ts +81 -0
- package/src/build/plugin-host.ts +294 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +288 -0
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +290 -0
- package/src/build/resolve.ts +52 -0
- package/src/build/routes.ts +342 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +209 -0
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +409 -0
- package/src/cli/commands.ts +372 -0
- package/src/cli/docs.ts +403 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +406 -0
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +52 -0
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +367 -0
- package/src/compiler/analyze.ts +651 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +250 -0
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +653 -0
- package/src/compiler/data-keys.ts +302 -0
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +452 -0
- package/src/compiler/dom.ts +641 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +587 -0
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +520 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +511 -0
- package/src/compiler/scope.ts +635 -0
- package/src/compiler/server-only.ts +365 -0
- package/src/compiler/shared.ts +397 -0
- package/src/compiler/signal-reads.ts +246 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +69 -0
- package/src/compiler/ssr.ts +525 -0
- package/src/context.ts +199 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +268 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +24 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +159 -0
- package/src/dev/supervisor.ts +212 -0
- package/src/dev/watchers.ts +276 -0
- package/src/dom/action.ts +187 -0
- package/src/dom/attrs.ts +84 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +579 -0
- package/src/dom/index.ts +329 -0
- package/src/dom/island.ts +318 -0
- package/src/dom/loop.ts +484 -0
- package/src/dom/revive.ts +36 -0
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +92 -0
- package/src/plugins/context.ts +109 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +90 -0
- package/src/plugins/order.ts +148 -0
- package/src/plugins/public.ts +15 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +119 -0
- package/src/plugins/types.ts +278 -0
- package/src/reactivity/graph.ts +259 -0
- package/src/reactivity/index.ts +541 -0
- package/src/reactivity/selector.ts +78 -0
- package/src/reactivity/store.ts +252 -0
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +511 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +168 -0
- package/src/server/compress.ts +646 -0
- package/src/server/consent.ts +290 -0
- package/src/server/cookies.ts +153 -0
- package/src/server/document.ts +286 -0
- package/src/server/env.ts +47 -0
- package/src/server/errors.ts +396 -0
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +588 -0
- package/src/server/index.ts +615 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +131 -0
- package/src/server/page.ts +122 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +267 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +20 -0
- package/src/server/rate-limit.ts +199 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +424 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +388 -0
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +550 -0
- package/src/server/serve-handlers.ts +93 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +104 -0
- package/src/server/start.ts +143 -0
- package/src/server/static-dir.ts +246 -0
- package/src/server/stream.ts +233 -0
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +399 -0
- package/src/ssr/index.ts +624 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +192 -0
- package/src/style.ts +96 -0
- package/src/test/browser.ts +201 -0
- package/src/test/index.ts +262 -0
- 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/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +8 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +22 -0
- package/templates/_shared/tsconfig.json +25 -0
- package/templates/app/.env.example +8 -0
- package/templates/app/package.json +22 -0
- package/templates/app/prunelle.config.ts +13 -0
- package/templates/app/src/auth.ts +12 -0
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +40 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +47 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +53 -0
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +14 -0
- package/templates/app/tests/app.test.ts +73 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +25 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +15 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +28 -0
- package/templates/fullstack/src/routes/_layout.tsx +18 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +39 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +18 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +32 -0
- package/ts-plugin.cjs +4277 -0
- package/README.md +0 -3
package/src/context.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Universal component context (createContext / useContext) and hydration-safe useId().
|
|
2
|
+
// Browser-pure: zero Node imports. SSR wires its per-request AsyncLocalStorage bridge via registerServerContextBridge().
|
|
3
|
+
|
|
4
|
+
import type { PrunelleChild } from './jsx-runtime'
|
|
5
|
+
import type { IdScope, Owner } from './reactivity/index'
|
|
6
|
+
import { createOwner, getOwner, runWithOwner } from './reactivity/index'
|
|
7
|
+
|
|
8
|
+
export interface RegionProps {
|
|
9
|
+
name: string
|
|
10
|
+
/** The element tag, `div` by default. */
|
|
11
|
+
as?: string
|
|
12
|
+
children?: PrunelleChild
|
|
13
|
+
[attribute: string]: unknown
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ConsentGateProps {
|
|
17
|
+
/** Consent category required to render `children` (e.g. `'analytics'`, `'media'`). */
|
|
18
|
+
category: string
|
|
19
|
+
/** Rendered when consent for `category` has not been granted. */
|
|
20
|
+
fallback?: PrunelleChild
|
|
21
|
+
children?: PrunelleChild
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The button and legend texts of `<ConsentBanner>` and `<ConsentPreferences>`, English by default. */
|
|
25
|
+
export interface ConsentTexts {
|
|
26
|
+
accept?: string | undefined
|
|
27
|
+
refuse?: string | undefined
|
|
28
|
+
save?: string | undefined
|
|
29
|
+
/** Legend of the checkbox list (default: `'Cookies by purpose'`). */
|
|
30
|
+
legend?: string | undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ConsentBannerProps extends ConsentTexts {
|
|
34
|
+
/** Heading of the banner (default: `'Cookies'`). */
|
|
35
|
+
title?: string | undefined
|
|
36
|
+
/** What the cookies are for; by default a sentence listing each category's label. */
|
|
37
|
+
children?: PrunelleChild
|
|
38
|
+
/** The cookie or privacy policy page, linked under the text. */
|
|
39
|
+
policyHref?: string | undefined
|
|
40
|
+
/** Text of the disclosure holding the checkboxes (default: `'Choose'`). */
|
|
41
|
+
choose?: string | undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface CatchProps {
|
|
45
|
+
/** Rendered in place of `children` when they throw; a function gets the error and a `retry` that renders the children again (a no-op on the server). */
|
|
46
|
+
fallback?: PrunelleChild | ((error: unknown, retry: () => void) => PrunelleChild)
|
|
47
|
+
children?: PrunelleChild
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ServerContextBridge {
|
|
51
|
+
region(props: RegionProps): PrunelleChild
|
|
52
|
+
consentGate(props: ConsentGateProps): PrunelleChild
|
|
53
|
+
consentBanner(props: ConsentBannerProps): PrunelleChild
|
|
54
|
+
consentPreferences(props: ConsentTexts): PrunelleChild
|
|
55
|
+
getContext(key: symbol): { found: true, value: unknown } | undefined
|
|
56
|
+
runWithContext<T>(key: symbol, value: unknown, fn: () => T): T
|
|
57
|
+
nextId(): string | undefined
|
|
58
|
+
head(props: { children?: PrunelleChild }): PrunelleChild
|
|
59
|
+
suspense(props: { fallback?: PrunelleChild, error?: PrunelleChild | ((error: unknown) => PrunelleChild), children?: PrunelleChild | Promise<PrunelleChild> }): PrunelleChild
|
|
60
|
+
catch(props: CatchProps): PrunelleChild | Promise<PrunelleChild>
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const BRIDGE_KEY = Symbol.for('pru.server.context.bridge')
|
|
64
|
+
|
|
65
|
+
function getServerBridge(): ServerContextBridge | undefined {
|
|
66
|
+
return (globalThis as Record<symbol, ServerContextBridge | undefined>)[BRIDGE_KEY]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Internal: registered by prunelle's SSR runtime on the server so `Head`, `Suspense`, `useContext`, and `useId` stay request-scoped without pulling `node:async_hooks` into client bundles. */
|
|
70
|
+
export function registerServerContextBridge(bridge: ServerContextBridge): void {
|
|
71
|
+
;(globalThis as Record<symbol, ServerContextBridge | undefined>)[BRIDGE_KEY] = bridge
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Collects document `<head>` tags during SSR; no-op in the browser. */
|
|
75
|
+
export function Head(props: { children?: PrunelleChild }): PrunelleChild {
|
|
76
|
+
const bridge = getServerBridge()
|
|
77
|
+
return bridge !== undefined ? bridge.head(props) : null
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Streaming SSR boundary: flushes `fallback` immediately and streams resolved async children. */
|
|
81
|
+
export function Suspense(props: { fallback?: PrunelleChild, error?: PrunelleChild | ((error: unknown) => PrunelleChild), children?: PrunelleChild | Promise<PrunelleChild> }): PrunelleChild {
|
|
82
|
+
const bridge = getServerBridge()
|
|
83
|
+
if (bridge !== undefined)
|
|
84
|
+
return bridge.suspense(props)
|
|
85
|
+
return props.children instanceof Promise ? (props.fallback ?? null) : (props.children ?? null)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Renders `fallback` in place of `children` when they throw, on the server and in an island; the app's `onError` hook still gets the error. */
|
|
89
|
+
export function Catch(props: CatchProps): PrunelleChild | Promise<PrunelleChild> {
|
|
90
|
+
const bridge = getOwner() === undefined ? getServerBridge() : undefined
|
|
91
|
+
if (bridge !== undefined)
|
|
92
|
+
return bridge.catch(props)
|
|
93
|
+
throw new Error('prunelle: Catch was called as a function in the browser: write it as <Catch fallback={...}>...</Catch>, which the compiler turns into the browser boundary.')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A part of the page that a link or form with `data-pru-target` swaps alone: a request for it renders only its children. Server-only, like `Head`. */
|
|
97
|
+
export function Region(props: RegionProps): PrunelleChild {
|
|
98
|
+
const bridge = getServerBridge()
|
|
99
|
+
return bridge !== undefined ? bridge.region(props) : null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Renders `children` on the server only when consent for `category` has been granted; otherwise renders `fallback`. Server-only, 0 KB browser JS. */
|
|
103
|
+
export function ConsentGate(props: ConsentGateProps): PrunelleChild {
|
|
104
|
+
const bridge = getServerBridge()
|
|
105
|
+
return bridge !== undefined ? bridge.consentGate(props) : (props.fallback ?? null)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Asks for consent until the visitor accepts, refuses or picks categories: a zero-JS form posting to the consent endpoint, with Accept and Refuse given the same weight. Server-only. */
|
|
109
|
+
export function ConsentBanner(props: ConsentBannerProps): PrunelleChild {
|
|
110
|
+
const bridge = getServerBridge()
|
|
111
|
+
return bridge !== undefined ? bridge.consentBanner(props) : null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The visitor's current choice per category, to change or withdraw it as easily as it was given: put it on the cookie policy page or behind a footer link. Server-only. */
|
|
115
|
+
export function ConsentPreferences(props: ConsentTexts): PrunelleChild {
|
|
116
|
+
const bridge = getServerBridge()
|
|
117
|
+
return bridge !== undefined ? bridge.consentPreferences(props) : null
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface Context<T> {
|
|
121
|
+
readonly id: symbol
|
|
122
|
+
readonly defaultValue: T
|
|
123
|
+
Provider: (props: { value: T, children?: PrunelleChild | (() => PrunelleChild) }) => PrunelleChild
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function findOwnerContext(owner: Owner | undefined, key: symbol): { found: true, value: unknown } | undefined {
|
|
127
|
+
for (let o = owner; o !== undefined; o = o.o) {
|
|
128
|
+
if (o.cx !== undefined && o.cx.has(key))
|
|
129
|
+
return { found: true, value: o.cx.get(key) }
|
|
130
|
+
}
|
|
131
|
+
return undefined
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function findOwnerIdScope(owner: Owner | undefined): IdScope | undefined {
|
|
135
|
+
for (let o = owner; o !== undefined; o = o.o) {
|
|
136
|
+
if (o.id !== undefined)
|
|
137
|
+
return o.id
|
|
138
|
+
}
|
|
139
|
+
return undefined
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Run `fn` with `ctx` bound to `value` on both server (SSR) and browser (reactive owner tree). */
|
|
143
|
+
export function provideContext<T, R>(ctx: Context<T>, value: T, fn: () => R): R {
|
|
144
|
+
const owner = getOwner()
|
|
145
|
+
if (owner !== undefined) {
|
|
146
|
+
const scope = createOwner(owner)
|
|
147
|
+
;(scope.cx ??= new Map()).set(ctx.id, value)
|
|
148
|
+
return runWithOwner(scope, fn)
|
|
149
|
+
}
|
|
150
|
+
const bridge = getServerBridge()
|
|
151
|
+
if (bridge !== undefined)
|
|
152
|
+
return bridge.runWithContext(ctx.id, value, fn)
|
|
153
|
+
return fn()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function createContext<T>(defaultValue: T): Context<T> {
|
|
157
|
+
const id = Symbol('prunelle.context')
|
|
158
|
+
const ctx: Context<T> = {
|
|
159
|
+
id,
|
|
160
|
+
defaultValue,
|
|
161
|
+
Provider(props) {
|
|
162
|
+
return provideContext(ctx, props.value, () => {
|
|
163
|
+
const c = props.children
|
|
164
|
+
return typeof c === 'function' ? (c as () => PrunelleChild)() : c
|
|
165
|
+
})
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
return ctx
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function useContext<T>(ctx: Context<T>): T {
|
|
172
|
+
const owner = getOwner()
|
|
173
|
+
if (owner !== undefined) {
|
|
174
|
+
const hit = findOwnerContext(owner, ctx.id)
|
|
175
|
+
if (hit !== undefined)
|
|
176
|
+
return hit.value as T
|
|
177
|
+
}
|
|
178
|
+
const bridge = getServerBridge()
|
|
179
|
+
if (bridge !== undefined) {
|
|
180
|
+
const hit = bridge.getContext(ctx.id)
|
|
181
|
+
if (hit !== undefined)
|
|
182
|
+
return hit.value as T
|
|
183
|
+
}
|
|
184
|
+
return ctx.defaultValue
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let fallbackIdSeq = 0
|
|
188
|
+
|
|
189
|
+
/** Generates a deterministic, hydration-safe unique ID scoped to the current island or SSR request. */
|
|
190
|
+
export function useId(prefix = 'k'): string {
|
|
191
|
+
const scope = findOwnerIdScope(getOwner())
|
|
192
|
+
if (scope !== undefined)
|
|
193
|
+
return `${prefix}-${scope.p}${scope.n++}`
|
|
194
|
+
const bridge = getServerBridge()
|
|
195
|
+
const serverId = bridge?.nextId()
|
|
196
|
+
if (serverId !== undefined)
|
|
197
|
+
return `${prefix}-${serverId}`
|
|
198
|
+
return `${prefix}-g${fallbackIdSeq++}`
|
|
199
|
+
}
|
package/src/dev/child.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// prunelle dev's app process: the build, the watchers and the app, on a Unix socket behind the `prunelle dev` process that keeps the port.
|
|
2
|
+
|
|
3
|
+
import type { DevChildMessage, DevChildOptions } from './protocol'
|
|
4
|
+
import { devServer } from './index'
|
|
5
|
+
import { DEV_CHILD_ENV } from './protocol'
|
|
6
|
+
|
|
7
|
+
if (import.meta.main) {
|
|
8
|
+
const options = JSON.parse(process.env[DEV_CHILD_ENV] ?? '{}') as DevChildOptions
|
|
9
|
+
const send = (message: DevChildMessage): void => {
|
|
10
|
+
process.send?.(message)
|
|
11
|
+
}
|
|
12
|
+
const srv = await devServer({
|
|
13
|
+
...options,
|
|
14
|
+
broadcast: payload => send({ t: 'event', payload }),
|
|
15
|
+
recycle: reason => send({ t: 'recycle', reason }),
|
|
16
|
+
})
|
|
17
|
+
let stopping: Promise<void> | undefined
|
|
18
|
+
const stop = (): void => {
|
|
19
|
+
stopping ??= srv.stop().finally(() => process.exit(0))
|
|
20
|
+
}
|
|
21
|
+
process.on('message', (message: { t?: string }) => {
|
|
22
|
+
if (message.t === 'stop')
|
|
23
|
+
stop()
|
|
24
|
+
})
|
|
25
|
+
// The parent died without asking, or Ctrl-C reached the whole process group.
|
|
26
|
+
process.on('disconnect', stop)
|
|
27
|
+
process.on('SIGINT', stop)
|
|
28
|
+
process.on('SIGTERM', stop)
|
|
29
|
+
send({ t: 'ready', assetPrefix: srv.assetPrefix })
|
|
30
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Persists the last dev build error, child crash, and recent runtime errors in an append-only journal, `.prunelle-dev/last-errors.jsonl`,
|
|
2
|
+
// so `prunelle mcp` (`dev_errors` tool) can inspect live dev failures without touching `dist/` or tracked files.
|
|
3
|
+
|
|
4
|
+
import type { ServerErrorInfo } from '../plugins/index'
|
|
5
|
+
import { appendFileSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { dirname, join, resolve } from 'node:path'
|
|
7
|
+
|
|
8
|
+
export interface DevErrorEntry {
|
|
9
|
+
kind: 'build' | 'runtime' | 'crash'
|
|
10
|
+
timestamp: string
|
|
11
|
+
message: string
|
|
12
|
+
source?: string | undefined
|
|
13
|
+
method?: string | undefined
|
|
14
|
+
path?: string | undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DevErrorLog {
|
|
18
|
+
updatedAt: string
|
|
19
|
+
buildError?: string | undefined
|
|
20
|
+
crashError?: string | undefined
|
|
21
|
+
runtimeErrors: DevErrorEntry[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const MAX_RUNTIME_ERRORS = 20
|
|
25
|
+
/** Past this size the journal is folded back to its current state. */
|
|
26
|
+
const COMPACT_BYTES = 256 * 1024
|
|
27
|
+
|
|
28
|
+
/** One line of the journal: a build or crash message (none clears it), or a runtime error. */
|
|
29
|
+
type JournalRecord =
|
|
30
|
+
| { op: 'build' | 'crash', at: string, message?: string | undefined }
|
|
31
|
+
| { op: 'runtime', at: string, entry: DevErrorEntry }
|
|
32
|
+
|
|
33
|
+
function errorLogPath(root: string): string {
|
|
34
|
+
return join(resolve(root), '.prunelle-dev', 'last-errors.jsonl')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isDevErrorEntry(value: unknown): value is DevErrorEntry {
|
|
38
|
+
if (typeof value !== 'object' || value === null)
|
|
39
|
+
return false
|
|
40
|
+
const rec = value as Record<string, unknown>
|
|
41
|
+
return (rec.kind === 'build' || rec.kind === 'runtime' || rec.kind === 'crash')
|
|
42
|
+
&& typeof rec.timestamp === 'string'
|
|
43
|
+
&& typeof rec.message === 'string'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parseRecord(line: string): JournalRecord | undefined {
|
|
47
|
+
try {
|
|
48
|
+
const rec = JSON.parse(line) as Record<string, unknown>
|
|
49
|
+
if (typeof rec.at !== 'string')
|
|
50
|
+
return undefined
|
|
51
|
+
if ((rec.op === 'build' || rec.op === 'crash') && (rec.message === undefined || typeof rec.message === 'string'))
|
|
52
|
+
return { op: rec.op, at: rec.at, message: rec.message }
|
|
53
|
+
if (rec.op === 'runtime' && isDevErrorEntry(rec.entry))
|
|
54
|
+
return { op: 'runtime', at: rec.at, entry: rec.entry }
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// A line cut by a crash is skipped; the rest of the journal still reads.
|
|
58
|
+
return undefined
|
|
59
|
+
}
|
|
60
|
+
return undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Folds the journal lines, in order, into the state they leave. */
|
|
64
|
+
function foldJournal(text: string): DevErrorLog | undefined {
|
|
65
|
+
let log: DevErrorLog | undefined
|
|
66
|
+
for (const line of text.split('\n')) {
|
|
67
|
+
const rec = line === '' ? undefined : parseRecord(line)
|
|
68
|
+
if (rec === undefined)
|
|
69
|
+
continue
|
|
70
|
+
log ??= { updatedAt: rec.at, runtimeErrors: [] }
|
|
71
|
+
log.updatedAt = rec.at
|
|
72
|
+
if (rec.op === 'runtime') {
|
|
73
|
+
log.runtimeErrors.push(rec.entry)
|
|
74
|
+
if (log.runtimeErrors.length > MAX_RUNTIME_ERRORS)
|
|
75
|
+
log.runtimeErrors.shift()
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
const key = rec.op === 'build' ? 'buildError' : 'crashError'
|
|
79
|
+
if (rec.message !== undefined && rec.message.length > 0)
|
|
80
|
+
log[key] = rec.message
|
|
81
|
+
else
|
|
82
|
+
delete log[key]
|
|
83
|
+
}
|
|
84
|
+
return log
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Reads `.prunelle-dev/last-errors.jsonl` safely, returning `undefined` when missing or unreadable. */
|
|
88
|
+
export function readDevErrorLog(root: string): DevErrorLog | undefined {
|
|
89
|
+
try {
|
|
90
|
+
return foldJournal(readFileSync(errorLogPath(root), 'utf8'))
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The records that rebuild `log`, for a compacted journal. */
|
|
98
|
+
function recordsOf(log: DevErrorLog): JournalRecord[] {
|
|
99
|
+
return [
|
|
100
|
+
...(log.buildError !== undefined ? [{ op: 'build', at: log.updatedAt, message: log.buildError } as const] : []),
|
|
101
|
+
...(log.crashError !== undefined ? [{ op: 'crash', at: log.updatedAt, message: log.crashError } as const] : []),
|
|
102
|
+
...log.runtimeErrors.map(entry => ({ op: 'runtime', at: entry.timestamp, entry }) as const),
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Appends one record. A short `O_APPEND` write lands whole, so two processes (the supervisor and its child) never overwrite each other.
|
|
108
|
+
* Only a journal past `COMPACT_BYTES` is rewritten, through a temp file.
|
|
109
|
+
*/
|
|
110
|
+
function appendRecord(root: string, rec: JournalRecord): void {
|
|
111
|
+
const file = errorLogPath(root)
|
|
112
|
+
try {
|
|
113
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
114
|
+
appendFileSync(file, `${JSON.stringify(rec)}\n`, 'utf8')
|
|
115
|
+
if (statSync(file).size <= COMPACT_BYTES)
|
|
116
|
+
return
|
|
117
|
+
const log = foldJournal(readFileSync(file, 'utf8'))
|
|
118
|
+
if (log === undefined)
|
|
119
|
+
return
|
|
120
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`
|
|
121
|
+
writeFileSync(tmp, recordsOf(log).map(r => `${JSON.stringify(r)}\n`).join(''), 'utf8')
|
|
122
|
+
renameSync(tmp, file)
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// The log is a convenience for `prunelle mcp`; a read-only folder must not take the dev server down.
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Records or clears the active dev build error (passing `undefined` clears it after a successful build). */
|
|
131
|
+
export function recordDevBuildError(root: string, message: string | undefined): void {
|
|
132
|
+
appendRecord(root, { op: 'build', at: new Date().toISOString(), message })
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Records or clears the active dev child crash message (passing `undefined` clears it once the child is ready). */
|
|
136
|
+
export function recordDevCrashError(root: string, message: string | undefined): void {
|
|
137
|
+
appendRecord(root, { op: 'crash', at: new Date().toISOString(), message })
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Appends a runtime error to `.prunelle-dev/last-errors.jsonl`; reading keeps the last 20. */
|
|
141
|
+
export function recordDevRuntimeError(root: string, entry: Omit<DevErrorEntry, 'kind' | 'timestamp'>): void {
|
|
142
|
+
const at = new Date().toISOString()
|
|
143
|
+
appendRecord(root, {
|
|
144
|
+
op: 'runtime',
|
|
145
|
+
at,
|
|
146
|
+
entry: {
|
|
147
|
+
kind: 'runtime',
|
|
148
|
+
timestamp: at,
|
|
149
|
+
message: entry.message,
|
|
150
|
+
...(entry.source !== undefined ? { source: entry.source } : {}),
|
|
151
|
+
...(entry.method !== undefined ? { method: entry.method } : {}),
|
|
152
|
+
...(entry.path !== undefined ? { path: entry.path } : {}),
|
|
153
|
+
},
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function safePathname(rawUrl: string): string {
|
|
158
|
+
try {
|
|
159
|
+
return new URL(rawUrl).pathname
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return rawUrl
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Builds an `errorSink` callback for `devServer` that records unhandled runtime errors into `.prunelle-dev/last-errors.json`. */
|
|
167
|
+
export function createDevRuntimeErrorSink(root: string): (info: ServerErrorInfo) => void {
|
|
168
|
+
return (info) => {
|
|
169
|
+
const message = info.error instanceof Error ? info.error.stack ?? info.error.message : String(info.error)
|
|
170
|
+
recordDevRuntimeError(root, {
|
|
171
|
+
message,
|
|
172
|
+
source: info.source,
|
|
173
|
+
method: info.request.method,
|
|
174
|
+
path: safePathname(info.request.url),
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// A DNS-rebinding page reaches a dev server under the page's own domain name, so the dev server answers only the names it can be opened under.
|
|
2
|
+
|
|
3
|
+
const LOOPBACK_NAMES = new Set(['localhost', '127.0.0.1', '[::1]'])
|
|
4
|
+
const IPV4_RE = /^\d{1,3}(?:\.\d{1,3}){3}$/
|
|
5
|
+
const BAD_HOST_CHARS_RE = /[@/?#\\\s\u0000-\u001F\u007F]/
|
|
6
|
+
|
|
7
|
+
function hostnameOf(host: string): string | undefined {
|
|
8
|
+
if (host.length === 0 || BAD_HOST_CHARS_RE.test(host) || !URL.canParse(`http://${host}`))
|
|
9
|
+
return undefined
|
|
10
|
+
const parsed = new URL(`http://${host}`)
|
|
11
|
+
if (parsed.username !== '' || parsed.password !== '' || parsed.pathname !== '/' || parsed.search !== '' || parsed.hash !== '')
|
|
12
|
+
return undefined
|
|
13
|
+
return parsed.hostname
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Refuses with `421` a request whose `Host` is not loopback, `*.localhost`, an IP address, the bound `hostname` or the host of one of `origins`. */
|
|
17
|
+
export function createDevHostCheck(hostname: string | undefined, origins: readonly (string | undefined)[]): (request: Request) => Response | undefined {
|
|
18
|
+
const names = new Set<string>()
|
|
19
|
+
if (hostname !== undefined)
|
|
20
|
+
names.add(hostnameOf(hostname) ?? hostname)
|
|
21
|
+
for (const origin of origins) {
|
|
22
|
+
if (origin !== undefined && URL.canParse(origin))
|
|
23
|
+
names.add(new URL(origin).hostname)
|
|
24
|
+
}
|
|
25
|
+
return (request) => {
|
|
26
|
+
const host = request.headers.get('host') ?? ''
|
|
27
|
+
const name = hostnameOf(host)
|
|
28
|
+
if (name !== undefined && (LOOPBACK_NAMES.has(name) || name.endsWith('.localhost') || name.startsWith('[') || IPV4_RE.test(name) || names.has(name)))
|
|
29
|
+
return undefined
|
|
30
|
+
return new Response(`prunelle dev: refused Host "${name ?? host}": a dev server answers only localhost, an IP address, its --host and the hosts of site and trustedOrigins in prunelle.config.ts, so that a page on another domain cannot reach it by DNS rebinding. Add the origin you open it under to trustedOrigins.\n`, { status: 421, headers: { 'content-type': 'text/plain; charset=utf-8' } })
|
|
31
|
+
}
|
|
32
|
+
}
|