Skip to content

Commit 4ad7142

Browse files
authored
Merge pull request #28 from nhooyr/stubs
API Stubs
2 parents 04b7d0d + e2a32fc commit 4ad7142

27 files changed

+741
-59
lines changed

.github/fmt/entrypoint.sh

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
#!/usr/bin/env bash
22

3-
source .github/lib.sh
3+
source .github/lib.sh || exit 1
44

5-
if [[ $(gofmt -l -s .) != "" ]]; then
6-
echo "files are not formatted correctly"
7-
echo "please run:"
8-
echo "gofmt -w -s ."
9-
exit 1
10-
fi
5+
gen() {
6+
# Unfortunately, this is the only way to ensure go.mod and go.sum are correct.
7+
# See https://github.com/golang/go/issues/27005
8+
go list ./... > /dev/null
9+
go mod tidy
1110

12-
out=$(go run golang.org/x/tools/cmd/goimports -l -local=nhooyr.io/ws .)
13-
if [[ $out != "" ]]; then
14-
echo "imports are not formatted correctly"
15-
echo "please run:"
16-
echo "goimports -w -local=nhooyr.io/ws ."
17-
exit 1
18-
fi
11+
go install golang.org/x/tools/cmd/stringer
12+
go generate ./...
13+
}
14+
15+
fmt() {
16+
gofmt -w -s .
17+
go run go.coder.com/go-tools/cmd/goimports -w "-local=$(go list -m)" .
18+
go run mvdan.cc/sh/cmd/shfmt -w -s -sr .
19+
}
20+
21+
gen
22+
fmt
1923

20-
out=$(go run mvdan.cc/sh/cmd/shfmt -l -s -sr .)
21-
if [[ $out != "" ]]; then
22-
echo "shell scripts are not formatted correctly"
24+
if [[ $CI && $(unstaged_files) != "" ]]; then
25+
set +x
26+
echo
27+
echo "files either need generation or are formatted incorrectly"
2328
echo "please run:"
24-
echo "shfmt -w -s -sr ."
29+
echo "./test.sh"
30+
echo
31+
git status
2532
exit 1
2633
fi

.github/lib.sh

100755100644
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,16 @@
33
set -euxo pipefail
44

55
export GO111MODULE=on
6-
export GOFLAGS=-mod=readonly
6+
export PAGER=cat
7+
8+
# shellcheck disable=SC2034
9+
# CI is used by the scripts that source this file.
10+
export CI=${GITHUB_ACTION-}
11+
12+
if [[ $CI ]]; then
13+
export GOFLAGS=-mod=readonly
14+
fi
15+
16+
unstaged_files() {
17+
git ls-files --other --modified --exclude-standard
18+
}

.github/lint/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM codercom/playcicache
2+
3+
LABEL "com.github.actions.name"="lint"
4+
LABEL "com.github.actions.description"="lint"
5+
LABEL "com.github.actions.icon"="code"
6+
LABEL "com.github.actions.color"="purple"
7+
8+
COPY entrypoint.sh /entrypoint.sh
9+
10+
CMD ["/entrypoint.sh"]

.github/lint/entrypoint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
source .github/lib.sh || exit 1
4+
5+
(
6+
shopt -s globstar nullglob dotglob
7+
shellcheck ./**/*.sh
8+
)
9+
10+
go vet -composites=false ./...
11+
go run golang.org/x/lint/golint -set_exit_status ./...

.github/main.workflow

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
workflow "main" {
22
on = "push"
3-
resolves = ["fmt", "test"]
3+
resolves = ["fmt", "lint", "test"]
4+
}
5+
6+
action "lint" {
7+
uses = "./.github/lint"
48
}
59

610
action "fmt" {

.github/test/entrypoint.sh

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
#!/usr/bin/env bash
22

3-
source .github/lib.sh
3+
source .github/lib.sh || exit 1
44

5-
function gomod_help() {
6-
echo
7-
echo "you may need to update go.mod/go.sum via:"
8-
echo "go list all > /dev/null"
9-
echo "go mod tidy"
10-
echo
11-
echo "or git add files to staging"
12-
exit 1
13-
}
14-
15-
go list ./... > /dev/null || gomod_help
16-
go mod tidy
17-
18-
# Until https://github.com/golang/go/issues/27005 the previous command can actually modify go.sum so we need to ensure its not changed.
19-
if [[ $(git diff --name-only) != "" ]]; then
20-
git diff
21-
gomod_help
22-
fi
23-
24-
mapfile -t scripts <<< "$(find . -type f -name "*.sh")"
25-
shellcheck "${scripts[@]}"
5+
COVERAGE_PROFILE=$(mktemp)
6+
go test -race -v "-coverprofile=${COVERAGE_PROFILE}" -vet=off ./...
7+
go tool cover "-func=${COVERAGE_PROFILE}"
268

27-
go vet -composites=false ./...
28-
29-
go test -race -v -coverprofile=coverage.out -vet=off ./...
30-
31-
if [[ -z ${GITHUB_ACTION-} ]]; then
32-
go tool cover -html=coverage.out
9+
if [[ $CI ]]; then
10+
bash <(curl -s https://codecov.io/bash) -f "$COVERAGE_PROFILE"
3311
else
34-
bash <(curl -s https://codecov.io/bash)
35-
fi
12+
go tool cover "-html=${COVERAGE_PROFILE}" -o=coverage.html
3613

37-
rm coverage.out
14+
set +x
15+
echo
16+
echo "please open coverage.html to see detailed test coverage stats"
17+
fi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage.html

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,32 @@ This library is in heavy development.
1313
```bash
1414
go get nhooyr.io/ws
1515
```
16+
17+
## Why
18+
19+
There is no other Go WebSocket library with a clean API.
20+
21+
Comparisons with existing WebSocket libraries below.
22+
23+
### [x/net/websocket](https://godoc.org/golang.org/x/net/websocket)
24+
25+
26+
Unmaintained and the API does not reflect WebSocket semantics.
27+
28+
See https://github.com/golang/go/issues/18152
29+
30+
### [gorilla/websocket](https://github.com/gorilla/websocket)
31+
32+
This package is the community standard but it is very old and over time
33+
has accumulated cruft. There are many ways to do the same thing and the API
34+
overall is just not very clear.
35+
36+
The callback hooks are also confusing. The API for this library has been designed
37+
such that there is only one way to do things and callbacks have been avoided.
38+
39+
Performance sensitive applications should use ws/wscore directly.
40+
41+
## [gobwas/ws](https://github.com/gobwas/ws)
42+
43+
This library has an extremely flexible API but that comes at a cost of usability
44+
and clarity. Its just not clear and simple how to do things in a safe manner.

accept.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ws
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
// AcceptOption is an option that can be passed to Accept.
9+
type AcceptOption interface {
10+
acceptOption()
11+
fmt.Stringer
12+
}
13+
14+
// AcceptSubprotocols list the subprotocols that Accept will negotiate with a client.
15+
// The first protocol that a client supports will be negotiated.
16+
// Pass "" as a subprotocol if you would like to allow the default protocol.
17+
func AcceptSubprotocols(subprotocols ...string) AcceptOption {
18+
panic("TODO")
19+
}
20+
21+
// AcceptOrigins lists the origins that Accept will accept.
22+
// Accept will always accept r.Host as the origin so you do not need to
23+
// specify that with this option.
24+
// Use this option with caution to avoid exposing your WebSocket
25+
// server to a CSRF attack.
26+
// See https://stackoverflow.com/a/37837709/4283659
27+
func AcceptOrigins(origins ...string) AcceptOption {
28+
panic("TODO")
29+
}
30+
31+
// Accept accepts a WebSocket handshake from a client and upgrades the
32+
// the connection to WebSocket.
33+
// Accept will reject the handshake if the Origin is not the same as the Host unless
34+
// InsecureAcceptOrigin is passed.
35+
func Accept(w http.ResponseWriter, r *http.Request, opts ...AcceptOption) (*Conn, error) {
36+
panic("TODO")
37+
}

datatype.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ws
2+
3+
import (
4+
"nhooyr.io/ws/wscore"
5+
)
6+
7+
// DataType represents the Opcode of a WebSocket data frame.
8+
//go:generate stringer -type=DataType
9+
type DataType int
10+
11+
// DataType constants.
12+
const (
13+
Text DataType = DataType(wscore.OpText)
14+
Binary DataType = DataType(wscore.OpBinary)
15+
)

datatype_string.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dial.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ws
2+
3+
import (
4+
"context"
5+
"encoding/base64"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
// DialOption represents a dial option that can be passed to Dial.
11+
type DialOption interface {
12+
dialOption()
13+
fmt.Stringer
14+
}
15+
16+
// DialHTTPClient is the http client used for the handshake.
17+
// Its Transport must use HTTP/1.1 and must return writable bodies
18+
// for WebSocket handshakes.
19+
// http.Transport does this correctly.
20+
func DialHTTPClient(h *http.Client) DialOption {
21+
panic("TODO")
22+
}
23+
24+
// DialHeader are the HTTP headers included in the handshake request.
25+
func DialHeader(h http.Header) DialOption {
26+
panic("TODO")
27+
}
28+
29+
// DialSubprotocols accepts a slice of protcols to include in the Sec-WebSocket-Protocol header.
30+
func DialSubprotocols(subprotocols ...string) DialOption {
31+
panic("TODO")
32+
}
33+
34+
// We use this key for all client requests as the Sec-WebSocket-Key header is useless.
35+
// See https://stackoverflow.com/a/37074398/4283659.
36+
var secWebSocketKey = base64.StdEncoding.EncodeToString(make([]byte, 16))
37+
38+
// Dial performs a websocket handshake on the given url with the given options.
39+
func Dial(ctx context.Context, u string, opts ...DialOption) (*Conn, *http.Response, error) {
40+
panic("TODO")
41+
}

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package ws implements the WebSocket protocol defined in RFC 6455.
2+
package ws

0 commit comments

Comments
 (0)