stream.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. 'use strict'
  22. /* replacement start */
  23. const { Buffer } = require('buffer')
  24. /* replacement end */
  25. const { ObjectDefineProperty, ObjectKeys, ReflectApply } = require('./ours/primordials')
  26. const {
  27. promisify: { custom: customPromisify }
  28. } = require('./ours/util')
  29. const { streamReturningOperators, promiseReturningOperators } = require('./internal/streams/operators')
  30. const {
  31. codes: { ERR_ILLEGAL_CONSTRUCTOR }
  32. } = require('./ours/errors')
  33. const compose = require('./internal/streams/compose')
  34. const { setDefaultHighWaterMark, getDefaultHighWaterMark } = require('./internal/streams/state')
  35. const { pipeline } = require('./internal/streams/pipeline')
  36. const { destroyer } = require('./internal/streams/destroy')
  37. const eos = require('./internal/streams/end-of-stream')
  38. const internalBuffer = {}
  39. const promises = require('./stream/promises')
  40. const utils = require('./internal/streams/utils')
  41. const Stream = (module.exports = require('./internal/streams/legacy').Stream)
  42. Stream.isDestroyed = utils.isDestroyed
  43. Stream.isDisturbed = utils.isDisturbed
  44. Stream.isErrored = utils.isErrored
  45. Stream.isReadable = utils.isReadable
  46. Stream.isWritable = utils.isWritable
  47. Stream.Readable = require('./internal/streams/readable')
  48. for (const key of ObjectKeys(streamReturningOperators)) {
  49. const op = streamReturningOperators[key]
  50. function fn(...args) {
  51. if (new.target) {
  52. throw ERR_ILLEGAL_CONSTRUCTOR()
  53. }
  54. return Stream.Readable.from(ReflectApply(op, this, args))
  55. }
  56. ObjectDefineProperty(fn, 'name', {
  57. __proto__: null,
  58. value: op.name
  59. })
  60. ObjectDefineProperty(fn, 'length', {
  61. __proto__: null,
  62. value: op.length
  63. })
  64. ObjectDefineProperty(Stream.Readable.prototype, key, {
  65. __proto__: null,
  66. value: fn,
  67. enumerable: false,
  68. configurable: true,
  69. writable: true
  70. })
  71. }
  72. for (const key of ObjectKeys(promiseReturningOperators)) {
  73. const op = promiseReturningOperators[key]
  74. function fn(...args) {
  75. if (new.target) {
  76. throw ERR_ILLEGAL_CONSTRUCTOR()
  77. }
  78. return ReflectApply(op, this, args)
  79. }
  80. ObjectDefineProperty(fn, 'name', {
  81. __proto__: null,
  82. value: op.name
  83. })
  84. ObjectDefineProperty(fn, 'length', {
  85. __proto__: null,
  86. value: op.length
  87. })
  88. ObjectDefineProperty(Stream.Readable.prototype, key, {
  89. __proto__: null,
  90. value: fn,
  91. enumerable: false,
  92. configurable: true,
  93. writable: true
  94. })
  95. }
  96. Stream.Writable = require('./internal/streams/writable')
  97. Stream.Duplex = require('./internal/streams/duplex')
  98. Stream.Transform = require('./internal/streams/transform')
  99. Stream.PassThrough = require('./internal/streams/passthrough')
  100. Stream.pipeline = pipeline
  101. const { addAbortSignal } = require('./internal/streams/add-abort-signal')
  102. Stream.addAbortSignal = addAbortSignal
  103. Stream.finished = eos
  104. Stream.destroy = destroyer
  105. Stream.compose = compose
  106. Stream.setDefaultHighWaterMark = setDefaultHighWaterMark
  107. Stream.getDefaultHighWaterMark = getDefaultHighWaterMark
  108. ObjectDefineProperty(Stream, 'promises', {
  109. __proto__: null,
  110. configurable: true,
  111. enumerable: true,
  112. get() {
  113. return promises
  114. }
  115. })
  116. ObjectDefineProperty(pipeline, customPromisify, {
  117. __proto__: null,
  118. enumerable: true,
  119. get() {
  120. return promises.pipeline
  121. }
  122. })
  123. ObjectDefineProperty(eos, customPromisify, {
  124. __proto__: null,
  125. enumerable: true,
  126. get() {
  127. return promises.finished
  128. }
  129. })
  130. // Backwards-compat with node 0.4.x
  131. Stream.Stream = Stream
  132. Stream._isUint8Array = function isUint8Array(value) {
  133. return value instanceof Uint8Array
  134. }
  135. Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) {
  136. return Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength)
  137. }