Canonical way to convert image arraybuffer to base64?

Hi there–

I’ve been using (and enamored with) Workers to build conversation design or “chat” tooling.

One of the things I’ve been able to accomplish in other environments but cannot get to work yet in Workers is image recognition.

Per the docs google cloud image docs I need to post this payload to an endpoint:

const request = {
  "requests": [
    {
      "image": {
        "content": "__big_base64_representation__"
      },
      "features": [
        {
          "maxResults": 10,
          "type": "LABEL_DETECTION"
        }
      ]
    }
  ]
}

I have access to an arraybuffer and I need to convert to a base64 string and I was attempting using this:

      const imgStr = btoa(String.fromCharCode(...new Uint8Array(data)))

But the error I see in console is “script exceeded time limit”-- so perhaps above isn’t performant enough.

Has anyone successfully converted array buffers to base64 in a Workers-compliant way so they can be consumed by 3rd-party resource.

Happy to collaborate or share additional techniques

Thanks & best,
Victor

Update-- all praise to Jon Leighton

I can’t posted links in this forum at the moment, but if you need a FAST arraybufer <> base64 implementation, google "“jonleighton/base64ArrayBuffer.js”