import type { BuildPluginPins, BuildStartParams } from '@stim-cli/core/protocol'; import { isJsonObject } from '@stim-cli/core/state'; import { iosPlugin } from './ios-plugins.ts'; import type { IosPluginCheckout, IosPluginSource } from './plugin-ios.ts'; const CHECKOUT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/; const SIBLING = /^(?:\.\.\/){1,16}(?:(?!\.\.?\/)[A-Za-z0-9._-]{1,100}\/){0,16}(?!\.\.?$)[A-Za-z0-9._-]{1,100}$/; /** * The checkout layout of a plugin's offloaded build as a build machine accepts it: a plain ASCII folder name, and a * relative path to the second checkout made only of leading `../` segments and plain names; null otherwise. */ export function pluginCheckoutParam(value: unknown): IosPluginCheckout | null { if (!isJsonObject(value)) return null; const { name, sibling = null } = value; if (typeof name !== 'string' || !CHECKOUT_NAME.test(name)) return null; if (sibling !== null && (typeof sibling !== 'string' || !SIBLING.test(sibling))) return null; return { name, sibling }; } function namedPlugin( value: unknown, method: 'build.offer' | 'build.start', ): { source: IosPluginSource; version: unknown; fields: Record } | string { const source = isJsonObject(value) && typeof value.id === 'string' ? iosPlugin(value.id) : null; if (!source || !isJsonObject(value)) return `${method} names no plugin this Stim builds.`; return { source, version: value.version, fields: value }; } /** The plugin a `build.offer` names, with its pins as that plugin reads them, or the refusal's message. */ export function pluginOfferParam(value: unknown): { source: IosPluginSource; plugin: BuildPluginPins } | string { const named = namedPlugin(value, 'build.offer'); if (typeof named === 'string') return named; const { source, version, fields } = named; const pins = source.offload.pins(fields.pins); return pins && typeof version === 'string' ? { source, plugin: { id: source.id, version, pins } } : source.offload.malformed.offer; } /** The plugin build a `build.start` names, read as `pluginOfferParam` reads the offer's, or the refusal's message. */ export function pluginStartParam( value: unknown, ): { source: IosPluginSource; plugin: NonNullable } | string { const named = namedPlugin(value, 'build.start'); if (typeof named === 'string') return named; const { source, version, fields } = named; const pins = source.offload.pins(fields.pins); const payload = source.offload.payload(fields.payload); const checkout = pluginCheckoutParam(fields.checkout); return pins && payload && checkout && typeof version === 'string' ? { source, plugin: { id: source.id, version, pins, payload, checkout } } : source.offload.malformed.start; }