|
| 1 | +import { readFileSync, writeFileSync } from 'node:fs' |
| 2 | + |
| 3 | +const upstreamTypesPath = './wa-sqlite/src/types/index.d.ts' |
| 4 | +const facadeVFSPath = './wa-sqlite/src/FacadeVFS.js' |
| 5 | +const typesOutputPath = './src/sqlite-types.d.ts' |
| 6 | +const modulesOutputPath = './src/wa-sqlite-modules.d.ts' |
| 7 | + |
| 8 | +const upstreamDeclarations = [ |
| 9 | + 'SQLiteCompatibleType', |
| 10 | + 'SQLiteVFS', |
| 11 | + 'SQLitePrepareOptions', |
| 12 | + 'SQLiteAPI', |
| 13 | +] as const |
| 14 | + |
| 15 | +const moduleDeclarations = String.raw` |
| 16 | +declare module 'wa-sqlite/src/examples/IDBBatchAtomicVFS.js' { |
| 17 | + export class IDBBatchAtomicVFS { |
| 18 | + static create(name: string, module: any, options: any): Promise<any> |
| 19 | + } |
| 20 | +} |
| 21 | +
|
| 22 | +declare module 'wa-sqlite/src/examples/IDBMirrorVFS.js' { |
| 23 | + export class IDBMirrorVFS { |
| 24 | + static create(name: string, module: any): Promise<any> |
| 25 | + } |
| 26 | +} |
| 27 | +
|
| 28 | +declare module 'wa-sqlite/src/examples/OPFSCoopSyncVFS.js' { |
| 29 | + export class OPFSCoopSyncVFS { |
| 30 | + static create(name: string, module: any): Promise<any> |
| 31 | + } |
| 32 | +} |
| 33 | +
|
| 34 | +declare module 'wa-sqlite/src/examples/OPFSWriteAheadVFS.js' { |
| 35 | + export class OPFSWriteAheadVFS { |
| 36 | + static create(name: string, module: any, options?: any): Promise<any> |
| 37 | + } |
| 38 | +} |
| 39 | +
|
| 40 | +declare module 'wa-sqlite/src/examples/MemoryVFS.js' { |
| 41 | + export class MemoryVFS { |
| 42 | + static create(name: string, module: any): Promise<any> |
| 43 | + } |
| 44 | +} |
| 45 | +
|
| 46 | +declare module 'wa-sqlite-fts5/wa-sqlite.mjs' { |
| 47 | + const SQLiteESMFactory: (moduleArg?: { locateFile?: (path: string) => string }) => Promise<any> |
| 48 | +
|
| 49 | + export default SQLiteESMFactory |
| 50 | +} |
| 51 | +
|
| 52 | +declare module 'wa-sqlite-fts5/wa-sqlite-async.mjs' { |
| 53 | + const SQLiteAsyncESMFactory: (moduleArg?: { |
| 54 | + locateFile?: (path: string) => string |
| 55 | + }) => Promise<any> |
| 56 | +
|
| 57 | + export default SQLiteAsyncESMFactory |
| 58 | +} |
| 59 | +`.trim() |
| 60 | + |
| 61 | +function extractDeclaration(source: string, name: (typeof upstreamDeclarations)[number]): string { |
| 62 | + const typeStart = source.search(new RegExp(`(?:declare\\s+)?type\\s+${name}\\b`)) |
| 63 | + if (typeStart >= 0) { |
| 64 | + const end = source.indexOf(';', typeStart) |
| 65 | + return source.slice(typeStart, end + 1) |
| 66 | + } |
| 67 | + |
| 68 | + const interfaceStart = source.search(new RegExp(`(?:declare\\s+)?interface\\s+${name}\\b`)) |
| 69 | + if (interfaceStart < 0) { |
| 70 | + throw new Error(`Unable to find ${name} in ${upstreamTypesPath}`) |
| 71 | + } |
| 72 | + |
| 73 | + const bodyStart = source.indexOf('{', interfaceStart) |
| 74 | + let depth = 0 |
| 75 | + for (let index = bodyStart; index < source.length; index++) { |
| 76 | + const char = source[index] |
| 77 | + if (char === '{') { |
| 78 | + depth++ |
| 79 | + } else if (char === '}') { |
| 80 | + depth-- |
| 81 | + if (depth === 0) { |
| 82 | + return source.slice(interfaceStart, index + 1) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + throw new Error(`Unable to parse ${name} in ${upstreamTypesPath}`) |
| 88 | +} |
| 89 | + |
| 90 | +function toExportedDeclaration(declaration: string): string { |
| 91 | + return declaration.replace(/^(?:declare\s+)?(interface|type)\s+/, 'export $1 ') |
| 92 | +} |
| 93 | + |
| 94 | +function patchUpstreamDeclaration(declaration: string): string { |
| 95 | + return declaration |
| 96 | + .replace('changes(db): number;', 'changes(db: number): number;') |
| 97 | + .replace('close(db): Promise<number>;', 'close(db: number): Promise<number>;') |
| 98 | + .replace( |
| 99 | + /progress_handler\(\s*db:\s*number,\s*nProgressOps:\s*number,\s*handler:\s*\(userData:\s*any\)\s*=>\s*number\s*\|\s*Promise<number>,\s*userData,?\s*\);?/, |
| 100 | + 'progress_handler(db: number, nProgressOps: number, handler: (userData: any) => number | Promise<number>, userData: any): void;', |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +function toTypeAnnotation(jsDocType: string): string { |
| 105 | + return jsDocType |
| 106 | + .replace('string?', 'string | null') |
| 107 | + .replace('number|Promise<number>', 'number | Promise<number>') |
| 108 | +} |
| 109 | + |
| 110 | +function generateFacadeVFSDeclaration(source: string): string { |
| 111 | + const methods: string[] = [] |
| 112 | + const methodPattern = /\/\*\*([\s\S]*?)\*\/\n\s+([a-zA-Z]\w*)\(([^)]*)\)\s*\{/g |
| 113 | + |
| 114 | + for (const match of source.matchAll(methodPattern)) { |
| 115 | + const [, jsdoc = '', methodName = '', rawParams = ''] = match |
| 116 | + if (!methodName || methodName === 'constructor' || methodName === 'hasAsyncMethod') { |
| 117 | + continue |
| 118 | + } |
| 119 | + if (!methodName.startsWith('j') && methodName !== 'getFilename') { |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + const params = rawParams |
| 124 | + .split(',') |
| 125 | + .map((param) => param.trim()) |
| 126 | + .filter(Boolean) |
| 127 | + .map((param) => { |
| 128 | + const paramType = jsdoc.match(new RegExp(`@param \\{([^}]+)\\} ${param}\\b`))?.[1] |
| 129 | + if (!paramType) { |
| 130 | + throw new Error(`Unable to find JSDoc type for FacadeVFS.${methodName}(${param})`) |
| 131 | + } |
| 132 | + return `${param}: ${toTypeAnnotation(paramType)}` |
| 133 | + }) |
| 134 | + .join(', ') |
| 135 | + |
| 136 | + const returnType = toTypeAnnotation(jsdoc.match(/@returns \{([^}]+)\}/)?.[1] ?? 'void') |
| 137 | + methods.push(` ${methodName}(${params}): ${returnType}`) |
| 138 | + } |
| 139 | + |
| 140 | + if (methods.length === 0) { |
| 141 | + throw new Error(`Unable to generate FacadeVFS from ${facadeVFSPath}`) |
| 142 | + } |
| 143 | + |
| 144 | + return `export interface FacadeVFS extends SQLiteVFS {\n${methods.join('\n\n')}\n}` |
| 145 | +} |
| 146 | + |
| 147 | +function generateSQLiteTypes(): void { |
| 148 | + const upstreamTypes = readFileSync(upstreamTypesPath, 'utf8') |
| 149 | + const facadeVFS = readFileSync(facadeVFSPath, 'utf8') |
| 150 | + const sqliteDeclarations = upstreamDeclarations |
| 151 | + .map((name) => |
| 152 | + patchUpstreamDeclaration(toExportedDeclaration(extractDeclaration(upstreamTypes, name))), |
| 153 | + ) |
| 154 | + .join('\n\n') |
| 155 | + |
| 156 | + writeFileSync( |
| 157 | + typesOutputPath, |
| 158 | + `// Generated from ${upstreamTypesPath} and ${facadeVFSPath}. Do not edit manually.\n/* eslint-disable */\n\n${sqliteDeclarations}\n\n${generateFacadeVFSDeclaration(facadeVFS)}\n`, |
| 159 | + ) |
| 160 | + writeFileSync( |
| 161 | + modulesOutputPath, |
| 162 | + `// Generated module declarations for vendored wa-sqlite entry points. Do not edit manually.\n/* eslint-disable */\n\n${moduleDeclarations}\n`, |
| 163 | + ) |
| 164 | +} |
| 165 | + |
| 166 | +generateSQLiteTypes() |
| 167 | + |
| 168 | +export { generateSQLiteTypes } |
0 commit comments