/** * @module sync-peer-http-receiver-v1 */ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../../../core-constants.mjs'; import { SyncPeer_V1 } from '../sync-peer-v1.mjs'; import { SyncSagaContextIbGib_V1 } from '../../sync-saga-context/sync-saga-context-types.mjs'; import { authenticateContextIntrinsically } from '../../sync-saga-context/sync-saga-context-helpers.mjs'; import { IbGibSpaceAny } from '../../../witness/space/space-base-v1.mjs'; import { createInMemoryTempSpace, putInSpace_dnasThenNonDnas } from '../../sync-helpers.mjs'; import { InitializeSyncPeerHttpReceiverOpts, SyncPeerHttpReceiverData_V1, SyncPeerHttpReceiverRel8ns_V1, SyncPeerHttpReceiverIbGib_V1, ConnectSyncPeerHttpReceiverOpts } from './sync-peer-http-receiver-types.mjs'; import { putInSpace, registerNewIbGib } from '../../../witness/space/space-helper.mjs'; import { KeystoneIbGib_V1 } from '../../../keystone/keystone-types.mjs'; const logalot = GLOBAL_LOG_A_LOT; /** * Http Receiver Peer implementation. * Acts as the Symmetric Server Node. Accepts requests, routes to Receiver Coordinator. */ export class SyncPeerHttpReceiver_V1 extends SyncPeer_V1 implements SyncPeerHttpReceiverIbGib_V1 { protected override lc: string = `[${SyncPeerHttpReceiver_V1.name}]`; override get classname(): string { return SyncPeerHttpReceiver_V1.name; } declare data: SyncPeerHttpReceiverData_V1 | undefined; declare rel8ns: SyncPeerHttpReceiverRel8ns_V1 | undefined; constructor( initialData: SyncPeerHttpReceiverData_V1, initialRel8ns?: SyncPeerHttpReceiverRel8ns_V1, ) { super(initialData, initialRel8ns); } protected override async preConnectCheck(opts: ConnectSyncPeerHttpReceiverOpts): Promise { const lc = `${this.lc}[${this.preConnectCheck.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 7cf8f805a31877fddf25b0ee09e49e26)`); } throw new Error(`not implemented (E: 4163e88d42f811da8b699cb6f7554a26)`); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } protected override async connectImpl(opts: ConnectSyncPeerHttpReceiverOpts): Promise { const lc = `${this.lc}[${this.connectImpl.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 3069f8c66239b65a2500cbe8e5a7c826)`); } // Stub implementation for now. throw new Error(`not implemented (E: 48a5b8714d6861272510fc8c3cfef726)`); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } protected override async postEstablishToReceiver({ newSenderIdentity, sessionIdentity, }: { newSenderIdentity: KeystoneIbGib_V1; sessionIdentity: KeystoneIbGib_V1; }): Promise { const lc = `${this.lc}[${this.postEstablishToReceiver.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: f8d7c6b5a4e3d2c1b0a9f8e7d6c5b4a3)`); } throw new Error(`not implemented for HTTP receiver peer yet (E: 360af8e7331f848d48c97a6b19662726)`); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } protected async ensureLocalTempSpace(): Promise { const lc = `${this.lc}[${this.ensureLocalTempSpace.name}]`; try { if (!this.opts) { throw new Error(`opts not initialized. (E: 3a77d1f466b92cc7940c0f241ac6c926)`); } if (!this.opts.localTempSpace) { const uuid = crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36); const tempSpaceName = `tmp_sync_recv_${uuid.substring(0, 8)}`; if (this.opts.useInMemoryTempSpace) { this.opts.localTempSpace = await createInMemoryTempSpace({ name: tempSpaceName }); } else if (this.opts.localMetaspace) { const localTempSpace = await this.opts.localMetaspace.createNewLocalSpace({ opts: { allowCancel: false, spaceName: tempSpaceName, getFnPrompt: this.opts.localMetaspace.getFnPrompt!, logalot } }); if (!localTempSpace) { throw new Error(`couldn't create a temp space (E: 72bfda06f23f46f3a7eb6832dbad75ef)`); } await localTempSpace.initialized; this.opts.localTempSpace = localTempSpace; } else { this.opts.localTempSpace = await createInMemoryTempSpace({ name: tempSpaceName }); } } return this.opts.localTempSpace; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } protected override async sendContextRequest(context: SyncSagaContextIbGib_V1): Promise { const lc = `${this.lc}[${this.sendContextRequest.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 5297013216ae491894600b5a81115d26)`); } // This method might not be directly called on the receiver in the same way // as it is on the sender, or it handles the internal routing for the HTTP request. // When an HTTP Endpoint receives a POST, it deserializes the context and triggers this. throw new Error(`Not implemented (E: c56d5d6799b76bd8fd661846f6b9ee26)`); } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } /** * Entry point from the HTTP Endpoint Adapter (e.g. Express POST route) */ public async handleIncomingSyncRequest({ context, payloadIbGibsControl = [], }: { context: SyncSagaContextIbGib_V1; payloadIbGibsControl?: IbGib_V1[]; }): Promise { const lc = `${this.lc}[${this.handleIncomingSyncRequest.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 1de30d7db7e404ddf59eeed22a3bde26)`); } if (!this.opts) { throw new Error(`opts not initialized. (E: 56f585e25a54489280c9ba1393a46226)`); } const { localCoordinator, localMetaspace, controlSpace, domainSpace } = this.opts; const localTempSpace = await this.ensureLocalTempSpace(); // Put control ibgibs into durable controlSpace const allControlIbGibs = [context, ...payloadIbGibsControl]; if (context.signedSessionIdentity) { allControlIbGibs.push(context.signedSessionIdentity); } for (const ibGib of allControlIbGibs) { await putInSpace({ space: controlSpace, ibGibs: [ibGib] }); await registerNewIbGib({ space: controlSpace, ibGib }); } // Auth verification (Bill architecture: turn-based context signing) const authErrors = await authenticateContextIntrinsically({ context, controlSpace, }); if (authErrors.length > 0) { throw new Error(`Context authentication failed: ${authErrors.join(', ')} (E: fc9e9d90af412ab0f2b4aed46d4aab10)`); } // Put domain payloads into temp space if (context.payloadIbGibsDomain && context.payloadIbGibsDomain.length > 0) { await putInSpace_dnasThenNonDnas({ space: localTempSpace, ibGibs: context.payloadIbGibsDomain }); } const responseCtx = await localCoordinator.continueSync({ sagaContext: context, metaspace: localMetaspace, controlSpace, domainSpace, myTempSpace: localTempSpace, peer: this, }); return responseCtx || undefined; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } finally { if (logalot) { console.log(`${lc} complete.`); } } } }