Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 62 lines 2.53 KB
Raw
1 /**
2 * macOS backend picker.
3 *
4 * The production CLI runs on Bun, so the FFI backend is the default
5 * there — it's ~25× faster per read than the subprocess backend and
6 * has no p99 tail. In Node environments (tests, dev tools that import
7 * this package without Bun), `bun:ffi` is not available; we fall back
8 * to the subprocess backend, which shells out to `/usr/bin/security`.
9 *
10 * Both backends expose the same `CredentialStore` interface so the
11 * rest of the package is agnostic about which one is active.
12 */
13
14 import type { CredentialStore } from '../core/store';
15 import { MacOSSecuritySubprocessStore } from './macos-security-subprocess';
16
17 /**
18 * Backend selector for `createMacOSStore`.
19 *
20 * - `"auto"` / `"subprocess"`: shell out to `/usr/bin/security` for
21 * every operation. ~25ms per call, no ACL dialogs, works with
22 * unsigned / ad-hoc signed callers. Safe default.
23 * - `"ffi"`: call Security.framework directly via `bun:ffi`. ~1ms
24 * per read, but currently triggers a macOS trust dialog when the
25 * calling binary is not signed with a stable Developer ID.
26 * Experimental — opt-in only. Falls back to subprocess when
27 * `bun:ffi` is unavailable (Node runtime).
28 */
29 export type MacOSBackend = 'auto' | 'subprocess' | 'ffi';
30
31 const isBun = typeof (globalThis as { Bun?: unknown }).Bun !== 'undefined';
32
33 /**
34 * Resolve the appropriate macOS store for the current runtime.
35 *
36 * Defaults to `"auto"` which picks the subprocess backend — the only
37 * path that reliably avoids macOS keychain trust dialogs for ad-hoc
38 * signed binaries. Callers can opt into `"ffi"` for the ~25× perf win
39 * if and only if the calling binary is signed with a stable Developer
40 * ID certificate (dialogs appear otherwise).
41 */
42 export async function createMacOSStore(backend: MacOSBackend = 'auto'): Promise<CredentialStore> {
43 if (backend === 'ffi' && isBun) {
44 const mod = await import('./macos-security-ffi');
45 return new mod.MacOSSecurityFFIStore();
46 }
47 return new MacOSSecuritySubprocessStore();
48 }
49
50 /**
51 * Synchronous variant — returns the subprocess backend unconditionally.
52 * Callers that cannot await (e.g. `createDefaultStore()` used by the
53 * CLI's synchronous Effect layer construction) can use this at the
54 * cost of giving up the FFI opt-in.
55 */
56 export function createMacOSStoreSync(): CredentialStore {
57 return new MacOSSecuritySubprocessStore();
58 }
59
60 // Re-export both concrete stores for callers that want to wire a
61 // specific one by hand (tests, benchmarks, diagnostics).
62 export { MacOSSecuritySubprocessStore } from './macos-security-subprocess';