123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- var SourceMapConsumer = require('source-map').SourceMapConsumer;
- var path = require('path');
- var fs;
- try {
- fs = require('fs');
- if (!fs.existsSync || !fs.readFileSync) {
-
- fs = null;
- }
- } catch (err) {
-
- }
- var bufferFrom = require('buffer-from');
- function dynamicRequire(mod, request) {
- return mod.require(request);
- }
- var errorFormatterInstalled = false;
- var uncaughtShimInstalled = false;
- var emptyCacheBetweenOperations = false;
- var environment = "auto";
- var fileContentsCache = {};
- var sourceMapCache = {};
- var reSourceMap = /^data:application\/json[^,]+base64,/;
- 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"));
- }
- function hasGlobalProcessEventEmitter() {
- return ((typeof process === 'object') && (process !== null) && (typeof process.on === 'function'));
- }
- function globalProcessVersion() {
- if ((typeof process === 'object') && (process !== null)) {
- return process.version;
- } else {
- return '';
- }
- }
- function globalProcessStderr() {
- if ((typeof process === 'object') && (process !== null)) {
- return process.stderr;
- }
- }
- function globalProcessExit(code) {
- if ((typeof process === 'object') && (process !== null) && (typeof process.exit === 'function')) {
- return process.exit(code);
- }
- }
- function handlerExec(list) {
- return function(arg) {
- for (var i = 0; i < list.length; i++) {
- var ret = list[i](arg);
- if (ret) {
- return ret;
- }
- }
- return null;
- };
- }
- var retrieveFile = handlerExec(retrieveFileHandlers);
- retrieveFileHandlers.push(function(path) {
-
- path = path.trim();
- if (/^file:/.test(path)) {
-
- path = path.replace(/file:\/\/\/(\w:)?/, function(protocol, drive) {
- return drive ?
- '' :
- '/';
- });
- }
- if (path in fileContentsCache) {
- return fileContentsCache[path];
- }
- var contents = '';
- try {
- if (!fs) {
-
- var xhr = new XMLHttpRequest();
- xhr.open('GET', path, false);
- xhr.send(null);
- if (xhr.readyState === 4 && xhr.status === 200) {
- contents = xhr.responseText;
- }
- } else if (fs.existsSync(path)) {
-
- contents = fs.readFileSync(path, 'utf8');
- }
- } catch (er) {
-
- }
- return fileContentsCache[path] = contents;
- });
- function supportRelativeURL(file, url) {
- if (!file) return url;
- var dir = path.dirname(file);
- var match = /^\w+:\/\/[^\/]*/.exec(dir);
- var protocol = match ? match[0] : '';
- var startPath = dir.slice(protocol.length);
- if (protocol && /^\/\w\:/.test(startPath)) {
-
- protocol += '/';
- return protocol + path.resolve(dir.slice(protocol.length), url).replace(/\\/g, '/');
- }
- return protocol + path.resolve(dir.slice(protocol.length), url);
- }
- 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;
-
- var sourceMapHeader = xhr.getResponseHeader("SourceMap") ||
- xhr.getResponseHeader("X-SourceMap");
- if (sourceMapHeader) {
- return sourceMapHeader;
- }
- } catch (e) {
- }
- }
-
- fileData = retrieveFile(source);
- var re = /(?:\/\/[@#][\s]*sourceMappingURL=([^\s'"]+)[\s]*$)|(?:\/\*[@#][\s]*sourceMappingURL=([^\s*'"]+)[\s]*(?:\*\/)[\s]*$)/mg;
-
-
- var lastMatch, match;
- while (match = re.exec(fileData)) lastMatch = match;
- if (!lastMatch) return null;
- return lastMatch[1];
- };
- var retrieveSourceMap = handlerExec(retrieveMapHandlers);
- retrieveMapHandlers.push(function(source) {
- var sourceMappingURL = retrieveSourceMapURL(source);
- if (!sourceMappingURL) return null;
-
- var sourceMapData;
- if (reSourceMap.test(sourceMappingURL)) {
-
- var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
- sourceMapData = bufferFrom(rawData, "base64").toString();
- sourceMappingURL = source;
- } else {
-
- sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
- sourceMapData = retrieveFile(sourceMappingURL);
- }
- if (!sourceMapData) {
- return null;
- }
- return {
- url: sourceMappingURL,
- map: sourceMapData
- };
- });
- function mapSourcePosition(position) {
- var sourceMap = sourceMapCache[position.source];
- if (!sourceMap) {
-
- var urlAndMap = retrieveSourceMap(position.source);
- if (urlAndMap) {
- sourceMap = sourceMapCache[position.source] = {
- url: urlAndMap.url,
- map: new SourceMapConsumer(urlAndMap.map)
- };
-
-
- if (sourceMap.map.sourcesContent) {
- sourceMap.map.sources.forEach(function(source, i) {
- var contents = sourceMap.map.sourcesContent[i];
- if (contents) {
- var url = supportRelativeURL(sourceMap.url, source);
- fileContentsCache[url] = contents;
- }
- });
- }
- } else {
- sourceMap = sourceMapCache[position.source] = {
- url: null,
- map: null
- };
- }
- }
-
- if (sourceMap && sourceMap.map && typeof sourceMap.map.originalPositionFor === 'function') {
- var originalPosition = sourceMap.map.originalPositionFor(position);
-
-
-
-
-
- if (originalPosition.source !== null) {
- originalPosition.source = supportRelativeURL(
- sourceMap.url, originalPosition.source);
- return originalPosition;
- }
- }
- return position;
- }
- function mapEvalOrigin(origin) {
-
- var match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
- if (match) {
- var position = mapSourcePosition({
- source: match[2],
- line: +match[3],
- column: match[4] - 1
- });
- return 'eval at ' + match[1] + ' (' + position.source + ':' +
- position.line + ':' + (position.column + 1) + ')';
- }
-
- match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
- if (match) {
- return 'eval at ' + match[1] + ' (' + mapEvalOrigin(match[2]) + ')';
- }
-
- return origin;
- }
- function CallSiteToString() {
- var fileName;
- var fileLocation = "";
- if (this.isNative()) {
- fileLocation = "native";
- } else {
- fileName = this.getScriptNameOrSourceURL();
- if (!fileName && this.isEval()) {
- fileLocation = this.getEvalOrigin();
- fileLocation += ", ";
- }
- if (fileName) {
- fileLocation += fileName;
- } else {
-
-
-
- fileLocation += "<anonymous>";
- }
- var lineNumber = this.getLineNumber();
- if (lineNumber != null) {
- fileLocation += ":" + lineNumber;
- var columnNumber = this.getColumnNumber();
- if (columnNumber) {
- fileLocation += ":" + columnNumber;
- }
- }
- }
- var line = "";
- var functionName = this.getFunctionName();
- var addSuffix = true;
- var isConstructor = this.isConstructor();
- var isMethodCall = !(this.isToplevel() || isConstructor);
- if (isMethodCall) {
- var typeName = this.getTypeName();
-
- if (typeName === "[object Object]") {
- typeName = "null";
- }
- var methodName = this.getMethodName();
- if (functionName) {
- if (typeName && functionName.indexOf(typeName) != 0) {
- line += typeName + ".";
- }
- line += functionName;
- if (methodName && functionName.indexOf("." + methodName) != functionName.length - methodName.length - 1) {
- line += " [as " + methodName + "]";
- }
- } else {
- line += typeName + "." + (methodName || "<anonymous>");
- }
- } else if (isConstructor) {
- line += "new " + (functionName || "<anonymous>");
- } else if (functionName) {
- line += functionName;
- } else {
- line += fileLocation;
- addSuffix = false;
- }
- if (addSuffix) {
- line += " (" + fileLocation + ")";
- }
- return line;
- }
- 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.toString = CallSiteToString;
- return object;
- }
- function wrapCallSite(frame, state) {
-
- if (state === undefined) {
- state = { nextPosition: null, curPosition: null }
- }
- if(frame.isNative()) {
- state.curPosition = null;
- return frame;
- }
-
-
-
- var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
- if (source) {
- var line = frame.getLineNumber();
- var column = frame.getColumnNumber() - 1;
-
-
-
-
-
- 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()) {
- column -= headerLength;
- }
- var position = mapSourcePosition({
- source: source,
- line: line,
- column: column
- });
- state.curPosition = position;
- frame = cloneCallSite(frame);
- var originalFunctionName = frame.getFunctionName;
- 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; };
- return frame;
- }
-
- var origin = frame.isEval() && frame.getEvalOrigin();
- if (origin) {
- origin = mapEvalOrigin(origin);
- frame = cloneCallSite(frame);
- frame.getEvalOrigin = function() { return origin; };
- return frame;
- }
-
- return frame;
- }
- function prepareStackTrace(error, stack) {
- if (emptyCacheBetweenOperations) {
- fileContentsCache = {};
- sourceMapCache = {};
- }
- 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));
- state.nextPosition = state.curPosition;
- }
- state.curPosition = state.nextPosition = null;
- return errorString + processedStack.reverse().join('');
- }
- function getErrorSource(error) {
- var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack);
- if (match) {
- var source = match[1];
- var line = +match[2];
- var column = +match[3];
-
- var contents = fileContentsCache[source];
-
- if (!contents && fs && fs.existsSync(source)) {
- try {
- contents = fs.readFileSync(source, 'utf8');
- } catch (er) {
- contents = '';
- }
- }
-
- 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 null;
- }
- function printErrorAndExit (error) {
- var source = getErrorSource(error);
-
- var stderr = globalProcessStderr();
- if (stderr && stderr._handle && stderr._handle.setBlocking) {
- stderr._handle.setBlocking(true);
- }
- if (source) {
- console.error();
- console.error(source);
- }
- console.error(error.stack);
- globalProcessExit(1);
- }
- 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 (hasStack && !hasListeners) {
- return printErrorAndExit(arguments[1]);
- }
- }
- return origEmit.apply(this, arguments);
- };
- }
- var originalRetrieveFileHandlers = retrieveFileHandlers.slice(0);
- var originalRetrieveMapHandlers = retrieveMapHandlers.slice(0);
- exports.wrapCallSite = wrapCallSite;
- exports.getErrorSource = getErrorSource;
- exports.mapSourcePosition = mapSourcePosition;
- exports.retrieveSourceMap = retrieveSourceMap;
- 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}")
- }
- }
-
-
- if (options.retrieveFile) {
- if (options.overrideRetrieveFile) {
- retrieveFileHandlers.length = 0;
- }
- retrieveFileHandlers.unshift(options.retrieveFile);
- }
-
-
- if (options.retrieveSourceMap) {
- if (options.overrideRetrieveSourceMap) {
- retrieveMapHandlers.length = 0;
- }
- retrieveMapHandlers.unshift(options.retrieveSourceMap);
- }
-
- if (options.hookRequire && !isInBrowser()) {
-
- var Module = dynamicRequire(module, 'module');
- var $compile = Module.prototype._compile;
- if (!$compile.__sourceMapSupport) {
- Module.prototype._compile = function(content, filename) {
- fileContentsCache[filename] = content;
- sourceMapCache[filename] = undefined;
- return $compile.call(this, content, filename);
- };
- Module.prototype._compile.__sourceMapSupport = true;
- }
- }
-
- if (!emptyCacheBetweenOperations) {
- emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
- options.emptyCacheBetweenOperations : false;
- }
-
- if (!errorFormatterInstalled) {
- errorFormatterInstalled = true;
- Error.prepareStackTrace = prepareStackTrace;
- }
- if (!uncaughtShimInstalled) {
- var installHandler = 'handleUncaughtExceptions' in options ?
- options.handleUncaughtExceptions : true;
-
-
-
- try {
-
- var worker_threads = dynamicRequire(module, 'worker_threads');
- if (worker_threads.isMainThread === false) {
- installHandler = false;
- }
- } catch(e) {}
-
-
-
-
-
-
-
- if (installHandler && hasGlobalProcessEventEmitter()) {
- uncaughtShimInstalled = true;
- shimEmitUncaughtException();
- }
- }
- };
- exports.resetRetrieveHandlers = function() {
- retrieveFileHandlers.length = 0;
- retrieveMapHandlers.length = 0;
- retrieveFileHandlers = originalRetrieveFileHandlers.slice(0);
- retrieveMapHandlers = originalRetrieveMapHandlers.slice(0);
- retrieveSourceMap = handlerExec(retrieveMapHandlers);
- retrieveFile = handlerExec(retrieveFileHandlers);
- }
|