TextEncoder / TextDecoder:UTF-8(或其它)和字节之间的互转。

const bytes = new TextEncoder().encode("hello");     // Uint8Array
const str   = new TextDecoder().decode(bytes);       // "hello"

文本 → 字节 TextEncoder().encode(...)

const secretBytes = new TextEncoder().encode(process.env.JWT_SECRET);

步骤 说明
new TextEncoder() 浏览器/Node 全局类,默认 UTF-8
.encode(string) 将 JS 字符串按 UTF-8 编码为 Uint8Array
结果 每个 UTF-8 字节占一项,便于后续加密、写文件等二进制场景。

atobJavaScript 内置的全局函数,用来把 Base64 编码的字符串解码成普通字符串。名字的意思就是 ASCII to binary

// 语法
atob(encodedData)

// 示例
const encoded = "SGVsbG8gV29ybGQh"; // "Hello World!"
const decoded = atob(encoded);

console.log(decoded); // 输出: Hello World!

const decoded = Buffer.from(encoded, "base64").toString("utf-8");