Skip to content

Commit

Permalink
Make certain readyState is actually a number when returned
Browse files Browse the repository at this point in the history
for some reason - likely a bug in goja/Sobek returning a type that is
uint8 under the hood (or int) doesn't actually get to be a number in js.

The current fix casts the ReadyState type to uint8 so that Sobek handles
it correctly.

Fixes #79
  • Loading branch information
mstoykov committed Sep 27, 2024
1 parent 3079f14 commit ad3dafa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func defineWebsocket(rt *sobek.Runtime, w *webSocket) {
must(rt, w.obj.DefineDataProperty(
"url", rt.ToValue(w.url.String()), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(rt, w.obj.DefineAccessorProperty( // this needs to be with an accessor as we change the value
"readyState", rt.ToValue(func() ReadyState {
return w.readyState
"readyState", rt.ToValue(func() sobek.Value {
return rt.ToValue((uint)(w.readyState))
}), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(rt, w.obj.DefineAccessorProperty(
"bufferedAmount", rt.ToValue(func() sobek.Value { return rt.ToValue(w.bufferedAmount) }), nil,
Expand Down
23 changes: 23 additions & 0 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,3 +1527,26 @@ func testArrayBufferViewSupport(t *testing.T, viewName string) {
logs := hook.Drain()
require.Len(t, logs, 0)
}

func TestReadyStateSwitch(t *testing.T) {
t.Parallel()
ts := newTestState(t)
logger, hook := testutils.NewLoggerWithHook(t, logrus.WarnLevel)
ts.runtime.VU.StateField.Logger = logger
_, err := ts.runtime.RunOnEventLoop(ts.tb.Replacer.Replace(`
var ws = new WebSocket("WSBIN_URL/ws-echo")
try {
switch (ws.readyState) {
case 0:
break;
default:
throw "ws.readyState doesn't get correct value in switch"
}
} finally {
ws.close()
}
`))
require.NoError(t, err)
logs := hook.Drain()
require.Len(t, logs, 0)
}

0 comments on commit ad3dafa

Please sign in to comment.