AWSEventStream.JSON (AWSEventStream v0.1.0)

Copy Markdown View Source

Optional Bedrock/JSON convenience layer over the core codec.

Classifies each frame by its :message-type header (event / exception / error) — before :event-type — into idiomatic tagged tuples, and unwraps Bedrock JSON payloads ({"bytes": base64} or direct JSON). Requires the optional :jason dependency.

Summary

Functions

Classify one decoded frame by its :message-type header.

Types

classified()

@type classified() ::
  {:event, String.t() | nil, map()}
  | {:exception, String.t() | nil, map()}
  | {:error, String.t() | nil, String.t() | nil}
  | {:malformed_payload, AWSEventStream.Message.t(), term()}

Functions

classify(msg)

@spec classify(AWSEventStream.Message.t()) :: classified()

Classify one decoded frame by its :message-type header.

A frame's classification comes from its headers, so :message-type is checked before :event-type. Crucially, an exception/error frame is always surfaced as {:exception, …} / {:error, …} — a payload that fails to decode never downgrades a server-side close to {:malformed_payload, …}, since a close may legitimately carry a non-JSON or empty body. For an exception frame whose body is not a JSON object, the raw bytes are preserved under "raw". Only event frames (the normal data path) surface {:malformed_payload, …} when their payload can't be decoded.

decode(buffer, opts \\ [])

@spec decode(
  binary(),
  keyword()
) :: {[classified() | {:malformed_frame, atom(), binary()}], binary()}