/** * @module package-galaxy-helpers * * Helper functions for Package Galaxy Archetype (Class II: Registry / Milestones). * Space-based and isomorphic. */ import { UUID_REGEXP } from '@ibgib/helper-gib/dist/constants.mjs'; import { getUUID, extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { ROOT } from '@ibgib/ts-gib/dist/V1/constants.mjs'; import { Factory_V1 } from '@ibgib/ts-gib/dist/V1/factory.mjs'; import { mut8 } from '@ibgib/ts-gib/dist/V1/transforms/mut8.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { Ib } from '@ibgib/ts-gib/dist/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { getIbGibFromSpace, isSpaceQualifiedAddr, resolveLatestIbGibInSpace } from '../../common/other/ibgib-helper.mjs'; import { MetaspaceService } from '../../witness/space/metaspace/metaspace-types.mjs'; import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs'; import { putInSpace } from '../../witness/space/space-helper.mjs'; import { getSpaceIdFromIb } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs'; import { GalaxyPresetType, GALAXY_PRESET_CONFIGS } from '../galaxy/galaxy-configs.mjs'; import { GalaxyPolicy } from '../galaxy/galaxy-types.mjs'; import { PACKAGE_GALAXY_ATOM, PACKAGE_GALAXY_REL8N_NAME, PACKAGE_DEFAULT_BRANCH_NAME, PACKAGE_DEFAULT_GALAXY_NAME, } from './package-galaxy-constants.mjs'; import { PackageGalaxyIbGib_V1, PackageGalaxyData_V1, PackageGalaxyIb_V1, PackageGalaxyRel8ns_V1, } from './package-galaxy-types.mjs'; const lcFile = `[package-galaxy-helpers]`; const logalot = GLOBAL_LOG_A_LOT; /** * Generates the ib string for a Package Galaxy. */ export function getPackageGalaxyIb({ galaxyId }: { galaxyId: string }): Ib { const lc = `[${getPackageGalaxyIb.name}]`; try { if (!UUID_REGEXP.test(galaxyId)) { throw new Error(`invalid galaxyId. Must match UUID_REGEXP: ${UUID_REGEXP.source} (E: e2e68449c25672a9b3f18e9d5041a826)`); } return `${PACKAGE_GALAXY_ATOM} ${galaxyId}`; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Parses an ib string into PackageGalaxyIb_V1. */ export function parsePackageGalaxyIb({ ib }: { ib: Ib }): PackageGalaxyIb_V1 { const lc = `[${parsePackageGalaxyIb.name}]`; try { const pieces = ib.split(' '); if (pieces.length !== 2) { throw new Error(`invalid ib. expected 2 pieces: atom galaxyId (E: 8f4c2e1b9a7d4352b069d2a451e73826)`); } const [atom, galaxyId] = pieces; if (atom !== PACKAGE_GALAXY_ATOM) { throw new Error(`invalid atom: expected ${PACKAGE_GALAXY_ATOM}, got ${atom} (E: a3f7e1b9c2d54860b069e4a251f84826)`); } if (!UUID_REGEXP.test(galaxyId)) { throw new Error(`invalid galaxyId. Must match UUID_REGEXP: ${UUID_REGEXP.source} (E: c2f4a1e9b8d74365b069f1a351e95826)`); } return { atom, galaxyId }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Creates genesis PackageGalaxyIbGib_V1 in space. */ export async function createPackageGalaxyIbGib({ galaxyName = PACKAGE_DEFAULT_GALAXY_NAME, defaultBranch = PACKAGE_DEFAULT_BRANCH_NAME, space, metaspace, sqBranchAddr, cosmosName, policy, }: { galaxyName?: string; defaultBranch?: string; space: IbGibSpaceAny; metaspace: MetaspaceService; sqBranchAddr?: string; cosmosName?: string; policy?: GalaxyPolicy; }): Promise { const lc = `${lcFile}[${createPackageGalaxyIbGib.name}]`; try { if (logalot) { console.log(`${lc} starting for galaxy '${galaxyName}'... (I: 4f1a2c3b5d6e7f80)`); } if (sqBranchAddr && !isSpaceQualifiedAddr(sqBranchAddr)) { throw new Error(`invalid sqBranchAddr (${sqBranchAddr}) (E: 8a2f4c1e9b7d4352a069d2a451e73826)`); } const spaceId = getSpaceIdFromIb(space.ib); const resolvedPolicy: GalaxyPolicy = policy || { topology: 'linear', role: 'intrinsic', storageMode: 'blob-chunked', defaultBranch, }; const galaxyId = await getUUID(); const ib = getPackageGalaxyIb({ galaxyId }); const data: PackageGalaxyData_V1 = { galaxyName, cosmosName, discriminator: GalaxyPresetType.package, policy: resolvedPolicy, mySpaceId: spaceId, trunkBranchSqAddr: sqBranchAddr, '$@branch': sqBranchAddr ? [sqBranchAddr] : [], packages: {}, }; const resFactory = await Factory_V1.firstGen({ ib, parentIbGib: ROOT, data, tjp: { timestamp: true }, dna: true, nCounter: true, }); const galaxyIbGib = resFactory.newIbGib as PackageGalaxyIbGib_V1; await putInSpace({ ibGib: galaxyIbGib, space }); if (metaspace) { await metaspace.getSpecialIbGib({ type: PACKAGE_GALAXY_ATOM, initialize: true, space, }); await metaspace.rel8ToSpecialIbGib({ type: PACKAGE_GALAXY_ATOM, rel8nName: PACKAGE_GALAXY_REL8N_NAME, ibGibsToRel8: [galaxyIbGib], space, linked: true, }); } if (logalot) { console.log(`${lc} created genesis PackageGalaxyIbGib: ${galaxyIbGib.ib}^${galaxyIbGib.gib}`); } return galaxyIbGib; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: b1e7a4f9c2d54860a069f4a251e84826)`); throw error; } } /** * Retrieves authoritative PackageGalaxyIbGib_V1 from space. */ export async function getPackageGalaxyIbGib({ space, metaspace, galaxyAddr, }: { space: IbGibSpaceAny; metaspace?: MetaspaceService; galaxyAddr?: string; }): Promise { const lc = `${lcFile}[${getPackageGalaxyIbGib.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 5c2a1b3d4e6f7a80)`); } let baseGalaxyIbGib: PackageGalaxyIbGib_V1 | undefined; if (galaxyAddr) { baseGalaxyIbGib = await getIbGibFromSpace({ addr: galaxyAddr, space, }); } else if (metaspace) { const specialIbGibs = await metaspace.getSpecialRel8dIbGibs({ type: PACKAGE_GALAXY_ATOM, rel8nName: PACKAGE_GALAXY_REL8N_NAME, space, }); baseGalaxyIbGib = specialIbGibs?.[0]; } if (!baseGalaxyIbGib) { return undefined; } return await resolveLatestIbGibInSpace({ ibGib: baseGalaxyIbGib, space, }); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: d3f2a1b4c5e67a80b069d2a451e73826)`); throw error; } } /** * Updates a PackageGalaxyIbGib_V1 in space with patched data. */ export async function updatePackageGalaxyIbGib({ space, metaspace, galaxyIbGib, dataToUpdate, }: { space: IbGibSpaceAny; metaspace?: MetaspaceService; galaxyIbGib: PackageGalaxyIbGib_V1; dataToUpdate: Partial; }): Promise { const lc = `${lcFile}[${updatePackageGalaxyIbGib.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 6d3f2a1b4c5e7a80)`); } const latestGalaxy = await resolveLatestIbGibInSpace({ ibGib: galaxyIbGib, space, }) || galaxyIbGib; const resMut8 = await mut8({ src: latestGalaxy, dataToAddOrPatch: dataToUpdate, dna: true, nCounter: true, }); const evolvedGalaxy = resMut8.newIbGib as PackageGalaxyIbGib_V1; await putInSpace({ ibGib: evolvedGalaxy, space }); if (metaspace) { await metaspace.rel8ToSpecialIbGib({ type: PACKAGE_GALAXY_ATOM, rel8nName: PACKAGE_GALAXY_REL8N_NAME, ibGibsToRel8: [evolvedGalaxy], space, linked: true, }); } return evolvedGalaxy; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: e4a2c1b3d5f67a80b069e4a251f84826)`); throw error; } }