使用CloudFront函数时如何做base64 encoding/decoding?

How to do base64 encoding/decoding when using CloudFront function?

Amazon CloudFront Function 是 AWS 推出的一项新功能。
CloudFront 函数只能使用 JavaScript 编写。
https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/

我们的网站生成时间戳并使用 btoa()(Base64 编码)对其进行编码。
然后,网站向 CloudFront 函数发送包含编码时间戳的 HTTP GET 请求。

var sending_time        = new Date().getTime();
var enc_sending_time    = btoa(sending_time);

function generate_http_request(enc_sending_time)
{
...
}

CloudFront 函数收到 HTTP 请求后,
它应该使用 atob() 解码时间戳。

但是,CloudFront 函数不支持 atob()。 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html

如何对整数进行base64编码
然后在 CloudFront 函数端进行 base64 解码
不使用 btoa() 和 atob()? (仅限JavaScript)

CloudFront 不支持 btoa() 和 atob() 函数,
所以我们不能使用这些函数进行 Base 64 encoding/decoding。

因此,作为替代方案,我们可以使用以下内容:
https://gist.github.com/oeon/0ada0457194ebf70ec2428900ba76255

很有魅力!

CloudFront adds a few non-standard methods,包括一个要从 base64 解码的字符串:

var decoded = String.bytesFrom(str, 'base64');

它也接受 base64url 编码版本。

他们在 an example which decodes JWTs at the edge 中展示了这个。

不幸的是,他们似乎没有进行 base64 编码的等价物。