@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
44 lines • 1.67 kB
JavaScript
import { Utils } from '../utils.js';
export class LogSyncer {
constructor(ipfsLoader) {
this.ipfsLoader = ipfsLoader;
}
async syncFullLog(streamID, tip) {
if (!tip) {
return this._syncGenesisLog(streamID);
}
const log = await this._syncLogHelper(streamID, tip, null);
if (log.commits.length == 0 || !log.commits[0].cid.equals(streamID.cid)) {
return this._syncGenesisLog(streamID);
}
return log;
}
async _syncGenesisLog(streamID) {
const genesisCommitData = await Utils.getCommitData(this.ipfsLoader, streamID.cid, streamID);
return { commits: [genesisCommitData], timestampStatus: 'pending' };
}
async syncLogUntilMatch(streamID, tip, existingCommits) {
return this._syncLogHelper(streamID, tip, existingCommits);
}
async _syncLogHelper(streamID, tip, existingCommits) {
const syncedLog = [];
let curCid = tip;
while (curCid && !this._logIncludes(curCid, existingCommits)) {
const commitData = await Utils.getCommitData(this.ipfsLoader, curCid, streamID);
const prevCid = commitData.commit.prev;
if (!prevCid && existingCommits) {
return { commits: [], timestampStatus: 'pending' };
}
syncedLog.push(commitData);
curCid = prevCid;
}
return { commits: syncedLog.reverse(), timestampStatus: 'pending' };
}
_logIncludes(cid, log) {
if (!log) {
return false;
}
return log.find((entry) => entry.equals(cid)) != null;
}
}
//# sourceMappingURL=log-syncer.js.map