Skip to content

Commit

Permalink
evanw#309 Add uninstall function
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalroth committed Aug 3, 2024
1 parent 7b5b81e commit 1f71b3a
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ function dynamicRequire(mod, request) {
var errorFormatterInstalled = false;
var uncaughtShimInstalled = false;

var DEFAULT_EMPTY_CACHE_OPTION = false;
// If true, the caches are reset before a stack trace formatting operation
var emptyCacheBetweenOperations = false;
var emptyCacheBetweenOperations = DEFAULT_EMPTY_CACHE_OPTION;

var DEFAULT_ENV = "auto";
// Supports {browser, node, auto}
var environment = "auto";
var environment = DEFAULT_ENV;

var originalModuleCompile = null;
var originalPrepareStackTrace = null;

// Maps a file path to a string containing the file contents
var fileContentsCache = {};
Expand Down Expand Up @@ -500,9 +505,9 @@ function printErrorAndExit (error) {
globalProcessExit(1);
}

function shimEmitUncaughtException () {
var origEmit = process.emit;
var origEmit = process.emit;

function shimEmitUncaughtException() {
process.emit = function (type) {
if (type === 'uncaughtException') {
var hasStack = (arguments[1] && arguments[1].stack);
Expand All @@ -517,13 +522,26 @@ function shimEmitUncaughtException () {
};
}

function resetRetrieveHandlers() {
retrieveFileHandlers.length = 0;
retrieveMapHandlers.length = 0;

retrieveFileHandlers = originalRetrieveFileHandlers.slice(0);
retrieveMapHandlers = originalRetrieveMapHandlers.slice(0);

retrieveSourceMap = handlerExec(retrieveMapHandlers);
retrieveFile = handlerExec(retrieveFileHandlers);
}


var originalRetrieveFileHandlers = retrieveFileHandlers.slice(0);
var originalRetrieveMapHandlers = retrieveMapHandlers.slice(0);

exports.wrapCallSite = wrapCallSite;
exports.getErrorSource = getErrorSource;
exports.mapSourcePosition = mapSourcePosition;
exports.retrieveSourceMap = retrieveSourceMap;
exports.resetRetrieveHandlers = resetRetrieveHandlers;

exports.install = function(options) {
options = options || {};
Expand Down Expand Up @@ -563,6 +581,7 @@ exports.install = function(options) {

if (!$compile.__sourceMapSupport) {
Module.prototype._compile = function(content, filename) {
originalModuleCompile = $compile;
fileContentsCache[filename] = content;
sourceMapCache[filename] = undefined;
return $compile.call(this, content, filename);
Expand All @@ -575,12 +594,13 @@ exports.install = function(options) {
// Configure options
if (!emptyCacheBetweenOperations) {
emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
options.emptyCacheBetweenOperations : false;
options.emptyCacheBetweenOperations : DEFAULT_EMPTY_CACHE_OPTION;
}

// Install the error reformatter
if (!errorFormatterInstalled) {
errorFormatterInstalled = true;
originalPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = prepareStackTrace;
}

Expand Down Expand Up @@ -613,13 +633,33 @@ exports.install = function(options) {
}
};

exports.resetRetrieveHandlers = function() {
retrieveFileHandlers.length = 0;
retrieveMapHandlers.length = 0;
exports.uninstall = function () {
fileContentsCache = {};
sourceMapCache = {};

retrieveFileHandlers = originalRetrieveFileHandlers.slice(0);
retrieveMapHandlers = originalRetrieveMapHandlers.slice(0);
if (uncaughtShimInstalled) {
process.emit = origEmit;
uncaughtShimInstalled = false;
}

retrieveSourceMap = handlerExec(retrieveMapHandlers);
retrieveFile = handlerExec(retrieveFileHandlers);
if (errorFormatterInstalled) {
Error.prepareStackTrace = originalPrepareStackTrace;
originalPrepareStackTrace = null;
errorFormatterInstalled = false;
}

emptyCacheBetweenOperations = DEFAULT_EMPTY_CACHE_OPTION;

if (!isInBrowser()) {
var Module = dynamicRequire(module, 'module');
var $compile = Module.prototype._compile;
if ($compile.__sourceMapSupport) {
Module.prototype._compile = originalModuleCompile;
}
originalModuleCompile = null;
}

resetRetrieveHandlers();

environment = DEFAULT_ENV;
}

0 comments on commit 1f71b3a

Please sign in to comment.