| 6 | import { McpAuth } from "../../src/mcp/auth" |
| 7 | |
| 8 | function authFile() { |
| 9 | let raw = "" |
| 10 | let activeWrites = 0 |
| 11 | let sawOverlap = false |
| 12 | |
| 13 | const fsLayer = Layer.effect( |
| 14 | FSUtil.Service, |
| 15 | Effect.gen(function* () { |
| 16 | const fs = yield* FSUtil.Service |
| 17 | |
| 18 | return FSUtil.Service.of({ |
| 19 | ...fs, |
| 20 | readJson: (file) => |
| 21 | file.endsWith("mcp-auth.json") |
| 22 | ? Effect.try({ |
| 23 | try: () => { |
| 24 | if (!raw) throw new Error("mcp-auth.json missing") |
| 25 | return JSON.parse(raw) |
| 26 | }, |
| 27 | catch: (cause) => new FSUtil.FileSystemError({ method: "readJson", cause }), |
| 28 | }) |
| 29 | : fs.readJson(file), |
| 30 | writeJson: (file, value, mode) => |
| 31 | file.endsWith("mcp-auth.json") |
| 32 | ? Effect.promise(async () => { |
| 33 | activeWrites++ |
| 34 | sawOverlap = sawOverlap || activeWrites > 1 |
| 35 | raw = "" |
| 36 | await sleep(10) |
| 37 | const next = JSON.stringify(value, null, 2) |
| 38 | raw = sawOverlap ? `${next}\n}` : next |
| 39 | activeWrites-- |
| 40 | }) |
| 41 | : fs.writeJson(file, value, mode), |
| 42 | }) |
| 43 | }), |
| 44 | ).pipe(Layer.provide(AppNodeBuilder.build(FSUtil.node))) |
| 45 | |
| 46 | return { fsLayer, raw: () => raw } |
| 47 | } |
| 48 | |
| 49 | function authService(fsLayer: Layer.Layer<FSUtil.Service>) { |
| 50 | return McpAuth.Service.use((auth) => Effect.succeed(auth)).pipe( |