Skip to content
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

[BUG][typescript-fetch][NodeJS] Requests hang if response body is larger than 16kb #18597

Open
5 of 6 tasks
eyalroth opened this issue May 7, 2024 · 1 comment
Open
5 of 6 tasks

Comments

@eyalroth
Copy link

eyalroth commented May 7, 2024

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In clients generated with typescript-fetch and used in NodeJS, if a post-middleware is added to client (withPostMiddleware) and tries to read the body of the response, then the request will hang when the body is larger than 16kb.

The culprit is the invocation of Response.clone(). Similarly to ReadableStream.tee() (which I suspect clone is invoking under the hood), the usage of this method is intended to be used for parallel consumption of the stream; however, the generated code consumes it sequentially.

When using this method, both the consumer of the original response (the main API method) and the consumer of the clone (the middleware) have to continuously read the body stream. If one of them does not read it, the underlying stream of both of them will stop reading data from the source once it reaches the highWaterMark of the stream, which is essentially the max size of the stream's buffer. In NodeJS, the default value of highWaterMark is 16kb.

openapi-generator version

Latest (7.5.0), not a regression.

OpenAPI declaration file content or url

This applies to any response that contains a body.

Steps to reproduce
let api = new MyGeneratedApi();

api = api.withPostMiddleware(async (context: ResponseContext) => { console.log(await context?.response?.text()) });

// hangs if body is larger than 16kb
const data = await api.getData();
Suggest a fix

Instead of cloning the response (here and here), the generated code should:

  1. Fully read the body before invoking the post middlewares.
  2. Pass the body it to the middle-wares as is (as a partial Response object).
  3. Construct a new (node-fetch) Response, so that the main API call can consume the body normally (once we read the body in step 1, the original response can no longer be used).
@wing328
Copy link
Member

wing328 commented May 8, 2024

your suggestion sounds good. may I know if you've time to contribute a PR for that enhancement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants