11import type { Announced } from "../announced.ts" ;
2- import type { Bandwidth } from "../bandwidth.ts" ;
2+ import { type Bandwidth , createBandwidth } from "../bandwidth.ts" ;
33import type { Broadcast } from "../broadcast.ts" ;
44import type { Established } from "../connection/established.ts" ;
55import * as Path from "../path.ts" ;
@@ -66,12 +66,12 @@ export class Connection implements Established {
6666 // Send bandwidth is version-agnostic: depends on browser/QUIC support.
6767 const hasGetStats = typeof ( quic as unknown as { getStats ?: unknown } ) . getStats === "function" ;
6868 if ( hasGetStats ) {
69- this . sendBandwidth = new Bandwidth ( ) ;
69+ this . sendBandwidth = createBandwidth ( ) ;
7070 }
7171
7272 // Recv bandwidth requires PROBE support (not available in older drafts).
7373 if ( version !== Version . DRAFT_01 && version !== Version . DRAFT_02 ) {
74- this . recvBandwidth = new Bandwidth ( ) ;
74+ this . recvBandwidth = createBandwidth ( ) ;
7575 }
7676
7777 this . #publisher = new Publisher ( this . #quic, this . #version) ;
@@ -115,47 +115,27 @@ export class Connection implements Established {
115115 }
116116 }
117117
118- /**
119- * Publishes a broadcast to the connection.
120- * @param name - The broadcast path to publish
121- * @param broadcast - The broadcast to publish
122- */
123118 publish ( path : Path . Valid , broadcast : Broadcast ) {
124119 this . #publisher. publish ( path , broadcast ) ;
125120 }
126121
127- /**
128- * Gets the next announced broadcast.
129- */
130122 announced ( prefix = Path . empty ( ) ) : Announced {
131123 return this . #subscriber. announced ( prefix ) ;
132124 }
133125
134- /**
135- * Consumes a broadcast from the connection.
136- *
137- * @remarks
138- * If the broadcast is not found, a "not found" error will be thrown when requesting any tracks.
139- *
140- * @param broadcast - The path of the broadcast to consume
141- * @returns A Broadcast instance
142- */
143126 consume ( broadcast : Path . Valid ) : Broadcast {
144127 return this . #subscriber. consume ( broadcast ) ;
145128 }
146129
147130 async #runSession( ) {
148131 if ( ! this . #session) {
149- // moq-lite draft-03 doesn't use a session stream.
150132 return ;
151133 }
152134
153135 try {
154- // Receive messages until the connection is closed.
155136 for ( ; ; ) {
156137 const msg = await SessionInfo . decodeMaybe ( this . #session. reader , this . #version) ;
157138 if ( ! msg ) break ;
158- // TODO use the session info
159139 }
160140 } finally {
161141 console . debug ( "session stream closed" ) ;
@@ -165,9 +145,7 @@ export class Connection implements Established {
165145 async #runBidis( ) {
166146 for ( ; ; ) {
167147 const stream = await Stream . accept ( this . #quic) ;
168- if ( ! stream ) {
169- break ;
170- }
148+ if ( ! stream ) break ;
171149
172150 this . #runBidi( stream )
173151 . catch ( ( err : unknown ) => {
@@ -187,14 +165,11 @@ export class Connection implements Established {
187165 } else if ( typ === StreamId . Announce ) {
188166 const msg = await AnnounceInterest . decode ( stream . reader ) ;
189167 await this . #publisher. runAnnounce ( msg , stream ) ;
190- return ;
191168 } else if ( typ === StreamId . Subscribe ) {
192169 const msg = await Subscribe . decode ( stream . reader , this . #version) ;
193170 await this . #publisher. runSubscribe ( msg , stream ) ;
194- return ;
195171 } else if ( typ === StreamId . Probe ) {
196172 await this . #publisher. runProbe ( stream ) ;
197- return ;
198173 } else {
199174 throw new Error ( `unknown stream type: ${ typ . toString ( ) } ` ) ;
200175 }
@@ -205,9 +180,7 @@ export class Connection implements Established {
205180
206181 for ( ; ; ) {
207182 const stream = await readers . next ( ) ;
208- if ( ! stream ) {
209- break ;
210- }
183+ if ( ! stream ) break ;
211184
212185 this . #runUni( stream )
213186 . then ( ( ) => {
@@ -229,12 +202,7 @@ export class Connection implements Established {
229202 }
230203 }
231204
232- /**
233- * Polls the QUIC congestion controller for estimated send rate.
234- * Resolves when the connection is closed.
235- */
236205 async #runSendBandwidth( bandwidth : Bandwidth ) : Promise < void > {
237- // getStats is not yet in the TypeScript WebTransport type definitions.
238206 const quic = this . #quic as unknown as {
239207 getStats : ( ) => Promise < { estimatedSendRate : number | null } > ;
240208 } ;
@@ -245,7 +213,6 @@ export class Connection implements Established {
245213 const stats = await quic . getStats ( ) ;
246214 bandwidth . set ( stats . estimatedSendRate ?? undefined ) ;
247215 } catch {
248- // Connection likely closed.
249216 clearInterval ( id ) ;
250217 resolve ( ) ;
251218 }
@@ -258,10 +225,6 @@ export class Connection implements Established {
258225 } ) ;
259226 }
260227
261- /**
262- * Returns a promise that resolves when the connection is closed.
263- * @returns A promise that resolves when closed
264- */
265228 get closed ( ) : Promise < void > {
266229 return this . #quic. closed . then ( ( ) => undefined ) ;
267230 }
0 commit comments