Skip to content

Commit 9b6c86c

Browse files
authored
Merge pull request #1369 from relu91/fix/1366
Handle long streams as inputs in HTTP binding
2 parents ecbeaa5 + 4ef545c commit 9b6c86c

File tree

5 files changed

+72
-54
lines changed

5 files changed

+72
-54
lines changed

package-lock.json

+39-43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/binding-http/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"client-oauth2": "^4.2.5",
4040
"eventsource": "^2.0.2",
4141
"find-my-way": "^8.2.2",
42-
"node-fetch": "^2.6.7",
42+
"node-fetch": "^2.7.0",
4343
"query-string": "^7.1.1",
4444
"rxjs": "5.5.11",
4545
"slugify": "^1.4.5"

packages/binding-http/src/http-client-browser.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
1414
********************************************************************************/
1515
import { ProtocolHelpers } from "@node-wot/core";
16-
import { RequestInit, Request } from "node-fetch";
16+
import fetch, { RequestInit, Request } from "node-fetch";
1717
import { Readable } from "stream";
1818
import { HttpForm, HTTPMethodName } from "./http";
1919
import HttpClient from "./http-client-impl";
@@ -24,10 +24,15 @@ export default class BrowserHttpClient extends HttpClient {
2424
defaultMethod: HTTPMethodName,
2525
additionalOptions?: RequestInit
2626
): Promise<Request> {
27+
// See https://github.com/eclipse-thingweb/node-wot/issues/790
2728
if (additionalOptions?.body instanceof Readable) {
2829
const buffer = await ProtocolHelpers.readStreamFully(additionalOptions.body);
2930
additionalOptions.body = buffer;
3031
}
3132
return super.generateFetchRequest(form, defaultMethod, additionalOptions);
3233
}
34+
35+
protected async _fetch(request: Request) {
36+
return fetch(request);
37+
}
3338
}

packages/binding-http/src/http-client-impl.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -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;

packages/binding-http/src/subscription-protocols.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class LongPollingSubscription implements InternalSubscription {
5050
timeout: 1000,
5151
signal: this.abortController.signal as AbortSignal,
5252
});
53-
const result = await this.client["fetch"](headRequest);
53+
const result = await this.client["doFetch"](headRequest);
5454
if (result.ok) resolve();
5555
}
5656

@@ -61,7 +61,7 @@ export class LongPollingSubscription implements InternalSubscription {
6161
});
6262
debug(`HttpClient (subscribeResource) sending ${request.method} to ${request.url}`);
6363

64-
const result = await this.client["fetch"](request);
64+
const result = await this.client["doFetch"](request);
6565

6666
this.client["checkFetchResponse"](result);
6767

0 commit comments

Comments
 (0)