Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@codesandbox/crdt-tree

Package Overview
Dependencies
Maintainers
6
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codesandbox/crdt-tree - npm Package Compare versions

Comparing version 1.1.4 to 1.2.0

39

dist/crdt-tree.cjs.development.js

@@ -253,3 +253,9 @@ 'use strict';

var State = /*#__PURE__*/function () {
function State() {
function State(options) {
var _options$conflictHand;
if (options === void 0) {
options = {};
}
/** A list of `LogOpMove` in descending timestamp order */

@@ -259,3 +265,7 @@ this.operationLog = [];

this.tree = new Tree();
this.tree = new Tree(); // Default to not handling conflict
this.conflictHandler = (_options$conflictHand = options.conflictHandler) != null ? _options$conflictHand : function () {
return false;
};
}

@@ -326,3 +336,3 @@ /** Insert a log entry to the top of the log */

// `oldNode` records the previous parent and metadata of c.
var oldNode = this.tree.get(op.id); // ensures no cycles are introduced. If the node c
var oldNode = this.tree.get(op.id); // ensures no cycles are introduced. If the node c
// is being moved, and c is an ancestor of the new parent

@@ -338,2 +348,11 @@ // newp, then the tree is returned unmodified, ie the operation

};
} // ignores operations that produce conflicts according to the
// custom conflict handler.
if (this.conflictHandler(op, this.tree)) {
return {
op: op,
oldNode: oldNode
};
} // Otherwise, the tree is updated by removing c from

@@ -377,12 +396,12 @@ // its existing parent, if any, and adding the new

var TreeReplica = /*#__PURE__*/function () {
function TreeReplica(authorId) {
/** The Tree state */
this.state = new State();
function TreeReplica(authorId, options) {
if (options === void 0) {
options = {};
}
/** Mapping of replicas and their latest time */
this.latestTimeByReplica = new Map();
/** A tree structure that represents the current state of the tree */
this.time = new Clock(authorId);
this.state = new State(options);
this.tree = this.state.tree;
this.time = new Clock(authorId);
}

@@ -389,0 +408,0 @@ /** Get a node by its id */

@@ -1,2 +0,2 @@

"use strict";var t;Object.defineProperty(exports,"__esModule",{value:!0}),(t=exports.Ordering||(exports.Ordering={}))[t.Equal=0]="Equal",t[t.Greater=1]="Greater",t[t.Less=2]="Less";var e=function(){function t(t,e){void 0===e&&(e=0),this.actorId=t,this.counter=e}var e=t.prototype;return e.inc=function(){return new t(this.actorId,this.counter+1)},e.tick=function(){return this.counter+=1,new t(this.actorId,this.counter)},e.merge=function(e){return new t(this.actorId,Math.max(this.counter,e.counter))},e.compare=function(t){return this.counter>t.counter?exports.Ordering.Greater:this.counter<t.counter?exports.Ordering.Less:this.actorId>t.actorId?exports.Ordering.Greater:this.actorId<t.actorId?exports.Ordering.Less:exports.Ordering.Equal},e.valueOf=function(){return this.toString()},e.toString=function(){return String(this.counter).padStart(10,"0")+":"+this.actorId},t}();function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,i=new Array(e);r<e;r++)i[r]=t[r];return i}function i(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(i)return(i=i.call(t)).next.bind(i);if(Array.isArray(t)||(i=function(t,e){if(t){if("string"==typeof t)return r(t,void 0);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?r(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n=function(){function t(){this.nodes=new Map,this.children=new Map,this.size=this.nodes.size}var e=t.prototype;return e.remove=function(t){var e=this.nodes.get(t);if(e){var r=this.children.get(e.parentId);r&&(r.delete(t),0===r.size&&this.children.delete(e.parentId),this.nodes.delete(t))}},e.addNode=function(t,e){var r=this.children.get(e.parentId);r||(r=new Set,this.children.set(e.parentId,r)),r.add(t),this.nodes.set(t,e)},e.get=function(t){return this.nodes.get(t)},e.isAncestor=function(t,e){for(var r,i=t;r=this.get(i);){if(r.parentId===e)return!0;i=r.parentId}return!1},e.printNode=function(t,e){var r;void 0===e&&(e=0);var i=this.get(t),n=t+" "+(i?""+JSON.stringify(i.metadata):""),o=" ".repeat(2*e);console.log(o+n);for(var a=0,s=Array.from(null!=(r=this.children.get(t))?r:[]);a<s.length;a++)this.printNode(s[a],e+1)},t}(),o=function(t,e){this.parentId=t,this.metadata=e},a=function(){function t(){this.operationLog=[],this.tree=new n}var e=t.prototype;return e.addLogEntry=function(t){this.operationLog.unshift(t)},e.applyOp=function(t){if(0===this.operationLog.length){var e=this.doOperation(t);this.addLogEntry(e)}else{var r=this.operationLog[0].op;if(t.timestamp===r.timestamp&&console.log("op with timestamp equal to previous op ignored. (not applied). Every op must have a unique timestamp."),t.timestamp<r.timestamp){var i=this.operationLog.shift();this.undoOp(i),this.applyOp(t),this.redoOp(i)}if(t.timestamp>r.timestamp){var n=this.doOperation(t);this.addLogEntry(n)}}},e.applyOps=function(t){for(var e,r=i(t);!(e=r()).done;)this.applyOp(e.value)},e.doOperation=function(t){var e=this.tree.get(t.id);if(t.id===t.parentId||this.tree.isAncestor(t.parentId,t.id))return{op:t,oldNode:e};this.tree.remove(t.id);var r=new o(t.parentId,t.metadata);return this.tree.addNode(t.id,r),{op:t,oldNode:e}},e.undoOp=function(t){if(this.tree.remove(t.op.id),t.oldNode){var e=new o(t.oldNode.parentId,t.oldNode.metadata);this.tree.addNode(t.op.id,e)}},e.redoOp=function(t){var e=this.doOperation(t.op);this.addLogEntry(e)},t}(),s=function(){function t(t){this.state=new a,this.latestTimeByReplica=new Map,this.tree=this.state.tree,this.time=new e(t)}var r=t.prototype;return r.get=function(t){return this.tree.get(t)},r.opMove=function(t,e,r){return{timestamp:this.time.inc(),metadata:e,id:t,parentId:r}},r.opMoves=function(t){for(var e,r=[],n=i(t);!(e=n()).done;){var o=e.value;r.push({timestamp:this.time.tick(),id:o[0],metadata:o[1],parentId:o[2]})}return r},r.applyOp=function(t){var e;this.time=this.time.merge(t.timestamp);var r=t.timestamp.actorId,i=null!=(e=this.latestTimeByReplica.get(r))?e:0;t.timestamp<=i?(console.log("Clock not increased, current timestamp "+i.toString()+", provided is "+t.timestamp.toString()+"."),console.log("Dropping operation.")):this.latestTimeByReplica.set(r,t.timestamp),this.state.applyOp(t)},r.applyOps=function(t){for(var e,r=i(t);!(e=r()).done;)this.applyOp(e.value)},t}();exports.Clock=e,exports.State=a,exports.Tree=n,exports.TreeNode=o,exports.TreeReplica=s;
"use strict";var t;Object.defineProperty(exports,"__esModule",{value:!0}),(t=exports.Ordering||(exports.Ordering={}))[t.Equal=0]="Equal",t[t.Greater=1]="Greater",t[t.Less=2]="Less";var e=function(){function t(t,e){void 0===e&&(e=0),this.actorId=t,this.counter=e}var e=t.prototype;return e.inc=function(){return new t(this.actorId,this.counter+1)},e.tick=function(){return this.counter+=1,new t(this.actorId,this.counter)},e.merge=function(e){return new t(this.actorId,Math.max(this.counter,e.counter))},e.compare=function(t){return this.counter>t.counter?exports.Ordering.Greater:this.counter<t.counter?exports.Ordering.Less:this.actorId>t.actorId?exports.Ordering.Greater:this.actorId<t.actorId?exports.Ordering.Less:exports.Ordering.Equal},e.valueOf=function(){return this.toString()},e.toString=function(){return String(this.counter).padStart(10,"0")+":"+this.actorId},t}();function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function n(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return r(t,void 0);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i=function(){function t(){this.nodes=new Map,this.children=new Map,this.size=this.nodes.size}var e=t.prototype;return e.remove=function(t){var e=this.nodes.get(t);if(e){var r=this.children.get(e.parentId);r&&(r.delete(t),0===r.size&&this.children.delete(e.parentId),this.nodes.delete(t))}},e.addNode=function(t,e){var r=this.children.get(e.parentId);r||(r=new Set,this.children.set(e.parentId,r)),r.add(t),this.nodes.set(t,e)},e.get=function(t){return this.nodes.get(t)},e.isAncestor=function(t,e){for(var r,n=t;r=this.get(n);){if(r.parentId===e)return!0;n=r.parentId}return!1},e.printNode=function(t,e){var r;void 0===e&&(e=0);var n=this.get(t),i=t+" "+(n?""+JSON.stringify(n.metadata):""),o=" ".repeat(2*e);console.log(o+i);for(var a=0,s=Array.from(null!=(r=this.children.get(t))?r:[]);a<s.length;a++)this.printNode(s[a],e+1)},t}(),o=function(t,e){this.parentId=t,this.metadata=e},a=function(){function t(t){var e;void 0===t&&(t={}),this.operationLog=[],this.tree=new i,this.conflictHandler=null!=(e=t.conflictHandler)?e:function(){return!1}}var e=t.prototype;return e.addLogEntry=function(t){this.operationLog.unshift(t)},e.applyOp=function(t){if(0===this.operationLog.length){var e=this.doOperation(t);this.addLogEntry(e)}else{var r=this.operationLog[0].op;if(t.timestamp===r.timestamp&&console.log("op with timestamp equal to previous op ignored. (not applied). Every op must have a unique timestamp."),t.timestamp<r.timestamp){var n=this.operationLog.shift();this.undoOp(n),this.applyOp(t),this.redoOp(n)}if(t.timestamp>r.timestamp){var i=this.doOperation(t);this.addLogEntry(i)}}},e.applyOps=function(t){for(var e,r=n(t);!(e=r()).done;)this.applyOp(e.value)},e.doOperation=function(t){var e=this.tree.get(t.id);if(t.id===t.parentId||this.tree.isAncestor(t.parentId,t.id))return{op:t,oldNode:e};if(this.conflictHandler(t,this.tree))return{op:t,oldNode:e};this.tree.remove(t.id);var r=new o(t.parentId,t.metadata);return this.tree.addNode(t.id,r),{op:t,oldNode:e}},e.undoOp=function(t){if(this.tree.remove(t.op.id),t.oldNode){var e=new o(t.oldNode.parentId,t.oldNode.metadata);this.tree.addNode(t.op.id,e)}},e.redoOp=function(t){var e=this.doOperation(t.op);this.addLogEntry(e)},t}(),s=function(){function t(t,r){void 0===r&&(r={}),this.latestTimeByReplica=new Map,this.time=new e(t),this.state=new a(r),this.tree=this.state.tree}var r=t.prototype;return r.get=function(t){return this.tree.get(t)},r.opMove=function(t,e,r){return{timestamp:this.time.inc(),metadata:e,id:t,parentId:r}},r.opMoves=function(t){for(var e,r=[],i=n(t);!(e=i()).done;){var o=e.value;r.push({timestamp:this.time.tick(),id:o[0],metadata:o[1],parentId:o[2]})}return r},r.applyOp=function(t){var e;this.time=this.time.merge(t.timestamp);var r=t.timestamp.actorId,n=null!=(e=this.latestTimeByReplica.get(r))?e:0;t.timestamp<=n?(console.log("Clock not increased, current timestamp "+n.toString()+", provided is "+t.timestamp.toString()+"."),console.log("Dropping operation.")):this.latestTimeByReplica.set(r,t.timestamp),this.state.applyOp(t)},r.applyOps=function(t){for(var e,r=n(t);!(e=r()).done;)this.applyOp(e.value)},t}();exports.Clock=e,exports.State=a,exports.Tree=i,exports.TreeNode=o,exports.TreeReplica=s;
//# sourceMappingURL=crdt-tree.cjs.production.min.js.map

@@ -251,3 +251,9 @@ var Ordering;

var State = /*#__PURE__*/function () {
function State() {
function State(options) {
var _options$conflictHand;
if (options === void 0) {
options = {};
}
/** A list of `LogOpMove` in descending timestamp order */

@@ -257,3 +263,7 @@ this.operationLog = [];

this.tree = new Tree();
this.tree = new Tree(); // Default to not handling conflict
this.conflictHandler = (_options$conflictHand = options.conflictHandler) != null ? _options$conflictHand : function () {
return false;
};
}

@@ -324,3 +334,3 @@ /** Insert a log entry to the top of the log */

// `oldNode` records the previous parent and metadata of c.
var oldNode = this.tree.get(op.id); // ensures no cycles are introduced. If the node c
var oldNode = this.tree.get(op.id); // ensures no cycles are introduced. If the node c
// is being moved, and c is an ancestor of the new parent

@@ -336,2 +346,11 @@ // newp, then the tree is returned unmodified, ie the operation

};
} // ignores operations that produce conflicts according to the
// custom conflict handler.
if (this.conflictHandler(op, this.tree)) {
return {
op: op,
oldNode: oldNode
};
} // Otherwise, the tree is updated by removing c from

@@ -375,12 +394,12 @@ // its existing parent, if any, and adding the new

var TreeReplica = /*#__PURE__*/function () {
function TreeReplica(authorId) {
/** The Tree state */
this.state = new State();
function TreeReplica(authorId, options) {
if (options === void 0) {
options = {};
}
/** Mapping of replicas and their latest time */
this.latestTimeByReplica = new Map();
/** A tree structure that represents the current state of the tree */
this.time = new Clock(authorId);
this.state = new State(options);
this.tree = this.state.tree;
this.time = new Clock(authorId);
}

@@ -387,0 +406,0 @@ /** Get a node by its id */

import { LogOpMove } from "./LogOpMove";
import { OpMove } from "./OpMove";
import { Tree } from "./Tree";
interface StateOptions<Id, Metadata> {
/**
* An function to provide domain-specific conflict handling logic.
* The resulting boolean value determines whether the operation conflicts.
*
* This is useful if metadata collision can produce conflicts in your business
* logic. For example, making name collisions impossible in a filesystem.
*/
conflictHandler?: (operation: OpMove<Id, Metadata>, tree: Tree<Id, Metadata>) => boolean;
}
export declare class State<Id, Metadata> {

@@ -9,2 +19,5 @@ /** A list of `LogOpMove` in descending timestamp order */

tree: Tree<Id, Metadata>;
/** Returns true if the given operation should be discarded */
conflictHandler: (operation: OpMove<Id, Metadata>, tree: Tree<Id, Metadata>) => boolean;
constructor(options?: StateOptions<Id, Metadata>);
/** Insert a log entry to the top of the log */

@@ -29,1 +42,2 @@ addLogEntry(entry: LogOpMove<Id, Metadata>): void;

}
export {};
import { Clock } from "./Clock";
import { OpMove } from "./OpMove";
import { State } from "./State";
import { Tree } from "./Tree";
import { TreeNode } from "./TreeNode";
interface ReplicaOptions<Id, Metadata> {
/**
* An function to provide domain-specific conflict handling logic.
* The resulting boolean value determines whether the operation conflicts.
*
* This is useful if metadata collision can produce conflicts in your business
* logic. For example, making name collisions impossible in a filesystem.
*/
conflictHandler?: (operation: OpMove<Id, Metadata>, tree: Tree<Id, Metadata>) => boolean;
}
export declare class TreeReplica<Id, Metadata> {

@@ -13,4 +24,4 @@ /** The Tree state */

/** A tree structure that represents the current state of the tree */
tree: import("./Tree").Tree<Id, Metadata>;
constructor(authorId: Id);
tree: Tree<Id, Metadata>;
constructor(authorId: Id, options?: ReplicaOptions<Id, Metadata>);
/** Get a node by its id */

@@ -43,1 +54,2 @@ get(id: Id): TreeNode<Id, Metadata> | undefined;

}
export {};

@@ -5,3 +5,3 @@ {

"author": "Matan Kushner",
"version": "1.1.4",
"version": "1.2.0",
"license": "MIT",

@@ -31,14 +31,6 @@ "main": "dist/index.js",

"size-limit": "^4.10.2",
"tsdx": "^0.14.1",
"@weiran.zsd/tsdx": "^0.15.0",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"resolutions": {
"--- fixes: https://github.com/formium/tsdx/issues/926 ---": "0",
"**/@typescript-eslint/eslint-plugin": "^4.11.1",
"**/@typescript-eslint/parser": "^4.11.1",
"**/jest": "^26.6.3",
"**/ts-jest": "^26.4.4",
"**/typescript": "^4.1.3"
},
"size-limit": [

@@ -45,0 +37,0 @@ {

@@ -22,2 +22,16 @@ // Holds Tree CRDT state and implements the core algorithm.

interface StateOptions<Id, Metadata> {
/**
* An function to provide domain-specific conflict handling logic.
* The resulting boolean value determines whether the operation conflicts.
*
* This is useful if metadata collision can produce conflicts in your business
* logic. For example, making name collisions impossible in a filesystem.
*/
conflictHandler?: (
operation: OpMove<Id, Metadata>,
tree: Tree<Id, Metadata>
) => boolean;
}
export class State<Id, Metadata> {

@@ -28,3 +42,13 @@ /** A list of `LogOpMove` in descending timestamp order */

tree: Tree<Id, Metadata> = new Tree();
/** Returns true if the given operation should be discarded */
conflictHandler: (
operation: OpMove<Id, Metadata>,
tree: Tree<Id, Metadata>
) => boolean;
constructor(options: StateOptions<Id, Metadata> = {}) {
// Default to not handling conflict
this.conflictHandler = options.conflictHandler ?? (() => false);
}
/** Insert a log entry to the top of the log */

@@ -85,3 +109,3 @@ addLogEntry(entry: LogOpMove<Id, Metadata>) {

// ensures no cycles are introduced. If the node c
// ensures no cycles are introduced. If the node c
// is being moved, and c is an ancestor of the new parent

@@ -95,2 +119,8 @@ // newp, then the tree is returned unmodified, ie the operation

// ignores operations that produce conflicts according to the
// custom conflict handler.
if (this.conflictHandler(op, this.tree)) {
return { op, oldNode };
}
// Otherwise, the tree is updated by removing c from

@@ -97,0 +127,0 @@ // its existing parent, if any, and adding the new

@@ -16,7 +16,22 @@ // `TreeReplica` holds tree `State` plus lamport timestamp (actor + counter)

import { State } from "./State";
import { Tree } from "./Tree";
import { TreeNode } from "./TreeNode";
interface ReplicaOptions<Id, Metadata> {
/**
* An function to provide domain-specific conflict handling logic.
* The resulting boolean value determines whether the operation conflicts.
*
* This is useful if metadata collision can produce conflicts in your business
* logic. For example, making name collisions impossible in a filesystem.
*/
conflictHandler?: (
operation: OpMove<Id, Metadata>,
tree: Tree<Id, Metadata>
) => boolean;
}
export class TreeReplica<Id, Metadata> {
/** The Tree state */
state: State<Id, Metadata> = new State();
state: State<Id, Metadata>;
/** The logical clock for this replica/tree */

@@ -27,6 +42,8 @@ time: Clock<Id>;

/** A tree structure that represents the current state of the tree */
tree = this.state.tree;
tree: Tree<Id, Metadata>;
constructor(authorId: Id) {
constructor(authorId: Id, options: ReplicaOptions<Id, Metadata> = {}) {
this.time = new Clock(authorId);
this.state = new State(options);
this.tree = this.state.tree;
}

@@ -33,0 +50,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc