stdio

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

This file implements the stdio transport layer for JSON-RPC communication. It provides functionality to read and write JSON-RPC messages over standard input/output streams, similar to the TypeScript implementation in @typescript-sdk/src/shared/stdio.ts.

Key Components:

1. ReadBuffer:

  • Buffers continuous stdio stream into discrete JSON-RPC messages
  • Thread-safe with mutex protection
  • Handles message framing using newline delimiters
  • Methods: Append (add data), ReadMessage (read complete message), Clear (reset buffer)

2. StdioTransport:

  • Implements the Transport interface using stdio
  • Uses bufio.Reader for efficient buffered reading
  • Thread-safe with mutex protection
  • Supports:
  • Asynchronous message reading
  • Message sending with newline framing
  • Proper cleanup on close
  • Event handlers for close, error, and message events

3. Message Handling:

  • Deserializes JSON-RPC messages into appropriate types:
  • JSONRPCRequest: Messages with ID and method
  • JSONRPCNotification: Messages with method but no ID
  • JSONRPCError: Error responses with ID
  • Generic responses: Success responses with ID
  • Serializes messages to JSON with newline termination

Thread Safety:

  • All public methods are thread-safe
  • Uses sync.Mutex for state protection
  • Safe for concurrent reading and writing

Usage:

transport := NewStdioTransport()
transport.SetMessageHandler(func(msg interface{}) {
    // Handle incoming message
})
transport.Start()
defer transport.Close()

// Send a message
transport.Send(map[string]interface{}{
    "jsonrpc": "2.0",
    "method": "test",
    "params": map[string]interface{}{},
})

Error Handling:

  • All methods return meaningful errors
  • Transport supports error handler for async errors
  • Proper cleanup on error conditions

For more details, see the test file stdio_test.go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReadBuffer

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

ReadBuffer buffers a continuous stdio stream into discrete JSON-RPC messages.

func NewReadBuffer

func NewReadBuffer() *ReadBuffer

NewReadBuffer creates a new ReadBuffer.

func (*ReadBuffer) Append

func (rb *ReadBuffer) Append(chunk []byte)

Append adds a chunk of data to the buffer.

func (*ReadBuffer) Clear

func (rb *ReadBuffer) Clear()

Clear clears the buffer.

func (*ReadBuffer) ReadMessage

func (rb *ReadBuffer) ReadMessage() (*transport.BaseJsonRpcMessage, error)

ReadMessage reads a complete JSON-RPC message from the buffer. Returns nil if no complete message is available.

Jump to

Keyboard shortcuts

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