UNPKG

1.18 kBJavaScriptView Raw
1
2import {inherits} from 'util';
3import {nextTick} from 'process';
4import {Readable} from './readable';
5import {Writable} from './writable';
6
7
8inherits(Duplex, Readable);
9
10var keys = Object.keys(Writable.prototype);
11for (var v = 0; v < keys.length; v++) {
12 var method = keys[v];
13 if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
14}
15export default Duplex;
16export function Duplex(options) {
17 if (!(this instanceof Duplex)) return new Duplex(options);
18
19 Readable.call(this, options);
20 Writable.call(this, options);
21
22 if (options && options.readable === false) this.readable = false;
23
24 if (options && options.writable === false) this.writable = false;
25
26 this.allowHalfOpen = true;
27 if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
28
29 this.once('end', onend);
30}
31
32// the no-half-open enforcer
33function onend() {
34 // if we allow half-open state, or if the writable side ended,
35 // then we're ok.
36 if (this.allowHalfOpen || this._writableState.ended) return;
37
38 // no more data can be written.
39 // But allow more writes to happen in this tick.
40 nextTick(onEndNT, this);
41}
42
43function onEndNT(self) {
44 self.end();
45}