@@ -43,7 +43,7 @@ export class Fetch implements Startable, FetchInterface {
4343 await data . stream . close ( )
4444 } )
4545 . catch ( err => {
46- this . log . error ( err )
46+ this . log . error ( 'error handling message - %e' , err )
4747 } )
4848 } , {
4949 maxInboundStreams : this . init . maxInboundStreams ,
@@ -64,8 +64,12 @@ export class Fetch implements Startable, FetchInterface {
6464 /**
6565 * Sends a request to fetch the value associated with the given key from the given peer
6666 */
67- async fetch ( peer : PeerId , key : string , options : AbortOptions = { } ) : Promise < Uint8Array | undefined > {
68- this . log ( 'dialing %s to %p' , this . protocol , peer )
67+ async fetch ( peer : PeerId , key : string | Uint8Array , options : AbortOptions = { } ) : Promise < Uint8Array | undefined > {
68+ if ( typeof key === 'string' ) {
69+ key = uint8arrayFromString ( key )
70+ }
71+
72+ this . log . trace ( 'dialing %s to %p' , this . protocol , peer )
6973
7074 const connection = await this . components . connectionManager . openConnection ( peer , options )
7175 let signal = options . signal
@@ -75,7 +79,7 @@ export class Fetch implements Startable, FetchInterface {
7579 // create a timeout if no abort signal passed
7680 if ( signal == null ) {
7781 const timeout = this . init . timeout ?? DEFAULT_TIMEOUT
78- this . log ( 'using default timeout of %d ms' , timeout )
82+ this . log . trace ( 'using default timeout of %d ms' , timeout )
7983 signal = AbortSignal . timeout ( timeout )
8084
8185 setMaxListeners ( Infinity , signal )
@@ -93,7 +97,7 @@ export class Fetch implements Startable, FetchInterface {
9397 // make stream abortable
9498 signal . addEventListener ( 'abort' , onAbort , { once : true } )
9599
96- this . log ( 'fetch %s ' , key )
100+ this . log . trace ( 'fetch %m ' , key )
97101
98102 const pb = pbStream ( stream )
99103 await pb . write ( {
@@ -105,20 +109,20 @@ export class Fetch implements Startable, FetchInterface {
105109
106110 switch ( response . status ) {
107111 case ( FetchResponse . StatusCode . OK ) : {
108- this . log ( 'received status for %s ok ' , key )
112+ this . log . trace ( 'received status OK for %m ' , key )
109113 return response . data
110114 }
111115 case ( FetchResponse . StatusCode . NOT_FOUND ) : {
112- this . log ( 'received status for %s not found ' , key )
116+ this . log ( 'received status NOT_FOUND for %m ' , key )
113117 return
114118 }
115119 case ( FetchResponse . StatusCode . ERROR ) : {
116- this . log ( 'received status for %s error ' , key )
120+ this . log ( 'received status ERROR for %m ' , key )
117121 const errmsg = uint8arrayToString ( response . data )
118122 throw new ProtocolError ( 'Error in fetch protocol response: ' + errmsg )
119123 }
120124 default : {
121- this . log ( 'received status for %s unknown ' , key )
125+ this . log ( 'received status unknown for %m ' , key )
122126 throw new InvalidMessageError ( 'Unknown response status' )
123127 }
124128 }
@@ -149,21 +153,32 @@ export class Fetch implements Startable, FetchInterface {
149153 } )
150154
151155 let response : FetchResponse
152- const lookup = this . _getLookupFunction ( request . identifier )
153- if ( lookup != null ) {
154- this . log ( 'look up data with identifier %s' , request . identifier )
155- const data = await lookup ( request . identifier )
156- if ( data != null ) {
157- this . log ( 'sending status for %s ok' , request . identifier )
158- response = { status : FetchResponse . StatusCode . OK , data }
159- } else {
160- this . log ( 'sending status for %s not found' , request . identifier )
161- response = { status : FetchResponse . StatusCode . NOT_FOUND , data : new Uint8Array ( 0 ) }
162- }
163- } else {
164- this . log ( 'sending status for %s error' , request . identifier )
165- const errmsg = uint8arrayFromString ( `No lookup function registered for key: ${ request . identifier } ` )
156+ const key = uint8arrayToString ( request . identifier )
157+
158+ const lookup = this . _getLookupFunction ( key )
159+
160+ if ( lookup == null ) {
161+ this . log . trace ( 'sending status ERROR for %m' , request . identifier )
162+ const errmsg = uint8arrayFromString ( 'No lookup function registered for key' )
166163 response = { status : FetchResponse . StatusCode . ERROR , data : errmsg }
164+ } else {
165+ this . log . trace ( 'lookup data with identifier %s' , lookup . prefix )
166+
167+ try {
168+ const data = await lookup . fn ( request . identifier )
169+
170+ if ( data == null ) {
171+ this . log . trace ( 'sending status NOT_FOUND for %m' , request . identifier )
172+ response = { status : FetchResponse . StatusCode . NOT_FOUND , data : new Uint8Array ( 0 ) }
173+ } else {
174+ this . log . trace ( 'sending status OK for %m' , request . identifier )
175+ response = { status : FetchResponse . StatusCode . OK , data }
176+ }
177+ } catch ( err : any ) {
178+ this . log . error ( 'error during lookup of %m - %e' , request . identifier , err )
179+ const errmsg = uint8arrayFromString ( err . message )
180+ response = { status : FetchResponse . StatusCode . ERROR , data : errmsg }
181+ }
167182 }
168183
169184 await pb . write ( response , FetchResponse , {
@@ -174,7 +189,7 @@ export class Fetch implements Startable, FetchInterface {
174189 signal
175190 } )
176191 } catch ( err : any ) {
177- this . log ( 'error answering fetch request' , err )
192+ this . log . error ( 'error answering fetch request - %e ' , err )
178193 stream . abort ( err )
179194 }
180195 }
@@ -183,10 +198,17 @@ export class Fetch implements Startable, FetchInterface {
183198 * Given a key, finds the appropriate function for looking up its corresponding value, based on
184199 * the key's prefix.
185200 */
186- _getLookupFunction ( key : string ) : LookupFunction | undefined {
201+ _getLookupFunction ( key : string ) : { fn : LookupFunction , prefix : string } | undefined {
187202 for ( const prefix of this . lookupFunctions . keys ( ) ) {
188203 if ( key . startsWith ( prefix ) ) {
189- return this . lookupFunctions . get ( prefix )
204+ const fn = this . lookupFunctions . get ( prefix )
205+
206+ if ( fn != null ) {
207+ return {
208+ fn,
209+ prefix
210+ }
211+ }
190212 }
191213 }
192214 }
0 commit comments