mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-26 02:52:16 +00:00
102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
|
import mergeOptions from 'merge-options'
|
||
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
||
|
import * as Constants from './constants.js'
|
||
|
import { AGENT_VERSION } from './identify/consts.js'
|
||
|
import * as RelayConstants from './circuit/constants.js'
|
||
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
||
|
import { FAULT_TOLERANCE } from './transport-manager.js'
|
||
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||
|
import type { Libp2pInit } from './index.js'
|
||
|
import { codes, messages } from './errors.js'
|
||
|
import errCode from 'err-code'
|
||
|
import type { RecursivePartial } from '@libp2p/interfaces'
|
||
|
|
||
|
const DefaultConfig: Partial<Libp2pInit> = {
|
||
|
addresses: {
|
||
|
listen: [],
|
||
|
announce: [],
|
||
|
noAnnounce: [],
|
||
|
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
|
||
|
},
|
||
|
connectionManager: {
|
||
|
maxConnections: 300,
|
||
|
minConnections: 50,
|
||
|
autoDialInterval: 10000,
|
||
|
autoDial: true
|
||
|
},
|
||
|
connectionGater: {},
|
||
|
transportManager: {
|
||
|
faultTolerance: FAULT_TOLERANCE.FATAL_ALL
|
||
|
},
|
||
|
dialer: {
|
||
|
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
||
|
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
||
|
dialTimeout: Constants.DIAL_TIMEOUT,
|
||
|
resolvers: {
|
||
|
dnsaddr: dnsaddrResolver
|
||
|
},
|
||
|
addressSorter: publicAddressesFirst
|
||
|
},
|
||
|
host: {
|
||
|
agentVersion: AGENT_VERSION
|
||
|
},
|
||
|
metrics: {
|
||
|
enabled: false,
|
||
|
computeThrottleMaxQueueSize: 1000,
|
||
|
computeThrottleTimeout: 2000,
|
||
|
movingAverageIntervals: [
|
||
|
60 * 1000, // 1 minute
|
||
|
5 * 60 * 1000, // 5 minutes
|
||
|
15 * 60 * 1000 // 15 minutes
|
||
|
],
|
||
|
maxOldPeersRetention: 50
|
||
|
},
|
||
|
peerRouting: {
|
||
|
refreshManager: {
|
||
|
enabled: true,
|
||
|
interval: 6e5,
|
||
|
bootDelay: 10e3
|
||
|
}
|
||
|
},
|
||
|
protocolPrefix: 'ipfs',
|
||
|
nat: {
|
||
|
enabled: true,
|
||
|
ttl: 7200,
|
||
|
keepAlive: true
|
||
|
},
|
||
|
relay: {
|
||
|
enabled: true,
|
||
|
advertise: {
|
||
|
bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY,
|
||
|
enabled: false,
|
||
|
ttl: RelayConstants.ADVERTISE_TTL
|
||
|
},
|
||
|
hop: {
|
||
|
enabled: false,
|
||
|
active: false
|
||
|
},
|
||
|
autoRelay: {
|
||
|
enabled: false,
|
||
|
maxListeners: 2
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit {
|
||
|
const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
|
||
|
|
||
|
if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
|
||
|
throw errCode(new Error(messages.ERR_TRANSPORTS_REQUIRED), codes.ERR_TRANSPORTS_REQUIRED)
|
||
|
}
|
||
|
|
||
|
if (resultingOptions.connectionEncryption == null || resultingOptions.connectionEncryption.length === 0) {
|
||
|
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
|
||
|
}
|
||
|
|
||
|
if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) { // eslint-disable-line no-undef
|
||
|
throw errCode(new Error(messages.ERR_PROTECTOR_REQUIRED), codes.ERR_PROTECTOR_REQUIRED)
|
||
|
}
|
||
|
|
||
|
return resultingOptions
|
||
|
}
|