| 122 | // generate-and-persist secret.key shape, minus the secrecy — this id is not a |
| 123 | // credential, it is the anonymous distinct_id the events file under). |
| 124 | const loadOrMintAnonymousId = ( |
| 125 | dataDir: string, |
| 126 | ): Effect.Effect<string, never, FileSystem.FileSystem> => |
| 127 | Effect.gen(function* () { |
| 128 | const fs = yield* FileSystem.FileSystem; |
| 129 | const idPath = `${dataDir}/${ANALYTICS_ID_FILE}`; |
| 130 | const existing = yield* fs.readFileString(idPath).pipe( |
| 131 | Effect.map((raw) => raw.trim()), |
| 132 | Effect.catch(() => Effect.succeed("")), |
| 133 | ); |
| 134 | if (existing.length > 0) return existing; |
| 135 | const minted = crypto.randomUUID(); |
| 136 | yield* fs.makeDirectory(dataDir, { recursive: true }).pipe(Effect.ignore); |
| 137 | yield* fs.writeFileString(idPath, minted).pipe(Effect.ignore); |
| 138 | return minted; |
| 139 | }); |
| 140 | |
| 141 | // --------------------------------------------------------------------------- |
| 142 | // Layer |