@@ -126,7 +126,7 @@ export default class HttpClient implements ProtocolClient {
126126 const request = await this . generateFetchRequest ( form , "GET" , { headers } ) ;
127127 debug ( `HttpClient (readResource) sending ${ request . method } to ${ request . url } ` ) ;
128128
129- const result = await this . fetch ( request ) ;
129+ const result = await this . doFetch ( request ) ;
130130
131131 this . checkFetchResponse ( result ) ;
132132
@@ -150,7 +150,7 @@ export default class HttpClient implements ProtocolClient {
150150 request . url
151151 } `
152152 ) ;
153- const result = await this . fetch ( request ) ;
153+ const result = await this . doFetch ( request ) ;
154154
155155 debug ( `HttpClient received ${ result . status } from ${ result . url } ` ) ;
156156
@@ -213,7 +213,7 @@ export default class HttpClient implements ProtocolClient {
213213 } to ${ request . url } `
214214 ) ;
215215
216- const result = await this . fetch ( request ) ;
216+ const result = await this . doFetch ( request ) ;
217217
218218 debug ( `HttpClient received ${ result . status } from ${ request . url } ` ) ;
219219 debug ( `HttpClient received Content-Type: ${ result . headers . get ( "content-type" ) } ` ) ;
@@ -245,7 +245,7 @@ export default class HttpClient implements ProtocolClient {
245245 Accept : "application/td+json" ,
246246 } ;
247247 const request = await this . generateFetchRequest ( { href : uri } , "GET" , headers ) ;
248- const response = await this . fetch ( request ) ;
248+ const response = await this . doFetch ( request ) ;
249249 const body = ProtocolHelpers . toNodeStream ( response . body as Readable ) ;
250250 return new Content ( response . headers . get ( "content-type" ) ?? "application/td+json" , body ) ;
251251 }
@@ -410,12 +410,29 @@ export default class HttpClient implements ProtocolClient {
410410 return request ;
411411 }
412412
413- private async fetch ( request : Request , content ?: Content ) {
414- const result = await fetch ( request , { body : content ?. body } ) ;
413+ /**
414+ * Performs the fetch operation for the given request.
415+ *
416+ * This method is intended to be overridden in browser implementations due to differences
417+ * in how the fetch operation handles streams in the request body.
418+ *
419+ * @param request - The HTTP request to be sent.
420+ * @returns A promise that resolves to the HTTP response.
421+ */
422+ protected _fetch ( request : Request ) : Promise < Response > {
423+ // TODO: need investigation. Even if the request has already a body
424+ // if we don't pass it again to the fetch as request init the stream is
425+ // not correctly consumed
426+ // see https://github.com/eclipse-thingweb/node-wot/issues/1366.
427+ return fetch ( request , { body : request . body } ) ;
428+ }
429+
430+ private async doFetch ( request : Request ) {
431+ const result = await this . _fetch ( request ) ;
415432
416433 if ( HttpClient . isOAuthTokenExpired ( result , this . credential ) ) {
417434 this . credential = await ( this . credential as OAuthCredential ) . refreshToken ( ) ;
418- return await fetch ( await this . credential . sign ( request ) ) ;
435+ return await this . _fetch ( await this . credential . sign ( request ) ) ;
419436 }
420437
421438 return result ;
0 commit comments