Skip to content

Commit db89d1b

Browse files
committed
fix: avoid importing net/http on wasm
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>
1 parent e46e020 commit db89d1b

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

ws_js.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99
"net"
10-
"net/http"
1110
"reflect"
1211
"runtime"
1312
"strings"
@@ -196,7 +195,7 @@ func (c *Conn) Ping(ctx context.Context) error {
196195
// Write writes a message of the given type to the connection.
197196
// Always non blocking.
198197
func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
199-
err := c.write(ctx, typ, p)
198+
err := c.write(typ, p)
200199
if err != nil {
201200
// Have to ensure the WebSocket is closed after a write error
202201
// to match the Go API. It can only error if the message type
@@ -210,7 +209,7 @@ func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
210209
return nil
211210
}
212211

213-
func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) error {
212+
func (c *Conn) write(typ MessageType, p []byte) error {
214213
if c.isClosed() {
215214
return net.ErrClosed
216215
}
@@ -287,15 +286,15 @@ type DialOptions struct {
287286
// The passed context bounds the maximum time spent waiting for the connection to open.
288287
// The returned *http.Response is always nil or a mock. It's only in the signature
289288
// to match the core API.
290-
func Dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Response, error) {
289+
func Dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *struct{}, error) {
291290
c, resp, err := dial(ctx, url, opts)
292291
if err != nil {
293292
return nil, nil, fmt.Errorf("failed to WebSocket dial %q: %w", url, err)
294293
}
295294
return c, resp, nil
296295
}
297296

298-
func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Response, error) {
297+
func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *struct{}, error) {
299298
if opts == nil {
300299
opts = &DialOptions{}
301300
}
@@ -324,9 +323,7 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp
324323
c.Close(StatusPolicyViolation, "dial timed out")
325324
return nil, nil, ctx.Err()
326325
case <-opench:
327-
return c, &http.Response{
328-
StatusCode: http.StatusSwitchingProtocols,
329-
}, nil
326+
return c, nil, nil
330327
case <-c.closed:
331328
return nil, nil, net.ErrClosed
332329
}
@@ -442,7 +439,7 @@ type AcceptOptions struct {
442439
}
443440

444441
// Accept is stubbed out for Wasm.
445-
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
442+
func Accept(w any, r any, opts *AcceptOptions) (*Conn, error) {
446443
return nil, errors.New("unimplemented")
447444
}
448445

ws_js_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package websocket_test
22

33
import (
44
"context"
5-
"net/http"
65
"os"
76
"testing"
87
"time"
@@ -18,14 +17,14 @@ func TestWasm(t *testing.T) {
1817
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
1918
defer cancel()
2019

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

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

3029
c.SetReadLimit(65536)
3130
for i := 0; i < 10; i++ {

0 commit comments

Comments
 (0)
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