extism / js-pdk

Write Extism plugins in JavaScript & TypeScript
BSD 3-Clause "New" or "Revised" License
56 stars 16 forks source link

feat: add Host.arrayBufferToBase64 and Host.base64ToArrayBuffer #94

Closed chrisdickinson closed 2 months ago

bhelx commented 2 months ago

I think this is a reasonable approach. I'd like to go back eventually and add btoa support, etc. i'll add a ticket for it.

nilslice commented 2 months ago

curious if we can wrap these in the JS prelude to make the literal builtin atob work

EDIT: just saw @bhelx track https://github.com/extism/js-pdk/issues/95

If its just the matter of providing a wrapper around these new Rust functions in JS, should we do it now?

chrisdickinson commented 2 months ago

If its just the matter of providing a wrapper around these new Rust functions in JS, should we do it now?

Ah, it's not – atob and btoa are legacy web platform APIs (added in the first version, I think!) They're pretty inefficient – in order to work with them you end up having to copy buffer data into or out of a string first (so you end up with 2 copies of the buffer + 1 copy of the encoded buffer.) They also don't work super well with Unicode, so it's tricky to roundtrip through the APIs. Most JS platforms add some method for encoding to and decoding from base64 as a result.

nilslice commented 2 months ago

TIL! thank you