cache source map data forever by default

This commit is contained in:
Evan Wallace 2013-07-22 09:21:10 -07:00
parent e773027aef
commit 2d3b8107bf
3 changed files with 43 additions and 23 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "source-map-support", "name": "source-map-support",
"description": "Fixes stack traces for files with source maps", "description": "Fixes stack traces for files with source maps",
"version": "0.2.0", "version": "0.2.1",
"main": "./source-map-support.js", "main": "./source-map-support.js",
"scripts": { "scripts": {
"test": "node_modules/mocha/bin/mocha" "test": "node_modules/mocha/bin/mocha"

View File

@ -2,25 +2,42 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer;
var path = require('path'); var path = require('path');
var fs = require('fs'); var fs = require('fs');
// 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 = {};
function isInBrowser() { function isInBrowser() {
return typeof window !== 'undefined'; return typeof window !== 'undefined';
} }
function retrieveFile(path) { function retrieveFile(path) {
if (path in fileContentsCache) {
return fileContentsCache[path];
}
// Use SJAX if we are in the browser // Use SJAX if we are in the browser
if (isInBrowser()) { if (isInBrowser()) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', path, false); xhr.open('GET', path, false);
xhr.send(null); xhr.send(null);
return xhr.readyState === 4 ? xhr.responseText : null; var contents = xhr.readyState === 4 ? xhr.responseText : null;
} }
// Otherwise, use the filesystem // Otherwise, use the filesystem
try { else {
return fs.readFileSync(path, 'utf8'); try {
} catch (e) { var contents = fs.readFileSync(path, 'utf8');
return null; } catch (e) {
var contents = null;
}
} }
return fileContentsCache[path] = contents;
} }
// Support URLs relative to a directory, but be careful about a protocol prefix // Support URLs relative to a directory, but be careful about a protocol prefix
@ -66,13 +83,13 @@ var retrieveSourceMap = function (source) {
}; };
}; };
var mapSourcePosition = exports.mapSourcePosition = function(cache, position) { var mapSourcePosition = exports.mapSourcePosition = function(position) {
var sourceMap = cache[position.source]; var sourceMap = sourceMapCache[position.source];
if (!sourceMap) { if (!sourceMap) {
// Call the (overrideable) retrieveSourceMap function to get the source map. // Call the (overrideable) retrieveSourceMap function to get the source map.
var urlAndMap = retrieveSourceMap(position.source); var urlAndMap = retrieveSourceMap(position.source);
if (urlAndMap) { if (urlAndMap) {
sourceMap = cache[position.source] = { sourceMap = sourceMapCache[position.source] = {
url: urlAndMap.url, url: urlAndMap.url,
map: new SourceMapConsumer(urlAndMap.map) map: new SourceMapConsumer(urlAndMap.map)
}; };
@ -102,11 +119,11 @@ var mapSourcePosition = exports.mapSourcePosition = function(cache, position) {
// Parses code generated by FormatEvalOrigin(), a function inside V8: // Parses code generated by FormatEvalOrigin(), a function inside V8:
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js // https://code.google.com/p/v8/source/browse/trunk/src/messages.js
function mapEvalOrigin(cache, origin) { function mapEvalOrigin(origin) {
// Most eval() calls are in this format // Most eval() calls are in this format
var match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin); var match = /^eval at ([^(]+) \((.+):(\d+):(\d+)\)$/.exec(origin);
if (match) { if (match) {
var position = mapSourcePosition(cache, { var position = mapSourcePosition({
source: match[2], source: match[2],
line: match[3], line: match[3],
column: match[4] column: match[4]
@ -118,20 +135,20 @@ function mapEvalOrigin(cache, origin) {
// Parse nested eval() calls using recursion // Parse nested eval() calls using recursion
match = /^eval at ([^(]+) \((.+)\)$/.exec(origin); match = /^eval at ([^(]+) \((.+)\)$/.exec(origin);
if (match) { if (match) {
return 'eval at ' + match[1] + ' (' + mapEvalOrigin(cache, match[2]) + ')'; return 'eval at ' + match[1] + ' (' + mapEvalOrigin(match[2]) + ')';
} }
// Make sure we still return useful information if we didn't find anything // Make sure we still return useful information if we didn't find anything
return origin; return origin;
} }
function wrapCallSite(cache, frame) { function wrapCallSite(frame) {
// Most call sites will return the source file from getFileName(), but code // Most call sites will return the source file from getFileName(), but code
// passed to eval() ending in "//# sourceURL=..." will return the source file // passed to eval() ending in "//# sourceURL=..." will return the source file
// from getScriptNameOrSourceURL() instead // from getScriptNameOrSourceURL() instead
var source = frame.getFileName() || frame.getScriptNameOrSourceURL(); var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
if (source) { if (source) {
var position = mapSourcePosition(cache, { var position = mapSourcePosition({
source: source, source: source,
line: frame.getLineNumber(), line: frame.getLineNumber(),
column: frame.getColumnNumber() column: frame.getColumnNumber()
@ -148,7 +165,7 @@ function wrapCallSite(cache, frame) {
// Code called using eval() needs special handling // Code called using eval() needs special handling
var origin = frame.isEval() && frame.getEvalOrigin(); var origin = frame.isEval() && frame.getEvalOrigin();
if (origin) { if (origin) {
origin = mapEvalOrigin(cache, origin); origin = mapEvalOrigin(origin);
return { return {
__proto__: frame, __proto__: frame,
getEvalOrigin: function() { return origin; } getEvalOrigin: function() { return origin; }
@ -162,12 +179,12 @@ function wrapCallSite(cache, frame) {
// This function is part of the V8 stack trace API, for more info see: // This function is part of the V8 stack trace API, for more info see:
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi // http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
function prepareStackTrace(error, stack) { function prepareStackTrace(error, stack) {
// Store source maps in a cache so we don't load them more than once when if (emptyCacheBetweenOperations) {
// formatting a single stack trace (don't cache them forever though in case fileContentsCache = {};
// the files change on disk and the user wants to see the updated mapping) sourceMapCache = {};
var cache = {}; }
return error + stack.map(function(frame) { return error + stack.map(function(frame) {
return '\n at ' + wrapCallSite(cache, frame); return '\n at ' + wrapCallSite(frame);
}).join(''); }).join('');
} }
@ -179,8 +196,7 @@ function handleUncaughtExceptions(error) {
} }
var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack); var match = /\n at [^(]+ \((.*):(\d+):(\d+)\)/.exec(error.stack);
if (match) { if (match) {
var cache = {}; var position = mapSourcePosition({
var position = mapSourcePosition(cache, {
source: match[1], source: match[1],
line: match[2], line: match[2],
column: match[3] column: match[3]
@ -206,6 +222,8 @@ exports.install = function(options) {
options = options || {}; options = options || {};
var installHandler = 'handleUncaughtExceptions' in options ? var installHandler = 'handleUncaughtExceptions' in options ?
options.handleUncaughtExceptions : true; options.handleUncaughtExceptions : true;
emptyCacheBetweenOperations = 'emptyCacheBetweenOperations' in options ?
options.emptyCacheBetweenOperations : false;
// Allow source maps to be found by methods other than reading the files // Allow source maps to be found by methods other than reading the files
// directly from disk. // directly from disk.

View File

@ -1,4 +1,6 @@
require('./source-map-support').install(); require('./source-map-support').install({
emptyCacheBetweenOperations: true // Needed to be able to test for failure
});
var SourceMapGenerator = require('source-map').SourceMapGenerator; var SourceMapGenerator = require('source-map').SourceMapGenerator;
var child_process = require('child_process'); var child_process = require('child_process');