@@ -8,6 +8,7 @@ import { pbStream } from 'it-protobuf-stream'
8
8
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9
9
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10
10
import { isNode , isBrowser , isWebWorker , isElectronMain , isElectronRenderer , isReactNative } from 'wherearewe'
11
+ import { boolean , number , object , string } from 'yup'
11
12
import { codes } from '../errors.js'
12
13
import {
13
14
AGENT_VERSION ,
@@ -18,9 +19,12 @@ import {
18
19
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION ,
19
20
MAX_INBOUND_STREAMS ,
20
21
MAX_OUTBOUND_STREAMS ,
21
- MAX_PUSH_INCOMING_STREAMS ,
22
22
MAX_IDENTIFY_MESSAGE_SIZE ,
23
23
TIMEOUT ,
24
+ RUN_ON_CONNECTION_OPEN ,
25
+ PROTOCOL_PREFIX ,
26
+ RUN_ON_TRANSIENT_CONNECTION ,
27
+ MAX_PUSH_INCOMING_STREAMS ,
24
28
MAX_PUSH_OUTGOING_STREAMS ,
25
29
MAX_OBSERVED_ADDRESSES
26
30
} from './consts.js'
@@ -60,9 +64,24 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
60
64
private readonly maxIdentifyMessageSize : number
61
65
private readonly maxObservedAddresses : number
62
66
private readonly events : EventEmitter < Libp2pEvents >
63
- private readonly runOnTransientConnection ?: boolean
67
+ private readonly runOnTransientConnection : boolean
68
+ private readonly runOnConnectionOpen : boolean
64
69
65
70
constructor ( components : IdentifyServiceComponents , init : IdentifyServiceInit ) {
71
+ const validatedConfig = object ( {
72
+ protocolPrefix : string ( ) . default ( PROTOCOL_PREFIX ) ,
73
+ agentVersion : string ( ) . default ( AGENT_VERSION ) ,
74
+ timeout : number ( ) . integer ( ) . default ( TIMEOUT ) ,
75
+ maxIdentifyMessageSize : number ( ) . integer ( ) . min ( 0 ) . default ( MAX_IDENTIFY_MESSAGE_SIZE ) ,
76
+ maxInboundStreams : number ( ) . integer ( ) . min ( 0 ) . default ( MAX_INBOUND_STREAMS ) ,
77
+ maxPushIncomingStreams : number ( ) . integer ( ) . min ( 0 ) . default ( MAX_PUSH_INCOMING_STREAMS ) ,
78
+ maxPushOutgoingStreams : number ( ) . integer ( ) . min ( 0 ) . default ( MAX_PUSH_OUTGOING_STREAMS ) ,
79
+ maxOutboundStreams : number ( ) . integer ( ) . min ( 0 ) . default ( MAX_OUTBOUND_STREAMS ) ,
80
+ maxObservedAddresses : number ( ) . integer ( ) . min ( 0 ) . default ( MAX_OBSERVED_ADDRESSES ) ,
81
+ runOnConnectionOpen : boolean ( ) . default ( RUN_ON_CONNECTION_OPEN ) ,
82
+ runOnTransientConnection : boolean ( ) . default ( RUN_ON_TRANSIENT_CONNECTION )
83
+ } ) . validateSync ( init )
84
+
66
85
this . started = false
67
86
this . peerId = components . peerId
68
87
this . peerStore = components . peerStore
@@ -71,24 +90,25 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
71
90
this . connectionManager = components . connectionManager
72
91
this . events = components . events
73
92
74
- this . identifyProtocolStr = `/${ init . protocolPrefix } /${ MULTICODEC_IDENTIFY_PROTOCOL_NAME } /${ MULTICODEC_IDENTIFY_PROTOCOL_VERSION } `
75
- this . identifyPushProtocolStr = `/${ init . protocolPrefix } /${ MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME } /${ MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION } `
76
- this . timeout = init . timeout ?? TIMEOUT
77
- this . maxInboundStreams = init . maxInboundStreams ?? MAX_INBOUND_STREAMS
78
- this . maxOutboundStreams = init . maxOutboundStreams ?? MAX_OUTBOUND_STREAMS
79
- this . maxPushIncomingStreams = init . maxPushIncomingStreams ?? MAX_PUSH_INCOMING_STREAMS
80
- this . maxPushOutgoingStreams = init . maxPushOutgoingStreams ?? MAX_PUSH_OUTGOING_STREAMS
81
- this . maxIdentifyMessageSize = init . maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
82
- this . maxObservedAddresses = init . maxObservedAddresses ?? MAX_OBSERVED_ADDRESSES
83
- this . runOnTransientConnection = init . runOnTransientConnection
93
+ this . identifyProtocolStr = `/${ validatedConfig . protocolPrefix } /${ MULTICODEC_IDENTIFY_PROTOCOL_NAME } /${ MULTICODEC_IDENTIFY_PROTOCOL_VERSION } `
94
+ this . identifyPushProtocolStr = `/${ validatedConfig . protocolPrefix } /${ MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME } /${ MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION } `
95
+ this . timeout = validatedConfig . timeout
96
+ this . maxInboundStreams = validatedConfig . maxInboundStreams
97
+ this . maxOutboundStreams = validatedConfig . maxOutboundStreams
98
+ this . maxPushIncomingStreams = validatedConfig . maxPushIncomingStreams
99
+ this . maxPushOutgoingStreams = validatedConfig . maxPushOutgoingStreams
100
+ this . maxIdentifyMessageSize = validatedConfig . maxIdentifyMessageSize
101
+ this . maxObservedAddresses = validatedConfig . maxObservedAddresses
102
+ this . runOnTransientConnection = validatedConfig . runOnTransientConnection
103
+ this . runOnConnectionOpen = validatedConfig . runOnConnectionOpen
84
104
85
105
// Store self host metadata
86
106
this . host = {
87
- protocolVersion : `${ init . protocolPrefix } /${ IDENTIFY_PROTOCOL_VERSION } ` ,
88
- agentVersion : init . agentVersion ?? AGENT_VERSION
107
+ protocolVersion : `${ validatedConfig . protocolPrefix } /${ IDENTIFY_PROTOCOL_VERSION } ` ,
108
+ agentVersion : validatedConfig . agentVersion
89
109
}
90
110
91
- if ( init . runOnConnectionOpen === true ) {
111
+ if ( this . runOnConnectionOpen ) {
92
112
// When a new connection happens, trigger identify
93
113
components . events . addEventListener ( 'connection:open' , ( evt ) => {
94
114
const connection = evt . detail
0 commit comments