@@ -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 {
15
16
CloseEvent,
@@ -2942,15 +2943,21 @@ describe('WebSocket', () => {
2942
2943
} ) ;
2943
2944
} ) ;
2944
2945
2945
- it ( 'consumes all received data when connection is closed abnormally ' , ( done ) => {
2946
+ it ( 'consumes all received data when connection is closed (1/2) ' , ( done ) => {
2946
2947
const wss = new WebSocket . Server (
2947
2948
{
2948
2949
perMessageDeflate : { threshold : 0 } ,
2949
2950
port : 0
2950
2951
} ,
2951
2952
( ) => {
2952
- const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
2953
2953
const messages = [ ] ;
2954
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
2955
+
2956
+ ws . on ( 'open' , ( ) => {
2957
+ ws . _socket . on ( 'close' , ( ) => {
2958
+ assert . strictEqual ( ws . _receiver . _state , 5 ) ;
2959
+ } ) ;
2960
+ } ) ;
2954
2961
2955
2962
ws . on ( 'message' , ( message , isBinary ) => {
2956
2963
assert . ok ( ! isBinary ) ;
@@ -2973,6 +2980,77 @@ describe('WebSocket', () => {
2973
2980
} ) ;
2974
2981
} ) ;
2975
2982
2983
+ it ( 'consumes all received data when connection is closed (2/2)' , ( done ) => {
2984
+ const payload1 = Buffer . alloc ( 15 * 1024 ) ;
2985
+ const payload2 = Buffer . alloc ( 1 ) ;
2986
+
2987
+ const opts = {
2988
+ fin : true ,
2989
+ opcode : 0x02 ,
2990
+ mask : false ,
2991
+ readOnly : false
2992
+ } ;
2993
+
2994
+ const list = [
2995
+ ...Sender . frame ( payload1 , { rsv1 : false , ...opts } ) ,
2996
+ ...Sender . frame ( payload2 , { rsv1 : true , ...opts } )
2997
+ ] ;
2998
+
2999
+ for ( let i = 0 ; i < 399 ; i ++ ) {
3000
+ list . push ( list [ list . length - 2 ] , list [ list . length - 1 ] ) ;
3001
+ }
3002
+
3003
+ const data = Buffer . concat ( list ) ;
3004
+
3005
+ const wss = new WebSocket . Server (
3006
+ {
3007
+ perMessageDeflate : true ,
3008
+ port : 0
3009
+ } ,
3010
+ ( ) => {
3011
+ const messageLengths = [ ] ;
3012
+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` ) ;
3013
+
3014
+ ws . on ( 'open' , ( ) => {
3015
+ ws . _socket . prependListener ( 'close' , ( ) => {
3016
+ assert . strictEqual ( ws . _receiver . _state , 5 ) ;
3017
+ assert . strictEqual ( ws . _socket . _readableState . length , 3 ) ;
3018
+ } ) ;
3019
+
3020
+ const push = ws . _socket . push ;
3021
+
3022
+ ws . _socket . push = ( data ) => {
3023
+ ws . _socket . push = push ;
3024
+ ws . _socket . push ( data ) ;
3025
+ ws . terminate ( ) ;
3026
+ } ;
3027
+
3028
+ // This hack is used because there is no guarantee that more than
3029
+ // 16 KiB will be sent as a single TCP packet.
3030
+ push . call ( ws . _socket , data ) ;
3031
+
3032
+ wss . clients
3033
+ . values ( )
3034
+ . next ( )
3035
+ . value . send ( payload2 , { compress : false } ) ;
3036
+ } ) ;
3037
+
3038
+ ws . on ( 'message' , ( message , isBinary ) => {
3039
+ assert . ok ( isBinary ) ;
3040
+ messageLengths . push ( message . length ) ;
3041
+ } ) ;
3042
+
3043
+ ws . on ( 'close' , ( code ) => {
3044
+ assert . strictEqual ( code , 1006 ) ;
3045
+ assert . strictEqual ( messageLengths . length , 402 ) ;
3046
+ assert . strictEqual ( messageLengths [ 0 ] , 15360 ) ;
3047
+ assert . strictEqual ( messageLengths [ messageLengths . length - 1 ] , 1 ) ;
3048
+ wss . close ( done ) ;
3049
+ } ) ;
3050
+ }
3051
+ ) ;
3052
+ } ) ;
3053
+
2976
3054
it ( 'handles a close frame received while compressing data' , ( done ) => {
2977
3055
const wss = new WebSocket . Server (
2978
3056
{
0 commit comments