Documentation ¶
Index ¶
- Constants
- Variables
- func Call[T any](ctx context.Context, c Conn, method string, args ...any) (*T, error)
- func CallInto(ctx context.Context, c Conn, result any, method string, args ...any) error
- func ContextWithConn(ctx context.Context, c Conn) context.Context
- func Do[T any](ctx context.Context, c Conn, method string, args any) (*T, error)
- func IsBatchMessage(raw json.RawMessage) bool
- func MarshalError(err error) []byte
- func MarshalMessage(m *Message, enc *jx.Encoder) (err error)
- func NewNull() json.RawMessage
- func NewStringReader(x string) io.ReadCloser
- func UnmarshalMessage(m *Message, dec *jx.Decoder) error
- func WrapErr(data any, code int, err error) error
- type ChainHandler
- type Conn
- type DataError
- type Doer
- type DummyClient
- type Error
- type ErrorInternalError
- type ErrorInvalidMessage
- type ErrorInvalidParams
- type ErrorInvalidRequest
- type ErrorMethodNotFound
- type ErrorParse
- type ErrorSubscriptionNotFound
- type HTTPError
- type Handler
- type HandlerFunc
- type HttpInfo
- type ID
- type JsonError
- type Listener
- type Message
- type Middleware
- type Middlewares
- type Mounter
- type Notifier
- type PeerInfo
- type Reader
- type ReaderWriter
- type Request
- type ResponseWriter
- type Writer
Constants ¶
const ( ErrorCodeDefault = -32000 ErrorCodeApplication = -32080 ErrorCodeJrpc = -42000 )
const VersionString = "2.0"
Version represents a JSON-RPC version.
Variables ¶
var ( ErrIllegalExtraField = errors.New("invalid extra field") ErrSendAlreadyCalled = errors.New("send already called") ErrCantSendNotification = errors.New("can't send to a notification") )
var Null = json.RawMessage("null")
Functions ¶
func ContextWithConn ¶
ClientFromContext retrieves the client from the context, if any. This can be used to perform 'reverse calls' in a handler method.
func IsBatchMessage ¶
func IsBatchMessage(raw json.RawMessage) bool
isBatch returns true when the first non-whitespace characters is '['
func MarshalError ¶ added in v0.3.11
func NewNull ¶
func NewNull() json.RawMessage
func NewStringReader ¶
func NewStringReader(x string) io.ReadCloser
Types ¶
type ChainHandler ¶
type ChainHandler struct { Endpoint Handler Middlewares Middlewares // contains filtered or unexported fields }
ChainHandler is a Handler with support for handler composition and execution.
func (*ChainHandler) ServeRPC ¶
func (c *ChainHandler) ServeRPC(w ResponseWriter, r *Request)
type DataError ¶
type DataError interface { Error() string // returns the message ErrorCode() int // returns the error code ErrorData() any // returns the error data }
A DataError contains some data in addition to the error message.
type DummyClient ¶
type DummyClient struct{}
func (*DummyClient) Close ¶
func (d *DummyClient) Close() error
func (*DummyClient) Closed ¶
func (d *DummyClient) Closed() <-chan struct{}
func (*DummyClient) Mount ¶
func (d *DummyClient) Mount(_ Middleware)
type ErrorInternalError ¶
type ErrorInternalError struct {
// contains filtered or unexported fields
}
unable to decode supplied params, or an invalid number of parameters
func NewInternalError ¶
func NewInternalError(message string) *ErrorInternalError
func (*ErrorInternalError) Error ¶
func (e *ErrorInternalError) Error() string
func (*ErrorInternalError) ErrorCode ¶
func (e *ErrorInternalError) ErrorCode() int
type ErrorInvalidMessage ¶
type ErrorInvalidMessage struct {
// contains filtered or unexported fields
}
received message is invalid
func (*ErrorInvalidMessage) Error ¶
func (e *ErrorInvalidMessage) Error() string
func (*ErrorInvalidMessage) ErrorCode ¶
func (e *ErrorInvalidMessage) ErrorCode() int
type ErrorInvalidParams ¶
type ErrorInvalidParams struct {
// contains filtered or unexported fields
}
unable to decode supplied params, or an invalid number of parameters
func NewInvalidParamsError ¶
func NewInvalidParamsError(message string) *ErrorInvalidParams
func (*ErrorInvalidParams) Error ¶
func (e *ErrorInvalidParams) Error() string
func (*ErrorInvalidParams) ErrorCode ¶
func (e *ErrorInvalidParams) ErrorCode() int
type ErrorInvalidRequest ¶
type ErrorInvalidRequest struct {
// contains filtered or unexported fields
}
received message isn't a valid request
func NewInvalidRequestError ¶
func NewInvalidRequestError(message string) *ErrorInvalidRequest
func (*ErrorInvalidRequest) Error ¶
func (e *ErrorInvalidRequest) Error() string
func (*ErrorInvalidRequest) ErrorCode ¶
func (e *ErrorInvalidRequest) ErrorCode() int
type ErrorMethodNotFound ¶
type ErrorMethodNotFound struct {
// contains filtered or unexported fields
}
func NewMethodNotFoundError ¶
func NewMethodNotFoundError(method string) *ErrorMethodNotFound
func (*ErrorMethodNotFound) Error ¶
func (e *ErrorMethodNotFound) Error() string
func (*ErrorMethodNotFound) ErrorCode ¶
func (e *ErrorMethodNotFound) ErrorCode() int
type ErrorParse ¶
type ErrorParse struct {
// contains filtered or unexported fields
}
Invalid JSON was received by the server.
func (*ErrorParse) Error ¶
func (e *ErrorParse) Error() string
func (*ErrorParse) ErrorCode ¶
func (e *ErrorParse) ErrorCode() int
type ErrorSubscriptionNotFound ¶
type ErrorSubscriptionNotFound struct {
// contains filtered or unexported fields
}
func (*ErrorSubscriptionNotFound) Error ¶
func (e *ErrorSubscriptionNotFound) Error() string
func (*ErrorSubscriptionNotFound) ErrorCode ¶
func (e *ErrorSubscriptionNotFound) ErrorCode() int
type HTTPError ¶
HTTPError is returned by client operations when the HTTP status code of the response is not a 2xx status.
type Handler ¶
type Handler interface {
ServeRPC(w ResponseWriter, r *Request)
}
http.handler, but for jrpc
type HandlerFunc ¶
type HandlerFunc func(w ResponseWriter, r *Request)
http.HandlerFunc,but for jrpc
func (HandlerFunc) ServeRPC ¶
func (fn HandlerFunc) ServeRPC(w ResponseWriter, r *Request)
ServeRPC implements (jsonrpc.Handler).ServeRPC
type ID ¶
type ID json.RawMessage
ID is a Request identifier.
alternatively, ID can be null
func NewNullIDPtr ¶
func NewNullIDPtr() *ID
func NewNumberIDPtr ¶
func NewStringIDPtr ¶
func (ID) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type JsonError ¶
type JsonError struct { Code int `json:"code"` Message string `json:"message"` Data any `json:"data,omitempty"` }
encapsulate json rpc error into struct
type Listener ¶
type Listener interface { Accept() (ReaderWriter, error) Close() error Addr() net.Addr }
type Message ¶
type Message struct { ID *ID `json:"id,omitempty"` Method string `json:"method,omitempty"` Params json.RawMessage `json:"params,omitempty"` Error error `json:"error,omitempty"` Result io.ReadCloser `json:"result,omitempty"` }
A value of this type can a JSON-RPC request, notification, successful response or error response. Which one it is depends on the fields.
func ParseMessage ¶
func ParseMessage(in json.RawMessage) ([]*Message, bool)
parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error checks in this function because the raw message has already been syntax-checked when it is called. Any non-JSON-RPC messages in the input return the zero value of Message.
func ReadMessage ¶
parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error checks in this function because the raw message has already been syntax-checked when it is called. Any non-JSON-RPC messages in the input return the zero value of Message.
func (Message) MarshalJSON ¶
func (*Message) UnmarshalJSON ¶
type Middleware ¶
type Middlewares ¶
type Middlewares []Middleware
Middlewares type is a slice of standard middleware handlers with methods to compose middleware chains and Handler's.
func Chain ¶
func Chain(middlewares ...func(Handler) Handler) Middlewares
Chain returns a Middlewares type from a slice of middleware handlers.
func (Middlewares) Handler ¶
func (mws Middlewares) Handler(h Handler) Handler
Handler builds and returns a Handler from the chain of middlewares, with `h Handler` as the final handler.
func (Middlewares) HandlerFunc ¶
func (mws Middlewares) HandlerFunc(h HandlerFunc) Handler
HandlerFunc builds and returns a Handler from the chain of middlewares, with `h Handler` as the final handler.
type Mounter ¶
type Mounter interface {
Mount(Middleware)
}
type Reader ¶
type Reader interface { // gets the peer info PeerInfo() PeerInfo // reads a batch of messages ReadBatch(ctx context.Context) (msgs []*Message, batch bool, err error) // closes the connection Close() error }
Reader can write JSON messages to its underlying connection Implementations must be safe for concurrent use
type ReaderWriter ¶
ReaderWriter represents a single stream this stream can be used to send/receive an arbitrary amount of requests and notifications
type Request ¶
type Request struct { ID *ID `json:"id,omitempty"` Method string `json:"method,omitempty"` Params json.RawMessage `json:"params,omitempty"` Peer PeerInfo `json:"-"` // contains filtered or unexported fields }
func NewRawRequest ¶
func NewRequest ¶
NewRequest makes a new request
type ResponseWriter ¶
http.ResponseWriter interface, but for jrpc
type Writer ¶
type Writer interface { // write json blob to stream io.Writer Flush() error // Closed returns a channel which is closed when the connection is closed. Closed() <-chan struct{} }
Writer can write bytes messages to their underlying connection. Implementations must be safe for concurrent use.