UNPKG

@ceramicnetwork/core

Version:

Typescript implementation of the Ceramic protocol

55 lines 2.4 kB
import { ServiceMetrics as Metrics } from '@ceramicnetwork/observability'; import { EnvironmentUtils, toCID, } from '@ceramicnetwork/common'; import { PinningAggregation } from '@ceramicnetwork/pinning-aggregation'; import { PinStore } from './pin-store.js'; import { IpfsPinning } from '@ceramicnetwork/pinning-ipfs-backend'; import { StreamStateStore } from './stream-state-store.js'; import { IPFS_CACHE_HIT, IPFS_CACHE_MISS } from './ipld-records-cache.js'; const IPFS_GET_TIMEOUT = 60000; export class PinStoreFactory { constructor(ipfs, ipldRecordsCache, repository, props, logger) { this.ipfs = ipfs; this.ipldRecordsCache = ipldRecordsCache; this.repository = repository; this.logger = logger; this.pinningEndpoints = props.pinningEndpoints && props.pinningEndpoints.length > 0 ? props.pinningEndpoints : ['ipfs+context']; this.pinningBackends = props.pinningBackends && props.pinningBackends.length > 0 ? props.pinningBackends : [IpfsPinning]; this._stateStore = new StreamStateStore(); } createPinStore() { const ipfs = this.ipfs; const pinning = PinningAggregation.build(ipfs, this.pinningEndpoints, this.pinningBackends); const retrieve = async (cid) => { const fromCache = this.ipldRecordsCache.get(cid); if (fromCache) { Metrics.count(IPFS_CACHE_HIT, 1); return fromCache.record; } Metrics.count(IPFS_CACHE_MISS, 1); const blob = await ipfs.dag.get(cid, { timeout: IPFS_GET_TIMEOUT, offline: EnvironmentUtils.useRustCeramic(), }); if (blob && blob.value) { const record = blob.value; this.ipldRecordsCache.setRecord(cid, record); return record; } return undefined; }; const resolve = async (path) => { return toCID((await ipfs.dag.resolve(path, { offline: EnvironmentUtils.useRustCeramic(), })).cid.toString()); }; const loadStream = this.repository.load.bind(this.repository); return new PinStore(this._stateStore, pinning, retrieve, resolve, loadStream); } } //# sourceMappingURL=pin-store-factory.js.map