Skip to content

Commit f5db819

Browse files
committed
refactor: adjust install & uninstall to use sharedData
1 parent 8ab8b7a commit f5db819

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

source-map-support.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ var sharedData = initializeSharedData({
7777
errorPrepareStackTraceHook: undefined,
7878
/** @type {HookState} */
7979
processEmitHook: undefined,
80+
/** @type {HookState} */
81+
moduleResolveFilenameHook: undefined,
8082

8183
// If true, the caches are reset before a stack trace formatting operation
8284
emptyCacheBetweenOperations: false,
@@ -639,9 +641,14 @@ exports.install = function(options) {
639641
// Redirect subsequent imports of "source-map-support"
640642
// to this package
641643
const {redirectConflictingLibrary = true, onConflictingLibraryRedirect} = options;
642-
if(redirectConflictingLibrary) {
643-
const originalResolveFilename = Module._resolveFilename;
644-
Module._resolveFilename = function resolveFilenameProxy(request, parent, isMain, options) {
644+
if(redirectConflictingLibrary && !sharedData.moduleResolveFilenameHook) {
645+
const originalValue = Module._resolveFilename;
646+
sharedData.moduleResolveFilenameHook = {
647+
enabled: true,
648+
originalValue,
649+
installedValue: undefined
650+
}
651+
Module._resolveFilename = sharedData.moduleResolveFilenameHook.installedValue = function (request, parent, isMain, options) {
645652
// Match all source-map-support entrypoints: source-map-support, source-map-support/register
646653
if (request === 'source-map-support') {
647654
const newRequest = require.resolve('./');
@@ -652,7 +659,7 @@ exports.install = function(options) {
652659
if(onConflictingLibraryRedirect) onConflictingLibraryRedirect(request, parent, isMain, options, newRequest);
653660
request = newRequest;
654661
}
655-
return originalResolveFilename.call(this, request, parent, isMain, options);
662+
return originalValue.call(this, request, parent, isMain, options);
656663
}
657664
}
658665

@@ -759,6 +766,16 @@ exports.uninstall = function() {
759766
}
760767
sharedData.errorPrepareStackTraceHook = undefined;
761768
}
769+
if (sharedData.moduleResolveFilenameHook) {
770+
// Disable behavior
771+
sharedData.moduleResolveFilenameHook.enabled = false;
772+
// If possible, remove our hook function. May not be possible if subsequent third-party hooks have wrapped around us.
773+
var Module = dynamicRequire(module, 'module');
774+
if(Module._resolveFilename === sharedData.moduleResolveFilenameHook.installedValue) {
775+
Module._resolveFilename = sharedData.moduleResolveFilenameHook.originalValue;
776+
}
777+
sharedData.moduleResolveFilenameHook = undefined;
778+
}
762779
}
763780

764781
exports.resetRetrieveHandlers = function() {

test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Note: some tests rely on side-effects from prior tests.
22
// You may not get meaningful results running a subset of tests.
33

4+
const Module = require('module');
45
const priorErrorPrepareStackTrace = Error.prepareStackTrace;
56
const priorProcessEmit = process.emit;
7+
const priorResolveFilename = Module._resolveFilename;
68
const underTest = require('./source-map-support');
79
var SourceMapGenerator = require('source-map').SourceMapGenerator;
810
var child_process = require('child_process');
@@ -742,11 +744,13 @@ describe('uninstall', function() {
742744
underTest.uninstall();
743745
process.emit = priorProcessEmit;
744746
Error.prepareStackTrace = priorErrorPrepareStackTrace;
747+
Module._resolveFilename = priorResolveFilename;
745748
});
746749

747750
it('uninstall removes hooks and source-mapping behavior', function() {
748751
assert.strictEqual(Error.prepareStackTrace, priorErrorPrepareStackTrace);
749752
assert.strictEqual(process.emit, priorProcessEmit);
753+
assert.strictEqual(Module._resolveFilename, priorResolveFilename);
750754
normalThrowWithoutSourceMapSupportInstalled();
751755
});
752756

@@ -806,4 +810,20 @@ describe('uninstall', function() {
806810
process.emit('foo');
807811
assert(peInvocations >= 1);
808812
});
813+
814+
it('uninstall preserves third-party module._resolveFilename hooks installed after us', function() {
815+
installSms();
816+
const wrappedResolveFilename = Module._resolveFilename;
817+
let peInvocations = 0;
818+
function thirdPartyModuleResolveFilename() {
819+
peInvocations++;
820+
return wrappedResolveFilename.apply(this, arguments);
821+
}
822+
Module._resolveFilename = thirdPartyModuleResolveFilename;
823+
underTest.uninstall();
824+
assert.strictEqual(Module._resolveFilename, thirdPartyModuleResolveFilename);
825+
normalThrowWithoutSourceMapSupportInstalled();
826+
Module._resolveFilename('repl');
827+
assert(peInvocations >= 1);
828+
});
809829
});

0 commit comments

Comments
 (0)