Files
attest/src/predicate.ts
T

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-02-18 08:52:30 -08:00
import fs from 'fs/promises'
2024-02-29 17:02:56 -08:00
2024-02-22 07:53:51 -08:00
import type { Predicate } from '@actions/attest'
export type PredicateInputs = {
predicateType: string
predicate: string
predicatePath: string
}
2024-06-03 09:41:25 -07:00
const MAX_PREDICATE_SIZE_BYTES = 16 * 1024 * 1024
2024-02-22 07:53:51 -08:00
// Returns the predicate specified by the action's inputs. The predicate value
// may be specified as a path to a file or as a string.
2026-02-18 08:52:30 -08:00
export const predicateFromInputs = async (
inputs: PredicateInputs
): Promise<Predicate> => {
const { predicateType, predicate, predicatePath } = inputs
if (!predicateType) {
throw new Error('predicate-type must be provided')
}
2024-02-22 07:53:51 -08:00
if (!predicatePath && !predicate) {
2024-02-22 07:53:51 -08:00
throw new Error('One of predicate-path or predicate must be provided')
}
if (predicatePath && predicate) {
2024-02-29 17:02:56 -08:00
throw new Error('Only one of predicate-path or predicate may be provided')
}
2024-06-03 09:41:25 -07:00
let params: string = predicate
if (predicatePath) {
2026-02-18 08:52:30 -08:00
try {
await fs.access(predicatePath)
} catch {
2024-06-03 09:41:25 -07:00
throw new Error(`predicate file not found: ${predicatePath}`)
}
2026-02-18 08:52:30 -08:00
const stat = await fs.stat(predicatePath)
2024-06-03 09:41:25 -07:00
/* istanbul ignore next */
2026-02-18 08:52:30 -08:00
if (stat.size > MAX_PREDICATE_SIZE_BYTES) {
2024-06-03 09:41:25 -07:00
throw new Error(
`predicate file exceeds maximum allowed size: ${MAX_PREDICATE_SIZE_BYTES} bytes`
)
}
2026-02-18 08:52:30 -08:00
params = await fs.readFile(predicatePath, 'utf-8')
2024-06-03 09:41:25 -07:00
} else {
if (predicate.length > MAX_PREDICATE_SIZE_BYTES) {
throw new Error(
`predicate string exceeds maximum allowed size: ${MAX_PREDICATE_SIZE_BYTES} bytes`
)
}
params = predicate
}
2024-02-22 07:53:51 -08:00
return { type: predicateType, params: JSON.parse(params) }
}