node-source-map-support/source-map-support.js

286 lines
9.3 KiB
JavaScript
Raw Normal View History

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
var alreadyInstalled = false;
// If true, the caches are reset before a stack trace formatting operation
var emptyCacheBetweenOperations = false;
// 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 = {};
2013-07-22 04:47:26 +08:00
function isInBrowser() {
return typeof window !== 'undefined';
}
function retrieveFile(path) {
if (path in fileContentsCache) {
return fileContentsCache[path];
}
try {
// Use SJAX if we are in the browser
if (isInBrowser()) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, false);
xhr.send(null);
var contents = xhr.readyState === 4 ? xhr.responseText : null;
}
2013-07-22 04:47:26 +08:00
// Otherwise, use the filesystem
else {
var contents = fs.readFileSync(path, 'utf8');
}
} catch (e) {
var contents = null;
2013-07-22 04:47:26 +08:00
}
return fileContentsCache[path] = contents;
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://")
function supportRelativeURL(file, url) {
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);
}
// 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).
2014-04-27 02:43:54 +08:00
function retrieveSourceMap(source) {
// Get the URL of the source map
2013-07-22 04:47:26 +08:00
var fileData = retrieveFile(source);
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();
sourceMappingURL = null;
2013-07-22 04:47:26 +08:00
} else {
// Support source map URLs relative to the source URL
sourceMappingURL = supportRelativeURL(source, sourceMappingURL);
2013-07-22 04:47:26 +08:00
sourceMapData = retrieveFile(sourceMappingURL, 'utf8');
}
if (!sourceMapData) {
return null;
}
2013-06-27 12:58:41 +08:00
return {
url: sourceMappingURL,
map: sourceMapData
};
2014-04-27 02:43:54 +08:00
}
2014-04-27 02:43:54 +08:00
function mapSourcePosition(position) {
var sourceMap = sourceMapCache[position.source];
if (!sourceMap) {
// Call the (overrideable) retrieveSourceMap function to get the source map.
var urlAndMap = retrieveSourceMap(position.source);
if (urlAndMap) {
sourceMap = sourceMapCache[position.source] = {
url: urlAndMap.url,
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) {
var contents = sourceMap.map.sourcesContent[i];
if (contents) {
var url = supportRelativeURL(sourceMap.url, source);
fileContentsCache[url] = contents;
}
});
}
2013-01-18 15:56:20 +08:00
}
}
// Resolve the source URL relative to the URL of the source map
if (sourceMap) {
var originalPosition = sourceMap.map.originalPositionFor(position);
// 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) {
if (sourceMap.url) {
2013-07-22 04:47:26 +08:00
originalPosition.source = supportRelativeURL(
sourceMap.url, originalPosition.source);
}
return originalPosition;
}
}
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
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) {
var position = mapSourcePosition({
2013-01-18 15:56:20 +08:00
source: match[2],
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) {
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;
}
function wrapCallSite(frame) {
2013-01-18 15:56:20 +08:00
// Most call sites will return the source file from getFileName(), but code
// 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({
2013-01-18 15:56:20 +08:00
source: source,
line: frame.getLineNumber(),
2014-04-27 02:15:38 +08:00
column: frame.getColumnNumber() - 1
2013-01-18 15:56:20 +08:00
});
return {
__proto__: frame,
getFileName: function() { return position.source; },
getLineNumber: function() { return position.line; },
2014-04-27 02:15:38 +08:00
getColumnNumber: function() { return position.column + 1; },
2013-01-18 15:56:20 +08:00
getScriptNameOrSourceURL: function() { return position.source; }
};
}
// Code called using eval() needs special handling
var origin = frame.isEval() && frame.getEvalOrigin();
2013-01-18 15:56:20 +08:00
if (origin) {
origin = mapEvalOrigin(origin);
2013-01-18 15:56:20 +08:00
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
function prepareStackTrace(error, stack) {
if (emptyCacheBetweenOperations) {
fileContentsCache = {};
sourceMapCache = {};
}
2013-01-18 15:56:20 +08:00
return error + stack.map(function(frame) {
return '\n at ' + wrapCallSite(frame);
2013-01-18 15:56:20 +08:00
}).join('');
}
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];
// 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) {
2014-04-27 02:43:54 +08:00
return '\n' + source + ':' + line + '\n' + code + '\n' +
new Array(column).join(' ') + '^';
2013-01-18 15:56:20 +08:00
}
}
}
2014-04-27 02:43:54 +08:00
return null;
}
2013-01-18 15:56:20 +08:00
// Mimic node's stack trace printing when an exception escapes the process
function handleUncaughtExceptions(error) {
if (!error || !error.stack) {
console.log('Uncaught exception:', error);
2014-04-27 02:43:54 +08:00
} else {
var source = getErrorSource(error);
if (source !== null) console.log(source);
console.log(error.stack);
2013-01-18 15:56:20 +08:00
}
process.exit(1);
}
exports.wrapCallSite = wrapCallSite;
2014-04-27 02:43:54 +08:00
exports.getErrorSource = getErrorSource;
exports.mapSourcePosition = mapSourcePosition;
exports.retrieveSourceMap = retrieveSourceMap;
exports.install = function(options) {
if (!alreadyInstalled) {
alreadyInstalled = true;
Error.prepareStackTrace = prepareStackTrace;
// Configure options
options = options || {};
var installHandler = 'handleUncaughtExceptions' in options ?
options.handleUncaughtExceptions : true;
emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
options.emptyCacheBetweenOperations : false;
2013-12-04 04:49:34 +08:00
// Allow sources to be found by methods other than reading the files
// directly from disk.
if (options.retrieveFile)
retrieveFile = options.retrieveFile;
// Allow source maps to be found by methods other than reading the files
// directly from disk.
if (options.retrieveSourceMap)
retrieveSourceMap = options.retrieveSourceMap;
// 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 && !isInBrowser()) {
process.on('uncaughtException', handleUncaughtExceptions);
}
}
};