Keine Beschreibung

index.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var stream = require("readable-stream");
  2. var duplex2 = module.exports = function duplex2(options, writable, readable) {
  3. return new DuplexWrapper(options, writable, readable);
  4. };
  5. var DuplexWrapper = exports.DuplexWrapper = function DuplexWrapper(options, writable, readable) {
  6. if (typeof readable === "undefined") {
  7. readable = writable;
  8. writable = options;
  9. options = null;
  10. }
  11. options = options || {};
  12. options.objectMode = true;
  13. stream.Duplex.call(this, options);
  14. this._bubbleErrors = (typeof options.bubbleErrors === "undefined") || !!options.bubbleErrors;
  15. this._writable = writable;
  16. this._readable = readable;
  17. var self = this;
  18. writable.once("finish", function() {
  19. self.end();
  20. });
  21. this.once("finish", function() {
  22. writable.end();
  23. });
  24. readable.on("data", function(e) {
  25. if (!self.push(e)) {
  26. readable.pause();
  27. }
  28. });
  29. readable.once("end", function() {
  30. return self.push(null);
  31. });
  32. if (this._bubbleErrors) {
  33. writable.on("error", function(err) {
  34. return self.emit("error", err);
  35. });
  36. readable.on("error", function(err) {
  37. return self.emit("error", err);
  38. });
  39. }
  40. };
  41. DuplexWrapper.prototype = Object.create(stream.Duplex.prototype, {constructor: {value: DuplexWrapper}});
  42. DuplexWrapper.prototype._write = function _write(input, encoding, done) {
  43. this._writable.write(input, encoding, done);
  44. };
  45. DuplexWrapper.prototype._read = function _read(n) {
  46. this._readable.resume();
  47. };