|
| 1 | +/** |
| 2 | + * Auto-discovers fetchRaw* methods and their param types from exchange |
| 3 | + * fetcher source files and BaseExchange.ts interface definitions. |
| 4 | + */ |
| 5 | + |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as path from 'path'; |
| 8 | + |
| 9 | +export interface ParamField { |
| 10 | + name: string; |
| 11 | + type: string; |
| 12 | +} |
| 13 | + |
| 14 | +export interface DiscoveredMethod { |
| 15 | + exchange: string; |
| 16 | + method: string; |
| 17 | + paramType: string; |
| 18 | + fields: ParamField[]; |
| 19 | + argPattern: 'params-only' | 'id-params' | 'params-wallet' | 'id-params-extra'; |
| 20 | +} |
| 21 | + |
| 22 | +function parseInterfaces(source: string): Map<string, ParamField[]> { |
| 23 | + const result = new Map<string, ParamField[]>(); |
| 24 | + const re = /export\s+interface\s+(\w+)(?:\s+extends\s+([\w,\s]+))?\s*\{/g; |
| 25 | + |
| 26 | + let match: RegExpExecArray | null; |
| 27 | + while ((match = re.exec(source)) !== null) { |
| 28 | + const name = match[1]; |
| 29 | + const startBrace = match.index + match[0].length - 1; |
| 30 | + let depth = 1; |
| 31 | + let pos = startBrace + 1; |
| 32 | + while (pos < source.length && depth > 0) { |
| 33 | + if (source[pos] === '{') depth++; |
| 34 | + if (source[pos] === '}') depth--; |
| 35 | + pos++; |
| 36 | + } |
| 37 | + const body = source.slice(startBrace + 1, pos - 1); |
| 38 | + result.set(name, parseFieldsFromBody(body)); |
| 39 | + } |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +function parseExtends(source: string): Map<string, string[]> { |
| 44 | + const result = new Map<string, string[]>(); |
| 45 | + const re = /export\s+interface\s+(\w+)\s+extends\s+([\w,\s]+)\s*\{/g; |
| 46 | + let match: RegExpExecArray | null; |
| 47 | + while ((match = re.exec(source)) !== null) { |
| 48 | + result.set(match[1], match[2].split(',').map((s) => s.trim()).filter(Boolean)); |
| 49 | + } |
| 50 | + return result; |
| 51 | +} |
| 52 | + |
| 53 | +function parseFieldsFromBody(body: string): ParamField[] { |
| 54 | + const fields: ParamField[] = []; |
| 55 | + const stripped = body.replace(/\/\*\*[\s\S]*?\*\//g, ''); |
| 56 | + const re = /^\s*(\w+)\??\s*:\s*([^;/\n]+)/gm; |
| 57 | + let m: RegExpExecArray | null; |
| 58 | + while ((m = re.exec(stripped)) !== null) { |
| 59 | + fields.push({ name: m[1], type: normalizeType(m[2].trim()) }); |
| 60 | + } |
| 61 | + return fields; |
| 62 | +} |
| 63 | + |
| 64 | +function normalizeType(raw: string): string { |
| 65 | + if (/^'[^']*'(\s*\|\s*'[^']*')*$/.test(raw)) return 'string'; |
| 66 | + if (raw === 'CandleInterval') return 'string'; |
| 67 | + return raw; |
| 68 | +} |
| 69 | + |
| 70 | +function resolveFields( |
| 71 | + name: string, |
| 72 | + ownFields: Map<string, ParamField[]>, |
| 73 | + extendsMap: Map<string, string[]>, |
| 74 | + visited: Set<string> = new Set(), |
| 75 | +): ParamField[] { |
| 76 | + if (visited.has(name)) return []; |
| 77 | + visited.add(name); |
| 78 | + |
| 79 | + const own = ownFields.get(name) ?? []; |
| 80 | + const parents = extendsMap.get(name) ?? []; |
| 81 | + const inherited: ParamField[] = []; |
| 82 | + for (const parent of parents) { |
| 83 | + inherited.push(...resolveFields(parent, ownFields, extendsMap, visited)); |
| 84 | + } |
| 85 | + |
| 86 | + const byName = new Map<string, ParamField>(); |
| 87 | + for (const f of inherited) byName.set(f.name, f); |
| 88 | + for (const f of own) byName.set(f.name, f); |
| 89 | + return Array.from(byName.values()); |
| 90 | +} |
| 91 | + |
| 92 | +function parseMethodSignatures(source: string): { method: string; paramType: string; argPattern: DiscoveredMethod['argPattern'] }[] { |
| 93 | + const results: { method: string; paramType: string; argPattern: DiscoveredMethod['argPattern'] }[] = []; |
| 94 | + const methodRe = /(?:^|\n)\s+async\s+(fetchRaw\w+)\s*\(/g; |
| 95 | + |
| 96 | + let match: RegExpExecArray | null; |
| 97 | + while ((match = methodRe.exec(source)) !== null) { |
| 98 | + const lineStart = source.lastIndexOf('\n', match.index) + 1; |
| 99 | + const prefix = source.slice(lineStart, match.index + match[0].length); |
| 100 | + if (/private\s+async\s+fetchRaw/.test(prefix)) continue; |
| 101 | + |
| 102 | + const argsStart = match.index + match[0].length; |
| 103 | + let depth = 1; |
| 104 | + let pos = argsStart; |
| 105 | + while (pos < source.length && depth > 0) { |
| 106 | + if (source[pos] === '(') depth++; |
| 107 | + if (source[pos] === ')') depth--; |
| 108 | + pos++; |
| 109 | + } |
| 110 | + |
| 111 | + const argsBody = source.slice(argsStart, pos - 1); |
| 112 | + const args = splitArgs(argsBody); |
| 113 | + |
| 114 | + let paramIdx = -1; |
| 115 | + let paramType = ''; |
| 116 | + for (let i = 0; i < args.length; i++) { |
| 117 | + const argMatch = args[i].trim().match(/^_?(\w+)\??\s*:\s*(.+)$/s); |
| 118 | + if (!argMatch) continue; |
| 119 | + const typeName = argMatch[2].trim(); |
| 120 | + if (/^Record\s*</.test(typeName) || /^\{/.test(typeName)) continue; |
| 121 | + if (/^\w+Params$/.test(typeName)) { |
| 122 | + paramIdx = i; |
| 123 | + paramType = typeName; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if (paramIdx === -1) continue; |
| 129 | + |
| 130 | + let argPattern: DiscoveredMethod['argPattern']; |
| 131 | + if (args.length === 1 && paramIdx === 0) argPattern = 'params-only'; |
| 132 | + else if (args.length === 2 && paramIdx === 1) argPattern = 'id-params'; |
| 133 | + else if (args.length === 2 && paramIdx === 0) argPattern = 'params-wallet'; |
| 134 | + else argPattern = 'id-params-extra'; |
| 135 | + |
| 136 | + results.push({ method: match[1], paramType, argPattern }); |
| 137 | + } |
| 138 | + return results; |
| 139 | +} |
| 140 | + |
| 141 | +function splitArgs(argsBody: string): string[] { |
| 142 | + const result: string[] = []; |
| 143 | + let current = ''; |
| 144 | + let angle = 0; |
| 145 | + let brace = 0; |
| 146 | + for (const ch of argsBody) { |
| 147 | + if (ch === '<') angle++; |
| 148 | + if (ch === '>') angle--; |
| 149 | + if (ch === '{') brace++; |
| 150 | + if (ch === '}') brace--; |
| 151 | + if (ch === ',' && angle === 0 && brace === 0) { |
| 152 | + result.push(current); |
| 153 | + current = ''; |
| 154 | + } else { |
| 155 | + current += ch; |
| 156 | + } |
| 157 | + } |
| 158 | + if (current.trim()) result.push(current); |
| 159 | + return result; |
| 160 | +} |
| 161 | + |
| 162 | +export function discoverTestMatrix(coreDir: string): DiscoveredMethod[] { |
| 163 | + const baseSource = fs.readFileSync(path.join(coreDir, 'BaseExchange.ts'), 'utf-8'); |
| 164 | + const ownFields = parseInterfaces(baseSource); |
| 165 | + const extendsMap = parseExtends(baseSource); |
| 166 | + |
| 167 | + const resolvedFields = new Map<string, ParamField[]>(); |
| 168 | + for (const name of ownFields.keys()) { |
| 169 | + resolvedFields.set(name, resolveFields(name, ownFields, extendsMap)); |
| 170 | + } |
| 171 | + |
| 172 | + const exchangesDir = path.join(coreDir, 'exchanges'); |
| 173 | + const exchangeDirs = fs.readdirSync(exchangesDir, { withFileTypes: true }) |
| 174 | + .filter((e) => e.isDirectory()) |
| 175 | + .map((e) => e.name) |
| 176 | + .sort(); |
| 177 | + |
| 178 | + const discovered: DiscoveredMethod[] = []; |
| 179 | + |
| 180 | + for (const exchangeName of exchangeDirs) { |
| 181 | + const fetcherPath = path.join(exchangesDir, exchangeName, 'fetcher.ts'); |
| 182 | + if (!fs.existsSync(fetcherPath)) continue; |
| 183 | + |
| 184 | + const fetcherSource = fs.readFileSync(fetcherPath, 'utf-8'); |
| 185 | + for (const sig of parseMethodSignatures(fetcherSource)) { |
| 186 | + const fields = resolvedFields.get(sig.paramType); |
| 187 | + if (!fields) continue; |
| 188 | + |
| 189 | + discovered.push({ |
| 190 | + exchange: exchangeName, |
| 191 | + method: sig.method, |
| 192 | + paramType: sig.paramType, |
| 193 | + fields, |
| 194 | + argPattern: sig.argPattern, |
| 195 | + }); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return discovered; |
| 200 | +} |
0 commit comments