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

storage.ts

1,119 lines41.9 KB

1import {
2 ConnectionCatalogDocument,
3 type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
4 EMPTY_CONNECTION_CATALOG_DOCUMENT,
5 ORCHESTRATION_CACHE_SCHEMA_VERSION,
6 StoredOrchestrationShellSnapshot,
7 StoredOrchestrationThreadSnapshot,
8 decodeOrDiscardOrchestrationCache,
9 putRemoteDpopTokenInCatalog,
10 registerConnectionInCatalog,
11 removeCatalogValue,
12 removeConnectionFromCatalog,
13 setConnectionEnabledInCatalog,
14 setRoutesInCatalog,
15 replaceCatalogValue,
16 Persistence,
17 makeStoredShellSnapshotEncoder,
18 makeStoredThreadSnapshotEncoder,
19 makeThreadCacheWriteOrder,
20} from "@t3tools/client-runtime/platform";
21import { TokenStore } from "@t3tools/client-runtime/authorization";
22import {
23 ConnectionTransientError,
24 ConnectionBlockedError,
25 CredentialStore,
26 ProfileStore,
27 PullRequestRoutingPermissions,
28 StoredPullRequestRoutingPermission,
29 pullRequestRoutingConnectionKey,
30 pullRequestRoutingPermissionFor,
31} from "@t3tools/client-runtime/connection";
32import { EnvironmentQueryStore } from "@t3tools/client-runtime/state/query-persistence";
33import { EnvironmentId, ServerConfig, ThreadId, VcsListRefsResult } from "@t3tools/contracts";
34import * as Clock from "effect/Clock";
35import * as Context from "effect/Context";
36import * as Effect from "effect/Effect";
37import * as Layer from "effect/Layer";
38import * as Option from "effect/Option";
39import * as Ref from "effect/Ref";
40import * as Queue from "effect/Queue";
41import * as Schema from "effect/Schema";
42import * as Semaphore from "effect/Semaphore";
43import * as Stream from "effect/Stream";
44import { projectFaviconCache } from "../assets/projectFaviconCache";
45​
46const DATABASE_NAME = "t3code:connection-runtime";
47const DATABASE_VERSION = 5;
48const CATALOG_STORE_NAME = "catalog";
49const SHELL_STORE_NAME = "shell";
50const THREAD_STORE_NAME = "thread";
51const SERVER_CONFIG_STORE_NAME = "server-config";
52const VCS_REFS_STORE_NAME = "vcs-refs";
53const QUERY_STORE_NAME = "query";
54const QUERY_UPDATED_INDEX_NAME = "environment-updated";
55const QUERY_MAX_ROWS = 256;
56const QUERY_MAX_BYTES = 32 * 1024 * 1024;
57const QUERY_MAX_PAYLOAD_BYTES = 4 * 1024 * 1024;
58const QUERY_MAX_AGE_MS = 30 * 24 * 60 * 60_000;
59// A stats answer saves one row per listed PR, so the bounds index is walked once per batch of
60// saves. Each walk trims a batch below the caps, so the caps hold between walks. The batch count
61// lives in the database, so tabs sharing it share one count.
62const QUERY_PRUNE_BATCH_ROWS = 32;
63const QUERY_PRUNE_BATCH_BYTES = 1024 * 1024;
64const CATALOG_KEY = "document";
65const StoredShellSnapshot = StoredOrchestrationShellSnapshot;
66const StoredShellSnapshotJson = Schema.fromJsonString(StoredShellSnapshot);
67const StoredThreadSnapshot = StoredOrchestrationThreadSnapshot;
68const StoredThreadSnapshotJson = Schema.fromJsonString(StoredThreadSnapshot);
69const StoredServerConfig = Schema.Struct({
70 schemaVersion: Schema.Literal(1),
71 environmentId: EnvironmentId,
72 config: ServerConfig,
73});
74const StoredServerConfigJson = Schema.fromJsonString(StoredServerConfig);
75const StoredVcsRefs = Schema.Struct({
76 schemaVersion: Schema.Literal(1),
77 environmentId: EnvironmentId,
78 cwd: Schema.String,
79 refs: VcsListRefsResult,
80});
81const StoredVcsRefsJson = Schema.fromJsonString(StoredVcsRefs);
82const isStoredQuery = Schema.is(
83 Schema.Struct({ environmentId: Schema.String, payload: Schema.String, updatedAt: Schema.Number }),
84);
85const isQueryBatch = Schema.is(Schema.Struct({ rows: Schema.Number, bytes: Schema.Number }));
86const ConnectionCatalogDocumentJson = Schema.fromJsonString(ConnectionCatalogDocument);
87const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCatalogDocumentJson);
88const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
89const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
90const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
91const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
92const encodeStoredServerConfig = Schema.encodeEffect(StoredServerConfigJson);
93const decodeStoredVcsRefs = Schema.decodeUnknownEffect(StoredVcsRefsJson);
94const encodeStoredVcsRefs = Schema.encodeEffect(StoredVcsRefsJson);
95​
96function catalogError(operation: string, cause: unknown) {
97 return new ConnectionTransientError({
98 reason: "remote-unavailable",
99 detail: `Could not ${operation} the local connection catalog: ${String(cause)}`,
100 });
102​
103function persistenceError(
104 operation:
105 | "list-targets"
106 | "list-disabled-targets"
107 | "register-connection"
108 | "set-connection-routes"
109 | "remove-connection"
110 | "set-connection-enabled"
111 | "load-shell"
112 | "save-shell"
113 | "load-thread"
114 | "save-thread"
115 | "remove-thread"
116 | "load-server-config"
117 | "save-server-config"
118 | "load-vcs-refs"
119 | "save-vcs-refs"
120 | "remove-vcs-refs"
121 | "clear-vcs-refs"
122 | "clear-environment",
123 cause: unknown,
124) {
125 return new Persistence.ConnectionPersistenceError({
126 operation,
127 message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
128 });
130​
131const openDatabase = Effect.fn("web.connectionStorage.openDatabase")(function* () {
132 return yield* Effect.callback<IDBDatabase, ConnectionTransientError>((resume) => {
133 if (typeof indexedDB === "undefined") {
134 resume(
135 Effect.fail(catalogError("open", "IndexedDB is unavailable in this browser context.")),
136 );
137 return;
138 }
139 try {
140 const request = indexedDB.open(DATABASE_NAME, DATABASE_VERSION);
141 request.addEventListener("upgradeneeded", () => {
142 if (!request.result.objectStoreNames.contains(CATALOG_STORE_NAME)) {
143 request.result.createObjectStore(CATALOG_STORE_NAME);
144 }
145 if (!request.result.objectStoreNames.contains(SHELL_STORE_NAME)) {
146 request.result.createObjectStore(SHELL_STORE_NAME);
147 }
148 if (!request.result.objectStoreNames.contains(THREAD_STORE_NAME)) {
149 request.result.createObjectStore(THREAD_STORE_NAME);
150 }
151 if (!request.result.objectStoreNames.contains(SERVER_CONFIG_STORE_NAME)) {
152 request.result.createObjectStore(SERVER_CONFIG_STORE_NAME);
153 }
154 if (!request.result.objectStoreNames.contains(VCS_REFS_STORE_NAME)) {
155 request.result.createObjectStore(VCS_REFS_STORE_NAME);
156 }
157 if (!request.result.objectStoreNames.contains(QUERY_STORE_NAME)) {
158 request.result
159 .createObjectStore(QUERY_STORE_NAME)
160 .createIndex(QUERY_UPDATED_INDEX_NAME, ["environmentId", "updatedAt", "bytes"]);
161 }
162 });
163 request.addEventListener("error", () => {
164 resume(Effect.fail(catalogError("open", request.error ?? "Unknown IndexedDB error")));
165 });
166 request.addEventListener("success", () => {
167 resume(Effect.succeed(request.result));
168 });
169 } catch (cause) {
170 resume(Effect.fail(catalogError("open", cause)));
171 }
172 });
173});
174​
175interface DatabaseHandle {
176 readonly get: Effect.Effect<IDBDatabase, ConnectionTransientError>;
177 readonly invalidate: (database: IDBDatabase) => Effect.Effect<void>;
179​
180const makeDatabaseHandle = Effect.fn("web.connectionStorage.makeDatabaseHandle")(function* () {
181 const lock = yield* Semaphore.make(1);
182 let current: IDBDatabase | null = null;
183 const forget = (database: IDBDatabase) => {
184 if (current === database) current = null;
185 };
186 const get = Effect.suspend(() =>
187 current !== null
188 ? Effect.succeed(current)
189 : lock.withPermits(1)(
190 Effect.gen(function* () {
191 if (current !== null) return current;
192 const opened = yield* openDatabase();
193 current = opened;
194 opened.addEventListener("close", () => forget(opened));
195 // Another tab upgrading the schema waits on this connection.
196 opened.addEventListener("versionchange", () => {
197 forget(opened);
198 opened.close();
199 });
200 return opened;
201 }),
202 ),
203 );
204 const close = lock.withPermits(1)(
205 Effect.sync(() => {
206 current?.close();
207 current = null;
208 }),
209 );
210 const handle: DatabaseHandle = {
211 get,
212 invalidate: (database) => Effect.sync(() => forget(database)),
213 };
214 return { handle, close };
215});
216​
217// Browsers can omit close events; retry InvalidStateError with a fresh connection.
218function withDatabase<A>(
219 database: DatabaseHandle,
220 use: (opened: IDBDatabase) => Effect.Effect<A, ConnectionTransientError>,
221) {
222 const closedConnection = Symbol("closedConnection");
223 const attempt = Effect.flatMap(database.get, (opened) =>
224 use(opened).pipe(
225 Effect.catchIf(
226 (error) => error.detail.includes("InvalidStateError"),
227 (error) =>
228 database
229 .invalidate(opened)
230 .pipe(Effect.andThen(Effect.fail({ [closedConnection]: error } as const))),
231 ),
232 ),
233 );
234 return attempt.pipe(
235 Effect.catchIf(
236 (error): error is { readonly [closedConnection]: ConnectionTransientError } =>
237 closedConnection in error,
238 () => Effect.flatMap(database.get, use),
239 ),
240 );
242​
243function readDatabaseValueOnConnection(database: IDBDatabase, storeName: string, key: IDBValidKey) {
244 return Effect.callback<unknown, ConnectionTransientError>((resume) => {
245 try {
246 const request = database.transaction(storeName, "readonly").objectStore(storeName).get(key);
247 request.addEventListener("error", () => {
248 resume(Effect.fail(catalogError("read", request.error ?? "Unknown IndexedDB read error")));
249 });
250 request.addEventListener("success", () => {
251 resume(Effect.succeed(request.result));
252 });
253 } catch (cause) {
254 resume(Effect.fail(catalogError("read", cause)));
255 }
256 }).pipe(Effect.withSpan("web.connectionStorage.readDatabaseValue"));
258​
259function writeDatabaseValueOnConnection(
260 database: IDBDatabase,
261 storeName: string,
262 key: IDBValidKey,
263 value: unknown,
264) {
265 return Effect.callback<void, ConnectionTransientError>((resume) => {
266 try {
267 const transaction = database.transaction(storeName, "readwrite");
268 // Commit failures can fire abort without error; every failed write fires abort.
269 transaction.addEventListener("abort", () => {
270 resume(
271 Effect.fail(catalogError("write", transaction.error ?? "Unknown IndexedDB write error")),
272 );
273 });
274 transaction.addEventListener("complete", () => {
275 resume(Effect.void);
276 });
277 transaction.objectStore(storeName).put(value, key);
278 } catch (cause) {
279 resume(Effect.fail(catalogError("write", cause)));
280 }
281 }).pipe(Effect.withSpan("web.connectionStorage.writeDatabaseValue"));
283​
284function removeDatabaseValueOnConnection(
285 database: IDBDatabase,
286 storeName: string,
287 key: IDBValidKey,
288) {
289 return Effect.callback<void, ConnectionTransientError>((resume) => {
290 try {
291 const transaction = database.transaction(storeName, "readwrite");
292 transaction.addEventListener("abort", () => {
293 resume(
294 Effect.fail(
295 catalogError("remove", transaction.error ?? "Unknown IndexedDB remove error"),
296 ),
297 );
298 });
299 transaction.addEventListener("complete", () => {
300 resume(Effect.void);
301 });
302 transaction.objectStore(storeName).delete(key);
303 } catch (cause) {
304 resume(Effect.fail(catalogError("remove", cause)));
305 }
306 }).pipe(Effect.withSpan("web.connectionStorage.removeDatabaseValue"));
308​
309function removeDatabaseValuesInRangeOnConnection(
310 database: IDBDatabase,
311 storeName: string,
312 range: IDBKeyRange,
313) {
314 return Effect.callback<void, ConnectionTransientError>((resume) => {
315 try {
316 const transaction = database.transaction(storeName, "readwrite");
317 transaction.addEventListener("abort", () => {
318 resume(
319 Effect.fail(
320 catalogError("remove", transaction.error ?? "Unknown IndexedDB cursor error"),
321 ),
322 );
323 });
324 transaction.addEventListener("complete", () => {
325 resume(Effect.void);
326 });
327 const request = transaction.objectStore(storeName).openCursor(range);
328 request.addEventListener("error", () => {
329 resume(
330 Effect.fail(catalogError("remove", request.error ?? "Unknown IndexedDB cursor error")),
331 );
332 });
333 request.addEventListener("success", () => {
334 const cursor = request.result;
335 if (cursor === null) {
336 return;
337 }
338 try {
339 cursor.delete();
340 cursor.continue();
341 } catch (cause) {
342 resume(Effect.fail(catalogError("remove", cause)));
343 }
344 });
345 } catch (cause) {
346 resume(Effect.fail(catalogError("remove", cause)));
347 }
348 }).pipe(Effect.withSpan("web.connectionStorage.removeDatabaseValuesInRange"));
350​
351// Mirrors mobile's pull-request cache bounds, per environment.
352function saveQueryOnConnection(
353 database: IDBDatabase,
354 environmentId: EnvironmentId,
355 key: string,
356 payload: string,
357 updatedAt: number,
358) {
359 return Effect.callback<void, ConnectionTransientError>((resume) => {
360 try {
361 const bytes = new TextEncoder().encode(payload).length;
362 const transaction = database.transaction(QUERY_STORE_NAME, "readwrite");
363 transaction.addEventListener("abort", () => {
364 resume(
365 Effect.fail(catalogError("write", transaction.error ?? "Unknown IndexedDB write error")),
366 );
367 });
368 transaction.addEventListener("complete", () => {
369 resume(Effect.void);
370 });
371 const store = transaction.objectStore(QUERY_STORE_NAME);
372 // An answer too large to keep must not leave its older copy to paint on the next start.
373 if (bytes > QUERY_MAX_PAYLOAD_BYTES) {
374 store.delete(queryCacheKey(environmentId, key));
375 return;
376 }
377 store.put({ environmentId, payload, updatedAt, bytes }, queryCacheKey(environmentId, key));
378 const batchKey = queryBatchKey(environmentId);
379 const batchRequest = store.get(batchKey);
380 batchRequest.addEventListener("success", () => {
381 const batch: unknown = batchRequest.result;
382 const rows = (isQueryBatch(batch) ? batch.rows : 0) + 1;
383 const written = (isQueryBatch(batch) ? batch.bytes : 0) + bytes;
384 if (
385 isQueryBatch(batch) &&
386 rows < QUERY_PRUNE_BATCH_ROWS &&
387 written < QUERY_PRUNE_BATCH_BYTES
388 ) {
389 store.put({ rows, bytes: written }, batchKey);
390 return;
391 }
392 store.put({ rows: 0, bytes: 0 }, batchKey);
393 const request = store
394 .index(QUERY_UPDATED_INDEX_NAME)
395 .openKeyCursor(IDBKeyRange.bound([environmentId], [environmentId, []]), "prev");
396 let kept = 0;
397 let totalBytes = 0;
398 request.addEventListener("success", () => {
399 const cursor = request.result;
400 if (cursor === null) return;
401 const [, rowUpdatedAt, rowBytes] = cursor.key as [string, number, number];
402 kept += 1;
403 totalBytes += rowBytes;
404 if (
405 kept > QUERY_MAX_ROWS - QUERY_PRUNE_BATCH_ROWS ||
406 totalBytes > QUERY_MAX_BYTES - QUERY_PRUNE_BATCH_BYTES ||
407 rowUpdatedAt < updatedAt - QUERY_MAX_AGE_MS
408 ) {
409 store.delete(cursor.primaryKey);
410 }
411 cursor.continue();
412 });
413 });
414 } catch (cause) {
415 resume(Effect.fail(catalogError("write", cause)));
416 }
417 }).pipe(Effect.withSpan("web.connectionStorage.saveQuery"));
419​
420function readDatabaseValue(database: DatabaseHandle, storeName: string, key: IDBValidKey) {
421 return withDatabase(database, (opened) => readDatabaseValueOnConnection(opened, storeName, key));
423​
424function writeDatabaseValue(
425 database: DatabaseHandle,
426 storeName: string,
427 key: IDBValidKey,
428 value: unknown,
429) {
430 return withDatabase(database, (opened) =>
431 writeDatabaseValueOnConnection(opened, storeName, key, value),
432 );
434​
435function removeDatabaseValue(database: DatabaseHandle, storeName: string, key: IDBValidKey) {
436 return withDatabase(database, (opened) =>
437 removeDatabaseValueOnConnection(opened, storeName, key),
438 );
440​
441function removeDatabaseValuesInRange(
442 database: DatabaseHandle,
443 storeName: string,
444 range: IDBKeyRange,
445) {
446 return withDatabase(database, (opened) =>
447 removeDatabaseValuesInRangeOnConnection(opened, storeName, range),
448 );
450​
451function threadCacheKey(environmentId: EnvironmentId, threadId: ThreadId) {
452 return `${environmentId}:${threadId}`;
454​
455function queryCacheKey(environmentId: EnvironmentId, key: string) {
456 return `${environmentId}:v1:${key}`;
458​
459function queryBatchKey(environmentId: EnvironmentId) {
460 return `${environmentId}:batch`;
462​
463function vcsRefsCacheKey(environmentId: EnvironmentId, cwd: string) {
464 return `${environmentId}:${cwd}`;
466​
467const decodeCatalog = Effect.fn("web.connectionStorage.decodeCatalog")(function* (raw: string) {
468 return yield* decodeConnectionCatalogDocument(raw).pipe(
469 Effect.mapError((cause) => catalogError("decode", cause)),
470 );
471});
472​
473const encodeCatalog = Effect.fn("web.connectionStorage.encodeCatalog")(function* (
474 catalog: ConnectionCatalogDocumentType,
475) {
476 return yield* encodeConnectionCatalogDocument(catalog).pipe(
477 Effect.mapError((cause) => catalogError("encode", cause)),
478 );
479});
480​
481export interface CatalogBackend {
482 readonly read: Effect.Effect<string | null, ConnectionTransientError>;
483 readonly write: (raw: string) => Effect.Effect<void, ConnectionTransientError>;
484 readonly quarantine?: (raw: string) => Effect.Effect<void, ConnectionTransientError>;
486​
487export function makeCatalogBackend(database: DatabaseHandle): CatalogBackend {
488 const bridge = window.desktopBridge;
489 if (bridge !== undefined) {
490 return {
491 read: Effect.tryPromise({
492 try: () => bridge.getConnectionCatalog(),
493 catch: (cause) => catalogError("load", cause),
494 }),
495 write: (raw) =>
496 Effect.tryPromise({
497 try: () => bridge.setConnectionCatalog(raw),
498 catch: (cause) => catalogError("save", cause),
499 }).pipe(
500 Effect.flatMap((stored) =>
501 stored
502 ? Effect.void
503 : Effect.fail(
504 catalogError(
505 "save",
506 "Desktop secure storage is unavailable in this system context.",
507 ),
508 ),
509 ),
510 ),
511 };
512 }
513​
514 return {
515 read: readDatabaseValue(database, CATALOG_STORE_NAME, CATALOG_KEY).pipe(
516 Effect.map((value) => (typeof value === "string" ? value : null)),
517 ),
518 write: (raw) => writeDatabaseValue(database, CATALOG_STORE_NAME, CATALOG_KEY, raw),
519 quarantine: (raw) =>
520 writeDatabaseValue(database, CATALOG_STORE_NAME, `${CATALOG_KEY}:corrupt:${Date.now()}`, raw),
521 };
523​
524interface CatalogStore {
525 readonly read: Effect.Effect<ConnectionCatalogDocumentType, ConnectionTransientError>;
526 readonly update: (
527 transform: (catalog: ConnectionCatalogDocumentType) => ConnectionCatalogDocumentType,
528 ) => Effect.Effect<void, ConnectionTransientError>;
530​
531export const makeCatalogStore = Effect.fn("web.connectionStorage.makeCatalogStore")(function* (
532 backend: CatalogBackend,
533) {
534 const state = yield* Ref.make<Option.Option<ConnectionCatalogDocumentType>>(Option.none());
535 const lock = yield* Semaphore.make(1);
536​
537 const loadUnlocked = Effect.fn("web.connectionStorage.loadCatalog")(function* () {
538 const cached = yield* Ref.get(state);
539 if (Option.isSome(cached)) {
540 return cached.value;
541 }
542 const raw = yield* backend.read;
543 let catalog = EMPTY_CONNECTION_CATALOG_DOCUMENT;
544 if (raw !== null && raw.trim() !== "") {
545 catalog = yield* decodeCatalog(raw).pipe(
546 Effect.catch((error) =>
547 Effect.gen(function* () {
548 yield* Effect.logWarning("Discarding a corrupt web connection catalog.", {
549 error: error.message,
550 });
551 if (backend.quarantine !== undefined) {
552 yield* backend.quarantine(raw).pipe(
553 Effect.catch((cause) =>
554 Effect.logWarning("Could not quarantine the corrupt web connection catalog.", {
555 error: cause.message,
556 }),
557 ),
558 );
559 }
560 const encoded = yield* encodeCatalog(EMPTY_CONNECTION_CATALOG_DOCUMENT);
561 yield* backend.write(encoded).pipe(
562 Effect.catch((cause) =>
563 Effect.logWarning("Could not persist the recovered web connection catalog.", {
564 error: cause.message,
565 }),
566 ),
567 );
568 return EMPTY_CONNECTION_CATALOG_DOCUMENT;
569 }),
570 ),
571 );
572 }
573 yield* Ref.set(state, Option.some(catalog));
574 return catalog;
575 });
576​
577 const read = lock.withPermits(1)(loadUnlocked());
578 const update: CatalogStore["update"] = Effect.fn("web.connectionStorage.updateCatalog")(
579 function* (transform) {
580 yield* lock.withPermits(1)(
581 Effect.gen(function* () {
582 const next = transform(yield* loadUnlocked());
583 yield* backend.write(yield* encodeCatalog(next));
584 yield* Ref.set(state, Option.some(next));
585 }),
586 );
587 },
588 );
589​
590 return { read, update } satisfies CatalogStore;
591});
592​
593// The stored names predate the rename and stay, so a tab still on an older version and an
594// updated one read and revoke the same grants.
595const PULL_REQUEST_ROUTING_KEY_PREFIX = "t3code:github-routing:";
596const PULL_REQUEST_ROUTING_CHANGED = "t3code:github-routing-changed";
597const isStoredPullRequestRoutingPermission = Schema.is(StoredPullRequestRoutingPermission);
598const encodeStoredPullRequestRoutingPermission = Schema.encodeSync(
599 Schema.fromJsonString(StoredPullRequestRoutingPermission),
600);
601​
602/** Each grant has its own key so stale tabs and unrelated catalog saves cannot restore trust. */
603export function makeBrowserPullRequestRoutingPermissions(
604 browser: Pick<Window, "localStorage"> & EventTarget = window,
605) {
606 const read = (key: string): StoredPullRequestRoutingPermission | null => {
607 try {
608 const raw = browser.localStorage.getItem(key);
609 const value: unknown = raw === null ? null : JSON.parse(raw);
610 return isStoredPullRequestRoutingPermission(value) &&
611 key === `${PULL_REQUEST_ROUTING_KEY_PREFIX}${value.environmentId}`
612 ? value
613 : null;
614 } catch {
615 return null;
616 }
617 };
618 const readAll = (): ReadonlyArray<StoredPullRequestRoutingPermission> => {
619 try {
620 const values: StoredPullRequestRoutingPermission[] = [];
621 const storage = browser.localStorage;
622 for (let index = 0; index < storage.length; index++) {
623 const key = storage.key(index);
624 if (key?.startsWith(PULL_REQUEST_ROUTING_KEY_PREFIX)) {
625 const value = read(key);
626 if (value !== null) values.push(value);
627 }
628 }
629 return values;
630 } catch {
631 return [];
632 }
633 };
634 const write = (environmentId: EnvironmentId, value: StoredPullRequestRoutingPermission | null) =>
635 Effect.try({
636 try: () => {
637 const key = `${PULL_REQUEST_ROUTING_KEY_PREFIX}${environmentId}`;
638 if (value === null) browser.localStorage.removeItem(key);
639 else browser.localStorage.setItem(key, encodeStoredPullRequestRoutingPermission(value));
640 browser.dispatchEvent(new Event(PULL_REQUEST_ROUTING_CHANGED));
641 },
642 catch: (cause) => catalogError("save pull request routing permissions in", cause),
643 });
644 return PullRequestRoutingPermissions.of({
645 get: Effect.fn("PullRequestRoutingPermissions.get")(function (entry) {
646 return Effect.sync(() => {
647 const value = read(`${PULL_REQUEST_ROUTING_KEY_PREFIX}${entry.target.environmentId}`);
648 return pullRequestRoutingPermissionFor(entry, value === null ? [] : [value]);
649 });
650 }),
651 changes: Stream.callback<ReadonlyArray<StoredPullRequestRoutingPermission>>((queue) =>
652 Effect.acquireRelease(
653 Effect.sync(() => {
654 const listener = (event: Event) => {
655 if (event.type === "storage") {
656 const key = (event as StorageEvent).key;
657 if (key !== null && !key?.startsWith(PULL_REQUEST_ROUTING_KEY_PREFIX)) return;
658 }
659 Queue.offerUnsafe(queue, readAll());
660 };
661 browser.addEventListener("storage", listener);
662 browser.addEventListener(PULL_REQUEST_ROUTING_CHANGED, listener);
663 Queue.offerUnsafe(queue, readAll());
664 return listener;
665 }),
666 (listener) =>
667 Effect.sync(() => {
668 browser.removeEventListener("storage", listener);
669 browser.removeEventListener(PULL_REQUEST_ROUTING_CHANGED, listener);
670 }),
671 ).pipe(Effect.asVoid),
672 ),
673 set: Effect.fn("PullRequestRoutingPermissions.set")(function* (entry, permission) {
674 const connectionKey = pullRequestRoutingConnectionKey(entry);
675 if (connectionKey === null)
676 return yield* new ConnectionBlockedError({
677 reason: "configuration",
678 detail: "This environment does not have a saved connection endpoint.",
679 });
680 return yield* write(
681 entry.target.environmentId,
682 permission === "off"
683 ? null
684 : {
685 environmentId: entry.target.environmentId,
686 connectionKey,
687 permission,
688 },
689 );
690 }),
691 forget: Effect.fn("PullRequestRoutingPermissions.forget")(function (environmentId) {
692 return write(environmentId, null);
693 }),
694 });
696​
697// MessageChannel yields without nested-timer clamping or background throttling.
698const yieldToBrowserTask = Effect.callback<void>((resume) => {
699 const { port1, port2 } = new MessageChannel();
700 port1.addEventListener(
701 "message",
702 () => {
703 port1.close();
704 resume(Effect.void);
705 },
706 { once: true },
707 );
708 port1.start();
709 port2.postMessage(null);
710 // A closed port delivers nothing, so interruption needs no listener bookkeeping.
711 return Effect.sync(() => port1.close());
712});
713​
714export const layer = Layer.effectContext(
715 Effect.gen(function* () {
716 const { handle: database } = yield* Effect.acquireRelease(
717 makeDatabaseHandle(),
718 (owned) => owned.close,
719 );
720 const catalog = yield* makeCatalogStore(makeCatalogBackend(database));
721 const pullRequestRoutingPermissions = makeBrowserPullRequestRoutingPermissions();
722 // Remembers each thread row's encoding, so a save only encodes rows that changed.
723 const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder({
724 yieldToHost: yieldToBrowserTask,
725 });
726 const encodeStoredThreadSnapshot = makeStoredThreadSnapshotEncoder({
727 format: "full",
728 yieldToHost: yieldToBrowserTask,
729 });
730 const threadWriteOrder = makeThreadCacheWriteOrder();
731​
732 const targetStore = Persistence.ConnectionTargetStore.of({
733 list: catalog.read.pipe(
734 Effect.map((document) => document.targets),
735 Effect.mapError((cause) => persistenceError("list-targets", cause)),
736 ),
737 listDisabled: catalog.read.pipe(
738 Effect.map((document) => document.disabledEnvironmentIds),
739 Effect.mapError((cause) => persistenceError("list-disabled-targets", cause)),
740 ),
741 });
742 const registrationStore = Persistence.ConnectionRegistrationStore.of({
743 register: Effect.fn("Persistence.ConnectionRegistrationStore.register")(
744 function (registration, routes) {
745 return catalog
746 .update((document) => registerConnectionInCatalog(document, registration, routes))
747 .pipe(Effect.mapError((cause) => persistenceError("register-connection", cause)));
748 },
749 ),
750 setRoutes: Effect.fn("Persistence.ConnectionRegistrationStore.setRoutes")(
751 function (environmentId, routes) {
752 return catalog
753 .update((document) => setRoutesInCatalog(document, environmentId, routes))
754 .pipe(Effect.mapError((cause) => persistenceError("set-connection-routes", cause)));
755 },
756 ),
757 remove: Effect.fn("Persistence.ConnectionRegistrationStore.remove")(function (environmentId) {
758 return catalog
759 .update((document) => removeConnectionFromCatalog(document, environmentId))
760 .pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause)));
761 }),
762 setEnabled: Effect.fn("Persistence.ConnectionRegistrationStore.setEnabled")(
763 function (environmentId, enabled) {
764 return catalog
765 .update((document) => setConnectionEnabledInCatalog(document, environmentId, enabled))
766 .pipe(Effect.mapError((cause) => persistenceError("set-connection-enabled", cause)));
767 },
768 ),
769 });
770 const profileStore = ProfileStore.make({
771 get: (connectionId) =>
772 catalog.read.pipe(
773 Effect.map((document) =>
774 Option.fromUndefinedOr(
775 document.profiles.find((profile) => profile.connectionId === connectionId),
776 ),
777 ),
778 ),
779 put: (profile) =>
780 catalog.update((document) => ({
781 ...document,
782 profiles: replaceCatalogValue(document.profiles, (value) => value.connectionId, profile),
783 })),
784 remove: (connectionId) =>
785 catalog.update((document) => ({
786 ...document,
787 profiles: removeCatalogValue(
788 document.profiles,
789 (value) => value.connectionId,
790 connectionId,
791 ),
792 })),
793 });
794 const credentialStore = CredentialStore.make({
795 get: (connectionId) =>
796 catalog.read.pipe(
797 Effect.map((document) =>
798 Option.fromUndefinedOr(
799 document.credentials.find((entry) => entry.connectionId === connectionId)?.credential,
800 ),
801 ),
802 ),
803 put: (connectionId, credential) =>
804 catalog.update((document) => ({
805 ...document,
806 credentials: replaceCatalogValue(document.credentials, (value) => value.connectionId, {
807 connectionId,
808 credential,
809 }),
810 })),
811 remove: (connectionId) =>
812 catalog.update((document) => ({
813 ...document,
814 credentials: removeCatalogValue(
815 document.credentials,
816 (value) => value.connectionId,
817 connectionId,
818 ),
819 })),
820 });
821 const remoteTokenStore = TokenStore.make({
822 get: (environmentId) =>
823 catalog.read.pipe(
824 Effect.map((document) =>
825 Option.fromUndefinedOr(
826 document.remoteDpopTokens.find((token) => token.environmentId === environmentId),
827 ),
828 ),
829 ),
830 put: (token) => catalog.update((document) => putRemoteDpopTokenInCatalog(document, token)),
831 remove: (environmentId) =>
832 catalog.update((document) => ({
833 ...document,
834 remoteDpopTokens: removeCatalogValue(
835 document.remoteDpopTokens,
836 (value) => value.environmentId,
837 environmentId,
838 ),
839 })),
840 });
841 const cacheStore = Persistence.EnvironmentCacheStore.of({
842 loadShell: Effect.fn("Persistence.EnvironmentCacheStore.loadShell")(function (environmentId) {
843 return readDatabaseValue(database, SHELL_STORE_NAME, environmentId).pipe(
844 Effect.tap(() => Effect.promise(() => projectFaviconCache.hydrate())),
845 Effect.flatMap((raw) => {
846 if (typeof raw !== "string") {
847 return Effect.succeedNone;
848 }
849 return decodeOrDiscardOrchestrationCache(
850 decodeStoredShellSnapshot(raw).pipe(
851 Effect.mapError((cause) => persistenceError("load-shell", cause)),
852 Effect.map((stored) =>
853 stored.environmentId === environmentId
854 ? Option.some(
855 stored.origin === undefined
856 ? stored.snapshot
857 : { ...stored.snapshot, origin: stored.origin },
858 )
859 : Option.none(),
860 ),
861 ),
862 removeDatabaseValue(database, SHELL_STORE_NAME, environmentId),
863 );
864 }),
865 Effect.mapError((cause) => persistenceError("load-shell", cause)),
866 );
867 }),
868 saveShell: Effect.fn("storage.saveShell")(
869 function* (environmentId, snapshot, origin) {
870 const encoded = yield* encodeStoredShellSnapshot({
871 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
872 environmentId,
873 snapshot,
874 ...(origin === undefined ? {} : { origin }),
875 }).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
876 yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
877 },
878 Effect.mapError((cause) =>
879 cause._tag === "ConnectionPersistenceError"
880 ? cause
881 : persistenceError("save-shell", cause),
882 ),
883 ),
884 loadServerConfig: Effect.fn("Persistence.EnvironmentCacheStore.loadServerConfig")(
885 function (environmentId) {
886 return readDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId).pipe(
887 Effect.flatMap((raw) => {
888 if (typeof raw !== "string") {
889 return Effect.succeedNone;
890 }
891 return decodeStoredServerConfig(raw).pipe(
892 Effect.mapError((cause) => persistenceError("load-server-config", cause)),
893 Effect.map((stored) =>
894 stored.environmentId === environmentId
895 ? Option.some(stored.config)
896 : Option.none(),
897 ),
898 );
899 }),
900 Effect.mapError((cause) =>
901 cause._tag === "ConnectionPersistenceError"
902 ? cause
903 : persistenceError("load-server-config", cause),
904 ),
905 );
906 },
907 ),
908 saveServerConfig: Effect.fn("storage.saveServerConfig")(
909 function* (environmentId, config) {
910 const encoded = yield* encodeStoredServerConfig({
911 schemaVersion: 1,
912 environmentId,
913 config,
914 }).pipe(Effect.mapError((cause) => persistenceError("save-server-config", cause)));
915 yield* writeDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId, encoded);
916 },
917 Effect.mapError((cause) =>
918 cause._tag === "ConnectionPersistenceError"
919 ? cause
920 : persistenceError("save-server-config", cause),
921 ),
922 ),
923 loadThread: Effect.fn("Persistence.EnvironmentCacheStore.loadThread")(
924 function (environmentId, threadId) {
925 return readDatabaseValue(
926 database,
927 THREAD_STORE_NAME,
928 threadCacheKey(environmentId, threadId),
929 ).pipe(
930 Effect.flatMap((raw) => {
931 if (typeof raw !== "string") {
932 return Effect.succeedNone;
933 }
934 return decodeOrDiscardOrchestrationCache(
935 decodeStoredThreadSnapshot(raw).pipe(
936 Effect.mapError((cause) => persistenceError("load-thread", cause)),
937 Effect.map((stored) =>
938 stored.environmentId === environmentId && stored.threadId === threadId
939 ? Option.some(stored.snapshot)
940 : Option.none(),
941 ),
942 ),
943 removeDatabaseValue(
944 database,
945 THREAD_STORE_NAME,
946 threadCacheKey(environmentId, threadId),
947 ),
948 );
949 }),
950 Effect.mapError((cause) => persistenceError("load-thread", cause)),
951 );
952 },
953 ),
954 saveThread: Effect.fn("storage.saveThread")(
955 function* (environmentId, snapshot) {
956 const key = threadCacheKey(environmentId, snapshot.projection.thread.id);
957 const isLatest = threadWriteOrder.beginSave(environmentId, snapshot.projection.thread.id);
958 const encoded = yield* encodeStoredThreadSnapshot({
959 environmentId,
960 threadId: snapshot.projection.thread.id,
961 snapshot,
962 }).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
963 if (!isLatest()) return;
964 // An older copy of a thread now too large to keep would paint stale on reopen.
965 yield* Option.match(encoded, {
966 onNone: () => removeDatabaseValue(database, THREAD_STORE_NAME, key),
967 onSome: (payload) => writeDatabaseValue(database, THREAD_STORE_NAME, key, payload),
968 });
969 },
970 Effect.mapError((cause) =>
971 cause._tag === "ConnectionPersistenceError"
972 ? cause
973 : persistenceError("save-thread", cause),
974 ),
975 ),
976 loadVcsRefs: Effect.fn("Persistence.EnvironmentCacheStore.loadVcsRefs")(
977 function (environmentId, cwd) {
978 return readDatabaseValue(
979 database,
980 VCS_REFS_STORE_NAME,
981 vcsRefsCacheKey(environmentId, cwd),
982 ).pipe(
983 Effect.flatMap((raw) => {
984 if (typeof raw !== "string") {
985 return Effect.succeedNone;
986 }
987 return decodeStoredVcsRefs(raw).pipe(
988 Effect.mapError((cause) => persistenceError("load-vcs-refs", cause)),
989 Effect.map((stored) =>
990 stored.environmentId === environmentId && stored.cwd === cwd
991 ? Option.some(stored.refs)
992 : Option.none(),
993 ),
994 );
995 }),
996 Effect.mapError((cause) =>
997 cause._tag === "ConnectionPersistenceError"
998 ? cause
999 : persistenceError("load-vcs-refs", cause),
1000 ),
1001 );
1002 },
1003 ),
1004 saveVcsRefs: Effect.fn("storage.saveVcsRefs")(
1005 function* (environmentId, cwd, refs) {
1006 const encoded = yield* encodeStoredVcsRefs({
1007 schemaVersion: 1,
1008 environmentId,
1009 cwd,
1010 refs,
1011 }).pipe(Effect.mapError((cause) => persistenceError("save-vcs-refs", cause)));
1012 yield* writeDatabaseValue(
1013 database,
1014 VCS_REFS_STORE_NAME,
1015 vcsRefsCacheKey(environmentId, cwd),
1016 encoded,
1017 );
1018 },
1019 Effect.mapError((cause) =>
1020 cause._tag === "ConnectionPersistenceError"
1021 ? cause
1022 : persistenceError("save-vcs-refs", cause),
1023 ),
1024 ),
1025 removeVcsRefs: Effect.fn("Persistence.EnvironmentCacheStore.removeVcsRefs")(
1026 function (environmentId, cwd) {
1027 return removeDatabaseValue(
1028 database,
1029 VCS_REFS_STORE_NAME,
1030 vcsRefsCacheKey(environmentId, cwd),
1031 ).pipe(Effect.mapError((cause) => persistenceError("remove-vcs-refs", cause)));
1032 },
1033 ),
1034 clearVcsRefs: Effect.fn("Persistence.EnvironmentCacheStore.clearVcsRefs")(
1035 function (environmentId) {
1036 return removeDatabaseValuesInRange(
1037 database,
1038 VCS_REFS_STORE_NAME,
1039 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1040 ).pipe(Effect.mapError((cause) => persistenceError("clear-vcs-refs", cause)));
1041 },
1042 ),
1043 removeThread: Effect.fn("Persistence.EnvironmentCacheStore.removeThread")(
1044 function (environmentId, threadId) {
1045 threadWriteOrder.removeThread(environmentId, threadId);
1046 return removeDatabaseValue(
1047 database,
1048 THREAD_STORE_NAME,
1049 threadCacheKey(environmentId, threadId),
1050 ).pipe(Effect.mapError((cause) => persistenceError("remove-thread", cause)));
1051 },
1052 ),
1053 clear: Effect.fn("Persistence.EnvironmentCacheStore.clear")(function (environmentId) {
1054 threadWriteOrder.clearEnvironment(environmentId);
1055 return Effect.all(
1057 Effect.promise(() => projectFaviconCache.clearEnvironment(environmentId)),
1058 removeDatabaseValue(database, SHELL_STORE_NAME, environmentId),
1059 removeDatabaseValuesInRange(
1060 database,
1061 THREAD_STORE_NAME,
1062 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1063 ),
1064 removeDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId),
1065 removeDatabaseValuesInRange(
1066 database,
1067 VCS_REFS_STORE_NAME,
1068 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1069 ),
1070 removeDatabaseValuesInRange(
1071 database,
1072 QUERY_STORE_NAME,
1073 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1074 ),
1075 ],
1076 { concurrency: "unbounded", discard: true },
1077 ).pipe(Effect.mapError((cause) => persistenceError("clear-environment", cause)));
1078 }),
1079 });
1080​
1081 const queryStore: NonNullable<typeof EnvironmentQueryStore.Service> = {
1082 load: Effect.fn("EnvironmentQueryStore.load")(
1083 function* (environmentId, key) {
1084 const stored = yield* readDatabaseValue(
1085 database,
1086 QUERY_STORE_NAME,
1087 queryCacheKey(environmentId, key),
1088 );
1089 const now = yield* Clock.currentTimeMillis;
1090 return isStoredQuery(stored) &&
1091 stored.environmentId === environmentId &&
1092 stored.updatedAt >= now - QUERY_MAX_AGE_MS
1093 ? Option.some(stored.payload)
1094 : Option.none();
1095 },
1096 Effect.catch(() => Effect.succeedNone),
1097 ),
1098 save: (environmentId, key, payload) =>
1099 Clock.currentTimeMillis.pipe(
1100 Effect.flatMap((now) =>
1101 withDatabase(database, (opened) =>
1102 saveQueryOnConnection(opened, environmentId, key, payload, now),
1103 ),
1104 ),
1105 Effect.catch(() => Effect.logWarning("Offline query database save failed")),
1106 ),
1107 };
1108​
1109 return Context.make(Persistence.ConnectionTargetStore, targetStore).pipe(
1110 Context.add(PullRequestRoutingPermissions, pullRequestRoutingPermissions),
1111 Context.add(Persistence.ConnectionRegistrationStore, registrationStore),
1112 Context.add(ProfileStore.ConnectionProfileStore, profileStore),
1113 Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
1114 Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
1115 Context.add(Persistence.EnvironmentCacheStore, cacheStore),
1116 Context.add(EnvironmentQueryStore, queryStore),
1117 );
1118 }),