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 EncodeObject(wr io.Writer, dat any) 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 BatchWriter
- 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 ID
- type JsonError
- type Listener
- type Message
- type MessageStream
- type MessageStreamer
- type MessageWriter
- type Middleware
- type Middlewares
- type Mounter
- type Notifier
- type PeerInfo
- type Reader
- type ReaderWriter
- type Request
- type ResponseWriter
- type ResultWriter
- type StreamingResponseWriter
- type Writer
Constants ¶
const ( ErrorCodeDefault = -32000 ErrorCodeApplication = -32080 ErrorCodeJrpc = -42000 )
const NullString = "null"
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 ErrNoMoreBatches = errors.New("no more batches")
var Null = json.RawMessage(NullString)
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 BatchWriter ¶ added in v0.3.17
type BatchWriter struct {
// contains filtered or unexported fields
}
func (*BatchWriter) Close ¶ added in v0.3.17
func (m *BatchWriter) Close() error
close must be called when you are done writing the batch. it releases the write lock
func (*BatchWriter) NewMessage ¶ added in v0.3.17
func (m *BatchWriter) NewMessage(ctx context.Context) (*MessageWriter, error)
Writes the next element in the batch. Note that the messagewriter is not thread safe
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"` Extensions map[string]json.RawMessage `json:"-"` 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 MessageStream ¶ added in v0.3.17
type MessageStream struct {
// contains filtered or unexported fields
}
MessageStream is a writer used to write jsonrpc message to a stream
func NewStream ¶ added in v0.3.17
func NewStream(w io.Writer) *MessageStream
func (*MessageStream) Flush ¶ added in v0.3.17
func (m *MessageStream) Flush(ctx context.Context) error
sends a flush in order to send an empty payload
func (*MessageStream) NewBatch ¶ added in v0.3.17
func (m *MessageStream) NewBatch(ctx context.Context) (*BatchWriter, error)
Start writing a batch to the stream. this function acquires the lock caller MUST call Close() on the BatchWriter iff err == nil
func (*MessageStream) NewMessage ¶ added in v0.3.17
func (m *MessageStream) NewMessage(ctx context.Context) (*MessageWriter, error)
NewMessage starts a new message and acquires the write lock. to free the write lock, you must call *MessageWriter.Close() the lock MUST be closed if and only if err == nil
type MessageStreamer ¶ added in v0.3.17
type MessageStreamer interface {
NewMessage(ctx context.Context) (*MessageWriter, error)
}
type MessageWriter ¶ added in v0.3.17
type MessageWriter struct {
// contains filtered or unexported fields
}
func (*MessageWriter) Close ¶ added in v0.3.17
func (m *MessageWriter) Close() error
close must be called when you are done writing the message. it releases the write lock
func (*MessageWriter) Field ¶ added in v0.3.17
func (m *MessageWriter) Field(name string, value json.RawMessage) error
func (*MessageWriter) Params ¶ added in v0.3.17
func (m *MessageWriter) Params() (io.WriteCloser, error)
Params returns a writer that writes to a params field
func (*MessageWriter) Result ¶ added in v0.3.17
func (m *MessageWriter) Result() (io.WriteCloser, error)
Result returns a writer that writes to a result field
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:"-"` Extensions map[string]json.RawMessage `json:"-"` // contains filtered or unexported fields }
func NewRawRequest ¶
func NewRequest ¶
NewRequest makes a new request
func (Request) MarshalJSON ¶ added in v0.3.18
type ResponseWriter ¶
http.ResponseWriter interface, but for jrpc
type ResultWriter ¶ added in v0.3.17
type ResultWriter struct {
// contains filtered or unexported fields
}
func (*ResultWriter) Close ¶ added in v0.3.17
func (m *ResultWriter) Close() error
type StreamingResponseWriter ¶ added in v0.3.17
type StreamingResponseWriter interface { ResponseWriter SendStream(func(MessageStreamer) error) error NotifyStream(func(MessageStreamer) error) error }
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.