atob 或者btoa is not defined

发布时间 2023-09-01 13:54:24作者: 守护式等待
atob 或者btoa 方法是浏览器实现的而非 js 自带,需要需要使用这两个方法需要自己实现  最前面加上这段js 就可以使用了
global.Buffer = global.Buffer || require('buffer').Buffer;

if (typeof btoa === 'undefined') {
    global.btoa = function (str) {
        return new Buffer.from(str).toString('base64');
    };
}
if (typeof atob === 'undefined') {
    global.atob = function (b64Encoded) {
        return new Buffer.from(b64Encoded, 'base64').toString();
    };
}