summaryrefslogtreecommitdiff
path: root/cli/js/web/streams/readable_stream.ts
blob: 27a733d9daa630f82cb3e4ebb28b60e9003913ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import {
  acquireReadableStreamDefaultReader,
  initializeReadableStream,
  isReadableStream,
  isReadableStreamLocked,
  isUnderlyingByteSource,
  isWritableStream,
  isWritableStreamLocked,
  makeSizeAlgorithmFromSizeFunction,
  setFunctionName,
  setPromiseIsHandledToTrue,
  readableStreamCancel,
  ReadableStreamGenericReader,
  readableStreamPipeTo,
  readableStreamTee,
  setUpReadableByteStreamControllerFromUnderlyingSource,
  setUpReadableStreamDefaultControllerFromUnderlyingSource,
  validateAndNormalizeHighWaterMark,
} from "./internals.ts";
import { ReadableByteStreamControllerImpl } from "./readable_byte_stream_controller.ts";
import { ReadableStreamAsyncIteratorPrototype } from "./readable_stream_async_iterator.ts";
import { ReadableStreamDefaultControllerImpl } from "./readable_stream_default_controller.ts";
import * as sym from "./symbols.ts";
import { customInspect } from "../console.ts";
import { AbortSignalImpl } from "../abort_signal.ts";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class ReadableStreamImpl<R = any> implements ReadableStream<R> {
  [sym.disturbed]: boolean;
  [sym.readableStreamController]:
    | ReadableStreamDefaultControllerImpl<R>
    | ReadableByteStreamControllerImpl;
  [sym.reader]: ReadableStreamGenericReader<R> | undefined;
  [sym.state]: "readable" | "closed" | "errored";
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  [sym.storedError]: any;

  constructor(
    underlyingSource: UnderlyingByteSource | UnderlyingSource<R> = {},
    strategy:
      | {
          highWaterMark?: number;
          size?: undefined;
        }
      | QueuingStrategy<R> = {}
  ) {
    initializeReadableStream(this);
    const { size } = strategy;
    let { highWaterMark } = strategy;
    const { type } = underlyingSource;

    if (isUnderlyingByteSource(underlyingSource)) {
      if (size !== undefined) {
        throw new RangeError(
          `When underlying source is "bytes", strategy.size must be undefined.`
        );
      }
      highWaterMark = validateAndNormalizeHighWaterMark(highWaterMark ?? 0);
      setUpReadableByteStreamControllerFromUnderlyingSource(
        this,
        underlyingSource,
        highWaterMark
      );
    } else if (type === undefined) {
      const sizeAlgorithm = makeSizeAlgorithmFromSizeFunction(size);
      highWaterMark = validateAndNormalizeHighWaterMark(highWaterMark ?? 1);
      setUpReadableStreamDefaultControllerFromUnderlyingSource(
        this,
        underlyingSource,
        highWaterMark,
        sizeAlgorithm
      );
    } else {
      throw new RangeError(
        `Valid values for underlyingSource are "bytes" or undefined.  Received: "${type}".`
      );
    }
  }

  get locked(): boolean {
    if (!isReadableStream(this)) {
      throw new TypeError("Invalid ReadableStream.");
    }
    return isReadableStreamLocked(this);
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  cancel(reason?: any): Promise<void> {
    if (!isReadableStream(this)) {
      return Promise.reject(new TypeError("Invalid ReadableStream."));
    }
    if (isReadableStreamLocked(this)) {
      return Promise.reject(
        new TypeError("Cannot cancel a locked ReadableStream.")
      );
    }
    return readableStreamCancel(this, reason);
  }

  getIterator({
    preventCancel,
  }: { preventCancel?: boolean } = {}): AsyncIterableIterator<R> {
    if (!isReadableStream(this)) {
      throw new TypeError("Invalid ReadableStream.");
    }
    const reader = acquireReadableStreamDefaultReader(this);
    const iterator = Object.create(ReadableStreamAsyncIteratorPrototype);
    iterator[sym.asyncIteratorReader] = reader;
    iterator[sym.preventCancel] = Boolean(preventCancel);
    return iterator;
  }

  getReader({ mode }: { mode?: string } = {}): ReadableStreamDefaultReader<R> {
    if (!isReadableStream(this)) {
      throw new TypeError("Invalid ReadableStream.");
    }
    if (mode === undefined) {
      return acquireReadableStreamDefaultReader(this, true);
    }
    mode = String(mode);
    // 3.2.5.4.4 If mode is "byob", return ? AcquireReadableStreamBYOBReader(this, true).
    throw new RangeError(`Unsupported mode "${mode}"`);
  }

  pipeThrough<T>(
    {
      writable,
      readable,
    }: {
      writable: WritableStream<R>;
      readable: ReadableStream<T>;
    },
    { preventClose, preventAbort, preventCancel, signal }: PipeOptions = {}
  ): ReadableStream<T> {
    if (!isReadableStream(this)) {
      throw new TypeError("Invalid ReadableStream.");
    }
    if (!isWritableStream(writable)) {
      throw new TypeError("writable is not a valid WritableStream.");
    }
    if (!isReadableStream(readable)) {
      throw new TypeError("readable is not a valid ReadableStream.");
    }
    preventClose = Boolean(preventClose);
    preventAbort = Boolean(preventAbort);
    preventCancel = Boolean(preventCancel);
    if (signal && !(signal instanceof AbortSignalImpl)) {
      throw new TypeError("Invalid signal.");
    }
    if (isReadableStreamLocked(this)) {
      throw new TypeError("ReadableStream is locked.");
    }
    if (isWritableStreamLocked(writable)) {
      throw new TypeError("writable is locked.");
    }
    const promise = readableStreamPipeTo(
      this,
      writable,
      preventClose,
      preventAbort,
      preventCancel,
      signal
    );
    setPromiseIsHandledToTrue(promise);
    return readable;
  }

  pipeTo(
    dest: WritableStream<R>,
    { preventClose, preventAbort, preventCancel, signal }: PipeOptions = {}
  ): Promise<void> {
    if (!isReadableStream(this)) {
      return Promise.reject(new TypeError("Invalid ReadableStream."));
    }
    if (!isWritableStream(dest)) {
      return Promise.reject(
        new TypeError("dest is not a valid WritableStream.")
      );
    }
    preventClose = Boolean(preventClose);
    preventAbort = Boolean(preventAbort);
    preventCancel = Boolean(preventCancel);
    if (signal && !(signal instanceof AbortSignalImpl)) {
      return Promise.reject(new TypeError("Invalid signal."));
    }
    if (isReadableStreamLocked(this)) {
      return Promise.reject(new TypeError("ReadableStream is locked."));
    }
    if (isWritableStreamLocked(dest)) {
      return Promise.reject(new TypeError("dest is locked."));
    }
    return readableStreamPipeTo(
      this,
      dest,
      preventClose,
      preventAbort,
      preventCancel,
      signal
    );
  }

  tee(): [ReadableStreamImpl<R>, ReadableStreamImpl<R>] {
    if (!isReadableStream(this)) {
      throw new TypeError("Invalid ReadableStream.");
    }
    return readableStreamTee(this, false);
  }

  [customInspect](): string {
    return `${this.constructor.name} { locked: ${String(this.locked)} }`;
  }

  [Symbol.asyncIterator](
    options: {
      preventCancel?: boolean;
    } = {}
  ): AsyncIterableIterator<R> {
    return this.getIterator(options);
  }
}

setFunctionName(ReadableStreamImpl, "ReadableStream");