Skip to content

Commit

Permalink
remove duplicate instances of origRequire
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanpharvey committed May 21, 2024
1 parent f736448 commit 6ab0b8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/dd-trace/src/ritm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const Module = require('module')
const parse = require('module-details-from-path')
const dc = require('dc-polyfill')

const origRequire = Module.prototype.require

// derived from require-in-the-middle@3 with tweaks

module.exports = Hook
Expand Down Expand Up @@ -35,7 +33,9 @@ function Hook (modules, options, onrequire) {
this.modules = modules
this.options = options
this.onrequire = onrequire
const _origRequire = Module.prototype.require
this.origRequire = Module.prototype.require

const self = this

if (Array.isArray(modules)) {
for (const mod of modules) {
Expand All @@ -61,7 +61,7 @@ function Hook (modules, options, onrequire) {
try {
filename = Module._resolveFilename(request, this)
} catch (resolveErr) {
return _origRequire.apply(this, arguments)
return self.origRequire.apply(this, arguments)
}

const core = filename.indexOf(path.sep) === -1
Expand All @@ -81,7 +81,7 @@ function Hook (modules, options, onrequire) {
const patched = patching[filename]
if (patched) {
// If it's already patched, just return it as-is.
return origRequire.apply(this, arguments)
return self.origRequire.apply(this, arguments)
} else {
patching[filename] = true
}
Expand All @@ -94,7 +94,7 @@ function Hook (modules, options, onrequire) {
if (moduleLoadStartChannel.hasSubscribers) {
moduleLoadStartChannel.publish(payload)
}
const exports = origRequire.apply(this, arguments)
const exports = self.origRequire.apply(this, arguments)
payload.module = exports
if (moduleLoadEndChannel.hasSubscribers) {
moduleLoadEndChannel.publish(payload)
Expand Down Expand Up @@ -159,7 +159,7 @@ function Hook (modules, options, onrequire) {
}

Hook.reset = function () {
Module.prototype.require = origRequire
Module.prototype.require = this.origRequire
patchedRequire = null
patching = Object.create(null)
cache = Object.create(null)
Expand Down
2 changes: 2 additions & 0 deletions packages/dd-trace/test/ritm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Hook = require('../src/ritm')
describe('Ritm', () => {
let moduleLoadStartChannel, moduleLoadEndChannel, startListener, endListener
let utilHook, aHook, bHook, httpHook
const origRequire = Module.prototype.require

before(() => {
moduleLoadStartChannel = dc.channel('dd-trace:moduleLoadStart')
Expand Down Expand Up @@ -50,6 +51,7 @@ describe('Ritm', () => {
aHook.unhook()
bHook.unhook()
httpHook.unhook()
Module.prototype.require = origRequire
})

it('should shim util', () => {
Expand Down

0 comments on commit 6ab0b8f

Please sign in to comment.