support charset in data url

master
Linus Unnebäck 9 years ago
parent e0e8653cc8
commit b6a96e067e

@ -14,6 +14,9 @@ var fileContentsCache = {};
// Maps a file path to a source map for that file // Maps a file path to a source map for that file
var sourceMapCache = {}; var sourceMapCache = {};
// Regex for detecting source maps
var reSourceMap = /^data:application\/json[^,]+base64,/;
function isInBrowser() { function isInBrowser() {
return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function')); return ((typeof window !== 'undefined') && (typeof XMLHttpRequest === 'function'));
} }
@ -97,10 +100,10 @@ function retrieveSourceMap(source) {
// Read the contents of the source map // Read the contents of the source map
var sourceMapData; var sourceMapData;
var dataUrlPrefix = "data:application/json;base64,"; if (reSourceMap.test(sourceMappingURL)) {
if (sourceMappingURL.slice(0, dataUrlPrefix.length).toLowerCase() == dataUrlPrefix) {
// Support source map URL as a data url // 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; sourceMappingURL = null;
} else { } else {
// Support source map URLs relative to the source URL // Support source map URLs relative to the source URL

@ -391,3 +391,27 @@ it('missing source maps should also be cached', function(done) {
'1', // The retrieval should only be attempted once '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');
});

Loading…
Cancel
Save