-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configuration validation (#1778)
Co-authored-by: Russell Dempsey <[email protected]>
- Loading branch information
Showing
28 changed files
with
338 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { FaultTolerance } from '@libp2p/interface/transport' | ||
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' | ||
import type { ConnectionManagerInit } from '../connection-manager/index.js' | ||
import type { Libp2pInit } from '../index.js' | ||
import type { ServiceMap, RecursivePartial } from '@libp2p/interface' | ||
|
||
const DefaultConfig: Partial<Libp2pInit> = { | ||
connectionManager: { | ||
resolvers: { | ||
dnsaddr: dnsaddrResolver | ||
}, | ||
addressSorter: publicAddressesFirst | ||
}, | ||
transportManager: { | ||
faultTolerance: FaultTolerance.FATAL_ALL | ||
} | ||
} | ||
|
||
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) | ||
}) | ||
|
||
if ((opts?.services) != null) { | ||
// @ts-expect-error until we resolve https://github.com/libp2p/js-libp2p/pull/1762 and have a better way of discovering type dependencies | ||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
if ((opts.services?.kadDHT || opts.services?.relay || opts.services?.ping) && !opts.services.identify) { | ||
throw new Error('identify service is required when using kadDHT, relay, or ping') | ||
} | ||
} | ||
|
||
const parsedOpts = libp2pConfig.validateSync(opts) | ||
|
||
const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, parsedOpts) | ||
|
||
return resultingOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.