@@ -2638,6 +2638,87 @@ describe('WebSocket', () => {
2638
2638
} ) ;
2639
2639
} ) ;
2640
2640
2641
+ it ( 'handles a close frame received while compressing data' , ( done ) => {
2642
+ const wss = new WebSocket . Server (
2643
+ {
2644
+ perMessageDeflate : true ,
2645
+ port : 0
2646
+ } ,
2647
+ ( ) => {
2648
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` , {
2649
+ perMessageDeflate : { threshold : 0 }
2650
+ } ) ;
2651
+
2652
+ ws . on ( 'open' , ( ) => {
2653
+ ws . _receiver . on ( 'conclude' , ( ) => {
2654
+ assert . ok ( ws . _sender . _deflating ) ;
2655
+ } ) ;
2656
+
2657
+ ws . send ( 'foo' ) ;
2658
+ ws . send ( 'bar' ) ;
2659
+ ws . send ( 'baz' ) ;
2660
+ ws . send ( 'qux' ) ;
2661
+ } ) ;
2662
+ }
2663
+ ) ;
2664
+
2665
+ wss . on ( 'connection' , ( ws ) => {
2666
+ const messages = [ ] ;
2667
+
2668
+ ws . on ( 'message' , ( message ) => {
2669
+ messages . push ( message ) ;
2670
+ } ) ;
2671
+
2672
+ ws . on ( 'close' , ( code , reason ) => {
2673
+ assert . deepStrictEqual ( messages , [ 'foo' , 'bar' , 'baz' , 'qux' ] ) ;
2674
+ assert . strictEqual ( code , 1000 ) ;
2675
+ assert . strictEqual ( reason , '' ) ;
2676
+ wss . close ( done ) ;
2677
+ } ) ;
2678
+
2679
+ ws . close ( 1000 ) ;
2680
+ } ) ;
2681
+ } ) ;
2682
+
2683
+ describe ( '#close' , ( ) => {
2684
+ it ( 'can be used while data is being decompressed' , ( done ) => {
2685
+ const wss = new WebSocket . Server (
2686
+ {
2687
+ perMessageDeflate : true ,
2688
+ port : 0
2689
+ } ,
2690
+ ( ) => {
2691
+ const messages = [ ] ;
2692
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
2693
+
2694
+ ws . on ( 'open' , ( ) => {
2695
+ ws . _socket . on ( 'end' , ( ) => {
2696
+ assert . strictEqual ( ws . _receiver . _state , 5 ) ;
2697
+ } ) ;
2698
+ } ) ;
2699
+
2700
+ ws . on ( 'message' , ( message ) => {
2701
+ if ( messages . push ( message ) > 1 ) return ;
2702
+
2703
+ ws . close ( 1000 ) ;
2704
+ } ) ;
2705
+
2706
+ ws . on ( 'close' , ( code , reason ) => {
2707
+ assert . deepStrictEqual ( messages , [ '' , '' , '' , '' ] ) ;
2708
+ assert . strictEqual ( code , 1000 ) ;
2709
+ assert . strictEqual ( reason , '' ) ;
2710
+ wss . close ( done ) ;
2711
+ } ) ;
2712
+ }
2713
+ ) ;
2714
+
2715
+ wss . on ( 'connection' , ( ws ) => {
2716
+ const buf = Buffer . from ( 'c10100c10100c10100c10100' , 'hex' ) ;
2717
+ ws . _socket . write ( buf ) ;
2718
+ } ) ;
2719
+ } ) ;
2720
+ } ) ;
2721
+
2641
2722
describe ( '#send' , ( ) => {
2642
2723
it ( 'ignores the `compress` option if the extension is disabled' , ( done ) => {
2643
2724
const wss = new WebSocket . Server ( { port : 0 } , ( ) => {
0 commit comments