2013-08-22 09:08:13 +08:00
|
|
|
var SourceMapConsumer = require('source-map').SourceMapConsumer;
|
2013-01-18 15:56:20 +08:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
2013-11-11 05:19:41 +08:00
|
|
|
// Only install once if called multiple times
|
2015-08-27 05:25:38 +08:00
|
|
|
var errorFormatterInstalled = false;
|
|
|
|
var uncaughtShimInstalled = false;
|
2013-11-08 11:10:12 +08:00
|
|
|
|
2013-07-23 00:21:10 +08:00
|
|
|
// If true, the caches are reset before a stack trace formatting operation
|
|
|
|
var emptyCacheBetweenOperations = false;
|
|
|
|
|
2015-10-23 00:33:55 +08:00
|
|
|
// Supports {browser, node, auto}
|
|
|
|
var environment = "auto";
|
|
|
|
|
2013-07-23 00:21:10 +08:00
|
|
|
// Maps a file path to a string containing the file contents
|
|
|
|
var fileContentsCache = {};
|
|
|
|
|
|
|
|
// Maps a file path to a source map for that file
|
|
|
|
var sourceMapCache = {};
|
|
|
|
|
2015-06-16 15:10:41 +08:00
|
|
|
// Regex for detecting source maps
|
|
|
|
var reSourceMap = /^data:application\/json[^,]+base64,/;
|
|
|
|
|
2015-08-27 05:25:38 +08:00
|
|
|
// Priority list of retrieve handlers
|
|
|
|
var retrieveFileHandlers = [];
|
|
|
|
var retrieveMapHandlers = [];
|
|
|
|
|
2013-07-22 04:47:26 +08:00
|
|
|
function isInBrowser() {
|
2015-10-23 00:33:55 +08:00
|
|
|
if (environment === "browser")
|
|
|
|
return true;
|
|
|
|
if (environment === "node")
|
|
|
|
return false;
|
2015-03-01 12:31:59 +08:00
|
|
|
return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function'));
|
2013-07-22 04:47:26 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 21:38:59 +08:00
|
|
|
function hasGlobalProcessEventEmitter() {
|
|
|
|
return ((typeof process === 'object') && (process !== null) && (typeof process.on === 'function'));
|
|
|
|
}
|
|
|
|
|
2015-08-27 05:25:38 +08:00
|
|
|
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) {
|
2015-05-29 15:07:39 +08:00
|
|
|
// Trim the path to make sure there is no extra whitespace.
|
|
|
|
path = path.trim();
|
2013-07-23 00:21:10 +08:00
|
|
|
if (path in fileContentsCache) {
|
|
|
|
return fileContentsCache[path];
|
|
|
|
}
|
|
|
|
|
2013-08-03 22:25:01 +08:00
|
|
|
try {
|
|
|
|
// Use SJAX if we are in the browser
|
|
|
|
if (isInBrowser()) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', path, false);
|
|
|
|
xhr.send(null);
|
2015-05-13 16:43:55 +08:00
|
|
|
var contents = null
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
contents = xhr.responseText
|
|
|
|
}
|
2013-08-03 22:25:01 +08:00
|
|
|
}
|
2013-07-22 04:47:26 +08:00
|
|
|
|
2013-08-03 22:25:01 +08:00
|
|
|
// Otherwise, use the filesystem
|
|
|
|
else {
|
2013-07-23 00:21:10 +08:00
|
|
|
var contents = fs.readFileSync(path, 'utf8');
|
|
|
|
}
|
2013-08-03 22:25:01 +08:00
|
|
|
} catch (e) {
|
|
|
|
var contents = null;
|
2013-07-22 04:47:26 +08:00
|
|
|
}
|
2013-07-23 00:21:10 +08:00
|
|
|
|
|
|
|
return fileContentsCache[path] = contents;
|
2015-08-27 05:25:38 +08:00
|
|
|
});
|
2013-07-22 04:47:26 +08:00
|
|
|
|
|
|
|
// Support URLs relative to a directory, but be careful about a protocol prefix
|
|
|
|
// in case we are in the browser (i.e. directories may start with "http://")
|
2014-04-27 03:15:02 +08:00
|
|
|
function supportRelativeURL(file, url) {
|
2014-04-27 03:24:31 +08:00
|
|
|
if (!file) return url;
|
2014-04-27 03:15:02 +08:00
|
|
|
var dir = path.dirname(file);
|
2013-07-22 04:47:26 +08:00
|
|
|
var match = /^\w+:\/\/[^\/]*/.exec(dir);
|
|
|
|
var protocol = match ? match[0] : '';
|
|
|
|
return protocol + path.resolve(dir.slice(protocol.length), url);
|
|
|
|
}
|
|
|
|
|
2014-07-02 05:11:34 +08:00
|
|
|
function retrieveSourceMapURL(source) {
|
|
|
|
var fileData;
|
|
|
|
|
|
|
|
if (isInBrowser()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 23:38:55 +08:00
|
|
|
|
2014-07-02 05:11:34 +08:00
|
|
|
// Get the URL of the source map
|
2014-07-03 23:38:55 +08:00
|
|
|
fileData = retrieveFile(source);
|
2015-07-27 21:34:21 +08:00
|
|
|
// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
|
|
|
|
var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;
|
2015-04-10 02:50:03 +08:00
|
|
|
// 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;
|
|
|
|
if (!lastMatch) return null;
|
|
|
|
return lastMatch[1];
|
2014-07-02 05:11:34 +08:00
|
|
|
};
|
|
|
|
|
2013-07-09 08:17:17 +08:00
|
|
|
// Can be overridden by the retrieveSourceMap option to install. Takes a
|
|
|
|
// generated source filename; returns a {map, optional url} object, or null if
|
|
|
|
// there is no source map. The map field may be either a string or the parsed
|
|
|
|
// JSON object (ie, it must be a valid argument to the SourceMapConsumer
|
|
|
|
// constructor).
|
2015-08-27 05:25:38 +08:00
|
|
|
var retrieveSourceMap = handlerExec(retrieveMapHandlers);
|
|
|
|
retrieveMapHandlers.push(function(source) {
|
2014-07-02 05:11:34 +08:00
|
|
|
var sourceMappingURL = retrieveSourceMapURL(source);
|
|
|
|
if (!sourceMappingURL) return null;
|
2013-07-09 08:17:17 +08:00
|
|
|
|
|
|
|
// Read the contents of the source map
|
|
|
|
var sourceMapData;
|
2015-06-16 15:10:41 +08:00
|
|
|
if (reSourceMap.test(sourceMappingURL)) {
|
2013-07-09 08:17:17 +08:00
|
|
|
// Support source map URL as a data url
|
2015-06-16 15:10:41 +08:00
|
|
|
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
|
|
|
|
sourceMapData = new Buffer(rawData, "base64").toString();
|
2013-12-12 04:30:20 +08:00
|
|
|
sourceMappingURL = null;
|
2013-07-22 04:47:26 +08:00
|
|
|
} else {
|
2013-07-09 08:17:17 +08:00
|
|
|
// Support source map URLs relative to the source URL
|
2014-04-27 03:15:02 +08:00
|
|
|
sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
|
2015-05-19 02:07:55 +08:00
|
|
|
sourceMapData = retrieveFile(sourceMappingURL);
|
2013-07-09 08:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sourceMapData) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-27 12:58:41 +08:00
|
|
|
|
2013-07-09 08:17:17 +08:00
|
|
|
return {
|
|
|
|
url: sourceMappingURL,
|
|
|
|
map: sourceMapData
|
|
|
|
};
|
2015-08-27 05:25:38 +08:00
|
|
|
});
|
2013-07-09 08:17:17 +08:00
|
|
|
|
2014-04-27 02:43:54 +08:00
|
|
|
function mapSourcePosition(position) {
|
2013-07-23 00:21:10 +08:00
|
|
|
var sourceMap = sourceMapCache[position.source];
|
2013-07-09 08:17:17 +08:00
|
|
|
if (!sourceMap) {
|
|
|
|
// Call the (overrideable) retrieveSourceMap function to get the source map.
|
|
|
|
var urlAndMap = retrieveSourceMap(position.source);
|
|
|
|
if (urlAndMap) {
|
2013-07-23 00:21:10 +08:00
|
|
|
sourceMap = sourceMapCache[position.source] = {
|
2013-07-09 08:17:17 +08:00
|
|
|
url: urlAndMap.url,
|
|
|
|
map: new SourceMapConsumer(urlAndMap.map)
|
2013-04-25 10:16:25 +08:00
|
|
|
};
|
2014-04-27 03:15:02 +08:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
var contents = sourceMap.map.sourcesContent[i];
|
|
|
|
if (contents) {
|
|
|
|
var url = supportRelativeURL(sourceMap.url, source);
|
|
|
|
fileContentsCache[url] = contents;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-02-24 17:37:16 +08:00
|
|
|
} else {
|
|
|
|
sourceMap = sourceMapCache[position.source] = {
|
|
|
|
url: null,
|
|
|
|
map: null
|
|
|
|
};
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 10:16:25 +08:00
|
|
|
|
|
|
|
// Resolve the source URL relative to the URL of the source map
|
2015-02-24 17:37:16 +08:00
|
|
|
if (sourceMap && sourceMap.map) {
|
2013-04-25 10:16:25 +08:00
|
|
|
var originalPosition = sourceMap.map.originalPositionFor(position);
|
2013-06-27 14:20:34 +08:00
|
|
|
|
|
|
|
// Only return the original position if a matching line was found. If no
|
|
|
|
// matching line is found then we return position instead, which will cause
|
|
|
|
// the stack trace to print the path and line for the compiled file. It is
|
|
|
|
// better to give a precise location in the compiled file than a vague
|
|
|
|
// location in the original file.
|
|
|
|
if (originalPosition.source !== null) {
|
2014-04-27 03:24:31 +08:00
|
|
|
originalPosition.source = supportRelativeURL(
|
|
|
|
sourceMap.url, originalPosition.source);
|
2013-06-27 14:20:34 +08:00
|
|
|
return originalPosition;
|
|
|
|
}
|
2013-03-19 06:43:02 +08:00
|
|
|
}
|
2013-06-27 14:20:34 +08:00
|
|
|
|
|
|
|
return position;
|
2014-04-27 02:43:54 +08:00
|
|
|
}
|
2013-01-18 15:56:20 +08:00
|
|
|
|
|
|
|
// Parses code generated by FormatEvalOrigin(), a function inside V8:
|
|
|
|
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js
|
2013-07-23 00:21:10 +08:00
|
|
|
function mapEvalOrigin(origin) {
|
2013-01-18 15:56:20 +08:00
|
|
|
// Most eval() calls are in this format
|
|
|
|
var match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
|
|
|
|
if (match) {
|
2013-07-23 00:21:10 +08:00
|
|
|
var position = mapSourcePosition({
|
2013-01-18 15:56:20 +08:00
|
|
|
source: match[2],
|
2016-01-23 23:16:58 +08:00
|
|
|
line: +match[3],
|
2014-04-27 02:43:54 +08:00
|
|
|
column: match[4] - 1
|
2013-01-18 15:56:20 +08:00
|
|
|
});
|
|
|
|
return 'eval at ' + match[1] + ' (' + position.source + ':' +
|
2014-04-27 02:43:54 +08:00
|
|
|
position.line + ':' + (position.column + 1) + ')';
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse nested eval() calls using recursion
|
|
|
|
match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
|
|
|
|
if (match) {
|
2013-07-23 00:21:10 +08:00
|
|
|
return 'eval at ' + match[1] + ' (' + mapEvalOrigin(match[2]) + ')';
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we still return useful information if we didn't find anything
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:09:11 +08:00
|
|
|
// This is copied almost verbatim from the V8 source code at
|
|
|
|
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js. The
|
|
|
|
// implementation of wrapCallSite() used to just forward to the actual source
|
|
|
|
// code of CallSite.prototype.toString but unfortunately a new release of V8
|
|
|
|
// did something to the prototype chain and broke the shim. The only fix I
|
|
|
|
// could find was copy/paste.
|
|
|
|
function CallSiteToString() {
|
|
|
|
var fileName;
|
|
|
|
var fileLocation = "";
|
|
|
|
if (this.isNative()) {
|
|
|
|
fileLocation = "native";
|
|
|
|
} else {
|
|
|
|
fileName = this.getScriptNameOrSourceURL();
|
|
|
|
if (!fileName && this.isEval()) {
|
|
|
|
fileLocation = this.getEvalOrigin();
|
|
|
|
fileLocation += ", "; // Expecting source position to follow.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileName) {
|
|
|
|
fileLocation += fileName;
|
|
|
|
} else {
|
|
|
|
// Source code does not originate from a file and is not native, but we
|
|
|
|
// can still get the source position inside the source string, e.g. in
|
|
|
|
// an eval string.
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-02 21:43:39 +08:00
|
|
|
function wrapCallSite(frame) {
|
2013-01-18 15:56:20 +08:00
|
|
|
// Most call sites will return the source file from getFileName(), but code
|
2013-06-10 03:53:20 +08:00
|
|
|
// passed to eval() ending in "//# sourceURL=..." will return the source file
|
2013-01-18 15:56:20 +08:00
|
|
|
// from getScriptNameOrSourceURL() instead
|
|
|
|
var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
|
|
|
|
if (source) {
|
2015-05-26 16:38:10 +08:00
|
|
|
var line = frame.getLineNumber();
|
|
|
|
var column = frame.getColumnNumber() - 1;
|
|
|
|
|
|
|
|
// Fix position in Node where some (internal) code is prepended.
|
|
|
|
// See https://github.com/evanw/node-source-map-support/issues/36
|
2015-06-02 21:43:39 +08:00
|
|
|
if (line === 1 && !isInBrowser() && !frame.isEval()) {
|
|
|
|
column -= 62;
|
2015-05-26 16:38:10 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 00:21:10 +08:00
|
|
|
var position = mapSourcePosition({
|
2013-01-18 15:56:20 +08:00
|
|
|
source: source,
|
2015-05-26 16:38:10 +08:00
|
|
|
line: line,
|
|
|
|
column: column
|
2013-01-18 15:56:20 +08:00
|
|
|
});
|
2014-10-22 16:09:11 +08:00
|
|
|
frame = cloneCallSite(frame);
|
|
|
|
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;
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Code called using eval() needs special handling
|
2013-04-29 05:30:55 +08:00
|
|
|
var origin = frame.isEval() && frame.getEvalOrigin();
|
2013-01-18 15:56:20 +08:00
|
|
|
if (origin) {
|
2013-07-23 00:21:10 +08:00
|
|
|
origin = mapEvalOrigin(origin);
|
2014-10-22 16:09:11 +08:00
|
|
|
frame = cloneCallSite(frame);
|
|
|
|
frame.getEvalOrigin = function() { return origin; };
|
|
|
|
return frame;
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here then we were unable to change the source position
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is part of the V8 stack trace API, for more info see:
|
|
|
|
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
|
2013-03-06 03:36:36 +08:00
|
|
|
function prepareStackTrace(error, stack) {
|
2013-07-23 00:21:10 +08:00
|
|
|
if (emptyCacheBetweenOperations) {
|
|
|
|
fileContentsCache = {};
|
|
|
|
sourceMapCache = {};
|
|
|
|
}
|
2015-05-26 16:38:10 +08:00
|
|
|
|
2013-01-18 15:56:20 +08:00
|
|
|
return error + stack.map(function(frame) {
|
2015-06-02 21:43:39 +08:00
|
|
|
return '\n at ' + wrapCallSite(frame);
|
2013-01-18 15:56:20 +08:00
|
|
|
}).join('');
|
2013-03-06 03:36:36 +08:00
|
|
|
}
|
2013-01-18 15:56:20 +08:00
|
|
|
|
2014-04-27 02:43:54 +08:00
|
|
|
// Generate position and snippet of original source with pointer
|
|
|
|
function getErrorSource(error) {
|
2013-01-18 15:56:20 +08:00
|
|
|
var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack);
|
|
|
|
if (match) {
|
2014-04-27 02:15:38 +08:00
|
|
|
var source = match[1];
|
|
|
|
var line = +match[2];
|
|
|
|
var column = +match[3];
|
2014-04-27 03:15:02 +08:00
|
|
|
|
|
|
|
// Support the inline sourceContents inside the source map
|
|
|
|
var contents = fileContentsCache[source];
|
|
|
|
|
|
|
|
// Support files on disk
|
|
|
|
if (!contents && fs.existsSync(source)) {
|
|
|
|
contents = fs.readFileSync(source, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the line from the original source code like node does
|
|
|
|
if (contents) {
|
2014-04-27 02:15:38 +08:00
|
|
|
var code = contents.split(/(?:\r\n|\r|\n)/)[line - 1];
|
|
|
|
if (code) {
|
2015-02-23 03:50:12 +08:00
|
|
|
return source + ':' + line + '\n' + code + '\n' +
|
2014-04-27 02:43:54 +08:00
|
|
|
new Array(column).join(' ') + '^';
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-27 02:43:54 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-06-16 16:00:49 +08:00
|
|
|
function printErrorAndExit (error) {
|
|
|
|
var source = getErrorSource(error);
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
console.error();
|
|
|
|
console.error(source);
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
2015-06-16 16:00:49 +08:00
|
|
|
|
|
|
|
console.error(error.stack);
|
2013-12-23 06:28:04 +08:00
|
|
|
process.exit(1);
|
2013-03-06 03:36:36 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 16:00:49 +08:00
|
|
|
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);
|
2015-08-27 05:25:38 +08:00
|
|
|
};
|
2015-06-16 16:00:49 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 08:25:01 +08:00
|
|
|
exports.wrapCallSite = wrapCallSite;
|
2014-04-27 02:43:54 +08:00
|
|
|
exports.getErrorSource = getErrorSource;
|
|
|
|
exports.mapSourcePosition = mapSourcePosition;
|
|
|
|
exports.retrieveSourceMap = retrieveSourceMap;
|
2014-02-04 08:25:01 +08:00
|
|
|
|
2013-03-06 03:36:36 +08:00
|
|
|
exports.install = function(options) {
|
2015-08-27 05:25:38 +08:00
|
|
|
options = options || {};
|
2013-11-08 11:10:12 +08:00
|
|
|
|
2015-08-27 05:25:38 +08:00
|
|
|
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}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow sources to be found by methods other than reading the files
|
|
|
|
// directly from disk.
|
|
|
|
if (options.retrieveFile) {
|
|
|
|
if (options.overrideRetrieveFile) {
|
|
|
|
retrieveFileHandlers.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
retrieveFileHandlers.unshift(options.retrieveFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow source maps to be found by methods other than reading the files
|
|
|
|
// directly from disk.
|
|
|
|
if (options.retrieveSourceMap) {
|
|
|
|
if (options.overrideRetrieveSourceMap) {
|
|
|
|
retrieveMapHandlers.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
retrieveMapHandlers.unshift(options.retrieveSourceMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure options
|
|
|
|
if (!emptyCacheBetweenOperations) {
|
2013-11-08 11:10:12 +08:00
|
|
|
emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
|
|
|
|
options.emptyCacheBetweenOperations : false;
|
2015-08-27 05:25:38 +08:00
|
|
|
}
|
2013-11-08 11:10:12 +08:00
|
|
|
|
2015-08-27 05:25:38 +08:00
|
|
|
// Install the error reformatter
|
|
|
|
if (!errorFormatterInstalled) {
|
|
|
|
errorFormatterInstalled = true;
|
|
|
|
Error.prepareStackTrace = prepareStackTrace;
|
|
|
|
}
|
2013-12-04 04:49:34 +08:00
|
|
|
|
2015-08-27 05:25:38 +08:00
|
|
|
if (!uncaughtShimInstalled) {
|
|
|
|
var installHandler = 'handleUncaughtExceptions' in options ?
|
|
|
|
options.handleUncaughtExceptions : true;
|
2013-11-08 11:10:12 +08:00
|
|
|
|
|
|
|
// Provide the option to not install the uncaught exception handler. This is
|
|
|
|
// to support other uncaught exception handlers (in test frameworks, for
|
|
|
|
// example). If this handler is not installed and there are no other uncaught
|
|
|
|
// exception handlers, uncaught exceptions will be caught by node's built-in
|
|
|
|
// exception handler and the process will still be terminated. However, the
|
|
|
|
// generated JavaScript code will be shown above the stack trace instead of
|
|
|
|
// the original source code.
|
2015-06-16 21:38:59 +08:00
|
|
|
if (installHandler && hasGlobalProcessEventEmitter()) {
|
2015-08-27 05:25:38 +08:00
|
|
|
uncaughtShimInstalled = true;
|
2015-06-16 16:00:49 +08:00
|
|
|
shimEmitUncaughtException();
|
2013-11-08 11:10:12 +08:00
|
|
|
}
|
2013-03-06 03:36:36 +08:00
|
|
|
}
|
|
|
|
};
|