Skip to content

Commit

Permalink
fix: avoid importing net/http on wasm
Browse files Browse the repository at this point in the history
Using goweight: https://github.com/paralin/goweight

GOOS=js GOARCH=wasm goweight | less

Without this change:

  7.9 MB runtime
  6.6 MB net/http
  3.1 MB net
  3.0 MB crypto/tls
  2.0 MB reflect
  1.4 MB math/big
  1.3 MB crypto/x509
  935 kB os
  ...

With this change:

  7.9 MB runtime
  3.1 MB net
  2.0 MB reflect
  935 kB os
  ...

Slight modifications to avoid importing net/http reduce the binary size significantly.

Signed-off-by: Christian Stewart <christian@aperture.us>
  • Loading branch information
paralin committed Aug 20, 2024
1 parent e46e020 commit db89d1b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
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

0 comments on commit db89d1b

Please sign in to comment.