Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decoder.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. var res, found;
  15. // try get charset from chunk, just once
  16. if (this.charset == 'utf8' && !this.parsed_chunk) {
  17. this.parsed_chunk = true;
  18. var matches = regex.exec(chunk.toString());
  19. if (matches) {
  20. found = matches[1].toLowerCase();
  21. this.charset = found == 'utf-8' ? 'utf8' : found;
  22. }
  23. }
  24. try {
  25. res = iconv.decode(chunk, this.charset);
  26. } catch(e) { // something went wrong, just return original chunk
  27. res = chunk;
  28. }
  29. this.push(res);
  30. done();
  31. }
  32. module.exports = function(charset) {
  33. try {
  34. if (!iconv) iconv = require('iconv-lite');
  35. } catch(e) {
  36. /* iconv not found */
  37. }
  38. if (iconv)
  39. return new StreamDecoder(charset);
  40. else
  41. return new stream.PassThrough;
  42. }