/** * @module witness/space/readonly-space-wrapper * * Implements a read-only mounted space wrapper around an underlying IbGibSpaceAny. * Permits all read commands ('get', 'getDependencyGraph', 'latest', etc.) while * rejecting any write or mutation attempts ('put', 'delete', 'persistTransformResult') * with a fail-fast ReadOnlySpaceError. * * Does NOT require registration with Metaspace (zero bootstrap^gib.json side effects). */ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getGib } from '@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs'; import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { SpaceBase_V1, IbGibSpaceAny } from './space-base-v1.mjs'; import { getSpaceIb } from './space-helper.mjs'; import { IbGibSpaceData, IbGibSpaceOptionsCmd, IbGibSpaceOptionsIbGib, IbGibSpaceRel8ns, IbGibSpaceResultIbGib, } from './space-types.mjs'; const logalot = GLOBAL_LOG_A_LOT; export type AnySpaceOptionsIbGib = IbGibSpaceOptionsIbGib; export type AnySpaceResultIbGib = IbGibSpaceResultIbGib; /** * Dedicated error thrown whenever a write or delete operation is attempted * against a ReadOnlyMountedSpace. */ export class ReadOnlySpaceError extends Error { constructor(message: string) { super(message); this.name = 'ReadOnlySpaceError'; } } /** * Transient, read-only wrapper around any concrete IbGibSpaceAny (such as * InnerSpace_V1, NodeFilesystemSpace_V1, or NodeIndexedFilesystemSpace_V1). * * Provides strict CQRS read isolation: * - Read operations ('get', 'latest', 'getDependencyGraph') pass through directly to underlyingSpace. * - Write operations ('put', 'delete') throw fail-fast ReadOnlySpaceError. * - Does not require registration with Metaspace and causes zero updates to bootstrap^gib.json. */ export class ReadOnlyMountedSpace extends SpaceBase_V1 implements IbGibSpaceAny { protected override lc: string = `[${ReadOnlyMountedSpace.name}]`; public readonly underlyingSpace!: IbGibSpaceAny; constructor(underlyingSpace: IbGibSpaceAny) { if (!underlyingSpace) { throw new Error(`underlyingSpace is required in ReadOnlyMountedSpace (E: fc45ddc754c75caf1c231646239faafc)`); } const underlyingData = (underlyingSpace.data || {}) as any; const name = `${underlyingData.name || 'space'}_readonly_mount`; super( { ...underlyingData, name, classname: ReadOnlyMountedSpace.name, isReadOnly: true, catchAllErrors: false, }, underlyingSpace.rel8ns ? { ...underlyingSpace.rel8ns } : undefined ); this.underlyingSpace = underlyingSpace; this.initialized = (async () => { if (underlyingSpace.initialized) { await underlyingSpace.initialized; } if (!this.data) { throw new Error(`this.data is falsy in ReadOnlyMountedSpace (E: 8e315518a7e32e7e7e13197c304ae8d9)`); } if (!this.data.classname) { this.data.classname = ReadOnlyMountedSpace.name; } this.ib = getSpaceIb({ space: this, classname: ReadOnlyMountedSpace.name }); this.gib = await getGib({ ibGib: this }); })(); } protected override async initialize(): Promise { // SpaceBase_V1 constructor calls this.initialized = this.initialize() // before derived class constructor assignments execute. // True initialization chaining is executed in the constructor above. } /** * Intercepts witness calls to strictly block write and delete commands * before delegating read commands to the underlying space. */ protected override async witnessImpl(arg: AnySpaceOptionsIbGib): Promise { const lc = `${this.lc}[${this.witnessImpl.name}]`; const cmd = arg.data?.cmd; const cmdModifiers = arg.data?.cmdModifiers ?? []; if (cmd === IbGibSpaceOptionsCmd.put) { if (cmdModifiers.includes('can')) { return await this.canPut(arg); } throw new ReadOnlySpaceError(`${lc} Write operation ('put') forbidden on read-only mounted space (E: 417d3f309edd576ab108677a6a1333b4)`); } if (cmd === IbGibSpaceOptionsCmd.delete) { if (cmdModifiers.includes('can')) { return await this.canDelete(arg); } throw new ReadOnlySpaceError(`${lc} Delete operation ('delete') forbidden on read-only mounted space (E: a1fe6ae3e267b8c1fd9394cd8d1702aa)`); } // Delegate read command directly to the underlying space return await this.underlyingSpace.witness(arg); } protected override async getImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async canGetImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async getLatestAddrsImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async getLatestIbGibsImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async getTjpAddrsImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async getTjpIbGibsImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async getAddrsImpl(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async getDependencyGraph(arg: AnySpaceOptionsIbGib): Promise { return await this.underlyingSpace.witness(arg); } protected override async putImpl(_arg: AnySpaceOptionsIbGib): Promise { const lc = `${this.lc}[${this.putImpl.name}]`; throw new ReadOnlySpaceError(`${lc} Write operation ('put') forbidden on read-only mounted space (E: 2c9fe14a6b694214ceb01b9481f2e619)`); } protected override async canPutImpl(_arg: AnySpaceOptionsIbGib): Promise { return { ib: 'result can-put', gib: 'gib', data: { success: false, canPut: false, errors: [`ReadOnlySpaceError: canPut is false on read-only mounted space`], }, } as any; } protected override async deleteImpl(_arg: AnySpaceOptionsIbGib): Promise { const lc = `${this.lc}[${this.deleteImpl.name}]`; throw new ReadOnlySpaceError(`${lc} Delete operation ('delete') forbidden on read-only mounted space (E: 8e38493de9e79c0ccac8f5e19d612e45)`); } protected override async canDeleteImpl(_arg: AnySpaceOptionsIbGib): Promise { return { ib: 'result can-delete', gib: 'gib', data: { success: false, canDelete: false, errors: [`ReadOnlySpaceError: canDelete is false on read-only mounted space`], }, } as any; } protected override async persistOptsAndResultIbGibs(_arg: any): Promise { // Strict NO-OP: do not persist opts and results into read-only space } } /** * Creates a transient ReadOnlyMountedSpace wrapping an underlying space. */ export function createReadOnlyMountedSpace(underlyingSpace: IbGibSpaceAny): ReadOnlyMountedSpace { return new ReadOnlyMountedSpace(underlyingSpace); }