@@ -10,6 +10,7 @@ const tls = require('tls');
10
10
const fs = require ( 'fs' ) ;
11
11
const { URL } = require ( 'url' ) ;
12
12
13
+ const Sender = require ( '../lib/sender' ) ;
13
14
const WebSocket = require ( '..' ) ;
14
15
const { GUID , NOOP } = require ( '../lib/constants' ) ;
15
16
@@ -2735,15 +2736,21 @@ describe('WebSocket', () => {
2735
2736
} ) ;
2736
2737
} ) ;
2737
2738
2738
- it ( 'consumes all received data when connection is closed abnormally ' , ( done ) => {
2739
+ it ( 'consumes all received data when connection is closed (1/2) ' , ( done ) => {
2739
2740
const wss = new WebSocket . Server (
2740
2741
{
2741
2742
perMessageDeflate : { threshold : 0 } ,
2742
2743
port : 0
2743
2744
} ,
2744
2745
( ) => {
2745
- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
2746
2746
const messages = [ ] ;
2747
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
2748
+
2749
+ ws . on ( 'open' , ( ) => {
2750
+ ws . _socket . on ( 'close' , ( ) => {
2751
+ assert . strictEqual ( ws . _receiver . _state , 5 ) ;
2752
+ } ) ;
2753
+ } ) ;
2747
2754
2748
2755
ws . on ( 'message' , ( message ) => messages . push ( message ) ) ;
2749
2756
ws . on ( 'close' , ( code ) => {
@@ -2762,6 +2769,76 @@ describe('WebSocket', () => {
2762
2769
} ) ;
2763
2770
} ) ;
2764
2771
2772
+ it ( 'consumes all received data when connection is closed (2/2)' , ( done ) => {
2773
+ const payload1 = Buffer . alloc ( 15 * 1024 ) ;
2774
+ const payload2 = Buffer . alloc ( 1 ) ;
2775
+
2776
+ const opts = {
2777
+ fin : true ,
2778
+ opcode : 0x02 ,
2779
+ mask : false ,
2780
+ readOnly : false
2781
+ } ;
2782
+
2783
+ const list = [
2784
+ ...Sender . frame ( payload1 , { rsv1 : false , ...opts } ) ,
2785
+ ...Sender . frame ( payload2 , { rsv1 : true , ...opts } )
2786
+ ] ;
2787
+
2788
+ for ( let i = 0 ; i < 399 ; i ++ ) {
2789
+ list . push ( list [ list . length - 2 ] , list [ list . length - 1 ] ) ;
2790
+ }
2791
+
2792
+ const data = Buffer . concat ( list ) ;
2793
+
2794
+ const wss = new WebSocket . Server (
2795
+ {
2796
+ perMessageDeflate : true ,
2797
+ port : 0
2798
+ } ,
2799
+ ( ) => {
2800
+ const messageLengths = [ ] ;
2801
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
2802
+
2803
+ ws . on ( 'open' , ( ) => {
2804
+ ws . _socket . prependListener ( 'close' , ( ) => {
2805
+ assert . strictEqual ( ws . _receiver . _state , 5 ) ;
2806
+ assert . strictEqual ( ws . _socket . _readableState . length , 3 ) ;
2807
+ } ) ;
2808
+
2809
+ const push = ws . _socket . push ;
2810
+
2811
+ ws . _socket . push = ( data ) => {
2812
+ ws . _socket . push = push ;
2813
+ ws . _socket . push ( data ) ;
2814
+ ws . terminate ( ) ;
2815
+ } ;
2816
+
2817
+ // This hack is used because there is no guarantee that more than
2818
+ // 16 KiB will be sent as a single TCP packet.
2819
+ push . call ( ws . _socket , data ) ;
2820
+
2821
+ wss . clients
2822
+ . values ( )
2823
+ . next ( )
2824
+ . value . send ( payload2 , { compress : false } ) ;
2825
+ } ) ;
2826
+
2827
+ ws . on ( 'message' , ( message ) => {
2828
+ messageLengths . push ( message . length ) ;
2829
+ } ) ;
2830
+
2831
+ ws . on ( 'close' , ( code ) => {
2832
+ assert . strictEqual ( code , 1006 ) ;
2833
+ assert . strictEqual ( messageLengths . length , 402 ) ;
2834
+ assert . strictEqual ( messageLengths [ 0 ] , 15360 ) ;
2835
+ assert . strictEqual ( messageLengths [ messageLengths . length - 1 ] , 1 ) ;
2836
+ wss . close ( done ) ;
2837
+ } ) ;
2838
+ }
2839
+ ) ;
2840
+ } ) ;
2841
+
2765
2842
it ( 'handles a close frame received while compressing data' , ( done ) => {
2766
2843
const wss = new WebSocket . Server (
2767
2844
{
0 commit comments