summaryrefslogtreecommitdiff
path: root/std/node/_stream/transform.ts
blob: a4246e81a065d7c97a5c22ff69cd265ae95c8beb (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
// Copyright Node.js contributors. All rights reserved. MIT License.
import { Encodings } from "../_utils.ts";
import Duplex from "./duplex.ts";
import type { DuplexOptions } from "./duplex.ts";
import type { writeV } from "./writable_internal.ts";
import { ERR_METHOD_NOT_IMPLEMENTED } from "../_errors.ts";

const kCallback = Symbol("kCallback");

type TransformFlush = (
  this: Transform,
  // deno-lint-ignore no-explicit-any
  callback: (error?: Error | null, data?: any) => void,
) => void;

export interface TransformOptions extends DuplexOptions {
  read?(this: Transform, size: number): void;
  write?(
    this: Transform,
    // deno-lint-ignore no-explicit-any
    chunk: any,
    encoding: Encodings,
    callback: (error?: Error | null) => void,
  ): void;
  writev?: writeV;
  final?(this: Transform, callback: (error?: Error | null) => void): void;
  destroy?(
    this: Transform,
    error: Error | null,
    callback: (error: Error | null) => void,
  ): void;
  transform?(
    this: Transform,
    // deno-lint-ignore no-explicit-any
    chunk: any,
    encoding: Encodings,
    // deno-lint-ignore no-explicit-any
    callback: (error?: Error | null, data?: any) => void,
  ): void;
  flush?: TransformFlush;
}

export default class Transform extends Duplex {
  [kCallback]: null | ((error?: Error | null) => void);
  _flush?: TransformFlush;

  constructor(options?: TransformOptions) {
    super(options);
    this._readableState.sync = false;

    this[kCallback] = null;

    if (options) {
      if (typeof options.transform === "function") {
        this._transform = options.transform;
      }

      if (typeof options.flush === "function") {
        this._flush = options.flush;
      }
    }

    this.on("prefinish", function (this: Transform) {
      if (typeof this._flush === "function" && !this.destroyed) {
        this._flush((er, data) => {
          if (er) {
            this.destroy(er);
            return;
          }

          if (data != null) {
            this.push(data);
          }
          this.push(null);
        });
      } else {
        this.push(null);
      }
    });
  }

  _read = () => {
    if (this[kCallback]) {
      const callback = this[kCallback] as (error?: Error | null) => void;
      this[kCallback] = null;
      callback();
    }
  };

  _transform(
    // deno-lint-ignore no-explicit-any
    _chunk: any,
    _encoding: string,
    // deno-lint-ignore no-explicit-any
    _callback: (error?: Error | null, data?: any) => void,
  ) {
    throw new ERR_METHOD_NOT_IMPLEMENTED("_transform()");
  }

  _write = (
    // deno-lint-ignore no-explicit-any
    chunk: any,
    encoding: string,
    callback: (error?: Error | null) => void,
  ) => {
    const rState = this._readableState;
    const wState = this._writableState;
    const length = rState.length;

    this._transform(chunk, encoding, (err, val) => {
      if (err) {
        callback(err);
        return;
      }

      if (val != null) {
        this.push(val);
      }

      if (
        wState.ended || // Backwards compat.
        length === rState.length || // Backwards compat.
        rState.length < rState.highWaterMark ||
        rState.length === 0
      ) {
        callback();
      } else {
        this[kCallback] = callback;
      }
    });
  };
}