@@ -78,20 +78,20 @@ export async function select <Stream extends SelectStream> (stream: Stream, prot
78
78
throw new Error ( 'At least one protocol must be specified' )
79
79
}
80
80
81
- options ?. log . trace ( 'select: write ["%s", "%s"]' , PROTOCOL_ID , protocol )
81
+ options ?. log ? .trace ( 'select: write ["%s", "%s"]' , PROTOCOL_ID , protocol )
82
82
const p1 = uint8ArrayFromString ( `${ PROTOCOL_ID } \n` )
83
83
const p2 = uint8ArrayFromString ( `${ protocol } \n` )
84
84
await multistream . writeAll ( lp , [ p1 , p2 ] , options )
85
85
86
- options ?. log . trace ( 'select: reading multistream-select header' )
86
+ options ?. log ? .trace ( 'select: reading multistream-select header' )
87
87
let response = await multistream . readString ( lp , options )
88
- options ?. log . trace ( 'select: read "%s"' , response )
88
+ options ?. log ? .trace ( 'select: read "%s"' , response )
89
89
90
90
// Read the protocol response if we got the protocolId in return
91
91
if ( response === PROTOCOL_ID ) {
92
- options ?. log . trace ( 'select: reading protocol response' )
92
+ options ?. log ? .trace ( 'select: reading protocol response' )
93
93
response = await multistream . readString ( lp , options )
94
- options ?. log . trace ( 'select: read "%s"' , response )
94
+ options ?. log ? .trace ( 'select: read "%s"' , response )
95
95
}
96
96
97
97
// We're done
@@ -101,11 +101,11 @@ export async function select <Stream extends SelectStream> (stream: Stream, prot
101
101
102
102
// We haven't gotten a valid ack, try the other protocols
103
103
for ( const protocol of protocols ) {
104
- options ?. log . trace ( 'select: write "%s"' , protocol )
104
+ options ?. log ? .trace ( 'select: write "%s"' , protocol )
105
105
await multistream . write ( lp , uint8ArrayFromString ( `${ protocol } \n` ) , options )
106
- options ?. log . trace ( 'select: reading protocol response' )
106
+ options ?. log ? .trace ( 'select: reading protocol response' )
107
107
const response = await multistream . readString ( lp , options )
108
- options ?. log . trace ( 'select: read "%s" for "%s"' , response , protocol )
108
+ options ?. log ? .trace ( 'select: read "%s" for "%s"' , response , protocol )
109
109
110
110
if ( response === protocol ) {
111
111
return { stream : lp . unwrap ( ) , protocol }
@@ -163,7 +163,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
163
163
if ( ! sentProtocol ) {
164
164
sendingProtocol = true
165
165
166
- options ?. log . trace ( 'optimistic: write ["%s", "%s", data(%d)] in sink' , PROTOCOL_ID , protocol , buf . byteLength )
166
+ options ?. log ? .trace ( 'optimistic: write ["%s", "%s", data(%d)] in sink' , PROTOCOL_ID , protocol , buf . byteLength )
167
167
168
168
const protocolString = `${ protocol } \n`
169
169
@@ -176,7 +176,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
176
176
buf
177
177
) . subarray ( )
178
178
179
- options ?. log . trace ( 'optimistic: wrote ["%s", "%s", data(%d)] in sink' , PROTOCOL_ID , protocol , buf . byteLength )
179
+ options ?. log ? .trace ( 'optimistic: wrote ["%s", "%s", data(%d)] in sink' , PROTOCOL_ID , protocol , buf . byteLength )
180
180
181
181
sentProtocol = true
182
182
sendingProtocol = false
@@ -198,7 +198,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
198
198
199
199
async function negotiate ( ) : Promise < void > {
200
200
if ( negotiating ) {
201
- options ?. log . trace ( 'optimistic: already negotiating %s stream' , protocol )
201
+ options ?. log ? .trace ( 'optimistic: already negotiating %s stream' , protocol )
202
202
await doneNegotiating . promise
203
203
return
204
204
}
@@ -208,13 +208,13 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
208
208
try {
209
209
// we haven't sent the protocol yet, send it now
210
210
if ( ! sentProtocol ) {
211
- options ?. log . trace ( 'optimistic: doing send protocol for %s stream' , protocol )
211
+ options ?. log ? .trace ( 'optimistic: doing send protocol for %s stream' , protocol )
212
212
await doSendProtocol ( )
213
213
}
214
214
215
215
// if we haven't read the protocol response yet, do it now
216
216
if ( ! readProtocol ) {
217
- options ?. log . trace ( 'optimistic: doing read protocol for %s stream' , protocol )
217
+ options ?. log ? .trace ( 'optimistic: doing read protocol for %s stream' , protocol )
218
218
await doReadProtocol ( )
219
219
}
220
220
} finally {
@@ -233,12 +233,12 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
233
233
sendingProtocol = true
234
234
235
235
try {
236
- options ?. log . trace ( 'optimistic: write ["%s", "%s", data] in source' , PROTOCOL_ID , protocol )
236
+ options ?. log ? .trace ( 'optimistic: write ["%s", "%s", data] in source' , PROTOCOL_ID , protocol )
237
237
await lp . writeV ( [
238
238
uint8ArrayFromString ( `${ PROTOCOL_ID } \n` ) ,
239
239
uint8ArrayFromString ( `${ protocol } \n` )
240
240
] )
241
- options ?. log . trace ( 'optimistic: wrote ["%s", "%s", data] in source' , PROTOCOL_ID , protocol )
241
+ options ?. log ? .trace ( 'optimistic: wrote ["%s", "%s", data] in source' , PROTOCOL_ID , protocol )
242
242
} finally {
243
243
sentProtocol = true
244
244
sendingProtocol = false
@@ -255,15 +255,15 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
255
255
readingProtocol = true
256
256
257
257
try {
258
- options ?. log . trace ( 'optimistic: reading multistream select header' )
258
+ options ?. log ? .trace ( 'optimistic: reading multistream select header' )
259
259
let response = await multistream . readString ( lp , options )
260
- options ?. log . trace ( 'optimistic: read multistream select header "%s"' , response )
260
+ options ?. log ? .trace ( 'optimistic: read multistream select header "%s"' , response )
261
261
262
262
if ( response === PROTOCOL_ID ) {
263
263
response = await multistream . readString ( lp , options )
264
264
}
265
265
266
- options ?. log . trace ( 'optimistic: read protocol "%s", expecting "%s"' , response , protocol )
266
+ options ?. log ? .trace ( 'optimistic: read protocol "%s", expecting "%s"' , response , protocol )
267
267
268
268
if ( response !== protocol ) {
269
269
throw new CodeError ( 'protocol selection failed' , 'ERR_UNSUPPORTED_PROTOCOL' )
@@ -279,7 +279,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
279
279
// make sure we've done protocol negotiation before we read stream data
280
280
await negotiate ( )
281
281
282
- options ?. log . trace ( 'optimistic: reading data from "%s" stream' , protocol )
282
+ options ?. log ? .trace ( 'optimistic: reading data from "%s" stream' , protocol )
283
283
yield * lp . unwrap ( ) . source
284
284
} ) ( )
285
285
@@ -291,7 +291,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
291
291
// this before closing the readable end of the stream
292
292
if ( ! negotiated ) {
293
293
await negotiate ( ) . catch ( err => {
294
- options ?. log . error ( 'could not negotiate protocol before close read' , err )
294
+ options ?. log ? .error ( 'could not negotiate protocol before close read' , err )
295
295
} )
296
296
}
297
297
@@ -308,7 +308,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
308
308
// this before closing the writable end of the stream
309
309
if ( ! negotiated ) {
310
310
await negotiate ( ) . catch ( err => {
311
- options ?. log . error ( 'could not negotiate protocol before close write' , err )
311
+ options ?. log ? .error ( 'could not negotiate protocol before close write' , err )
312
312
} )
313
313
}
314
314
0 commit comments