|
| 1 | +import { Package } from "../Package"; |
| 2 | +import { BuiltinModule } from "../BuiltinModule"; |
| 3 | +import { |
| 4 | + BuiltinInstrumentationInstructionJSON, |
| 5 | + IntereptorFunctionsObj, |
| 6 | + PackageFileInstrumentationInstructionJSON, |
| 7 | +} from "./types"; |
| 8 | +import { satisfiesVersion } from "../../../helpers/satisfiesVersion"; |
| 9 | + |
| 10 | +// Keys are package / builtin names |
| 11 | +let packages = new Map<string, PackageFileInstrumentationInstructionJSON[]>(); |
| 12 | +let builtins = new Map<string, BuiltinInstrumentationInstructionJSON>(); |
| 13 | + |
| 14 | +// Stores the callbacks for the instrumented functions of builtin modules |
| 15 | +// Identifier for builtin is: moduleName.functionName |
| 16 | +let builtinCallbacks = new Map<string, IntereptorFunctionsObj>(); |
| 17 | +// Stores the callbacks for the instrumented functions of package files |
| 18 | +// Identifier for package file is: packageName.relativePath.functionName.matchingVersion |
| 19 | +let packageFileCallbacks = new Map<string, IntereptorFunctionsObj>(); |
| 20 | + |
| 21 | +export function setPackagesToInstrument(_packages: Package[]) { |
| 22 | + // Clear the previous packages |
| 23 | + packages = new Map(); |
| 24 | + packageFileCallbacks = new Map(); |
| 25 | + |
| 26 | + for (const pkg of _packages) { |
| 27 | + const packageInstructions = pkg |
| 28 | + .getVersions() |
| 29 | + .map((versionedPackage) => { |
| 30 | + return versionedPackage |
| 31 | + .getFileInstrumentationInstructions() |
| 32 | + .map((file) => { |
| 33 | + return { |
| 34 | + path: file.path, |
| 35 | + versionRange: versionedPackage.getRange(), |
| 36 | + functions: file.functions.map((func) => { |
| 37 | + const identifier = `${pkg.getName()}.${file.path}.${func.name}.${versionedPackage.getRange()}`; |
| 38 | + |
| 39 | + return { |
| 40 | + nodeType: func.nodeType, |
| 41 | + name: func.name, |
| 42 | + identifier, |
| 43 | + inspectArgs: !!func.inspectArgs, |
| 44 | + modifyArgs: !!func.modifyArgs, |
| 45 | + modifyReturnValue: !!func.modifyReturnValue, |
| 46 | + }; |
| 47 | + }), |
| 48 | + }; |
| 49 | + }) |
| 50 | + .flat(); |
| 51 | + }) |
| 52 | + .flat(); |
| 53 | + |
| 54 | + packages.set(pkg.getName(), packageInstructions); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export function setBuiltinsToInstrument(builtinModules: BuiltinModule[]) { |
| 59 | + // Clear the previous builtins |
| 60 | + builtins = new Map(); |
| 61 | + builtinCallbacks = new Map(); |
| 62 | + |
| 63 | + for (const builtin of builtinModules) { |
| 64 | + const instructions = builtin.getInstrumentationInstruction(); |
| 65 | + |
| 66 | + if ( |
| 67 | + !instructions || |
| 68 | + !instructions.functions || |
| 69 | + instructions.functions.length === 0 |
| 70 | + ) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + // Check if function is included twice |
| 75 | + const functionNames = new Set<string>(); |
| 76 | + for (const f of instructions.functions) { |
| 77 | + if (functionNames.has(f.name)) { |
| 78 | + throw new Error( |
| 79 | + `Function ${f.name} is included twice in the instrumentation instructions for ${builtin.getName()}` |
| 80 | + ); |
| 81 | + } |
| 82 | + functionNames.add(f.name); |
| 83 | + } |
| 84 | + |
| 85 | + const functions = instructions.functions.map((f) => { |
| 86 | + builtinCallbacks.set(`${builtin.getName()}.${f.name}`, { |
| 87 | + inspectArgs: f.inspectArgs, |
| 88 | + modifyArgs: f.modifyArgs, |
| 89 | + modifyReturnValue: f.modifyReturnValue, |
| 90 | + }); |
| 91 | + |
| 92 | + return { |
| 93 | + name: f.name, |
| 94 | + inspectArgs: !!f.inspectArgs, |
| 95 | + modifyArgs: !!f.modifyArgs, |
| 96 | + modifyReturnValue: !!f.modifyReturnValue, |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + builtins.set(builtin.getName(), { |
| 101 | + functions, |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +export function getBuiltinInstrumentationInstructions( |
| 107 | + name: string |
| 108 | +): BuiltinInstrumentationInstructionJSON | undefined { |
| 109 | + return builtins.get(name); |
| 110 | +} |
| 111 | + |
| 112 | +export function shouldPatchPackage(name: string): boolean { |
| 113 | + return packages.has(name); |
| 114 | +} |
| 115 | + |
| 116 | +export function getPackageFileInstrumentationInstructions( |
| 117 | + packageName: string, |
| 118 | + version: string |
| 119 | +): PackageFileInstrumentationInstructionJSON | undefined { |
| 120 | + const instructions = packages.get(packageName); |
| 121 | + if (!instructions) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + return instructions.find((f) => satisfiesVersion(f.versionRange, version)); |
| 126 | +} |
0 commit comments