1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25const stream = require('stream');
26
27{
28  let count = 1000;
29
30  const source = new stream.Readable();
31  source._read = function(n) {
32    n = Math.min(count, n);
33    count -= n;
34    source.push(Buffer.allocUnsafe(n));
35  };
36
37  let unpipedDest;
38  source.unpipe = function(dest) {
39    unpipedDest = dest;
40    stream.Readable.prototype.unpipe.call(this, dest);
41  };
42
43  const dest = new stream.Writable();
44  dest._write = function(chunk, encoding, cb) {
45    cb();
46  };
47
48  source.pipe(dest);
49
50  let gotErr = null;
51  dest.on('error', function(err) {
52    gotErr = err;
53  });
54
55  let unpipedSource;
56  dest.on('unpipe', function(src) {
57    unpipedSource = src;
58  });
59
60  const err = new Error('This stream turned into bacon.');
61  dest.emit('error', err);
62  assert.strictEqual(gotErr, err);
63  assert.strictEqual(unpipedSource, source);
64  assert.strictEqual(unpipedDest, dest);
65}
66
67{
68  let count = 1000;
69
70  const source = new stream.Readable();
71  source._read = function(n) {
72    n = Math.min(count, n);
73    count -= n;
74    source.push(Buffer.allocUnsafe(n));
75  };
76
77  let unpipedDest;
78  source.unpipe = function(dest) {
79    unpipedDest = dest;
80    stream.Readable.prototype.unpipe.call(this, dest);
81  };
82
83  const dest = new stream.Writable({ autoDestroy: false });
84  dest._write = function(chunk, encoding, cb) {
85    cb();
86  };
87
88  source.pipe(dest);
89
90  let unpipedSource;
91  dest.on('unpipe', function(src) {
92    unpipedSource = src;
93  });
94
95  const err = new Error('This stream turned into bacon.');
96
97  let gotErr = null;
98  try {
99    dest.emit('error', err);
100  } catch (e) {
101    gotErr = e;
102  }
103  assert.strictEqual(gotErr, err);
104  assert.strictEqual(unpipedSource, source);
105  assert.strictEqual(unpipedDest, dest);
106}
107