diff --git a/.changeset/message-limiter-frame-class.md b/.changeset/message-limiter-frame-class.md
new file mode 100644
index 000000000..853d7c8fe
--- /dev/null
+++ b/.changeset/message-limiter-frame-class.md
@@ -0,0 +1,6 @@
+---
+---
+
+fix(relay): key the message limiter by the frame's type
+
+The message level bucketed `{address}:message:60000`, so an EVENT burst spent the REQ budget and refused mixed traffic the capture allows. The key now carries the frame's class, read before decoding from the first 32 bytes against the capture's closed set, with one shared `OTHER` bucket. No published package changes.
diff --git a/crates/nostream-relay/src/limits.rs b/crates/nostream-relay/src/limits.rs
index 9b20f0715..2b9f12ccc 100644
--- a/crates/nostream-relay/src/limits.rs
+++ b/crates/nostream-relay/src/limits.rs
@@ -61,6 +61,87 @@ impl From<&EventRateLimit> for RateLimitConfig {
}
}
+/// The frame class the message limiter buckets by (`web-socket-adapter.ts:48-68`): the
+/// capture's closed set plus the one bucket everything unrecognised shares.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum MessageClass {
+ Req,
+ Event,
+ Count,
+ Close,
+ Auth,
+ NegOpen,
+ NegMsg,
+ NegClose,
+ Other,
+}
+
+impl MessageClass {
+ /// The class a raw frame belongs to, read from its first 32 bytes before decoding
+ /// (`web-socket-adapter.ts:59-68`): leading whitespace, `[`, optional whitespace, then a
+ /// quoted 1–8 character `[A-Za-z-]` word, upper-cased. Anything else is
+ /// [`MessageClass::Other`] — including `NEG-CLOSE`, whose nine characters exceed the
+ /// capture's eight-character bound.
+ pub fn from_frame(frame: &str) -> Self {
+ let mut end = frame.len().min(32);
+
+ while !frame.is_char_boundary(end) {
+ end -= 1;
+ }
+
+ let mut chars = frame[..end].chars().skip_while(|c| c.is_whitespace());
+
+ if chars.next() != Some('[') {
+ return Self::Other;
+ }
+
+ let mut chars = chars.skip_while(|c| c.is_whitespace());
+
+ if chars.next() != Some('"') {
+ return Self::Other;
+ }
+
+ let mut word = String::new();
+
+ loop {
+ match chars.next() {
+ Some('"') if !word.is_empty() => break,
+ Some(c) if word.len() < 8 && (c.is_ascii_alphabetic() || c == '-') => {
+ word.push(c.to_ascii_uppercase());
+ }
+ _ => return Self::Other,
+ }
+ }
+
+ match word.as_str() {
+ "REQ" => Self::Req,
+ "EVENT" => Self::Event,
+ "COUNT" => Self::Count,
+ "CLOSE" => Self::Close,
+ "AUTH" => Self::Auth,
+ "NEG-OPEN" => Self::NegOpen,
+ "NEG-MSG" => Self::NegMsg,
+ "NEG-CLOSE" => Self::NegClose,
+ _ => Self::Other,
+ }
+ }
+
+ /// The token this class contributes to the limiter key.
+ pub fn as_str(&self) -> &'static str {
+ match self {
+ Self::Req => "REQ",
+ Self::Event => "EVENT",
+ Self::Count => "COUNT",
+ Self::Close => "CLOSE",
+ Self::Auth => "AUTH",
+ Self::NegOpen => "NEG-OPEN",
+ Self::NegMsg => "NEG-MSG",
+ Self::NegClose => "NEG-CLOSE",
+ Self::Other => "OTHER",
+ }
+ }
+}
+
/// One sliding-window hit: the instant it was recorded and the weight it carries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SlidingEntry {
@@ -514,10 +595,15 @@ impl RateLimits {
Ok(limited)
}
- /// `limits.message.rateLimits` with its `ipWhitelist` bypass
- /// (`web-socket-adapter.ts:264-282`). `Ok(Some(backoff))` is the message not being
- /// handled; `Ok(None)` is allowed.
- pub fn message_backoff(&self, address: &str) -> Result