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.

streams.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var Buffer = require("safer-buffer").Buffer;
  3. // NOTE: Due to 'stream' module being pretty large (~100Kb, significant in browser environments),
  4. // we opt to dependency-inject it instead of creating a hard dependency.
  5. module.exports = function(stream_module) {
  6. var Transform = stream_module.Transform;
  7. // == Encoder stream =======================================================
  8. function IconvLiteEncoderStream(conv, options) {
  9. this.conv = conv;
  10. options = options || {};
  11. options.decodeStrings = false; // We accept only strings, so we don't need to decode them.
  12. Transform.call(this, options);
  13. }
  14. IconvLiteEncoderStream.prototype = Object.create(Transform.prototype, {
  15. constructor: { value: IconvLiteEncoderStream }
  16. });
  17. IconvLiteEncoderStream.prototype._transform = function(chunk, encoding, done) {
  18. if (typeof chunk != 'string')
  19. return done(new Error("Iconv encoding stream needs strings as its input."));
  20. try {
  21. var res = this.conv.write(chunk);
  22. if (res && res.length) this.push(res);
  23. done();
  24. }
  25. catch (e) {
  26. done(e);
  27. }
  28. }
  29. IconvLiteEncoderStream.prototype._flush = function(done) {
  30. try {
  31. var res = this.conv.end();
  32. if (res && res.length) this.push(res);
  33. done();
  34. }
  35. catch (e) {
  36. done(e);
  37. }
  38. }
  39. IconvLiteEncoderStream.prototype.collect = function(cb) {
  40. var chunks = [];
  41. this.on('error', cb);
  42. this.on('data', function(chunk) { chunks.push(chunk); });
  43. this.on('end', function() {
  44. cb(null, Buffer.concat(chunks));
  45. });
  46. return this;
  47. }
  48. // == Decoder stream =======================================================
  49. function IconvLiteDecoderStream(conv, options) {
  50. this.conv = conv;
  51. options = options || {};
  52. options.encoding = this.encoding = 'utf8'; // We output strings.
  53. Transform.call(this, options);
  54. }
  55. IconvLiteDecoderStream.prototype = Object.create(Transform.prototype, {
  56. constructor: { value: IconvLiteDecoderStream }
  57. });
  58. IconvLiteDecoderStream.prototype._transform = function(chunk, encoding, done) {
  59. if (!Buffer.isBuffer(chunk) && !(chunk instanceof Uint8Array))
  60. return done(new Error("Iconv decoding stream needs buffers as its input."));
  61. try {
  62. var res = this.conv.write(chunk);
  63. if (res && res.length) this.push(res, this.encoding);
  64. done();
  65. }
  66. catch (e) {
  67. done(e);
  68. }
  69. }
  70. IconvLiteDecoderStream.prototype._flush = function(done) {
  71. try {
  72. var res = this.conv.end();
  73. if (res && res.length) this.push(res, this.encoding);
  74. done();
  75. }
  76. catch (e) {
  77. done(e);
  78. }
  79. }
  80. IconvLiteDecoderStream.prototype.collect = function(cb) {
  81. var res = '';
  82. this.on('error', cb);
  83. this.on('data', function(chunk) { res += chunk; });
  84. this.on('end', function() {
  85. cb(null, res);
  86. });
  87. return this;
  88. }
  89. return {
  90. IconvLiteEncoderStream: IconvLiteEncoderStream,
  91. IconvLiteDecoderStream: IconvLiteDecoderStream,
  92. };
  93. };