| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! Cell-isolate dispatch decisions, reified sans-IO. A cell isolate runs one |
| 4 | //! event at a time. A top-level Worker fetch takes the resident-isolate fast |
| 5 | //! path only when the isolate is idle; if the isolate is already pumping an |
| 6 | //! actor event, the fetch must reschedule to the stateless Worker pool — never |
| 7 | //! run nested — carrying its request identity so the reply still lands |
| 8 | //! (`js.rs`). The executor and the production run loop hold the isolate |
| 9 | //! channels and the pool; this is the pure routing they consult, so a |
| 10 | //! deterministic executor can drive it directly. |
| 11 | //! |
| 12 | //! Small protocol sequencing choices also live here when the executor owns |
| 13 | //! the bytes but not the decision. That keeps the shell mechanical and lets |
| 14 | //! callers exercise the same branch production takes. |
| 15 | |
| 16 | /// Return whether a close code can appear in a WebSocket close frame. |
| 17 | /// |
| 18 | /// RFC 6455 uses 1005, 1006, and 1015 only for local reporting, so an endpoint |
| 19 | /// cannot put them on the wire. Code 1004 is reserved. The IANA registry |
| 20 | /// assigns the remaining standard codes through 1014, and codes from 3000 |
| 21 | /// through 4999 are available to applications and libraries. |
| 22 | pub fn websocket_close_code_is_allowed(code: u16) -> bool { |
| 23 | matches!(code, 1000..=1003 | 1007..=1014 | 3000..=4999) |
| 24 | } |
| 25 | |
| 26 | /// Select the protocol close code the shell must echo after the application |
| 27 | /// close handler has run and all of its output has been written. |
| 28 | /// |
| 29 | /// An application-selected close wins. Otherwise RFC 6455 requires a clean |
| 30 | /// peer close to receive a close response; 1005 is the local sentinel for a |
| 31 | /// frame with no status and cannot itself appear on the wire, so it becomes |
| 32 | /// the normal close code 1000. A parsed protocol failure receives its permitted |
| 33 | /// error code, while an abnormal transport end receives no frame. |
| 34 | pub fn websocket_echo_close( |
| 35 | peer_code: u16, |
| 36 | peer_was_clean: bool, |
| 37 | handler_sent_close: bool, |
| 38 | ) -> Option<u16> { |
| 39 | if handler_sent_close { |
| 40 | None |
| 41 | } else if !peer_was_clean { |
| 42 | matches!(peer_code, 1002 | 1007).then_some(peer_code) |
| 43 | } else if peer_code == 1005 { |
| 44 | Some(1000) |
| 45 | } else { |
| 46 | Some(peer_code) |
| 47 | } |
| 48 | } |