diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index 907647f4e2f..a7302acad65 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -630,6 +630,7 @@ export namespace Socket { readonly producer: Track.Producer; readonly session: string; } + export function isEndpoint(path: Path.Valid): boolean; } export const StreamCode: ((code: number) => StreamCode) & { diff --git a/js/net/src/socket.test.ts b/js/net/src/socket.test.ts index 8b78365474c..cd76e168adb 100644 --- a/js/net/src/socket.test.ts +++ b/js/net/src/socket.test.ts @@ -66,6 +66,15 @@ test("Socket namespace exposes endpoint and both stream roles", () => { expect(Socket.TrackStream).toBeFunction(); }); +test("isEndpoint tells an endpoint's broadcast from media", () => { + for (const role of [".server", ".client"]) { + expect(Socket.isEndpoint(Path.from("room", role, "alice"))).toBeTrue(); + } + for (const path of ["room/alice", ".server", "room/.server", "room/.server/alice/cam", "room/server/alice"]) { + expect(Socket.isEndpoint(Path.from(path))).toBeFalse(); + } +}); + test("an endpoint closes with its origin before it dials or accepts", async () => { const origin = new Origin.Producer(); const endpoint = new Socket.Endpoint(origin, Path.from("room"), "alice"); diff --git a/js/net/src/socket.ts b/js/net/src/socket.ts index 7fb2c1bb7a8..67a437ab4ba 100644 --- a/js/net/src/socket.ts +++ b/js/net/src/socket.ts @@ -45,6 +45,15 @@ function endpointPath(prefix: Path.Valid, role: Role, id: string): Path.Valid { return Path.encode(Path.from(prefix, SEGMENT[role], id)); } +/** + * Whether `path` is an endpoint's broadcast, which only routes sessions and carries no + * media, so a worker that acts on every announced broadcast can skip it. + */ +export function isEndpoint(path: Path.Valid): boolean { + const parent = path.split("/").at(-2); + return Object.values(SEGMENT).some((role) => parent === role); +} + function parse(name: string): { protocol: string; dialer: string; acceptor: string; session: string } | undefined { const parts = name.split("/"); if (parts.length !== 4 || parts.some((part) => !part || part.includes("\\"))) return; diff --git a/rs/moq-net/src/model/socket.rs b/rs/moq-net/src/model/socket.rs index 92a484ac21c..e6e9d1876f2 100644 --- a/rs/moq-net/src/model/socket.rs +++ b/rs/moq-net/src/model/socket.rs @@ -72,6 +72,15 @@ fn endpoint_path(prefix: &Path, role: Role, id: &str) -> Result { Ok(prefix.join(&Path::from_static(role.segment()))?.join(&Path::new(id)?)?) } +/// Whether `path` is an endpoint's broadcast, which only routes sessions and carries no +/// media, so a worker that acts on every announced broadcast can skip it. +pub fn is_endpoint(path: &Path) -> bool { + let parent = path.as_str().rsplit('/').nth(1); + [Role::Server, Role::Client] + .into_iter() + .any(|role| parent == Some(role.segment())) +} + /// A fresh session id: 128 random bits as hex, so nobody can guess a session's tracks. fn session_id() -> Arc { use rand::RngExt; @@ -1397,10 +1406,20 @@ mod tests { "room/.client/alice" ); for role in [Role::Server, Role::Client] { + assert!(is_endpoint(&endpoint_path(&prefix, role, "alice").unwrap())); assert!(endpoint_path(&prefix, role, "a/b").is_err()); assert!(endpoint_path(&prefix, role, "").is_err()); assert!(endpoint_path(&prefix, role, "a\\b").is_err()); } + for path in [ + "room/alice", + ".server", + "room/.server", + "room/.server/alice/cam", + "room/server/alice", + ] { + assert!(!is_endpoint(&Path::new(path).unwrap()), "{path}"); + } } /// Taking a dial wakes the dialer's subscription, and an inline executor can re-enter