kriskowal / q-io

Interfaces for IO using Q promises in JavaScript on Node
http://documentup.com/kriskowal/q-io
MIT License
317 stars 73 forks source link

Implements buffer decoding in HTTP.read() #141

Closed Tomalak closed 9 years ago

Tomalak commented 9 years ago

When HTTP responses arrive as buffers there is no safe way for the user of HTTP.read to determine the buffer encoding.

The read method however can use the Content-Type header to determine content encoding and convert the respose to string safely. This change makes read behave according to the API docs, which state that read() returns a string.

Introduces dependencies on iconv-lite and strip-bom.

Related issue: https://github.com/kriskowal/q-io/issues/139

Tomalak commented 9 years ago

I would trust the regex to find the encoding name, if it is there. If the header is malformed all bets are off anyway, a more rigorous parser won't make things better in such a case.

But you are right, passing it to iconv-lite unchecked is sloppy. I've also noticed that iconv-lite strips the BOM on its own, so the dependency on strip-bom is actually unnecessary. I'll brush up the implementation.