endpoints

package
v0.0.0-...-5f8c399 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidEndpoint = errors.New("invalid endpoint")
	ErrStreamClosed    = errors.New("stream closed")
)

Functions

func Batch

func Batch[TParams rpc.ParamsType, TResult rpc.ResultType](ctx context.Context, c EndpointClient, requests []RequestInfo[TParams]) ([]*rpc.Response[TResult], error)

Batch

func BatchTo

func BatchTo[TParams rpc.ParamsType, TResult rpc.ResultType](ctx context.Context, c EndpointClient, requests []RequestInfo[TParams], results []*rpc.Response[TResult]) error

func Notify

func Notify[TParams rpc.ParamsType](ctx context.Context, c EndpointClient, method string, params TParams) error

notify

func ProcessRpcRequest

func ProcessRpcRequest(ctx context.Context, reg RpcMethodRegistry, rpcMsg *rpc.Message) interface{}

func RegisterEndpointMethod

func RegisterEndpointMethod[TParam rpc.ParamsType, TResult rpc.ResultType](c EndpointServer, method string, handler RpcMethod[TParam, TResult])

register method to server endpoint

func RegisterMethod

func RegisterMethod[TParam rpc.ParamsType, TResult rpc.ResultType](reg RpcMethodRegistry, method string, handler RpcMethod[TParam, TResult])

func RegisterServerMuxEndpointMethod

func RegisterServerMuxEndpointMethod[TParam rpc.ParamsType, TResult rpc.ResultType](mux *ServerMux, endpoint string, method string, handler RpcMethod[TParam, TResult])

func Request

func Request[TParams rpc.ParamsType, TResult rpc.ResultType](ctx context.Context, c EndpointClient, method string, params TParams) (*rpc.Response[TResult], error)

request

func RequestTo

func RequestTo[TParams rpc.ParamsType, TResult rpc.ResultType](ctx context.Context, c EndpointClient, method string, params TParams, result *rpc.Response[TResult]) error

Types

type EndpointClient

type EndpointClient interface {
	WriteObject(obj interface{}) error
	RegisterPendingRequest(id interface{}) <-chan rpc.Message
	UnregisterPendingRequest(id interface{})
	Close() error
	IsClosed() bool
	UseLogger(logger *slog.Logger)
}

type EndpointRegistry

type EndpointRegistry map[string]RpcMethodRegistry

type EndpointServer

type EndpointServer interface {
	GetMethods() RpcMethodRegistry
	UseLogger(logger *slog.Logger)
}

type HttpClientEndpoint

type HttpClientEndpoint struct {
	*http.Client
	// contains filtered or unexported fields
}

func NewHttpClientEndpoint

func NewHttpClientEndpoint(baseUrl string, client *http.Client) *HttpClientEndpoint

func (*HttpClientEndpoint) Close

func (c *HttpClientEndpoint) Close() error

func (*HttpClientEndpoint) IsClosed

func (c *HttpClientEndpoint) IsClosed() bool

func (*HttpClientEndpoint) RegisterPendingRequest

func (c *HttpClientEndpoint) RegisterPendingRequest(requestID interface{}) <-chan rpc.Message

func (*HttpClientEndpoint) UnregisterPendingRequest

func (c *HttpClientEndpoint) UnregisterPendingRequest(requestID interface{})

func (*HttpClientEndpoint) UseLogger

func (c *HttpClientEndpoint) UseLogger(logger *slog.Logger)

func (*HttpClientEndpoint) WriteObject

func (c *HttpClientEndpoint) WriteObject(object interface{}) error

type ObjectCodec

type ObjectCodec interface {
	// WriteObject writes a JSON-RPC 2.0 object to the stream.
	WriteObject(stream io.Writer, obj interface{}) error

	// ReadObject reads the next JSON-RPC 2.0 object from the stream
	// and stores it in the value pointed to by v.
	ReadObject(stream *bufio.Reader, v interface{}) error
}

An ObjectCodec specifies how to encode and decode a JSON-RPC 2.0 object in a stream.

type ObjectStream

type ObjectStream interface {
	// WriteObject writes a JSON-RPC 2.0 object to the stream.
	WriteObject(obj interface{}) error

	// ReadObject reads the next JSON-RPC 2.0 object from the stream
	// and stores it in the value pointed to by v.
	ReadObject(v interface{}) error

	io.Closer
}

An ObjectStream is a bidirectional stream of JSON-RPC 2.0 objects.

func NewBufferedStream

func NewBufferedStream(conn io.ReadWriteCloser, codec ObjectCodec) ObjectStream

NewBufferedStream creates a buffered stream from a network connection (or other similar interface). The underlying objectStream is used to produce the bytes to write to the stream for the JSON-RPC 2.0 objects.

func NewPlainObjectStream

func NewPlainObjectStream(conn io.ReadWriteCloser) ObjectStream

NewPlainObjectStream creates a buffered stream from a network connection (or other similar interface). The underlying objectStream produces plain JSON-RPC 2.0 objects without a header.

type PlainObjectCodec deprecated

type PlainObjectCodec struct {
	// contains filtered or unexported fields
}

PlainObjectCodec reads/writes plain JSON-RPC 2.0 objects without a header.

Deprecated: use NewPlainObjectStream

func (PlainObjectCodec) ReadObject

func (c PlainObjectCodec) ReadObject(stream *bufio.Reader, v interface{}) error

ReadObject implements ObjectCodec.

func (PlainObjectCodec) WriteObject

func (c PlainObjectCodec) WriteObject(stream io.Writer, v interface{}) error

WriteObject implements ObjectCodec.

type RequestInfo

type RequestInfo[TParams rpc.ParamsType] struct {
	Method         string
	Params         TParams
	IsNotification bool
}

type RpcHandler

type RpcHandler func(ctx context.Context, rpcMessage *rpc.Message) interface{}

type RpcMethod

type RpcMethod[TParam rpc.ParamsType, TResult rpc.ResultType] func(ctx context.Context, p TParam) (TResult, *rpc.Error)

type RpcMethodRegistry

type RpcMethodRegistry map[string]RpcHandler

func NewMethodRegistry

func NewMethodRegistry() RpcMethodRegistry

type ServerMux

type ServerMux struct {
	http.ServeMux
	// contains filtered or unexported fields
}

func NewServerMux

func NewServerMux() *ServerMux

func (*ServerMux) GetEndpoints

func (mux *ServerMux) GetEndpoints() EndpointRegistry

func (*ServerMux) GetMethods

func (mux *ServerMux) GetMethods() RpcMethodRegistry

func (*ServerMux) RegisterEndpoint

func (mux *ServerMux) RegisterEndpoint(path string)

func (*ServerMux) UseLogger

func (mux *ServerMux) UseLogger(logger *slog.Logger)

type StreamEndpoint

type StreamEndpoint struct {
	// contains filtered or unexported fields
}

StreamEndpoint is a endpoint that implements both client and server side of jsonrpc over a stream. Usually used over tcp or stdio streams.

func NewStreamEndpoint

func NewStreamEndpoint(ctx context.Context, stream ObjectStream) *StreamEndpoint

func (*StreamEndpoint) Close

func (c *StreamEndpoint) Close() error

func (*StreamEndpoint) GetMethods

func (c *StreamEndpoint) GetMethods() RpcMethodRegistry

func (*StreamEndpoint) GetOnCloseListener

func (c *StreamEndpoint) GetOnCloseListener() <-chan struct{}

returns a channel that will be closed when the connection is closed

func (*StreamEndpoint) IsClosed

func (c *StreamEndpoint) IsClosed() bool

func (*StreamEndpoint) ListMethods

func (c *StreamEndpoint) ListMethods() []string

func (*StreamEndpoint) RegisterPendingRequest

func (c *StreamEndpoint) RegisterPendingRequest(requestId interface{}) <-chan rpc.Message

func (*StreamEndpoint) UnregisterPendingRequest

func (c *StreamEndpoint) UnregisterPendingRequest(requestId interface{})

func (*StreamEndpoint) UseLogger

func (c *StreamEndpoint) UseLogger(logger *slog.Logger)

func (*StreamEndpoint) WriteObject

func (c *StreamEndpoint) WriteObject(obj interface{}) error

type VSCodeObjectCodec

type VSCodeObjectCodec struct{}

VSCodeObjectCodec reads/writes JSON-RPC 2.0 objects with Content-Length and Content-Type headers, as specified by https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#base-protocol.

func (VSCodeObjectCodec) ReadObject

func (VSCodeObjectCodec) ReadObject(stream *bufio.Reader, v interface{}) error

ReadObject implements ObjectCodec.

func (VSCodeObjectCodec) WriteObject

func (VSCodeObjectCodec) WriteObject(stream io.Writer, obj interface{}) error

WriteObject implements ObjectCodec.

type VarintObjectCodec

type VarintObjectCodec struct{}

VarintObjectCodec reads/writes JSON-RPC 2.0 objects with a varint header that encodes the byte length.

func (VarintObjectCodec) ReadObject

func (VarintObjectCodec) ReadObject(stream *bufio.Reader, v interface{}) error

ReadObject implements ObjectCodec.

func (VarintObjectCodec) WriteObject

func (VarintObjectCodec) WriteObject(stream io.Writer, obj interface{}) error

WriteObject implements ObjectCodec.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL