Skip to content

Commit b6a96e0

Browse files
committed
support charset in data url
1 parent e0e8653 commit b6a96e0

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

source-map-support.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ var fileContentsCache = {};
1414
// Maps a file path to a source map for that file
1515
var sourceMapCache = {};
1616

17+
// Regex for detecting source maps
18+
var reSourceMap = /^data:application\/json[^,]+base64,/;
19+
1720
function isInBrowser() {
1821
return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function'));
1922
}
@@ -97,10 +100,10 @@ function retrieveSourceMap(source) {
97100

98101
// Read the contents of the source map
99102
var sourceMapData;
100-
var dataUrlPrefix = "data:application/json;base64,";
101-
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) {
103+
if (reSourceMap.test(sourceMappingURL)) {
102104
// Support source map URL as a data url
103-
sourceMapData = new Buffer(sourceMappingURL.slice(dataUrlPrefix.length), "base64").toString();
105+
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
106+
sourceMapData = new Buffer(rawData, "base64").toString();
104107
sourceMappingURL = null;
105108
} else {
106109
// Support source map URLs relative to the source URL

test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,27 @@ it('missing source maps should also be cached', function(done) {
391391
'1', // The retrieval should only be attempted once
392392
]);
393393
});
394+
395+
/* The following test duplicates some of the code in
396+
* `compareStackTrace` but appends a charset to the
397+
* source mapping url.
398+
*/
399+
it('finds source maps with charset specified', function() {
400+
var sourceMap = createMultiLineSourceMap()
401+
var source = [ 'throw new Error("test");' ];
402+
var expected = [
403+
'Error: test',
404+
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
405+
];
406+
407+
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
408+
source.join('\n') + '};//@ sourceMappingURL=data:application/json;charset=utf8;base64,' +
409+
new Buffer(sourceMap.toString()).toString('base64'));
410+
try {
411+
delete require.cache[require.resolve('./.generated')];
412+
require('./.generated').test();
413+
} catch (e) {
414+
compareLines(e.stack.split('\n'), expected);
415+
}
416+
fs.unlinkSync('.generated.js');
417+
});

0 commit comments

Comments
 (0)