Skip to content

Commit

Permalink
Add extensions support to WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed May 29, 2024
1 parent 21b9fd5 commit 9a97c2e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type webSocket struct {
bufferedAmount int
binaryType string
protocol string
extensions []string
}

type ping struct {
Expand Down Expand Up @@ -193,7 +194,8 @@ func defineWebsocket(rt *goja.Runtime, w *webSocket) {
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("extensions",
rt.ToValue(func() goja.Value { return rt.ToValue(w.extensions) }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(rt, w.obj.DefineAccessorProperty(
"protocol", rt.ToValue(func() goja.Value { return rt.ToValue(w.protocol) }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(rt, w.obj.DefineAccessorProperty(
Expand Down Expand Up @@ -304,6 +306,7 @@ func (w *webSocket) establishConnection(params *wsParams) {
if conn != nil {
w.protocol = conn.Subprotocol()
}
w.extensions = httpResponse.Header.Values("Sec-WebSocket-Extensions")
w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagSubproto, w.protocol)
}
w.conn = conn
Expand Down
5 changes: 5 additions & 0 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,11 @@ func TestCompressionSession(t *testing.T) {
throw new Error("wrong message received from server: ", event.data)
}
const expectedExtension = "permessage-deflate; server_no_context_takeover; client_no_context_takeover"
if (!(ws.extensions.includes(expectedExtension))) {
throw "expected value '" + expectedExtension + "' missing in " + JSON.stringify(ws.extensions);
}
ws.close()
}
Expand Down

0 comments on commit 9a97c2e

Please sign in to comment.