From: Jesse Tane Date: Tue, 16 Dec 2014 00:09:06 +0000 (-0500) Subject: base64 changes to improve node compatibility X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=28b2ff6add1b424d39e8c7d13fcadd67b091ca2a;p=buffer.git base64 changes to improve node compatibility --- diff --git a/index.js b/index.js index 5321a25..ebe1406 100644 --- a/index.js +++ b/index.js @@ -75,8 +75,6 @@ function Buffer (subject, encoding, noZero) { if (type === 'number') length = subject > 0 ? subject >>> 0 : 0 else if (type === 'string') { - if (encoding === 'base64') - subject = base64clean(subject) length = Buffer.byteLength(subject, encoding) } else if (type === 'object' && subject !== null) { // assume object is array-like if (subject.type === 'Buffer' && isArray(subject.data)) @@ -986,11 +984,13 @@ Buffer._augment = function (arr) { return arr } -var INVALID_BASE64_RE = /[^+\/0-9A-z]/g +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 converts strings with length < 2 to '' + if (str.length < 2) return '' // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not while (str.length % 4 !== 0) { str = str + '=' @@ -1056,7 +1056,7 @@ function utf16leToBytes (str) { } function base64ToBytes (str) { - return base64.toByteArray(str) + return base64.toByteArray(base64clean(str)) } function blitBuffer (src, dst, offset, length, unitSize) {