UNPKG

pipe-protocol

Version:

A protocol for large scale Interplanetary Intertool Agent Context

155 lines 4.56 kB
"use strict"; /** * @file IpfsClient Implementation * @version 1.0.0 * @status IN_DEVELOPMENT * @lastModified 2024-02-03 * * High-level IPFS client that provides application-specific operations * on top of the IpfsNode implementation. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.IpfsClient = void 0; const schema_1 = require("./schema"); const util_1 = require("util"); class IpfsClient { constructor(node) { this.encoder = new util_1.TextEncoder(); this.decoder = new util_1.TextDecoder(); this.pinnedCids = new Set(); this.node = node; } async init() { try { if (!this.node) { throw new Error('IPFS node not provided'); } await this.node.init(); } catch (error) { console.error('Error initializing IPFS client:', error); throw error; } } async publish(record) { try { const validatedRecord = schema_1.PipeRecordSchema.parse(record); const data = this.encoder.encode(JSON.stringify(validatedRecord)); const cid = await this.node.add(data); if (record.pinned) { await this.pin(cid, record.scope); } return { ...validatedRecord, cid }; } catch (error) { console.error('Error publishing record:', error); throw error; } } async fetch(cid, scope) { try { const data = await this.node.get(cid); if (!data) return null; const content = JSON.parse(this.decoder.decode(data)); return { cid, content, type: 'data', scope, accessPolicy: { hiddenFromLLM: false } }; } catch (error) { console.error('Error fetching record:', error); return null; } } async pin(cid, _scope) { try { // Verify the data exists by trying to get it const data = await this.node.get(cid); if (!data) throw new Error('Data not found'); await this.node.pin(cid); this.pinnedCids.add(cid); } catch (error) { console.error('Error pinning data:', error); throw error; } } async unpin(cid, _scope) { try { await this.node.unpin(cid); this.pinnedCids.delete(cid); } catch (error) { console.error('Error unpinning data:', error); throw error; } } async replicate(cid, _fromScope, _toScope) { try { // Get the data from the source scope const data = await this.node.get(cid); if (!data) throw new Error('Data not found'); // Add it back to create a new copy const newCid = await this.node.add(data); if (newCid !== cid) { throw new Error('Data integrity check failed during replication'); } } catch (error) { console.error('Error replicating data:', error); throw error; } } async stop() { await this.node.stop(); } getStatus() { return { localNode: true, publicNode: false // Default to offline mode }; } async getNodeInfo(_scope) { const peerId = await this.node.getPeerId(); return { peerId: peerId?.toString() || 'unknown' }; } async getConfiguration(_scope) { const peerId = await this.node.getPeerId(); const addrs = this.node.getMultiaddrs(); return { peerId: peerId?.toString() || 'unknown', addrs }; } async getStorageMetrics(_scope) { return { totalSize: 0, // Implement actual size calculation if needed numObjects: this.pinnedCids.size }; } async getPinnedCids(_scope) { return Array.from(this.pinnedCids); } async exportData(cid) { const data = await this.node.get(cid); if (!data) throw new Error('Data not found'); return { data, cid }; } async importData(data) { return await this.node.add(data); } } exports.IpfsClient = IpfsClient; //# sourceMappingURL=ipfsClient.js.map