It turns out that:
let objStr = new Buffer(JSON.stringify(obj), 'ascii');
new Buffer(objStr, 'base64').toString('ascii') !== objStr
You misunderstood the
Buffer(str, [encoding]) constructor, the
encoding tells the constructor what encoding was used to create
str, or what encoding the constructor should use to decode
str into a byte array.
Buffer class represents byte streams, it's only when you convert it from/to strings that encoding comes into context.
You should instead use
buffer.toString("base64") to get base-64 encoded of the buffer content.
let objJsonStr = JSON.stringify(obj); let objJsonB64 = new Buffer(objJsonStr).toString("base64");