src/vs/platform/agentHost/node/messagePortProtocolServer.ts

165 LOC · 149 covered · 16 uncovered · 47 ranges · 179 concepts · 9 introducers · 80 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

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 related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- messagePortProtocolServer.ts ×13
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 { Emitter, Event } from '../../../base/common/event.js';
7 > import { Disposable } from '../../../base/common/lifecycle.js';
8 > import { IServerChannel } from '../../../base/parts/ipc/common/ipc.js';
9 > import { JSON_RPC_PARSE_ERROR, type AhpServerNotification, type JsonRpcNotification, type JsonRpcParseErrorResponse, type JsonRpcRequest, type JsonRpcResponse, type ProtocolMessage } from '../common/state/sessionProtocol.js';
10 > import type { IProtocolServer, IProtocolTransport } from '../common/state/sessionTransport.js';
11 >
12 > /**
13 > * Adapts MessagePort IPC clients to Agent Host Protocol transports.
14 > *
15 > * Consumers must call {@link closeClient} when a UtilityProcessServer client
16 > * connection disappears.
17 > */
18 > export class MessagePortProtocolServer<TContext> extends Disposable implements IProtocolServer, IServerChannel<TContext> {
20 > private readonly _onConnection = this._register(new Emitter<IProtocolTransport>());
21 > readonly onConnection = this._onConnection.event;
22 >
23 > readonly address = undefined;
24 >
25 > private readonly _transports = new Map<TContext, MessagePortProtocolTransport>();
27 > listen<T>(ctx: TContext, event: string): Event<T> {
28 > switch (event) { messagePortProtocolServer.ts ×4
29 > case 'frame':
30 > return this._getOrCreateTransport(ctx).onFrame as Event<T>; messagePortProtocolServer.ts ×2
32 > return this._getOrCreateTransport(ctx).onClose as Event<T>; messagePortProtocolServer.ts ×3
34
35 throw new Error(`Invalid listen: ${event}`);
38 > async call<T>(ctx: TContext, command: string, arg?: unknown): Promise<T> {
39 > switch (command) { messagePortProtocolServer.ts ×19
40 > case 'connect': {
41 > const transport = this._getOrCreateTransport(ctx);
42 > if (transport.connect()) {
43 > this._onConnection.fire(transport);
44 > }
45 > return undefined as T;
46 > }
47 > case 'send': {
48 > if (typeof arg !== 'string') {
49 throw new Error('send: arg must be a string frame');
50 }
52 > const transport = this._transports.get(ctx);
53 > if (!transport?.isConnected) {
54 > throw new Error('send: client is not connected'); messagePortProtocolServer.ts ×3
55 > }
57 > transport.acceptFrame(arg);
58 > return undefined as T;
59 > }
60 > case 'close':
61 > this.closeClient(ctx); messagePortProtocolServer.ts ×3
62 > return undefined as T;
64
65 throw new Error(`Invalid call: ${command}`);
68 > /**
69 > * Closes a client's transport after its owning IPC connection disappears.
70 > */
71 > closeClient(ctx: TContext): void {
72 > const transport = this._transports.get(ctx); messagePortProtocolServer.ts ×2
73 > if (!transport) {
74 return;
75 }
77 > this._transports.delete(ctx);
78 > transport.dispose();
79 > }
81 > override dispose(): void {
82 > const transports = [...this._transports.values()]; messagePortProtocolServer.ts ×19
83 > this._transports.clear();
84 > for (const transport of transports) {
85 > transport.dispose(); messagePortProtocolServer.ts ×2
86 > }
87 > super.dispose(); messagePortProtocolServer.ts ×19
88 > }
90 > private _getOrCreateTransport(ctx: TContext): MessagePortProtocolTransport {
91 > if (this._store.isDisposed) { messagePortProtocolServer.ts ×19
92 throw new Error('MessagePortProtocolServer is disposed');
93 }
95 > let transport = this._transports.get(ctx);
96 > if (!transport) {
97 > transport = new MessagePortProtocolTransport();
98 > this._transports.set(ctx, transport);
99 >
100 > const onClose = transport.onClose(() => {
101 > onClose.dispose();
102 > if (this._transports.get(ctx) === transport) {
103 this._transports.delete(ctx);
104 }
106 > }
107 >
108 > return transport;
109 > }
111 >
112 > class MessagePortProtocolTransport extends Disposable implements IProtocolTransport { messagePortProtocolServer.ts ×19
113 >
114 > private readonly _onFrame = this._register(new Emitter<string>());
115 > readonly onFrame = this._onFrame.event;
116 >
117 > private readonly _onMessage = this._register(new Emitter<ProtocolMessage>());
118 > readonly onMessage = this._onMessage.event;
119 >
120 > private readonly _onClose = this._register(new Emitter<void>());
121 > readonly onClose = this._onClose.event;
122 >
123 > private _isConnected = false;
124 > private _isClosed = false;
126 > get isConnected(): boolean {
127 > return this._isConnected && !this._isClosed; messagePortProtocolServer.ts ×19
128 > }
130 > connect(): boolean {
131 > if (this._isClosed || this._isConnected) { messagePortProtocolServer.ts ×19
132 return false;
133 }
135 > this._isConnected = true;
136 > return true;
137 > }
139 > acceptFrame(frame: string): void {
141 > this._onMessage.fire(JSON.parse(frame) as ProtocolMessage);
142 > } catch {
143 > this.send({ jsonrpc: '2.0', id: null, error: { code: JSON_RPC_PARSE_ERROR, message: 'Parse error' } }); messagePortProtocolServer.ts ×1
144 > }
147 > send(message: ProtocolMessage | AhpServerNotification | JsonRpcNotification | JsonRpcParseErrorResponse | JsonRpcResponse | JsonRpcRequest): void {
148 > if (!this.isConnected) { messagePortProtocolServer.ts ×2
149 return;
150 }
152 > this._onFrame.fire(JSON.stringify(message));
153 > }
155 > override dispose(): void {
156 > if (this._isClosed) { messagePortProtocolServer.ts ×19
158 > }
160 > this._isClosed = true;
161 > this._isConnected = false;
162 > this._onClose.fire();
163 > super.dispose();
164 > }