WebSocket frame codec (RFC 6455)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-websocket: enable MDX on lib/websocket.mli

Run mdx on lib/websocket.mli so the {[ ... ]} odoc block now
type-checks the encode/decode round-trip.

The example used `_rest` to discard the trailing bytes (silencing the
unused-binding warning) and `assert false` on the Error branch
(losing the payload of `[`Need_more | `Invalid of string]`).

Switched the encode call to `~mask:false` so a server-side frame is
emitted, asserted that decode consumes the full buffer (`Ok (frame,
"")`) and the payload round-trips, and surfaced both error branches
via Fmt.failwith with their constructor data so a future regression
prints a useful message.

+12 -3
+4
lib/dune
··· 2 2 (name websocket) 3 3 (public_name nox-websocket) 4 4 (libraries fmt logs base64 digestif nox-crypto-rng nox-http)) 5 + 6 + (mdx 7 + (files websocket.mli) 8 + (libraries nox-websocket fmt))
+8 -3
lib/websocket.mli
··· 13 13 14 14 {[ 15 15 let frame = Websocket.text "hello" 16 - let bytes = Websocket.encode frame 16 + 17 + (* Server frames are unmasked. *) 18 + let bytes = Websocket.encode ~mask:false frame 17 19 18 20 let () = 19 21 match Websocket.decode bytes with 20 - | Ok (frame, _rest) -> assert (frame.payload = "hello") 21 - | Error _ -> assert false 22 + | Ok (frame, "") -> assert (frame.payload = "hello") 23 + | Ok (_, leftover) -> 24 + Fmt.failwith "unexpected %d trailing bytes" (String.length leftover) 25 + | Error (`Invalid e) -> Fmt.failwith "decode: %s" e 26 + | Error `Need_more -> Fmt.failwith "decode: need more" 22 27 ]} *) 23 28 24 29 module Handshake = Handshake