Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b444300af0 |
No files matched your search
@@ -62,6 +62,22 @@ function catalogError(operation: string, cause: unknown) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shellPersistenceError(
|
||||||
|
operation:
|
||||||
|
| "load-shell"
|
||||||
|
| "save-shell"
|
||||||
|
| "load-thread"
|
||||||
|
| "save-thread"
|
||||||
|
| "remove-thread"
|
||||||
|
| "clear-environment",
|
||||||
|
cause: unknown,
|
||||||
|
) {
|
||||||
|
return new ConnectionPersistenceError({
|
||||||
|
operation,
|
||||||
|
message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function threadSnapshotFileName(threadId: ThreadId): string {
|
function threadSnapshotFileName(threadId: ThreadId): string {
|
||||||
return `${encodeURIComponent(threadId)}.json`;
|
return `${encodeURIComponent(threadId)}.json`;
|
||||||
}
|
}
|
||||||
@@ -84,7 +100,7 @@ const threadSnapshotDirectory = Effect.fn("mobile.connectionStorage.threadSnapsh
|
|||||||
}
|
}
|
||||||
return directory;
|
return directory;
|
||||||
},
|
},
|
||||||
catch: (cause) => new ConnectionPersistenceError({ operation, environmentId, cause }),
|
catch: (cause) => shellPersistenceError(operation, cause),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -101,6 +117,16 @@ const threadSnapshotFile = Effect.fn("mobile.connectionStorage.threadSnapshotFil
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function targetPersistenceError(
|
||||||
|
operation: "list-targets" | "register-connection" | "remove-connection",
|
||||||
|
error: ConnectionTransientError,
|
||||||
|
) {
|
||||||
|
return new ConnectionPersistenceError({
|
||||||
|
operation,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const secureCatalogStorage: SecureCatalogStorage = {
|
const secureCatalogStorage: SecureCatalogStorage = {
|
||||||
getItem: (key) =>
|
getItem: (key) =>
|
||||||
Effect.tryPromise({
|
Effect.tryPromise({
|
||||||
@@ -137,7 +163,7 @@ const shellSnapshotFileInDirectory = Effect.fn(
|
|||||||
directory.create({ idempotent: true, intermediates: true });
|
directory.create({ idempotent: true, intermediates: true });
|
||||||
return new File(directory, shellSnapshotFileName(environmentId));
|
return new File(directory, shellSnapshotFileName(environmentId));
|
||||||
},
|
},
|
||||||
catch: (cause) => new ConnectionPersistenceError({ operation, environmentId, cause }),
|
catch: (cause) => shellPersistenceError(operation, cause),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -158,38 +184,18 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
const targetStore = ConnectionTargetStore.of({
|
const targetStore = ConnectionTargetStore.of({
|
||||||
list: catalog.read.pipe(
|
list: catalog.read.pipe(
|
||||||
Effect.map((document) => document.targets),
|
Effect.map((document) => document.targets),
|
||||||
Effect.mapError(
|
Effect.mapError((error) => targetPersistenceError("list-targets", error)),
|
||||||
(cause) => new ConnectionPersistenceError({ operation: "list-targets", cause }),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
const registrationStore = ConnectionRegistrationStore.of({
|
const registrationStore = ConnectionRegistrationStore.of({
|
||||||
register: (registration) =>
|
register: (registration) =>
|
||||||
catalog
|
catalog
|
||||||
.update((document) => registerConnectionInCatalog(document, registration))
|
.update((document) => registerConnectionInCatalog(document, registration))
|
||||||
.pipe(
|
.pipe(Effect.mapError((error) => targetPersistenceError("register-connection", error))),
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "register-connection",
|
|
||||||
environmentId: registration.target.environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
remove: (target) =>
|
remove: (target) =>
|
||||||
catalog
|
catalog
|
||||||
.update((document) => removeConnectionFromCatalog(document, target))
|
.update((document) => removeConnectionFromCatalog(document, target))
|
||||||
.pipe(
|
.pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))),
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "remove-connection",
|
|
||||||
environmentId: target.environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
const profileStore = ProfileStore.make({
|
const profileStore = ProfileStore.make({
|
||||||
get: (connectionId) =>
|
get: (connectionId) =>
|
||||||
@@ -277,34 +283,15 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
if (file.exists) {
|
if (file.exists) {
|
||||||
const raw = yield* Effect.tryPromise({
|
const raw = yield* Effect.tryPromise({
|
||||||
try: () => file.text(),
|
try: () => file.text(),
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
const parsed = yield* Effect.try({
|
const parsed = yield* Effect.try({
|
||||||
try: () => JSON.parse(raw) as unknown,
|
try: () => JSON.parse(raw) as unknown,
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
const stored = yield* Effect.fromResult(
|
const stored = yield* Effect.fromResult(
|
||||||
Schema.decodeUnknownResult(StoredShellSnapshot)(parsed),
|
Schema.decodeUnknownResult(StoredShellSnapshot)(parsed),
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => shellPersistenceError("load-shell", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return stored.environmentId === environmentId
|
return stored.environmentId === environmentId
|
||||||
? Option.some(stored.snapshot)
|
? Option.some(stored.snapshot)
|
||||||
: Option.none();
|
: Option.none();
|
||||||
@@ -316,34 +303,15 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
}
|
}
|
||||||
const legacyRaw = yield* Effect.tryPromise({
|
const legacyRaw = yield* Effect.tryPromise({
|
||||||
try: () => legacyFile.text(),
|
try: () => legacyFile.text(),
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
const legacyParsed = yield* Effect.try({
|
const legacyParsed = yield* Effect.try({
|
||||||
try: () => JSON.parse(legacyRaw) as unknown,
|
try: () => JSON.parse(legacyRaw) as unknown,
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
const legacyStored = yield* Effect.fromResult(
|
const legacyStored = yield* Effect.fromResult(
|
||||||
Schema.decodeUnknownResult(LegacyStoredShellSnapshot)(legacyParsed),
|
Schema.decodeUnknownResult(LegacyStoredShellSnapshot)(legacyParsed),
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => shellPersistenceError("load-shell", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return legacyStored.environmentId === environmentId
|
return legacyStored.environmentId === environmentId
|
||||||
? Option.some(legacyStored.snapshot)
|
? Option.some(legacyStored.snapshot)
|
||||||
: Option.none();
|
: Option.none();
|
||||||
@@ -358,16 +326,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
} as const;
|
} as const;
|
||||||
const encoded = yield* Effect.fromResult(
|
const encoded = yield* Effect.fromResult(
|
||||||
Schema.encodeUnknownResult(StoredShellSnapshot)(stored),
|
Schema.encodeUnknownResult(StoredShellSnapshot)(stored),
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => shellPersistenceError("save-shell", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "save-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
yield* Effect.try({
|
yield* Effect.try({
|
||||||
try: () => {
|
try: () => {
|
||||||
if (!file.exists) {
|
if (!file.exists) {
|
||||||
@@ -375,12 +334,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
}
|
}
|
||||||
file.write(JSON.stringify(encoded));
|
file.write(JSON.stringify(encoded));
|
||||||
},
|
},
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("save-shell", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "save-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
loadThread: (environmentId, threadId) =>
|
loadThread: (environmentId, threadId) =>
|
||||||
@@ -391,37 +345,15 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
}
|
}
|
||||||
const raw = yield* Effect.tryPromise({
|
const raw = yield* Effect.tryPromise({
|
||||||
try: () => file.text(),
|
try: () => file.text(),
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("load-thread", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
const parsed = yield* Effect.try({
|
const parsed = yield* Effect.try({
|
||||||
try: () => JSON.parse(raw) as unknown,
|
try: () => JSON.parse(raw) as unknown,
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("load-thread", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
const stored = yield* Effect.fromResult(
|
const stored = yield* Effect.fromResult(
|
||||||
Schema.decodeUnknownResult(StoredThreadSnapshot)(parsed),
|
Schema.decodeUnknownResult(StoredThreadSnapshot)(parsed),
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => shellPersistenceError("load-thread", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return stored.environmentId === environmentId && stored.threadId === threadId
|
return stored.environmentId === environmentId && stored.threadId === threadId
|
||||||
? Option.some(stored.thread)
|
? Option.some(stored.thread)
|
||||||
: Option.none();
|
: Option.none();
|
||||||
@@ -436,17 +368,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
threadId: thread.id,
|
threadId: thread.id,
|
||||||
thread,
|
thread,
|
||||||
}),
|
}),
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => shellPersistenceError("save-thread", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "save-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId: thread.id,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
yield* Effect.try({
|
yield* Effect.try({
|
||||||
try: () => {
|
try: () => {
|
||||||
if (!file.exists) {
|
if (!file.exists) {
|
||||||
@@ -454,55 +376,36 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
}
|
}
|
||||||
file.write(JSON.stringify(encoded));
|
file.write(JSON.stringify(encoded));
|
||||||
},
|
},
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("save-thread", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "save-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId: thread.id,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
removeThread: (environmentId, threadId) =>
|
removeThread: (environmentId, threadId) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const file = yield* threadSnapshotFile(environmentId, threadId, "remove-thread");
|
const file = yield* threadSnapshotFile(environmentId, threadId, "remove-thread");
|
||||||
if (file.exists) {
|
if (file.exists) {
|
||||||
yield* Effect.try({
|
file.delete();
|
||||||
try: () => file.delete(),
|
|
||||||
catch: (cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "remove-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}),
|
}).pipe(
|
||||||
|
Effect.mapError((cause) =>
|
||||||
|
cause._tag === "ConnectionPersistenceError"
|
||||||
|
? cause
|
||||||
|
: shellPersistenceError("remove-thread", cause),
|
||||||
|
),
|
||||||
|
),
|
||||||
clear: (environmentId) =>
|
clear: (environmentId) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const file = yield* shellSnapshotFile(environmentId, "clear-environment");
|
const file = yield* shellSnapshotFile(environmentId, "clear-environment");
|
||||||
if (file.exists) {
|
if (file.exists) {
|
||||||
yield* Effect.try({
|
yield* Effect.try({
|
||||||
try: () => file.delete(),
|
try: () => file.delete(),
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("clear-environment", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "clear-environment",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const legacyFile = yield* legacyShellSnapshotFile(environmentId, "clear-environment");
|
const legacyFile = yield* legacyShellSnapshotFile(environmentId, "clear-environment");
|
||||||
if (legacyFile.exists) {
|
if (legacyFile.exists) {
|
||||||
yield* Effect.try({
|
yield* Effect.try({
|
||||||
try: () => legacyFile.delete(),
|
try: () => legacyFile.delete(),
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("clear-environment", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "clear-environment",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const threadDirectory = yield* threadSnapshotDirectory(
|
const threadDirectory = yield* threadSnapshotDirectory(
|
||||||
@@ -512,12 +415,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
if (threadDirectory.exists) {
|
if (threadDirectory.exists) {
|
||||||
yield* Effect.try({
|
yield* Effect.try({
|
||||||
try: () => threadDirectory.delete(),
|
try: () => threadDirectory.delete(),
|
||||||
catch: (cause) =>
|
catch: (cause) => shellPersistenceError("clear-environment", cause),
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "clear-environment",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -67,6 +67,25 @@ function catalogError(operation: string, cause: unknown) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function persistenceError(
|
||||||
|
operation:
|
||||||
|
| "list-targets"
|
||||||
|
| "register-connection"
|
||||||
|
| "remove-connection"
|
||||||
|
| "load-shell"
|
||||||
|
| "save-shell"
|
||||||
|
| "load-thread"
|
||||||
|
| "save-thread"
|
||||||
|
| "remove-thread"
|
||||||
|
| "clear-environment",
|
||||||
|
cause: unknown,
|
||||||
|
) {
|
||||||
|
return new ConnectionPersistenceError({
|
||||||
|
operation,
|
||||||
|
message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const openDatabase = Effect.fn("web.connectionStorage.openDatabase")(function* () {
|
const openDatabase = Effect.fn("web.connectionStorage.openDatabase")(function* () {
|
||||||
return yield* Effect.callback<IDBDatabase, ConnectionTransientError>((resume) => {
|
return yield* Effect.callback<IDBDatabase, ConnectionTransientError>((resume) => {
|
||||||
if (typeof indexedDB === "undefined") {
|
if (typeof indexedDB === "undefined") {
|
||||||
@@ -311,38 +330,18 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
const targetStore = ConnectionTargetStore.of({
|
const targetStore = ConnectionTargetStore.of({
|
||||||
list: catalog.read.pipe(
|
list: catalog.read.pipe(
|
||||||
Effect.map((document) => document.targets),
|
Effect.map((document) => document.targets),
|
||||||
Effect.mapError(
|
Effect.mapError((cause) => persistenceError("list-targets", cause)),
|
||||||
(cause) => new ConnectionPersistenceError({ operation: "list-targets", cause }),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
const registrationStore = ConnectionRegistrationStore.of({
|
const registrationStore = ConnectionRegistrationStore.of({
|
||||||
register: (registration) =>
|
register: (registration) =>
|
||||||
catalog
|
catalog
|
||||||
.update((document) => registerConnectionInCatalog(document, registration))
|
.update((document) => registerConnectionInCatalog(document, registration))
|
||||||
.pipe(
|
.pipe(Effect.mapError((cause) => persistenceError("register-connection", cause))),
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "register-connection",
|
|
||||||
environmentId: registration.target.environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
remove: (target) =>
|
remove: (target) =>
|
||||||
catalog
|
catalog
|
||||||
.update((document) => removeConnectionFromCatalog(document, target))
|
.update((document) => removeConnectionFromCatalog(document, target))
|
||||||
.pipe(
|
.pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause))),
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "remove-connection",
|
|
||||||
environmentId: target.environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
const profileStore = ProfileStore.make({
|
const profileStore = ProfileStore.make({
|
||||||
get: (connectionId) =>
|
get: (connectionId) =>
|
||||||
@@ -431,14 +430,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
return Effect.succeed(Option.none());
|
return Effect.succeed(Option.none());
|
||||||
}
|
}
|
||||||
return decodeStoredShellSnapshot(raw).pipe(
|
return decodeStoredShellSnapshot(raw).pipe(
|
||||||
Effect.mapError(
|
Effect.mapError((cause) => persistenceError("load-shell", cause)),
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
Effect.map((stored) =>
|
Effect.map((stored) =>
|
||||||
stored.environmentId === environmentId
|
stored.environmentId === environmentId
|
||||||
? Option.some(stored.snapshot)
|
? Option.some(stored.snapshot)
|
||||||
@@ -449,11 +441,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
Effect.mapError((cause) =>
|
Effect.mapError((cause) =>
|
||||||
cause._tag === "ConnectionPersistenceError"
|
cause._tag === "ConnectionPersistenceError"
|
||||||
? cause
|
? cause
|
||||||
: new ConnectionPersistenceError({
|
: persistenceError("load-shell", cause),
|
||||||
operation: "load-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
saveShell: (environmentId, snapshot) =>
|
saveShell: (environmentId, snapshot) =>
|
||||||
@@ -462,26 +450,13 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
|
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
|
||||||
environmentId,
|
environmentId,
|
||||||
snapshot,
|
snapshot,
|
||||||
}).pipe(
|
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "save-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
|
yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
|
||||||
}).pipe(
|
}).pipe(
|
||||||
Effect.mapError((cause) =>
|
Effect.mapError((cause) =>
|
||||||
cause._tag === "ConnectionPersistenceError"
|
cause._tag === "ConnectionPersistenceError"
|
||||||
? cause
|
? cause
|
||||||
: new ConnectionPersistenceError({
|
: persistenceError("save-shell", cause),
|
||||||
operation: "save-shell",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
loadThread: (environmentId, threadId) =>
|
loadThread: (environmentId, threadId) =>
|
||||||
@@ -495,15 +470,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
return Effect.succeed(Option.none());
|
return Effect.succeed(Option.none());
|
||||||
}
|
}
|
||||||
return decodeStoredThreadSnapshot(raw).pipe(
|
return decodeStoredThreadSnapshot(raw).pipe(
|
||||||
Effect.mapError(
|
Effect.mapError((cause) => persistenceError("load-thread", cause)),
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "load-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
Effect.map((stored) =>
|
Effect.map((stored) =>
|
||||||
stored.environmentId === environmentId && stored.threadId === threadId
|
stored.environmentId === environmentId && stored.threadId === threadId
|
||||||
? Option.some(stored.thread)
|
? Option.some(stored.thread)
|
||||||
@@ -514,12 +481,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
Effect.mapError((cause) =>
|
Effect.mapError((cause) =>
|
||||||
cause._tag === "ConnectionPersistenceError"
|
cause._tag === "ConnectionPersistenceError"
|
||||||
? cause
|
? cause
|
||||||
: new ConnectionPersistenceError({
|
: persistenceError("load-thread", cause),
|
||||||
operation: "load-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
saveThread: (environmentId, thread) =>
|
saveThread: (environmentId, thread) =>
|
||||||
@@ -529,17 +491,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
environmentId,
|
environmentId,
|
||||||
threadId: thread.id,
|
threadId: thread.id,
|
||||||
thread,
|
thread,
|
||||||
}).pipe(
|
}).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "save-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId: thread.id,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
yield* writeDatabaseValue(
|
yield* writeDatabaseValue(
|
||||||
database,
|
database,
|
||||||
THREAD_STORE_NAME,
|
THREAD_STORE_NAME,
|
||||||
@@ -550,12 +502,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
Effect.mapError((cause) =>
|
Effect.mapError((cause) =>
|
||||||
cause._tag === "ConnectionPersistenceError"
|
cause._tag === "ConnectionPersistenceError"
|
||||||
? cause
|
? cause
|
||||||
: new ConnectionPersistenceError({
|
: persistenceError("save-thread", cause),
|
||||||
operation: "save-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId: thread.id,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
removeThread: (environmentId, threadId) =>
|
removeThread: (environmentId, threadId) =>
|
||||||
@@ -563,17 +510,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
database,
|
database,
|
||||||
THREAD_STORE_NAME,
|
THREAD_STORE_NAME,
|
||||||
threadCacheKey(environmentId, threadId),
|
threadCacheKey(environmentId, threadId),
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => persistenceError("remove-thread", cause))),
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "remove-thread",
|
|
||||||
environmentId,
|
|
||||||
threadId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
clear: (environmentId) =>
|
clear: (environmentId) =>
|
||||||
Effect.all(
|
Effect.all(
|
||||||
[
|
[
|
||||||
@@ -585,16 +522,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
{ concurrency: "unbounded", discard: true },
|
{ concurrency: "unbounded", discard: true },
|
||||||
).pipe(
|
).pipe(Effect.mapError((cause) => persistenceError("clear-environment", cause))),
|
||||||
Effect.mapError(
|
|
||||||
(cause) =>
|
|
||||||
new ConnectionPersistenceError({
|
|
||||||
operation: "clear-environment",
|
|
||||||
environmentId,
|
|
||||||
cause,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return Context.make(ConnectionTargetStore, targetStore).pipe(
|
return Context.make(ConnectionTargetStore, targetStore).pipe(
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
import type { AdvertisedEndpoint, DesktopServerExposureState } from "@t3tools/contracts";
|
import type { AdvertisedEndpoint, DesktopServerExposureState } from "@t3tools/contracts";
|
||||||
|
import * as Cause from "effect/Cause";
|
||||||
|
import * as Schema from "effect/Schema";
|
||||||
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
||||||
import { AtomRegistry } from "effect/unstable/reactivity";
|
import { AtomRegistry } from "effect/unstable/reactivity";
|
||||||
import { describe, expect, it, vi } from "vite-plus/test";
|
import { describe, expect, it, vi } from "vite-plus/test";
|
||||||
|
|
||||||
import { createDesktopNetworkAccessStateAtom } from "./desktopNetworkAccess";
|
import {
|
||||||
|
createDesktopNetworkAccessStateAtom,
|
||||||
|
DesktopNetworkAccessLoadError,
|
||||||
|
DesktopNetworkAccessUnavailableError,
|
||||||
|
} from "./desktopNetworkAccess";
|
||||||
|
|
||||||
const serverExposureState: DesktopServerExposureState = {
|
const serverExposureState: DesktopServerExposureState = {
|
||||||
advertisedHost: "192.168.1.10",
|
advertisedHost: "192.168.1.10",
|
||||||
@@ -14,6 +20,7 @@ const serverExposureState: DesktopServerExposureState = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const advertisedEndpoints: ReadonlyArray<AdvertisedEndpoint> = [];
|
const advertisedEndpoints: ReadonlyArray<AdvertisedEndpoint> = [];
|
||||||
|
const isDesktopNetworkAccessLoadError = Schema.is(DesktopNetworkAccessLoadError);
|
||||||
|
|
||||||
describe("desktopNetworkAccessState", () => {
|
describe("desktopNetworkAccessState", () => {
|
||||||
it("retains the loaded snapshot when the settings screen remounts", async () => {
|
it("retains the loaded snapshot when the settings screen remounts", async () => {
|
||||||
@@ -47,4 +54,57 @@ describe("desktopNetworkAccessState", () => {
|
|||||||
remount();
|
remount();
|
||||||
registry.dispose();
|
registry.dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("reports an unavailable desktop bridge without inventing a cause", async () => {
|
||||||
|
const atom = createDesktopNetworkAccessStateAtom(() => undefined);
|
||||||
|
const registry = AtomRegistry.make();
|
||||||
|
registry.mount(atom);
|
||||||
|
|
||||||
|
await vi.waitFor(() => {
|
||||||
|
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
|
||||||
|
});
|
||||||
|
const result = registry.get(atom);
|
||||||
|
if (!AsyncResult.isFailure(result)) {
|
||||||
|
throw new Error("Expected desktop network access to fail");
|
||||||
|
}
|
||||||
|
const error = Cause.squash(result.cause);
|
||||||
|
|
||||||
|
expect(error).toBeInstanceOf(DesktopNetworkAccessUnavailableError);
|
||||||
|
expect(error).toMatchObject({
|
||||||
|
_tag: "DesktopNetworkAccessUnavailableError",
|
||||||
|
message: "Desktop network access is unavailable.",
|
||||||
|
});
|
||||||
|
|
||||||
|
registry.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("retains the failing bridge operation and cause", async () => {
|
||||||
|
const cause = new Error("native bridge rejected");
|
||||||
|
const atom = createDesktopNetworkAccessStateAtom(() => ({
|
||||||
|
getAdvertisedEndpoints: () => Promise.reject(cause),
|
||||||
|
getServerExposureState: () => Promise.resolve(serverExposureState),
|
||||||
|
}));
|
||||||
|
const registry = AtomRegistry.make();
|
||||||
|
registry.mount(atom);
|
||||||
|
|
||||||
|
await vi.waitFor(() => {
|
||||||
|
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
|
||||||
|
});
|
||||||
|
const result = registry.get(atom);
|
||||||
|
if (!AsyncResult.isFailure(result)) {
|
||||||
|
throw new Error("Expected desktop network access to fail");
|
||||||
|
}
|
||||||
|
const error = Cause.squash(result.cause);
|
||||||
|
if (!isDesktopNetworkAccessLoadError(error)) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).toMatchObject({
|
||||||
|
operation: "get-advertised-endpoints",
|
||||||
|
message: "Desktop network access operation 'get advertised endpoints' failed.",
|
||||||
|
});
|
||||||
|
expect(error.cause).toBe(cause);
|
||||||
|
|
||||||
|
registry.dispose();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -21,13 +21,32 @@ export interface DesktopNetworkAccessSnapshot {
|
|||||||
readonly serverExposureState: DesktopServerExposureState;
|
readonly serverExposureState: DesktopServerExposureState;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DesktopNetworkAccessError extends Schema.TaggedErrorClass<DesktopNetworkAccessError>()(
|
export class DesktopNetworkAccessUnavailableError extends Schema.TaggedErrorClass<DesktopNetworkAccessUnavailableError>()(
|
||||||
"DesktopNetworkAccessError",
|
"DesktopNetworkAccessUnavailableError",
|
||||||
|
{},
|
||||||
|
) {
|
||||||
|
override get message(): string {
|
||||||
|
return "Desktop network access is unavailable.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DesktopNetworkAccessLoadError extends Schema.TaggedErrorClass<DesktopNetworkAccessLoadError>()(
|
||||||
|
"DesktopNetworkAccessLoadError",
|
||||||
{
|
{
|
||||||
message: Schema.String,
|
operation: Schema.Literals(["get-advertised-endpoints", "get-server-exposure-state"]),
|
||||||
cause: Schema.optional(Schema.Defect()),
|
cause: Schema.Defect(),
|
||||||
},
|
},
|
||||||
) {}
|
) {
|
||||||
|
override get message(): string {
|
||||||
|
return `Desktop network access operation '${this.operation.replaceAll("-", " ")}' failed.`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DesktopNetworkAccessError = Schema.Union([
|
||||||
|
DesktopNetworkAccessUnavailableError,
|
||||||
|
DesktopNetworkAccessLoadError,
|
||||||
|
]);
|
||||||
|
export type DesktopNetworkAccessError = typeof DesktopNetworkAccessError.Type;
|
||||||
|
|
||||||
function getDesktopNetworkAccessBridge(): DesktopNetworkAccessBridge | undefined {
|
function getDesktopNetworkAccessBridge(): DesktopNetworkAccessBridge | undefined {
|
||||||
return typeof window === "undefined" ? undefined : window.desktopBridge;
|
return typeof window === "undefined" ? undefined : window.desktopBridge;
|
||||||
@@ -39,25 +58,30 @@ export function createDesktopNetworkAccessStateAtom(
|
|||||||
const loadDesktopNetworkAccess = Effect.fn("loadDesktopNetworkAccess")(function* () {
|
const loadDesktopNetworkAccess = Effect.fn("loadDesktopNetworkAccess")(function* () {
|
||||||
const bridge = getBridge();
|
const bridge = getBridge();
|
||||||
if (!bridge) {
|
if (!bridge) {
|
||||||
return yield* new DesktopNetworkAccessError({
|
return yield* new DesktopNetworkAccessUnavailableError();
|
||||||
message: "Desktop network access is unavailable.",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return yield* Effect.tryPromise({
|
const { serverExposureState, advertisedEndpoints } = yield* Effect.all(
|
||||||
try: async (): Promise<DesktopNetworkAccessSnapshot> => {
|
{
|
||||||
const [serverExposureState, advertisedEndpoints] = await Promise.all([
|
serverExposureState: Effect.tryPromise({
|
||||||
bridge.getServerExposureState(),
|
try: () => bridge.getServerExposureState(),
|
||||||
bridge.getAdvertisedEndpoints(),
|
catch: (cause) =>
|
||||||
]);
|
new DesktopNetworkAccessLoadError({
|
||||||
return { advertisedEndpoints, serverExposureState };
|
operation: "get-server-exposure-state",
|
||||||
},
|
cause,
|
||||||
catch: (cause) =>
|
}),
|
||||||
new DesktopNetworkAccessError({
|
|
||||||
message:
|
|
||||||
cause instanceof Error ? cause.message : "Failed to load desktop network access.",
|
|
||||||
cause,
|
|
||||||
}),
|
}),
|
||||||
});
|
advertisedEndpoints: Effect.tryPromise({
|
||||||
|
try: () => bridge.getAdvertisedEndpoints(),
|
||||||
|
catch: (cause) =>
|
||||||
|
new DesktopNetworkAccessLoadError({
|
||||||
|
operation: "get-advertised-endpoints",
|
||||||
|
cause,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{ concurrency: "unbounded" },
|
||||||
|
);
|
||||||
|
return { advertisedEndpoints, serverExposureState };
|
||||||
});
|
});
|
||||||
|
|
||||||
return Atom.make(loadDesktopNetworkAccess()).pipe(
|
return Atom.make(loadDesktopNetworkAccess()).pipe(
|
||||||
|
|||||||
@@ -694,7 +694,7 @@ describe("EnvironmentRegistry", () => {
|
|||||||
Effect.fail(
|
Effect.fail(
|
||||||
new Persistence.ConnectionPersistenceError({
|
new Persistence.ConnectionPersistenceError({
|
||||||
operation: "remove-connection",
|
operation: "remove-connection",
|
||||||
cause: new Error("Storage is unavailable."),
|
message: "Storage is unavailable.",
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
|
|
||||||
import { describe, expect, it } from "vite-plus/test";
|
|
||||||
|
|
||||||
import { ConnectionPersistenceError } from "./persistence.ts";
|
|
||||||
|
|
||||||
describe("ConnectionPersistenceError", () => {
|
|
||||||
it("retains resource context and the exact underlying failure", () => {
|
|
||||||
const cause = new Error("disk unavailable");
|
|
||||||
const error = new ConnectionPersistenceError({
|
|
||||||
operation: "load-thread",
|
|
||||||
environmentId: EnvironmentId.make("environment-1"),
|
|
||||||
threadId: ThreadId.make("thread-1"),
|
|
||||||
cause,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(error).toMatchObject({
|
|
||||||
operation: "load-thread",
|
|
||||||
environmentId: "environment-1",
|
|
||||||
threadId: "thread-1",
|
|
||||||
cause,
|
|
||||||
});
|
|
||||||
expect(error.message).toBe("Could not load thread.");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
EnvironmentId,
|
type EnvironmentId,
|
||||||
type OrchestrationThread,
|
type OrchestrationThread,
|
||||||
type OrchestrationShellSnapshot,
|
type OrchestrationShellSnapshot,
|
||||||
ThreadId,
|
type ThreadId,
|
||||||
} from "@t3tools/contracts";
|
} from "@t3tools/contracts";
|
||||||
import * as Context from "effect/Context";
|
import * as Context from "effect/Context";
|
||||||
import * as Effect from "effect/Effect";
|
import * as Effect from "effect/Effect";
|
||||||
@@ -26,15 +26,9 @@ export class ConnectionPersistenceError extends Schema.TaggedErrorClass<Connecti
|
|||||||
"remove-thread",
|
"remove-thread",
|
||||||
"clear-environment",
|
"clear-environment",
|
||||||
]),
|
]),
|
||||||
environmentId: Schema.optionalKey(EnvironmentId),
|
message: Schema.String,
|
||||||
threadId: Schema.optionalKey(ThreadId),
|
|
||||||
cause: Schema.Defect(),
|
|
||||||
},
|
},
|
||||||
) {
|
) {}
|
||||||
override get message(): string {
|
|
||||||
return `Could not ${this.operation.replaceAll("-", " ")}.`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ConnectionTargetStore extends Context.Service<
|
export class ConnectionTargetStore extends Context.Service<
|
||||||
ConnectionTargetStore,
|
ConnectionTargetStore,
|
||||||
|
|||||||
Reference in new issue
Block a user