[whatwg] BinaryEncoding for Typed Arrays using window.btoa and window.atob
Glenn Maynard
glenn at zewt.org
Mon Aug 12 16:50:36 PDT 2013
On Mon, Aug 12, 2013 at 12:16 PM, Joshua Bell <jsbell at google.com> wrote:
> To recap history: early iterations of the Encoding API proposal did have
> base64 but it was removed with the suggestion to extend atob()/btoa()
> instead, and due to the confusion around the encode/decode verbs. If the
> APIs were something like StringToBytesConverter::convert() and
> BytesToStringConverter::convert() it would make more sense for encoding of
> both text (use StringToBytes) and binary data (use BytesToString).
>
I thought about suggesting something like "StringToBytes", but that seems
less obvious for the (probably) more common usage of encoding/decoding a
String, and it's still a bit off (though not *strictly* wrong) for
converting to UTF-16, UTF-32, etc. I tend to think the slightly
unintuitive names of TextEncoder and TextDecoder aren't bad enough that
it's worth renaming them.
> While we're re-opening this can of worms, there's been a request to add a
> flush() method to the TextEncoder/TextDecoder objects, which would behave
> the same as calling encode(null, {stream: false}) / decode(null,
> {stream:false}) but make the code more readable. This fails the "adding a
> new method for something that behaves exactly like something we already
> have" test. Opinions?
>
I think you only need to say encode() and decode(), which is less of a win,
especially since creating two ways of doing the same thing means that
people have to learn both ways. Otherwise, they'll see code end with
".encode()" and not realize that it's the same as the ".finish()" they've
been using.
On Mon, Aug 12, 2013 at 6:26 PM, Jonas Sicking <jonas at sicking.cc> wrote:
> I don't think that base64 encoding fits with the current
> TextEncoder/Decoder API. Not because of names, but because base64
> encoding is by nature opposite. I.e. the encoded format is in string
> form, whereas the decoded format is in binary form.
>
The names are the only things that are opposite. TextEncoder is just a
streaming String-to-binary-blob conversion API, and TextDecoder is just a
streaming binary-blob-to-String API, and that's precisely what base64
encoding and decoding are. That's the same whether you're converting
String-to-base64 or String-to-UTF-8. The only difference is that the names
we've given to those ideas are reversed here.
One thing that might need special attention is that U+FFFD error handling
doesn't make sense for base64; errors should probably always be fatal.
--
Glenn Maynard
More information about the whatwg
mailing list