jsonrpc

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonrpc speaks the JSON half of stdiorpc's protocol over a pipe: Content-Length-delimited frames carrying JSON-RPC 2.0 bodies, with the canonical gRPC status codes numbering a refused call. It is shared by the WebAssembly commands, whose frames are read without net/textproto so the networking packages stay out of a small binary.

Index

Constants

View Source
const (
	ContentTypeJSON     = "application/json"
	ContentTypeProtobuf = "application/proto"
)

Content types a frame may carry, as stdiorpc reads them. Only JSON is served; a protobuf frame is refused rather than answered in kind.

View Source
const (
	CodeCanceled           = 1
	CodeUnknown            = 2
	CodeInvalidArgument    = 3
	CodeDeadlineExceeded   = 4
	CodeNotFound           = 5
	CodeResourceExhausted  = 8
	CodeFailedPrecondition = 9
	CodeUnimplemented      = 12
	CodeInternal           = 13
)

The canonical gRPC status codes an answer is reported under, numbered as every transport of the service numbers them.

View Source
const MaxHeaderBytes = 64 << 10

MaxHeaderBytes bounds the header lines of one frame in place of textproto's limits, so an endless line is refused before it is buffered.

Variables

This section is empty.

Functions

func Envelope

func Envelope(body []byte, err error) string

Envelope is the JSON-RPC answer the JS host surface returns for one call: the result body, or the error's code and message, under a null id.

func ErrorParts

func ErrorParts(err error) (uint32, string)

ErrorParts reads the canonical status code and message a failed call is answered with; the codes number the same as gRPC's.

func ReadHeader added in v0.13.0

func ReadHeader(r *bufio.Reader) (map[string][]string, error)

ReadHeader reads one frame's MIME header from r, refusing one over MaxHeaderBytes.

func Serve

func Serve(ctx context.Context, r io.Reader, w io.Writer, call Handler) error

Serve reads Content-Length-delimited frames from r and writes answers to w until r reaches its end, speaking the JSON half of stdiorpc's protocol: the same framing, envelope and error codes, with a protobuf frame refused since this product carries no protobuf. Calls are handled sequentially and answered in order.

Types

type Error

type Error struct {
	Code    uint32
	Message string
}

Error is a refused call: the canonical status code and its message.

func Decode

func Decode(params []byte, into any) *Error

Decode reads the request body as protojson does over the lowerCamel field names: unknown fields are ignored, absent params are the empty request.

func Errorf

func Errorf(code uint32, format string, args ...any) *Error

Errorf is a refused call with a formatted message.

func (*Error) Error

func (e *Error) Error() string

Error returns the status message.

type Handler

type Handler func(ctx context.Context, method string, params []byte) ([]byte, error)

Handler answers one call's params body, as an engine does.

Jump to

Keyboard shortcuts

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