From 06643d378e8c61cc7ace30b08d47bcbc58f1bdd3 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 15:05:36 +0800 Subject: [PATCH] Import packages/webrtc/src/control-lease.ts --- packages/webrtc/src/control-lease.ts | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 packages/webrtc/src/control-lease.ts diff --git a/packages/webrtc/src/control-lease.ts b/packages/webrtc/src/control-lease.ts new file mode 100644 index 0000000..24991c0 --- /dev/null +++ b/packages/webrtc/src/control-lease.ts @@ -0,0 +1,78 @@ +export interface ControlLeaseAck { connectionId: string; requestId: string; ticket: string; leaseMs: number } + +const WEBRTC_LEASE_MS = 300_000 + +/** A connection authorization deadline; socket activity alone never renews it. */ +export class ControlLease { + private confirmed = false + private stopped = false + private sent?: { id: string; mono: number; wall: number } + private deadline?: { mono: number; wall: number } + private sequence = 0 + private timer?: ReturnType + private lastRequest = -Infinity + + constructor( + readonly connectionId: string, + private ticket: string, + private readonly request: (payload: { connectionId: string; requestId: string; ticket: string }) => void, + private readonly expired: () => void, + ) {} + + start(): void { + this.timer = setInterval(() => this.tick(), 1_000) + // Bound first confirmation too, but it never permits detached operation. + this.deadline = { mono: performance.now() + WEBRTC_LEASE_MS, wall: Date.now() + WEBRTC_LEASE_MS } + this.tick(true) + } + + valid(): boolean { + if (this.stopped) return false + const d = this.deadline + if (d && (performance.now() >= d.mono || Date.now() >= d.wall)) { + this.stop() + this.expired() + return false + } + return true + } + + remainingMs(): number | undefined { + if (!this.confirmed || this.stopped || !this.deadline) return undefined + return Math.max(0, Math.min(this.deadline.mono - performance.now(), this.deadline.wall - Date.now())) + } + + canDetach(): boolean { return this.confirmed && this.valid() } + recoveryTicket(): string | undefined { return this.canDetach() ? this.ticket : undefined } + + tick(force = false): void { + if (!this.valid()) return + const mono = performance.now() + if (!force && mono - this.lastRequest < 25_000) return + const sent = { id: String(++this.sequence), mono, wall: Date.now() } + this.lastRequest = mono + try { + this.request({ connectionId: this.connectionId, requestId: sent.id, ticket: this.ticket }) + this.sent = sent + } catch { /* Keep the existing deadline while control is unavailable. */ } + } + + acknowledge(ack: ControlLeaseAck): void { + if (!this.valid() || ack.connectionId !== this.connectionId || ack.leaseMs !== WEBRTC_LEASE_MS) return + const sent = this.sent + if (!sent || sent.id !== ack.requestId) return + // Late replies cannot create a fresh five-minute window. + if (performance.now() - sent.mono >= 30_000 || Date.now() - sent.wall >= 30_000) return + this.sent = undefined + this.ticket = ack.ticket + this.confirmed = true + this.deadline = { mono: sent.mono + WEBRTC_LEASE_MS, wall: sent.wall + WEBRTC_LEASE_MS } + } + + stop(): void { + this.stopped = true + clearInterval(this.timer) + this.timer = undefined + this.sent = undefined + } +}