From c5cd6012a416b3ae7890be01d3b651436b509a31 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 15 Aug 2024 11:11:18 +0200 Subject: [PATCH] fix: do validation first before actual code, like super() --- lib/api/readable.js | 3 ++- lib/dispatcher/agent.js | 4 ++-- lib/dispatcher/balanced-pool.js | 8 ++++---- lib/dispatcher/client.js | 4 ++-- lib/dispatcher/pool.js | 4 ++-- lib/dispatcher/proxy-agent.js | 4 ++-- lib/interceptor/dump.js | 4 ++-- lib/mock/mock-client.js | 4 ++-- lib/mock/mock-pool.js | 4 ++-- 9 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 2a597c006e7..2a302b0fa71 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -159,13 +159,14 @@ class BodyReadable extends Readable { } async dump (opts) { - const limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024 const signal = opts?.signal if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) { throw new InvalidArgumentError('signal must be an AbortSignal') } + const limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024 + signal?.throwIfAborted() if (this._readableState.closeEmitted) { diff --git a/lib/dispatcher/agent.js b/lib/dispatcher/agent.js index 9e362fa0682..46fc15742d1 100644 --- a/lib/dispatcher/agent.js +++ b/lib/dispatcher/agent.js @@ -22,8 +22,6 @@ function defaultFactory (origin, opts) { class Agent extends DispatcherBase { constructor ({ factory = defaultFactory, connect, ...options } = {}) { - super() - if (typeof factory !== 'function') { throw new InvalidArgumentError('factory must be a function.') } @@ -32,6 +30,8 @@ class Agent extends DispatcherBase { throw new InvalidArgumentError('connect must be a function or an object') } + super() + if (connect && typeof connect !== 'function') { connect = { ...connect } } diff --git a/lib/dispatcher/balanced-pool.js b/lib/dispatcher/balanced-pool.js index e17baed686f..5bbec0e618d 100644 --- a/lib/dispatcher/balanced-pool.js +++ b/lib/dispatcher/balanced-pool.js @@ -50,6 +50,10 @@ function defaultFactory (origin, opts) { class BalancedPool extends PoolBase { constructor (upstreams = [], { factory = defaultFactory, ...opts } = {}) { + if (typeof factory !== 'function') { + throw new InvalidArgumentError('factory must be a function.') + } + super() this[kOptions] = opts @@ -63,10 +67,6 @@ class BalancedPool extends PoolBase { upstreams = [upstreams] } - if (typeof factory !== 'function') { - throw new InvalidArgumentError('factory must be a function.') - } - this[kFactory] = factory for (const upstream of upstreams) { diff --git a/lib/dispatcher/client.js b/lib/dispatcher/client.js index 87714d5009d..f3143fdbdea 100644 --- a/lib/dispatcher/client.js +++ b/lib/dispatcher/client.js @@ -101,8 +101,6 @@ class Client extends DispatcherBase { maxConcurrentStreams, allowH2 } = {}) { - super() - if (keepAlive !== undefined) { throw new InvalidArgumentError('unsupported keepAlive, use pipelining=0 instead') } @@ -187,6 +185,8 @@ class Client extends DispatcherBase { throw new InvalidArgumentError('maxConcurrentStreams must be a positive integer, greater than 0') } + super() + if (typeof connect !== 'function') { connect = buildConnector({ ...tls, diff --git a/lib/dispatcher/pool.js b/lib/dispatcher/pool.js index 9c59a4003c9..08303150add 100644 --- a/lib/dispatcher/pool.js +++ b/lib/dispatcher/pool.js @@ -37,8 +37,6 @@ class Pool extends PoolBase { allowH2, ...options } = {}) { - super() - if (connections != null && (!Number.isFinite(connections) || connections < 0)) { throw new InvalidArgumentError('invalid connections') } @@ -51,6 +49,8 @@ class Pool extends PoolBase { throw new InvalidArgumentError('connect must be a function or an object') } + super() + if (typeof connect !== 'function') { connect = buildConnector({ ...tls, diff --git a/lib/dispatcher/proxy-agent.js b/lib/dispatcher/proxy-agent.js index 226b67846da..7956cc5680f 100644 --- a/lib/dispatcher/proxy-agent.js +++ b/lib/dispatcher/proxy-agent.js @@ -25,8 +25,6 @@ function defaultFactory (origin, opts) { class ProxyAgent extends DispatcherBase { constructor (opts) { - super() - if (!opts || (typeof opts === 'object' && !(opts instanceof URL) && !opts.uri)) { throw new InvalidArgumentError('Proxy uri is mandatory') } @@ -36,6 +34,8 @@ class ProxyAgent extends DispatcherBase { throw new InvalidArgumentError('Proxy opts.clientFactory must be a function.') } + super() + const url = this.#getUrl(opts) const { href, origin, port, protocol, username, password, hostname: proxyHostname } = url diff --git a/lib/interceptor/dump.js b/lib/interceptor/dump.js index fc9cacb198d..6e8a01032be 100644 --- a/lib/interceptor/dump.js +++ b/lib/interceptor/dump.js @@ -14,12 +14,12 @@ class DumpHandler extends DecoratorHandler { #handler = null constructor ({ maxSize }, handler) { - super(handler) - if (maxSize != null && (!Number.isFinite(maxSize) || maxSize < 1)) { throw new InvalidArgumentError('maxSize must be a number greater than 0') } + super(handler) + this.#maxSize = maxSize ?? this.#maxSize this.#handler = handler } diff --git a/lib/mock/mock-client.js b/lib/mock/mock-client.js index c375dbd455b..fd840fba908 100644 --- a/lib/mock/mock-client.js +++ b/lib/mock/mock-client.js @@ -21,12 +21,12 @@ const { InvalidArgumentError } = require('../core/errors') */ class MockClient extends Client { constructor (origin, opts) { - super(origin, opts) - if (!opts || !opts.agent || typeof opts.agent.dispatch !== 'function') { throw new InvalidArgumentError('Argument opts.agent must implement Agent') } + super(origin, opts) + this[kMockAgent] = opts.agent this[kOrigin] = origin this[kDispatches] = [] diff --git a/lib/mock/mock-pool.js b/lib/mock/mock-pool.js index 8b005d72ead..bc68a37faa3 100644 --- a/lib/mock/mock-pool.js +++ b/lib/mock/mock-pool.js @@ -21,12 +21,12 @@ const { InvalidArgumentError } = require('../core/errors') */ class MockPool extends Pool { constructor (origin, opts) { - super(origin, opts) - if (!opts || !opts.agent || typeof opts.agent.dispatch !== 'function') { throw new InvalidArgumentError('Argument opts.agent must implement Agent') } + super(origin, opts) + this[kMockAgent] = opts.agent this[kOrigin] = origin this[kDispatches] = []