71 lines
4.5 KiB
TypeScript
71 lines
4.5 KiB
TypeScript
/** Opt-in reader for the separate v2 payload schema. No DDL, execution-time
|
|
* credential revival, mutations, claim/replay, retention or registration. */
|
|
import { createServerProbeJournalHistoryReader, ServerProbeHistoryUnavailable,
|
|
type ServerProbeHistoryBinding, type ServerProbeJournalHistoryDependencies } from "./server-probe-journal-history.js"
|
|
import type { ProbeStorageOptions } from "./server-probe-journal-maintenance.js"
|
|
import { connectProbeStorage } from "./server-probe-storage-connection.js"
|
|
|
|
export interface ServerProbeStorageHistoryDependencies {
|
|
assertReadAuthority: ServerProbeJournalHistoryDependencies["assertReadAuthority"]
|
|
crypto?: ServerProbeJournalHistoryDependencies["crypto"]
|
|
options: ProbeStorageOptions
|
|
now?: () => number
|
|
}
|
|
const SQL = `/* probe-storage:history */ SELECT j.*,p.receipt_ciphertext,
|
|
p.receipt_proof AS payload_proof,a.receipt_id AS admission_receipt_id,
|
|
a.owner_id AS admission_owner_id,a.identity_kind AS admission_identity_kind,
|
|
a.execution_id AS admission_execution_id,a.binding_proof AS admission_binding_proof,
|
|
a.authority_expires_at AS admission_authority_expires_at,a.claim_token,
|
|
a.claim_attempted_at,a.purged_at,
|
|
(SELECT count(*) FROM server_probe_storage_admissions s
|
|
WHERE s.receipt_id=$1 OR(s.identity_kind=$2 AND s.execution_id=$3)) AS admission_count,
|
|
(SELECT count(*) FROM server_probe_execution_quarantines q
|
|
WHERE q.receipt_id=$1 OR(q.identity_kind=$2 AND q.execution_id=$3)) AS quarantine_count
|
|
FROM server_probe_executions j
|
|
LEFT JOIN server_probe_storage_admissions a ON a.receipt_id=j.receipt_id
|
|
LEFT JOIN server_probe_execution_payloads p ON p.receipt_id=j.receipt_id
|
|
WHERE j.receipt_id=$1 OR(j.identity_kind=$2 AND j.execution_id=$3) LIMIT 2`
|
|
function must(value: unknown): asserts value { if (!value) throw new ServerProbeHistoryUnavailable() }
|
|
|
|
export function createServerProbeStorageHistoryReader(deps: ServerProbeStorageHistoryDependencies) {
|
|
const options = { ...deps.options }, now = deps.now ?? Date.now, authorize = deps.assertReadAuthority
|
|
must(typeof authorize === "function" && Number.isSafeInteger(options.deadlineAt) && options.deadlineAt > now() && options.deadlineAt <= now() + 120000)
|
|
const live = () => { options.signal?.throwIfAborted(); must(now() < options.deadlineAt) }
|
|
const fence = async (binding: Readonly<ServerProbeHistoryBinding>) => { live(); await authorize(structuredClone(binding)); live() }
|
|
return createServerProbeJournalHistoryReader({ crypto: deps.crypto, assertReadAuthority: fence,
|
|
async loadArchive(binding) {
|
|
await fence(binding)
|
|
// Separate autocommit snapshots deliberately observe quarantine/purge
|
|
// committed during decryption. No repeatable-read snapshot across reads.
|
|
const client = await connectProbeStorage(options)
|
|
let unsafe = false
|
|
const result = await (async () => {
|
|
try {
|
|
live()
|
|
return await client.query(SQL, [binding.receiptId, binding.context.identity.kind, binding.context.identity.executionId], {
|
|
signal: options.signal, timeoutMs: Math.max(1, Math.min(10000, options.deadlineAt - now())) })
|
|
}
|
|
catch (error) { unsafe = true; throw error }
|
|
finally { client.release(unsafe) }
|
|
})()
|
|
await fence(binding)
|
|
return result.rows.map((row: Record<string, unknown>) => {
|
|
const identity = binding.context.identity
|
|
must((row.admission_count === "1" || row.admission_count === 1) && row.admission_receipt_id === binding.receiptId
|
|
&& row.admission_owner_id === identity.ownerId && row.admission_identity_kind === identity.kind
|
|
&& row.admission_execution_id === identity.executionId && row.admission_binding_proof === binding.bindingProof
|
|
&& new Date(row.admission_authority_expires_at as Date | string).getTime() === identity.expiresAt
|
|
&& row.claim_token === null && row.claim_attempted_at !== null
|
|
&& Number.isFinite(new Date(row.claim_attempted_at as Date | string).getTime()))
|
|
let state: "present" | "expired" | "unavailable"
|
|
if (row.purged_at !== null) {
|
|
must(Number.isFinite(new Date(row.purged_at as Date | string).getTime()) && row.receipt_ciphertext === null && row.payload_proof === null)
|
|
state = "expired"
|
|
} else if (row.receipt_ciphertext === null) { must(row.payload_proof === null); state = "unavailable" }
|
|
else { must(row.payload_proof === row.receipt_proof); state = "present" }
|
|
return { ...row, stored_payload_state: state }
|
|
})
|
|
},
|
|
})
|
|
}
|