|
| 1 | +import type { ISessionTicketStore } from "@univerjs-pro/collaboration-endpoint"; |
| 2 | +import type { |
| 3 | + NodeTransportConnection, |
| 4 | + NodeTransportEndpoint, |
| 5 | +} from "@univerjs-pro/collaboration-transport-node"; |
| 6 | +import type { WorktreeProductChange } from "../../modules/worktrees/index.js"; |
| 7 | + |
| 8 | +export const WORKTREE_CHANGE_FEED_PATH = "/api/worktree-events"; |
| 9 | + |
| 10 | +export interface WorktreeChangeFeed { |
| 11 | + endpoint(ticketStore: ISessionTicketStore): NodeTransportEndpoint; |
| 12 | + publish(change: WorktreeProductChange): void; |
| 13 | + dispose(): Promise<void>; |
| 14 | +} |
| 15 | + |
| 16 | +export function createWorktreeChangeFeed(): WorktreeChangeFeed { |
| 17 | + const connections = new Map< |
| 18 | + string, |
| 19 | + Map<string, NodeTransportConnection> |
| 20 | + >(); |
| 21 | + let disposed = false; |
| 22 | + let endpointCreated = false; |
| 23 | + |
| 24 | + return { |
| 25 | + endpoint(ticketStore) { |
| 26 | + if (endpointCreated) { |
| 27 | + throw new Error("Worktree change feed endpoint is already created."); |
| 28 | + } |
| 29 | + endpointCreated = true; |
| 30 | + return { |
| 31 | + async handleUpgrade(context, next) { |
| 32 | + const url = new URL( |
| 33 | + context.incomingMessage.url ?? "/", |
| 34 | + "http://localhost" |
| 35 | + ); |
| 36 | + if (url.pathname !== WORKTREE_CHANGE_FEED_PATH) { |
| 37 | + await next(); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (disposed) { |
| 41 | + context.reject(503, "Worktree change feed is unavailable"); |
| 42 | + return; |
| 43 | + } |
| 44 | + const ticket = await ticketStore.consume( |
| 45 | + url.searchParams.get("sessionTicket") ?? "" |
| 46 | + ); |
| 47 | + if (!ticket) { |
| 48 | + context.reject(401, "Invalid or expired session ticket"); |
| 49 | + return; |
| 50 | + } |
| 51 | + context.accept({ |
| 52 | + open({ connection }) { |
| 53 | + const userConnections = |
| 54 | + connections.get(ticket.userID) ?? new Map(); |
| 55 | + userConnections.set(connection.id, connection); |
| 56 | + connections.set(ticket.userID, userConnections); |
| 57 | + safeSend( |
| 58 | + connection, |
| 59 | + JSON.stringify({ event: "worktreeChangeFeedReady" }) |
| 60 | + ); |
| 61 | + }, |
| 62 | + message({ connection }) { |
| 63 | + connection.close(1003, "Worktree change feed is server-only"); |
| 64 | + }, |
| 65 | + close({ connection }) { |
| 66 | + removeConnection(ticket.userID, connection.id); |
| 67 | + }, |
| 68 | + }); |
| 69 | + }, |
| 70 | + dispose: async () => { |
| 71 | + await dispose(); |
| 72 | + }, |
| 73 | + }; |
| 74 | + }, |
| 75 | + |
| 76 | + publish(change) { |
| 77 | + if (disposed) return; |
| 78 | + const message = JSON.stringify({ event: "worktreesChanged" }); |
| 79 | + for (const userId of change.audienceUserIds) { |
| 80 | + for (const connection of connections.get(userId)?.values() ?? []) { |
| 81 | + safeSend(connection, message); |
| 82 | + } |
| 83 | + } |
| 84 | + }, |
| 85 | + |
| 86 | + dispose, |
| 87 | + }; |
| 88 | + |
| 89 | + function removeConnection(userId: string, connectionId: string): void { |
| 90 | + const userConnections = connections.get(userId); |
| 91 | + userConnections?.delete(connectionId); |
| 92 | + if (userConnections?.size === 0) connections.delete(userId); |
| 93 | + } |
| 94 | + |
| 95 | + async function dispose(): Promise<void> { |
| 96 | + if (disposed) return; |
| 97 | + disposed = true; |
| 98 | + for (const userConnections of connections.values()) { |
| 99 | + for (const connection of userConnections.values()) { |
| 100 | + connection.close(1001, "Worktree change feed disposed"); |
| 101 | + } |
| 102 | + } |
| 103 | + connections.clear(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function safeSend(connection: NodeTransportConnection, message: string): void { |
| 108 | + void Promise.resolve(connection.send(message)).catch(() => { |
| 109 | + connection.close(1011, "Worktree change delivery failed"); |
| 110 | + }); |
| 111 | +} |
0 commit comments