1 |
|
2 | import {inherits} from 'util';
|
3 | import {nextTick} from 'process';
|
4 | import {Readable} from './readable';
|
5 | import {Writable} from './writable';
|
6 |
|
7 | inherits(Duplex, Readable);
|
8 |
|
9 | var keys = Object.keys(Writable.prototype);
|
10 | for (var v = 0; v < keys.length; v++) {
|
11 | var method = keys[v];
|
12 | if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
|
13 | }
|
14 | export default Duplex;
|
15 | export function Duplex(options) {
|
16 | if (!(this instanceof Duplex)) return new Duplex(options);
|
17 |
|
18 | Readable.call(this, options);
|
19 | Writable.call(this, options);
|
20 |
|
21 | if (options && options.readable === false) this.readable = false;
|
22 |
|
23 | if (options && options.writable === false) this.writable = false;
|
24 |
|
25 | this.allowHalfOpen = true;
|
26 | if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
|
27 |
|
28 | this.once('end', onend);
|
29 | }
|
30 |
|
31 |
|
32 | function onend() {
|
33 |
|
34 |
|
35 | if (this.allowHalfOpen || this._writableState.ended) return;
|
36 |
|
37 |
|
38 |
|
39 | nextTick(onEndNT, this);
|
40 | }
|
41 |
|
42 | function onEndNT(self) {
|
43 | self.end();
|
44 | }
|