var type = typeof subject
- // Workaround: node's base64 implementation allows for non-padded strings
- // while base64-js does not.
if (encoding === 'base64' && type === 'string') {
- subject = stringtrim(subject)
- while (subject.length % 4 !== 0) {
- subject = subject + '='
- }
+ subject = base64clean(subject)
}
// Find the length
return arr
}
+var INVALID_BASE64_RE = /[^+\/0-9A-z]/g
+
+function base64clean (str) {
+ // Node strips out invalid characters like \n and \t from the string, base64-js does not
+ str = stringtrim(str).replace(INVALID_BASE64_RE, '')
+ // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
+ while (str.length % 4 !== 0) {
+ str = str + '='
+ }
+ return str
+}
+
function stringtrim (str) {
if (str.trim) return str.trim()
return str.replace(/^\s+|\s+$/g, '')