What are multipart responses?
Multipart responses allow servers to send multiple pieces of data in a single HTTP response. Each part can have its own headers and content type, separated by boundaries defined in theContent-Type header.
Meros implements the multipart/mixed content type as defined in RFC1341, making it simple to consume these responses in both browser and Node.js environments.
How Meros processes multipart data
When you pass a response tomeros(), it follows this workflow:
1. Content type validation
Meros first checks if the response is actually multipart:If the response is not multipart, Meros returns the original response unchanged. This makes it safe to use as a middleware in your fetch chain.
2. Boundary extraction
The boundary delimiter is extracted from theContent-Type header:
multipart/mixed; boundary="graphql", Meros extracts graphql and prepends it with -- to match the RFC1341 format.
3. Stream processing
Meros processes the response body as a stream, reading chunks incrementally: Browser implementation (browser.ts:23-27):Part structure
Each part yielded by Meros contains three properties:Headers extraction
Meros parses headers from each part using the double CRLF separator (browser.ts:45-65):Automatic JSON parsing
If a part hasContent-Type: application/json, Meros automatically parses the body:
Preamble and epilogue handling
Meros correctly handles RFC1341’s preamble and epilogue sections:- Preamble: Content before the first boundary is skipped (browser.ts:41-44)
- Epilogue: Content after the final boundary (
--boundary--) stops iteration (browser.ts:79)
Environment-specific differences
While the algorithm is identical, there are key differences between environments:| Aspect | Browser | Node |
|---|---|---|
| Body type | ReadableStream<Uint8Array> | Readable stream |
| Text handling | Uses TextDecoder | Uses Buffer |
| Fallback body | string | Buffer |
| Response type | Response object | IncomingMessage |
You can import from
meros/browser or meros/node explicitly, or use the main meros export which relies on bundler/environment detection.