Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Add charset support for dataUri base64
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Dec 14, 2019
1 parent 858cd9e commit d63937e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"define": false,
"window": false,
"atob": true,
"JSON": false
"JSON": false,
"TextDecoder": true
}
}
41 changes: 36 additions & 5 deletions lib/source-map-resolve-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,24 @@ function resolveSourceMapSync(code, codeUrl, read) {
return mapData
}

var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/
var dataUriRegex = /^data:(([^,;]*)(;[^,;]*)*)(?:,(.*))?$/
var jsonMimeTypeRegex = /^(?:application|text)\/json$/

function decodeBase64String(utfLabel, b64) {
var buf = base64ToBuf(b64);
var txt = new TextDecoder(utfLabel, {fatal: true}).decode(buf);
return txt;
function base64ToBuf(str) {
var binStr = atob(str);
var len = binStr.length;
var arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
return arr;
}
}

function resolveSourceMapHelper(code, codeUrl) {
codeUrl = urix(codeUrl)

Expand All @@ -83,9 +98,23 @@ function resolveSourceMapHelper(code, codeUrl) {

var dataUri = url.match(dataUriRegex)
if (dataUri) {
var mimeType = dataUri[1]
var lastParameter = dataUri[2] || ""
var encoded = dataUri[3] || ""
var mimeType = dataUri[2]
var lastParameter = dataUri[3] || ""
var encoded = dataUri[4] || ""
/**
*
* @link https://tools.ietf.org/html/rfc2397#section-2
*/
var defaultCharset = 'us-ascii'
var charset = dataUri[1]
.split(';')
.filter(function(x) {
return x.startsWith('charset=')
})
.map(function(x) {
return x.slice('charset='.length)
})
.pop() || defaultCharset
var data = {
sourceMappingURL: url,
url: null,
Expand All @@ -98,7 +127,9 @@ function resolveSourceMapHelper(code, codeUrl) {
throw error
}
data.map = parseMapToJSON(
lastParameter === ";base64" ? atob(encoded) : decodeURIComponent(encoded),
lastParameter === ";base64" ?
decodeBase64String(charset, encoded) :
decodeURIComponent(encoded),
data
)
return data
Expand Down
41 changes: 36 additions & 5 deletions source-map-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,24 @@ void (function(root, factory) {
return mapData
}

var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/
var dataUriRegex = /^data:(([^,;]*)(;[^,;]*)*)(?:,(.*))?$/
var jsonMimeTypeRegex = /^(?:application|text)\/json$/

function decodeBase64String(utfLabel, b64) {
var buf = base64ToBuf(b64);
var txt = new TextDecoder(utfLabel, {fatal: true}).decode(buf);
return txt;
function base64ToBuf(str) {
var binStr = atob(str);
var len = binStr.length;
var arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
return arr;
}
}

function resolveSourceMapHelper(code, codeUrl) {
var url = sourceMappingURL.getFrom(code)
if (!url) {
Expand All @@ -89,9 +104,23 @@ void (function(root, factory) {

var dataUri = url.match(dataUriRegex)
if (dataUri) {
var mimeType = dataUri[1]
var lastParameter = dataUri[2] || ""
var encoded = dataUri[3] || ""
var mimeType = dataUri[2]
var lastParameter = dataUri[3] || ""
var encoded = dataUri[4] || ""
/**
*
* @link https://tools.ietf.org/html/rfc2397#section-2
*/
var defaultCharset = 'us-ascii'
var charset = dataUri[1]
.split(';')
.filter(function(x) {
return x.startsWith('charset=')
})
.map(function(x) {
return x.slice('charset='.length)
})
.pop() || defaultCharset
var data = {
sourceMappingURL: url,
url: null,
Expand All @@ -104,7 +133,9 @@ void (function(root, factory) {
throw error
}
data.map = parseMapToJSON(
lastParameter === ";base64" ? atob(encoded) : decodeURIComponent(encoded),
lastParameter === ";base64" ?
decodeBase64String(charset, encoded) :
decodeURIComponent(encoded),
data
)
return data
Expand Down

0 comments on commit d63937e

Please sign in to comment.