Skip to content

Commit a0d7470

Browse files
committed
fix: Updated module patch to only wrap _compile as it calls _resolveFileName
1 parent 428c8fe commit a0d7470

File tree

2 files changed

+17
-47
lines changed

2 files changed

+17
-47
lines changed

index.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/index.test.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,7 @@ test('should init ModulePatch', (t) => {
4141
const { modulePatch } = t.ctx
4242
assert.ok(modulePatch instanceof ModulePatch)
4343
assert.ok(modulePatch.instrumentator)
44-
assert.equal(modulePatch.resolve, Module._resolveFilename)
4544
assert.ok(modulePatch.compile, Module.prototype._compile)
46-
assert.ok(modulePatch.transformers instanceof Map)
47-
})
48-
49-
test('should set a transformer for a matched patch', (t) => {
50-
const { modulePath, modulePatch } = t.ctx
51-
modulePatch.patch()
52-
Module._resolveFilename(modulePath, null, false)
53-
assert.ok(modulePatch.transformers.has(modulePath))
54-
modulePatch.unpatch()
55-
assert.equal(modulePatch.transformers.size, 0)
56-
})
57-
58-
test('should not set a transformer for an unmatched patch', (t) => {
59-
const { modulePatch } = t.ctx
60-
modulePatch.patch()
61-
const modulePath = path.join(__dirname, './example-deps/lib/node_modules/pkg-2/index.js')
62-
Module._resolveFilename(modulePath, null, false)
63-
assert.equal(modulePatch.transformers.size, 0)
6445
})
6546

6647
test('should rewrite code for a match transformer', async (t) => {

0 commit comments

Comments
 (0)