Skip to content

chore: apply various modernisations #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

var v interface{}
var v any
err = wsjson.Read(ctx, c, &v)
if err != nil {
// ...
Expand Down
1 change: 0 additions & 1 deletion accept.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down
1 change: 0 additions & 1 deletion accept_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down
5 changes: 2 additions & 3 deletions autobahn_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket_test

Expand Down Expand Up @@ -130,7 +129,7 @@ func wstestServer(tb testing.TB, ctx context.Context) (url string, closeFn func(
url = "ws://" + serverAddr
const outDir = "ci/out/autobahn-report"

specFile, err := tempJSONFile(map[string]interface{}{
specFile, err := tempJSONFile(map[string]any{
"url": url,
"outdir": outDir,
"cases": autobahnCases,
Expand Down Expand Up @@ -280,7 +279,7 @@ func unusedListenAddr() (_ string, err error) {
return l.Addr().String(), nil
}

func tempJSONFile(v interface{}) (string, error) {
func tempJSONFile(v any) (string, error) {
f, err := os.CreateTemp("", "temp.json")
if err != nil {
return "", fmt.Errorf("temp file: %w", err)
Expand Down
1 change: 0 additions & 1 deletion close.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down
1 change: 0 additions & 1 deletion close_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down
7 changes: 4 additions & 3 deletions compress.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down Expand Up @@ -168,8 +167,10 @@ type slidingWindow struct {
buf []byte
}

var swPoolMu sync.RWMutex
var swPool = map[int]*sync.Pool{}
var (
swPoolMu sync.RWMutex
swPool = map[int]*sync.Pool{}
)

func slidingWindowPool(n int) *sync.Pool {
swPoolMu.RLock()
Expand Down
3 changes: 1 addition & 2 deletions compress_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand All @@ -19,7 +18,7 @@ func Test_slidingWindow(t *testing.T) {

const testCount = 99
const maxWindow = 99999
for i := 0; i < testCount; i++ {
for range testCount {
t.Run("", func(t *testing.T) {
t.Parallel()

Expand Down
1 change: 0 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down
20 changes: 10 additions & 10 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestConn(t *testing.T) {
return websocket.CompressionMode(xrand.Int(int(websocket.CompressionContextTakeover) + 1))
}

for i := 0; i < 5; i++ {
for range 5 {
t.Run("", func(t *testing.T) {
tt, c1, c2 := newConnTest(t, &websocket.DialOptions{
CompressionMode: compressionMode(),
Expand All @@ -50,7 +50,7 @@ func TestConn(t *testing.T) {

c1.SetReadLimit(131072)

for i := 0; i < 5; i++ {
for range 5 {
err := wstest.Echo(tt.ctx, c1, 131072)
assert.Success(t, err)
}
Expand All @@ -76,7 +76,7 @@ func TestConn(t *testing.T) {
c1.CloseRead(tt.ctx)
c2.CloseRead(tt.ctx)

for i := 0; i < 10; i++ {
for range 10 {
err := c1.Ping(tt.ctx)
assert.Success(t, err)
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestConn(t *testing.T) {
const count = 100
errs := make(chan error, count)

for i := 0; i < count; i++ {
for range count {
go func() {
select {
case errs <- c1.Write(tt.ctx, websocket.MessageBinary, msg):
Expand All @@ -195,7 +195,7 @@ func TestConn(t *testing.T) {
}()
}

for i := 0; i < count; i++ {
for range count {
select {
case err := <-errs:
assert.Success(t, err)
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestConn(t *testing.T) {
return wsjson.Write(tt.ctx, c1, exp)
})

var act interface{}
var act any
err := wsjson.Read(tt.ctx, c1, &act)
assert.Success(t, err)
assert.Equal(t, "read msg", exp, act)
Expand Down Expand Up @@ -372,7 +372,7 @@ func TestConn(t *testing.T) {
return wsjson.Write(tt.ctx, c1, exp)
})

var act interface{}
var act any
err := wsjson.Read(tt.ctx, c1, &act)
assert.Success(t, err)
assert.Equal(t, "read msg", exp, act)
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestConn(t *testing.T) {

c1.SetReadLimit(131072)

for i := 0; i < 5; i++ {
for range 5 {
err := wstest.Echo(tt.ctx, c1, 131072)
assert.Success(t, err)
}
Expand Down Expand Up @@ -660,7 +660,7 @@ func assertEcho(tb testing.TB, ctx context.Context, c *websocket.Conn) {
return wsjson.Write(ctx, c, exp)
})

var act interface{}
var act any
c.SetReadLimit(1 << 30)
err := wsjson.Read(ctx, c, &act)
assert.Success(tb, err)
Expand All @@ -682,7 +682,7 @@ func assertClose(tb testing.TB, c *websocket.Conn) {

func TestConcurrentClosePing(t *testing.T) {
t.Parallel()
for i := 0; i < 64; i++ {
for range 64 {
func() {
c1, c2 := wstest.Pipe(nil, nil)
defer c1.CloseNow()
Expand Down
1 change: 0 additions & 1 deletion dial.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down
8 changes: 3 additions & 5 deletions dial_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket_test

Expand All @@ -8,6 +7,7 @@ import (
"context"
"crypto/rand"
"io"
"maps"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -172,7 +172,6 @@ func Test_verifyHostOverride(t *testing.T) {
c.CloseNow()
})
}

}

type mockBody struct {
Expand Down Expand Up @@ -357,11 +356,10 @@ func (fc *forwardProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

for k, v := range resp.Header {
w.Header()[k] = v
}
maps.Copy(w.Header(), resp.Header)
w.Header().Set("PROXIED", "true")
w.WriteHeader(resp.StatusCode)

if resprw, ok := resp.Body.(io.ReadWriter); ok {
c, brw, err := w.(http.Hijacker).Hijack()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

// Package websocket implements the RFC 6455 WebSocket protocol.
//
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ExampleAccept() {
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
defer cancel()

var v interface{}
var v any
err = wsjson.Read(ctx, c, &v)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -150,7 +150,7 @@ func ExampleConn_Ping() {
// Required to read the Pongs from the server.
ctx = c.CloseRead(ctx)

for i := 0; i < 5; i++ {
for range 5 {
err = c.Ping(ctx)
if err != nil {
log.Fatal(err)
Expand Down
11 changes: 6 additions & 5 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down Expand Up @@ -30,9 +29,11 @@ func (c *Conn) RecordBytesRead() *int {

var ErrClosed = net.ErrClosed

var ExportedDial = dial
var SecWebSocketAccept = secWebSocketAccept
var SecWebSocketKey = secWebSocketKey
var VerifyServerResponse = verifyServerResponse
var (
ExportedDial = dial
SecWebSocketAccept = secWebSocketAccept
SecWebSocketKey = secWebSocketKey
VerifyServerResponse = verifyServerResponse
)

var CompressionModeOpts = CompressionMode.opts
3 changes: 1 addition & 2 deletions frame_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package websocket

Expand Down Expand Up @@ -54,7 +53,7 @@ func TestHeader(t *testing.T) {
return r.Intn(2) == 0
}

for i := 0; i < 10000; i++ {
for range 10000 {
h := header{
fin: randBool(),
rsv1: randBool(),
Expand Down
2 changes: 1 addition & 1 deletion internal/errd/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// Wrap wraps err with fmt.Errorf if err is non nil.
// Intended for use with defer and a named error return.
// Inspired by https://github.com/golang/go/issues/32676.
func Wrap(err *error, f string, v ...interface{}) {
func Wrap(err *error, f string, v ...any) {
if *err != nil {
*err = fmt.Errorf(f+": %w", append(v, *err)...)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type chatServer struct {

// logf controls where logs are sent.
// Defaults to log.Printf.
logf func(f string, v ...interface{})
logf func(f string, v ...any)

// serveMux routes the various endpoints to the appropriate handler.
serveMux http.ServeMux
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// only allows one message every 100ms with a 10 message burst.
type echoServer struct {
// logf controls where logs are sent.
logf func(f string, v ...interface{})
logf func(f string, v ...any)
}

func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions internal/test/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Equal asserts exp == act.
func Equal(t testing.TB, name string, exp, got interface{}) {
func Equal(t testing.TB, name string, exp, got any) {
t.Helper()

if !reflect.DeepEqual(exp, got) {
Expand All @@ -36,7 +36,7 @@ func Error(t testing.TB, err error) {
}

// Contains asserts the fmt.Sprint(v) contains sub.
func Contains(t testing.TB, v interface{}, sub string) {
func Contains(t testing.TB, v any, sub string) {
t.Helper()

s := fmt.Sprint(v)
Expand Down
1 change: 0 additions & 1 deletion internal/test/wstest/pipe.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !js
// +build !js

package wstest

Expand Down
2 changes: 1 addition & 1 deletion internal/thirdparty/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGin(t *testing.T) {
err = wsjson.Write(ctx, c, "hello")
assert.Success(t, err)

var v interface{}
var v any
err = wsjson.Read(ctx, c, &v)
assert.Success(t, err)
assert.Equal(t, "read msg", "hello", v)
Expand Down
8 changes: 4 additions & 4 deletions internal/wsjs/wsjs_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func New(url string, protocols []string) (c WebSocket, err error) {
c = WebSocket{}
})

jsProtocols := make([]interface{}, len(protocols))
jsProtocols := make([]any, len(protocols))
for i, p := range protocols {
jsProtocols[i] = p
}
Expand All @@ -57,7 +57,7 @@ func (c WebSocket) setBinaryType(typ string) {
}

func (c WebSocket) addEventListener(eventType string, fn func(e js.Value)) func() {
f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
f := js.FuncOf(func(this js.Value, args []js.Value) any {
fn(args[0])
return nil
})
Expand Down Expand Up @@ -97,7 +97,7 @@ func (c WebSocket) OnError(fn func(e js.Value)) (remove func()) {
// MessageEvent is the type passed to a message handler.
type MessageEvent struct {
// string or []byte.
Data interface{}
Data any

// There are more fields to the interface but we don't use them.
// See https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent
Expand All @@ -106,7 +106,7 @@ type MessageEvent struct {
// OnMessage registers a function to be called when the WebSocket receives a message.
func (c WebSocket) OnMessage(fn func(m MessageEvent)) (remove func()) {
return c.addEventListener("message", func(e js.Value) {
var data interface{}
var data any

arrayBuffer := e.Get("data")
if arrayBuffer.Type() == js.TypeString {
Expand Down
2 changes: 1 addition & 1 deletion mask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestMask(t *testing.T) {
func testMask(t *testing.T, name string, fn func(b []byte, key uint32) uint32) {
t.Run(name, func(t *testing.T) {
t.Parallel()
for i := 0; i < 9999; i++ {
for range 9999 {
keyb := make([]byte, 4)
_, err := rand.Read(keyb)
assert.Success(t, err)
Expand Down
Loading