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

R2 Object body missing clone() function type #2453

Closed
chrisspiegl opened this issue Jul 28, 2024 · 4 comments
Closed

R2 Object body missing clone() function type #2453

chrisspiegl opened this issue Jul 28, 2024 · 4 comments
Assignees
Labels
types Related to @cloudflare/workers-types

Comments

@chrisspiegl
Copy link

chrisspiegl commented Jul 28, 2024

I wanted to use an r2object body twice.

I am aware that with a fetch() response, we can use .clone() to do this.

When I tried, r2object.body.clone() types suggest that it's not present.

Previous Statement: When running the code, it does work as expected, giving me the feeling that the type definition for clone() is missing.

Correction: It appears that the clone() function indeed does not exist. But maybe it should.

CleanShot 2024-07-28 at 15 08 10@2x

This is a minimal code:

r2object = await env.MAIN_R2_OLD.get(cacheKey)

if (r2object) {
  ctx.waitUntil(env.MAIN_R2_NEW.put(cacheKey, r2object.body.clone(), {
    httpMetadata: r2object.httpMetadata,
    customMetadata: r2object.customMetadata,
  }))
}

return new Response(r2object.body, init)
@chrisspiegl chrisspiegl added the types Related to @cloudflare/workers-types label Jul 28, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Jul 28, 2024
@kentonv
Copy link
Member

kentonv commented Jul 28, 2024

ReadableStream has a tee() method, which does what you are looking for: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/tee

For whatever reason, the standards decided the method on ReadableStream should be called tee() instead of clone(). (The clone() method appears on the HTTP Response type, but R2 does not use this type.)

@chrisspiegl
Copy link
Author

Thank you for your detailed response. Then I'll soon close this issue again.

One more question if I may: tee() returns an array of two copies of the stream, would it be possible to still use the one that's the original r2object.body or is that “used” at that point?

Cause handing over the “split” stream is another step compared to the ease of use of the response.clone() variant.

@jasnell
Copy link
Member

jasnell commented Jul 31, 2024

... would it be possible to still use the one that's the original r2object.body or is that “used” at that point?

No, the original stream will be locked and is no longer directly usable.

@chrisspiegl
Copy link
Author

Thank you for this information. With that, I'll close this issue, as I do not expect the clone() to be added to the r2 response body. Considering, it will stay a standard stream.

The workaround for using a r2 body twice is to use const [body1, body2] = r2object.body.tee to split the stream into two, and then do whatever needs to be done with those two streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Related to @cloudflare/workers-types
Projects
None yet
Development

No branches or pull requests

4 participants