decoder.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var iconv,
  2. inherits = require('util').inherits,
  3. stream = require('stream');
  4. var regex = /(?:charset|encoding)\s*=\s*['"]? *([\w\-]+)/i;
  5. inherits(StreamDecoder, stream.Transform);
  6. function StreamDecoder(charset) {
  7. if (!(this instanceof StreamDecoder))
  8. return new StreamDecoder(charset);
  9. stream.Transform.call(this, charset);
  10. this.charset = charset;
  11. this.parsed_chunk = false;
  12. }
  13. StreamDecoder.prototype._transform = function(chunk, encoding, done) {
  14. // try to get charset from chunk, but just once
  15. if (!this.parsed_chunk && (this.charset == 'utf-8' || this.charset == 'utf8')) {
  16. this.parsed_chunk = true;
  17. var matches = regex.exec(chunk.toString());
  18. if (matches) {
  19. var found = matches[1].toLowerCase().replace('utf8', 'utf-8'); // canonicalize;
  20. // set charset, but only if iconv can handle it
  21. if (iconv.encodingExists(found)) this.charset = found;
  22. }
  23. }
  24. if (this.charset == 'utf-8') { // no need to decode, just pass through
  25. this.push(chunk);
  26. return done();
  27. }
  28. // initialize stream decoder if not present
  29. var self = this;
  30. if (!this.decoder) {
  31. this.decoder = iconv.decodeStream(this.charset);
  32. this.decoder.on('data', function(decoded_chunk) {
  33. self.push(decoded_chunk);
  34. });
  35. };
  36. this.decoder.write(chunk);
  37. done();
  38. }
  39. module.exports = function(charset) {
  40. try {
  41. if (!iconv) iconv = require('iconv-lite');
  42. } catch(e) {
  43. /* iconv not found */
  44. }
  45. if (iconv)
  46. return new StreamDecoder(charset);
  47. else
  48. return new stream.PassThrough;
  49. }