multipipe.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var assert = require('assert');
  2. var pipe = require('..');
  3. var Stream = require('stream');
  4. describe('pipe(a)', function(){
  5. it('should return a', function(){
  6. var readable = Readable();
  7. var stream = pipe(readable);
  8. assert.equal(stream, readable);
  9. });
  10. });
  11. describe('pipe(a, b, c)', function(){
  12. it('should pipe internally', function(done){
  13. pipe(Readable(), Transform(), Writable(done));
  14. });
  15. it('should be writable', function(done){
  16. var stream = pipe(Transform(), Writable(done));
  17. assert(stream.writable);
  18. Readable().pipe(stream);
  19. });
  20. it('should be readable', function(done){
  21. var stream = pipe(Readable(), Transform());
  22. assert(stream.readable);
  23. stream.pipe(Writable(done));
  24. });
  25. it('should be readable and writable', function(done){
  26. var stream = pipe(Transform(), Transform());
  27. assert(stream.readable);
  28. assert(stream.writable);
  29. Readable()
  30. .pipe(stream)
  31. .pipe(Writable(done));
  32. });
  33. describe('errors', function(){
  34. it('should reemit', function(done){
  35. var a = Transform();
  36. var b = Transform();
  37. var c = Transform();
  38. var stream = pipe(a, b, c);
  39. var err = new Error;
  40. var i = 0;
  41. stream.on('error', function(_err){
  42. i++;
  43. assert.equal(_err, err);
  44. assert(i <= 3);
  45. if (i == 3) done();
  46. });
  47. a.emit('error', err);
  48. b.emit('error', err);
  49. c.emit('error', err);
  50. });
  51. it('should not reemit endlessly', function(done){
  52. var a = Transform();
  53. var b = Transform();
  54. var c = Transform();
  55. c.readable = false;
  56. var stream = pipe(a, b, c);
  57. var err = new Error;
  58. var i = 0;
  59. stream.on('error', function(_err){
  60. i++;
  61. assert.equal(_err, err);
  62. assert(i <= 3);
  63. if (i == 3) done();
  64. });
  65. a.emit('error', err);
  66. b.emit('error', err);
  67. c.emit('error', err);
  68. });
  69. });
  70. });
  71. describe('pipe(a, b, c, fn)', function(){
  72. it('should call on finish', function(done){
  73. var finished = false;
  74. var a = Readable();
  75. var b = Transform();
  76. var c = Writable(function(){
  77. finished = true;
  78. });
  79. pipe(a, b, c, function(err){
  80. assert(!err);
  81. assert(finished);
  82. done();
  83. });
  84. });
  85. it('should call with error once', function(done){
  86. var a = Readable();
  87. var b = Transform();
  88. var c = Writable();
  89. var err = new Error;
  90. pipe(a, b, c, function(err){
  91. assert(err);
  92. done();
  93. });
  94. a.emit('error', err);
  95. b.emit('error', err);
  96. c.emit('error', err);
  97. });
  98. });
  99. function Readable(){
  100. var readable = new Stream.Readable({ objectMode: true });
  101. readable._read = function(){
  102. this.push('a');
  103. this.push(null);
  104. };
  105. return readable;
  106. }
  107. function Transform(){
  108. var transform = new Stream.Transform({ objectMode: true });
  109. transform._transform = function(chunk, _, done){
  110. done(null, chunk.toUpperCase());
  111. };
  112. return transform;
  113. }
  114. function Writable(cb){
  115. var writable = new Stream.Writable({ objectMode: true });
  116. writable._write = function(chunk, _, done){
  117. assert.equal(chunk, 'A');
  118. done();
  119. cb();
  120. };
  121. return writable;
  122. }