aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/undici/lib/handler/deduplication-handler.js
blob: 33a2c4fdb30688380782eada3a5838268c8e797c (plain) (blame)
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
'use strict'

/**
 * @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler
 */

/**
 * Handler that buffers response data and notifies multiple waiting handlers.
 * Used for request deduplication.
 *
 * @implements {DispatchHandler}
 */
class DeduplicationHandler {
  /**
   * @type {DispatchHandler}
   */
  #primaryHandler

  /**
   * @type {DispatchHandler[]}
   */
  #waitingHandlers = []

  /**
   * @type {Buffer[]}
   */
  #chunks = []

  /**
   * @type {number}
   */
  #statusCode = 0

  /**
   * @type {Record<string, string | string[]>}
   */
  #headers = {}

  /**
   * @type {string}
   */
  #statusMessage = ''

  /**
   * @type {boolean}
   */
  #aborted = false

  /**
   * @type {import('../../types/dispatcher.d.ts').default.DispatchController | null}
   */
  #controller = null

  /**
   * @type {(() => void) | null}
   */
  #onComplete = null

  /**
   * @param {DispatchHandler} primaryHandler The primary handler
   * @param {() => void} onComplete Callback when request completes
   */
  constructor (primaryHandler, onComplete) {
    this.#primaryHandler = primaryHandler
    this.#onComplete = onComplete
  }

  /**
   * Add a waiting handler that will receive the buffered response
   * @param {DispatchHandler} handler
   */
  addWaitingHandler (handler) {
    this.#waitingHandlers.push(handler)
  }

  /**
   * @param {() => void} abort
   * @param {any} context
   */
  onRequestStart (controller, context) {
    this.#controller = controller
    this.#primaryHandler.onRequestStart?.(controller, context)
  }

  /**
   * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
   * @param {number} statusCode
   * @param {import('../../types/header.d.ts').IncomingHttpHeaders} headers
   * @param {Socket} socket
   */
  onRequestUpgrade (controller, statusCode, headers, socket) {
    this.#primaryHandler.onRequestUpgrade?.(controller, statusCode, headers, socket)
  }

  /**
   * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
   * @param {number} statusCode
   * @param {Record<string, string | string[]>} headers
   * @param {string} statusMessage
   */
  onResponseStart (controller, statusCode, headers, statusMessage) {
    this.#statusCode = statusCode
    this.#headers = headers
    this.#statusMessage = statusMessage
    this.#primaryHandler.onResponseStart?.(controller, statusCode, headers, statusMessage)
  }

  /**
   * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
   * @param {Buffer} chunk
   */
  onResponseData (controller, chunk) {
    // Buffer the chunk for waiting handlers
    this.#chunks.push(Buffer.from(chunk))
    this.#primaryHandler.onResponseData?.(controller, chunk)
  }

  /**
   * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
   * @param {object} trailers
   */
  onResponseEnd (controller, trailers) {
    this.#primaryHandler.onResponseEnd?.(controller, trailers)
    this.#notifyWaitingHandlers()
    this.#onComplete?.()
  }

  /**
   * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
   * @param {Error} err
   */
  onResponseError (controller, err) {
    this.#aborted = true
    this.#primaryHandler.onResponseError?.(controller, err)
    this.#notifyWaitingHandlersError(err)
    this.#onComplete?.()
  }

  /**
   * Notify all waiting handlers with the buffered response
   */
  #notifyWaitingHandlers () {
    const body = Buffer.concat(this.#chunks)

    for (const handler of this.#waitingHandlers) {
      // Create a simple controller for each waiting handler
      const waitingController = {
        resume () {},
        pause () {},
        get paused () { return false },
        get aborted () { return false },
        get reason () { return null },
        abort () {}
      }

      try {
        handler.onRequestStart?.(waitingController, null)

        if (waitingController.aborted) {
          continue
        }

        handler.onResponseStart?.(
          waitingController,
          this.#statusCode,
          this.#headers,
          this.#statusMessage
        )

        if (waitingController.aborted) {
          continue
        }

        if (body.length > 0) {
          handler.onResponseData?.(waitingController, body)
        }

        handler.onResponseEnd?.(waitingController, {})
      } catch {
        // Ignore errors from waiting handlers
      }
    }

    this.#waitingHandlers = []
    this.#chunks = []
  }

  /**
   * Notify all waiting handlers of an error
   * @param {Error} err
   */
  #notifyWaitingHandlersError (err) {
    for (const handler of this.#waitingHandlers) {
      const waitingController = {
        resume () {},
        pause () {},
        get paused () { return false },
        get aborted () { return true },
        get reason () { return err },
        abort () {}
      }

      try {
        handler.onRequestStart?.(waitingController, null)
        handler.onResponseError?.(waitingController, err)
      } catch {
        // Ignore errors from waiting handlers
      }
    }

    this.#waitingHandlers = []
    this.#chunks = []
  }
}

module.exports = DeduplicationHandler