summaryrefslogtreecommitdiff
path: root/cli/js/web/streams/readable-stream-default-controller.ts
blob: d33226a9bd14dcabf52460fe6bc28d6d0f7cc3b5 (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
// Forked from https://github.com/stardazed/sd-streams/tree/8928cf04b035fd02fb1340b7eb541c76be37e546
// Copyright (c) 2018-Present by Arthur Langereis - @zenmumbler MIT

/* eslint-disable @typescript-eslint/no-explicit-any */
// TODO reenable this lint here

import * as rs from "./readable-internals.ts";
import * as shared from "./shared-internals.ts";
import * as q from "./queue-mixin.ts";
import { Queue } from "./queue.ts";
import { QueuingStrategySizeCallback, UnderlyingSource } from "../dom_types.ts";

export class ReadableStreamDefaultController<OutputType>
  implements rs.SDReadableStreamDefaultController<OutputType> {
  [rs.cancelAlgorithm_]: rs.CancelAlgorithm;
  [rs.closeRequested_]: boolean;
  [rs.controlledReadableStream_]: rs.SDReadableStream<OutputType>;
  [rs.pullAgain_]: boolean;
  [rs.pullAlgorithm_]: rs.PullAlgorithm<OutputType>;
  [rs.pulling_]: boolean;
  [rs.strategyHWM_]: number;
  [rs.strategySizeAlgorithm_]: QueuingStrategySizeCallback<OutputType>;
  [rs.started_]: boolean;

  [q.queue_]: Queue<q.QueueElement<OutputType>>;
  [q.queueTotalSize_]: number;

  constructor() {
    throw new TypeError();
  }

  get desiredSize(): number | null {
    return rs.readableStreamDefaultControllerGetDesiredSize(this);
  }

  close(): void {
    if (!rs.isReadableStreamDefaultController(this)) {
      throw new TypeError();
    }
    if (!rs.readableStreamDefaultControllerCanCloseOrEnqueue(this)) {
      throw new TypeError(
        "Cannot close, the stream is already closing or not readable"
      );
    }
    rs.readableStreamDefaultControllerClose(this);
  }

  enqueue(chunk?: OutputType): void {
    if (!rs.isReadableStreamDefaultController(this)) {
      throw new TypeError();
    }
    if (!rs.readableStreamDefaultControllerCanCloseOrEnqueue(this)) {
      throw new TypeError(
        "Cannot enqueue, the stream is closing or not readable"
      );
    }
    rs.readableStreamDefaultControllerEnqueue(this, chunk!);
  }

  error(e?: shared.ErrorResult): void {
    if (!rs.isReadableStreamDefaultController(this)) {
      throw new TypeError();
    }
    rs.readableStreamDefaultControllerError(this, e);
  }

  [rs.cancelSteps_](reason: shared.ErrorResult): Promise<void> {
    q.resetQueue(this);
    const result = this[rs.cancelAlgorithm_](reason);
    rs.readableStreamDefaultControllerClearAlgorithms(this);
    return result;
  }

  [rs.pullSteps_](
    forAuthorCode: boolean
  ): Promise<IteratorResult<OutputType, any>> {
    const stream = this[rs.controlledReadableStream_];
    if (this[q.queue_].length > 0) {
      const chunk = q.dequeueValue(this);
      if (this[rs.closeRequested_] && this[q.queue_].length === 0) {
        rs.readableStreamDefaultControllerClearAlgorithms(this);
        rs.readableStreamClose(stream);
      } else {
        rs.readableStreamDefaultControllerCallPullIfNeeded(this);
      }
      return Promise.resolve(
        rs.readableStreamCreateReadResult(chunk, false, forAuthorCode)
      );
    }

    const pendingPromise = rs.readableStreamAddReadRequest(
      stream,
      forAuthorCode
    );
    rs.readableStreamDefaultControllerCallPullIfNeeded(this);
    return pendingPromise;
  }
}

export function setUpReadableStreamDefaultControllerFromUnderlyingSource<
  OutputType
>(
  stream: rs.SDReadableStream<OutputType>,
  underlyingSource: UnderlyingSource<OutputType>,
  highWaterMark: number,
  sizeAlgorithm: QueuingStrategySizeCallback<OutputType>
): void {
  // Assert: underlyingSource is not undefined.
  const controller = Object.create(ReadableStreamDefaultController.prototype);
  const startAlgorithm = (): any => {
    return shared.invokeOrNoop(underlyingSource, "start", [controller]);
  };
  const pullAlgorithm = shared.createAlgorithmFromUnderlyingMethod(
    underlyingSource,
    "pull",
    [controller]
  );
  const cancelAlgorithm = shared.createAlgorithmFromUnderlyingMethod(
    underlyingSource,
    "cancel",
    []
  );
  rs.setUpReadableStreamDefaultController(
    stream,
    controller,
    startAlgorithm,
    pullAlgorithm,
    cancelAlgorithm,
    highWaterMark,
    sizeAlgorithm
  );
}