Skip to content
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

fix: avoid importing net/http on wasm #449

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 6 additions & 9 deletions ws_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net"
"net/http"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -196,7 +195,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
Expand All @@ -210,7 +209,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
}
Expand Down Expand Up @@ -287,15 +286,15 @@ type DialOptions struct {
// The passed context bounds the maximum time spent waiting for the connection to open.
// The returned *http.Response is always nil or a mock. It's only in the signature
// to match the core API.
func Dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Response, error) {
func Dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *struct{}, error) {
c, resp, err := dial(ctx, url, opts)
if err != nil {
return nil, nil, fmt.Errorf("failed to WebSocket dial %q: %w", url, err)
}
return c, resp, nil
}

func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Response, error) {
func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *struct{}, error) {
if opts == nil {
opts = &DialOptions{}
}
Expand Down Expand Up @@ -324,9 +323,7 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp
c.Close(StatusPolicyViolation, "dial timed out")
return nil, nil, ctx.Err()
case <-opench:
return c, &http.Response{
StatusCode: http.StatusSwitchingProtocols,
}, nil
return c, nil, nil
case <-c.closed:
return nil, nil, net.ErrClosed
}
Expand Down Expand Up @@ -442,7 +439,7 @@ type AcceptOptions struct {
}

// Accept is stubbed out for Wasm.
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
func Accept(w any, r any, opts *AcceptOptions) (*Conn, error) {
return nil, errors.New("unimplemented")
}

Expand Down
5 changes: 2 additions & 3 deletions ws_js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package websocket_test

import (
"context"
"net/http"
"os"
"testing"
"time"
Expand All @@ -18,14 +17,14 @@ func TestWasm(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

c, resp, err := websocket.Dial(ctx, os.Getenv("WS_ECHO_SERVER_URL"), &websocket.DialOptions{
// NOTE: the response from websocket.Dial is a mock on js and not actually used.
c, _, err := websocket.Dial(ctx, os.Getenv("WS_ECHO_SERVER_URL"), &websocket.DialOptions{
Subprotocols: []string{"echo"},
})
assert.Success(t, err)
defer c.Close(websocket.StatusInternalError, "")

assert.Equal(t, "subprotocol", "echo", c.Subprotocol())
assert.Equal(t, "response code", http.StatusSwitchingProtocols, resp.StatusCode)

c.SetReadLimit(65536)
for i := 0; i < 10; i++ {
Expand Down
Loading