Initial commit
[yaffs-website] / node_modules / glob-stream / node_modules / readable-stream / lib / _stream_duplex.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 // a duplex stream is just a stream that is both readable and writable.
23 // Since JS doesn't have multiple prototypal inheritance, this class
24 // prototypally inherits from Readable, and then parasitically from
25 // Writable.
26
27 module.exports = Duplex;
28
29 /*<replacement>*/
30 var objectKeys = Object.keys || function (obj) {
31   var keys = [];
32   for (var key in obj) keys.push(key);
33   return keys;
34 }
35 /*</replacement>*/
36
37
38 /*<replacement>*/
39 var util = require('core-util-is');
40 util.inherits = require('inherits');
41 /*</replacement>*/
42
43 var Readable = require('./_stream_readable');
44 var Writable = require('./_stream_writable');
45
46 util.inherits(Duplex, Readable);
47
48 forEach(objectKeys(Writable.prototype), function(method) {
49   if (!Duplex.prototype[method])
50     Duplex.prototype[method] = Writable.prototype[method];
51 });
52
53 function Duplex(options) {
54   if (!(this instanceof Duplex))
55     return new Duplex(options);
56
57   Readable.call(this, options);
58   Writable.call(this, options);
59
60   if (options && options.readable === false)
61     this.readable = false;
62
63   if (options && options.writable === false)
64     this.writable = false;
65
66   this.allowHalfOpen = true;
67   if (options && options.allowHalfOpen === false)
68     this.allowHalfOpen = false;
69
70   this.once('end', onend);
71 }
72
73 // the no-half-open enforcer
74 function onend() {
75   // if we allow half-open state, or if the writable side ended,
76   // then we're ok.
77   if (this.allowHalfOpen || this._writableState.ended)
78     return;
79
80   // no more data can be written.
81   // But allow more writes to happen in this tick.
82   process.nextTick(this.end.bind(this));
83 }
84
85 function forEach (xs, f) {
86   for (var i = 0, l = xs.length; i < l; i++) {
87     f(xs[i], i);
88   }
89 }