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