driverdan/node-XMLHttpRequest — Issue #24: support binary data via overrideMimeType() (or other means of not assuming the returned data is utf8)
Author: asutherland
Created: 2012-03-06 06:39:15+00:00
State: open

https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data_in_older_browsers documents using:

req.overrideMimeType('text/plain; charset=x-user-defined');

as a way to force web browsers to not treat the returned data as utf-8. This is useful if you are doing something like trying to fetch an image in web browsers and also would like that same code to work in node.

Currently, this library forces the data stream to be interpreted as utf-8 which is potentially lossy, and definitely lossy in the case of png images. Specifically, the first byte 0x89 when round-tripped through utf-8 gets converted into the replacement character 0xffd and comes back (utf-8 encoded) as 0xef 0xbf 0xbd.