internal/webfetchhelp/text/framed.txt

FRAMED WEBSOCKET CONTROLLER PROTOCOL

USAGE
  controller | ez-agent-webfetch --ws-mode framed [OPTIONS] ws://URL

Framed mode preserves complete application-message boundaries and per-message
text/binary type while streaming bounded chunks. Records are local semantic
chunks, not raw RFC 6455 frames. The WebSocket library owns masking, compression,
opcodes, wire fragmentation, continuation frames, and FIN.

GRAMMAR
  record  = kind length "-" payload LF
  kind    = "O" / "T" / "B" / "D" / "E" / "C"
  length  = 1*HEXDIG
  payload = exactly <length> bytes
  LF      = one byte 0x0a

Length counts bytes, not Unicode characters. Input hex may be upper/lowercase;
output is canonical lowercase without 0x. Input accepts leading zeroes. At most
16 hex digits are accepted. Values above the signed-63-bit ceiling,
0x7fffffffffffffff (9,223,372,036,854,775,807 bytes), are rejected, and cumulative T/B/D
sizes are added with overflow checks. The payload is streamed without allocating its declared size, then exactly one LF is
required. Payload may contain LF, hyphen, NUL, or arbitrary bytes. CRLF is not LF;
a CR is valid only when counted in payload. Empty payload is encoded as `0-\n`.
Output data records are at most 65,536 bytes; input records may be larger but
65,536-or-smaller chunks are recommended for responsive cancellation.

DIRECTION AND STATE
  Kind  stdin  stdout  Meaning
  O     no     yes     Handshake succeeded; compact JSON metadata.
  T     yes    yes     Start one text message; carry its first chunk.
  B     yes    yes     Start one binary message; carry its first chunk.
  D     yes    yes     Continue the currently open message.
  E     yes    yes     End/commit the open message; payload must be empty.
  C     yes    yes     Initiate or report connection closure.

T/B/D/E MESSAGES
  T or B is valid only with no message open and selects that message's type. D and
  E require an open message. Zero or more D records may follow T/B, and exactly one
  E0- record is required to commit the complete WebSocket application message.
  The next message independently chooses T or B, so mixed types are supported.
  An empty message is T0- or B0- followed by E0-.

  Text validity applies across the concatenated T and D payloads; a UTF-8 sequence
  may cross record boundaries and must be complete at E. Binary is transparent.
  A positive --ws-max-message limits cumulative T/B/D bytes in both directions;
  zero is unlimited. Overflow, invalid UTF-8, size excess, malformed framing, EOF
  before E, or another bad transition force-closes without committing an active
  outgoing writer. C is valid on stdin only between messages. Input O, unknown
  kinds, D/E without a message, nested T/B, nonempty E, and C during a message fail.

  For a peer message, stdout gets T or B with its first bounded chunk, D for later
  chunks, and E0- only after clean message EOF and text validation. Empty peer
  messages get an empty T/B then E. If late peer text is invalid, prior chunks may
  remain but E is omitted, proving the message was incomplete before terminal C.

O — OPENED OUTPUT
  After a successful handshake and before application records, stdout receives
  exactly one O record containing compact UTF-8 JSON:

      {"protocol":"chat","extensions":"permessage-deflate"}

  protocol is the selected subprotocol or "". extensions is the negotiated
  Sec-WebSocket-Extensions value or "" and is nonempty only after explicitly
  enabled compression. The example JSON is 53 bytes (hex 35), so its record is:

      O35-{"protocol":"chat","extensions":"permessage-deflate"}\n
  Failed handshake emits no O; its diagnostic is stderr-only.

C — CLOSE INPUT
  Input C contains compact UTF-8 JSON:

      {"code":1000,"reason":"finished"}

  code is optional. Accepted transmitted codes are 1000-1014 except reserved
  1004, event-only 1005/1006, plus application codes 3000-4999. reason is
  optional, valid UTF-8, and at most 123 encoded bytes. If code is omitted,
  reason must be empty/omitted and a code-less Close is sent. The JSON must be
  compact (no insignificant whitespace), duplicate and unknown fields are
  rejected, and field types are strict. After input C, no application input is
  accepted. Local 1000, 1001, and code-less closure can exit successfully;
  other local status codes are reported and exit nonzero.

C — TERMINAL OUTPUT
  Output C contains compact UTF-8 JSON:

      {"code":1000,"reason":"finished","initiator":"peer"}

  initiator is peer (peer Close observed), local (input C, EOF-idle close, or local
  validation close), or transport (no clean close possible/observed). Event-only
  1005 represents code-less close and 1006 abnormal transport; neither is sent on
  the wire. Local validation uses an appropriate wire code such as 1002, 1007, or
  1009 and still exits 1. After O, at most one terminal C is emitted when stdout
  remains writable. Runtime failures are also explained on stderr.

EOF AND CLOSE
  EOF is valid only between complete records while no message is open. EOF inside
  a header/payload/trailing LF or before E is a framing error and does not commit
  the message. Plain valid EOF stops sends and enters passive receive-idle behavior
  controlled by --ws-eof-timeout; idle expiry initiates normal 1000 close. An
  explicit input C is preferred. Zero EOF timeout waits indefinitely.

EXAMPLES
  One text message "hello world":
      T5-hello\n
      D6- world\n
      E0-\n

  One empty binary message, then text "ok":
      B0-\n
      E0-\n
      T2-ok\n
      E0-\n

  Generate lengths from bytes rather than visually counted placeholders. For
  example, a shell controller can produce a complete text message with:

      payload='hello world'
      printf 'T%x-' "${#payload}"
      printf '%s\n' "$payload"
      printf 'E0-\n'

  For non-ASCII or binary payloads, compute the byte count in a byte-oriented
  language/tool; shell character counts may not equal encoded byte counts.