Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Subprocess plumbing shared by the macOS and Linux stores. |
| 3 | * |
| 4 | * Both stores shell out to OS-provided CLIs (`/usr/bin/security` and |
| 5 | * `secret-tool`) via `node:child_process`. Keeping the spawn helper |
| 6 | * here means the backends only have to deal with argv construction |
| 7 | * and exit-code mapping — not stream buffering. |
| 8 | */ |
| 9 | |
| 10 | import { spawn, type SpawnOptionsWithoutStdio } from 'node:child_process'; |
| 11 | |
| 12 | const textEncoder = new TextEncoder(); |
| 13 | const textDecoder = new TextDecoder('utf-8'); |
| 14 | |
| 15 | export interface SpawnResult { |
| 16 | readonly code: number | null; |
| 17 | readonly signal: NodeJS.Signals | null; |
| 18 | readonly stdout: Uint8Array; |
| 19 | readonly stderr: Uint8Array; |
| 20 | } |
| 21 | |
| 22 | export interface SpawnInput { |
| 23 | readonly command: string; |
| 24 | readonly args: readonly string[]; |
| 25 | /** |
| 26 | * Raw bytes to write to stdin before closing it. Used by the Linux |
| 27 | * store to pipe passwords into `secret-tool store` without putting |
| 28 | * them on argv. |
| 29 | */ |
| 30 | readonly stdin?: Uint8Array; |
| 31 | readonly env?: NodeJS.ProcessEnv; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Concatenate an array of Uint8Arrays without going through Buffer. |
| 36 | */ |
| 37 | function concatBytes(chunks: readonly Uint8Array[]): Uint8Array { |
| 38 | let total = 0; |
| 39 | for (const chunk of chunks) total += chunk.byteLength; |
| 40 | const out = new Uint8Array(total); |
| 41 | let offset = 0; |
| 42 | for (const chunk of chunks) { |
| 43 | out.set(chunk, offset); |
| 44 | offset += chunk.byteLength; |
| 45 | } |
| 46 | return out; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Spawn a subprocess and return all output buffered. Resolves once |
| 51 | * the process exits; never rejects — errors from failed spawns (e.g. |
| 52 | * ENOENT when `secret-tool` isn't installed) surface as a rejected |
| 53 | * promise with the original `Error`, which callers convert into a |
| 54 | * `KeyringError({ kind: 'NoStorageAccess' })` or similar. |
| 55 | */ |
| 56 | export function runCommand(input: SpawnInput): Promise<SpawnResult> { |
| 57 | return new Promise((resolve, reject) => { |
| 58 | const options: SpawnOptionsWithoutStdio = { |
| 59 | stdio: ['pipe', 'pipe', 'pipe'], |
| 60 | env: input.env, |
| 61 | }; |
| 62 | let child; |
| 63 | try { |
| 64 | child = spawn(input.command, [...input.args], options); |
| 65 | } catch (err) { |
| 66 | reject(err); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | const stdoutChunks: Uint8Array[] = []; |
| 71 | const stderrChunks: Uint8Array[] = []; |
| 72 | |
| 73 | child.stdout.on('data', (chunk: Uint8Array) => stdoutChunks.push(chunk)); |
| 74 | child.stderr.on('data', (chunk: Uint8Array) => stderrChunks.push(chunk)); |
| 75 | |
| 76 | child.once('error', err => reject(err)); |
| 77 | child.once('close', (code, signal) => { |
| 78 | resolve({ |
| 79 | code, |
| 80 | signal, |
| 81 | stdout: concatBytes(stdoutChunks), |
| 82 | stderr: concatBytes(stderrChunks), |
| 83 | }); |
| 84 | }); |
| 85 | |
| 86 | if (input.stdin !== undefined) { |
| 87 | child.stdin.end(input.stdin); |
| 88 | } else { |
| 89 | child.stdin.end(); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | /** Decode a byte slice as UTF-8. */ |
| 95 | export function bytesToUtf8(bytes: Uint8Array): string { |
| 96 | return textDecoder.decode(bytes); |
| 97 | } |
| 98 | |
| 99 | /** Encode a JS string as UTF-8 bytes. */ |
| 100 | export function utf8ToBytes(s: string): Uint8Array { |
| 101 | return textEncoder.encode(s); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Universal on-disk encoding for stored secrets. We base64-encode |
| 106 | * every secret before handing it to `security` / `secret-tool` because: |
| 107 | * |
| 108 | * 1. `security -w` takes the password on argv and cannot carry binary |
| 109 | * bytes (NUL bytes truncate; newlines break parsing). |
| 110 | * 2. `secret-tool store` reads one line from stdin via |
| 111 | * `g_io_channel_read_line`, which can't carry NUL bytes or embedded |
| 112 | * newlines either. |
| 113 | * |
| 114 | * The tradeoff is that credentials written by this package are NOT |
| 115 | * readable by other tools that share the same keychain namespace — |
| 116 | * they'll see `<base64 blob>` instead of the original value. For the |
| 117 | * CLI's use case (our own API key) that's fine, and the prefix tag |
| 118 | * keeps us honest about what we're doing. |
| 119 | */ |
| 120 | const STORAGE_PREFIX = 'b64:'; |
| 121 | |
| 122 | /** |
| 123 | * Encode bytes to base64 via `btoa` on a binary latin-1 string — |
| 124 | * avoids `Buffer` and works in every runtime that provides the HTML5 |
| 125 | * `btoa` global (Node 16+, Bun, browsers). |
| 126 | */ |
| 127 | function bytesToBase64(bytes: Uint8Array): string { |
| 128 | let binary = ''; |
| 129 | for (let i = 0; i < bytes.byteLength; i++) { |
| 130 | binary += String.fromCharCode(bytes[i]!); |
| 131 | } |
| 132 | return btoa(binary); |
| 133 | } |
| 134 | |
| 135 | function base64ToBytes(b64: string): Uint8Array { |
| 136 | const binary = atob(b64); |
| 137 | const bytes = new Uint8Array(binary.length); |
| 138 | for (let i = 0; i < binary.length; i++) { |
| 139 | bytes[i] = binary.charCodeAt(i); |
| 140 | } |
| 141 | return bytes; |
| 142 | } |
| 143 | |
| 144 | /** Encode raw bytes into the on-disk format. */ |
| 145 | export function encodeSecret(secret: Uint8Array): string { |
| 146 | return STORAGE_PREFIX + bytesToBase64(secret); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Decode the on-disk format back into raw bytes. Throws a plain |
| 151 | * `Error` on malformed input; callers wrap it in |
| 152 | * `KeyringError({ kind: 'BadDataFormat' })`. |
| 153 | */ |
| 154 | export function decodeSecret(stored: string): Uint8Array { |
| 155 | const trimmed = stored.replace(/\n$/, ''); |
| 156 | if (!trimmed.startsWith(STORAGE_PREFIX)) { |
| 157 | throw new Error(`expected keyring value to start with "${STORAGE_PREFIX}" prefix`); |
| 158 | } |
| 159 | const encoded = trimmed.slice(STORAGE_PREFIX.length); |
| 160 | // Strict validation — `atob` is lenient and malformed blobs would |
| 161 | // silently decode to truncated garbage otherwise. |
| 162 | if (!/^[A-Za-z0-9+/]*={0,2}$/.test(encoded)) { |
| 163 | throw new Error('keyring value contains non-base64 characters'); |
| 164 | } |
| 165 | return base64ToBytes(encoded); |
| 166 | } |