prunelle 0.1.1 → 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 +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- 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 +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- 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 +407 -352
- 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 +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- 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 +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- 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 +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- 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/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- package/ts-plugin.cjs +4277 -0
package/src/server/rate-limit.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// How often a client may call a plugin route or an action: a sliding window per key, in one store per server bounded in keys and hits.
|
|
2
2
|
|
|
3
|
+
import { nodeEnv } from './env'
|
|
4
|
+
|
|
3
5
|
export interface RateLimit<Ctx extends { ip?: string | undefined } = { ip?: string | undefined }> {
|
|
4
6
|
/** Requests a key may make per window. */
|
|
5
7
|
max: number
|
|
@@ -12,6 +14,8 @@ export interface RateLimit<Ctx extends { ip?: string | undefined } = { ip?: stri
|
|
|
12
14
|
export interface RateLimiter {
|
|
13
15
|
/** Counts a request from `key` and returns 0; past the limit, counts nothing and returns the whole seconds until the next request would pass. */
|
|
14
16
|
hit(key: string): number
|
|
17
|
+
/** The seconds `hit(key)` would answer, counting nothing: 0 while `key` is under the limit. */
|
|
18
|
+
peek(key: string): number
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
export interface RateLimitStore {
|
|
@@ -31,9 +35,10 @@ export interface RateLimitStoreOptions {
|
|
|
31
35
|
maxHits?: number | undefined
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
interface
|
|
35
|
-
|
|
36
|
-
windowMs: number
|
|
38
|
+
interface Bucket {
|
|
39
|
+
readonly entries: Map<string, number[]>
|
|
40
|
+
readonly windowMs: number
|
|
41
|
+
hitCount: number
|
|
37
42
|
}
|
|
38
43
|
|
|
39
44
|
const SWEEP_MS = 60_000
|
|
@@ -49,47 +54,55 @@ export function assertRateLimit(limit: Pick<RateLimit, 'max' | 'windowMs'>, wher
|
|
|
49
54
|
export function createRateLimitStore(options: RateLimitStoreOptions = {}): RateLimitStore {
|
|
50
55
|
const maxKeys = Math.max(1, options.maxKeys ?? 10_000)
|
|
51
56
|
const maxHits = Math.max(1, options.maxHits ?? 1_000_000)
|
|
52
|
-
//
|
|
53
|
-
const
|
|
54
|
-
let
|
|
55
|
-
let nextId = 0
|
|
57
|
+
// Each limiter owns a bounded LRU map so flooding one endpoint cannot evict keys from another.
|
|
58
|
+
const buckets: Bucket[] = []
|
|
59
|
+
let totalKeys = 0
|
|
56
60
|
let timer: ReturnType<typeof setInterval> | undefined
|
|
57
61
|
|
|
58
|
-
function
|
|
59
|
-
|
|
62
|
+
function secondsUntilFree(hits: readonly number[], max: number, windowMs: number, now: number): number {
|
|
63
|
+
return Math.max(1, Math.ceil((hits[hits.length - max]! + windowMs - now) / 1000))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function trim(bucket: Bucket, hits: number[], now: number): void {
|
|
67
|
+
const cutoff = now - bucket.windowMs
|
|
60
68
|
let stale = 0
|
|
61
|
-
while (stale <
|
|
69
|
+
while (stale < hits.length && hits[stale]! <= cutoff)
|
|
62
70
|
stale++
|
|
63
71
|
if (stale > 0) {
|
|
64
|
-
|
|
65
|
-
hitCount -= stale
|
|
72
|
+
hits.splice(0, stale)
|
|
73
|
+
bucket.hitCount -= stale
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
|
|
69
77
|
function sweep(now = Date.now()): number {
|
|
70
78
|
let removed = 0
|
|
71
|
-
for (const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
for (const bucket of buckets) {
|
|
80
|
+
for (const [key, hits] of bucket.entries) {
|
|
81
|
+
trim(bucket, hits, now)
|
|
82
|
+
if (hits.length === 0) {
|
|
83
|
+
bucket.entries.delete(key)
|
|
84
|
+
totalKeys--
|
|
85
|
+
removed++
|
|
86
|
+
}
|
|
76
87
|
}
|
|
77
88
|
}
|
|
78
89
|
return removed
|
|
79
90
|
}
|
|
80
91
|
|
|
81
|
-
function evict(): void {
|
|
82
|
-
while (entries.size > maxKeys || hitCount > maxHits) {
|
|
83
|
-
const [key,
|
|
84
|
-
entries.delete(key)
|
|
85
|
-
hitCount -=
|
|
92
|
+
function evict(bucket: Bucket): void {
|
|
93
|
+
while (bucket.entries.size > maxKeys || bucket.hitCount > maxHits) {
|
|
94
|
+
const [key, hits] = bucket.entries.entries().next().value!
|
|
95
|
+
bucket.entries.delete(key)
|
|
96
|
+
bucket.hitCount -= hits.length
|
|
97
|
+
totalKeys--
|
|
86
98
|
}
|
|
87
99
|
}
|
|
88
100
|
|
|
89
101
|
return {
|
|
90
102
|
limiter(limit) {
|
|
91
103
|
const { max, windowMs } = limit
|
|
92
|
-
const
|
|
104
|
+
const bucket: Bucket = { entries: new Map(), windowMs, hitCount: 0 }
|
|
105
|
+
buckets.push(bucket)
|
|
93
106
|
if (timer === undefined) {
|
|
94
107
|
timer = setInterval(sweep, SWEEP_MS)
|
|
95
108
|
timer.unref()
|
|
@@ -97,23 +110,36 @@ export function createRateLimitStore(options: RateLimitStoreOptions = {}): RateL
|
|
|
97
110
|
return {
|
|
98
111
|
hit(key) {
|
|
99
112
|
const now = Date.now()
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
113
|
+
const existing = bucket.entries.get(key)
|
|
114
|
+
const hits = existing ?? []
|
|
115
|
+
if (existing !== undefined) {
|
|
116
|
+
trim(bucket, hits, now)
|
|
117
|
+
bucket.entries.delete(key)
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
totalKeys++
|
|
121
|
+
}
|
|
122
|
+
bucket.entries.set(key, hits)
|
|
123
|
+
if (hits.length >= max)
|
|
124
|
+
return secondsUntilFree(hits, max, windowMs, now)
|
|
125
|
+
hits.push(now)
|
|
126
|
+
bucket.hitCount++
|
|
127
|
+
evict(bucket)
|
|
110
128
|
return 0
|
|
111
129
|
},
|
|
130
|
+
peek(key) {
|
|
131
|
+
const hits = bucket.entries.get(key)
|
|
132
|
+
if (hits === undefined)
|
|
133
|
+
return 0
|
|
134
|
+
const now = Date.now()
|
|
135
|
+
trim(bucket, hits, now)
|
|
136
|
+
return hits.length >= max ? secondsUntilFree(hits, max, windowMs, now) : 0
|
|
137
|
+
},
|
|
112
138
|
}
|
|
113
139
|
},
|
|
114
140
|
sweep,
|
|
115
141
|
get size() {
|
|
116
|
-
return
|
|
142
|
+
return totalKeys
|
|
117
143
|
},
|
|
118
144
|
close() {
|
|
119
145
|
clearInterval(timer)
|
|
@@ -127,7 +153,47 @@ export function tooManyRequests(retryAfter: number): Response {
|
|
|
127
153
|
return Response.json({ ok: false, status: 429, error: 'Too many requests' }, { status: 429, headers: { 'retry-after': String(retryAfter) } })
|
|
128
154
|
}
|
|
129
155
|
|
|
130
|
-
|
|
156
|
+
// One warning per limit declaration, so a test or a second server does not inherit another's silence.
|
|
157
|
+
const warnedMissingIp = new WeakSet<object>()
|
|
158
|
+
|
|
159
|
+
const BRACKETED_RE = /^\[([^\]]+)\](?::\d+)?$/
|
|
160
|
+
const IPV4_PORT_RE = /^(\d{1,3}(?:\.\d{1,3}){3}):\d+$/
|
|
161
|
+
const MAPPED_IPV4_RE = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i
|
|
162
|
+
const HEXTET_RE = /^[\da-f]{1,4}$/i
|
|
163
|
+
|
|
164
|
+
/** The address a rate limit counts: an IPv4 address without its port or its `::ffff:` mapping, or the /64 network of an IPv6 address, any of whose 2^64 addresses one host may use. */
|
|
165
|
+
export function rateLimitAddress(ip: string): string {
|
|
166
|
+
const bare = BRACKETED_RE.exec(ip)?.[1] ?? IPV4_PORT_RE.exec(ip)?.[1] ?? ip
|
|
167
|
+
const addr = MAPPED_IPV4_RE.exec(bare)?.[1] ?? bare
|
|
168
|
+
const halves = addr.split('::')
|
|
169
|
+
if (!addr.includes(':') || halves.length > 2)
|
|
170
|
+
return addr
|
|
171
|
+
const head = halves[0] === '' ? [] : halves[0]!.split(':')
|
|
172
|
+
const tail = halves[1] ? halves[1].split(':') : []
|
|
173
|
+
// A dotted IPv4 ending holds the last two groups.
|
|
174
|
+
const tailGroups = tail.length + (tail.at(-1)?.includes('.') === true ? 1 : 0)
|
|
175
|
+
const zeros = halves.length === 2 ? 8 - head.length - tailGroups : 0
|
|
176
|
+
const groups = [...head, ...Array.from({ length: Math.max(0, zeros) }, () => '0'), ...tail].slice(0, 4)
|
|
177
|
+
if (groups.length < 4 || !groups.every(g => HEXTET_RE.test(g)))
|
|
178
|
+
return addr
|
|
179
|
+
return `${groups.map(g => Number.parseInt(g, 16).toString(16)).join(':')}::/64`
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The key `limit` counts `ctx` under: the limit's own `key`, else the client's network (`rateLimitAddress`), else one shared fallback that warns in production. */
|
|
183
|
+
export function rateLimitKey<Ctx extends { ip?: string | undefined }>(limit: RateLimit<Ctx>, ctx: Ctx): string {
|
|
184
|
+
const custom = limit.key?.(ctx)
|
|
185
|
+
if (custom !== undefined)
|
|
186
|
+
return `k:${custom}`
|
|
187
|
+
if (ctx.ip !== undefined && ctx.ip !== '')
|
|
188
|
+
return `ip:${rateLimitAddress(ctx.ip)}`
|
|
189
|
+
if (!warnedMissingIp.has(limit) && nodeEnv() === 'production') {
|
|
190
|
+
warnedMissingIp.add(limit)
|
|
191
|
+
console.warn('prunelle: rateLimit ran with no client IP (ctx.ip is undefined); configure trustProxy in prunelle.config.ts or pass remoteIp to app.handle() so callers do not share one fallback bucket.')
|
|
192
|
+
}
|
|
193
|
+
return 'ip:unknown'
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Counts `ctx` against `limiter` under `rateLimitKey`; returns 0 or the seconds to wait. */
|
|
131
197
|
export function hitFor<Ctx extends { ip?: string | undefined }>(limiter: RateLimiter, limit: RateLimit<Ctx>, ctx: Ctx): number {
|
|
132
|
-
return limiter.hit(limit
|
|
198
|
+
return limiter.hit(rateLimitKey(limit, ctx))
|
|
133
199
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export async function readStringFields(request: Request): Promise<Record<string, string>> {
|
|
9
9
|
const fields: Record<string, string> = Object.create(null)
|
|
10
|
-
const contentType = request.headers.get('content-type') ?? ''
|
|
10
|
+
const contentType = (request.headers.get('content-type') ?? '').toLowerCase()
|
|
11
11
|
if (contentType.includes('application/json')) {
|
|
12
12
|
const raw: unknown = await request.json()
|
|
13
13
|
if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
|