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

feat(cloudflare-r2-binding): allow specify raw type #519

Merged
merged 3 commits into from
Dec 10, 2024
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Dec 10, 2024

(context: #517 by @MickL)

This PR allows getItemRaw(key, { type: "..." }) with specified return type based on user needs: object/stream/blob/bytes/arrayBuffer are possible (check docs)

@pi0 pi0 self-assigned this Dec 10, 2024
Copy link

cloudflare-workers-and-pages bot commented Dec 10, 2024

Deploying unstorage with  Cloudflare Pages  Cloudflare Pages

Latest commit: be4f18b
Status: ✅  Deploy successful!
Preview URL: https://8b4dae62.unstorage.pages.dev
Branch Preview URL: https://feat-cf-r2-type.unstorage.pages.dev

View logs

docs/2.drivers/cloudflare.md Outdated Show resolved Hide resolved
docs/2.drivers/cloudflare.md Outdated Show resolved Hide resolved
@pi0 pi0 merged commit 90ae631 into main Dec 10, 2024
2 of 3 checks passed
@MickL
Copy link

MickL commented Dec 10, 2024

Maybe

  • getItemRaw() should return .body
  • getItem() should have the option to set return type

?

What I just saw from the docs is that they return .body and I guess this is exactly what is needed to return the correct object? But I am not very familiar with the differences of ArrayBuffer, Blob and ReadableStream.

Example from Cloudflare where tey return .body: https://developers.cloudflare.com/r2/examples/demo-worker/

@pi0 pi0 deleted the feat/cf-r2-type branch December 10, 2024 14:30
@pi0
Copy link
Member Author

pi0 commented Dec 10, 2024

Unstorage raw* methods return a resolved value and .body is a stream which is not resolved + getItem is text based. All usages are valid for CF R2 bindings and it depends on how we want it to be consumed. for unstorage resolved makes sense (but this PR allows you to use other possible ways).

@MickL
Copy link

MickL commented Dec 10, 2024

I saw you marked type bytes to default for v2. Would then the following code work with H3? If yes, I am okay with the new implementation:

export default defineEventHandler(async (event) => {
  return useStorage('files').getItemRaw('some-key');
});

@pi0
Copy link
Member Author

pi0 commented Dec 10, 2024

Yes it should. If you had time can you please make an issue + reproduction in h3 for supporting arrayBuffer too? (it looks an issue there)

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

Successfully merging this pull request may close these issues.

2 participants