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

Your worker called response.clone(), but did not read the body of both clones #37

Closed
2 tasks done
Fabb111 opened this issue Nov 25, 2022 · 1 comment
Closed
2 tasks done

Comments

@Fabb111
Copy link

Fabb111 commented Nov 25, 2022

What happened?

Running this

const issuer = new URL("...");
const as = await oauth2
  .discoveryRequest(issuer)
  .then((response) => oauth2.processDiscoveryResponse(issuer, response))

causes Cloudflare Workers to issue the following warning:

Your worker called response.clone(), but did not read the body of both clones. This is wasteful, as it forces the system to buffer the entire response body in memory, rather than streaming it through. This may cause your worker to be unexpectedly terminated for going over the memory limit. If you only meant to copy the response headers and metadata (e.g. in order to be able to modify them), use new Response(response.body, response) instead.

Version

v2.0.1

Runtime

Cloudflare Workers

Runtime Details

Wrangler compatibility_date = "2022-11-25"

Code to reproduce

export default {
	async fetch(
		request: Request,
		env: Env,
		ctx: ExecutionContext
	): Promise<Response> {
		const url = new URL(request.url);

		if (url.pathname === "/") {
			const issuer = new URL("...");
			const as = await oauth2
				.discoveryRequest(issuer)
				.then((response) => oauth2.processDiscoveryResponse(issuer, response))

			console.log(as);

			return new Response(null, { headers, status: 302 });
		} else {
			return new Response(null, { status: 404 });
		}
	},
};

Required

  • I have searched the issues tracker and discussions for similar topics and couldn't find anything related.
  • I agree to follow this project's Code of Conduct
@Fabb111 Fabb111 added the triage label Nov 25, 2022
@panva
Copy link
Owner

panva commented Nov 25, 2022

https://github.com/panva/oauth4webapi/blob/v2.0.1/src/index.ts#L764

The response is being read straight after cloning.

It is true that only the clone is being read in your code and this will be true for mostly every response consuming routine, but it is there for a reason, so that in case the library throws while processing the response, the developer can still access its body.

I will change this so that the clone() would have to be done by the developer before this routine as an opt-in.

@panva panva closed this as completed in a785223 Nov 25, 2022
@panva panva removed the triage label Nov 25, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants