(requestedAuthToken?: string)
| 150 | } |
| 151 | |
| 152 | async 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 |
no test coverage detected