Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb89d1b

Browse files
committed
fix: avoid importing net/http on wasm
Using goweight:https://github.com/paralin/goweightGOOS=js GOARCH=wasm goweight | lessWithout 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 parente46e020 commitdb89d1b

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,typMessageType,p []byte)error {
199-
err:=c.write(ctx,typ,p)
198+
err:=c.write(typ,p)
200199
iferr!=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
returnnil
211210
}
212211

213-
func (c*Conn)write(ctx context.Context,typMessageType,p []byte)error {
212+
func (c*Conn)write(typMessageType,p []byte)error {
214213
ifc.isClosed() {
215214
returnnet.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-
funcDial(ctx context.Context,urlstring,opts*DialOptions) (*Conn,*http.Response,error) {
289+
funcDial(ctx context.Context,urlstring,opts*DialOptions) (*Conn,*struct{},error) {
291290
c,resp,err:=dial(ctx,url,opts)
292291
iferr!=nil {
293292
returnnil,nil,fmt.Errorf("failed to WebSocket dial %q: %w",url,err)
294293
}
295294
returnc,resp,nil
296295
}
297296

298-
funcdial(ctx context.Context,urlstring,opts*DialOptions) (*Conn,*http.Response,error) {
297+
funcdial(ctx context.Context,urlstring,opts*DialOptions) (*Conn,*struct{},error) {
299298
ifopts==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
returnnil,nil,ctx.Err()
326325
case<-opench:
327-
returnc,&http.Response{
328-
StatusCode:http.StatusSwitchingProtocols,
329-
},nil
326+
returnc,nil,nil
330327
case<-c.closed:
331328
returnnil,nil,net.ErrClosed
332329
}
@@ -442,7 +439,7 @@ type AcceptOptions struct {
442439
}
443440

444441
// Accept is stubbed out for Wasm.
445-
funcAccept(whttp.ResponseWriter,r*http.Request,opts*AcceptOptions) (*Conn,error) {
442+
funcAccept(wany,rany,opts*AcceptOptions) (*Conn,error) {
446443
returnnil,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
defercancel()
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
deferc.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
fori:=0;i<10;i++ {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp