|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Deterministic diff between index.ts and the canonical cross-plugin schema |
| 4 | + * (schemas/plugins-rpc-schema/appsflyer-plugins-rpc-schema.json, mirrored from |
| 5 | + * gitlab.appsflyer.com/mobile/appsflyer-plugins-rpc-schema). |
| 6 | + * |
| 7 | + * Parses index.ts with the TypeScript compiler API (already a devDependency) instead of |
| 8 | + * regex, so it finds every callRpc/callRpcVoid/callRpcWithCallback/dispatchRpc/onceRegistrar |
| 9 | + * call site and the literal param keys passed. Matches index.ts's dispatched (canonical) method |
| 10 | + * name against the schema's per-platform *wire* method name (schema.methods[].rpc.<platform>.method), |
| 11 | + * through the same native aliasing tables __tests__/rpc-wire-contract.test.js uses - matching |
| 12 | + * against schema.methods[].name directly is wrong, since that's the schema's public-API label, |
| 13 | + * not necessarily what's on the wire (e.g. index.ts dispatches "isDebug"; the schema's public |
| 14 | + * name for that same wire call is "enableDebug"). |
| 15 | + * |
| 16 | + * Reports: |
| 17 | + * 1. schema methods index.ts never dispatches (on either platform) |
| 18 | + * 2. index.ts dispatches with no matching schema entry on either platform |
| 19 | + * 3. for methods present in both: params index.ts sends that no matched schema entry |
| 20 | + * declares, and platform-required params no call site ever sends |
| 21 | + * |
| 22 | + * This is a report, not a hard gate — some mismatches are expected (native lifecycle hooks |
| 23 | + * like continueUserActivity/handleLaunchOptions aren't invoked from JS). Read the output and |
| 24 | + * judge each line; promote confirmed-real ones into __tests__/rpc-wire-contract.test.js if you |
| 25 | + * want them CI-enforced. |
| 26 | + * |
| 27 | + * Usage: node scripts/verify-plugin-schema.js |
| 28 | + */ |
| 29 | +const fs = require("fs"); |
| 30 | +const path = require("path"); |
| 31 | +const ts = require("typescript"); |
| 32 | + |
| 33 | +const repoRoot = path.resolve(__dirname, ".."); |
| 34 | +const schemaPath = path.join( |
| 35 | + repoRoot, |
| 36 | + "schemas", |
| 37 | + "plugins-rpc-schema", |
| 38 | + "appsflyer-plugins-rpc-schema.json" |
| 39 | +); |
| 40 | +const indexPath = path.join(repoRoot, "index.ts"); |
| 41 | + |
| 42 | +const DISPATCH_CALLEES = new Set([ |
| 43 | + "callRpc", |
| 44 | + "callRpcVoid", |
| 45 | + "callRpcWithCallback", |
| 46 | + "dispatchRpc", |
| 47 | +]); |
| 48 | + |
| 49 | +function callSitesFromIndexTs() { |
| 50 | + const source = fs.readFileSync(indexPath, "utf8"); |
| 51 | + const sf = ts.createSourceFile( |
| 52 | + indexPath, |
| 53 | + source, |
| 54 | + ts.ScriptTarget.Latest, |
| 55 | + true, |
| 56 | + ts.ScriptKind.TS |
| 57 | + ); |
| 58 | + |
| 59 | + const sites = []; // { method, keys: string[] | null } - null means "can't statically verify" |
| 60 | + |
| 61 | + // null = params arg isn't a plain object literal we can read keys from (an Identifier like |
| 62 | + // `callRpcVoid("setConsentData", consentData)`, or a literal containing `...spread`). |
| 63 | + function objectLiteralKeys(node) { |
| 64 | + if (!node) return []; |
| 65 | + if (!ts.isObjectLiteralExpression(node)) return null; |
| 66 | + if (node.properties.some((p) => ts.isSpreadAssignment(p))) return null; |
| 67 | + return node.properties |
| 68 | + .map((p) => p.name && ts.isIdentifier(p.name) ? p.name.text : null) |
| 69 | + .filter(Boolean); |
| 70 | + } |
| 71 | + |
| 72 | + function visit(node) { |
| 73 | + if (ts.isCallExpression(node)) { |
| 74 | + const callee = node.expression; |
| 75 | + const name = ts.isIdentifier(callee) ? callee.text : null; |
| 76 | + |
| 77 | + if (name && DISPATCH_CALLEES.has(name)) { |
| 78 | + const [methodArg, paramsArg] = node.arguments; |
| 79 | + if (methodArg && ts.isStringLiteral(methodArg)) { |
| 80 | + sites.push({ method: methodArg.text, keys: objectLiteralKeys(paramsArg) }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // onceRegistrar("registerDeeplinkListener") etc. - listener registration RPCs, no params. |
| 85 | + if (name === "onceRegistrar") { |
| 86 | + const [methodArg] = node.arguments; |
| 87 | + if (methodArg && ts.isStringLiteral(methodArg)) { |
| 88 | + sites.push({ method: methodArg.text, keys: [] }); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + ts.forEachChild(node, visit); |
| 94 | + } |
| 95 | + visit(sf); |
| 96 | + |
| 97 | + // Catch inline `"method": "..."` object literals (setUserFbLoginId-style) that skip callRpc |
| 98 | + // entirely to dodge Number()'s precision loss - params are template-literal text, not readable keys. |
| 99 | + for (const match of source.matchAll(/"method"\s*:\s*"([^"]+)"/g)) { |
| 100 | + if (!sites.some((s) => s.method === match[1])) { |
| 101 | + sites.push({ method: match[1], keys: null }); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return sites; |
| 106 | +} |
| 107 | + |
| 108 | +function schemaParamProps(entry, platform) { |
| 109 | + const params = entry.rpc?.[platform]?.parameters; |
| 110 | + return { |
| 111 | + properties: new Set(Object.keys(params?.properties || {})), |
| 112 | + required: new Set(params?.required || []), |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +// Native rewrites the dispatched method string only (never params) before reading it - mirrors |
| 117 | +// RNAppsFlyerImpl.swift `canonicalToIOSMethod` / RNAppsFlyerModule.kt `CANONICAL_TO_ANDROID_METHOD`, |
| 118 | +// copied from __tests__/rpc-wire-contract.test.js. The schema's rpc.<platform>.method is the |
| 119 | +// post-alias wire name, so matching against schema.name directly (as v1 of this script did) |
| 120 | +// produces false positives for every aliased method - go through these tables first. |
| 121 | +const IOS_METHOD_ALIASES = { |
| 122 | + init: "initialize", |
| 123 | + sendPushNotificationData: "handlePushNotification", |
| 124 | + updateServerUninstallToken: "registerUninstall", |
| 125 | +}; |
| 126 | +const ANDROID_METHOD_ALIASES = { |
| 127 | + registerDeeplinkListener: "subscribeForDeepLink", |
| 128 | +}; |
| 129 | + |
| 130 | +function main() { |
| 131 | + const schema = JSON.parse(fs.readFileSync(schemaPath, "utf8")); |
| 132 | + |
| 133 | + // wire method name -> schema entry, per platform. A dispatched name can match a different |
| 134 | + // schema entry per platform (e.g. sendPushNotificationData's ios wire name "handlePushNotification" |
| 135 | + // is its own separate schema entry, with different params than the android-only "sendPushNotificationData" entry). |
| 136 | + const wireIndex = { ios: new Map(), android: new Map() }; |
| 137 | + for (const entry of schema.methods) { |
| 138 | + for (const platform of ["ios", "android"]) { |
| 139 | + const wireMethod = entry.rpc?.[platform]?.method; |
| 140 | + if (wireMethod) wireIndex[platform].set(wireMethod, entry); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + const sites = callSitesFromIndexTs(); |
| 145 | + const dispatchedNames = new Set(sites.map((s) => s.method)); |
| 146 | + const keysByMethod = new Map(); // dispatched name -> Set<string> | "dynamic" |
| 147 | + for (const { method, keys } of sites) { |
| 148 | + if (keysByMethod.get(method) === "dynamic") continue; |
| 149 | + if (keys === null) { |
| 150 | + keysByMethod.set(method, "dynamic"); |
| 151 | + continue; |
| 152 | + } |
| 153 | + const existing = keysByMethod.get(method); |
| 154 | + const set = existing instanceof Set ? existing : new Set(); |
| 155 | + keys.forEach((k) => set.add(k)); |
| 156 | + keysByMethod.set(method, set); |
| 157 | + } |
| 158 | + |
| 159 | + const matchedSchemaNames = new Set(); |
| 160 | + const missingFromSchema = []; |
| 161 | + const paramProblems = []; |
| 162 | + const unverifiable = []; |
| 163 | + |
| 164 | + for (const dispatched of dispatchedNames) { |
| 165 | + const matchByPlatform = { |
| 166 | + ios: wireIndex.ios.get(IOS_METHOD_ALIASES[dispatched] || dispatched), |
| 167 | + android: wireIndex.android.get(ANDROID_METHOD_ALIASES[dispatched] || dispatched), |
| 168 | + }; |
| 169 | + const matchedEntries = [...new Set([matchByPlatform.ios, matchByPlatform.android].filter(Boolean))]; |
| 170 | + |
| 171 | + if (matchedEntries.length === 0) { |
| 172 | + missingFromSchema.push(dispatched); |
| 173 | + continue; |
| 174 | + } |
| 175 | + matchedEntries.forEach((e) => matchedSchemaNames.add(e.name)); |
| 176 | + |
| 177 | + const sentKeys = keysByMethod.get(dispatched); |
| 178 | + if (sentKeys === "dynamic") { |
| 179 | + unverifiable.push(dispatched); |
| 180 | + continue; // params built from a spread/variable - can't check keys statically |
| 181 | + } |
| 182 | + |
| 183 | + const knownAnywhere = new Set(); |
| 184 | + for (const platform of ["ios", "android"]) { |
| 185 | + const entry = matchByPlatform[platform]; |
| 186 | + if (!entry) continue; |
| 187 | + const { properties, required } = schemaParamProps(entry, platform); |
| 188 | + properties.forEach((p) => knownAnywhere.add(p)); |
| 189 | + for (const req of required) { |
| 190 | + if (!sentKeys.has(req)) { |
| 191 | + paramProblems.push( |
| 192 | + `"${dispatched}" (schema "${entry.name}"): ${platform} requires "${req}", but no call site sends it` |
| 193 | + ); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + for (const key of sentKeys) { |
| 198 | + if (!knownAnywhere.has(key)) { |
| 199 | + paramProblems.push(`"${dispatched}": sends param "${key}" that no matched schema entry declares`); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + const missingFromIndex = schema.methods |
| 205 | + .map((m) => m.name) |
| 206 | + .filter((n) => !matchedSchemaNames.has(n)) |
| 207 | + .sort(); |
| 208 | + missingFromSchema.sort(); |
| 209 | + |
| 210 | + console.log(`schema methods: ${schema.methods.length}`); |
| 211 | + console.log(`index.ts dispatch sites: ${dispatchedNames.size}\n`); |
| 212 | + |
| 213 | + console.log(`=== in schema, never dispatched by index.ts (${missingFromIndex.length}) ===`); |
| 214 | + missingFromIndex.forEach((n) => console.log(` ${n}`)); |
| 215 | + |
| 216 | + console.log(`\n=== dispatched by index.ts, not in schema (${missingFromSchema.length}) ===`); |
| 217 | + missingFromSchema.forEach((n) => console.log(` ${n}`)); |
| 218 | + |
| 219 | + console.log(`\n=== param mismatches on methods present in both (${paramProblems.length}) ===`); |
| 220 | + paramProblems.forEach((p) => console.log(` ${p}`)); |
| 221 | + |
| 222 | + console.log(`\n=== params built dynamically (spread/variable) - not checked (${unverifiable.length}) ===`); |
| 223 | + unverifiable.sort().forEach((m) => console.log(` ${m}`)); |
| 224 | + |
| 225 | + const total = missingFromIndex.length + missingFromSchema.length + paramProblems.length; |
| 226 | + console.log(`\n${total} total finding(s).`); |
| 227 | + process.exit(total ? 1 : 0); |
| 228 | +} |
| 229 | + |
| 230 | +main(); |
0 commit comments