[6a3a178] | 1 | var iconv,
|
---|
| 2 | inherits = require('util').inherits,
|
---|
| 3 | stream = require('stream');
|
---|
| 4 |
|
---|
| 5 | var regex = /(?:charset|encoding)\s*=\s*['"]? *([\w\-]+)/i;
|
---|
| 6 |
|
---|
| 7 | inherits(StreamDecoder, stream.Transform);
|
---|
| 8 |
|
---|
| 9 | function StreamDecoder(charset) {
|
---|
| 10 | if (!(this instanceof StreamDecoder))
|
---|
| 11 | return new StreamDecoder(charset);
|
---|
| 12 |
|
---|
| 13 | stream.Transform.call(this, charset);
|
---|
| 14 | this.charset = charset;
|
---|
| 15 | this.parsed_chunk = false;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | StreamDecoder.prototype._transform = function(chunk, encoding, done) {
|
---|
| 19 | var res, found;
|
---|
| 20 |
|
---|
| 21 | // try get charset from chunk, just once
|
---|
| 22 | if (this.charset == 'utf8' && !this.parsed_chunk) {
|
---|
| 23 | this.parsed_chunk = true;
|
---|
| 24 |
|
---|
| 25 | var matches = regex.exec(chunk.toString());
|
---|
| 26 | if (matches) {
|
---|
| 27 | found = matches[1].toLowerCase();
|
---|
| 28 | this.charset = found == 'utf-8' ? 'utf8' : found;
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | try {
|
---|
| 33 | res = iconv.decode(chunk, this.charset);
|
---|
| 34 | } catch(e) { // something went wrong, just return original chunk
|
---|
| 35 | res = chunk;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | this.push(res);
|
---|
| 39 | done();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | module.exports = function(charset) {
|
---|
| 43 | try {
|
---|
| 44 | if (!iconv) iconv = require('iconv-lite');
|
---|
| 45 | } catch(e) {
|
---|
| 46 | /* iconv not found */
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if (iconv)
|
---|
| 50 | return new StreamDecoder(charset);
|
---|
| 51 | else
|
---|
| 52 | return new stream.PassThrough;
|
---|
| 53 | }
|
---|