2013-01-18 15:56:20 +08:00
|
|
|
var SourceMapConsumer = require('source-map').SourceMapConsumer;
|
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
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).
|
|
|
|
var retrieveSourceMap = function (source) {
|
|
|
|
if (!fs.existsSync(source))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Get the URL of the source map
|
|
|
|
var fileData = fs.readFileSync(source, 'utf8');
|
|
|
|
var match = /\/\/[#@]\s*sourceMappingURL=(.*)\s*$/m.exec(fileData);
|
|
|
|
if (!match) return null;
|
|
|
|
var sourceMappingURL = match[1];
|
|
|
|
|
|
|
|
// Read the contents of the source map
|
|
|
|
var sourceMapData;
|
|
|
|
var dataUrlPrefix = "data:application/json;base64,";
|
|
|
|
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) {
|
|
|
|
// Support source map URL as a data url
|
|
|
|
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Support source map URLs relative to the source URL
|
|
|
|
var dir = path.dirname(source);
|
|
|
|
sourceMappingURL = path.resolve(dir, sourceMappingURL);
|
2013-06-27 12:58:41 +08:00
|
|
|
|
2013-07-09 08:17:17 +08:00
|
|
|
if (fs.existsSync(sourceMappingURL)) {
|
|
|
|
sourceMapData = fs.readFileSync(sourceMappingURL, 'utf8');
|
2013-06-27 12:58:41 +08:00
|
|
|
}
|
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
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var mapSourcePosition = exports.mapSourcePosition = function(cache, position) {
|
|
|
|
var sourceMap = cache[position.source];
|
|
|
|
if (!sourceMap) {
|
|
|
|
// Call the (overrideable) retrieveSourceMap function to get the source map.
|
|
|
|
var urlAndMap = retrieveSourceMap(position.source);
|
|
|
|
if (urlAndMap) {
|
|
|
|
sourceMap = cache[position.source] = {
|
|
|
|
url: urlAndMap.url,
|
|
|
|
map: new SourceMapConsumer(urlAndMap.map)
|
2013-04-25 10:16:25 +08:00
|
|
|
};
|
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
|
2013-03-19 06:43:02 +08:00
|
|
|
if (sourceMap) {
|
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) {
|
2013-07-09 08:17:17 +08:00
|
|
|
if (sourceMap.url) {
|
|
|
|
originalPosition.source = path.resolve(path.dirname(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;
|
2013-07-09 08:17:17 +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
|
|
|
|
function mapEvalOrigin(cache, origin) {
|
|
|
|
// Most eval() calls are in this format
|
|
|
|
var match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
|
|
|
|
if (match) {
|
|
|
|
var position = mapSourcePosition(cache, {
|
|
|
|
source: match[2],
|
|
|
|
line: match[3],
|
|
|
|
column: match[4]
|
|
|
|
});
|
|
|
|
return 'eval at ' + match[1] + ' (' + position.source + ':' +
|
|
|
|
position.line + ':' + position.column + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse nested eval() calls using recursion
|
|
|
|
match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
|
|
|
|
if (match) {
|
|
|
|
return 'eval at ' + match[1] + ' (' + mapEvalOrigin(cache, match[2]) + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we still return useful information if we didn't find anything
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapCallSite(cache, frame) {
|
|
|
|
// 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) {
|
|
|
|
var position = mapSourcePosition(cache, {
|
|
|
|
source: source,
|
|
|
|
line: frame.getLineNumber(),
|
|
|
|
column: frame.getColumnNumber()
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
__proto__: frame,
|
|
|
|
getFileName: function() { return position.source; },
|
|
|
|
getLineNumber: function() { return position.line; },
|
|
|
|
getColumnNumber: function() { return position.column; },
|
|
|
|
getScriptNameOrSourceURL: function() { return position.source; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
origin = mapEvalOrigin(cache, origin);
|
|
|
|
return {
|
|
|
|
__proto__: frame,
|
|
|
|
getEvalOrigin: function() { return origin; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01-18 15:56:20 +08:00
|
|
|
// Store source maps in a cache so we don't load them more than once when
|
|
|
|
// formatting a single stack trace (don't cache them forever though in case
|
|
|
|
// the files change on disk and the user wants to see the updated mapping)
|
|
|
|
var cache = {};
|
|
|
|
return error + stack.map(function(frame) {
|
|
|
|
return '\n at ' + wrapCallSite(cache, frame);
|
|
|
|
}).join('');
|
2013-03-06 03:36:36 +08:00
|
|
|
}
|
2013-01-18 15:56:20 +08:00
|
|
|
|
|
|
|
// Mimic node's stack trace printing when an exception escapes the process
|
2013-03-06 03:36:36 +08:00
|
|
|
function handleUncaughtExceptions(error) {
|
2013-01-18 16:17:51 +08:00
|
|
|
if (!error || !error.stack) {
|
|
|
|
console.log('Uncaught exception:', error);
|
|
|
|
process.exit();
|
|
|
|
}
|
2013-01-18 15:56:20 +08:00
|
|
|
var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack);
|
|
|
|
if (match) {
|
|
|
|
var cache = {};
|
|
|
|
var position = mapSourcePosition(cache, {
|
|
|
|
source: match[1],
|
|
|
|
line: match[2],
|
|
|
|
column: match[3]
|
|
|
|
});
|
|
|
|
if (fs.existsSync(position.source)) {
|
|
|
|
var contents = fs.readFileSync(position.source, 'utf8');
|
|
|
|
var line = contents.split(/(?:\r\n|\r|\n)/)[position.line - 1];
|
|
|
|
if (line) {
|
|
|
|
console.log('\n' + position.source + ':' + position.line);
|
|
|
|
console.log(line);
|
|
|
|
console.log(new Array(+position.column).join(' ') + '^');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(error.stack);
|
|
|
|
process.exit();
|
2013-03-06 03:36:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.install = function(options) {
|
|
|
|
Error.prepareStackTrace = prepareStackTrace;
|
|
|
|
|
|
|
|
// Configure options
|
|
|
|
options = options || {};
|
|
|
|
var installHandler = 'handleUncaughtExceptions' in options ?
|
|
|
|
options.handleUncaughtExceptions : true;
|
|
|
|
|
2013-07-09 08:17:17 +08:00
|
|
|
// Allow source maps to be found by methods other than reading the files
|
|
|
|
// directly from disk.
|
|
|
|
if (options.retrieveSourceMap)
|
|
|
|
retrieveSourceMap = options.retrieveSourceMap;
|
|
|
|
|
2013-03-06 03:36:36 +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.
|
|
|
|
if (installHandler) {
|
|
|
|
process.on('uncaughtException', handleUncaughtExceptions);
|
|
|
|
}
|
|
|
|
};
|