← Public packages
@kody/kody-factory
Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.
Files
/**
* Signal source CRUD. Each source gets an inbound URL at /signals/in/:sourceId
* and an HMAC signing key generated at creation time.
*/
import { SOURCE_KINDS, type CreateSourceInput, type SourceRecord, type UpdateSourceInput } from './types.ts'
import { getSource, listSources, insertSource, updateSource as storeUpdate, deleteSource as storeDelete } from './store.ts'
import { generateSigningKey, deleteSigningKey } from './hmac.ts'
export { listSources, getSource } from './store.ts'
export { verifyHmac, loadSigningKey } from './hmac.ts'
export { SOURCE_KINDS } from './types.ts'
export type { SourceRecord, SourceKind, CreateSourceInput, UpdateSourceInput } from './types.ts'
const SOURCE_ID_RE = /^[a-z][a-z0-9_-]{1,62}$/
function nowIso(): string {
return new Date().toISOString()
}
export async function createSource(input: CreateSourceInput): Promise<{ source: SourceRecord; signingKey: string | null; persisted: boolean }> {
if (!input.id || !SOURCE_ID_RE.test(input.id)) {
throw new Error(`Invalid source id "${input.id}": must match ${SOURCE_ID_RE}`)
}
if (!input.name?.trim()) throw new Error('Source name is required')
if (!SOURCE_KINDS.includes(input.kind)) throw new Error(`Unknown source kind "${input.kind}"`)
const existing = await getSource(input.id)
if (existing) throw new Error(`Source "${input.id}" already exists`)
const at = nowIso()
const signingSecretName = `signal_source_${input.id}`
const record: SourceRecord = {
id: input.id,
name: input.name.trim(),
kind: input.kind,
projectScope: (input.projectScope ?? []).filter(Boolean),
signingSecretName,
signingHeader: input.signingHeader || 'x-signature-256',
signingAlgo: 'hmac-sha256',
note: input.note ?? '',
enabled: input.enabled !== false,
createdAt: at,
updatedAt: at,
}
if (input.dryRun) return { source: record, signingKey: null, persisted: false }
const signingKey = await generateSigningKey(input.id)
await insertSource(record)
return { source: record, signingKey, persisted: true }
}
export async function updateSource(input: UpdateSourceInput): Promise<{ source: SourceRecord }> {
if (!input.id) throw new Error('Source id is required')
const existing = await getSource(input.id)
if (!existing) throw new Error(`Source "${input.id}" not found`)
if (input.kind && !SOURCE_KINDS.includes(input.kind)) throw new Error(`Unknown source kind "${input.kind}"`)
if (input.dryRun) {
return {
source: {
...existing,
...(input.name !== undefined ? { name: input.name.trim() } : {}),
...(input.kind !== undefined ? { kind: input.kind } : {}),
...(input.projectScope !== undefined ? { projectScope: input.projectScope.filter(Boolean) } : {}),
...(input.note !== undefined ? { note: input.note } : {}),
...(input.signingHeader !== undefined ? { signingHeader: input.signingHeader } : {}),
...(input.enabled !== undefined ? { enabled: input.enabled } : {}),
},
}
}
const updated = await storeUpdate(input.id, {
...(input.name !== undefined ? { name: input.name.trim() } : {}),
...(input.kind !== undefined ? { kind: input.kind } : {}),
...(input.projectScope !== undefined ? { projectScope: input.projectScope.filter(Boolean) } : {}),
...(input.note !== undefined ? { note: input.note } : {}),
...(input.signingHeader !== undefined ? { signingHeader: input.signingHeader } : {}),
...(input.enabled !== undefined ? { enabled: input.enabled } : {}),
updatedAt: nowIso(),
})
if (!updated) throw new Error(`Source "${input.id}" not found after update`)
return { source: updated }
}
export async function deleteSourceById(id: string, opts?: { confirm?: boolean }): Promise<{ deleted: boolean }> {
if (!id) throw new Error('Source id is required')
if (!opts?.confirm) throw new Error('Pass { confirm: true } to delete a source')
const existing = await getSource(id)
if (!existing) throw new Error(`Source "${id}" not found`)
await storeDelete(id)
await deleteSigningKey(id)
return { deleted: true }
}