jsonrpc

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 24 Imported by: 0

README

JSON RPC

Part of the code in this package comes from https://github.com/TobiasYin/go-lsp (MIT license)

Documentation

Index

Constants

View Source
const (
	JSONRPC_VERSION = "2.0"

	MAX_PARAMS_LOGGING_SIZE = 3000
)
View Source
const (
	JSON_RPC_SERVER_LOG_SRC                 = "json-rpc"
	DEFAULT_MAX_IP_WS_CONNS                 = 3
	DEFAULT_MAX_IP_WS_CONNS_IF_BEHIND_PROXY = 10_000
)
View Source
const (
	CANCEL_REQUEST_METHOD = "$/cancelRequest"
)
View Source
const (
	CONTENT_LENGTH_HEADER = "Content-Length: "
)
View Source
const ContentModifiedCode = -32801
View Source
const InternalErrorCode = -32603
View Source
const InvalidParamsCode = -32602
View Source
const InvalidRequestCode = -32600
View Source
const MethodNotFoundCode = -32601
View Source
const ParseErrorCode = -32700
View Source
const RequestCancelledCode = -32800
View Source
const ServerNotInitializedCode = -32002
View Source
const UnknownErrorCodeCode = -32001

Variables

View Source
var (
	DEFAULT_METHOD_RATE_LIMITS  = []int{5, 20, 80} //rate limits for a single method
	DEFAULT_MESSAGE_RATE_LIMITS = [2]int{70, 200}
)
View Source
var (
	ErrRateLimitedMethod   = errors.New("rate limited method")
	ErrRateLimited         = errors.New("rate limited")
	ErrAlreadyShuttingDown = errors.New("session is already shutting down")
)
View Source
var ContentModified = BuildInError{
	Code:    ContentModifiedCode,
	Message: "ContentModified",
}
View Source
var (
	ErrOnly127001AllowedIfBehindProxy = errors.New("only connections from the same host (127.0.0.1) are allowed")
)
View Source
var InternalError = BuildInError{
	Code:    InternalErrorCode,
	Message: "InternalError",
}
View Source
var InvalidParams = BuildInError{
	Code:    InvalidParamsCode,
	Message: "InvalidParams",
}
View Source
var InvalidRequest = BuildInError{
	Code:    InvalidRequestCode,
	Message: "InvalidRequest",
}
View Source
var MethodNotFound = BuildInError{
	Code:    MethodNotFoundCode,
	Message: "MethodNotFound",
}
View Source
var ParseError = BuildInError{
	Code:    ParseErrorCode,
	Message: "ParseError",
}
View Source
var RequestCancelled = BuildInError{
	Code:    RequestCancelledCode,
	Message: "RequestCancelled",
}
View Source
var ServerNotInitialized = BuildInError{
	Code:    ServerNotInitializedCode,
	Message: "ServerNotInitialized",
}
View Source
var UnknownErrorCode = BuildInError{
	Code:    UnknownErrorCodeCode,
	Message: "UnknownErrorCode",
}

Functions

This section is empty.

Types

type BaseMessage

type BaseMessage struct {
	Jsonrpc string `json:"jsonrpc"`
}

type BuildInError

type BuildInError = ResponseError

type CancelParams

type CancelParams struct {
	ID interface{} `json:"id"` // the method id should be canceled
}

type CloserReader

type CloserReader interface {
	io.Reader
	io.Closer
}

func NewFakeCloserReader

func NewFakeCloserReader(r io.Reader) CloserReader

type CloserWriter

type CloserWriter interface {
	io.Writer
	io.Closer
}

func NewFakeCloserWriter

func NewFakeCloserWriter(w io.Writer) CloserWriter

type Conn

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

The connection of rpc, not limited to net.Conn

func NewConn

func NewConn(reader CloserReader, writer CloserWriter) *Conn

func NewNotCloseConn

func NewNotCloseConn(reader io.Reader, writer io.Writer) *Conn

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

func (*Conn) Write

func (c *Conn) Write(p []byte) (n int, err error)

type FnMessageReaderWriter

type FnMessageReaderWriter struct {
	ReadMessageFn  func() (msg []byte, err error)
	WriteMessageFn func(msg []byte) error
	CloseFn        func() error
	ClientFn       func() string
}

func (FnMessageReaderWriter) Client

func (rw FnMessageReaderWriter) Client() string

func (FnMessageReaderWriter) Close

func (rw FnMessageReaderWriter) Close() error

func (FnMessageReaderWriter) ReadMessage

func (rw FnMessageReaderWriter) ReadMessage() (msg []byte, err error)

func (FnMessageReaderWriter) WriteMessage

func (rw FnMessageReaderWriter) WriteMessage(msg []byte) error

type JsonRpcWebsocket

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

func NewJsonRpcWebsocket

func NewJsonRpcWebsocket(conn *ws_ns.WebsocketConnection, logger zerolog.Logger) *JsonRpcWebsocket

func (*JsonRpcWebsocket) Client

func (s *JsonRpcWebsocket) Client() string

func (*JsonRpcWebsocket) Close

func (s *JsonRpcWebsocket) Close() error

func (*JsonRpcWebsocket) ReadMessage

func (s *JsonRpcWebsocket) ReadMessage() ([]byte, error)

func (*JsonRpcWebsocket) WriteMessage

func (s *JsonRpcWebsocket) WriteMessage(msg []byte) error

type JsonRpcWebsocketServer

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

func NewJsonRpcWebsocketServer

func NewJsonRpcWebsocketServer(ctx *core.Context, config JsonRpcWebsocketServerConfig) (*JsonRpcWebsocketServer, error)

func (*JsonRpcWebsocketServer) HandleNew

func (server *JsonRpcWebsocketServer) HandleNew(httpRespWriter http.ResponseWriter, httpReq *http.Request)

func (*JsonRpcWebsocketServer) Logger

func (server *JsonRpcWebsocketServer) Logger() *zerolog.Logger

type JsonRpcWebsocketServerConfig

type JsonRpcWebsocketServerConfig struct {
	Addr      string
	RpcServer *Server

	//defaults to DEFAULT_MAX_IP_WS_CONNS
	MaxWebsocketPerIp int

	//if true only connections from localhost are allowed and
	//the effective value of MaxWebsocketPerIp is set to DEFAULT_MAX_IP_WS_CONNS_IF_BEHIND_PROXY.
	BehindCloudProxy bool
}

type MessageReaderWriter

type MessageReaderWriter interface {
	//ReadMessage reads an entire message and returns it, the returned bytes should not be modified by the caller.
	ReadMessage() (msg []byte, err error)

	//WriteMessage writes an entire message and returns it, the written bytes should not modified by the implementation.
	WriteMessage(msg []byte) error

	io.Closer

	Client() string
}

type MethodInfo

type MethodInfo struct {
	Name          string
	NewRequest    func() interface{}
	Handler       func(ctx context.Context, req interface{}) (interface{}, error)
	SensitiveData bool
	AvoidLogging  bool

	// List of the maximum number of calls allowed during windows with increasing durations (1s, 10s, and 100s).
	// Example: [10, 50, 200] means at most 10 calls in 1s, 50 calls in 50s and 200 calls in 100s.
	RateLimits []int
}

func CancelRequest

func CancelRequest() MethodInfo

type NotificationMessage

type NotificationMessage struct {
	BaseMessage
	Method string          `json:"method"` // starts with "/$", server build-in methods.
	Params json.RawMessage `json:"params"` // params, is some struct or slice
}

type ProgressParams

type ProgressParams struct {
	Token interface{} `json:"token"` // int or string
	/**
	 * The progress data.
	 */
	Value interface{} `json:"value"`
}

type ReaderWriter

type ReaderWriter interface {
	io.Reader
	io.Writer
	io.Closer
}

type RequestMessage

type RequestMessage struct {
	BaseMessage
	ID     interface{}     `json:"id"` // may be int or string
	Method string          `json:"method"`
	Params json.RawMessage `json:"params"` // params, is some struct or slice
}

type ResponseError

type ResponseError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func (ResponseError) Error

func (r ResponseError) Error() string

type ResponseMessage

type ResponseMessage struct {
	BaseMessage
	ID     interface{}    `json:"id"` // may be int or string
	Result interface{}    `json:"result"`
	Error  *ResponseError `json:"error"`
}

type Server

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

func NewServer

func NewServer(ctx *core.Context, onSession SessionCreationCallbackFn) *Server

func (*Server) ConnComeIn

func (server *Server) ConnComeIn(conn ReaderWriter)

func (*Server) MsgConnComeIn

func (server *Server) MsgConnComeIn(conn MessageReaderWriter, onCreatedSession func(session *Session))

func (*Server) RegisterMethod

func (server *Server) RegisterMethod(m MethodInfo)

type Session

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

func GetSession

func GetSession(ctx context.Context) *Session

func (*Session) Client

func (s *Session) Client() string

func (*Session) Close

func (s *Session) Close() error

Close shutdowns the session & closes the connection.

func (*Session) Closed

func (s *Session) Closed() bool

func (*Session) Context

func (s *Session) Context() *core.Context

func (*Session) IsShuttingDown

func (s *Session) IsShuttingDown() bool

func (*Session) Notify

func (s *Session) Notify(notif NotificationMessage) error

SendRequest sends a notification to the client, NotificationMessage.BaseMessage is set by the callee.

func (*Session) SendRequest

func (s *Session) SendRequest(req RequestMessage) error

SendRequest sends a request to the client, RequestMessage.ID & RequestMessage.BaseMessage are set by the callee.

func (*Session) SetClosedCallbackFn

func (s *Session) SetClosedCallbackFn(fn func(session *Session))

func (*Session) SetContextOnce

func (s *Session) SetContextOnce(ctx *core.Context) error

func (*Session) SetShutdownCallbackFn

func (s *Session) SetShutdownCallbackFn(fn func(session *Session))

func (*Session) Start

func (s *Session) Start()

type SessionCreationCallbackFn

type SessionCreationCallbackFn func(rpcServerContext *core.Context, session *Session) error

Called before starting each new JSON RPC session.

Jump to

Keyboard shortcuts

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