| 1 |
'use strict' |
| 2 |
|
| 3 |
const Dispatcher = require('./dispatcher') |
| 4 |
const { |
| 5 |
ClientDestroyedError, |
| 6 |
ClientClosedError, |
| 7 |
InvalidArgumentError |
| 8 |
} = require('../core/errors') |
| 9 |
const { kDestroy, kClose, kClosed, kDestroyed, kDispatch } = require('../core/symbols') |
| 10 |
|
| 11 |
const kOnDestroyed = Symbol('onDestroyed') |
| 12 |
const kOnClosed = Symbol('onClosed') |
| 13 |
const kWebSocketOptions = Symbol('webSocketOptions') |
| 14 |
|
| 15 |
class DispatcherBase extends Dispatcher { |
| 16 |
/** @type {boolean} */ |
| 17 |
[kDestroyed] = false; |
| 18 |
|
| 19 |
/** @type {Array<Function|null} */ |
| 20 |
[kOnDestroyed] = null; |
| 21 |
|
| 22 |
/** @type {boolean} */ |
| 23 |
[kClosed] = false; |
| 24 |
|
| 25 |
/** @type {Array<Function>|null} */ |
| 26 |
[kOnClosed] = null |
| 27 |
|
| 28 |
/** |
| 29 |
* @param {import('../../types/dispatcher').DispatcherOptions} [opts] |
| 30 |
*/ |
| 31 |
constructor (opts) { |
| 32 |
super() |
| 33 |
this[kWebSocketOptions] = opts?.webSocket ?? {} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @returns {import('../../types/dispatcher').WebSocketOptions} |
| 38 |
*/ |
| 39 |
get webSocketOptions () { |
| 40 |
return { |
| 41 |
maxFragments: this[kWebSocketOptions].maxFragments ?? 131072, |
| 42 |
maxPayloadSize: this[kWebSocketOptions].maxPayloadSize ?? 128 * 1024 * 1024 // 128 MB default |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** @returns {boolean} */ |
| 47 |
get destroyed () { |
| 48 |
return this[kDestroyed] |
| 49 |
} |
| 50 |
|
| 51 |
/** @returns {boolean} */ |
| 52 |
get closed () { |
| 53 |
return this[kClosed] |
| 54 |
} |
| 55 |
|
| 56 |
close (callback) { |
| 57 |
if (callback === undefined) { |
| 58 |
return new Promise((resolve, reject) => { |
| 59 |
this.close((err, data) => { |
| 60 |
return err ? reject(err) : resolve(data) |
| 61 |
}) |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
if (typeof callback !== 'function') { |
| 66 |
throw new InvalidArgumentError('invalid callback') |
| 67 |
} |
| 68 |
|
| 69 |
if (this[kDestroyed]) { |
| 70 |
const err = new ClientDestroyedError() |
| 71 |
queueMicrotask(() => callback(err, null)) |
| 72 |
return |
| 73 |
} |
| 74 |
|
| 75 |
if (this[kClosed]) { |
| 76 |
if (this[kOnClosed]) { |
| 77 |
this[kOnClosed].push(callback) |
| 78 |
} else { |
| 79 |
queueMicrotask(() => callback(null, null)) |
| 80 |
} |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
this[kClosed] = true |
| 85 |
this[kOnClosed] ??= [] |
| 86 |
this[kOnClosed].push(callback) |
| 87 |
|
| 88 |
const onClosed = () => { |
| 89 |
const callbacks = this[kOnClosed] |
| 90 |
this[kOnClosed] = null |
| 91 |
for (let i = 0; i < callbacks.length; i++) { |
| 92 |
callbacks[i](null, null) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Should not error. |
| 97 |
this[kClose]() |
| 98 |
.then(() => this.destroy()) |
| 99 |
.then(() => queueMicrotask(onClosed)) |
| 100 |
} |
| 101 |
|
| 102 |
destroy (err, callback) { |
| 103 |
if (typeof err === 'function') { |
| 104 |
callback = err |
| 105 |
err = null |
| 106 |
} |
| 107 |
|
| 108 |
if (callback === undefined) { |
| 109 |
return new Promise((resolve, reject) => { |
| 110 |
this.destroy(err, (err, data) => { |
| 111 |
return err ? reject(err) : resolve(data) |
| 112 |
}) |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
if (typeof callback !== 'function') { |
| 117 |
throw new InvalidArgumentError('invalid callback') |
| 118 |
} |
| 119 |
|
| 120 |
if (this[kDestroyed]) { |
| 121 |
if (this[kOnDestroyed]) { |
| 122 |
this[kOnDestroyed].push(callback) |
| 123 |
} else { |
| 124 |
queueMicrotask(() => callback(null, null)) |
| 125 |
} |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
if (!err) { |
| 130 |
err = new ClientDestroyedError() |
| 131 |
} |
| 132 |
|
| 133 |
this[kDestroyed] = true |
| 134 |
this[kOnDestroyed] ??= [] |
| 135 |
this[kOnDestroyed].push(callback) |
| 136 |
|
| 137 |
const onDestroyed = () => { |
| 138 |
const callbacks = this[kOnDestroyed] |
| 139 |
this[kOnDestroyed] = null |
| 140 |
for (let i = 0; i < callbacks.length; i++) { |
| 141 |
callbacks[i](null, null) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Should not error. |
| 146 |
this[kDestroy](err) |
| 147 |
.then(() => queueMicrotask(onDestroyed)) |
| 148 |
} |
| 149 |
|
| 150 |
dispatch (opts, handler) { |
| 151 |
if (!handler || typeof handler !== 'object') { |
| 152 |
throw new InvalidArgumentError('handler must be an object') |
| 153 |
} |
| 154 |
|
| 155 |
try { |
| 156 |
if (!opts || typeof opts !== 'object') { |
| 157 |
throw new InvalidArgumentError('opts must be an object.') |
| 158 |
} |
| 159 |
|
| 160 |
if (opts.dispatcher) { |
| 161 |
throw new InvalidArgumentError('opts.dispatcher is not supported by instance methods. Pass opts.dispatcher to the top-level undici functions or call the dispatcher instance method directly.') |
| 162 |
} |
| 163 |
|
| 164 |
if (this[kDestroyed] || this[kOnDestroyed]) { |
| 165 |
throw new ClientDestroyedError() |
| 166 |
} |
| 167 |
|
| 168 |
if (this[kClosed]) { |
| 169 |
throw new ClientClosedError() |
| 170 |
} |
| 171 |
|
| 172 |
return this[kDispatch](opts, handler) |
| 173 |
} catch (err) { |
| 174 |
if (typeof handler.onResponseError !== 'function') { |
| 175 |
throw err |
| 176 |
} |
| 177 |
|
| 178 |
handler.onResponseError(null, err) |
| 179 |
|
| 180 |
return false |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
module.exports = DispatcherBase |