No Description

NodejsStreamOutputAdapter.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var Readable = require('readable-stream').Readable;
  3. var utils = require('../utils');
  4. utils.inherits(NodejsStreamOutputAdapter, Readable);
  5. /**
  6. * A nodejs stream using a worker as source.
  7. * @see the SourceWrapper in http://nodejs.org/api/stream.html
  8. * @constructor
  9. * @param {StreamHelper} helper the helper wrapping the worker
  10. * @param {Object} options the nodejs stream options
  11. * @param {Function} updateCb the update callback.
  12. */
  13. function NodejsStreamOutputAdapter(helper, options, updateCb) {
  14. Readable.call(this, options);
  15. this._helper = helper;
  16. var self = this;
  17. helper.on("data", function (data, meta) {
  18. if (!self.push(data)) {
  19. self._helper.pause();
  20. }
  21. if(updateCb) {
  22. updateCb(meta);
  23. }
  24. })
  25. .on("error", function(e) {
  26. self.emit('error', e);
  27. })
  28. .on("end", function () {
  29. self.push(null);
  30. });
  31. }
  32. NodejsStreamOutputAdapter.prototype._read = function() {
  33. this._helper.resume();
  34. };
  35. module.exports = NodejsStreamOutputAdapter;