From 778d161bfd21f3cfa8052d8b84e8c86e08ce41c7 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 14 Mar 2025 17:38:18 +0200 Subject: [PATCH 1/4] build: update to Go 1.23 (#524) * build: update to Go 1.23 * ci: update tools --- ci/fmt.sh | 3 +-- ci/lint.sh | 7 +++++-- go.mod | 2 +- internal/examples/go.mod | 2 +- internal/thirdparty/go.mod | 2 +- internal/thirdparty/go.sum | 3 +++ 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ci/fmt.sh b/ci/fmt.sh index e319a1e4..588510ba 100755 --- a/ci/fmt.sh +++ b/ci/fmt.sh @@ -2,8 +2,7 @@ set -eu cd -- "$(dirname "$0")/.." -# Pin golang.org/x/tools, the go.mod of v0.25.0 is incompatible with Go 1.19. -X_TOOLS_VERSION=v0.24.0 +X_TOOLS_VERSION=v0.31.0 go mod tidy (cd ./internal/thirdparty && go mod tidy) diff --git a/ci/lint.sh b/ci/lint.sh index cf9d1abd..20daff92 100755 --- a/ci/lint.sh +++ b/ci/lint.sh @@ -3,10 +3,13 @@ set -x set -eu cd -- "$(dirname "$0")/.." +STATICCHECK_VERSION=v0.6.1 +GOVULNCHECK_VERSION=v1.1.4 + go vet ./... GOOS=js GOARCH=wasm go vet ./... -go install honnef.co/go/tools/cmd/staticcheck@v0.4.7 +go install honnef.co/go/tools/cmd/staticcheck@${STATICCHECK_VERSION} staticcheck ./... GOOS=js GOARCH=wasm staticcheck ./... @@ -16,7 +19,7 @@ govulncheck() { cat "$tmpf" fi } -go install golang.org/x/vuln/cmd/govulncheck@v1.1.1 +go install golang.org/x/vuln/cmd/govulncheck@${GOVULNCHECK_VERSION} govulncheck ./... GOOS=js GOARCH=wasm govulncheck ./... diff --git a/go.mod b/go.mod index 336411a5..d32fbd77 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/coder/websocket -go 1.19 +go 1.23 diff --git a/internal/examples/go.mod b/internal/examples/go.mod index 2aa1ee02..e368b76b 100644 --- a/internal/examples/go.mod +++ b/internal/examples/go.mod @@ -1,6 +1,6 @@ module github.com/coder/websocket/examples -go 1.19 +go 1.23 replace github.com/coder/websocket => ../.. diff --git a/internal/thirdparty/go.mod b/internal/thirdparty/go.mod index e060ce67..7a86aca9 100644 --- a/internal/thirdparty/go.mod +++ b/internal/thirdparty/go.mod @@ -1,6 +1,6 @@ module github.com/coder/websocket/internal/thirdparty -go 1.19 +go 1.23 replace github.com/coder/websocket => ../.. diff --git a/internal/thirdparty/go.sum b/internal/thirdparty/go.sum index 2352ac75..a7be7082 100644 --- a/internal/thirdparty/go.sum +++ b/internal/thirdparty/go.sum @@ -16,6 +16,7 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= @@ -31,6 +32,7 @@ github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakr github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -96,6 +98,7 @@ golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= From 246891f172ef96b0b5681c8e4d59dfd32ad1b091 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 14 Mar 2025 17:49:00 +0200 Subject: [PATCH 2/4] build: add Makefile (#525) --- .github/workflows/ci.yml | 8 ++++---- .github/workflows/daily.yml | 8 ++++---- .github/workflows/static.yml | 2 +- Makefile | 18 ++++++++++++++++++ ci/lint.sh | 1 - make.sh | 12 ------------ 6 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 Makefile delete mode 100755 make.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f7aed46..836381ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: ./ci/fmt.sh + - run: make fmt lint: runs-on: ubuntu-latest @@ -28,7 +28,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: ./ci/lint.sh + - run: make lint test: runs-on: ubuntu-latest @@ -43,7 +43,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: ./ci/test.sh + - run: make test - uses: actions/upload-artifact@v4 with: name: coverage.html @@ -56,4 +56,4 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: ./ci/bench.sh + - run: make bench diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 0eac94cc..62e3d337 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -15,7 +15,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: AUTOBAHN=1 ./ci/bench.sh + - run: AUTOBAHN=1 make bench test: runs-on: ubuntu-latest steps: @@ -29,7 +29,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: AUTOBAHN=1 ./ci/test.sh + - run: AUTOBAHN=1 make test - uses: actions/upload-artifact@v4 with: name: coverage.html @@ -43,7 +43,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: AUTOBAHN=1 ./ci/bench.sh + - run: AUTOBAHN=1 make bench test-dev: runs-on: ubuntu-latest steps: @@ -59,7 +59,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version-file: ./go.mod - - run: AUTOBAHN=1 ./ci/test.sh + - run: AUTOBAHN=1 make test - uses: actions/upload-artifact@v4 with: name: coverage-dev.html diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 6ea76ab6..a78ce1b9 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -38,7 +38,7 @@ jobs: go-version-file: ./go.mod - name: Generate coverage and badge run: | - ./ci/test.sh + make test mkdir -p ./ci/out/static cp ./ci/out/coverage.html ./ci/out/static/coverage.html percent=$(go tool cover -func ./ci/out/coverage.prof | tail -n1 | awk '{print $3}' | tr -d '%') diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..a3e4a20d --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.PHONY: all +all: fmt lint test + +.PHONY: fmt +fmt: + ./ci/fmt.sh + +.PHONY: lint +lint: + ./ci/lint.sh + +.PHONY: test +test: + ./ci/test.sh + +.PHONY: bench +bench: + ./ci/bench.sh \ No newline at end of file diff --git a/ci/lint.sh b/ci/lint.sh index 20daff92..316b035d 100755 --- a/ci/lint.sh +++ b/ci/lint.sh @@ -1,5 +1,4 @@ #!/bin/sh -set -x set -eu cd -- "$(dirname "$0")/.." diff --git a/make.sh b/make.sh deleted file mode 100755 index 170d00a8..00000000 --- a/make.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh -set -eu -cd -- "$(dirname "$0")" - -echo "=== fmt.sh" -./ci/fmt.sh -echo "=== lint.sh" -./ci/lint.sh -echo "=== test.sh" -./ci/test.sh "$@" -echo "=== bench.sh" -./ci/bench.sh From efb626be44240d7979b57427265d9b6402166b96 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 25 Mar 2025 13:27:06 +0100 Subject: [PATCH 3/4] chore: update LICENSE file (#526) --- LICENSE.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.txt b/LICENSE.txt index 77b5bef6..7e79329f 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2023 Anmol Sethi +Copyright (c) 2025 Coder Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above From 91013c129147ee6375b65cf150fa151d2fcc0bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Alz=C3=A9n?= Date: Thu, 10 Jul 2025 10:49:59 +0200 Subject: [PATCH 4/4] chore: apply various modernisations (#531) * Use any instead of interface{} given Go > 1.18 * Minor tidies thanks to gofumpt * Run "go fix ./..." on the codebase * Apply a few modernisations suggested by gopls check * Remove unused parameter suggested by gopls check --- README.md | 2 +- accept.go | 1 - accept_test.go | 1 - autobahn_test.go | 5 ++--- close.go | 1 - close_test.go | 1 - compress.go | 7 ++++--- compress_test.go | 3 +-- conn.go | 1 - conn_test.go | 20 ++++++++++---------- dial.go | 1 - dial_test.go | 8 +++----- doc.go | 1 - example_test.go | 4 ++-- export_test.go | 11 ++++++----- frame_test.go | 3 +-- internal/errd/wrap.go | 2 +- internal/examples/chat/chat.go | 2 +- internal/examples/echo/server.go | 2 +- internal/test/assert/assert.go | 4 ++-- internal/test/wstest/pipe.go | 1 - internal/thirdparty/gin_test.go | 2 +- internal/wsjs/wsjs_js.go | 8 ++++---- mask_test.go | 2 +- netconn.go | 3 +-- netconn_notjs.go | 1 - read.go | 1 - write.go | 6 +----- ws_js.go | 4 ++-- ws_js_test.go | 2 +- wsjson/wsjson.go | 8 ++++---- 31 files changed, 50 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 80d2b3cc..6e986897 100644 --- a/README.md +++ b/README.md @@ -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 { // ... diff --git a/accept.go b/accept.go index f45fdd0b..0f3b0d16 100644 --- a/accept.go +++ b/accept.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/accept_test.go b/accept_test.go index 3b45ac5c..aeea1d8a 100644 --- a/accept_test.go +++ b/accept_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/autobahn_test.go b/autobahn_test.go index cd0cc9bb..20b89609 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket_test @@ -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, @@ -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) diff --git a/close.go b/close.go index f94951dc..2de1a5c2 100644 --- a/close.go +++ b/close.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/close_test.go b/close_test.go index aec582c1..1e04807e 100644 --- a/close_test.go +++ b/close_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/compress.go b/compress.go index 1f3adcfb..41bd5bdb 100644 --- a/compress.go +++ b/compress.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket @@ -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() diff --git a/compress_test.go b/compress_test.go index d97492cf..1964c84f 100644 --- a/compress_test.go +++ b/compress_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket @@ -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() diff --git a/conn.go b/conn.go index 42fe89fe..5907bc81 100644 --- a/conn.go +++ b/conn.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/conn_test.go b/conn_test.go index 45bb75be..58ac394c 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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(), @@ -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) } @@ -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) } @@ -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): @@ -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) @@ -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) @@ -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) @@ -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) } @@ -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) @@ -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() diff --git a/dial.go b/dial.go index 0b11ecbb..f5e4544b 100644 --- a/dial.go +++ b/dial.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/dial_test.go b/dial_test.go index f94cd73b..492ac6b3 100644 --- a/dial_test.go +++ b/dial_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket_test @@ -8,6 +7,7 @@ import ( "context" "crypto/rand" "io" + "maps" "net/http" "net/http/httptest" "net/url" @@ -172,7 +172,6 @@ func Test_verifyHostOverride(t *testing.T) { c.CloseNow() }) } - } type mockBody struct { @@ -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 { diff --git a/doc.go b/doc.go index 03edf129..0c7f8316 100644 --- a/doc.go +++ b/doc.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js // Package websocket implements the RFC 6455 WebSocket protocol. // diff --git a/example_test.go b/example_test.go index 4cc0cf11..7026e311 100644 --- a/example_test.go +++ b/example_test.go @@ -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) @@ -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) diff --git a/export_test.go b/export_test.go index d3443991..e0071922 100644 --- a/export_test.go +++ b/export_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket @@ -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 diff --git a/frame_test.go b/frame_test.go index 08874cb5..6b2e21f5 100644 --- a/frame_test.go +++ b/frame_test.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket @@ -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(), diff --git a/internal/errd/wrap.go b/internal/errd/wrap.go index 6e779131..c80d0a65 100644 --- a/internal/errd/wrap.go +++ b/internal/errd/wrap.go @@ -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)...) } diff --git a/internal/examples/chat/chat.go b/internal/examples/chat/chat.go index 29f304b7..cc24ac7c 100644 --- a/internal/examples/chat/chat.go +++ b/internal/examples/chat/chat.go @@ -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 diff --git a/internal/examples/echo/server.go b/internal/examples/echo/server.go index 37e2f2c4..5748b110 100644 --- a/internal/examples/echo/server.go +++ b/internal/examples/echo/server.go @@ -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) { diff --git a/internal/test/assert/assert.go b/internal/test/assert/assert.go index 1b90cc9f..7fd98b08 100644 --- a/internal/test/assert/assert.go +++ b/internal/test/assert/assert.go @@ -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) { @@ -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) diff --git a/internal/test/wstest/pipe.go b/internal/test/wstest/pipe.go index b8cf094d..0e7fdb84 100644 --- a/internal/test/wstest/pipe.go +++ b/internal/test/wstest/pipe.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package wstest diff --git a/internal/thirdparty/gin_test.go b/internal/thirdparty/gin_test.go index bd30ebdd..4c88337c 100644 --- a/internal/thirdparty/gin_test.go +++ b/internal/thirdparty/gin_test.go @@ -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) diff --git a/internal/wsjs/wsjs_js.go b/internal/wsjs/wsjs_js.go index 11eb59cb..45ecf49d 100644 --- a/internal/wsjs/wsjs_js.go +++ b/internal/wsjs/wsjs_js.go @@ -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 } @@ -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 }) @@ -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 @@ -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 { diff --git a/mask_test.go b/mask_test.go index 00a9f0a2..7d6aedd7 100644 --- a/mask_test.go +++ b/mask_test.go @@ -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) diff --git a/netconn.go b/netconn.go index b118e4d3..1f73b04b 100644 --- a/netconn.go +++ b/netconn.go @@ -188,8 +188,7 @@ func (nc *netConn) read(p []byte) (int, error) { return n, err } -type websocketAddr struct { -} +type websocketAddr struct{} func (a websocketAddr) Network() string { return "websocket" diff --git a/netconn_notjs.go b/netconn_notjs.go index f3eb0d66..cab76349 100644 --- a/netconn_notjs.go +++ b/netconn_notjs.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/read.go b/read.go index 2db22435..aab9e141 100644 --- a/read.go +++ b/read.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket diff --git a/write.go b/write.go index 7324de74..7104b227 100644 --- a/write.go +++ b/write.go @@ -1,5 +1,4 @@ //go:build !js -// +build !js package websocket @@ -351,10 +350,7 @@ func (c *Conn) writeFramePayload(p []byte) (n int, err error) { // Start of next write in the buffer. i := c.bw.Buffered() - j := len(p) - if j > c.bw.Available() { - j = c.bw.Available() - } + j := min(len(p), c.bw.Available()) _, err := c.bw.Write(p[:j]) if err != nil { diff --git a/ws_js.go b/ws_js.go index 5e324c47..8d52aeab 100644 --- a/ws_js.go +++ b/ws_js.go @@ -196,7 +196,7 @@ func (c *Conn) Ping(ctx context.Context) error { // Write writes a message of the given type to the connection. // Always non blocking. func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error { - err := c.write(ctx, typ, p) + err := c.write(typ, p) if err != nil { // Have to ensure the WebSocket is closed after a write error // to match the Go API. It can only error if the message type @@ -210,7 +210,7 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error { return nil } -func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) error { +func (c *Conn) write(typ MessageType, p []byte) error { if c.isClosed() { return net.ErrClosed } diff --git a/ws_js_test.go b/ws_js_test.go index b56ad16b..1fa242f4 100644 --- a/ws_js_test.go +++ b/ws_js_test.go @@ -28,7 +28,7 @@ func TestWasm(t *testing.T) { assert.Equal(t, "response code", http.StatusSwitchingProtocols, resp.StatusCode) c.SetReadLimit(65536) - for i := 0; i < 10; i++ { + for range 10 { err = wstest.Echo(ctx, c, 65536) assert.Success(t, err) } diff --git a/wsjson/wsjson.go b/wsjson/wsjson.go index 05e7cfa1..ffa068bf 100644 --- a/wsjson/wsjson.go +++ b/wsjson/wsjson.go @@ -14,11 +14,11 @@ import ( // Read reads a JSON message from c into v. // It will reuse buffers in between calls to avoid allocations. -func Read(ctx context.Context, c *websocket.Conn, v interface{}) error { +func Read(ctx context.Context, c *websocket.Conn, v any) error { return read(ctx, c, v) } -func read(ctx context.Context, c *websocket.Conn, v interface{}) (err error) { +func read(ctx context.Context, c *websocket.Conn, v any) (err error) { defer errd.Wrap(&err, "failed to read JSON message") _, r, err := c.Reader(ctx) @@ -45,11 +45,11 @@ func read(ctx context.Context, c *websocket.Conn, v interface{}) (err error) { // Write writes the JSON message v to c. // It will reuse buffers in between calls to avoid allocations. -func Write(ctx context.Context, c *websocket.Conn, v interface{}) error { +func Write(ctx context.Context, c *websocket.Conn, v any) error { return write(ctx, c, v) } -func write(ctx context.Context, c *websocket.Conn, v interface{}) (err error) { +func write(ctx context.Context, c *websocket.Conn, v any) (err error) { defer errd.Wrap(&err, "failed to write JSON message") // json.Marshal cannot reuse buffers between calls as it has to return pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy