All files / src delta.ts

92.72% Statements 51/55
90.9% Branches 30/33
90% Functions 9/10
98.07% Lines 51/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 1466x 6x 6x     6x                                         6x               23x 23x 23x 23x 23x       6x               3x 3x 3x 3x 3x       6x           17x 17x 17x 17x 17x 17x       6x       3x       6x           3x 3x 3x 3x 3x 3x               1x       1x 1x 2x 1x   1x       1x 1x 1x             2x 2x 7x 4x 4x 4x 4x   3x     2x               6x        
import {randomUUID} from "crypto";
import Debug from 'debug';
import microtime from 'microtime';
import {PeerAddress} from "./peers";
import {CreatorID, DomainEntityID, HostID, PropertyID, Timestamp, TransactionID} from "./types";
const debug = Debug('rz:delta');
 
export type DeltaID = string;
 
export type PointerTarget = string | number | null;
 
type PointerV1 = {
  localContext: string;
  target: PointerTarget;
  targetContext?: string;
};
 
export type Scalar = string | number | null;
export type Reference = {
  [key: DomainEntityID]: PropertyID
};
 
export type PointersV2 = {
  [key: PropertyID]: Scalar | Reference
};
 
export class DeltaNetworkImageV1 {
  id: DeltaID;
  timeCreated: Timestamp;
  host: HostID;
  creator: CreatorID;
  pointers: PointerV1[];
 
  constructor({id, timeCreated, host, creator, pointers}: DeltaNetworkImageV1) {
    this.id = id;
    this.host = host;
    this.creator = creator;
    this.timeCreated = timeCreated;
    this.pointers = pointers;
  }
};
 
export class DeltaNetworkImageV2 {
  id: DeltaID;
  timeCreated: Timestamp;
  host: HostID;
  creator: CreatorID;
  pointers: PointersV2;
 
  constructor({id, timeCreated, host, creator, pointers}: DeltaNetworkImageV2) {
    this.id = id;
    this.host = host;
    this.creator = creator;
    this.timeCreated = timeCreated;
    this.pointers = pointers;
  }
};
 
export class DeltaV1 extends DeltaNetworkImageV1 {
  receivedFrom?: PeerAddress;
  timeReceived: Timestamp;
  transactionId?: TransactionID;
 
  constructor({id, timeCreated, host, creator, pointers}: Partial<DeltaNetworkImageV1>) {
    id = id ?? randomUUID();
    timeCreated = timeCreated ?? microtime.now();
    Iif (!host || !creator || !pointers) throw new Error('uninitializied values');
    super({id, timeCreated, host, creator, pointers});
    this.timeCreated = timeCreated;
    this.timeReceived = this.timeCreated;
  }
 
  toNetworkImage() {
    return new DeltaNetworkImageV1(this);
  }
 
  static fromNetworkImage(delta: DeltaNetworkImageV1) {
    return new DeltaV1(delta);
  }
}
 
export class DeltaV2 extends DeltaNetworkImageV2 {
  receivedFrom?: PeerAddress;
  timeReceived: Timestamp;
  transactionId?: TransactionID;
 
  constructor({id, timeCreated, host, creator, pointers}: Partial<DeltaNetworkImageV2>) {
    id = id ?? randomUUID();
    timeCreated = timeCreated ?? microtime.now();
    Iif (!host || !creator || !pointers) throw new Error('uninitializied values');
    super({id, timeCreated, host, creator, pointers});
    this.timeCreated = timeCreated;
    this.timeReceived = this.timeCreated;
  }
 
  toNetworkImage() {
    return new DeltaNetworkImageV2(this);
  }
 
  static fromNetworkImage(delta: DeltaNetworkImageV2) {
    return new DeltaV2(delta);
  }
 
  static fromV1(delta: DeltaV1) {
    const pointersV2: PointersV2 = {};
    for (const {localContext, target, targetContext} of delta.pointers) {
      if (targetContext && typeof target === "string") {
        pointersV2[localContext] = {[target]: targetContext};
      } else {
        pointersV2[localContext] = target;
      }
    }
 
    debug(`fromV1, pointers in: ${JSON.stringify(delta.pointers)}`);
    debug(`fromV1, pointers out: ${JSON.stringify(pointersV2)}`);
    return DeltaV2.fromNetworkImage({
      ...delta,
      pointers: pointersV2
    });
  }
 
  toV1() {
    const pointersV1: PointerV1[] = [];
    for (const [localContext, pointerTarget] of Object.entries(this.pointers)) {
      if (pointerTarget && typeof pointerTarget === "object") {
        const [obj] = Object.entries(pointerTarget)
        Iif (!obj) throw new Error("invalid pointer target");
        const [target, targetContext] = Object.entries(pointerTarget)[0];
        pointersV1.push({localContext, target, targetContext});
      } else {
        pointersV1.push({localContext, target: pointerTarget});
      }
    }
    return new DeltaV1({
      ...this,
      pointers: pointersV1
    });
  }
}
 
// Alias
export class Delta extends DeltaV1 {}
 
export type DeltaFilter = (delta: Delta) => boolean;