UNPKG

jinaga

Version:

Data management for web and mobile applications.

76 lines 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serializeGraph = exports.GraphSerializer = void 0; class GraphSerializer { constructor(write) { this.write = write; this.index = 0; this.indexByFactReference = {}; this.publicKeys = []; } serialize(result) { // Write the facts for (const fact of result) { // Skip facts that have already been written const key = fact.fact.type + ":" + fact.fact.hash; if (this.indexByFactReference.hasOwnProperty(key)) { continue; } // Write any new public keys for (const signature of fact.signatures) { if (!this.publicKeys.includes(signature.publicKey)) { const pkIndex = this.publicKeys.length; const publicKey = JSON.stringify(signature.publicKey); this.write(`PK${pkIndex.toString()}\n${publicKey}\n\n`); this.publicKeys.push(signature.publicKey); } } // Write the fact const factType = JSON.stringify(fact.fact.type); const predecessorIndexes = JSON.stringify(this.getPredecessorIndexes(fact.fact.predecessors)); const factFields = JSON.stringify(fact.fact.fields); let output = `${factType}\n${predecessorIndexes}\n${factFields}`; // Write the signatures for (const signature of fact.signatures) { const publicKeyIndex = this.publicKeys.indexOf(signature.publicKey); const publicKey = `PK${publicKeyIndex.toString()}`; const signatureString = JSON.stringify(signature.signature); output += `\n${publicKey}\n${signatureString}`; } output += "\n\n"; this.write(output); this.indexByFactReference[key] = this.index; this.index++; } } getPredecessorIndexes(predecessors) { const result = {}; for (const role in predecessors) { const reference = predecessors[role]; if (Array.isArray(reference)) { result[role] = reference.map(r => this.getFactIndex(r)); } else { result[role] = this.getFactIndex(reference); } } return result; } getFactIndex(reference) { const key = reference.type + ":" + reference.hash; if (!this.indexByFactReference.hasOwnProperty(key)) { throw new Error(`Fact reference not found in graph: ${key}`); } return this.indexByFactReference[key]; } } exports.GraphSerializer = GraphSerializer; function serializeGraph(graph) { const serializedData = []; const serializer = new GraphSerializer(chunk => serializedData.push(chunk)); serializer.serialize(graph); const body = serializedData.join(''); return body; } exports.serializeGraph = serializeGraph; //# sourceMappingURL=serializer.js.map