@@ -161,6 +161,7 @@ class QUICStream
161
161
} ,
162
162
write : async ( chunk : Uint8Array ) => {
163
163
await this . streamSend ( chunk ) ;
164
+ void this . connection . send ( ) . catch ( ( ) => { } ) ;
164
165
} ,
165
166
close : async ( ) => {
166
167
// This gracefully closes, by sending a message at the end
@@ -169,11 +170,13 @@ class QUICStream
169
170
// If this itself results in an error, we can continue
170
171
// But continue to do the below
171
172
this . logger . info ( 'sending fin frame' ) ;
173
+ // This.sendFinishedProm.resolveP();
172
174
await this . streamSend ( new Uint8Array ( 0 ) , true ) . catch ( ( e ) => {
173
175
// Ignore send error if stream is already closed
174
176
if ( e . message !== 'send' ) throw e ;
175
177
} ) ;
176
178
await this . closeSend ( ) ;
179
+ void this . connection . send ( ) . catch ( ( ) => { } ) ;
177
180
} ,
178
181
abort : async ( reason ?: any ) => {
179
182
// Abort can be called even if there are writes are queued up
@@ -221,7 +224,7 @@ class QUICStream
221
224
this . writableController . error ( e ) ;
222
225
await this . closeSend ( true , e ) ;
223
226
}
224
- await this . connection . send ( ) ;
227
+ void this . connection . send ( ) . catch ( ( ) => { } ) ;
225
228
this . logger . debug ( 'waiting for underlying streams to finish' ) ;
226
229
this . isFinished ( ) ;
227
230
// We need to wait for the connection to finish before fully destroying
@@ -241,10 +244,11 @@ class QUICStream
241
244
// After reading it's possible the writer had a state change.
242
245
this . isSendFinished ( ) ;
243
246
if ( this . _recvPaused ) {
247
+ console . log ( 'SKIPPING!' ) ;
244
248
// Do nothing if we are paused
245
249
return ;
246
250
}
247
- void this . streamRecv ( ) ;
251
+ void this . streamRecv ( ) . catch ( ( ) => { } ) ;
248
252
}
249
253
250
254
/**
@@ -254,7 +258,7 @@ class QUICStream
254
258
@ready ( new errors . ErrorQUICStreamDestroyed ( ) , false , [ 'destroying' ] )
255
259
public write ( ) : void {
256
260
// Checking if writable has ended
257
- void this . isSendFinished ( ) ;
261
+ this . isSendFinished ( ) ;
258
262
if ( this . resolveWritableP != null ) {
259
263
this . resolveWritableP ( ) ;
260
264
}
@@ -283,7 +287,7 @@ class QUICStream
283
287
'Readable stream closed early with no reason' ,
284
288
) ;
285
289
this . readableController . error ( err ) ;
286
- void this . closeRecv ( true , err ) ;
290
+ void this . closeRecv ( true , err ) . catch ( ( ) => { } ) ;
287
291
}
288
292
}
289
293
return recvFinished ;
@@ -301,6 +305,7 @@ class QUICStream
301
305
} catch ( e ) {
302
306
// If the writable has ended, we need to close the writable.
303
307
// We need to do this in the background to keep this synchronous.
308
+ this . sendFinishedProm . resolveP ( ) ;
304
309
void this . processSendStreamError ( e , 'send' ) . then ( ( reason ) => {
305
310
if ( ! this . _sendClosed ) {
306
311
const err =
@@ -309,10 +314,10 @@ class QUICStream
309
314
'Writable stream closed early with no reason' ,
310
315
) ;
311
316
this . writableController . error ( err ) ;
312
- void this . closeSend ( true , err ) ;
317
+ void this . closeSend ( true , err ) . catch ( ( ) => { } ) ;
313
318
}
314
- this . sendFinishedProm . resolveP ( ) ;
315
319
} ) ;
320
+ this . logger . info ( 'send FINISHED' ) ;
316
321
return true ;
317
322
}
318
323
}
@@ -321,63 +326,63 @@ class QUICStream
321
326
const buf = Buffer . alloc ( 1024 ) ;
322
327
let recvLength : number , fin : boolean ;
323
328
this . logger . debug ( 'trying receiving' ) ;
324
- try {
325
- [ recvLength , fin ] = this . conn . streamRecv ( this . streamId , buf ) ;
326
- } catch ( e ) {
327
- if ( e . message === 'Done' ) {
328
- // When it is reported to be `Done`, it just means that there is no data to read
329
- // it does not mean that the stream is closed or finished
330
- // In such a case, we just ignore and continue
331
- // However after the stream is closed, then it would continue to return `Done`
332
- // This can only occur in 2 ways, either via the `fin`
333
- // or through an exception here where the stream reports an error
334
- // Since we don't call this method unless it is readable
335
- // This should never be reported... (this branch should be dead code)
336
- return ;
337
- } else {
338
- this . logger . info ( 'Stream reported: error' ) ;
339
- this . logger . error ( `Stream reported: error ${ e . message } ` ) ;
340
- // Signal receiving has ended
341
- this . recvFinishedProm . resolveP ( ) ;
342
- const reason = await this . processSendStreamError ( e , 'recv' ) ;
343
- if ( reason != null ) {
344
- // If it is `StreamReset(u64)` error, then the peer has closed
345
- // the stream, and we are receiving the error code
346
- this . readableController . error ( reason ) ;
347
- await this . closeRecv ( true , reason ) ;
329
+ while ( true ) {
330
+ try {
331
+ [ recvLength , fin ] = this . conn . streamRecv ( this . streamId , buf ) ;
332
+ } catch ( e ) {
333
+ if ( e . message === 'Done' ) {
334
+ // When it is reported to be `Done`, it just means that there is no data to read
335
+ // it does not mean that the stream is closed or finished
336
+ // In such a case, we just ignore and continue
337
+ // However after the stream is closed, then it would continue to return `Done`
338
+ // This can only occur in 2 ways, either via the `fin`
339
+ // or through an exception here where the stream reports an error
340
+ // Since we don't call this method unless it is readable
341
+ // This should never be reported... (this branch should be dead code)
342
+ return ;
348
343
} else {
349
- // If it is not a `StreamReset(u64)`, then something else broke
350
- // and we need to propagate the error up and down the stream
351
- this . readableController . error ( e ) ;
352
- await this . closeRecv ( true , e ) ;
344
+ this . logger . info ( 'Stream reported: error' ) ;
345
+ this . logger . error ( `Stream reported: error ${ e . message } ` ) ;
346
+ // Signal receiving has ended
347
+ this . recvFinishedProm . resolveP ( ) ;
348
+ if ( ! this . _recvClosed ) {
349
+ const reason = await this . processSendStreamError ( e , 'recv' ) ;
350
+ if ( reason != null ) {
351
+ // If it is `StreamReset(u64)` error, then the peer has closed
352
+ // the stream, and we are receiving the error code
353
+ this . readableController . error ( reason ) ;
354
+ await this . closeRecv ( true , reason ) ;
355
+ } else {
356
+ // If it is not a `StreamReset(u64)`, then something else broke
357
+ // and we need to propagate the error up and down the stream
358
+ this . readableController . error ( e ) ;
359
+ await this . closeRecv ( true , e ) ;
360
+ }
361
+ }
362
+ return ;
353
363
}
354
- return ;
355
364
}
356
- } finally {
357
- // Let's check if sending side has finished
358
- await this . connection . send ( ) ;
359
- }
360
365
361
- // If fin is true, then that means, the stream is CLOSED
362
- if ( fin ) {
363
- // This will render `stream.cancel` a noop
364
- this . logger . info ( 'Stream reported: fin' ) ;
365
- if ( ! this . _recvClosed ) this . readableController . close ( ) ;
366
- await this . closeRecv ( ) ;
367
- // Signal receiving has ended
368
- this . recvFinishedProm . resolveP ( ) ;
369
- return ;
370
- }
371
- // Only fin packets are 0 length, so we enqueue after checking fin
372
- if ( ! this . _recvClosed ) {
373
- this . readableController . enqueue ( buf . subarray ( 0 , recvLength ) ) ;
374
- }
375
- // Now we pause receiving if the queue is full
376
- if (
377
- this . readableController . desiredSize != null &&
378
- this . readableController . desiredSize <= 0
379
- ) {
380
- this . _recvPaused = true ;
366
+ // If fin is true, then that means, the stream is CLOSED
367
+ if ( fin ) {
368
+ // This will render `stream.cancel` a noop
369
+ this . logger . info ( 'Stream reported: fin' ) ;
370
+ if ( ! this . _recvClosed ) this . readableController . close ( ) ;
371
+ await this . closeRecv ( ) ;
372
+ // Signal receiving has ended
373
+ this . recvFinishedProm . resolveP ( ) ;
374
+ return ;
375
+ }
376
+ if ( ! this . _recvClosed ) {
377
+ this . readableController . enqueue ( buf . subarray ( 0 , recvLength ) ) ;
378
+ }
379
+ // Now we pause receiving if the queue is full
380
+ if (
381
+ this . readableController . desiredSize != null &&
382
+ this . readableController . desiredSize <= 0
383
+ ) {
384
+ this . _recvPaused = true ;
385
+ }
381
386
}
382
387
}
383
388
@@ -423,8 +428,6 @@ class QUICStream
423
428
throw e ;
424
429
}
425
430
}
426
- } finally {
427
- await this . connection . send ( ) ;
428
431
}
429
432
if ( sentLength < chunk . length ) {
430
433
const { p : writableP , resolveP : resolveWritableP } = utils . promise ( ) ;
@@ -447,6 +450,7 @@ class QUICStream
447
450
isError : boolean = false ,
448
451
reason ?: any ,
449
452
) : Promise < void > {
453
+ if ( isError ) this . logger . error ( reason . message ) ;
450
454
// Further closes are NOPs
451
455
if ( this . _recvClosed ) return ;
452
456
this . logger . info ( `Close Recv` ) ;
@@ -481,6 +485,7 @@ class QUICStream
481
485
isError : boolean = false ,
482
486
reason ?: any ,
483
487
) : Promise < void > {
488
+ if ( isError ) this . logger . error ( reason . message ) ;
484
489
// Further closes are NOPs
485
490
if ( this . _sendClosed ) return ;
486
491
this . logger . info ( `Close Send` ) ;
@@ -530,6 +535,39 @@ class QUICStream
530
535
}
531
536
return null ;
532
537
}
538
+
539
+ static checkStreamStates (
540
+ conn : Connection ,
541
+ streamId : number ,
542
+ message : string ,
543
+ logger : Logger ,
544
+ ) {
545
+ const fin = conn . streamFinished ( streamId ) ;
546
+ const read = conn . streamReadable ( streamId ) ;
547
+ let write : boolean | string ;
548
+ try {
549
+ write = conn . streamWritable ( streamId , 0 ) ;
550
+ } catch ( e ) {
551
+ write = e . message ;
552
+ }
553
+ let cap : number | string ;
554
+ try {
555
+ cap = conn . streamCapacity ( streamId ) ;
556
+ } catch ( e ) {
557
+ cap = e . message ;
558
+ }
559
+ let readIter = false ;
560
+ for ( const id of conn . readable ( ) ) {
561
+ if ( streamId === id ) readIter = true ;
562
+ }
563
+ let writeIter = false ;
564
+ for ( const id of conn . writable ( ) ) {
565
+ if ( streamId === id ) writeIter = true ;
566
+ }
567
+ logger . info (
568
+ `Stream states (${ message } ) iterRW(${ readIter } , ${ writeIter } ),finished(${ fin } ), read(${ read } ), write(${ write } ), capacity(${ cap } )` ,
569
+ ) ;
570
+ }
533
571
}
534
572
535
573
export default QUICStream ;
0 commit comments