Open History to see the last commit
1import {
2 ConnectionCatalogDocument,
3 type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
4 ConnectionPersistenceError,
5 ConnectionRegistrationStore,
6 ConnectionTargetStore,
7 EMPTY_CONNECTION_CATALOG_DOCUMENT,
8 EnvironmentCacheStore,
9 ORCHESTRATION_CACHE_SCHEMA_VERSION,
10 StoredOrchestrationShellSnapshot,
11 StoredOrchestrationThreadSnapshot,
12 decodeOrDiscardOrchestrationCache,
13 putRemoteDpopTokenInCatalog,
14 registerConnectionInCatalog,
15 removeCatalogValue,
16 removeConnectionFromCatalog,
17 replaceCatalogValue,
18} from "@t3tools/client-runtime/platform";
19import { TokenStore } from "@t3tools/client-runtime/authorization";
20import {
21 ConnectionTransientError,
22 CredentialStore,
23 ProfileStore,
24} from "@t3tools/client-runtime/connection";
25import { EnvironmentId, ServerConfig, ThreadId, VcsListRefsResult } from "@t3tools/contracts";
26import * as Context from "effect/Context";
27import * as Effect from "effect/Effect";
28import * as Layer from "effect/Layer";
29import * as Option from "effect/Option";
30import * as Ref from "effect/Ref";
31import * as Schema from "effect/Schema";
32import * as Semaphore from "effect/Semaphore";
33
34const DATABASE_NAME = "t3code:connection-runtime";
35const DATABASE_VERSION = 4;
36const CATALOG_STORE_NAME = "catalog";
37const SHELL_STORE_NAME = "shell";
38const THREAD_STORE_NAME = "thread";
39const SERVER_CONFIG_STORE_NAME = "server-config";
40const VCS_REFS_STORE_NAME = "vcs-refs";
41const CATALOG_KEY = "document";
42const StoredShellSnapshot = StoredOrchestrationShellSnapshot;
43const StoredShellSnapshotJson = Schema.fromJsonString(StoredShellSnapshot);
44const StoredThreadSnapshot = StoredOrchestrationThreadSnapshot;
45const StoredThreadSnapshotJson = Schema.fromJsonString(StoredThreadSnapshot);
46const StoredServerConfig = Schema.Struct({
47 schemaVersion: Schema.Literal(1),
48 environmentId: EnvironmentId,
49 config: ServerConfig,
50});
51const StoredServerConfigJson = Schema.fromJsonString(StoredServerConfig);
52const StoredVcsRefs = Schema.Struct({
53 schemaVersion: Schema.Literal(1),
54 environmentId: EnvironmentId,
55 cwd: Schema.String,
56 refs: VcsListRefsResult,
57});
58const StoredVcsRefsJson = Schema.fromJsonString(StoredVcsRefs);
59const ConnectionCatalogDocumentJson = Schema.fromJsonString(ConnectionCatalogDocument);
60const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCatalogDocumentJson);
61const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
62const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
63const encodeStoredShellSnapshot = Schema.encodeEffect(StoredShellSnapshotJson);
64const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
65const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson);
66const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
67const encodeStoredServerConfig = Schema.encodeEffect(StoredServerConfigJson);
68const decodeStoredVcsRefs = Schema.decodeUnknownEffect(StoredVcsRefsJson);
69const encodeStoredVcsRefs = Schema.encodeEffect(StoredVcsRefsJson);
70
71function catalogError(operation: string, cause: unknown) {
72 return new ConnectionTransientError({
73 reason: "remote-unavailable",
74 detail: `Could not ${operation} the local connection catalog: ${String(cause)}`,
75 });
76}
77
78function persistenceError(
79 operation:
80 | "list-targets"
81 | "register-connection"
82 | "remove-connection"
83 | "load-shell"
84 | "save-shell"
85 | "load-thread"
86 | "save-thread"
87 | "remove-thread"
88 | "load-server-config"
89 | "save-server-config"
90 | "load-vcs-refs"
91 | "save-vcs-refs"
92 | "remove-vcs-refs"
93 | "clear-vcs-refs"
94 | "clear-environment",
95 cause: unknown,
96) {
97 return new ConnectionPersistenceError({
98 operation,
99 message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
100 });
101}
102
103const openDatabase = Effect.fn("web.connectionStorage.openDatabase")(function* () {
104 return yield* Effect.callback<IDBDatabase, ConnectionTransientError>((resume) => {
105 if (typeof indexedDB === "undefined") {
106 resume(
107 Effect.fail(catalogError("open", "IndexedDB is unavailable in this browser context.")),
108 );
109 return;
110 }
111 const request = indexedDB.open(DATABASE_NAME, DATABASE_VERSION);
112 request.addEventListener("upgradeneeded", () => {
113 if (!request.result.objectStoreNames.contains(CATALOG_STORE_NAME)) {
114 request.result.createObjectStore(CATALOG_STORE_NAME);
115 }
116 if (!request.result.objectStoreNames.contains(SHELL_STORE_NAME)) {
117 request.result.createObjectStore(SHELL_STORE_NAME);
118 }
119 if (!request.result.objectStoreNames.contains(THREAD_STORE_NAME)) {
120 request.result.createObjectStore(THREAD_STORE_NAME);
121 }
122 if (!request.result.objectStoreNames.contains(SERVER_CONFIG_STORE_NAME)) {
123 request.result.createObjectStore(SERVER_CONFIG_STORE_NAME);
124 }
125 if (!request.result.objectStoreNames.contains(VCS_REFS_STORE_NAME)) {
126 request.result.createObjectStore(VCS_REFS_STORE_NAME);
127 }
128 });
129 request.addEventListener("error", () => {
130 resume(Effect.fail(catalogError("open", request.error ?? "Unknown IndexedDB error")));
131 });
132 request.addEventListener("success", () => {
133 resume(Effect.succeed(request.result));
134 });
135 });
136});
137
138function readDatabaseValue(database: IDBDatabase, storeName: string, key: IDBValidKey) {
139 return Effect.callback<unknown, ConnectionTransientError>((resume) => {
140 const request = database.transaction(storeName, "readonly").objectStore(storeName).get(key);
141 request.addEventListener("error", () => {
142 resume(Effect.fail(catalogError("read", request.error ?? "Unknown IndexedDB read error")));
143 });
144 request.addEventListener("success", () => {
145 resume(Effect.succeed(request.result));
146 });
147 }).pipe(Effect.withSpan("web.connectionStorage.readDatabaseValue"));
148}
149
150function writeDatabaseValue(
151 database: IDBDatabase,
152 storeName: string,
153 key: IDBValidKey,
154 value: unknown,
155) {
156 return Effect.callback<void, ConnectionTransientError>((resume) => {
157 const transaction = database.transaction(storeName, "readwrite");
158 transaction.addEventListener("error", () => {
159 resume(
160 Effect.fail(catalogError("write", transaction.error ?? "Unknown IndexedDB write error")),
161 );
162 });
163 transaction.addEventListener("complete", () => {
164 resume(Effect.void);
165 });
166 transaction.objectStore(storeName).put(value, key);
167 }).pipe(Effect.withSpan("web.connectionStorage.writeDatabaseValue"));
168}
169
170function removeDatabaseValue(database: IDBDatabase, storeName: string, key: IDBValidKey) {
171 return Effect.callback<void, ConnectionTransientError>((resume) => {
172 const transaction = database.transaction(storeName, "readwrite");
173 transaction.addEventListener("error", () => {
174 resume(
175 Effect.fail(catalogError("remove", transaction.error ?? "Unknown IndexedDB remove error")),
176 );
177 });
178 transaction.addEventListener("complete", () => {
179 resume(Effect.void);
180 });
181 transaction.objectStore(storeName).delete(key);
182 }).pipe(Effect.withSpan("web.connectionStorage.removeDatabaseValue"));
183}
184
185function removeDatabaseValuesInRange(database: IDBDatabase, storeName: string, range: IDBKeyRange) {
186 return Effect.callback<void, ConnectionTransientError>((resume) => {
187 const transaction = database.transaction(storeName, "readwrite");
188 transaction.addEventListener("error", () => {
189 resume(
190 Effect.fail(catalogError("remove", transaction.error ?? "Unknown IndexedDB cursor error")),
191 );
192 });
193 transaction.addEventListener("complete", () => {
194 resume(Effect.void);
195 });
196 const request = transaction.objectStore(storeName).openCursor(range);
197 request.addEventListener("error", () => {
198 resume(
199 Effect.fail(catalogError("remove", request.error ?? "Unknown IndexedDB cursor error")),
200 );
201 });
202 request.addEventListener("success", () => {
203 const cursor = request.result;
204 if (cursor === null) {
205 return;
206 }
207 cursor.delete();
208 cursor.continue();
209 });
210 }).pipe(Effect.withSpan("web.connectionStorage.removeDatabaseValuesInRange"));
211}
212
213function threadCacheKey(environmentId: EnvironmentId, threadId: ThreadId) {
214 return `${environmentId}:${threadId}`;
215}
216
217function vcsRefsCacheKey(environmentId: EnvironmentId, cwd: string) {
218 return `${environmentId}:${cwd}`;
219}
220
221const decodeCatalog = Effect.fn("web.connectionStorage.decodeCatalog")(function* (raw: string) {
222 return yield* decodeConnectionCatalogDocument(raw).pipe(
223 Effect.mapError((cause) => catalogError("decode", cause)),
224 );
225});
226
227const encodeCatalog = Effect.fn("web.connectionStorage.encodeCatalog")(function* (
228 catalog: ConnectionCatalogDocumentType,
229) {
230 return yield* encodeConnectionCatalogDocument(catalog).pipe(
231 Effect.mapError((cause) => catalogError("encode", cause)),
232 );
233});
234
235export interface CatalogBackend {
236 readonly read: Effect.Effect<string | null, ConnectionTransientError>;
237 readonly write: (raw: string) => Effect.Effect<void, ConnectionTransientError>;
238 readonly quarantine?: (raw: string) => Effect.Effect<void, ConnectionTransientError>;
239}
240
241export function makeCatalogBackend(database: IDBDatabase): CatalogBackend {
242 const bridge = window.desktopBridge;
243 if (bridge?.getConnectionCatalog !== undefined && bridge.setConnectionCatalog !== undefined) {
244 return {
245 read: Effect.tryPromise({
246 try: () => bridge.getConnectionCatalog!(),
247 catch: (cause) => catalogError("load", cause),
248 }),
249 write: (raw) =>
250 Effect.tryPromise({
251 try: () => bridge.setConnectionCatalog!(raw),
252 catch: (cause) => catalogError("save", cause),
253 }).pipe(
254 Effect.flatMap((stored) =>
255 stored
256 ? Effect.void
257 : Effect.fail(
258 catalogError(
259 "save",
260 "Desktop secure storage is unavailable in this system context.",
261 ),
262 ),
263 ),
264 ),
265 };
266 }
267
268 return {
269 read: readDatabaseValue(database, CATALOG_STORE_NAME, CATALOG_KEY).pipe(
270 Effect.map((value) => (typeof value === "string" ? value : null)),
271 ),
272 write: (raw) => writeDatabaseValue(database, CATALOG_STORE_NAME, CATALOG_KEY, raw),
273 quarantine: (raw) =>
274 writeDatabaseValue(database, CATALOG_STORE_NAME, `${CATALOG_KEY}:corrupt:${Date.now()}`, raw),
275 };
276}
277
278interface CatalogStore {
279 readonly read: Effect.Effect<ConnectionCatalogDocumentType, ConnectionTransientError>;
280 readonly update: (
281 transform: (catalog: ConnectionCatalogDocumentType) => ConnectionCatalogDocumentType,
282 ) => Effect.Effect<void, ConnectionTransientError>;
283}
284
285export const makeCatalogStore = Effect.fn("web.connectionStorage.makeCatalogStore")(function* (
286 backend: CatalogBackend,
287) {
288 const state = yield* Ref.make<Option.Option<ConnectionCatalogDocumentType>>(Option.none());
289 const lock = yield* Semaphore.make(1);
290
291 const loadUnlocked = Effect.fn("web.connectionStorage.loadCatalog")(function* () {
292 const cached = yield* Ref.get(state);
293 if (Option.isSome(cached)) {
294 return cached.value;
295 }
296 const raw = yield* backend.read;
297 let catalog = EMPTY_CONNECTION_CATALOG_DOCUMENT;
298 if (raw !== null && raw.trim() !== "") {
299 catalog = yield* decodeCatalog(raw).pipe(
300 Effect.catch((error) =>
301 Effect.gen(function* () {
302 yield* Effect.logWarning("Discarding a corrupt web connection catalog.", {
303 error: error.message,
304 });
305 if (backend.quarantine !== undefined) {
306 yield* backend.quarantine(raw).pipe(
307 Effect.catch((cause) =>
308 Effect.logWarning("Could not quarantine the corrupt web connection catalog.", {
309 error: cause.message,
310 }),
311 ),
312 );
313 }
314 const encoded = yield* encodeCatalog(EMPTY_CONNECTION_CATALOG_DOCUMENT);
315 yield* backend.write(encoded).pipe(
316 Effect.catch((cause) =>
317 Effect.logWarning("Could not persist the recovered web connection catalog.", {
318 error: cause.message,
319 }),
320 ),
321 );
322 return EMPTY_CONNECTION_CATALOG_DOCUMENT;
323 }),
324 ),
325 );
326 }
327 yield* Ref.set(state, Option.some(catalog));
328 return catalog;
329 });
330
331 const read = lock.withPermits(1)(loadUnlocked());
332 const update: CatalogStore["update"] = Effect.fn("web.connectionStorage.updateCatalog")(
333 function* (transform) {
334 yield* lock.withPermits(1)(
335 Effect.gen(function* () {
336 const next = transform(yield* loadUnlocked());
337 yield* backend.write(yield* encodeCatalog(next));
338 yield* Ref.set(state, Option.some(next));
339 }),
340 );
341 },
342 );
343
344 return { read, update } satisfies CatalogStore;
345});
346
347export const connectionStorageLayer = Layer.effectContext(
348 Effect.gen(function* () {
349 const database = yield* Effect.acquireRelease(openDatabase(), (database) =>
350 Effect.sync(() => database.close()),
351 );
352 const catalog = yield* makeCatalogStore(makeCatalogBackend(database));
353
354 const targetStore = ConnectionTargetStore.of({
355 list: catalog.read.pipe(
356 Effect.map((document) => document.targets),
357 Effect.mapError((cause) => persistenceError("list-targets", cause)),
358 ),
359 });
360 const registrationStore = ConnectionRegistrationStore.of({
361 register: (registration) =>
362 catalog
363 .update((document) => registerConnectionInCatalog(document, registration))
364 .pipe(Effect.mapError((cause) => persistenceError("register-connection", cause))),
365 remove: (target) =>
366 catalog
367 .update((document) => removeConnectionFromCatalog(document, target))
368 .pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause))),
369 });
370 const profileStore = ProfileStore.make({
371 get: (connectionId) =>
372 catalog.read.pipe(
373 Effect.map((document) =>
374 Option.fromUndefinedOr(
375 document.profiles.find((profile) => profile.connectionId === connectionId),
376 ),
377 ),
378 ),
379 put: (profile) =>
380 catalog.update((document) => ({
381 ...document,
382 profiles: replaceCatalogValue(document.profiles, (value) => value.connectionId, profile),
383 })),
384 remove: (connectionId) =>
385 catalog.update((document) => ({
386 ...document,
387 profiles: removeCatalogValue(
388 document.profiles,
389 (value) => value.connectionId,
390 connectionId,
391 ),
392 })),
393 });
394 const credentialStore = CredentialStore.make({
395 get: (connectionId) =>
396 catalog.read.pipe(
397 Effect.map((document) =>
398 Option.fromUndefinedOr(
399 document.credentials.find((entry) => entry.connectionId === connectionId)?.credential,
400 ),
401 ),
402 ),
403 put: (connectionId, credential) =>
404 catalog.update((document) => ({
405 ...document,
406 credentials: replaceCatalogValue(document.credentials, (value) => value.connectionId, {
407 connectionId,
408 credential,
409 }),
410 })),
411 remove: (connectionId) =>
412 catalog.update((document) => ({
413 ...document,
414 credentials: removeCatalogValue(
415 document.credentials,
416 (value) => value.connectionId,
417 connectionId,
418 ),
419 })),
420 });
421 const remoteTokenStore = TokenStore.make({
422 get: (environmentId) =>
423 catalog.read.pipe(
424 Effect.map((document) =>
425 Option.fromUndefinedOr(
426 document.remoteDpopTokens.find((token) => token.environmentId === environmentId),
427 ),
428 ),
429 ),
430 put: (token) => catalog.update((document) => putRemoteDpopTokenInCatalog(document, token)),
431 remove: (environmentId) =>
432 catalog.update((document) => ({
433 ...document,
434 remoteDpopTokens: removeCatalogValue(
435 document.remoteDpopTokens,
436 (value) => value.environmentId,
437 environmentId,
438 ),
439 })),
440 });
441 const cacheStore = EnvironmentCacheStore.of({
442 loadShell: (environmentId) =>
443 readDatabaseValue(database, SHELL_STORE_NAME, environmentId).pipe(
444 Effect.flatMap((raw) => {
445 if (typeof raw !== "string") {
446 return Effect.succeed(Option.none());
447 }
448 return decodeOrDiscardOrchestrationCache(
449 decodeStoredShellSnapshot(raw).pipe(
450 Effect.mapError((cause) => persistenceError("load-shell", cause)),
451 Effect.map((stored) =>
452 stored.environmentId === environmentId
453 ? Option.some(stored.snapshot)
454 : Option.none(),
455 ),
456 ),
457 removeDatabaseValue(database, SHELL_STORE_NAME, environmentId),
458 );
459 }),
460 Effect.mapError((cause) => persistenceError("load-shell", cause)),
461 ),
462 saveShell: (environmentId, snapshot) =>
463 Effect.gen(function* () {
464 const encoded = yield* encodeStoredShellSnapshot({
465 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
466 environmentId,
467 snapshot,
468 }).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
469 yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
470 }).pipe(
471 Effect.mapError((cause) =>
472 cause._tag === "ConnectionPersistenceError"
473 ? cause
474 : persistenceError("save-shell", cause),
475 ),
476 ),
477 loadServerConfig: (environmentId) =>
478 readDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId).pipe(
479 Effect.flatMap((raw) => {
480 if (typeof raw !== "string") {
481 return Effect.succeed(Option.none());
482 }
483 return decodeStoredServerConfig(raw).pipe(
484 Effect.mapError((cause) => persistenceError("load-server-config", cause)),
485 Effect.map((stored) =>
486 stored.environmentId === environmentId ? Option.some(stored.config) : Option.none(),
487 ),
488 );
489 }),
490 Effect.mapError((cause) =>
491 cause._tag === "ConnectionPersistenceError"
492 ? cause
493 : persistenceError("load-server-config", cause),
494 ),
495 ),
496 saveServerConfig: (environmentId, config) =>
497 Effect.gen(function* () {
498 const encoded = yield* encodeStoredServerConfig({
499 schemaVersion: 1,
500 environmentId,
501 config,
502 }).pipe(Effect.mapError((cause) => persistenceError("save-server-config", cause)));
503 yield* writeDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId, encoded);
504 }).pipe(
505 Effect.mapError((cause) =>
506 cause._tag === "ConnectionPersistenceError"
507 ? cause
508 : persistenceError("save-server-config", cause),
509 ),
510 ),
511 loadThread: (environmentId, threadId) =>
512 readDatabaseValue(
513 database,
514 THREAD_STORE_NAME,
515 threadCacheKey(environmentId, threadId),
516 ).pipe(
517 Effect.flatMap((raw) => {
518 if (typeof raw !== "string") {
519 return Effect.succeed(Option.none());
520 }
521 return decodeOrDiscardOrchestrationCache(
522 decodeStoredThreadSnapshot(raw).pipe(
523 Effect.mapError((cause) => persistenceError("load-thread", cause)),
524 Effect.map((stored) =>
525 stored.environmentId === environmentId && stored.threadId === threadId
526 ? Option.some(stored.snapshot)
527 : Option.none(),
528 ),
529 ),
530 removeDatabaseValue(
531 database,
532 THREAD_STORE_NAME,
533 threadCacheKey(environmentId, threadId),
534 ),
535 );
536 }),
537 Effect.mapError((cause) => persistenceError("load-thread", cause)),
538 ),
539 saveThread: (environmentId, snapshot) =>
540 Effect.gen(function* () {
541 const encoded = yield* encodeStoredThreadSnapshot({
542 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
543 environmentId,
544 threadId: snapshot.projection.thread.id,
545 snapshot,
546 }).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
547 yield* writeDatabaseValue(
548 database,
549 THREAD_STORE_NAME,
550 threadCacheKey(environmentId, snapshot.projection.thread.id),
551 encoded,
552 );
553 }).pipe(
554 Effect.mapError((cause) =>
555 cause._tag === "ConnectionPersistenceError"
556 ? cause
557 : persistenceError("save-thread", cause),
558 ),
559 ),
560 loadVcsRefs: (environmentId, cwd) =>
561 readDatabaseValue(database, VCS_REFS_STORE_NAME, vcsRefsCacheKey(environmentId, cwd)).pipe(
562 Effect.flatMap((raw) => {
563 if (typeof raw !== "string") {
564 return Effect.succeed(Option.none());
565 }
566 return decodeStoredVcsRefs(raw).pipe(
567 Effect.mapError((cause) => persistenceError("load-vcs-refs", cause)),
568 Effect.map((stored) =>
569 stored.environmentId === environmentId && stored.cwd === cwd
570 ? Option.some(stored.refs)
571 : Option.none(),
572 ),
573 );
574 }),
575 Effect.mapError((cause) =>
576 cause._tag === "ConnectionPersistenceError"
577 ? cause
578 : persistenceError("load-vcs-refs", cause),
579 ),
580 ),
581 saveVcsRefs: (environmentId, cwd, refs) =>
582 Effect.gen(function* () {
583 const encoded = yield* encodeStoredVcsRefs({
584 schemaVersion: 1,
585 environmentId,
586 cwd,
587 refs,
588 }).pipe(Effect.mapError((cause) => persistenceError("save-vcs-refs", cause)));
589 yield* writeDatabaseValue(
590 database,
591 VCS_REFS_STORE_NAME,
592 vcsRefsCacheKey(environmentId, cwd),
593 encoded,
594 );
595 }).pipe(
596 Effect.mapError((cause) =>
597 cause._tag === "ConnectionPersistenceError"
598 ? cause
599 : persistenceError("save-vcs-refs", cause),
600 ),
601 ),
602 removeVcsRefs: (environmentId, cwd) =>
603 removeDatabaseValue(
604 database,
605 VCS_REFS_STORE_NAME,
606 vcsRefsCacheKey(environmentId, cwd),
607 ).pipe(Effect.mapError((cause) => persistenceError("remove-vcs-refs", cause))),
608 clearVcsRefs: (environmentId) =>
609 removeDatabaseValuesInRange(
610 database,
611 VCS_REFS_STORE_NAME,
612 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
613 ).pipe(Effect.mapError((cause) => persistenceError("clear-vcs-refs", cause))),
614 removeThread: (environmentId, threadId) =>
615 removeDatabaseValue(
616 database,
617 THREAD_STORE_NAME,
618 threadCacheKey(environmentId, threadId),
619 ).pipe(Effect.mapError((cause) => persistenceError("remove-thread", cause))),
620 clear: (environmentId) =>
621 Effect.all(
622 [
623 removeDatabaseValue(database, SHELL_STORE_NAME, environmentId),
624 removeDatabaseValuesInRange(
625 database,
626 THREAD_STORE_NAME,
627 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
628 ),
629 removeDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId),
630 removeDatabaseValuesInRange(
631 database,
632 VCS_REFS_STORE_NAME,
633 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
634 ),
635 ],
636 { concurrency: "unbounded", discard: true },
637 ).pipe(Effect.mapError((cause) => persistenceError("clear-environment", cause))),
638 });
639
640 return Context.make(ConnectionTargetStore, targetStore).pipe(
641 Context.add(ConnectionRegistrationStore, registrationStore),
642 Context.add(ProfileStore.ConnectionProfileStore, profileStore),
643 Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
644 Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
645 Context.add(EnvironmentCacheStore, cacheStore),
646 );
647 }),
648);