MCPcopy Index your code
hub / github.com/coder/mux / connectToInProcessServer

Function connectToInProcessServer

src/node/acp/serverConnection.ts:152–216  ·  view source on GitHub ↗
(requestedAuthToken?: string)

Source from the content-addressed store, hash-verified

150}
151
152async function connectToInProcessServer(requestedAuthToken?: string): Promise<ServerConnection> {
153 const authToken = requestedAuthToken ?? crypto.randomUUID();
154 const config = new Config();
155 const serviceContainer = new ServiceContainer(config);
156
157 let initialized = false;
158 let inProcessServer: InProcessOrpcServer | undefined;
159
160 try {
161 await serviceContainer.initialize();
162 initialized = true;
163
164 const context = serviceContainer.toORPCContext();
165 inProcessServer = await createOrpcServer({
166 host: "127.0.0.1",
167 port: 0,
168 authToken,
169 context,
170 });
171
172 const activeServer = inProcessServer;
173 assert(
174 activeServer != null,
175 "connectToInProcessServer: expected createOrpcServer to return a server instance"
176 );
177
178 const connection = await connectViaWebSocket(activeServer.baseUrl, authToken);
179
180 return {
181 client: connection.client,
182 inProcessServer: activeServer,
183 baseUrl: connection.baseUrl,
184 close: async () => {
185 let firstError: Error | undefined;
186
187 const captureError = (error: unknown) => {
188 firstError ??=
189 error instanceof Error
190 ? error
191 : new Error("connectToInProcessServer: failed to close resources", {
192 cause: error,
193 });
194 };
195
196 await closeWebSocket(connection.websocket).catch(captureError);
197 await activeServer.close().catch(captureError);
198 await serviceContainer.dispose().catch(captureError);
199
200 if (firstError !== undefined) {
201 throw firstError;
202 }
203 },
204 };
205 } catch (error) {
206 if (inProcessServer) {
207 await inProcessServer.close().catch(() => undefined);
208 }
209

Callers 1

connectToServerFunction · 0.85

Calls 8

initializeMethod · 0.95
toORPCContextMethod · 0.95
disposeMethod · 0.95
createOrpcServerFunction · 0.90
connectViaWebSocketFunction · 0.85
closeWebSocketFunction · 0.70
closeMethod · 0.65
assertFunction · 0.50

Tested by

no test coverage detected