Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

mangle-private-members.mjs

217 lines8.1 KB

1// Keeps packages/db/mangle-cache.json safe. The db build renames every
2// property whose name is a cache key, anywhere in its output, so each key must
3// be a name that is only ever a TypeScript `private` class member.
4//
5// A name is safe when:
6// - every property-name use of it in packages/db/src, including constructor
7// parameter properties, resolves through the type checker to a `private`
8// member declared in packages/db/src;
9// - it never appears as a string literal in packages/db/src;
10// - no other package's src or tests reads `.name` or a quoted `'name'`.
11//
12// Each cache value must be unique and must not be an identifier anywhere in
13// db or db-ivm src, so a renamed member cannot collide with a real property.
14//
15// node scripts/mangle-private-members.mjs check the cache (CI)
16// node scripts/mangle-private-members.mjs --write update the cache
17//
18// --write keeps existing assignments, drops names that are no longer safe,
19// and gives new safe names the next free short names, most-used first.
20import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'
21import path from 'node:path'
22import { fileURLToPath } from 'node:url'
23import ts from 'typescript'
24​
25const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
26const cachePath = path.join(root, 'packages/db/mangle-cache.json')
27const write = process.argv.includes('--write')
28​
29const walk = (dir, exts) =>
30 readdirSync(dir).flatMap((entry) => {
31 const file = path.join(dir, entry)
32 if (entry === 'node_modules' || entry === 'dist') return []
33 if (statSync(file).isDirectory()) return walk(file, exts)
34 return exts.some((ext) => file.endsWith(ext)) ? [file] : []
35 })
36const dbSrc = path.join(root, 'packages/db/src')
37const dbFiles = walk(dbSrc, ['.ts'])
38const ivmFiles = walk(path.join(root, 'packages/db-ivm/src'), ['.ts'])
39​
40const config = ts.getParsedCommandLineOfConfigFile(
41 path.join(root, 'packages/db/tsconfig.json'),
42 {},
43 { ...ts.sys, onUnRecoverableConfigFileDiagnostic: () => {} },
45const program = ts.createProgram({
46 rootNames: dbFiles,
47 options: { ...config.options, noEmit: true },
48})
49const checker = program.getTypeChecker()
50const inDbSrc = (file) => path.resolve(file).startsWith(dbSrc + path.sep)
51const isPrivateMember = (decl) =>
52 (ts.isPropertyDeclaration(decl) ||
53 ts.isMethodDeclaration(decl) ||
54 ts.isGetAccessorDeclaration(decl) ||
55 ts.isSetAccessorDeclaration(decl) ||
56 ts.isParameter(decl)) &&
57 ts.getModifiers(decl)?.some((m) => m.kind === ts.SyntaxKind.PrivateKeyword) &&
58 inDbSrc(decl.getSourceFile().fileName)
59​
60const candidates = new Set()
61const useCounts = new Map()
62for (const file of dbFiles) {
63 const visit = (node) => {
64 if (isPrivateMember(node) && node.name && ts.isIdentifier(node.name))
65 candidates.add(node.name.text)
66 ts.forEachChild(node, visit)
67 }
68 visit(program.getSourceFile(file))
70​
71const unsafe = new Map()
72const mark = (name, reason) => {
73 if (candidates.has(name) && !unsafe.has(name)) unsafe.set(name, reason)
75for (const file of dbFiles) {
76 const sourceFile = program.getSourceFile(file)
77 const where = (node) =>
78 `${path.relative(root, file)}:${sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1}`
79 const checkName = (nameNode) => {
80 const name = nameNode.text
81 if (!candidates.has(name)) return
82 useCounts.set(name, (useCounts.get(name) ?? 0) + 1)
83 const symbol = checker.getSymbolAtLocation(nameNode)
84 const declarations = symbol?.declarations ?? []
85 if (declarations.length === 0)
86 return mark(name, `unresolved use at ${where(nameNode)}`)
87 const other = declarations.find((decl) => !isPrivateMember(decl))
88 if (other)
89 mark(
90 name,
91 `${ts.SyntaxKind[other.kind]} in ${path.relative(root, other.getSourceFile().fileName)}, used at ${where(nameNode)}`,
92 )
93 }
94 const visit = (node) => {
95 if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name))
96 checkName(node.name)
97 else if (
98 (ts.isPropertyAssignment(node) ||
99 ts.isPropertyDeclaration(node) ||
100 ts.isMethodDeclaration(node) ||
101 ts.isGetAccessorDeclaration(node) ||
102 ts.isSetAccessorDeclaration(node) ||
103 ts.isPropertySignature(node) ||
104 ts.isMethodSignature(node)) &&
105 node.name &&
106 ts.isIdentifier(node.name)
107 )
108 checkName(node.name)
109 // A constructor parameter with a modifier also declares a property.
110 else if (
111 ts.isParameter(node) &&
112 ts.isParameterPropertyDeclaration(node, node.parent) &&
113 ts.isIdentifier(node.name)
114 )
115 checkName(node.name)
116 else if (ts.isShorthandPropertyAssignment(node))
117 mark(node.name.text, `shorthand property at ${where(node)}`)
118 else if (ts.isBindingElement(node)) {
119 const key = node.propertyName ?? node.name
120 if (ts.isIdentifier(key))
121 mark(key.text, `destructuring at ${where(node)}`)
122 } else if (ts.isStringLiteralLike(node))
123 mark(node.text, `string literal at ${where(node)}`)
124 ts.forEachChild(node, visit)
125 }
126 visit(sourceFile)
128​
129const otherFiles = readdirSync(path.join(root, 'packages'))
130 .filter((pkg) => pkg !== 'db')
131 .flatMap((pkg) =>
132 ['src', 'tests', 'test', 'e2e'].flatMap((sub) => {
133 const dir = path.join(root, 'packages', pkg, sub)
134 try {
135 return walk(dir, ['.ts', '.tsx', '.js', '.mjs', '.svelte', '.vue'])
136 } catch {
137 return []
138 }
139 }),
140 )
141 .map((file) => [file, readFileSync(file, 'utf8')])
142for (const name of candidates) {
143 if (unsafe.has(name)) continue
144 const use = new RegExp(`\\.${name}\\b|[\`'"]${name}[\`'"]`)
145 const hit = otherFiles.find(([, text]) => use.test(text))
146 if (hit) mark(name, `used in ${path.relative(root, hit[0])}`)
148​
149const safe = new Set([...candidates].filter((name) => !unsafe.has(name)))
150const identifiers = new Set(
151 [...dbFiles, ...ivmFiles]
152 .map((file) => readFileSync(file, 'utf8'))
153 .join('\n')
154 .match(/[A-Za-z_$][\w$]*/g),
156const cache = JSON.parse(readFileSync(cachePath, 'utf8'))
157​
158if (write) {
159 const next = {}
160 const taken = new Set()
161 for (const [name, short] of Object.entries(cache))
162 if (safe.has(name) && !identifiers.has(short) && !taken.has(short)) {
163 next[name] = short
164 taken.add(short)
165 }
166 const first = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$`
167 const rest = `${first}0123456789`
168 function* shortNames() {
169 for (const a of first) yield a
170 for (const a of first) for (const b of rest) yield a + b
171 for (const a of first)
172 for (const b of rest) for (const c of rest) yield a + b + c
173 }
174 const free = shortNames()
175 const added = [...safe]
176 .filter((name) => !(name in next))
177 .sort(
178 (a, b) =>
179 (useCounts.get(b) ?? 0) * b.length -
180 (useCounts.get(a) ?? 0) * a.length || a.localeCompare(b),
181 )
182 for (const name of added) {
183 let short
184 do short = free.next().value
185 while (identifiers.has(short) || taken.has(short) || safe.has(short))
186 next[name] = short
187 taken.add(short)
188 }
189 writeFileSync(cachePath, `${JSON.stringify(next, null, 2)}\n`)
190 const dropped = Object.keys(cache).filter((name) => !(name in next))
191 console.log(
192 `mangle cache: ${Object.keys(next).length} names (${added.length} added, ${dropped.length} dropped)`,
193 )
194} else {
195 const problems = []
196 const shorts = new Map()
197 for (const [name, short] of Object.entries(cache)) {
198 if (!candidates.has(name))
199 problems.push(`${name}: no longer a private member (run --write)`)
200 else if (unsafe.has(name)) problems.push(`${name}: ${unsafe.get(name)}`)
201 if (identifiers.has(short))
202 problems.push(`${name} -> ${short}: ${short} is an identifier in src`)
203 if (shorts.has(short))
204 problems.push(`${name} -> ${short}: also used for ${shorts.get(short)}`)
205 shorts.set(short, name)
206 }
207 if (problems.length > 0) {
208 console.error(`Unsafe entries in packages/db/mangle-cache.json:`)
209 for (const problem of problems) console.error(` ${problem}`)
210 process.exit(1)
211 }
212 const missing = [...safe].filter((name) => !(name in cache)).length
213 console.log(
214 `mangle cache OK: ${Object.keys(cache).length} names` +
215 (missing > 0 ? ` (${missing} more safe names; --write adds them)` : ``),
216 )