Comments (5)
Something similar happens with RC6 for Server Sent Events. (scastie)
from zio-http.
I think there's a room for improvement in stream body encoding.
For example, maybe SSE deserves a dedicated HttpContentCodec
rather than the hidden Schema comparison.
from zio-http.
I think I've spotted the cause:
Endpoint#outStream[A]
summons an implicitHttpContentCodec
(code)- New
HttpContentCodec
is created using the existing schema, withapplication/json
as the first candidate (code) - The JSON codec is picked up
- The encoder checks if the default media type is binary (code)
- JSON is defined as a binary codec (code)
- The encoder unsafely casts the stream as byte-emitting without properly encoding it (code)
- NettyWriter crashes as
Body.StreamBody
assumes it contains a byte stream
from zio-http.
application/json
has been treated as binary since 2022, unlike the other JSON variants. I think it's just a mistake.
from zio-http.
For SSE, I took a workaround with a custom codec that might be added to the upstream once there is a proper decoder implementation.
val binaryCodec: BinaryCodec[ServerSentEvent] =
new BinaryCodec[ServerSentEvent] {
override def encode(value: ServerSentEvent): Chunk[Byte] =
Chunk.fromArray(value.encode.getBytes)
override def streamEncoder: ZPipeline[Any, Nothing, ServerSentEvent, Byte] =
ZPipeline.fromFunction(_.mapChunks(_.flatMap(encode)))
override def decode(whole: Chunk[Byte]): Either[DecodeError, ServerSentEvent] =
Left(DecodeError.UnsupportedSchema(ServerSentEvent.schema, "ServerSendEvent"))
override def streamDecoder: ZPipeline[Any, DecodeError, Byte, ServerSentEvent] =
ZPipeline.fromSink(ZSink.fail(DecodeError.UnsupportedSchema(ServerSentEvent.schema, "ServerSendEvent")))
}
val httpContentCodec: HttpContentCodec[ServerSentEvent] =
HttpContentCodec(
ListMap(
MediaType.text.`event-stream` -> binaryCodec
),
ServerSentEvent.schema
)
val httpCodec: HttpCodec[HttpCodecType.Content, ZStream[Any, Nothing, ServerSentEvent]] =
HttpCodec.contentStream(httpContentCodec)
from zio-http.
Related Issues (20)
- Provide easy default layer for `TestServer`
- Generated OpenAPI code does not include authentication information from the endpoint HOT 2
- Code gen schema import missing HOT 1
- Header name not quoted
- Only last response is generated into Endpoint code
- OpenAPI can't deserialize json with header correctly
- OpenAPIGen fails on PathCodec.empty Root path HOT 4
- Attaching HandlerAspect to a Routes object with mulitple dependencies does not work (using Scala2) HOT 7
- Default encoding of `Enpoint.outStream[X]` should be JSON array, or the Content Type should be `application/json-seq` HOT 4
- Multipart boundary generated by Body.fromMultipartFormUUID cannot be parsed by server HOT 5
- Publishing of 3.0.0 failed HOT 1
- Update `Alternator` and perhaps others to include implicits for scala 3 type updates HOT 3
- Optional Codecs are required in OpenApi
- It would be great if one could specify more than 8 error types using the outErrors method of Endpoint
- Adding ?? Doc.p(...) to an Endpoint with a tag overwrites the tag HOT 1
- Adding Doc to a PathCodec puts the documentation in the endpoint method description and not the parameter description. HOT 3
- Flag ZClient.request as deprecated HOT 2
- Using a handled handler with error handling functions causes a ClassCastException
- Pass metadata when using HttpCodec.binaryStream HOT 3
- Customize `HttpCodec` errors with `Endpoint` api HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zio-http.