Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can’t report Unicode strings anymore #5

Open
tonsky opened this issue Dec 15, 2015 · 5 comments
Open

Can’t report Unicode strings anymore #5

tonsky opened this issue Dec 15, 2015 · 5 comments

Comments

@tonsky
Copy link
Contributor

tonsky commented Dec 15, 2015

I understand it’s because of this commit: 93067d1

I’m positive I’ve reported unicode messages before. What is the motivation behind this change? Why is exception so cryptic (“Dom Error 5” instead of “Can’t report unicode” or something)? What’s the recommended way to convert UTF-8 to ASCII from this point on?

@tonsky
Copy link
Contributor Author

tonsky commented Dec 15, 2015

BTW I believe these lines:

* if (!window.btoa) window.btoa = yellerBase64.encode
* if (!window.atob) window.atob = yellerBase64.decode

Should probably be not in a comment, but in actual code (I think browser-provided atob/btoa are somewhat faster)

@tcrayford
Copy link
Contributor

@tonsky dangit. I'm very sorry this has caused you problems.

That commit was introduced because Yeller was seeing errors where some browsers were sending invalid JSON to Yeller's servers. yeller.js uses a very gnarly transport mechanism - it creates an Image with the json payload as a url attribute. This is the only way (as far as I could tell from my research) of doing cross domain https requests across browsers.

As such all the characters it sends have to be url safe. I'll dig into diagnosing and fixing this, but it's a somewhat nasty problem.

@tonsky
Copy link
Contributor Author

tonsky commented Dec 15, 2015

Hmm, interesting :) Sure, cross-domain requests are hard
JFYI, I patched it by replacing JSON.stringify with code from here: http://stackoverflow.com/a/4901205

function JSON_stringify(s, emit_unicode)
{
   var json = JSON.stringify(s);
   return emit_unicode ? json : json.replace(/[\u007f-\uffff]/g,
      function(c) { 
        return '\\u'+('0000'+c.charCodeAt(0).toString(16)).slice(-4);
      }
   );
}

Quick summary: JSON spec allows for escaping any character with \udddd sequence, which, if enabled, will emit ASCII-safe string for you. Sadly, browser’s JSON.stringify has no option to turn this on (JSON.parse understands it, but you probably don’t need it).

Not sure about its performance though.

@tcrayford
Copy link
Contributor

@tonsky that might work. Performance of the encoding isn't too important for JS clients, the blobs are relatively small most of the time, and it's on the user's side.

@tonsky
Copy link
Contributor Author

tonsky commented Feb 5, 2016

ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants