返回 CodeWhale
bun-closure.ts
根目录 / crates / tui / extension-host / src / dsh / bun-closure.ts
1 /** Syntax preparation for Bun runtime imports; parsing never evaluates plugin code. */
2 import { parse } from 'acorn'
3 import { pathToFileURL } from 'node:url'
4
5 export const REVIEWED_IMPORT = Symbol.for('codewhale.extension-host.reviewed-import')
6
7 /** Keep module identity and import.meta source paths; guard every syntactic edge. */
8 export function prepareBunSource(source: string, path: string, checked: (specifier: string, require: boolean) => string): string {
9 const tree = parse(source, { ecmaVersion: 'latest', sourceType: path.endsWith('.cjs') ? 'commonjs' : 'module' })
10 const pending: object[] = [tree]
11 const edits: { start: number; end: number; source: string }[] = []
12 let nodes = 0
13 while (pending.length) {
14 const node = pending.pop() as Record<string, any>
15 if (++nodes > 500_000) throw new Error('reviewed module syntax exceeds the preparation bound')
16 if (['ImportDeclaration', 'ExportNamedDeclaration', 'ExportAllDeclaration'].includes(node.type) && node.source) {
17 const target = checked(node.source.value, false)
18 edits.push({start:node.source.start,end:node.source.end,source:JSON.stringify(target)})
19 }
20 if (node.type === 'ImportExpression') {
21 // The source expression remains unevaluated until the original call site.
22 // The host helper then checks its exact receipt, including computed edges.
23 edits.push({start:node.start,end:node.start+6,source:`globalThis[Symbol.for(${JSON.stringify(REVIEWED_IMPORT.description)})]`})
24 edits.push({start:node.source.end,end:node.source.end,source:`,${JSON.stringify(pathToFileURL(path).href)}`})
25 }
26 if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require') {
27 if (node.arguments.length !== 1 || node.arguments[0].type !== 'Literal' || typeof node.arguments[0].value !== 'string') {
28 throw new Error('computed composition require calls require the diagnosed Node resolver')
29 }
30 const argument=node.arguments[0]
31 edits.push({start:argument.start,end:argument.end,source:JSON.stringify(checked(argument.value,true))})
32 }
33 if (node.type === 'MemberExpression' && !node.computed
34 && ((node.object.type==='MetaProperty' && node.property.name==='require')
35 || (node.object.type==='Identifier' && node.object.name==='require' && node.property.name==='resolve'))) {
36 throw new Error('runtime composition require resolution requires the diagnosed Node resolver')
37 }
38 for (const value of Object.values(node)) {
39 if (Array.isArray(value)) {
40 for (const child of value) if (child && typeof child === 'object' && typeof child.type === 'string') pending.push(child)
41 } else if (value && typeof value === 'object' && typeof value.type === 'string') pending.push(value)
42 }
43 }
44 for (const edit of edits.sort((a,b)=>b.start-a.start || b.end-a.end)) source=source.slice(0,edit.start)+edit.source+source.slice(edit.end)
45 return source
46 }
47
47 lines TYPESCRIPT