Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

storage.ts

656 lines24.0 KB

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