• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* replacement start */
2
3const { Buffer } = require('buffer')
4
5/* replacement end */
6// Copyright Joyent, Inc. and other Node contributors.
7//
8// Permission is hereby granted, free of charge, to any person obtaining a
9// copy of this software and associated documentation files (the
10// "Software"), to deal in the Software without restriction, including
11// without limitation the rights to use, copy, modify, merge, publish,
12// distribute, sublicense, and/or sell copies of the Software, and to permit
13// persons to whom the Software is furnished to do so, subject to the
14// following conditions:
15//
16// The above copyright notice and this permission notice shall be included
17// in all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
22// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25// USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27;('use strict')
28const { ObjectDefineProperty, ObjectKeys, ReflectApply } = require('./ours/primordials')
29const {
30  promisify: { custom: customPromisify }
31} = require('./ours/util')
32const { streamReturningOperators, promiseReturningOperators } = require('./internal/streams/operators')
33const {
34  codes: { ERR_ILLEGAL_CONSTRUCTOR }
35} = require('./ours/errors')
36const compose = require('./internal/streams/compose')
37const { pipeline } = require('./internal/streams/pipeline')
38const { destroyer } = require('./internal/streams/destroy')
39const eos = require('./internal/streams/end-of-stream')
40const internalBuffer = {}
41const promises = require('./stream/promises')
42const utils = require('./internal/streams/utils')
43const Stream = (module.exports = require('./internal/streams/legacy').Stream)
44Stream.isDisturbed = utils.isDisturbed
45Stream.isErrored = utils.isErrored
46Stream.isReadable = utils.isReadable
47Stream.Readable = require('./internal/streams/readable')
48for (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}
72for (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}
96Stream.Writable = require('./internal/streams/writable')
97Stream.Duplex = require('./internal/streams/duplex')
98Stream.Transform = require('./internal/streams/transform')
99Stream.PassThrough = require('./internal/streams/passthrough')
100Stream.pipeline = pipeline
101const { addAbortSignal } = require('./internal/streams/add-abort-signal')
102Stream.addAbortSignal = addAbortSignal
103Stream.finished = eos
104Stream.destroy = destroyer
105Stream.compose = compose
106ObjectDefineProperty(Stream, 'promises', {
107  __proto__: null,
108  configurable: true,
109  enumerable: true,
110  get() {
111    return promises
112  }
113})
114ObjectDefineProperty(pipeline, customPromisify, {
115  __proto__: null,
116  enumerable: true,
117  get() {
118    return promises.pipeline
119  }
120})
121ObjectDefineProperty(eos, customPromisify, {
122  __proto__: null,
123  enumerable: true,
124  get() {
125    return promises.finished
126  }
127})
128
129// Backwards-compat with node 0.4.x
130Stream.Stream = Stream
131Stream._isUint8Array = function isUint8Array(value) {
132  return value instanceof Uint8Array
133}
134Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) {
135  return Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength)
136}
137