Skip to content

Commit

Permalink
refactor: added addresses config validation (libp2p#1573)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed May 29, 2023
1 parent 5874c4e commit 71490db
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/address-manager/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { type ObjectSchema, object, array, string } from 'yup'
import { validateMultiaddr } from '../utils.js'
import type { AddressManagerInit } from '.'

export function debounce (func: () => void, wait: number): () => void {
let timeout: ReturnType<typeof setTimeout> | undefined
Expand All @@ -12,3 +15,11 @@ export function debounce (func: () => void, wait: number): () => void {
timeout = setTimeout(later, wait)
}
}

export function validateAddressManagerConfig (opts: AddressManagerInit): ObjectSchema<Record<string, unknown>> {
return object({
listen: array().of(string()).test('is multiaddr', validateMultiaddr).optional(),
announce: array().of(string()).test('is multiaddr', validateMultiaddr).optional(),
noAnnounce: array().of(string()).test('is multiaddr', validateMultiaddr).optional()
})
}
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { publicAddressesFirst } from '@libp2p/utils/address-sort'
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
import mergeOptions from 'merge-options'
import { object } from 'yup'
import { validateAddressManagerConfig } from './address-manager/utils.js'
import { validateConnectionManagerConfig } from './connection-manager/utils.js'
import type { AddressManagerInit } from './address-manager/index.js'
import type { ConnectionManagerInit } from './connection-manager/index.js'
import type { Libp2pInit } from './index.js'
import type { ServiceMap } from '@libp2p/interface-libp2p'
Expand All @@ -30,6 +32,7 @@ const DefaultConfig: Partial<Libp2pInit> = {

export function validateConfig <T extends ServiceMap = Record<string, unknown>> (opts: RecursivePartial<Libp2pInit<T>>): Libp2pInit<T> {
const libp2pConfig = object({
addresses: validateAddressManagerConfig(opts?.addresses as AddressManagerInit),
connectionManager: validateConnectionManagerConfig(opts?.connectionManager as ConnectionManagerInit)
})

Expand Down
12 changes: 1 addition & 11 deletions src/connection-manager/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logger } from '@libp2p/logger'
import { type AbortOptions, multiaddr, type Multiaddr } from '@multiformats/multiaddr'
import { type ClearableSignal, anySignal } from 'any-signal'
import { type ObjectSchema, array, number, object, string } from 'yup'
import { validateMultiaddr } from '../utils.js'
import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_PRIORITY, DIAL_TIMEOUT, INBOUND_CONNECTION_THRESHOLD, INBOUND_UPGRADE_TIMEOUT, MAX_CONNECTIONS, MAX_INCOMING_PENDING_CONNECTIONS, MAX_PARALLEL_DIALS, MAX_PARALLEL_DIALS_PER_PEER, MAX_PEER_ADDRS_TO_DIAL, MIN_CONNECTIONS } from './constants.js'
import type { ConnectionManagerInit } from '.'

Expand Down Expand Up @@ -78,17 +79,6 @@ export function combineSignals (...signals: Array<AbortSignal | undefined>): Cle
}

export const validateConnectionManagerConfig = (opts: ConnectionManagerInit): ObjectSchema<Record<string, unknown>> => {
const validateMultiaddr = (value: Array<string | undefined> | undefined): boolean => {
value?.forEach((addr) => {
try {
multiaddr(addr)
} catch (err) {
throw new Error(`invalid multiaddr: ${addr}`)
}
})
return true
}

return object({
maxConnections: number().min(opts?.minConnections ?? MIN_CONNECTIONS, `maxConnections must be greater than the min connections limit: ${opts?.minConnections}`).integer().default(MAX_CONNECTIONS),
minConnections: number().min(0).integer().max(opts?.maxConnections ?? MAX_CONNECTIONS, `minConnections must be less than the max connections limit: ${opts?.maxConnections}`).default(MIN_CONNECTIONS),
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { multiaddr } from '@multiformats/multiaddr'

export const validateMultiaddr = (value: Array<string | undefined> | undefined): boolean => {
value?.forEach((addr) => {
try {
multiaddr(addr)
} catch (err) {
throw new Error(`invalid multiaddr: ${addr}`)
}
})
return true
}

0 comments on commit 71490db

Please sign in to comment.