| 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 | } |
| 49 | |
| 50 | /// Track frame boundaries in the unmasked stream from a WebSocket owner. |
| 51 | /// |
| 52 | /// A byte-splicing hop can append a failure Close only between frames and |
| 53 | /// before any owner Close. Otherwise a second Close fails browser channels, |
| 54 | /// or the injected bytes become part of an incomplete frame's payload. |
| 55 | /// Payloads are skipped without buffering; this is not a protocol validator. |
| 56 | #[derive(Debug, Default)] |
| 57 | pub struct WebSocketCloseScanner { |
| 58 | state: WebSocketScanState, |
| 59 | } |
| 60 | |
| 61 | #[derive(Debug, Default)] |
| 62 | enum WebSocketScanState { |
| 63 | #[default] |
| 64 | Opcode, |
| 65 | Length, |
| 66 | ExtendedLength { |
| 67 | remaining: u8, |
| 68 | value: u64, |
| 69 | }, |
| 70 | Payload { |
| 71 | remaining: u64, |
| 72 | }, |
| 73 | // A Close has started, or a masked/invalid-length owner frame makes |
| 74 | // appending a server frame unsafe. Never resume scanning this stream. |
| 75 | Stopped, |
| 76 | } |
| 77 | |
| 78 | impl WebSocketCloseScanner { |
| 79 | /// Observe bytes successfully written to the client, in stream order. |
| 80 | pub fn observe(&mut self, mut bytes: &[u8]) { |
| 81 | use WebSocketScanState::*; |
| 82 | while let Some((&byte, rest)) = bytes.split_first() { |
| 83 | match &mut self.state { |
| 84 | Opcode => { |
| 85 | self.state = if byte & 0x0f == 8 { Stopped } else { Length }; |
| 86 | } |
| 87 | Length => { |
| 88 | self.state = match byte { |
| 89 | 0 => Opcode, |
| 90 | 1..=125 => Payload { |
| 91 | remaining: u64::from(byte), |
| 92 | }, |
| 93 | 126 => ExtendedLength { |
| 94 | remaining: 2, |
| 95 | value: 0, |
| 96 | }, |
| 97 | 127 => ExtendedLength { |
| 98 | remaining: 8, |
| 99 | value: 0, |
| 100 | }, |
| 101 | // A server must not mask its frames. |
| 102 | _ => Stopped, |
| 103 | }; |
| 104 | } |
| 105 | ExtendedLength { remaining, value } => { |
| 106 | // RFC 6455 limits extended lengths to 63 bits. |
| 107 | if *remaining == 8 && byte & 0x80 != 0 { |
| 108 | self.state = Stopped; |
| 109 | } else { |
| 110 | *value = (*value << 8) | u64::from(byte); |
| 111 | *remaining -= 1; |
| 112 | if *remaining == 0 { |
| 113 | self.state = if *value == 0 { |
| 114 | Opcode |
| 115 | } else { |
| 116 | Payload { remaining: *value } |
| 117 | }; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | Payload { remaining } => { |
| 122 | let count = (*remaining).min(bytes.len() as u64) as usize; |
| 123 | *remaining -= count as u64; |
| 124 | bytes = &bytes[count..]; |
| 125 | if *remaining == 0 { |
| 126 | self.state = Opcode; |
| 127 | } |
| 128 | continue; |
| 129 | } |
| 130 | Stopped => return, |
| 131 | } |
| 132 | bytes = rest; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Whether the stream can accept a synthetic Close at its current end. |
| 137 | pub fn can_append_close(&self) -> bool { |
| 138 | matches!(self.state, WebSocketScanState::Opcode) |
| 139 | } |
| 140 | } |