diff --git a/source-map-support.js b/source-map-support.js index 2942b5e..8b19f54 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -1,9 +1,9 @@ -var SourceMapConsumer = require('source-map').SourceMapConsumer; -var path = require('path'); +var SourceMapConsumer = require("source-map").SourceMapConsumer; +var path = require("path"); var fs; try { - fs = require('fs'); + fs = require("fs"); if (!fs.existsSync || !fs.readFileSync) { // fs doesn't have all methods we need fs = null; @@ -12,7 +12,7 @@ try { /* nop */ } -var bufferFrom = require('buffer-from'); +var bufferFrom = require("buffer-from"); /** * Requires a module which is protected against bundler minification. @@ -48,39 +48,54 @@ var retrieveFileHandlers = []; var retrieveMapHandlers = []; function isInBrowser() { - if (environment === "browser") - return true; - if (environment === "node") - return false; - return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function') && !(window.require && window.module && window.process && window.process.type === "renderer")); + if (environment === "browser") return true; + if (environment === "node") return false; + return ( + typeof window !== "undefined" && + typeof XMLHttpRequest === "function" && + !( + window.require && + window.module && + window.process && + window.process.type === "renderer" + ) + ); } function hasGlobalProcessEventEmitter() { - return ((typeof process === 'object') && (process !== null) && (typeof process.on === 'function')); + return ( + typeof process === "object" && + process !== null && + typeof process.on === "function" + ); } function globalProcessVersion() { - if ((typeof process === 'object') && (process !== null)) { + if (typeof process === "object" && process !== null) { return process.version; } else { - return ''; + return ""; } } function globalProcessStderr() { - if ((typeof process === 'object') && (process !== null)) { + if (typeof process === "object" && process !== null) { return process.stderr; } } function globalProcessExit(code) { - if ((typeof process === 'object') && (process !== null) && (typeof process.exit === 'function')) { + if ( + typeof process === "object" && + process !== null && + typeof process.exit === "function" + ) { return process.exit(code); } } function handlerExec(list) { - return function(arg) { + return function (arg) { for (var i = 0; i < list.length; i++) { var ret = list[i](arg); if (ret) { @@ -93,40 +108,40 @@ function handlerExec(list) { var retrieveFile = handlerExec(retrieveFileHandlers); -retrieveFileHandlers.push(function(path) { +retrieveFileHandlers.push(function (path) { // Trim the path to make sure there is no extra whitespace. path = path.trim(); if (/^file:/.test(path)) { // existsSync/readFileSync can't handle file protocol, but once stripped, it works - path = path.replace(/file:\/\/\/(\w:)?/, function(protocol, drive) { - return drive ? - '' : // file:///C:/dir/file -> C:/dir/file - '/'; // file:///root-dir/file -> /root-dir/file + path = path.replace(/file:\/\/\/(\w:)?/, function (protocol, drive) { + return drive + ? "" // file:///C:/dir/file -> C:/dir/file + : "/"; // file:///root-dir/file -> /root-dir/file }); } if (path in fileContentsCache) { return fileContentsCache[path]; } - var contents = ''; + var contents = ""; try { if (!fs) { // Use SJAX if we are in the browser var xhr = new XMLHttpRequest(); - xhr.open('GET', path, /** async */ false); + xhr.open("GET", path, /** async */ false); xhr.send(null); if (xhr.readyState === 4 && xhr.status === 200) { contents = xhr.responseText; } } else if (fs.existsSync(path)) { // Otherwise, use the filesystem - contents = fs.readFileSync(path, 'utf8'); + contents = fs.readFileSync(path, "utf8"); } } catch (er) { /* ignore any errors */ } - return fileContentsCache[path] = contents; + return (fileContentsCache[path] = contents); }); // Support URLs relative to a directory, but be careful about a protocol prefix @@ -135,12 +150,15 @@ function supportRelativeURL(file, url) { if (!file) return url; var dir = path.dirname(file); var match = /^\w+:\/\/[^\/]*/.exec(dir); - var protocol = match ? match[0] : ''; + var protocol = match ? match[0] : ""; var startPath = dir.slice(protocol.length); if (protocol && /^\/\w\:/.test(startPath)) { // handle file:///C:/ paths - protocol += '/'; - return protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, '/'); + protocol += "/"; + return ( + protocol + + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, "/") + ); } return protocol + path.resolve(dir.slice(protocol.length), url); } @@ -149,32 +167,33 @@ function retrieveSourceMapURL(source) { var fileData; if (isInBrowser()) { - try { - var xhr = new XMLHttpRequest(); - xhr.open('GET', source, false); - xhr.send(null); - fileData = xhr.readyState === 4 ? xhr.responseText : null; - - // Support providing a sourceMappingURL via the SourceMap header - var sourceMapHeader = xhr.getResponseHeader("SourceMap") || - xhr.getResponseHeader("X-SourceMap"); - if (sourceMapHeader) { - return sourceMapHeader; - } - } catch (e) { - } + try { + var xhr = new XMLHttpRequest(); + xhr.open("GET", source, false); + xhr.send(null); + fileData = xhr.readyState === 4 ? xhr.responseText : null; + + // Support providing a sourceMappingURL via the SourceMap header + var sourceMapHeader = + xhr.getResponseHeader("SourceMap") || + xhr.getResponseHeader("X-SourceMap"); + if (sourceMapHeader) { + return sourceMapHeader; + } + } catch (e) {} } // Get the URL of the source map fileData = retrieveFile(source); - var re = /(?:\/\/[@#][\s]*sourceMappingURL=([^\s'"]+)[\s]*$)|(?:\/\*[@#][\s]*sourceMappingURL=([^\s*'"]+)[\s]*(?:\*\/)[\s]*$)/mg; + var re = + /(?:\/\/[@#][\s]*sourceMappingURL=([^\s'"]+)[\s]*$)|(?:\/\*[@#][\s]*sourceMappingURL=([^\s*'"]+)[\s]*(?:\*\/)[\s]*$)/gm; // Keep executing the search to find the *last* sourceMappingURL to avoid // picking up sourceMappingURLs from comments, strings, etc. var lastMatch, match; - while (match = re.exec(fileData)) lastMatch = match; + while ((match = re.exec(fileData))) lastMatch = match; if (!lastMatch) return null; return lastMatch[1]; -}; +} // Can be overridden by the retrieveSourceMap option to install. Takes a // generated source filename; returns a {map, optional url} object, or null if @@ -182,7 +201,7 @@ function retrieveSourceMapURL(source) { // JSON object (ie, it must be a valid argument to the SourceMapConsumer // constructor). var retrieveSourceMap = handlerExec(retrieveMapHandlers); -retrieveMapHandlers.push(function(source) { +retrieveMapHandlers.push(function (source) { var sourceMappingURL = retrieveSourceMapURL(source); if (!sourceMappingURL) return null; @@ -190,7 +209,7 @@ retrieveMapHandlers.push(function(source) { var sourceMapData; if (reSourceMap.test(sourceMappingURL)) { // Support source map URL as a data url - var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1); + var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(",") + 1); sourceMapData = bufferFrom(rawData, "base64").toString(); sourceMappingURL = source; } else { @@ -205,7 +224,7 @@ retrieveMapHandlers.push(function(source) { return { url: sourceMappingURL, - map: sourceMapData + map: sourceMapData, }; }); @@ -217,13 +236,13 @@ function mapSourcePosition(position) { if (urlAndMap) { sourceMap = sourceMapCache[position.source] = { url: urlAndMap.url, - map: new SourceMapConsumer(urlAndMap.map) + map: new SourceMapConsumer(urlAndMap.map), }; // Load all sources stored inline with the source map into the file cache // to pretend like they are already loaded. They may not exist on disk. if (sourceMap.map.sourcesContent) { - sourceMap.map.sources.forEach(function(source, i) { + sourceMap.map.sources.forEach(function (source, i) { var contents = sourceMap.map.sourcesContent[i]; if (contents) { var url = supportRelativeURL(sourceMap.url, source); @@ -234,13 +253,17 @@ function mapSourcePosition(position) { } else { sourceMap = sourceMapCache[position.source] = { url: null, - map: null + map: null, }; } } // Resolve the source URL relative to the URL of the source map - if (sourceMap && sourceMap.map && typeof sourceMap.map.originalPositionFor === 'function') { + if ( + sourceMap && + sourceMap.map && + typeof sourceMap.map.originalPositionFor === "function" + ) { var originalPosition = sourceMap.map.originalPositionFor(position); // Only return the original position if a matching line was found. If no @@ -250,7 +273,9 @@ function mapSourcePosition(position) { // location in the original file. if (originalPosition.source !== null) { originalPosition.source = supportRelativeURL( - sourceMap.url, originalPosition.source); + sourceMap.url, + originalPosition.source + ); return originalPosition; } } @@ -267,16 +292,25 @@ function mapEvalOrigin(origin) { var position = mapSourcePosition({ source: match[2], line: +match[3], - column: match[4] - 1 + column: match[4] - 1, }); - return 'eval at ' + match[1] + ' (' + position.source + ':' + - position.line + ':' + (position.column + 1) + ')'; + return ( + "eval at " + + match[1] + + " (" + + position.source + + ":" + + position.line + + ":" + + (position.column + 1) + + ")" + ); } // Parse nested eval() calls using recursion match = /^eval at ([^(]+) \((.+)\)$/.exec(origin); if (match) { - return 'eval at ' + match[1] + ' (' + mapEvalOrigin(match[2]) + ')'; + return "eval at " + match[1] + " (" + mapEvalOrigin(match[2]) + ")"; } // Make sure we still return useful information if we didn't find anything @@ -298,7 +332,7 @@ function CallSiteToString() { fileName = this.getScriptNameOrSourceURL(); if (!fileName && this.isEval()) { fileLocation = this.getEvalOrigin(); - fileLocation += ", "; // Expecting source position to follow. + fileLocation += ", "; // Expecting source position to follow. } if (fileName) { @@ -336,7 +370,11 @@ function CallSiteToString() { line += typeName + "."; } line += functionName; - if (methodName && functionName.indexOf("." + methodName) != functionName.length - methodName.length - 1) { + if ( + methodName && + functionName.indexOf("." + methodName) != + functionName.length - methodName.length - 1 + ) { line += " [as " + methodName + "]"; } } else { @@ -358,8 +396,14 @@ function CallSiteToString() { function cloneCallSite(frame) { var object = {}; - Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach(function(name) { - object[name] = /^(?:is|get)/.test(name) ? function() { return frame[name].call(frame); } : frame[name]; + Object.getOwnPropertyNames(Object.getPrototypeOf(frame)).forEach(function ( + name + ) { + object[name] = /^(?:is|get)/.test(name) + ? function () { + return frame[name].call(frame); + } + : frame[name]; }); object.toString = CallSiteToString; return object; @@ -368,9 +412,9 @@ function cloneCallSite(frame) { function wrapCallSite(frame, state) { // provides interface backward compatibility if (state === undefined) { - state = { nextPosition: null, curPosition: null } + state = { nextPosition: null, curPosition: null }; } - if(frame.isNative()) { + if (frame.isNative()) { state.curPosition = null; return frame; } @@ -388,30 +432,44 @@ function wrapCallSite(frame, state) { // Header removed in node at ^10.16 || >=11.11.0 // v11 is not an LTS candidate, we can just test the one version with it. // Test node versions for: 10.16-19, 10.20+, 12-19, 20-99, 100+, or 11.11 - var noHeader = /^v(10\.1[6-9]|10\.[2-9][0-9]|10\.[0-9]{3,}|1[2-9]\d*|[2-9]\d|\d{3,}|11\.11)/; + var noHeader = + /^v(10\.1[6-9]|10\.[2-9][0-9]|10\.[0-9]{3,}|1[2-9]\d*|[2-9]\d|\d{3,}|11\.11)/; var headerLength = noHeader.test(globalProcessVersion()) ? 0 : 62; - if (line === 1 && column > headerLength && !isInBrowser() && !frame.isEval()) { + if ( + line === 1 && + column > headerLength && + !isInBrowser() && + !frame.isEval() + ) { column -= headerLength; } var position = mapSourcePosition({ source: source, line: line, - column: column + column: column, }); state.curPosition = position; frame = cloneCallSite(frame); var originalFunctionName = frame.getFunctionName; - frame.getFunctionName = function() { + frame.getFunctionName = function () { if (state.nextPosition == null) { return originalFunctionName(); } return state.nextPosition.name || originalFunctionName(); }; - frame.getFileName = function() { return position.source; }; - frame.getLineNumber = function() { return position.line; }; - frame.getColumnNumber = function() { return position.column + 1; }; - frame.getScriptNameOrSourceURL = function() { return position.source; }; + frame.getFileName = function () { + return position.source; + }; + frame.getLineNumber = function () { + return position.line; + }; + frame.getColumnNumber = function () { + return position.column + 1; + }; + frame.getScriptNameOrSourceURL = function () { + return position.source; + }; return frame; } @@ -420,7 +478,9 @@ function wrapCallSite(frame, state) { if (origin) { origin = mapEvalOrigin(origin); frame = cloneCallSite(frame); - frame.getEvalOrigin = function() { return origin; }; + frame.getEvalOrigin = function () { + return origin; + }; return frame; } @@ -436,18 +496,18 @@ function prepareStackTrace(error, stack) { sourceMapCache = {}; } - var name = error.name || 'Error'; - var message = error.message || ''; + var name = error.name || "Error"; + var message = error.message || ""; var errorString = name + ": " + message; var state = { nextPosition: null, curPosition: null }; var processedStack = []; for (var i = stack.length - 1; i >= 0; i--) { - processedStack.push('\n at ' + wrapCallSite(stack[i], state)); + processedStack.push("\n at " + wrapCallSite(stack[i], state)); state.nextPosition = state.curPosition; } state.curPosition = state.nextPosition = null; - return errorString + processedStack.reverse().join(''); + return errorString + processedStack.reverse().join(""); } // Generate position and snippet of original source with pointer @@ -464,9 +524,9 @@ function getErrorSource(error) { // Support files on disk if (!contents && fs && fs.existsSync(source)) { try { - contents = fs.readFileSync(source, 'utf8'); + contents = fs.readFileSync(source, "utf8"); } catch (er) { - contents = ''; + contents = ""; } } @@ -474,15 +534,23 @@ function getErrorSource(error) { if (contents) { var code = contents.split(/(?:\r\n|\r|\n)/)[line - 1]; if (code) { - return source + ':' + line + '\n' + code + '\n' + - new Array(column).join(' ') + '^'; + return ( + source + + ":" + + line + + "\n" + + code + + "\n" + + new Array(column).join(" ") + + "^" + ); } } } return null; } -function printErrorAndExit (error) { +function printErrorAndExit(error) { var source = getErrorSource(error); // Ensure error is printed synchronously and not truncated @@ -495,18 +563,26 @@ function printErrorAndExit (error) { console.error(); console.error(source); } - - console.error(error.stack); + const { stack } = error; + for (let i of stack.split("\n")) { + var pos = i.indexOf("file://"); + if (pos + 1) { + pos += 7; + var end = i.lastIndexOf(":", i.lastIndexOf(":") - 1); + i = i.slice(0, pos) + decodeURI(i.slice(pos, end)) + i.slice(end); + } + console.error(i); + } globalProcessExit(1); } -function shimEmitUncaughtException () { +function shimEmitUncaughtException() { var origEmit = process.emit; process.emit = function (type) { - if (type === 'uncaughtException') { - var hasStack = (arguments[1] && arguments[1].stack); - var hasListeners = (this.listeners(type).length > 0); + if (type === "uncaughtException") { + var hasStack = arguments[1] && arguments[1].stack; + var hasListeners = this.listeners(type).length > 0; if (hasStack && !hasListeners) { return printErrorAndExit(arguments[1]); @@ -525,13 +601,17 @@ exports.getErrorSource = getErrorSource; exports.mapSourcePosition = mapSourcePosition; exports.retrieveSourceMap = retrieveSourceMap; -exports.install = function(options) { +exports.install = function (options) { options = options || {}; if (options.environment) { environment = options.environment; if (["node", "browser", "auto"].indexOf(environment) === -1) { - throw new Error("environment " + environment + " was unknown. Available options are {auto, browser, node}") + throw new Error( + "environment " + + environment + + " was unknown. Available options are {auto, browser, node}" + ); } } @@ -558,11 +638,11 @@ exports.install = function(options) { // Support runtime transpilers that include inline source maps if (options.hookRequire && !isInBrowser()) { // Use dynamicRequire to avoid including in browser bundles - var Module = dynamicRequire(module, 'module'); + var Module = dynamicRequire(module, "module"); var $compile = Module.prototype._compile; if (!$compile.__sourceMapSupport) { - Module.prototype._compile = function(content, filename) { + Module.prototype._compile = function (content, filename) { fileContentsCache[filename] = content; sourceMapCache[filename] = undefined; return $compile.call(this, content, filename); @@ -574,8 +654,10 @@ exports.install = function(options) { // Configure options if (!emptyCacheBetweenOperations) { - emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ? - options.emptyCacheBetweenOperations : false; + emptyCacheBetweenOperations = + "emptyCacheBetweenOperations" in options + ? options.emptyCacheBetweenOperations + : false; } // Install the error reformatter @@ -585,19 +667,21 @@ exports.install = function(options) { } if (!uncaughtShimInstalled) { - var installHandler = 'handleUncaughtExceptions' in options ? - options.handleUncaughtExceptions : true; + var installHandler = + "handleUncaughtExceptions" in options + ? options.handleUncaughtExceptions + : true; // Do not override 'uncaughtException' with our own handler in Node.js // Worker threads. Workers pass the error to the main thread as an event, // rather than printing something to stderr and exiting. try { // We need to use `dynamicRequire` because `require` on it's own will be optimized by WebPack/Browserify. - var worker_threads = dynamicRequire(module, 'worker_threads'); + var worker_threads = dynamicRequire(module, "worker_threads"); if (worker_threads.isMainThread === false) { installHandler = false; } - } catch(e) {} + } catch (e) {} // Provide the option to not install the uncaught exception handler. This is // to support other uncaught exception handlers (in test frameworks, for @@ -613,7 +697,7 @@ exports.install = function(options) { } }; -exports.resetRetrieveHandlers = function() { +exports.resetRetrieveHandlers = function () { retrieveFileHandlers.length = 0; retrieveMapHandlers.length = 0; @@ -622,4 +706,4 @@ exports.resetRetrieveHandlers = function() { retrieveSourceMap = handlerExec(retrieveMapHandlers); retrieveFile = handlerExec(retrieveFileHandlers); -} +};