diff --git a/source-map-support.js b/source-map-support.js index 5074dc5..b312e62 100644 --- a/source-map-support.js +++ b/source-map-support.js @@ -14,6 +14,9 @@ var fileContentsCache = {}; // Maps a file path to a source map for that file var sourceMapCache = {}; +// Regex for detecting source maps +var reSourceMap = /^data:application\/json[^,]+base64,/; + function isInBrowser() { return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function')); } @@ -97,10 +100,10 @@ function retrieveSourceMap(source) { // Read the contents of the source map var sourceMapData; - var dataUrlPrefix = "data:application/json;base64,"; - if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) { + if (reSourceMap.test(sourceMappingURL)) { // Support source map URL as a data url - sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString(); + var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1); + sourceMapData = new Buffer(rawData, "base64").toString(); sourceMappingURL = null; } else { // Support source map URLs relative to the source URL diff --git a/test.js b/test.js index 8eab1c4..bd25f92 100644 --- a/test.js +++ b/test.js @@ -391,3 +391,27 @@ it('missing source maps should also be cached', function(done) { '1', // The retrieval should only be attempted once ]); }); + +/* The following test duplicates some of the code in + * `compareStackTrace` but appends a charset to the + * source mapping url. + */ +it('finds source maps with charset specified', function() { + var sourceMap = createMultiLineSourceMap() + var source = [ 'throw new Error("test");' ]; + var expected = [ + 'Error: test', + /^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/ + ]; + + fs.writeFileSync('.generated.js', 'exports.test = function() {' + + source.join('\n') + '};//@ sourceMappingURL=data:application/json;charset=utf8;base64,' + + new Buffer(sourceMap.toString()).toString('base64')); + try { + delete require.cache[require.resolve('./.generated')]; + require('./.generated').test(); + } catch (e) { + compareLines(e.stack.split('\n'), expected); + } + fs.unlinkSync('.generated.js'); +});