由于浏览器和 Nodejs 中的接口等不一致,所以需要分类讨论
浏览器
此方案基于 utf-8 编码实现编/解码。经过个人探索,除了 utf-8 外,JavaScript 字符串仅可以使用 utf-16 来编解码,原理类似,仅在取二进制值时不同,不在此赘述,留作思考题。
实现
如果仅仅只是快速采用轮子,可以直接 cv 下面的代码。
javascript 版:
function decode64(text) {
return new TextDecoder().decode(Uint8Array.from(atob(text), (c) => c.charCodeAt(0)))
}
" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="javascript hljs language-javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">encode64</span>(<span class="hljs-params">text</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-title function_">btoa</span>(<span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCode</span>(...<span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>().<span class="hljs-title function_">encode</span>(text)))
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">decode64</span>(<span class="hljs-params">text</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextDecoder</span>().<span class="hljs-title function_">decode</span>(<span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title function_">atob</span>(text), <span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">charCodeAt</span>(<span class="hljs-number">0</span>)))
}
</pre><p>typescript 版:</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="function encode64(text: string): string {
return btoa(String.fromCharCode(...new TextEncoder().encode(text)))
}
function decode64(text: string): string {
return new TextDecoder().decode(Uint8Array.from(atob(text), (c) => c.charCodeAt(0)))
}" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">encode64</span>(<span class="hljs-params">text: <span class="hljs-built_in">string</span></span>): <span class="hljs-built_in">string</span> {
<span class="hljs-keyword">return</span> <span class="hljs-title function_">btoa</span>(<span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCode</span>(...<span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>().<span class="hljs-title function_">encode</span>(text)))
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">decode64</span>(<span class="hljs-params">text: <span class="hljs-built_in">string</span></span>): <span class="hljs-built_in">string</span> {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextDecoder</span>().<span class="hljs-title function_">decode</span>(<span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title function_">atob</span>(text), <span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">charCodeAt</span>(<span class="hljs-number">0</span>)))
}</pre><h3 id="item-0-3">原理讲解</h3><p>浏览器中用于将字符串和 base64 互转的 api 为 <a href="https://link.segmentfault.com/?enc=ZarhZFGgxiaC9hrlVMPmyw%3D%3D.qqHKsuMuUuy%2BF%2FaEeh3EqT1A0FeCAufGc%2BNsY3BWRH9dtJg0HYu9ZLUmzo90YkAHsWtqq541NOHrkZHjmKwH6Q%3D%3D" rel="nofollow" target="_blank"><code>atob</code></a> 和 <a href="https://link.segmentfault.com/?enc=WKAOKn2g9IxFEhbylZ3VbQ%3D%3D.VCx%2FUO6Ow0jYv%2BvqzaH9z0SwQLu1uNfzX1Us0TvKTpWTPiUJBNboJzGxhbamfIFoevVPt53PCBjM9wGQCISQbA%3D%3D" rel="nofollow" target="_blank"><code>btoa</code></a> ,但是这两个 API 只支持 Latin-1 字符集。如果需要对中文进行编码,<code>btoa</code> 则会出现如下错误:</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range." aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="hljs language-smali">Uncaught DOMException: Failed to<span class="hljs-built_in"> execute </span>'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.</pre><p><code>atob</code> 则无法解码出正确的字符串</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="// '5Lit5paH' 为 '中文' 的 utf-8 的 base64 编码
console.log(atob('5Lit5paH'))
// 输出: '䏿\x96\x87'" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-comment">// '5Lit5paH' 为 '中文' 的 utf-8 的 base64 编码</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">atob</span>(<span class="hljs-string">'5Lit5paH'</span>))
<span class="hljs-comment">// 输出: '䏿\x96\x87'</span></pre><p>mdn 中的<a href="https://link.segmentfault.com/?enc=qhMSPzc8S9u6%2BhCCe%2FR5cw%3D%3D.D74glFtdsL6H3eO6RHvF93zf0f%2BeBLpZFzPIcJtlN1FGfPIGKPV1mucqj0%2BagywCCOwMrKFJafolHCuk00MPyamtTizZuyMxoGQJlJMhb6Aw%2FmiABAR5mhZhx0vuXED3" rel="nofollow" target="_blank">解释</a>如下</p><blockquote>根据设计,Base64 仅将二进制数据作为其输入。而在 JavaScript 字符串中,这意味着每个字符只能使用一个字节表示。所以,如果你将一个字符串传递给 <code>btoa()</code>,而其中包含了需要使用超过一个字节才能表示的字符,你就会得到一个错误,因为这个字符串不能被看作是二进制数据</blockquote><p>所以对于中文编码成 base64,我们的思路就很简单:取字符串的二进制(<code>utf-8</code> 也好 <code>utf-16</code> 也好),取每一字节当做一个字符的码点,重新生成一个字符串,再拿这个字符串去调用 <code>btoa</code>,就可以成功转出来 base64 了。</p><p>那么第一步就是拿到这个字符串的 <code>utf-8</code> 的二进制流。虽然 JavaScript 默认使用 <code>utf-16</code>,但是其提供了 <a href="https://link.segmentfault.com/?enc=65FrcJU86kIiMPhB0Cy6%2BA%3D%3D.y36ExlNkxxJhacn46TImPIdmgxUslYd1LuxvMqlW2F3K8wSJtORvKJ6mMhjFK%2FZFfE4dIMBplMkSDGQp5EhYKhOPG5zvqwIa7dIpN8r%2BMTs%3D" rel="nofollow" target="_blank"><code>TextEncoder.encode()</code></a> 和 <a href="https://link.segmentfault.com/?enc=2W2lMs89%2FRyww%2FpNxukooQ%3D%3D.cZr1a%2BBbP2ky6Q711D0WbwNIIRcY1Qw%2Bk9bfMU6HPrp%2FgTwukjWVB4BDKG3K5N5MXCMtTLyugjnqboeHHAKwu%2FELjvYPhjpJOqp9LsPis3o%3D" rel="nofollow" target="_blank"><code>TextDecoder.decode()</code></a> 用于字符串和 <code>utf-8</code> 字节流转换。接下来以<code>中文</code>这个字符串为例,分<strong>编码</strong>和<strong>解码</strong>进行讲解。</p><h4>编码</h4><p>对于编码,我们可以通过下面的代码拿到一个字符串的 <code>utf-8</code> 字节流</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="new TextEncoder().encode('中文')
// 此字节流的值为 Uint8Array: [228, 184, 173, 230, 150, 135]" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>().<span class="hljs-title function_">encode</span>(<span class="hljs-string">'中文'</span>)
<span class="hljs-comment">// 此字节流的值为 Uint8Array: [228, 184, 173, 230, 150, 135]</span></pre><p>现在我们就拿到了<code>中文</code>的二进制,为 228, 184, 173, 230, 150, 135,一共6位。</p><p>接下来就可以拿这6个字节来构造6个字符,形成一个字符串。这些二进制的值又被称为一个字符的码点,所以我们可以使用 <a href="https://link.segmentfault.com/?enc=UkVrnu%2Bh%2BGMd3jsy8aruaQ%3D%3D.7968L03d%2FdLJ8ZBXbJ63RAyevcqZoilnLClr6vN6O22l%2BpCbOHpuLzt%2FqOlqrQN06p1ojxTEpmCVXZQcKT2tX7%2F9ISCZ129%2FjMKAOu7bM9O%2FiXbecfbcpVHeoR7DknxCRLg9t6fK2Q3mC6ePoBf9Fg%3D%3D" rel="nofollow" target="_blank"><code>String.fromCharCode()</code></a> 这个静态方法将这 6 个值转换成 6 个字符</p><blockquote><p>静态 <strong><code>String.fromCharCode()</code></strong> 方法返回由指定的 UTF-16 代码单元序列创建的字符串。</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="String.fromCharCode(num1[, ...[, numN]])" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="hljs language-reasonml"><span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">String</span>.</span></span>from<span class="hljs-constructor">CharCode(<span class="hljs-params">num1</span>[, <span class="hljs-operator">...</span>[, <span class="hljs-params">numN</span>]])</span></pre><p>—— mdn</p></blockquote><p>由 <code>String.fromCharCode()</code> 的函数签名和 <code>Uint8Array</code> 的数组特性可知,我们可以直接使用下面的代码将得到的二进制字节流转换成字符串</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="String.fromCharCode(...new TextEncoder().encode('中文'))
// 值: '䏿\x96\x87'" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCode</span>(...<span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>().<span class="hljs-title function_">encode</span>(<span class="hljs-string">'中文'</span>))
<span class="hljs-comment">// 值: '䏿\x96\x87'</span></pre><p>现在我们将一个 <code>utf-16</code> 的字符串成功转成了 <code>utf-8</code> 字节流对应的字符串,现在我们就可以使用 <code>btoa()</code> 将这个字符串转换成 base64 编码了。</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="btoa(String.fromCharCode(...new TextEncoder().encode('中文')))
// 值: '5Lit5paH'" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-title function_">btoa</span>(<span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCode</span>(...<span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>().<span class="hljs-title function_">encode</span>(<span class="hljs-string">'中文'</span>)))
<span class="hljs-comment">// 值: '5Lit5paH'</span></pre><h4>解码</h4><p>对于解码,首先我们使用 <code>atob()</code> 将上面得到的 base64 编码转换成字符串。</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="atob('5Lit5paH')
// 值: '䏿\x96\x87'" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-title function_">atob</span>(<span class="hljs-string">'5Lit5paH'</span>)
<span class="hljs-comment">// 值: '䏿\x96\x87'</span></pre><p>接下来我们需要将这个字符串转换成一个 <code>Uint8Array</code> 二进制字节流,这里我们可以使用 <a href="https://link.segmentfault.com/?enc=%2B8qG7mLPs2yI19B6vOcCng%3D%3D.5rI5VrJnkMlb5CIEhxw8vJJin4t2ViRlpaVTS7OEvBlUcWQ4imuzpI%2FXterloTCOkNM1F7rK7B3XZhWIpMcfelwTwSq6ZO6oA%2F30Hrv9TxekgPYeLOzUYbQIpME5umrQAZZ1Yj8j8rKrpC5idYxclA%3D%3D" rel="nofollow" target="_blank"><code>Uint8Array.from()</code></a> 这个 api 来将字符串转换成 <code>Uint8Array</code> 字节流。</p><blockquote><p><code>TypedArray.from()</code> 方法 从一个类数组或者可迭代对象中创建一个新<a href="https://link.segmentfault.com/?enc=JbMT4U6y6mwWbnpB%2B9vcXA%3D%3D.d8FMNrAK7WeBZ1ex5dNvE01Mbj0yI0hSi0BMXgVRbcZlk8LtiY%2Fs29sWqkAVsDiD4BVQ5QlFI%2Bs3vbDcHJiQ96UrpN8g9kSTKMUzWg2HKnHvz8eif0oIOUwsLxDbW1DS6P7P5c%2BYObF62%2BHGV85eaA%3D%3D" rel="nofollow" target="_blank">类型数组</a>。这个方法和 <a href="https://link.segmentfault.com/?enc=ZZV%2FsSazUyDEpWhGbT%2F9%2Fw%3D%3D.hF0SKOflfGLHTrdcj9TBUp31ne7OEh9jifD4539nDMF8wEZ2Dd4DCr9PGFImIbs%2Fknu2lyXJZsAlmRkVeohXX8q995ydbDz4rYXN4D%2F2YxwofB0USHVvGay5uIdzsfgp" rel="nofollow" target="_blank"><code>Array.from()</code></a> 类似。</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="TypedArray.from(arrayLike, mapFn)
TypedArray.from(arrayLike, mapFn, thisArg)" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="javascript hljs language-javascript"><span class="hljs-title class_">TypedArray</span>.<span class="hljs-title function_">from</span>(arrayLike, mapFn)
<span class="hljs-title class_">TypedArray</span>.<span class="hljs-title function_">from</span>(arrayLike, mapFn, thisArg)</pre><p>—— mdn</p></blockquote><p>但是如果我们如下代码直接传递给 <code>Uint8Array</code> 的话,会发现这个二进制字节流的每一位都是 <code>0</code>。</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="Uint8Array.from(atob('5Lit5paH'))
// 值: Uint8Array: [0, 0, 0, 0, 0, 0]" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title function_">atob</span>(<span class="hljs-string">'5Lit5paH'</span>))
<span class="hljs-comment">// 值: Uint8Array: [0, 0, 0, 0, 0, 0]</span></pre><p>此处明显为 <code>Uint8Array.from</code> 没有正确取到该字符串每一位的码点(即使是使用 <code>Uint16Array</code> 也不行),但是观察他的函数签名,我们可以发现有 <code>mapFn</code> 这个参数,所以我们可以通过这个参数取到每一个字符的码点。</p><p>对于取字符的码点,有 <a href="https://link.segmentfault.com/?enc=8L%2BLyldF1N9mjS46PmZlhg%3D%3D.SMCRXlJ1UIR%2Bt3%2BUNiVEnfVvPHXVYuUi6790MD2dJ240R%2F2tTmRzYV9oxRi5MDXqtZGMGo%2B3BCYrNQvMy93%2BqMTfO38IDX4jvQGi6aTvHTkP%2FVJ5jmcYf%2BymjNG9ZMeI612cSG0ApfqUznHzIP5MBA%3D%3D" rel="nofollow" target="_blank"><code>String.prototype.charCodeAt()</code></a> 这个 api。所以转换成二进制字节流的代码如下:</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="Uint8Array.from(atob('5Lit5paH'), (c) => c.charCodeAt(0))
// 值: Uint8Array: [228, 184, 173, 230, 150, 135]" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title function_">atob</span>(<span class="hljs-string">'5Lit5paH'</span>), <span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">charCodeAt</span>(<span class="hljs-number">0</span>))
<span class="hljs-comment">// 值: Uint8Array: [228, 184, 173, 230, 150, 135]</span></pre><p>现在我们就可以将其传递给 <code>TextDecoder.decode()</code> 转换成字符串了!</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="new TextDecoder().decode(Uint8Array.from(atob(text), (c) => c.charCodeAt(0)))" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-keyword">new</span> <span class="hljs-title class_">TextDecoder</span>().<span class="hljs-title function_">decode</span>(<span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>(<span class="hljs-title function_">atob</span>(text), <span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">charCodeAt</span>(<span class="hljs-number">0</span>)))</pre><h3 id="item-0-4">思考题</h3><p>上面的实现为通过 <code>utf-8</code> 编码的二进制流与 base64 互转的实现,那么如果通过 JavaScript 默认的 <code>utf-16</code> 又该怎么实现呢?</p><blockquote>提示:可以参考 mdn 中的<a href="https://link.segmentfault.com/?enc=xmt4XdMU9eVjyXkn5BC%2F7w%3D%3D.9q9UwDTtb3wUXh0%2FOl%2B%2BPUpQGUQqDskYooiAWevZWADobV%2FG2hrMSCxkCFBULVZTUp9ekdnEP%2FVSyhWX9l3cBHm3%2FGDFDab96J33GYk3SFZsFOufxvCUe8XwRCYrTgt3" rel="nofollow" target="_blank">此部分</a>进行实现,并优化到一行代码。</blockquote><h2 id="item-0-5">Nodejs</h2><p>Nodejs 中由于有原生的 api 用于转换 base64,在 vscode 中如果把鼠标放到 <code>atob()</code> 或者 <code>btoa()</code> 上,可以得到如下说明</p><blockquote>This function is only provided for compatibility with legacy web platform APIs and should never be used in new code, because they use strings to represent binary data and predate the introduction of typed arrays in JavaScript. For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and<code>buf.toString('base64')</code>.</blockquote><p>据此实现就很简单了。</p><h3 id="item-0-6">实现</h3><p>JavaScript 版:</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="function encode64(text) {
return Buffer.from(text).toString('base64')
}
function decode64(text) {
return Buffer.from(text, 'base64').toString()
}" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="javascript hljs language-javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">encode64</span>(<span class="hljs-params">text</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(text).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>)
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">decode64</span>(<span class="hljs-params">text</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(text, <span class="hljs-string">'base64'</span>).<span class="hljs-title function_">toString</span>()
}</pre><p>typescript 版:</p><div class="widget-codetool" style="display: none;">
<div class="widget-codetool--inner">
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text="function encode64(text: string): string {
return Buffer.from(text).toString('base64')
}
function decode64(text: string): string {
return Buffer.from(text, 'base64').toString()
}" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="typescript hljs language-typescript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">encode64</span>(<span class="hljs-params">text: <span class="hljs-built_in">string</span></span>): <span class="hljs-built_in">string</span> {
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(text).<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>)
}
<span class="hljs-keyword">function</span> <span class="hljs-title function_">decode64</span>(<span class="hljs-params">text: <span class="hljs-built_in">string</span></span>): <span class="hljs-built_in">string</span> {
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(text, <span class="hljs-string">'base64'</span>).<span class="hljs-title function_">toString</span>()
}</pre>