11import { Package } from "../Package" ;
22import { BuiltinModule } from "../BuiltinModule" ;
33import {
4- BuiltinInstrumentationInstructionJSON ,
54 IntereptorFunctionsObj ,
65 PackageFileInstrumentationInstructionJSON ,
76} from "./types" ;
87import { satisfiesVersion } from "../../../helpers/satisfiesVersion" ;
8+ import { RequireInterceptor } from "../RequireInterceptor" ;
99
1010// Keys are package / builtin names
1111let packages = new Map < string , PackageFileInstrumentationInstructionJSON [ ] > ( ) ;
12- let builtins = new Map < string , BuiltinInstrumentationInstructionJSON > ( ) ;
1312
1413// Stores the callbacks for the instrumented functions of builtin modules
1514// Identifier for builtin is: moduleName.functionName
16- let builtinCallbacks = new Map < string , IntereptorFunctionsObj > ( ) ;
15+ let builtinRequireInterceptors = new Map < string , RequireInterceptor [ ] > ( ) ;
1716// Stores the callbacks for the instrumented functions of package files
1817// Identifier for package file is: packageName.relativePath.functionName.matchingVersion
1918let packageFileCallbacks = new Map < string , IntereptorFunctionsObj > ( ) ;
@@ -65,58 +64,17 @@ export function setPackagesToInstrument(_packages: Package[]) {
6564
6665export function setBuiltinsToInstrument ( builtinModules : BuiltinModule [ ] ) {
6766 // Clear the previous builtins
68- builtins = new Map ( ) ;
69- builtinCallbacks = new Map ( ) ;
67+ builtinRequireInterceptors = new Map ( ) ;
7068
7169 for ( const builtin of builtinModules ) {
72- const instructions = builtin . getInstrumentationInstruction ( ) ;
73-
74- if (
75- ! instructions ||
76- ! instructions . functions ||
77- instructions . functions . length === 0
78- ) {
79- continue ;
80- }
70+ const interceptors = builtin . getRequireInterceptors ( ) ;
8171
82- // Check if function is included twice
83- const functionNames = new Set < string > ( ) ;
84- for ( const f of instructions . functions ) {
85- if ( functionNames . has ( f . name ) ) {
86- throw new Error (
87- `Function ${ f . name } is included twice in the instrumentation instructions for ${ builtin . getName ( ) } `
88- ) ;
89- }
90- functionNames . add ( f . name ) ;
72+ if ( interceptors . length > 0 ) {
73+ builtinRequireInterceptors . set ( builtin . getName ( ) , interceptors ) ;
9174 }
92-
93- const functions = instructions . functions . map ( ( f ) => {
94- builtinCallbacks . set ( `${ builtin . getName ( ) } .${ f . name } ` , {
95- inspectArgs : f . inspectArgs ,
96- modifyArgs : f . modifyArgs ,
97- modifyReturnValue : f . modifyReturnValue ,
98- } ) ;
99-
100- return {
101- name : f . name ,
102- inspectArgs : ! ! f . inspectArgs ,
103- modifyArgs : ! ! f . modifyArgs ,
104- modifyReturnValue : ! ! f . modifyReturnValue ,
105- } ;
106- } ) ;
107-
108- builtins . set ( builtin . getName ( ) , {
109- functions,
110- } ) ;
11175 }
11276}
11377
114- export function getBuiltinInstrumentationInstructions (
115- name : string
116- ) : BuiltinInstrumentationInstructionJSON | undefined {
117- return builtins . get ( name ) ;
118- }
119-
12078export function shouldPatchPackage ( name : string ) : boolean {
12179 return packages . has ( name ) ;
12280}
@@ -139,8 +97,10 @@ export function getPackageCallbacks(
13997 return packageFileCallbacks . get ( identifier ) || { } ;
14098}
14199
142- export function getBuiltinCallbacks (
143- identifier : string
144- ) : IntereptorFunctionsObj {
145- return builtinCallbacks . get ( identifier ) || { } ;
100+ export function getBuiltinInterceptors ( name : string ) : RequireInterceptor [ ] {
101+ return builtinRequireInterceptors . get ( name ) || [ ] ;
102+ }
103+
104+ export function shouldPatchBuiltin ( name : string ) : boolean {
105+ return builtinRequireInterceptors . has ( name ) ;
146106}
0 commit comments