• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
23const common = require('../common');
24const assert = require('assert');
25const { Stream, PassThrough } = require('stream');
26
27{
28  const source = new Stream();
29  const dest = new Stream();
30
31  source.pipe(dest);
32
33  let gotErr = null;
34  source.on('error', function(err) {
35    gotErr = err;
36  });
37
38  const err = new Error('This stream turned into bacon.');
39  source.emit('error', err);
40  assert.strictEqual(gotErr, err);
41}
42
43{
44  const source = new Stream();
45  const dest = new Stream();
46
47  source.pipe(dest);
48
49  const err = new Error('This stream turned into bacon.');
50
51  let gotErr = null;
52  try {
53    source.emit('error', err);
54  } catch (e) {
55    gotErr = e;
56  }
57
58  assert.strictEqual(gotErr, err);
59}
60
61{
62  const R = Stream.Readable;
63  const W = Stream.Writable;
64
65  const r = new R({ autoDestroy: false });
66  const w = new W({ autoDestroy: false });
67  let removed = false;
68
69  r._read = common.mustCall(function() {
70    setTimeout(common.mustCall(function() {
71      assert(removed);
72      assert.throws(function() {
73        w.emit('error', new Error('fail'));
74      }, /^Error: fail$/);
75    }), 1);
76  });
77
78  w.on('error', myOnError);
79  r.pipe(w);
80  w.removeListener('error', myOnError);
81  removed = true;
82
83  function myOnError() {
84    throw new Error('this should not happen');
85  }
86}
87
88{
89  const R = Stream.Readable;
90  const W = Stream.Writable;
91
92  const r = new R();
93  const w = new W();
94  let removed = false;
95
96  r._read = common.mustCall(function() {
97    setTimeout(common.mustCall(function() {
98      assert(removed);
99      w.emit('error', new Error('fail'));
100    }), 1);
101  });
102
103  w.on('error', common.mustCall());
104  w._write = () => {};
105
106  r.pipe(w);
107  // Removing some OTHER random listener should not do anything
108  w.removeListener('error', () => {});
109  removed = true;
110}
111
112{
113  const _err = new Error('this should be handled');
114  const destination = new PassThrough();
115  destination.once('error', common.mustCall((err) => {
116    assert.strictEqual(err, _err);
117  }));
118
119  const stream = new Stream();
120  stream
121    .pipe(destination);
122
123  destination.destroy(_err);
124}
125