Coder Social home page Coder Social logo

Comments (5)

guersam avatar guersam commented on September 15, 2024

Something similar happens with RC6 for Server Sent Events. (scastie)

from zio-http.

guersam avatar guersam commented on September 15, 2024

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.

guersam avatar guersam commented on September 15, 2024

I think I've spotted the cause:

  1. Endpoint#outStream[A] summons an implicit HttpContentCodec (code)
  2. New HttpContentCodec is created using the existing schema, with application/json as the first candidate (code)
  3. The JSON codec is picked up
  4. The encoder checks if the default media type is binary (code)
  5. JSON is defined as a binary codec (code)
  6. The encoder unsafely casts the stream as byte-emitting without properly encoding it (code)
  7. NettyWriter crashes as Body.StreamBody assumes it contains a byte stream

from zio-http.

guersam avatar guersam commented on September 15, 2024

application/json has been treated as binary since 2022, unlike the other JSON variants. I think it's just a mistake.

from zio-http.

guersam avatar guersam commented on September 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.