Skip to content

Handle long streams as inputs in HTTP binding #1369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 39 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/binding-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"client-oauth2": "^4.2.5",
"eventsource": "^2.0.2",
"find-my-way": "^8.2.2",
"node-fetch": "^2.6.7",
"node-fetch": "^2.7.0",
"query-string": "^7.1.1",
"rxjs": "5.5.11",
"slugify": "^1.4.5"
Expand Down
7 changes: 6 additions & 1 deletion packages/binding-http/src/http-client-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
import { ProtocolHelpers } from "@node-wot/core";
import { RequestInit, Request } from "node-fetch";
import fetch, { RequestInit, Request } from "node-fetch";
import { Readable } from "stream";
import { HttpForm, HTTPMethodName } from "./http";
import HttpClient from "./http-client-impl";
Expand All @@ -24,10 +24,15 @@ export default class BrowserHttpClient extends HttpClient {
defaultMethod: HTTPMethodName,
additionalOptions?: RequestInit
): Promise<Request> {
// See https://github.com/eclipse-thingweb/node-wot/issues/790
if (additionalOptions?.body instanceof Readable) {
const buffer = await ProtocolHelpers.readStreamFully(additionalOptions.body);
additionalOptions.body = buffer;
}
return super.generateFetchRequest(form, defaultMethod, additionalOptions);
}

protected async _fetch(request: Request) {
return fetch(request);
}
}
23 changes: 20 additions & 3 deletions packages/binding-http/src/http-client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,29 @@ export default class HttpClient implements ProtocolClient {
return request;
}

private async fetch(request: Request, content?: Content) {
const result = await fetch(request, { body: content?.body });
/**
* Performs the fetch operation for the given request.
*
* This method is intended to be overridden in browser implementations due to differences
* in how the fetch operation handles streams in the request body.
*
* @param request - The HTTP request to be sent.
* @returns A promise that resolves to the HTTP response.
*/
protected _fetch(request: Request): Promise<Response> {
// TODO: need investigation. Even if the request has already a body
// if we don't pass it again to the fetch as request init the stream is
// not correctly consumed
// see https://github.com/eclipse-thingweb/node-wot/issues/1366.
return fetch(request, { body: request.body });
}

private async fetch(request: Request) {
const result = await this._fetch(request);

if (HttpClient.isOAuthTokenExpired(result, this.credential)) {
this.credential = await (this.credential as OAuthCredential).refreshToken();
return await fetch(await this.credential.sign(request));
return await this._fetch(await this.credential.sign(request));
}

return result;
Expand Down