File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -594,10 +594,12 @@ class WebSocket extends EventTarget {
594594 * @param {Buffer|undefined } buffer
595595 */
596596 static ping ( ws , buffer ) {
597- if ( ! Buffer . isBuffer ( buffer ) ) {
597+ if ( Buffer . isBuffer ( buffer ) ) {
598+ if ( buffer . length > 125 ) {
599+ throw new TypeError ( 'A PING frame cannot have a body larger than 125 bytes.' )
600+ }
601+ } else if ( buffer !== undefined ) {
598602 throw new TypeError ( 'Expected buffer payload' )
599- } else if ( buffer . length > 125 ) {
600- throw new TypeError ( 'A PING frame cannot have a body larger than 125 bytes.' )
601603 }
602604
603605 // An endpoint MAY send a Ping frame any time after the connection is
Original file line number Diff line number Diff line change @@ -18,10 +18,7 @@ test('ping', async (t) => {
1818 } )
1919
2020 const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` )
21-
22- ws . addEventListener ( 'open' , ( ) => {
23- ping ( ws , pingBody )
24- } )
21+ ws . onopen = ( ) => ping ( ws , pingBody )
2522
2623 t . after ( ( ) => {
2724 ws . close ( )
@@ -54,3 +51,25 @@ test('attempting to send invalid ping body', async (t) => {
5451
5552 await completed
5653} )
54+
55+ test ( 'ping with no payload' , async ( t ) => {
56+ const { completed, deepStrictEqual } = tspl ( t , { plan : 1 } )
57+
58+ const wss = new WebSocketServer ( { port : 0 } )
59+
60+ wss . on ( 'connection' , ( ws ) => {
61+ ws . on ( 'ping' , ( b ) => {
62+ deepStrictEqual ( b , Buffer . alloc ( 0 ) )
63+ } )
64+ } )
65+
66+ const ws = new WebSocket ( `ws://localhost:${ wss . address ( ) . port } ` )
67+ ws . onopen = ( ) => ping ( ws )
68+
69+ t . after ( ( ) => {
70+ ws . close ( )
71+ wss . close ( )
72+ } )
73+
74+ await completed
75+ } )
You can’t perform that action at this time.
0 commit comments