Skip to content

lspeasy / core/src / Transport

Interface: Transport

Defined in: packages/core/src/transport/transport.ts:82

Pluggable communication layer for JSON-RPC message exchange.

Remarks

Transport is the lowest-level abstraction in lspeasy. Every LSPServer and LSPClient operates over a single Transport instance. Built-in implementations include WebSocketTransport (browser + Node), and the Node-only StdioTransport, TcpTransport, and IpcTransport from @lspeasy/core/node.

Implement this interface when you need a custom channel (e.g. in-process message passing for tests, or a custom binary framing layer).

Use When

You need a custom channel not covered by built-in transports — for example, a web worker bridge, an in-process pipe for unit tests, or a proprietary framing protocol.

Avoid When

A built-in transport already satisfies your environment. Prefer WebSocketTransport, StdioTransport (@lspeasy/core/node), TcpTransport (@lspeasy/core/node), or IpcTransport (@lspeasy/core/node).

Never

NEVER share one Transport instance between two LSPServer or LSPClient instances — each connection maintains independent protocol state (pending request IDs, lifecycle phase) and sharing causes ID collisions and state desync.

NEVER mix message framing styles on the same transport. StdioTransport uses Content-Length framing; WebSocketTransport uses raw JSON over WebSocket frames. Using a stdio-framing reader on a WebSocket connection will corrupt the message stream.

Example

ts
// Minimal in-process test transport
import type { Transport, Message, Disposable } from '@lspeasy/core';

class InProcessTransport implements Transport {
  private peer?: InProcessTransport;
  private messageHandlers = new Set<(msg: Message) => void>();
  private errorHandlers = new Set<(err: Error) => void>();
  private closeHandlers = new Set<() => void>();
  private open = true;

  link(other: InProcessTransport) { this.peer = other; }

  async send(message: Message): Promise<void> {
    if (!this.open) throw new Error('Transport closed');
    this.peer?.messageHandlers.forEach(h => h(message));
  }
  onMessage(handler: (msg: Message) => void): Disposable {
    this.messageHandlers.add(handler);
    return { dispose: () => this.messageHandlers.delete(handler) };
  }
  onError(handler: (err: Error) => void): Disposable {
    this.errorHandlers.add(handler);
    return { dispose: () => this.errorHandlers.delete(handler) };
  }
  onClose(handler: () => void): Disposable {
    this.closeHandlers.add(handler);
    return { dispose: () => this.closeHandlers.delete(handler) };
  }
  async close(): Promise<void> {
    this.open = false;
    this.closeHandlers.forEach(h => h());
  }
  isConnected(): boolean { return this.open; }
}

Methods

close()

ts
close(): Promise<void>;

Defined in: packages/core/src/transport/transport.ts:118

Close the transport connection and release resources.

Returns

Promise<void>


isConnected()

ts
isConnected(): boolean;

Defined in: packages/core/src/transport/transport.ts:124

Returns true if the transport is currently connected and able to send messages.

Returns

boolean


onClose()

ts
onClose(handler): Disposable;

Defined in: packages/core/src/transport/transport.ts:113

Subscribe to connection close.

Parameters

ParameterTypeDescription
handler() => voidCallback invoked when the transport is closed.

Returns

Disposable

A Disposable that unsubscribes the handler when disposed.


onError()

ts
onError(handler): Disposable;

Defined in: packages/core/src/transport/transport.ts:105

Subscribe to transport errors.

Parameters

ParameterTypeDescription
handler(error) => voidCallback invoked when a transport-level error occurs.

Returns

Disposable

A Disposable that unsubscribes the handler when disposed.


onMessage()

ts
onMessage(handler): Disposable;

Defined in: packages/core/src/transport/transport.ts:97

Subscribe to incoming messages.

Parameters

ParameterTypeDescription
handler(message) => voidCallback invoked for each received message.

Returns

Disposable

A Disposable that unsubscribes the handler when disposed.


send()

ts
send(message): Promise<void>;

Defined in: packages/core/src/transport/transport.ts:89

Send a message to the remote peer.

Parameters

ParameterTypeDescription
messageMessageThe JSON-RPC message to send.

Returns

Promise<void>

Throws

If the transport is not connected or an I/O error occurs.

Released under the MIT License.