Skip to content

Commit

Permalink
Fix bufferedAmount not updating (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov authored May 29, 2024
1 parent d72cc50 commit 21b9fd5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ func defineWebsocket(rt *goja.Runtime, w *webSocket) {
"readyState", rt.ToValue(func() ReadyState {
return w.readyState
}), nil, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(rt, w.obj.DefineDataProperty(
"bufferedAmount", rt.ToValue(w.bufferedAmount), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(rt, w.obj.DefineAccessorProperty(
"bufferedAmount", rt.ToValue(func() goja.Value { return rt.ToValue(w.bufferedAmount) }), nil,
goja.FLAG_FALSE, goja.FLAG_TRUE))
// extensions
must(rt, w.obj.DefineAccessorProperty(
"protocol", rt.ToValue(func() goja.Value { return rt.ToValue(w.protocol) }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE))
Expand Down
9 changes: 9 additions & 0 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,17 @@ func TestBinaryState(t *testing.T) {
var ws = new WebSocket("WSBIN_URL/ws-echo-invalid")
ws.addEventListener("open", () => {
ws.send(new Uint8Array([164,41]).buffer)
if (ws.bufferedAmount != 2) {
throw "Expected 2 bufferedAmount got "+ ws.bufferedAmount
}
ws.send("k6")
if (ws.bufferedAmount != 4) {
throw "Expected 4 bufferedAmount got "+ ws.bufferedAmount
}
ws.onmessage = (e) => {
if (ws.bufferedAmount != 0 && ws.bufferedAmount != 2) { // it is possible one or both were flushed
throw "Expected 0 or 2 bufferedAmount, but got "+ ws.bufferedAmount
}
ws.close()
call(JSON.stringify(e))
}
Expand Down

0 comments on commit 21b9fd5

Please sign in to comment.