Skip to content

lspeasy / core/src

core/src

Core types, transports, and utilities shared by all lspeasy packages.

Remarks

@lspeasy/core is the shared foundation for the lspeasy SDK. It contains everything needed to build custom LSP integrations, and re-exports the most-used pieces from @lspeasy/client and @lspeasy/server.

Key areas

JSON-RPC 2.0 — Message types (RequestMessage, NotificationMessage, ResponseMessage), framing (parseMessage, serializeMessage), and Zod schemas for validation.

Transports — The Transport interface plus browser-compatible implementations: WebSocketTransport, DedicatedWorkerTransport, SharedWorkerTransport. Node.js transports (StdioTransport, TcpTransport, IpcTransport) are in @lspeasy/core/node to avoid importing Node.js builtins in browsers.

Transport Selection Guide

NeedTransportCritical Gotcha
Spawn server as child processStdioTransportConsoleLogger corrupts stdout — use NullLogger
Browser or remote serverWebSocketTransportCall send() only after isConnected() is true
Persistent local daemonTcpTransportCreate a new server instance per client reconnect
In-process browser isolationDedicatedWorkerTransportMonitor worker.onerror; crashes are silent
Shared worker, multiple tabsSharedWorkerTransportOne worker handles all port connections

Middleware — The Middleware pipeline runs on every client-to-server and server-to-client message. Use createScopedMiddleware to limit a middleware to specific methods, and createTypedMiddleware for full param/result type inference.

LSP protocolLSPRequest and LSPNotification namespaces expose every standard LSP method with its params and result types. LSPRequestMethod / LSPNotificationMethod are the union types for string-literal method names.

UtilitiesCancellationTokenSource for request cancellation, DisposableStore for lifecycle management, ResponseError for structured JSON-RPC errors, DocumentVersionTracker for document sync.

Document

NameDescription
DocumentVersionTrackerTracks monotonically increasing version numbers for open text documents.
createFullDidChangeParamsBuilds DidChangeTextDocumentParams for a full-document text replacement.
createIncrementalDidChangeParamsBuilds DidChangeTextDocumentParams for an incremental (range-based) document change notification.

Errors

NameDescription
ResponseErrorAn Error subclass that maps to a JSON-RPC 2.0 error response.
JSONRPCErrorCodeNumeric error codes defined by JSON-RPC 2.0 and the LSP specification.

JSON-RPC

NameDescription
BaseMessageBase JSON-RPC 2.0 message discriminant.
ErrorResponseMessageJSON-RPC 2.0 error response to a prior request.
NotificationMessageJSON-RPC 2.0 Notification message — no response is expected or sent.
RequestMessageJSON-RPC 2.0 Request message — expects a response from the peer.
ResponseErrorInterfaceJSON-RPC 2.0 error object embedded in an error response.
SuccessResponseMessageJSON-RPC 2.0 success response to a prior request.
MessageUnion of all JSON-RPC 2.0 message types sent over a transport.
ResponseMessageJSON-RPC 2.0 Response message — either a success result or an error.
isErrorResponseReturns true when response carries an error (error case).
isNotificationMessageReturns true when message is a JSON-RPC notification (has method, no id).
isRequestMessageReturns true when message is a JSON-RPC request (has id + method).
isResponseMessageReturns true when message is a JSON-RPC response (has id, no method).
isSuccessResponseReturns true when response carries a result (success case).
parseMessageParses a single framed JSON-RPC 2.0 message from a raw byte buffer.
serializeMessageSerializes a JSON-RPC 2.0 message into a framed byte buffer with Content-Length and Content-Type headers.

Lifecycle

NameDescription
CancellationTokenSourceController that creates and manages a CancellationToken.
DisposableStoreCollects multiple Disposable instances and releases them together.
CancellationTokenRead-only handle for observing cancellation state.
DisposableRepresents a resource that can be explicitly released.

Logging

NameDescription
LogLevelNumeric severity levels for filtering log output.
ConsoleLoggerLogger implementation that writes to the process console with level filtering.
NullLoggerNo-op logger that silently discards all messages.
LoggerStructured logging interface used throughout lspeasy.

Middleware

NameDescription
MethodFilterFilter predicate used by ScopedMiddleware to select which messages a middleware should intercept.
MiddlewareContextExecution context passed to every middleware function in the pipeline.
MiddlewareResultThe return value from a middleware function.
ScopedMiddlewareA Middleware paired with a MethodFilter so it only runs for matching messages.
TypedMiddlewareContextTyped middleware context narrowed to a specific LSP method. Use with createTypedMiddleware for full type inference.
LSPMethodUnion of all LSP request and notification method strings.
MiddlewareA function that intercepts JSON-RPC messages flowing through LSPServer or LSPClient.
MiddlewareDirectionDirection of a JSON-RPC message in the middleware pipeline.
MiddlewareMessageThe JSON-RPC message exposed to middleware, with id made read-only to prevent accidental mutation that would break response correlation.
MiddlewareMessageTypeKind of JSON-RPC message flowing through middleware.
MiddlewareNextCalls the next middleware (or final handler) in the pipeline.
TypedMiddlewareA middleware function narrowed to a specific LSP method with full type inference for params and result.
TypedParamsInfers the params type for a given LSP method.
TypedResultInfers the result type for a given LSP method (void for notifications).
composeMiddlewareCombines multiple middleware functions into a single middleware that runs them left-to-right, each delegating to the next via next().
createScopedMiddlewareWraps a middleware with a filter so it only runs for matching LSP messages.
createTypedMiddlewareCreates a typed, method-scoped middleware with full TypeScript inference for the message params and result.
executeMiddlewarePipelineRuns the registered middleware chain for a single JSON-RPC message, then calls finalHandler if no middleware short-circuits.

Other

CancellationTokenNone

Renames and re-exports CancellationToken

Transport

NameDescription
WebSocketTransportWebSocket-based transport for LSP communication.
TransportPluggable communication layer for JSON-RPC message exchange.
WebSocketTransportOptionsOptions for configuring a WebSocketTransport.
createWebSocketClientCreates a WebSocket client instance, preferring the native globalThis.WebSocket (Node ≥ 22.4 / modern browsers) and falling back to the optional ws peer dependency.

Utilities

NameDescription
CLIENT_METHODSPre-built method sets indexed by client capability. Used by ClientCapabilityGuard in @lspeasy/client and @lspeasy/server.
SERVER_METHODSPre-built method sets indexed by server capability. Used by CapabilityGuard in @lspeasy/client and @lspeasy/server.
buildMethodSetsBuilds the full set of LSP methods and the subset that are always allowed (not gated by a capability) for a given capability key.

Released under the MIT License.