Debian Codegraph @

polyfills/readable-stream/duplex.js

raw
1
2 import {inherits} from 'util';
3 import {nextTick} from 'process';
4 import {Readable} from './readable';
5 import {Writable} from './writable';
6
7
8 inherits(Duplex, Readable);
9
10 var keys = Object.keys(Writable.prototype);
11 for (var v = 0; v < keys.length; v++) {
12 var method = keys[v];
13 if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
14 }
15 export default Duplex;
16 export 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
33 function 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
43 function onEndNT(self) {
44 self.end();
45 }