|
| 1 | +import { fromLenientJson } from "@t3tools/shared/schemaJson"; |
| 2 | +import * as Context from "effect/Context"; |
| 3 | +import * as Crypto from "effect/Crypto"; |
| 4 | +import * as Data from "effect/Data"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as Encoding from "effect/Encoding"; |
| 7 | +import * as FileSystem from "effect/FileSystem"; |
| 8 | +import * as Layer from "effect/Layer"; |
| 9 | +import * as Option from "effect/Option"; |
| 10 | +import * as Path from "effect/Path"; |
| 11 | +import * as PlatformError from "effect/PlatformError"; |
| 12 | +import * as Schema from "effect/Schema"; |
| 13 | + |
| 14 | +import * as ElectronSafeStorage from "../electron/ElectronSafeStorageService.ts"; |
| 15 | +import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 16 | + |
| 17 | +const ConnectionCatalogDocument = Schema.Struct({ |
| 18 | + version: Schema.Literal(1), |
| 19 | + encryptedCatalog: Schema.String, |
| 20 | +}); |
| 21 | +type ConnectionCatalogDocument = typeof ConnectionCatalogDocument.Type; |
| 22 | + |
| 23 | +const ConnectionCatalogDocumentJson = fromLenientJson(ConnectionCatalogDocument); |
| 24 | +const decodeConnectionCatalogDocumentJson = Schema.decodeEffect(ConnectionCatalogDocumentJson); |
| 25 | +const encodeConnectionCatalogDocumentJson = Schema.encodeEffect(ConnectionCatalogDocumentJson); |
| 26 | + |
| 27 | +export class DesktopConnectionCatalogStoreWriteError extends Data.TaggedError( |
| 28 | + "DesktopConnectionCatalogStoreWriteError", |
| 29 | +)<{ |
| 30 | + readonly cause: PlatformError.PlatformError | Schema.SchemaError; |
| 31 | +}> { |
| 32 | + override get message() { |
| 33 | + return `Failed to write desktop connection catalog: ${this.cause.message}`; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +export class DesktopConnectionCatalogStoreDecodeError extends Data.TaggedError( |
| 38 | + "DesktopConnectionCatalogStoreDecodeError", |
| 39 | +)<{ |
| 40 | + readonly cause: Encoding.EncodingError; |
| 41 | +}> { |
| 42 | + override get message() { |
| 43 | + return "Failed to decode the desktop connection catalog."; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export interface DesktopConnectionCatalogStoreShape { |
| 48 | + readonly get: Effect.Effect< |
| 49 | + Option.Option<string>, |
| 50 | + | DesktopConnectionCatalogStoreDecodeError |
| 51 | + | ElectronSafeStorage.ElectronSafeStorageAvailabilityError |
| 52 | + | ElectronSafeStorage.ElectronSafeStorageDecryptError |
| 53 | + >; |
| 54 | + readonly set: ( |
| 55 | + catalog: string, |
| 56 | + ) => Effect.Effect< |
| 57 | + boolean, |
| 58 | + | DesktopConnectionCatalogStoreWriteError |
| 59 | + | ElectronSafeStorage.ElectronSafeStorageAvailabilityError |
| 60 | + | ElectronSafeStorage.ElectronSafeStorageEncryptError |
| 61 | + >; |
| 62 | + readonly clear: Effect.Effect<void>; |
| 63 | +} |
| 64 | + |
| 65 | +export class DesktopConnectionCatalogStore extends Context.Service< |
| 66 | + DesktopConnectionCatalogStore, |
| 67 | + DesktopConnectionCatalogStoreShape |
| 68 | +>()("@t3tools/desktop/app/DesktopConnectionCatalogStore") {} |
| 69 | + |
| 70 | +function decodeSecretBytes( |
| 71 | + encoded: string, |
| 72 | +): Effect.Effect<Uint8Array, DesktopConnectionCatalogStoreDecodeError> { |
| 73 | + return Effect.fromResult(Encoding.decodeBase64(encoded)).pipe( |
| 74 | + Effect.mapError((cause) => new DesktopConnectionCatalogStoreDecodeError({ cause })), |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +const readDocument = ( |
| 79 | + fileSystem: FileSystem.FileSystem, |
| 80 | + catalogPath: string, |
| 81 | +): Effect.Effect<Option.Option<ConnectionCatalogDocument>> => |
| 82 | + fileSystem.readFileString(catalogPath).pipe( |
| 83 | + Effect.option, |
| 84 | + Effect.flatMap( |
| 85 | + Option.match({ |
| 86 | + onNone: () => Effect.succeed(Option.none<ConnectionCatalogDocument>()), |
| 87 | + onSome: (raw) => decodeConnectionCatalogDocumentJson(raw).pipe(Effect.option), |
| 88 | + }), |
| 89 | + ), |
| 90 | + ); |
| 91 | + |
| 92 | +const writeDocument = Effect.fn("desktop.connectionCatalogStore.writeDocument")(function* (input: { |
| 93 | + readonly fileSystem: FileSystem.FileSystem; |
| 94 | + readonly path: Path.Path; |
| 95 | + readonly catalogPath: string; |
| 96 | + readonly document: ConnectionCatalogDocument; |
| 97 | + readonly suffix: string; |
| 98 | +}): Effect.fn.Return<void, PlatformError.PlatformError | Schema.SchemaError> { |
| 99 | + const directory = input.path.dirname(input.catalogPath); |
| 100 | + const tempPath = `${input.catalogPath}.${process.pid}.${input.suffix}.tmp`; |
| 101 | + const encoded = yield* encodeConnectionCatalogDocumentJson(input.document); |
| 102 | + yield* input.fileSystem.makeDirectory(directory, { recursive: true }); |
| 103 | + yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`); |
| 104 | + yield* input.fileSystem.rename(tempPath, input.catalogPath); |
| 105 | +}); |
| 106 | + |
| 107 | +export const layer = Layer.effect( |
| 108 | + DesktopConnectionCatalogStore, |
| 109 | + Effect.gen(function* () { |
| 110 | + const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 111 | + const fileSystem = yield* FileSystem.FileSystem; |
| 112 | + const path = yield* Path.Path; |
| 113 | + const safeStorage = yield* ElectronSafeStorage.ElectronSafeStorage; |
| 114 | + const crypto = yield* Crypto.Crypto; |
| 115 | + const catalogPath = path.join(environment.stateDir, "connection-catalog.json"); |
| 116 | + |
| 117 | + return DesktopConnectionCatalogStore.of({ |
| 118 | + get: Effect.gen(function* () { |
| 119 | + const document = yield* readDocument(fileSystem, catalogPath); |
| 120 | + if (Option.isNone(document) || !(yield* safeStorage.isEncryptionAvailable)) { |
| 121 | + return Option.none<string>(); |
| 122 | + } |
| 123 | + const bytes = yield* decodeSecretBytes(document.value.encryptedCatalog); |
| 124 | + return Option.some(yield* safeStorage.decryptString(bytes)); |
| 125 | + }).pipe(Effect.withSpan("desktop.connectionCatalogStore.get")), |
| 126 | + set: Effect.fn("desktop.connectionCatalogStore.set")(function* (catalog) { |
| 127 | + if (!(yield* safeStorage.isEncryptionAvailable)) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + const encryptedCatalog = Encoding.encodeBase64(yield* safeStorage.encryptString(catalog)); |
| 131 | + const suffix = (yield* crypto.randomUUIDv4.pipe( |
| 132 | + Effect.mapError((cause) => new DesktopConnectionCatalogStoreWriteError({ cause })), |
| 133 | + )).replace(/-/g, ""); |
| 134 | + yield* writeDocument({ |
| 135 | + fileSystem, |
| 136 | + path, |
| 137 | + catalogPath, |
| 138 | + document: { version: 1, encryptedCatalog }, |
| 139 | + suffix, |
| 140 | + }).pipe(Effect.mapError((cause) => new DesktopConnectionCatalogStoreWriteError({ cause }))); |
| 141 | + return true; |
| 142 | + }), |
| 143 | + clear: fileSystem.remove(catalogPath, { force: true }).pipe( |
| 144 | + Effect.catch(() => Effect.void), |
| 145 | + Effect.withSpan("desktop.connectionCatalogStore.clear"), |
| 146 | + ), |
| 147 | + }); |
| 148 | + }), |
| 149 | +); |
0 commit comments