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.

feedme.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const stream_1 = require("stream");
  7. const xmlfeedparser_1 = __importDefault(require("./xmlfeedparser"));
  8. const jsonfeedparser_1 = __importDefault(require("./jsonfeedparser"));
  9. class FeedMe extends stream_1.Writable {
  10. /**
  11. * Creates an instance of a parser. Parser can be JSON/XML.
  12. *
  13. * @param {boolean} buffer If true, feed will be buffered into memory
  14. * and can be retrieved using `parser.done()` on the `end` event.
  15. * @constructor
  16. */
  17. constructor(buffer = false) {
  18. super();
  19. this._buffer = buffer;
  20. }
  21. _proxyEvents() {
  22. const parserEmit = this._parser.parser.emit;
  23. this._parser.parser.emit = (event, ...args) => {
  24. parserEmit.apply(this._parser.parser, [event, ...args]);
  25. if (event !== 'error') {
  26. return this.emit(event, ...args);
  27. }
  28. return true;
  29. };
  30. this._parser.parser.on('error', this.emit.bind(this, 'error'));
  31. }
  32. /**
  33. * @param {Buffer} data
  34. */
  35. _write(data, encoding, callback) {
  36. const str = data.toString();
  37. // First find out what type of feed this is.
  38. if (!this._parser) {
  39. if (/^\s*</.test(str)) {
  40. this._parser = new xmlfeedparser_1.default(this._buffer);
  41. this._proxyEvents();
  42. }
  43. else if (/^\s*[{[]/.test(str)) {
  44. this._parser = new jsonfeedparser_1.default(this._buffer);
  45. this.emit('type', 'json');
  46. this._proxyEvents();
  47. }
  48. else {
  49. callback(new Error('Not a correctly formatted feed'));
  50. return;
  51. }
  52. }
  53. this._parser.write(data, encoding, callback);
  54. }
  55. _final(callback) {
  56. if (this._parser) {
  57. this._parser.end(callback);
  58. }
  59. else {
  60. callback(null);
  61. }
  62. }
  63. done() {
  64. return this._parser.done();
  65. }
  66. }
  67. exports.default = FeedMe;
  68. module.exports = FeedMe;
  69. //# sourceMappingURL=feedme.js.map