diff --git a/index.js b/index.js index 98013d1..6037809 100644 --- a/index.js +++ b/index.js @@ -22,16 +22,16 @@ function retrieveFile(path) { } if (/^\S+:\/\//.test(path) && !/^http(s)?:\/\//.test(path)) { - return null + return null; } try { var xhr = new XMLHttpRequest(); xhr.open('GET', path, false); xhr.send(null); - var contents = null + var contents = null; if (xhr.readyState === 4 && xhr.status === 200) { - contents = xhr.responseText + contents = xhr.responseText; } } catch (e) { @@ -44,9 +44,11 @@ function retrieveFile(path) { // 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 regx = /^\w+:\/\/[^\/]*/; + if (regx.test(url)) return url; if (!file) return url; var dir = path.dirname(file); - var match = /^\w+:\/\/[^\/]*/.exec(dir); + var match = regx.exec(dir); var protocol = match ? match[0] : ''; return protocol + path.resolve(dir.slice(protocol.length), url); } @@ -62,12 +64,11 @@ function retrieveSourceMapURL(source, position) { fileData = xhr.responseText; fileContentsCache[source] = fileData; } - if (xhr.status !== 200) return null - } catch (e) { return null } + if (xhr.status !== 200) return null; + } catch (e) { return null; } // Support providing a sourceMappingURL via the SourceMap header - var sourceMapHeader = xhr.getResponseHeader('SourceMap') || - xhr.getResponseHeader('X-SourceMap'); + var sourceMapHeader = xhr.getResponseHeader('SourceMap') || xhr.getResponseHeader('X-SourceMap'); if (sourceMapHeader) { return sourceMapHeader; } @@ -80,7 +81,7 @@ function retrieveSourceMapURL(source, position) { while (match = re.exec(fileData)) lastMatch = match; // eslint-disable-line if (!lastMatch) { var line = fileData.split(/(?:\r\n|\r|\n)/)[position.line - 1]; - line = line.replace(/^.*sourceMappingURL=/, '').replace(/"\s*\);\s*$/,'') + line = line.replace(/^.*sourceMappingURL=/, '').replace(/"\s*\);\s*$/,''); if (reSourceMap.test(line)) return line; return null; } @@ -103,8 +104,8 @@ function retrieveSourceMap(source, position) { // Support source map URL as a data url var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1); sourceMapData = new Buffer(rawData, 'base64').toString(); - sourceMapData = JSON.parse(sourceMapData) - source = sourceMapData.sources[0] + sourceMapData = JSON.parse(sourceMapData); + source = sourceMapData.sources[0]; sourceMappingURL = null; } else { // Support source map URLs relative to the source URL @@ -128,8 +129,8 @@ function mapSourcePosition(position) { // Call the (overrideable) retrieveSourceMap function to get the source map. var urlAndMap = retrieveSourceMap(position.source, position); if (urlAndMap) { - var source = urlAndMap.url ? position.source : urlAndMap.map.sources[0] - position.source = source + var source = urlAndMap.url ? position.source : urlAndMap.map.sources[0]; + position.source = source; sourceMap = sourceMapCache[source] = { url: urlAndMap.url, map: new SourceMapConsumer(urlAndMap.map) @@ -156,17 +157,17 @@ function mapSourcePosition(position) { // Resolve the source URL relative to the URL of the source map if (sourceMap && sourceMap.map) { - var pos + var pos; if (position.eval) { pos = { line: position.eval[0], column: position.eval[1] - } + }; } else { pos = { line: position.line, column: position.column - } + }; } var originalPosition = sourceMap.map.originalPositionFor(pos); @@ -200,7 +201,7 @@ function getEvalPosition(origin, ln, cn) { eval:[ln, cn] }); - return position + return position; } // Parse nested eval() calls using recursion @@ -231,8 +232,8 @@ function CallSiteToString() { fileLocation += ', '; // Expecting source position to follow. } if (fileName && !/^\w+:\/\//.test(fileName)) { - var root = location.protocol + '//' + location.host - fileName = root + (/^\//.test(fileName) ? fileName : '/' + fileName) + var root = location.protocol + '//' + location.host; + fileName = root + (/^\//.test(fileName) ? fileName : '/' + fileName); } if (fileName) { @@ -299,9 +300,9 @@ function wrapCallSite(frame) { // Most call sites will return the source file from getFileName(), but code // passed to eval() ending in "//# sourceURL=..." will return the source file // from getScriptNameOrSourceURL() instead - if (frame.isNative()) return frame + if (frame.isNative()) return frame; var source = frame.getFileName() || frame.getScriptNameOrSourceURL(); - var position + var position; if (source) { var line = frame.getLineNumber(); var column = frame.getColumnNumber() - 1; @@ -322,15 +323,15 @@ function wrapCallSite(frame) { var origin = frame.isEval() && frame.getEvalOrigin(); if (origin) { // ln and cn in eval - var ln = frame.getLineNumber() - var cn = frame.getColumnNumber() + var ln = frame.getLineNumber(); + var cn = frame.getColumnNumber(); position = getEvalPosition(origin, ln, cn); frame = cloneCallSite(frame); frame.getScriptNameOrSourceURL = function() { return position.source.replace(/\?.*$/, ''); }; frame.getLineNumber = function() { return position.line; }; frame.getColumnNumber = function() { return position.column ? position.column + 1: cn; }; frame.getEvalOrigin = function() { return origin; }; - frame.getFileName = function () { return position.source} + frame.getFileName = function () { return position.source;}; return frame; } @@ -347,8 +348,8 @@ function prepareStackTrace(error, stack) { } stack = stack.filter(function (frame) { var source = frame.getFileName() || frame.getScriptNameOrSourceURL(); - return !/node_modules\/mocha/.test(source) - }) + return !/node_modules\/mocha/.test(source); + }); return error + stack.map(function(frame) { return '\n at ' + wrapCallSite(frame); }).join(''); @@ -370,8 +371,7 @@ function getErrorSource(error) { if (contents) { var code = contents.split(/(?:\r\n|\r|\n)/)[line - 1]; if (code) { - return source + ':' + line + '\n' + code + '\n' + - new Array(column).join(' ') + '^'; + return source + ':' + line + '\n' + code + '\n' + new Array(column).join(' ') + '^'; } } } @@ -384,12 +384,12 @@ exports.mapSourcePosition = mapSourcePosition; exports.retrieveSourceMap = retrieveSourceMap; module.exports = function (option) { - option = option || {} + option = option || {}; if (/^file/i.test(location.protocol)) { - console.warn('stack-source-map not works on file protocol') + console.warn('stack-source-map not works on file protocol'); } else { Error.prepareStackTrace = prepareStackTrace; } - if (option.hasOwnProperty('empty')) emptyCacheBetweenOperations = option.empty -} + if (option.hasOwnProperty('empty')) emptyCacheBetweenOperations = option.empty; +};