jsonrpc

package
v1.10.4 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package jsonrpc implements a thin JSON-RPC 2.0 codec for communicating with Language Server child processes over Content-Length framed stdio.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadMessage

func ReadMessage(r *bufio.Reader) (json.RawMessage, error)

ReadMessage reads a single Content-Length framed JSON-RPC message from r. It parses the Content-Length header, ignores other headers (e.g., Content-Type), and reads the exact number of body bytes specified.

func WriteMessage

func WriteMessage(w io.Writer, msg json.RawMessage) error

WriteMessage writes a Content-Length framed JSON-RPC message to w. It writes "Content-Length: N\r\n\r\n" followed by the message body.

Types

type Conn

type Conn struct {

	// OnNotification is called for incoming notifications during Listen.
	OnNotification NotificationFunc
	// contains filtered or unexported fields
}

Conn is a bidirectional JSON-RPC 2.0 connection over a Content-Length framed stream. It supports Call (request-response), Notify (fire-and-forget), and Listen (dispatch loop).

func NewConn

func NewConn(rwc io.ReadWriteCloser, sessionPrefix string, tracer trace.Tracer) *Conn

NewConn creates a new JSON-RPC connection wrapping the given stream. sessionPrefix is prepended to request IDs to avoid collisions when multiple sessions share an LS worker (per Pitfall 4: sequential integer IDs).

tracer is used to emit lspool.lsp.{method} child spans on Call and lspool.lsp.notify.{method} on Notify. A nil tracer falls back to a noop tracer (Phase 55 D-01: never reach for the global tracer provider).

func (*Conn) Call

func (c *Conn) Call(ctx context.Context, method string, params interface{}, result interface{}) error

Call sends a JSON-RPC request and waits for the response. The result is unmarshaled into the provided result pointer.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the underlying connection.

func (*Conn) Listen

func (c *Conn) Listen(ctx context.Context) error

Listen reads messages from the connection and dispatches them. Responses are routed to pending Call waiters. Notifications are delivered to the OnNotification callback. Listen blocks until the context is cancelled or the connection is closed.

func (*Conn) Notify

func (c *Conn) Notify(ctx context.Context, method string, params interface{}) error

Notify sends a JSON-RPC notification (no ID, no response expected).

func (*Conn) Send

func (c *Conn) Send(ctx context.Context, method string, params interface{}) (string, error)

Send sends a JSON-RPC request and returns the assigned ID. It does not wait for a response; use Call for request-response.

type Notification

type Notification struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Notification is a JSON-RPC 2.0 notification (no ID, no response expected).

func NewNotification

func NewNotification(method string, params interface{}) (*Notification, error)

NewNotification creates a new JSON-RPC 2.0 notification.

type NotificationFunc

type NotificationFunc func(method string, params json.RawMessage)

NotificationFunc is a callback for handling incoming notifications.

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Request is a JSON-RPC 2.0 request message.

func NewRequest

func NewRequest(id interface{}, method string, params interface{}) (*Request, error)

NewRequest creates a new JSON-RPC 2.0 request.

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *ResponseError  `json:"error,omitempty"`
}

Response is a JSON-RPC 2.0 response message.

type ResponseError

type ResponseError struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

ResponseError is a JSON-RPC 2.0 error object.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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