|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Handler that buffers response data and notifies multiple waiting handlers. |
| 9 | + * Used for request deduplication. |
| 10 | + * |
| 11 | + * @implements {DispatchHandler} |
| 12 | + */ |
| 13 | +class DeduplicationHandler { |
| 14 | + /** |
| 15 | + * @type {DispatchHandler} |
| 16 | + */ |
| 17 | + #primaryHandler |
| 18 | + |
| 19 | + /** |
| 20 | + * @type {DispatchHandler[]} |
| 21 | + */ |
| 22 | + #waitingHandlers = [] |
| 23 | + |
| 24 | + /** |
| 25 | + * @type {Buffer[]} |
| 26 | + */ |
| 27 | + #chunks = [] |
| 28 | + |
| 29 | + /** |
| 30 | + * @type {number} |
| 31 | + */ |
| 32 | + #statusCode = 0 |
| 33 | + |
| 34 | + /** |
| 35 | + * @type {Record<string, string | string[]>} |
| 36 | + */ |
| 37 | + #headers = {} |
| 38 | + |
| 39 | + /** |
| 40 | + * @type {string} |
| 41 | + */ |
| 42 | + #statusMessage = '' |
| 43 | + |
| 44 | + /** |
| 45 | + * @type {boolean} |
| 46 | + */ |
| 47 | + #aborted = false |
| 48 | + |
| 49 | + /** |
| 50 | + * @type {import('../../types/dispatcher.d.ts').default.DispatchController | null} |
| 51 | + */ |
| 52 | + #controller = null |
| 53 | + |
| 54 | + /** |
| 55 | + * @type {(() => void) | null} |
| 56 | + */ |
| 57 | + #onComplete = null |
| 58 | + |
| 59 | + /** |
| 60 | + * @param {DispatchHandler} primaryHandler The primary handler |
| 61 | + * @param {() => void} onComplete Callback when request completes |
| 62 | + */ |
| 63 | + constructor (primaryHandler, onComplete) { |
| 64 | + this.#primaryHandler = primaryHandler |
| 65 | + this.#onComplete = onComplete |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Add a waiting handler that will receive the buffered response |
| 70 | + * @param {DispatchHandler} handler |
| 71 | + */ |
| 72 | + addWaitingHandler (handler) { |
| 73 | + this.#waitingHandlers.push(handler) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param {() => void} abort |
| 78 | + * @param {any} context |
| 79 | + */ |
| 80 | + onRequestStart (controller, context) { |
| 81 | + this.#controller = controller |
| 82 | + this.#primaryHandler.onRequestStart?.(controller, context) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller |
| 87 | + * @param {number} statusCode |
| 88 | + * @param {import('../../types/header.d.ts').IncomingHttpHeaders} headers |
| 89 | + * @param {Socket} socket |
| 90 | + */ |
| 91 | + onRequestUpgrade (controller, statusCode, headers, socket) { |
| 92 | + this.#primaryHandler.onRequestUpgrade?.(controller, statusCode, headers, socket) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller |
| 97 | + * @param {number} statusCode |
| 98 | + * @param {Record<string, string | string[]>} headers |
| 99 | + * @param {string} statusMessage |
| 100 | + */ |
| 101 | + onResponseStart (controller, statusCode, headers, statusMessage) { |
| 102 | + this.#statusCode = statusCode |
| 103 | + this.#headers = headers |
| 104 | + this.#statusMessage = statusMessage |
| 105 | + this.#primaryHandler.onResponseStart?.(controller, statusCode, headers, statusMessage) |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller |
| 110 | + * @param {Buffer} chunk |
| 111 | + */ |
| 112 | + onResponseData (controller, chunk) { |
| 113 | + // Buffer the chunk for waiting handlers |
| 114 | + this.#chunks.push(Buffer.from(chunk)) |
| 115 | + this.#primaryHandler.onResponseData?.(controller, chunk) |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller |
| 120 | + * @param {object} trailers |
| 121 | + */ |
| 122 | + onResponseEnd (controller, trailers) { |
| 123 | + this.#primaryHandler.onResponseEnd?.(controller, trailers) |
| 124 | + this.#notifyWaitingHandlers() |
| 125 | + this.#onComplete?.() |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller |
| 130 | + * @param {Error} err |
| 131 | + */ |
| 132 | + onResponseError (controller, err) { |
| 133 | + this.#aborted = true |
| 134 | + this.#primaryHandler.onResponseError?.(controller, err) |
| 135 | + this.#notifyWaitingHandlersError(err) |
| 136 | + this.#onComplete?.() |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Notify all waiting handlers with the buffered response |
| 141 | + */ |
| 142 | + #notifyWaitingHandlers () { |
| 143 | + const body = Buffer.concat(this.#chunks) |
| 144 | + |
| 145 | + for (const handler of this.#waitingHandlers) { |
| 146 | + // Create a simple controller for each waiting handler |
| 147 | + const waitingController = { |
| 148 | + resume () {}, |
| 149 | + pause () {}, |
| 150 | + get paused () { return false }, |
| 151 | + get aborted () { return false }, |
| 152 | + get reason () { return null }, |
| 153 | + abort () {} |
| 154 | + } |
| 155 | + |
| 156 | + try { |
| 157 | + handler.onRequestStart?.(waitingController, null) |
| 158 | + |
| 159 | + if (waitingController.aborted) { |
| 160 | + continue |
| 161 | + } |
| 162 | + |
| 163 | + handler.onResponseStart?.( |
| 164 | + waitingController, |
| 165 | + this.#statusCode, |
| 166 | + this.#headers, |
| 167 | + this.#statusMessage |
| 168 | + ) |
| 169 | + |
| 170 | + if (waitingController.aborted) { |
| 171 | + continue |
| 172 | + } |
| 173 | + |
| 174 | + if (body.length > 0) { |
| 175 | + handler.onResponseData?.(waitingController, body) |
| 176 | + } |
| 177 | + |
| 178 | + handler.onResponseEnd?.(waitingController, {}) |
| 179 | + } catch { |
| 180 | + // Ignore errors from waiting handlers |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + this.#waitingHandlers = [] |
| 185 | + this.#chunks = [] |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Notify all waiting handlers of an error |
| 190 | + * @param {Error} err |
| 191 | + */ |
| 192 | + #notifyWaitingHandlersError (err) { |
| 193 | + for (const handler of this.#waitingHandlers) { |
| 194 | + const waitingController = { |
| 195 | + resume () {}, |
| 196 | + pause () {}, |
| 197 | + get paused () { return false }, |
| 198 | + get aborted () { return true }, |
| 199 | + get reason () { return err }, |
| 200 | + abort () {} |
| 201 | + } |
| 202 | + |
| 203 | + try { |
| 204 | + handler.onRequestStart?.(waitingController, null) |
| 205 | + handler.onResponseError?.(waitingController, err) |
| 206 | + } catch { |
| 207 | + // Ignore errors from waiting handlers |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + this.#waitingHandlers = [] |
| 212 | + this.#chunks = [] |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +module.exports = DeduplicationHandler |
0 commit comments