source-map-support
Advanced tools
Comparing version 0.1.9 to 0.2.0
{ | ||
"name": "source-map-support", | ||
"description": "Fixes stack traces for files with source maps", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"main": "./source-map-support.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -5,2 +5,31 @@ var SourceMapConsumer = require('source-map').SourceMapConsumer; | ||
function isInBrowser() { | ||
return typeof window !== 'undefined'; | ||
} | ||
function retrieveFile(path) { | ||
// Use SJAX if we are in the browser | ||
if (isInBrowser()) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', path, false); | ||
xhr.send(null); | ||
return xhr.readyState === 4 ? xhr.responseText : null; | ||
} | ||
// Otherwise, use the filesystem | ||
try { | ||
return fs.readFileSync(path, 'utf8'); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
// 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(dir, url) { | ||
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 | ||
@@ -12,7 +41,4 @@ // generated source filename; returns a {map, optional url} object, or null if | ||
var retrieveSourceMap = function (source) { | ||
if (!fs.existsSync(source)) | ||
return null; | ||
// Get the URL of the source map | ||
var fileData = fs.readFileSync(source, 'utf8'); | ||
var fileData = retrieveFile(source); | ||
var match = /\/\/[#@]\s*sourceMappingURL=(.*)\s*$/m.exec(fileData); | ||
@@ -28,11 +54,7 @@ if (!match) return null; | ||
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString(); | ||
} | ||
else { | ||
} else { | ||
// Support source map URLs relative to the source URL | ||
var dir = path.dirname(source); | ||
sourceMappingURL = path.resolve(dir, sourceMappingURL); | ||
if (fs.existsSync(sourceMappingURL)) { | ||
sourceMapData = fs.readFileSync(sourceMappingURL, 'utf8'); | ||
} | ||
sourceMappingURL = supportRelativeURL(dir, sourceMappingURL); | ||
sourceMapData = retrieveFile(sourceMappingURL, 'utf8'); | ||
} | ||
@@ -74,3 +96,4 @@ | ||
if (sourceMap.url) { | ||
originalPosition.source = path.resolve(path.dirname(sourceMap.url), originalPosition.source); | ||
originalPosition.source = supportRelativeURL( | ||
path.dirname(sourceMap.url), originalPosition.source); | ||
} | ||
@@ -203,5 +226,5 @@ return originalPosition; | ||
// the original source code. | ||
if (installHandler) { | ||
if (installHandler && !isInBrowser()) { | ||
process.on('uncaughtException', handleUncaughtExceptions); | ||
} | ||
}; |
22630
477