@@ -10,6 +10,7 @@ const tls = require('tls');
1010const fs = require ( 'fs' ) ;
1111const { URL } = require ( 'url' ) ;
1212
13+ const Sender = require ( '../lib/sender' ) ;
1314const WebSocket = require ( '..' ) ;
1415const { GUID , NOOP } = require ( '../lib/constants' ) ;
1516
@@ -2735,15 +2736,21 @@ describe('WebSocket', () => {
27352736 } ) ;
27362737 } ) ;
27372738
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 ) => {
27392740 const wss = new WebSocket . Server (
27402741 {
27412742 perMessageDeflate : { threshold : 0 } ,
27422743 port : 0
27432744 } ,
27442745 ( ) => {
2745- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
27462746 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+ } ) ;
27472754
27482755 ws . on ( 'message' , ( message ) => messages . push ( message ) ) ;
27492756 ws . on ( 'close' , ( code ) => {
@@ -2762,6 +2769,77 @@ describe('WebSocket', () => {
27622769 } ) ;
27632770 } ) ;
27642771
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 , isBinary ) => {
2828+ assert . ok ( isBinary ) ;
2829+ messageLengths . push ( message . length ) ;
2830+ } ) ;
2831+
2832+ ws . on ( 'close' , ( code ) => {
2833+ assert . strictEqual ( code , 1006 ) ;
2834+ assert . strictEqual ( messageLengths . length , 402 ) ;
2835+ assert . strictEqual ( messageLengths [ 0 ] , 15360 ) ;
2836+ assert . strictEqual ( messageLengths [ messageLengths . length - 1 ] , 1 ) ;
2837+ wss . close ( done ) ;
2838+ } ) ;
2839+ }
2840+ ) ;
2841+ } ) ;
2842+
27652843 it ( 'handles a close frame received while compressing data' , ( done ) => {
27662844 const wss = new WebSocket . Server (
27672845 {
0 commit comments