1'use strict';
2
3const common = require('../common');
4const { Transform } = require('stream');
5const assert = require('assert');
6
7{
8  const transform = new Transform({
9    transform(chunk, enc, cb) {}
10  });
11
12  transform.resume();
13
14  transform.on('end', common.mustNotCall());
15  transform.on('close', common.mustCall());
16  transform.on('finish', common.mustNotCall());
17
18  transform.destroy();
19}
20
21{
22  const transform = new Transform({
23    transform(chunk, enc, cb) {}
24  });
25  transform.resume();
26
27  const expected = new Error('kaboom');
28
29  transform.on('end', common.mustNotCall());
30  transform.on('finish', common.mustNotCall());
31  transform.on('close', common.mustCall());
32  transform.on('error', common.mustCall((err) => {
33    assert.strictEqual(err, expected);
34  }));
35
36  transform.destroy(expected);
37}
38
39{
40  const transform = new Transform({
41    transform(chunk, enc, cb) {}
42  });
43
44  transform._destroy = common.mustCall(function(err, cb) {
45    assert.strictEqual(err, expected);
46    cb(err);
47  }, 1);
48
49  const expected = new Error('kaboom');
50
51  transform.on('finish', common.mustNotCall('no finish event'));
52  transform.on('close', common.mustCall());
53  transform.on('error', common.mustCall((err) => {
54    assert.strictEqual(err, expected);
55  }));
56
57  transform.destroy(expected);
58}
59
60{
61  const expected = new Error('kaboom');
62  const transform = new Transform({
63    transform(chunk, enc, cb) {},
64    destroy: common.mustCall(function(err, cb) {
65      assert.strictEqual(err, expected);
66      cb();
67    }, 1)
68  });
69  transform.resume();
70
71  transform.on('end', common.mustNotCall('no end event'));
72  transform.on('close', common.mustCall());
73  transform.on('finish', common.mustNotCall('no finish event'));
74
75  // Error is swallowed by the custom _destroy
76  transform.on('error', common.mustNotCall('no error event'));
77
78  transform.destroy(expected);
79}
80
81{
82  const transform = new Transform({
83    transform(chunk, enc, cb) {}
84  });
85
86  transform._destroy = common.mustCall(function(err, cb) {
87    assert.strictEqual(err, null);
88    cb();
89  }, 1);
90
91  transform.destroy();
92}
93
94{
95  const transform = new Transform({
96    transform(chunk, enc, cb) {}
97  });
98  transform.resume();
99
100  transform._destroy = common.mustCall(function(err, cb) {
101    assert.strictEqual(err, null);
102    process.nextTick(() => {
103      this.push(null);
104      this.end();
105      cb();
106    });
107  }, 1);
108
109  const fail = common.mustNotCall('no event');
110
111  transform.on('finish', fail);
112  transform.on('end', fail);
113  transform.on('close', common.mustCall());
114
115  transform.destroy();
116
117  transform.removeListener('end', fail);
118  transform.removeListener('finish', fail);
119  transform.on('end', common.mustCall());
120  transform.on('finish', common.mustNotCall());
121}
122
123{
124  const transform = new Transform({
125    transform(chunk, enc, cb) {}
126  });
127
128  const expected = new Error('kaboom');
129
130  transform._destroy = common.mustCall(function(err, cb) {
131    assert.strictEqual(err, null);
132    cb(expected);
133  }, 1);
134
135  transform.on('close', common.mustCall());
136  transform.on('finish', common.mustNotCall('no finish event'));
137  transform.on('end', common.mustNotCall('no end event'));
138  transform.on('error', common.mustCall((err) => {
139    assert.strictEqual(err, expected);
140  }));
141
142  transform.destroy();
143}
144