summaryrefslogtreecommitdiff
path: root/cli/js/web/streams/transform_stream_default_controller.ts
blob: 2fc8d2160081a328a5b913ff413264dc812e6b69 (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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import {
  FlushAlgorithm,
  isTransformStreamDefaultController,
  readableStreamDefaultControllerGetDesiredSize,
  setFunctionName,
  TransformAlgorithm,
  transformStreamDefaultControllerEnqueue,
  transformStreamDefaultControllerError,
  transformStreamDefaultControllerTerminate,
} from "./internals.ts";
import { ReadableStreamDefaultControllerImpl } from "./readable_stream_default_controller.ts";
import * as sym from "./symbols.ts";
import { TransformStreamImpl } from "./transform_stream.ts";
import { customInspect } from "../console.ts";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class TransformStreamDefaultControllerImpl<I = any, O = any>
  implements TransformStreamDefaultController<O> {
  [sym.controlledTransformStream]: TransformStreamImpl<I, O>;
  [sym.flushAlgorithm]: FlushAlgorithm;
  [sym.transformAlgorithm]: TransformAlgorithm<I>;

  private constructor() {
    throw new TypeError(
      "TransformStreamDefaultController's constructor cannot be called."
    );
  }

  get desiredSize(): number | null {
    if (!isTransformStreamDefaultController(this)) {
      throw new TypeError("Invalid TransformStreamDefaultController.");
    }
    const readableController = this[sym.controlledTransformStream][
      sym.readable
    ][sym.readableStreamController];
    return readableStreamDefaultControllerGetDesiredSize(
      readableController as ReadableStreamDefaultControllerImpl<O>
    );
  }

  enqueue(chunk: O): void {
    if (!isTransformStreamDefaultController(this)) {
      throw new TypeError("Invalid TransformStreamDefaultController.");
    }
    transformStreamDefaultControllerEnqueue(this, chunk);
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  error(reason?: any): void {
    if (!isTransformStreamDefaultController(this)) {
      throw new TypeError("Invalid TransformStreamDefaultController.");
    }
    transformStreamDefaultControllerError(this, reason);
  }

  terminate(): void {
    if (!isTransformStreamDefaultController(this)) {
      throw new TypeError("Invalid TransformStreamDefaultController.");
    }
    transformStreamDefaultControllerTerminate(this);
  }

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

setFunctionName(
  TransformStreamDefaultControllerImpl,
  "TransformStreamDefaultController"
);