1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const stream = require('stream');
|
17 |
|
18 | class PassThroughDuplex extends stream.Duplex {
|
19 | constructor(options) {
|
20 | super(options);
|
21 |
|
22 | this.destinations = [];
|
23 |
|
24 | this.passThroughReadable = new stream.PassThrough();
|
25 | this.passThroughReadable.on('error', function(error) {
|
26 | this.emit(error);
|
27 | });
|
28 |
|
29 | this.passThroughWritable = new stream.PassThrough();
|
30 | this.passThroughWritable.on('error', function(error) {
|
31 | this.emit(error);
|
32 | });
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | pipe(destination, options) {
|
38 | this.destinations.push(destination);
|
39 | return this.passThroughReadable.pipe(destination, options);
|
40 | }
|
41 |
|
42 | read(size) {
|
43 | return this.passThroughReadable.read(size);
|
44 | }
|
45 |
|
46 | setEncoding(encoding) {
|
47 | return this.passThroughReadable.setEncoding(encoding);
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 | end(chunk, encoding, callback) {
|
53 | return this.passThroughWritable.end(chunk, encoding, callback);
|
54 | }
|
55 |
|
56 | write(chunk, encoding, callback) {
|
57 | return this.passThroughWritable.write(chunk, encoding, callback);
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | destroy(error) {
|
63 | this.passThroughWritable.destroy(error);
|
64 | this.passThroughReadable.destroy(error);
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | on(event, listener) {
|
70 | if (!this.passThroughWritable) {
|
71 | return super.on(event, listener);
|
72 | }
|
73 |
|
74 | switch (event) {
|
75 | case 'drain':
|
76 | case 'finish':
|
77 | return this.passThroughWritable.on(event, listener);
|
78 | default:
|
79 | return super.on(event, listener);
|
80 | }
|
81 | }
|
82 | }
|
83 |
|
84 | PassThroughDuplex.addListener = PassThroughDuplex.on;
|
85 |
|
86 | module.exports = PassThroughDuplex;
|