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
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// What must never reach a browser bundle, shared by `prunelle build` and `prunelle check`.
|
|
2
|
+
|
|
3
|
+
import type { MemberExpression, Node, Program } from 'oxc-parser'
|
|
4
|
+
import { visitorKeys } from 'oxc-parser'
|
|
5
|
+
import { loadDeclarations } from './analyze'
|
|
6
|
+
import { unwrapTsExpr } from './shared'
|
|
7
|
+
|
|
8
|
+
export const SERVER_ONLY_MODULES = new Set([
|
|
9
|
+
'prunelle/server',
|
|
10
|
+
'prunelle/config',
|
|
11
|
+
'prunelle/internal/server',
|
|
12
|
+
'prunelle/internal/ssr',
|
|
13
|
+
'prunelle/build',
|
|
14
|
+
'prunelle/test',
|
|
15
|
+
'bun',
|
|
16
|
+
'bun:sqlite',
|
|
17
|
+
'bun:sql',
|
|
18
|
+
'bun:ffi',
|
|
19
|
+
'bun:dns',
|
|
20
|
+
'bun:test',
|
|
21
|
+
'bun:jsc',
|
|
22
|
+
'node:assert',
|
|
23
|
+
'node:assert/strict',
|
|
24
|
+
'node:async_hooks',
|
|
25
|
+
'node:buffer',
|
|
26
|
+
'node:child_process',
|
|
27
|
+
'node:cluster',
|
|
28
|
+
'node:constants',
|
|
29
|
+
'node:crypto',
|
|
30
|
+
'node:dgram',
|
|
31
|
+
'node:diagnostics_channel',
|
|
32
|
+
'node:dns',
|
|
33
|
+
'node:dns/promises',
|
|
34
|
+
'node:domain',
|
|
35
|
+
'node:events',
|
|
36
|
+
'node:fs',
|
|
37
|
+
'node:fs/promises',
|
|
38
|
+
'node:http',
|
|
39
|
+
'node:http2',
|
|
40
|
+
'node:https',
|
|
41
|
+
'node:inspector',
|
|
42
|
+
'node:inspector/promises',
|
|
43
|
+
'node:module',
|
|
44
|
+
'node:net',
|
|
45
|
+
'node:os',
|
|
46
|
+
'node:path',
|
|
47
|
+
'node:path/posix',
|
|
48
|
+
'node:path/win32',
|
|
49
|
+
'node:perf_hooks',
|
|
50
|
+
'node:process',
|
|
51
|
+
'node:punycode',
|
|
52
|
+
'node:querystring',
|
|
53
|
+
'node:readline',
|
|
54
|
+
'node:readline/promises',
|
|
55
|
+
'node:sqlite',
|
|
56
|
+
'node:stream',
|
|
57
|
+
'node:stream/promises',
|
|
58
|
+
'node:stream/web',
|
|
59
|
+
'node:string_decoder',
|
|
60
|
+
'node:test',
|
|
61
|
+
'node:timers',
|
|
62
|
+
'node:timers/promises',
|
|
63
|
+
'node:tls',
|
|
64
|
+
'node:trace_events',
|
|
65
|
+
'node:tty',
|
|
66
|
+
'node:url',
|
|
67
|
+
'node:util',
|
|
68
|
+
'node:v8',
|
|
69
|
+
'node:vm',
|
|
70
|
+
'node:wasi',
|
|
71
|
+
'node:worker_threads',
|
|
72
|
+
'node:zlib',
|
|
73
|
+
'fs',
|
|
74
|
+
'fs/promises',
|
|
75
|
+
'child_process',
|
|
76
|
+
'crypto',
|
|
77
|
+
'dns',
|
|
78
|
+
'dns/promises',
|
|
79
|
+
'async_hooks',
|
|
80
|
+
'module',
|
|
81
|
+
'vm',
|
|
82
|
+
'os',
|
|
83
|
+
'http2',
|
|
84
|
+
'worker_threads',
|
|
85
|
+
'cluster',
|
|
86
|
+
'dgram',
|
|
87
|
+
'tls',
|
|
88
|
+
'readline',
|
|
89
|
+
'readline/promises',
|
|
90
|
+
'net',
|
|
91
|
+
'http',
|
|
92
|
+
'https',
|
|
93
|
+
'zlib',
|
|
94
|
+
'perf_hooks',
|
|
95
|
+
'inspector',
|
|
96
|
+
'inspector/promises',
|
|
97
|
+
'sqlite',
|
|
98
|
+
'v8',
|
|
99
|
+
'tty',
|
|
100
|
+
'diagnostics_channel',
|
|
101
|
+
'wasi',
|
|
102
|
+
])
|
|
103
|
+
|
|
104
|
+
export function isServerOnlyModule(source: string): boolean {
|
|
105
|
+
return source.startsWith('node:') || source.startsWith('bun:') || SERVER_ONLY_MODULES.has(source)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const SERVER_ONLY_FILTER = new RegExp(`^(?:node:.+|bun:.+|${[...SERVER_ONLY_MODULES].map(m => m.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')).join('|')})$`)
|
|
109
|
+
|
|
110
|
+
// The client build inlines PUBLIC_* (from its own environment) and NODE_ENV; any other read ships a server variable name and crashes in the browser.
|
|
111
|
+
const CLIENT_ENV_KEY = /^(?:PUBLIC_\w*|NODE_ENV)$/
|
|
112
|
+
const IMPORT_META_SAFE_KEYS = new Set(['MODE', 'DEV', 'PROD'])
|
|
113
|
+
const TS_EXPR_WRAPPERS = new Set([
|
|
114
|
+
'TSAsExpression',
|
|
115
|
+
'TSSatisfiesExpression',
|
|
116
|
+
'TSNonNullExpression',
|
|
117
|
+
'TSTypeAssertion',
|
|
118
|
+
'TSInstantiationExpression',
|
|
119
|
+
])
|
|
120
|
+
|
|
121
|
+
type EnvRootKind = 'process' | 'bun' | 'import.meta'
|
|
122
|
+
|
|
123
|
+
function propKeyName(member: MemberExpression): string | undefined {
|
|
124
|
+
if (!member.computed && member.property.type === 'Identifier')
|
|
125
|
+
return member.property.name
|
|
126
|
+
if (member.computed && member.property.type === 'Literal' && typeof member.property.value === 'string')
|
|
127
|
+
return member.property.value
|
|
128
|
+
return undefined
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isEnvRoot(node: Node, shadowed: ReadonlySet<string>): EnvRootKind | undefined {
|
|
132
|
+
const inner = unwrapTsExpr(node)
|
|
133
|
+
if (inner.type !== 'MemberExpression' || propKeyName(inner) !== 'env')
|
|
134
|
+
return undefined
|
|
135
|
+
const obj = unwrapTsExpr(inner.object)
|
|
136
|
+
if (obj.type === 'MetaProperty' && obj.meta.name === 'import' && obj.property.name === 'meta')
|
|
137
|
+
return 'import.meta'
|
|
138
|
+
if (obj.type === 'Identifier') {
|
|
139
|
+
if (obj.name === 'process' && !shadowed.has('process'))
|
|
140
|
+
return 'process'
|
|
141
|
+
if (obj.name === 'Bun' && !shadowed.has('Bun'))
|
|
142
|
+
return 'bun'
|
|
143
|
+
}
|
|
144
|
+
if (obj.type === 'MemberExpression') {
|
|
145
|
+
const base = unwrapTsExpr(obj.object)
|
|
146
|
+
if (base.type === 'Identifier' && base.name === 'globalThis' && !shadowed.has('globalThis')) {
|
|
147
|
+
const key = propKeyName(obj)
|
|
148
|
+
if (key === 'process')
|
|
149
|
+
return 'process'
|
|
150
|
+
if (key === 'Bun')
|
|
151
|
+
return 'bun'
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return undefined
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface PrivateEnvMatch {
|
|
158
|
+
offset: number
|
|
159
|
+
kind: 'process' | 'bun'
|
|
160
|
+
/** More specific source (`'import.meta'`) while preserving `'process' | 'bun'` on `kind`. */
|
|
161
|
+
source?: EnvRootKind | undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
interface EnvScanState {
|
|
165
|
+
found: PrivateEnvMatch | undefined
|
|
166
|
+
loadCode: ReadonlySet<Node>
|
|
167
|
+
isInlined: (key: string) => boolean
|
|
168
|
+
acceptOffset: ((offset: number) => boolean) | undefined
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function checkEnvMember(n: MemberExpression, shadowed: ReadonlySet<string>, state: EnvScanState): boolean {
|
|
172
|
+
const rootKind = isEnvRoot(n.object, shadowed)
|
|
173
|
+
if (rootKind !== undefined) {
|
|
174
|
+
if (state.acceptOffset !== undefined && !state.acceptOffset(n.start))
|
|
175
|
+
return true
|
|
176
|
+
if (rootKind === 'bun') {
|
|
177
|
+
state.found = { offset: n.start, kind: 'bun', source: 'bun' }
|
|
178
|
+
return true
|
|
179
|
+
}
|
|
180
|
+
const key = propKeyName(n)
|
|
181
|
+
if (rootKind === 'import.meta') {
|
|
182
|
+
const allowed = key !== undefined && (IMPORT_META_SAFE_KEYS.has(key) || (key.startsWith('PUBLIC_') && state.isInlined(key)))
|
|
183
|
+
if (!allowed)
|
|
184
|
+
state.found = { offset: n.start, kind: 'process', source: 'import.meta' }
|
|
185
|
+
return true
|
|
186
|
+
}
|
|
187
|
+
if (key === undefined || !state.isInlined(key))
|
|
188
|
+
state.found = { offset: n.start, kind: 'process', source: 'process' }
|
|
189
|
+
return true
|
|
190
|
+
}
|
|
191
|
+
const directKind = isEnvRoot(n, shadowed)
|
|
192
|
+
if (directKind !== undefined) {
|
|
193
|
+
if (state.acceptOffset === undefined || state.acceptOffset(n.start))
|
|
194
|
+
state.found = { offset: n.start, kind: directKind === 'bun' ? 'bun' : 'process', source: directKind }
|
|
195
|
+
return true
|
|
196
|
+
}
|
|
197
|
+
return false
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function visitEnvPattern(pattern: Node | null | undefined, shadowed: Set<string>, state: EnvScanState): void {
|
|
201
|
+
if (state.found !== undefined || pattern === null || pattern === undefined)
|
|
202
|
+
return
|
|
203
|
+
switch (pattern.type) {
|
|
204
|
+
case 'Identifier':
|
|
205
|
+
shadowed.add(pattern.name)
|
|
206
|
+
return
|
|
207
|
+
case 'AssignmentPattern':
|
|
208
|
+
visitEnvNode(pattern.right, shadowed, state)
|
|
209
|
+
visitEnvPattern(pattern.left, shadowed, state)
|
|
210
|
+
return
|
|
211
|
+
case 'RestElement':
|
|
212
|
+
visitEnvPattern(pattern.argument, shadowed, state)
|
|
213
|
+
return
|
|
214
|
+
case 'TSParameterProperty':
|
|
215
|
+
visitEnvPattern(pattern.parameter, shadowed, state)
|
|
216
|
+
return
|
|
217
|
+
case 'ArrayPattern':
|
|
218
|
+
for (const el of pattern.elements) {
|
|
219
|
+
if (el !== null)
|
|
220
|
+
visitEnvPattern(el, shadowed, state)
|
|
221
|
+
}
|
|
222
|
+
return
|
|
223
|
+
case 'ObjectPattern':
|
|
224
|
+
for (const prop of pattern.properties) {
|
|
225
|
+
if (prop.type === 'RestElement') {
|
|
226
|
+
visitEnvPattern(prop.argument, shadowed, state)
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
if (prop.computed)
|
|
230
|
+
visitEnvNode(prop.key, shadowed, state)
|
|
231
|
+
visitEnvPattern(prop.value, shadowed, state)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function visitEnvStatements(body: readonly Node[], shadowed: Set<string>, state: EnvScanState): void {
|
|
238
|
+
for (const stmt of body) {
|
|
239
|
+
const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') ? stmt.declaration : stmt
|
|
240
|
+
if ((decl?.type === 'FunctionDeclaration' || decl?.type === 'ClassDeclaration') && decl.id !== null)
|
|
241
|
+
shadowed.add(decl.id.name)
|
|
242
|
+
}
|
|
243
|
+
for (const stmt of body) {
|
|
244
|
+
const varDecl = stmt.type === 'VariableDeclaration'
|
|
245
|
+
? stmt
|
|
246
|
+
: stmt.type === 'ExportNamedDeclaration' && stmt.declaration?.type === 'VariableDeclaration'
|
|
247
|
+
? stmt.declaration
|
|
248
|
+
: undefined
|
|
249
|
+
if (varDecl !== undefined) {
|
|
250
|
+
for (const d of varDecl.declarations) {
|
|
251
|
+
visitEnvNode(d.init, shadowed, state)
|
|
252
|
+
visitEnvPattern(d.id, shadowed, state)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
visitEnvNode(stmt, shadowed, state)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function visitEnvScoped(n: Node, shadowed: Set<string>, state: EnvScanState): boolean {
|
|
262
|
+
if (n.type === 'ArrowFunctionExpression' || n.type === 'FunctionExpression' || n.type === 'FunctionDeclaration') {
|
|
263
|
+
const next = new Set(shadowed)
|
|
264
|
+
if (n.type !== 'ArrowFunctionExpression' && n.id !== null)
|
|
265
|
+
next.add(n.id.name)
|
|
266
|
+
for (const p of n.params)
|
|
267
|
+
visitEnvPattern(p, next, state)
|
|
268
|
+
visitEnvNode(n.body, next, state)
|
|
269
|
+
return true
|
|
270
|
+
}
|
|
271
|
+
if (n.type === 'Program' || n.type === 'BlockStatement') {
|
|
272
|
+
visitEnvStatements(n.body, new Set(shadowed), state)
|
|
273
|
+
return true
|
|
274
|
+
}
|
|
275
|
+
if (n.type === 'CatchClause') {
|
|
276
|
+
const next = new Set(shadowed)
|
|
277
|
+
visitEnvPattern(n.param, next, state)
|
|
278
|
+
visitEnvNode(n.body, next, state)
|
|
279
|
+
return true
|
|
280
|
+
}
|
|
281
|
+
if (n.type === 'ForInStatement' || n.type === 'ForOfStatement') {
|
|
282
|
+
visitEnvNode(n.right, shadowed, state)
|
|
283
|
+
const next = new Set(shadowed)
|
|
284
|
+
if (n.left.type === 'VariableDeclaration')
|
|
285
|
+
for (const d of n.left.declarations) visitEnvPattern(d.id, next, state)
|
|
286
|
+
else
|
|
287
|
+
visitEnvNode(n.left, shadowed, state)
|
|
288
|
+
visitEnvNode(n.body, next, state)
|
|
289
|
+
return true
|
|
290
|
+
}
|
|
291
|
+
if (n.type === 'ForStatement') {
|
|
292
|
+
const next = new Set(shadowed)
|
|
293
|
+
if (n.init?.type === 'VariableDeclaration') {
|
|
294
|
+
for (const d of n.init.declarations) {
|
|
295
|
+
visitEnvNode(d.init, next, state)
|
|
296
|
+
visitEnvPattern(d.id, next, state)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
visitEnvNode(n.init, next, state)
|
|
301
|
+
}
|
|
302
|
+
visitEnvNode(n.test, next, state)
|
|
303
|
+
visitEnvNode(n.update, next, state)
|
|
304
|
+
visitEnvNode(n.body, next, state)
|
|
305
|
+
return true
|
|
306
|
+
}
|
|
307
|
+
return false
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function visitEnvNode(node: unknown, shadowed: Set<string>, state: EnvScanState): void {
|
|
311
|
+
if (state.found !== undefined || node === null || typeof node !== 'object')
|
|
312
|
+
return
|
|
313
|
+
if (Array.isArray(node)) {
|
|
314
|
+
for (let i = 0; i < node.length; i++)
|
|
315
|
+
visitEnvNode(node[i], shadowed, state)
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
const n = node as Node
|
|
319
|
+
if (typeof n.type !== 'string' || state.loadCode.has(n))
|
|
320
|
+
return
|
|
321
|
+
if (n.type === 'MemberExpression' && checkEnvMember(n, shadowed, state))
|
|
322
|
+
return
|
|
323
|
+
if (visitEnvScoped(n, shadowed, state))
|
|
324
|
+
return
|
|
325
|
+
if (n.type.startsWith('TS')) {
|
|
326
|
+
if (TS_EXPR_WRAPPERS.has(n.type))
|
|
327
|
+
visitEnvNode((n as { expression?: unknown }).expression, shadowed, state)
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
const keys = visitorKeys[n.type]
|
|
331
|
+
if (keys === undefined)
|
|
332
|
+
return
|
|
333
|
+
const rec = n as unknown as Record<string, unknown>
|
|
334
|
+
for (let i = 0; i < keys.length; i++) {
|
|
335
|
+
const val = rec[keys[i]!]
|
|
336
|
+
if (val !== null && typeof val === 'object')
|
|
337
|
+
visitEnvNode(val, shadowed, state)
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Offset and source (`process`, `bun`, or `import.meta`) of the first private env read outside `load()`, or `undefined`. */
|
|
342
|
+
export function findPrivateEnv(
|
|
343
|
+
program: Program,
|
|
344
|
+
isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key),
|
|
345
|
+
acceptOffset?: (offset: number) => boolean,
|
|
346
|
+
): PrivateEnvMatch | undefined {
|
|
347
|
+
const state: EnvScanState = {
|
|
348
|
+
found: undefined,
|
|
349
|
+
loadCode: new Set<Node>(loadDeclarations(program)),
|
|
350
|
+
isInlined,
|
|
351
|
+
acceptOffset,
|
|
352
|
+
}
|
|
353
|
+
visitEnvNode(program, new Set(), state)
|
|
354
|
+
return state.found
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Byte offset of the first private `process.env`, `Bun.env`, or `import.meta.env` read outside `load()`, or `undefined`. */
|
|
358
|
+
export function findPrivateEnvOffset(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): number | undefined {
|
|
359
|
+
return findPrivateEnv(program, isInlined)?.offset
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** True when the module reads private `process.env`, `Bun.env`, or `import.meta.env` outside its `load()` (stripped from client code). */
|
|
363
|
+
export function readsPrivateEnv(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): boolean {
|
|
364
|
+
return findPrivateEnvOffset(program, isInlined) !== undefined
|
|
365
|
+
}
|