-
Notifications
You must be signed in to change notification settings - Fork 31
feat(fetch): add Body mixin interface for Request #1804
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
base: main
Are you sure you want to change the base?
Conversation
Adds the FetchBody interface implementing the Fetch API Body mixin, which provides methods for reading request/response bodies: - arrayBuffer() - Read body as ArrayBuffer - blob() - Read body as Blob - formData() - Read body as FormData - json() - Read body as parsed JSON - text() - Read body as string FetchRequest now extends FetchBody to inherit these methods. Implementation is partial - methods are stubbed with TODOs for @darvld to review and complete the integration with: - ReadableStream consumption - JSON.parse intrinsic - ArrayBuffer intrinsic - Blob intrinsic Why this matters: While fetch() works for outbound requests, server-side code receiving requests via the fetch handler pattern needs to parse incoming request bodies. Without request.json(), developers must manually read the body stream and parse JSON, which is error-prone and inconsistent with the Fetch API spec. Refs: Discovered while testing elide-showcases server examples
Implements the Body mixin methods using ReadableStreamDefaultReader pattern from JsWorkerStreams (thanks @darvld for the pointer!). - text(): Consumes stream, decodes as UTF-8 - json(): Uses text() then GraalJS JSON.parse - arrayBuffer(): Consumes stream, returns byte array - blob()/formData(): Still not implemented (need intrinsics) Removes TODO(@darvld) comments - no more assigning tasks to humans!
|
Thanks for the pointer to Changes pushed to
Also removed the The code compiles successfully. Would you like me to open a new PR with these changes, or should I try to push to this branch? |
- Make Blob interface extend BufferAPI.Blob for compatibility - Make NodeBlob implement the unified Blob interface - Implement blob() using arrayBuffer() + NodeBlob wrapper - Content-Type header is preserved in the Blob formData() still pending - requires multipart parsing implementation.
- arrayBuffer(): Returns NIO ByteBuffer (maps to JS ArrayBuffer) - blob(): Uses NodeBlob with content-type from headers - formData(): Supports application/x-www-form-urlencoded (multipart/form-data still pending) - Added FormData class with standard Web API methods Thanks @darvld for the ByteBuffer hint!
Update: Body mixin implementation completeAll Implemented Methods
formData() DetailsSupported:
Not yet supported:
Multipart parsing is significantly more complex as it requires:
This can be added in a follow-up PR if needed. Files Changed
Branch: |
- Added multipart/form-data parsing with boundary detection - Added FormDataFile class for file uploads (implements Blob) - Parses Content-Disposition headers for field names and filenames - Handles both text fields and binary file content - URL-encoded form data still supported as fallback All Body mixin methods now fully implemented: - text() ✅ - json() ✅ - arrayBuffer() ✅ - blob() ✅ - formData() ✅
Update: All Body mixin methods now complete! 🎉
formData() ImplementationURL-encoded (
Multipart (
New Classes
Branch: |
Summary
Adds the
FetchBodyinterface implementing the Fetch API Body mixin, which provides methods for reading request/response bodies:arrayBuffer()- Read body as ArrayBufferblob()- Read body as BlobformData()- Read body as FormDatajson()- Read body as parsed JSONtext()- Read body as stringFetchRequestnow extendsFetchBodyto inherit these methods.This PR is a draft - the implementation is stubbed with TODOs. I need guidance on:
@darvld - Would appreciate your guidance on the best approach for these integrations. Happy to implement once I understand the patterns.
Why This Matters
While
fetch()works for outbound requests, server-side code receiving requests via the fetch handler pattern needs to parse incoming request bodies:This is blocking real-world server applications in elide-showcases.
Changes
FetchBody.kt- Interface defining Body mixin methods with full JSDocFetchRequest.kt- Now extendsFetchBodyFetchRequestIntrinsic.kt- Stub implementations with TODOsRelated