w3c / push-api

Push API
https://w3c.github.io/push-api/
Other
145 stars 40 forks source link

Support throwing exception from text method during UTF-8 decode #276

Closed stevenatkin closed 7 years ago

stevenatkin commented 7 years ago
  1. PushMessageData interface

https://www.w3.org/TR/push-api/#pushmessagedata-interface

You should add support for an exception to the text() method. The text() method converts the bytes to UTF-8 using UTF-8 decode. If the UTF-8 byte sequence is malformed, then the text() method should throw an exception.

beverloo commented 7 years ago

We depend on the UTF-8 decode operation defined in the Encoding spec. The default error mode for decoders is defined to be replacement instead of fatal, which means we shouldn't throw. Replacement characters will be inserted instead.

Is my reading correct, @annevk? That matches at least Chrome's implementation.

Would we want to explicitly override the error mode? I'm inclined not to, since our closest sibling method on Body doesn't either.

annevk commented 7 years ago

Yeah, we generally don't do that in the platform. Are the raw bytes exposed somewhere? If they are you can already get this kind of error handling if you want by using a TextDecoder object.

beverloo commented 7 years ago

Yes, they're exposed. The following would work:

self.addEventListener('push', event => {
  const decoder = new TextDecoder('utf-8', {
    fatal: true
  });
  try {
    decoder.decode(event.data.arrayBuffer());
  } catch (e) {
    /* do something with |e| */
  }
});

Let me close this based on prior art and the availability of a way to get the exceptions if necessary. Happy to reconsider if you disagree!