All files / omnium-gatherum/src/controllers/caplet types.ts

100% Statements 11/11
100% Branches 7/7
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                3x 43x             3x                           3x 40x       3x         3x                                   28x                       3x 2x                                                              
import { define, is, object, string } from '@metamask/superstruct';
import type { Infer } from '@metamask/superstruct';
import semverValid from 'semver/functions/valid';
 
/**
 * Unique identifier for a Caplet (any non-empty ASCII string without whitespace).
 */
export type CapletId = string;
 
/**
 * Validate CapletId format.
 * Requires non-empty ASCII string with no whitespace.
 *
 * @param value - The value to validate.
 * @returns True if valid CapletId format.
 */
export const isCapletId = (value: unknown): value is CapletId =>
  typeof value === 'string' &&
  value.length > 0 &&
  // All ASCII characters except control characters and whitespace.
  // 0x20 is the space character.
 
  /^[\x21-\x7E]+$/u.test(value);
 
export const CapletIdStruct = define<CapletId>('CapletId', isCapletId);
 
/**
 * Semantic version string (e.g., "1.0.0").
 */
export type SemVer = string;
 
/**
 * Validate SemVer format using the semver package.
 * Requires strict format without 'v' prefix (e.g., "1.0.0" not "v1.0.0").
 *
 * @param value - The value to validate.
 * @returns True if valid SemVer format.
 */
export const isSemVer = (value: unknown): value is SemVer =>
  typeof value === 'string' &&
  // semver.valid() is lenient and strips 'v' prefix, so check that cleaned value equals original
  semverValid(value) === value;
 
export const SemVerStruct = define<SemVer>('SemVer', isSemVer);
 
/**
 * Superstruct schema for validating CapletManifest objects.
 */
export const CapletManifestStruct = object({
  id: CapletIdStruct,
  name: string(),
  version: SemVerStruct,
  bundleSpec: string(),
});
 
/**
 * Metadata that defines a Caplet's identity, dependencies, and capabilities.
 */
export type CapletManifest = Infer<typeof CapletManifestStruct>;
 
/**
 * Type guard for CapletManifest validation.
 *
 * @param value - The value to validate.
 * @returns True if the value is a valid CapletManifest.
 */
export const isCapletManifest = (value: unknown): value is CapletManifest =>
  is(value, CapletManifestStruct);
 
/**
 * Assert that a value is a valid CapletManifest.
 *
 * @param value - The value to validate.
 * @throws If the value is not a valid CapletManifest.
 */
export function assertCapletManifest(
  value: unknown,
): asserts value is CapletManifest {
  if (!isCapletManifest(value)) {
    throw new Error('Invalid CapletManifest');
  }
}
 
/**
 * Record for an installed Caplet.
 * Combines manifest with runtime identifiers.
 */
export type InstalledCaplet = {
  manifest: CapletManifest;
  subclusterId: string;
  rootKref: string;
  installedAt: number;
};
 
/**
 * Result of installing a Caplet.
 */
export type InstallResult = {
  capletId: CapletId;
  subclusterId: string;
};
 
/**
 * Result of launching a subcluster.
 * This is the interface expected by CapletController's deps.
 */
export type LaunchResult = {
  subclusterId: string;
  rootKref: string;
};