Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do validation first before actual business logic, like super() #3463

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/dispatcher/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
Expand All @@ -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 }
}
Expand Down
8 changes: 4 additions & 4 deletions lib/dispatcher/balanced-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ class Client extends DispatcherBase {
maxConcurrentStreams,
allowH2
} = {}) {
super()

if (keepAlive !== undefined) {
throw new InvalidArgumentError('unsupported keepAlive, use pipelining=0 instead')
}
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/dispatcher/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class Pool extends PoolBase {
allowH2,
...options
} = {}) {
super()

if (connections != null && (!Number.isFinite(connections) || connections < 0)) {
throw new InvalidArgumentError('invalid connections')
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/dispatcher/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/interceptor/dump.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/mock/mock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
4 changes: 2 additions & 2 deletions lib/mock/mock-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
Loading