event-message-unmarshaller-stream.js 975 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var Transform = require('stream').Transform;
  2. var parseEvent = require('./parse-event').parseEvent;
  3. /** @type {Transform} */
  4. function EventUnmarshallerStream(options) {
  5. options = options || {};
  6. // set output to object mode
  7. options.readableObjectMode = true;
  8. Transform.call(this, options);
  9. this._readableState.objectMode = true;
  10. this.parser = options.parser;
  11. this.eventStreamModel = options.eventStreamModel;
  12. }
  13. EventUnmarshallerStream.prototype = Object.create(Transform.prototype);
  14. /**
  15. *
  16. * @param {Buffer} chunk
  17. * @param {string} encoding
  18. * @param {*} callback
  19. */
  20. EventUnmarshallerStream.prototype._transform = function(chunk, encoding, callback) {
  21. try {
  22. var event = parseEvent(this.parser, chunk, this.eventStreamModel);
  23. this.push(event);
  24. return callback();
  25. } catch (err) {
  26. callback(err);
  27. }
  28. };
  29. /**
  30. * @api private
  31. */
  32. module.exports = {
  33. EventUnmarshallerStream: EventUnmarshallerStream
  34. };