From 5526e7c48da4391850ff739f9d656e0d192bb46b Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Fri, 25 Sep 2026 14:49:00 -0400 Subject: [PATCH] feat(live): parse stream.place links and build playback urls --- .../streamplace/__tests__/url.test.ts | 39 +++++++++++++++++++ src/features/streamplace/url.ts | 29 ++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/features/streamplace/__tests__/url.test.ts create mode 100644 src/features/streamplace/url.ts diff --git a/src/features/streamplace/__tests__/url.test.ts b/src/features/streamplace/__tests__/url.test.ts new file mode 100644 index 000000000..3213658f2 --- /dev/null +++ b/src/features/streamplace/__tests__/url.test.ts @@ -0,0 +1,39 @@ +import { + livePlaylistUrl, + liveSocketUrl, + parseStreamplaceActor, +} from '../url' + +describe('parseStreamplaceActor', () => { + it.each([ + ['https://stream.place/alice.bsky.social', 'alice.bsky.social'], + ['https://stream.place/@Alice.Bsky.Social', 'alice.bsky.social'], + ['https://www.stream.place/alice.bsky.social/', 'alice.bsky.social'], + ['https://stream.place/embed/alice.bsky.social', 'alice.bsky.social'], + ['https://stream.place/did:plc:7icioco6iksxt3nl2oxpkj2u', 'did:plc:7icioco6iksxt3nl2oxpkj2u'], + ])('parses %s', (url, actor) => { + expect(parseStreamplaceActor(url)).toBe(actor) + }) + + it.each([ + 'https://stream.place/', + 'https://stream.place/about', + 'https://stream.place/docs', + 'https://stream.place/alice.bsky.social/video/3abc', + 'https://example.com/alice.bsky.social', + 'not a url', + ])('rejects %s', url => { + expect(parseStreamplaceActor(url)).toBeUndefined() + }) +}) + +describe('urls', () => { + it('builds the playlist and socket urls', () => { + expect(livePlaylistUrl('did:plc:abc')).toBe( + 'https://stream.place/xrpc/place.stream.playback.getLivePlaylist?streamer=did:plc:abc', + ) + expect(liveSocketUrl('did:plc:abc')).toBe( + 'wss://stream.place/api/websocket/did:plc:abc', + ) + }) +}) diff --git a/src/features/streamplace/url.ts b/src/features/streamplace/url.ts new file mode 100644 index 000000000..248aa0695 --- /dev/null +++ b/src/features/streamplace/url.ts @@ -0,0 +1,29 @@ +export const STREAMPLACE_ORIGIN = 'https://stream.place' + +const HOSTS = new Set(['stream.place', 'www.stream.place']) +const ACTOR_RE = + /^(did:[a-z]+:[a-zA-Z0-9._:-]+|[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+)$/ + +export function parseStreamplaceActor(url: string): string | undefined { + let urlp: URL + try { + urlp = new URL(url) + } catch { + return undefined + } + if (!HOSTS.has(urlp.hostname)) return undefined + const parts = urlp.pathname.split('/').filter(Boolean) + if (parts[0] === 'embed') parts.shift() + if (parts.length !== 1) return undefined + const actor = decodeURIComponent(parts[0]).replace(/^@/, '') + if (!ACTOR_RE.test(actor)) return undefined + return actor.startsWith('did:') ? actor : actor.toLowerCase() +} + +export function livePlaylistUrl(actor: string) { + return `${STREAMPLACE_ORIGIN}/xrpc/place.stream.playback.getLivePlaylist?streamer=${actor}` +} + +export function liveSocketUrl(actor: string) { + return `wss://stream.place/api/websocket/${actor}` +} -- knotmirror 0.0.1