remove Socket.init method

This commit is contained in:
syumai 2024-01-03 20:55:26 +09:00
parent 19fa821d01
commit fe098426b2
2 changed files with 20 additions and 24 deletions

View File

@ -4,8 +4,9 @@ import (
"context"
"net"
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
"github.com/syumai/workers/internal/jsutil"
"github.com/syumai/workers/cloudflare/internal/cfruntimecontext"
)
type SecureTransport string
@ -35,9 +36,6 @@ func Connect(ctx context.Context, addr string, opts *SocketOptions) (net.Conn, e
optionsObj.Set("secureTransport", opts.SecureTransport)
}
}
sock := &Socket{}
sock.socket = connect.Invoke(addr, optionsObj)
sock.options = opts
sock.init(ctx)
return sock, nil
sockVal := connect.Invoke(addr, optionsObj)
return newSocket(ctx, sockVal), nil
}

View File

@ -11,13 +11,20 @@ import (
"github.com/syumai/workers/internal/jsutil"
)
func (sock *Socket) init(ctx context.Context) {
sock.SetDeadline(time.Now().Add(999999 * time.Hour))
sock.writer = sock.socket.Get("writable").Call("getWriter")
sock.reader = sock.socket.Get("readable").Call("getReader")
sock.rd = jsutil.ConvertStreamReaderToReader(sock.reader)
sock.ctx, sock.cancel = context.WithCancel(ctx)
return
func newSocket(ctx context.Context, sockVal js.Value) *Socket {
ctx, cancel := context.WithCancel(ctx)
reader := sockVal.Get("readable").Call("getReader")
sock := &Socket{
socket: sockVal,
writer: sockVal.Get("writable").Call("getWriter"),
reader: reader,
rd: jsutil.ConvertStreamReaderToReader(reader),
ctx: ctx,
cancel: cancel,
}
// SetDeadline returns no error
_ = sock.SetDeadline(time.Now().Add(999999 * time.Hour))
return sock
}
type Socket struct {
@ -27,8 +34,6 @@ type Socket struct {
rd io.Reader
options *SocketOptions
readDeadline time.Time
writeDeadline time.Time
@ -36,10 +41,6 @@ type Socket struct {
cancel context.CancelFunc
}
func (t *Socket) Socket() js.Value {
return t.socket
}
var _ net.Conn = (*Socket)(nil)
// Read reads data from the connection.
@ -87,11 +88,8 @@ func (t *Socket) Write(b []byte) (n int, err error) {
// StartTls will call startTls on the socket
func (t *Socket) StartTls() *Socket {
sock := &Socket{}
sock.socket = t.socket.Call("startTls")
sock.options = t.options
sock.init(t.ctx)
return sock
sockVal := t.socket.Call("startTls")
return newSocket(t.ctx, sockVal)
}
// Close closes the connection.