35 lines
2.3 KiB
JavaScript
35 lines
2.3 KiB
JavaScript
// Pure evidence checks; no HTTP, SQL or historical incident data.
|
|
export const TEST_DIRECT_DELIVERY_URLS = Object.freeze({
|
|
LASTLEG_SERVICE_URL: 'http://lastleg-service:9303',
|
|
FIRSTLEG_SERVICE_URL: 'http://firstleg:9202',
|
|
FINANCE_SERVICE_URL: 'http://finance:9203',
|
|
})
|
|
export const INTERNAL_TOKEN_COHORT_SERVICES = Object.freeze(['firstleg', 'lastleg-service', 'finance'])
|
|
export function validDirectDeliveryEnvironment(values = {}) {
|
|
return Object.entries(TEST_DIRECT_DELIVERY_URLS).every(([key, url]) => values[key] === url)
|
|
}
|
|
// Only provide non-secret credential revision/fingerprints, never tokens.
|
|
export function validInternalTokenCohort(fingerprints = {}) {
|
|
const values = INTERNAL_TOKEN_COHORT_SERVICES.map(service => fingerprints[service])
|
|
return values.every(value => typeof value === 'string' && value.trim().length > 0) && new Set(values).size === 1
|
|
}
|
|
export function validDirectDeliveryEvidence(facts = {}) {
|
|
const attempts = facts.attempts
|
|
if (!facts.marker || !facts.eventId || !facts.payloadHash || !Array.isArray(attempts) || attempts.length < 1) return false
|
|
if (!Number.isInteger(facts.maxAttempts) || facts.maxAttempts < 1 || facts.sqlMutationCount !== 0) return false
|
|
if (!attempts.every(a => a.eventId === facts.eventId && a.payloadHash === facts.payloadHash
|
|
&& Number.isFinite(Date.parse(a.at)) && a.requestId && a.traceId)) return false
|
|
const failures = attempts.filter(a => ['DNS_UNAVAILABLE','TOKEN_REJECTED','TIMEOUT'].includes(a.failure))
|
|
if (failures.length === 0 || attempts.some(a => a.failure && a.outcome)) return false
|
|
if (failures.some(a => a.failure === 'TOKEN_REJECTED') && facts.status !== 'DEAD'
|
|
&& facts.authorizedDeadRetry !== true) return false
|
|
const successful = attempts.filter(a => ['APPLIED','DUPLICATE','STALE'].includes(a.outcome))
|
|
if (facts.status === 'DEAD') return successful.length === 0 && (failures.length === facts.maxAttempts
|
|
|| (failures.length === 1 && failures[0].failure === 'TOKEN_REJECTED'))
|
|
&& facts.automaticRetryScheduled === false
|
|
// A read-only snapshot alone can never prove a duplicate delivery happened.
|
|
return facts.status === 'SENT' && failures.length < facts.maxAttempts
|
|
&& successful.some(a => a.outcome === 'APPLIED' && a.ackLost === true) && successful.some(a => a.outcome === 'DUPLICATE')
|
|
&& facts.inboxEventCount === 1 && facts.businessEffectCount === 1
|
|
}
|