@@ -9,45 +9,36 @@ class ModulePatch {
99 constructor ( { instrumentations = [ ] } = { } ) {
1010 this . packages = new Set ( instrumentations . map ( i => i . module . name ) )
1111 this . instrumentator = create ( instrumentations )
12- this . transformers = new Map ( )
13- this . resolve = Module . _resolveFilename
1412 this . compile = Module . prototype . _compile
1513 }
1614
1715 /**
18- * Patches the Node.js module class methods that are responsible for resolving filePaths and compiling code.
16+ * Patches the Node.js module class method that is responsible for compiling code.
1917 * If a module is found that has an instrumentator, it will transform the code before compiling it
2018 * with tracing channel methods.
2119 */
2220 patch ( ) {
2321 const self = this
24- Module . _resolveFilename = function wrappedResolveFileName ( ) {
25- const resolvedName = self . resolve . apply ( this , arguments )
26- const resolvedModule = parse ( resolvedName )
22+ Module . prototype . _compile = function wrappedCompile ( ... args ) {
23+ const [ content , filename ] = args
24+ const resolvedModule = parse ( filename )
2725 if ( resolvedModule && self . packages . has ( resolvedModule . name ) ) {
26+ debug ( 'found resolved module, checking if there is a transformer %s' , filename )
2827 const version = getPackageVersion ( resolvedModule . basedir , resolvedModule . name )
2928 const transformer = self . instrumentator . getTransformer ( resolvedModule . name , version , resolvedModule . path )
3029 if ( transformer ) {
31- self . transformers . set ( resolvedName , transformer )
32- }
33- }
34- return resolvedName
35- }
36-
37- Module . prototype . _compile = function wrappedCompile ( ...args ) {
38- const [ content , filename ] = args
39- if ( self . transformers . has ( filename ) ) {
40- const transformer = self . transformers . get ( filename )
41- try {
42- const transformedCode = transformer . transform ( content , 'unknown' )
43- args [ 0 ] = transformedCode ?. code
44- if ( process . env . TRACING_DUMP ) {
45- dump ( args [ 0 ] , filename )
30+ debug ( 'transforming file %s' , filename )
31+ try {
32+ const transformedCode = transformer . transform ( content , 'unknown' )
33+ args [ 0 ] = transformedCode ?. code
34+ if ( process . env . TRACING_DUMP ) {
35+ dump ( args [ 0 ] , filename )
36+ }
37+ } catch ( error ) {
38+ debug ( 'Error transforming module %s: %o' , filename , error )
39+ } finally {
40+ transformer . free ( )
4641 }
47- } catch ( error ) {
48- debug ( 'Error transforming module %s: %o' , filename , error )
49- } finally {
50- transformer . free ( )
5142 }
5243 }
5344
@@ -56,12 +47,10 @@ class ModulePatch {
5647 }
5748
5849 /**
59- * Clears all the transformers and restores the original Module methods that were wrapped.
50+ * Restores the original Module.prototype._compile method
6051 * **Note**: This is intended to be used in testing only.
6152 */
6253 unpatch ( ) {
63- this . transformers . clear ( )
64- Module . _resolveFilename = this . resolve
6554 Module . prototype . _compile = this . compile
6655 }
6756}
0 commit comments