jsonRpcProtocol.ts ×17

Frontier kind: Code frontier

unlabeled · c_9ae580ca3607

180 tests · 7076 LOC · 32 files · introduces 0 tests · 115 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
17 ranges115 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
1036 ranges7076 lines · 32 files · Browse complete extent
All tests (intent)
180 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 115 introduced LOC across 17 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/base/common/jsonRpcProtocol.ts 115 introduced LOC · 17 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- jsonRpcProtocol.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { DeferredPromise } from './async.js';
7 > import { CancellationToken, CancellationTokenSource } from './cancellation.js';
8 > import { CancellationError } from './errors.js';
9 > import { Disposable, toDisposable } from './lifecycle.js';
10 > import { hasKey } from './types.js';
11 >
12 > export type JsonRpcId = string | number;
13 >
14 > export interface IJsonRpcError {
15 > code: number;
16 > message: string;
17 > data?: unknown;
18 > }
19 >
20 > export interface IJsonRpcRequest {
21 > jsonrpc: '2.0';
22 > id: JsonRpcId;
23 > method: string;
24 > params?: unknown;
25 > }
26 >
27 > export interface IJsonRpcNotification {
28 > jsonrpc: '2.0';
29 > method: string;
30 > params?: unknown;
31 > }
32 >
33 > export interface IJsonRpcSuccessResponse {
34 > jsonrpc: '2.0';
35 > id: JsonRpcId;
36 > result: unknown;
37 > }
38 >
39 > export interface IJsonRpcErrorResponse {
40 > jsonrpc: '2.0';
41 > id?: JsonRpcId;
42 > error: IJsonRpcError;
43 > }
44 >
45 > export type JsonRpcMessage = IJsonRpcRequest | IJsonRpcNotification | IJsonRpcSuccessResponse | IJsonRpcErrorResponse;
46 > export type JsonRpcResponse = IJsonRpcSuccessResponse | IJsonRpcErrorResponse;
47 >
48 > interface IPendingRequest {
49 > promise: DeferredPromise<unknown>;
50 > cts: CancellationTokenSource;
51 > }
52 >
53 > export interface IJsonRpcProtocolHandlers {
54 > handleRequest?(request: IJsonRpcRequest, token: CancellationToken): Promise<unknown> | unknown;
55 > handleNotification?(notification: IJsonRpcNotification): void;
56 > }
57 >
58 > export class JsonRpcError extends Error {
59 > constructor(
60 public readonly code: number,
61 message: string,
64 super(message);
65 }
67 >
68 > /**
69 > * Generic JSON-RPC 2.0 protocol helper.
70 > */
71 > export class JsonRpcProtocol extends Disposable {
72 > private static readonly ParseError = -32700;
73 > private static readonly MethodNotFound = -32601;
74 > private static readonly InternalError = -32603;
75 >
76 > private _nextRequestId = 1;
77 > private readonly _pendingRequests = new Map<JsonRpcId, IPendingRequest>();
78 >
79 > constructor(
80 private readonly _send: (message: JsonRpcMessage) => void,
81 private readonly _handlers: IJsonRpcProtocolHandlers,
83 super();
84 }
86 > public sendNotification(notification: Omit<IJsonRpcNotification, 'jsonrpc'>): void {
87 this._send({
88 jsonrpc: '2.0',
90 });
91 }
93 > public sendRequest<T = unknown>(request: Omit<IJsonRpcRequest, 'jsonrpc' | 'id'>, token: CancellationToken = CancellationToken.None, onCancel?: (id: JsonRpcId) => void): Promise<T> {
94 if (this._store.isDisposed) {
95 return Promise.reject(new CancellationError());
123 }) as Promise<T>;
124 }
126 > /**
127 > * Handles one or more incoming JSON-RPC messages.
128 > *
129 > * Returns an array of JSON-RPC response objects generated for any incoming
130 > * requests in the message(s). Notifications and responses to our own
131 > * outgoing requests do not produce return values. For batch inputs, the
132 > * returned responses are in the same order as the corresponding requests.
133 > *
134 > * Note: responses are also emitted via the `_send` callback, so callers
135 > * that rely on the return value should not re-send them.
136 > */
137 > public async handleMessage(message: JsonRpcMessage | JsonRpcMessage[]): Promise<JsonRpcResponse[]> {
138 if (Array.isArray(message)) {
139 const replies: JsonRpcResponse[] = [];
150 return reply ? [reply] : [];
151 }
153 > public cancelPendingRequest(id: JsonRpcId): void {
154 const request = this._pendingRequests.get(id);
155 if (request) {
160 }
161 }
163 > public cancelAllRequests(): void {
164 for (const [id, pending] of this._pendingRequests) {
165 this._pendingRequests.delete(id);
169 }
170 }
172 > private async _handleMessage(message: JsonRpcMessage): Promise<JsonRpcResponse | undefined> {
173 if (isJsonRpcResponse(message)) {
174 if (hasKey(message, { result: true })) {
190 return undefined;
191 }
193 > private _handleResult(response: IJsonRpcSuccessResponse): void {
194 const request = this._pendingRequests.get(response.id);
195 if (request) {
199 }
200 }
202 > private _handleError(response: IJsonRpcErrorResponse): void {
203 if (response.id === undefined) {
204 return;
212 }
213 }
215 > private async _handleRequest(request: IJsonRpcRequest): Promise<JsonRpcResponse> {
216 if (!this._handlers.handleRequest) {
217 const response: IJsonRpcErrorResponse = {
268 }
269 }
271 > public override dispose(): void {
272 this.cancelAllRequests();
273 super.dispose();
274 }
276 > public static createParseError(message: string, data?: unknown): IJsonRpcErrorResponse {
277 return {
278 jsonrpc: '2.0',
284 };
285 }
287 >
288 > export function isJsonRpcRequest(message: JsonRpcMessage): message is IJsonRpcRequest {
289 return 'method' in message && 'id' in message && (typeof message.id === 'string' || typeof message.id === 'number');
290 }
292 > export function isJsonRpcResponse(message: JsonRpcMessage): message is IJsonRpcSuccessResponse | IJsonRpcErrorResponse {
293 return hasKey(message, { id: true, result: true }) || hasKey(message, { id: true, error: true });
294 }
296 > export function isJsonRpcNotification(message: JsonRpcMessage): message is IJsonRpcNotification {
297 return hasKey(message, { method: true }) && !hasKey(message, { id: true });
298 }
300 >
301 function isThenable<T>(value: T | Promise<T>): value is Promise<T> {
302 return typeof value === 'object' && value !== null && 'then' in value && typeof value.then === 'function';