Skip to content

Commit

Permalink
fix: use trace logging for happy paths (libp2p#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 17, 2023
1 parent 647568f commit 184ef21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ export async function handle (stream: Duplex<any>, protocols: string | string[],

while (true) {
const protocol = await multistream.readString(reader, options)
log('read "%s"', protocol)
log.trace('read "%s"', protocol)

if (protocol === PROTOCOL_ID) {
log('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
multistream.write(writer, uint8ArrayFromString(PROTOCOL_ID), options)
continue
}

if (protocols.includes(protocol)) {
multistream.write(writer, uint8ArrayFromString(protocol), options)
log('respond with "%s" for "%s"', protocol, protocol)
log.trace('respond with "%s" for "%s"', protocol, protocol)
rest()
return { stream: shakeStream, protocol }
}
Expand All @@ -82,7 +82,7 @@ export async function handle (stream: Duplex<any>, protocols: string | string[],
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n\n
multistream.write(writer, new Uint8ArrayList(...protocols.map(p => multistream.encode(uint8ArrayFromString(p)))), options)
// multistream.writeAll(writer, protocols.map(p => uint8ArrayFromString(p)))
log('respond with "%s" for %s', protocols, protocol)
log.trace('respond with "%s" for %s', protocols, protocol)
continue
}

Expand Down
10 changes: 5 additions & 5 deletions src/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ export async function select (stream: Duplex<any>, protocols: string | string[],
throw new Error('At least one protocol must be specified')
}

log('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
const p1 = uint8ArrayFromString(PROTOCOL_ID)
const p2 = uint8ArrayFromString(protocol)
multistream.writeAll(writer, [p1, p2], options)

let response = await multistream.readString(reader, options)
log('select: read "%s"', response)
log.trace('select: read "%s"', response)

// Read the protocol response if we got the protocolId in return
if (response === PROTOCOL_ID) {
response = await multistream.readString(reader, options)
log('select: read "%s"', response)
log.trace('select: read "%s"', response)
}

// We're done
Expand All @@ -90,10 +90,10 @@ export async function select (stream: Duplex<any>, protocols: string | string[],

// We haven't gotten a valid ack, try the other protocols
for (const protocol of protocols) {
log('select: write "%s"', protocol)
log.trace('select: write "%s"', protocol)
multistream.write(writer, uint8ArrayFromString(protocol), options)
const response = await multistream.readString(reader, options)
log('select: read "%s" for "%s"', response, protocol)
log.trace('select: read "%s" for "%s"', response, protocol)

if (response === protocol) {
rest() // End our writer so others can start writing to stream
Expand Down

0 comments on commit 184ef21

Please sign in to comment.