Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,12 @@ class WebSocket extends EventTarget {
* @param {Buffer|undefined} buffer
*/
static ping (ws, buffer) {
if (!Buffer.isBuffer(buffer)) {
if (Buffer.isBuffer(buffer)) {
if (buffer.length > 125) {
throw new TypeError('A PING frame cannot have a body larger than 125 bytes.')
}
} else if (buffer !== undefined) {
throw new TypeError('Expected buffer payload')
} else if (buffer.length > 125) {
throw new TypeError('A PING frame cannot have a body larger than 125 bytes.')
}

// An endpoint MAY send a Ping frame any time after the connection is
Expand Down
27 changes: 23 additions & 4 deletions test/websocket/ping-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ test('ping', async (t) => {
})

const ws = new WebSocket(`ws://localhost:${wss.address().port}`)

ws.addEventListener('open', () => {
ping(ws, pingBody)
})
ws.onopen = () => ping(ws, pingBody)

t.after(() => {
ws.close()
Expand Down Expand Up @@ -54,3 +51,25 @@ test('attempting to send invalid ping body', async (t) => {

await completed
})

test('ping with no payload', async (t) => {
const { completed, deepStrictEqual } = tspl(t, { plan: 1 })

const wss = new WebSocketServer({ port: 0 })

wss.on('connection', (ws) => {
ws.on('ping', (b) => {
deepStrictEqual(b, Buffer.alloc(0))
})
})

const ws = new WebSocket(`ws://localhost:${wss.address().port}`)
ws.onopen = () => ping(ws)

t.after(() => {
ws.close()
wss.close()
})

await completed
})
Loading