| ... | ... | @@ -1,5 +1,4 @@ |
| 1 | 1 | import { ManagedRelay } from "@t3tools/client-runtime/relay"; |
| 2 | | import * as Data from "effect/Data"; |
| 3 | 2 | import * as Effect from "effect/Effect"; |
| 4 | 3 | import * as Schema from "effect/Schema"; |
| 5 | 4 | import * as SecureStore from "expo-secure-store"; |
| ... | ... | @@ -31,36 +30,50 @@ const decodeManagedRelayAccessTokenCache = Schema.decodeUnknownEffect( |
| 31 | 30 | ); |
| 32 | 31 | const encodeManagedRelayAccessTokenCache = Schema.encodeEffect(ManagedRelayAccessTokenCacheSchema); |
| 33 | 32 | |
| 34 | | export class ManagedRelayTokenStoreError extends Data.TaggedError("ManagedRelayTokenStoreError")<{ |
| 35 | | readonly message: string; |
| 36 | | readonly cause: unknown; |
| 37 | | }> {} |
| 38 | | |
| 39 | | const storeError = |
| 40 | | (message: string) => |
| 41 | | (cause: unknown): ManagedRelayTokenStoreError => |
| 42 | | new ManagedRelayTokenStoreError({ message, cause }); |
| 33 | export class ManagedRelayTokenStoreError extends Schema.TaggedErrorClass<ManagedRelayTokenStoreError>()( |
| 34 | "ManagedRelayTokenStoreError", |
| 35 | { |
| 36 | operation: Schema.Literals(["read", "decode", "encode", "write", "clear"]), |
| 37 | storageKey: Schema.String, |
| 38 | cause: Schema.Defect(), |
| 39 | }, |
| 40 | ) { |
| 41 | override get message(): string { |
| 42 | return `Managed relay token store operation "${this.operation}" failed for key "${this.storageKey}".`; |
| 43 | } |
| 44 | } |
| 43 | 45 | |
| 44 | | function logStoreFailure(operation: string) { |
| 45 | | return (error: ManagedRelayTokenStoreError) => |
| 46 | | Effect.logWarning(`Managed relay token store ${operation} failed.`).pipe( |
| 47 | | Effect.annotateLogs({ |
| 48 | | errorTag: error._tag, |
| 49 | | message: error.message, |
| 50 | | }), |
| 51 | | ); |
| 46 | function logStoreFailure(error: ManagedRelayTokenStoreError) { |
| 47 | return Effect.logWarning("Managed relay token store operation failed.", { |
| 48 | errorTag: error._tag, |
| 49 | operation: error.operation, |
| 50 | storageKey: error.storageKey, |
| 51 | cause: error, |
| 52 | }); |
| 52 | 53 | } |
| 53 | 54 | |
| 54 | 55 | const loadManagedRelayAccessTokens = Effect.tryPromise({ |
| 55 | 56 | try: () => SecureStore.getItemAsync(MANAGED_RELAY_TOKEN_CACHE_KEY), |
| 56 | | catch: storeError("Could not read persisted relay access tokens."), |
| 57 | catch: (cause) => |
| 58 | new ManagedRelayTokenStoreError({ |
| 59 | operation: "read", |
| 60 | storageKey: MANAGED_RELAY_TOKEN_CACHE_KEY, |
| 61 | cause, |
| 62 | }), |
| 57 | 63 | }).pipe( |
| 58 | 64 | Effect.flatMap((encoded) => |
| 59 | 65 | encoded === null |
| 60 | 66 | ? Effect.succeed<ReadonlyArray<ManagedRelay.ManagedRelayAccessTokenCacheEntry>>([]) |
| 61 | 67 | : decodeManagedRelayAccessTokenCache(encoded).pipe( |
| 62 | 68 | Effect.map((cache) => cache.entries), |
| 63 | | Effect.mapError(storeError("Persisted relay access tokens are invalid.")), |
| 69 | Effect.mapError( |
| 70 | (cause) => |
| 71 | new ManagedRelayTokenStoreError({ |
| 72 | operation: "decode", |
| 73 | storageKey: MANAGED_RELAY_TOKEN_CACHE_KEY, |
| 74 | cause, |
| 75 | }), |
| 76 | ), |
| 64 | 77 | ), |
| 65 | 78 | ), |
| 66 | 79 | ); |
| ... | ... | @@ -72,34 +85,48 @@ const saveManagedRelayAccessTokens = ( |
| 72 | 85 | version: MANAGED_RELAY_TOKEN_CACHE_VERSION, |
| 73 | 86 | entries, |
| 74 | 87 | }).pipe( |
| 75 | | Effect.mapError(storeError("Could not encode relay access tokens.")), |
| 88 | Effect.mapError( |
| 89 | (cause) => |
| 90 | new ManagedRelayTokenStoreError({ |
| 91 | operation: "encode", |
| 92 | storageKey: MANAGED_RELAY_TOKEN_CACHE_KEY, |
| 93 | cause, |
| 94 | }), |
| 95 | ), |
| 76 | 96 | Effect.flatMap((encoded) => |
| 77 | 97 | Effect.tryPromise({ |
| 78 | 98 | try: () => SecureStore.setItemAsync(MANAGED_RELAY_TOKEN_CACHE_KEY, encoded), |
| 79 | | catch: storeError("Could not persist relay access tokens."), |
| 99 | catch: (cause) => |
| 100 | new ManagedRelayTokenStoreError({ |
| 101 | operation: "write", |
| 102 | storageKey: MANAGED_RELAY_TOKEN_CACHE_KEY, |
| 103 | cause, |
| 104 | }), |
| 80 | 105 | }), |
| 81 | 106 | ), |
| 82 | 107 | ); |
| 83 | 108 | |
| 84 | 109 | const clearManagedRelayAccessTokens = Effect.tryPromise({ |
| 85 | 110 | try: () => SecureStore.deleteItemAsync(MANAGED_RELAY_TOKEN_CACHE_KEY), |
| 86 | | catch: storeError("Could not clear persisted relay access tokens."), |
| 111 | catch: (cause) => |
| 112 | new ManagedRelayTokenStoreError({ |
| 113 | operation: "clear", |
| 114 | storageKey: MANAGED_RELAY_TOKEN_CACHE_KEY, |
| 115 | cause, |
| 116 | }), |
| 87 | 117 | }); |
| 88 | 118 | |
| 89 | 119 | export const managedRelayAccessTokenStore: ManagedRelay.ManagedRelayAccessTokenStore = { |
| 90 | 120 | load: loadManagedRelayAccessTokens.pipe( |
| 91 | | Effect.tapError(logStoreFailure("load")), |
| 121 | Effect.tapError(logStoreFailure), |
| 92 | 122 | Effect.orElseSucceed(() => []), |
| 93 | 123 | Effect.withSpan("mobile.managedRelayTokenStore.load"), |
| 94 | 124 | ), |
| 95 | 125 | save: Effect.fn("mobile.managedRelayTokenStore.save")((entries) => |
| 96 | | saveManagedRelayAccessTokens(entries).pipe( |
| 97 | | Effect.tapError(logStoreFailure("save")), |
| 98 | | Effect.ignore, |
| 99 | | ), |
| 126 | saveManagedRelayAccessTokens(entries).pipe(Effect.tapError(logStoreFailure), Effect.ignore), |
| 100 | 127 | ), |
| 101 | 128 | clear: clearManagedRelayAccessTokens.pipe( |
| 102 | | Effect.tapError(logStoreFailure("clear")), |
| 129 | Effect.tapError(logStoreFailure), |
| 103 | 130 | Effect.ignore, |
| 104 | 131 | Effect.withSpan("mobile.managedRelayTokenStore.clear"), |
| 105 | 132 | ), |