duplex.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // a duplex stream is just a stream that is both readable and writable.
  22. // Since JS doesn't have multiple prototype inheritance, this class
  23. // prototypically inherits from Readable, and then parasitically from
  24. // Writable.
  25. 'use strict'
  26. const {
  27. ObjectDefineProperties,
  28. ObjectGetOwnPropertyDescriptor,
  29. ObjectKeys,
  30. ObjectSetPrototypeOf
  31. } = require('../../ours/primordials')
  32. module.exports = Duplex
  33. const Readable = require('./readable')
  34. const Writable = require('./writable')
  35. ObjectSetPrototypeOf(Duplex.prototype, Readable.prototype)
  36. ObjectSetPrototypeOf(Duplex, Readable)
  37. {
  38. const keys = ObjectKeys(Writable.prototype)
  39. // Allow the keys array to be GC'ed.
  40. for (let i = 0; i < keys.length; i++) {
  41. const method = keys[i]
  42. if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]
  43. }
  44. }
  45. function Duplex(options) {
  46. if (!(this instanceof Duplex)) return new Duplex(options)
  47. Readable.call(this, options)
  48. Writable.call(this, options)
  49. if (options) {
  50. this.allowHalfOpen = options.allowHalfOpen !== false
  51. if (options.readable === false) {
  52. this._readableState.readable = false
  53. this._readableState.ended = true
  54. this._readableState.endEmitted = true
  55. }
  56. if (options.writable === false) {
  57. this._writableState.writable = false
  58. this._writableState.ending = true
  59. this._writableState.ended = true
  60. this._writableState.finished = true
  61. }
  62. } else {
  63. this.allowHalfOpen = true
  64. }
  65. }
  66. ObjectDefineProperties(Duplex.prototype, {
  67. writable: {
  68. __proto__: null,
  69. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writable')
  70. },
  71. writableHighWaterMark: {
  72. __proto__: null,
  73. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableHighWaterMark')
  74. },
  75. writableObjectMode: {
  76. __proto__: null,
  77. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableObjectMode')
  78. },
  79. writableBuffer: {
  80. __proto__: null,
  81. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableBuffer')
  82. },
  83. writableLength: {
  84. __proto__: null,
  85. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableLength')
  86. },
  87. writableFinished: {
  88. __proto__: null,
  89. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableFinished')
  90. },
  91. writableCorked: {
  92. __proto__: null,
  93. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableCorked')
  94. },
  95. writableEnded: {
  96. __proto__: null,
  97. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableEnded')
  98. },
  99. writableNeedDrain: {
  100. __proto__: null,
  101. ...ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writableNeedDrain')
  102. },
  103. destroyed: {
  104. __proto__: null,
  105. get() {
  106. if (this._readableState === undefined || this._writableState === undefined) {
  107. return false
  108. }
  109. return this._readableState.destroyed && this._writableState.destroyed
  110. },
  111. set(value) {
  112. // Backward compatibility, the user is explicitly
  113. // managing destroyed.
  114. if (this._readableState && this._writableState) {
  115. this._readableState.destroyed = value
  116. this._writableState.destroyed = value
  117. }
  118. }
  119. }
  120. })
  121. let webStreamsAdapters
  122. // Lazy to avoid circular references
  123. function lazyWebStreams() {
  124. if (webStreamsAdapters === undefined) webStreamsAdapters = {}
  125. return webStreamsAdapters
  126. }
  127. Duplex.fromWeb = function (pair, options) {
  128. return lazyWebStreams().newStreamDuplexFromReadableWritablePair(pair, options)
  129. }
  130. Duplex.toWeb = function (duplex) {
  131. return lazyWebStreams().newReadableWritablePairFromDuplex(duplex)
  132. }
  133. let duplexify
  134. Duplex.from = function (body) {
  135. if (!duplexify) {
  136. duplexify = require('./duplexify')
  137. }
  138. return duplexify(body, 'body')
  139. }