Skip to content

Commit

Permalink
fix parsing when receiving empty body websocket (#3205)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored May 5, 2024
1 parent 5d54543 commit 6866d29
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class ByteParser extends Writable {
}
}

if (this.#byteOffset === 0) {
if (this.#byteOffset === 0 && this.#info.payloadLength !== 0) {
callback()
break
}
Expand Down
35 changes: 35 additions & 0 deletions test/websocket/issue-3202.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const { test } = require('node:test')
const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

test('Receiving frame with payload length 0 works', async (t) => {
const { ok, completed } = tspl(t, { plan: 1 })

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

server.on('connection', (socket) => {
socket.on('message', () => {
socket.send('')
})
})

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

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

ws.addEventListener('open', () => {
ws.send('Hi')
})

ws.addEventListener('message', () => {
ok(true)
})

await completed
})

0 comments on commit 6866d29

Please sign in to comment.