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

crdts

Package Overview
Dependencies
Maintainers
4
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crdts - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6-f8f738d.0

.github/workflows/npm-publish-next.yml

18

benchmark/benchmark-sets.js

@@ -1,8 +0,6 @@

'use strict'
// All supported Set CRDTs
const GSet = require('../src/G-Set')
const TwoPSet = require('../src/2P-Set')
const ORSet = require('../src/OR-Set')
const LWWSet = require('../src/LWW-Set')
import GSet from '../src/G-Set.js'
import TwoPSet from '../src/2P-Set.js'
import ORSet from '../src/OR-Set.js'
import LWWSet from '../src/LWW-Set.js'

@@ -29,4 +27,4 @@ // Choose your weapon from ^

let run = (() => {
console.log('Starting benchmark...')
export default (() => {
console.log('Starting benchmark....js')
// Output metrics at 1 second interval

@@ -37,3 +35,3 @@ setInterval(() => {

console.log(`--> Average of ${lastTenSeconds / 10} q/s in the last 10 seconds`)
if (lastTenSeconds === 0) throw new Error('Problems!')
if (lastTenSeconds === 0) throw new Error('Problems!.js')
lastTenSeconds = 0

@@ -47,3 +45,1 @@ }

})()
module.exports = run
{
"name": "crdts",
"version": "0.1.5",
"description": "A CRDT Library for JavaScript",
"main": "src/index.js",
"version": "0.1.6-f8f738d.0",
"description": "A library of Conflict-Free Replicated Data Types for JavaScript.",
"type": "module",
"main": "./src/index.js",
"repository": {

@@ -10,3 +11,5 @@ "type": "git",

},
"bugs": "https://github.com/orbitdb/crdts/issues",
"author": "Haad",
"homepage": "https://github.com/orbitdb/crdts",
"license": "MIT",

@@ -25,10 +28,15 @@ "keywords": [

},
"devDependencies": {
"mocha": "^4.0.1"
},
"dependencies": {},
"scripts": {
"test": "./node_modules/.bin/mocha",
"test": "mocha",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha"
},
"localMaintainers": [
"haad <haad@haja.io>",
"shamb0t <shams@haja.io>",
"hajamark <mark@haja.io>"
],
"devDependencies": {
"istanbul": "^0.4.5",
"mocha": "^10.2.0"
}
}

@@ -5,5 +5,5 @@ # CRDTs

[![CircleCI](https://circleci.com/gh/orbitdb/crdts.svg?style=shield)](https://circleci.com/gh/orbitdb/crdts)
[![](https://img.shields.io/badge/freenode-%23orbitdb-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23orbitdb)
[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/orbitdb/Lobby) [![Matrix](https://img.shields.io/badge/matrix-%23orbitdb%3Apermaweb.io-blue.svg)](https://riot.permaweb.io/#/room/#orbitdb:permaweb.io)
A CRDT library for JavaScript.
> A library of Conflict-Free Replicated Data Types for JavaScript.

@@ -14,3 +14,3 @@ ***Work In Progress***

Implemented CRDTs:
CRDTs implemented in this module:

@@ -24,17 +24,16 @@ - [G-Counter](https://github.com/orbitdb/crdts/blob/master/src/G-Counter.js)

## Usage
## Install
This module uses [npm](https://www.npmjs.com/) and [node](https://nodejs.org/en/).
To install, run:
```sh
$ npm install crdts
```
npm install crdts
```
## Usage
```javascript
const GCounter = require('crdts').GCounter
const PNCounter = require('crdts').PNCounter
const GSet = require('crdts').GSet
const TwoPSet = require('crdts').TwoPSet
const ORSet = require('crdts').ORSet
const LWWSet = require('crdts').LWWSet
// Or:
const { GSet, ORSet, LWWSet } = require('crdts')
import { GCounter, PNCounter, GSet, TwoPSet, ORSet, LWWSet, GSet, ORSet, LWWSet } from 'crdts'
```

@@ -52,9 +51,22 @@

|-----------------------------------------------+---------------------------+
CRDT Type | Operation-Based | State-based |
CRDT Type | Operation-Based | State-based |
+-----------------------------------------------+---------------------------+
```
## CRDTs
## CRDTs Research
CRDT research:
https://github.com/ipfs/research-CRDT
To learn more about CRDTs, check out this research:
- ["A comprehensive study of Convergent and Commutative Replicated Data Types"](http://hal.upmc.fr/inria-00555588/document) paper
- [CRDTs on Wikipedia](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type#Known_CRDTs)
- [IPFS's CRDT research group](https://github.com/ipfs/research-CRDT)
## Contribute
If you think this could be better, please [open an issue](https://github.com/orbitdb/crdts/issues/new)!
Please note that all interactions in [@OrbitDB](https://github.com/OrbitDB) fall under our [Code of Conduct](CODE_OF_CONDUCT.md).
## License
[MIT](LICENSE) © 2017-2019 Haja Networks Oy

@@ -1,6 +0,4 @@

'use strict'
import CRDTSet from './CmRDT-Set.js'
import GSet from './G-Set.js'
const CRDTSet = require('./CmRDT-Set')
const GSet = require('./G-Set.js')
/**

@@ -18,3 +16,3 @@ * 2P-Set

*/
class TwoPSet extends CRDTSet {
export default class TwoPSet extends CRDTSet {
/**

@@ -99,3 +97,1 @@ * Create a new TwoPSet instance

}
module.exports = TwoPSet

@@ -1,5 +0,3 @@

'use strict'
import { OperationTuple3 } from './utils.js'
const { OperationTuple3 } = require('./utils')
/**

@@ -29,3 +27,3 @@ * CmRDT-Set

*/
class CmRDTSet extends Set {
export default class CmRDTSet extends Set {
/**

@@ -281,3 +279,1 @@ * Create a new CmRDTSet instance

}
module.exports = CmRDTSet

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

'use strict'
const { deepEqual } = require('./utils')
import { deepEqual } from './utils.js'
const sum = (acc, val) => acc + val

@@ -16,3 +14,3 @@

class GCounter {
export default class GCounter {
constructor (id, counter) {

@@ -68,3 +66,1 @@ this.id = id

}
module.exports = GCounter

@@ -1,5 +0,3 @@

'use strict'
import CRDTSet from './CmRDT-Set.js'
const CRDTSet = require('./CmRDT-Set')
/**

@@ -23,3 +21,3 @@ * G-Set

*/
class GSet extends CRDTSet {
export default class GSet extends CRDTSet {
/**

@@ -89,3 +87,1 @@ * Create a G-Set CRDT instance

}
module.exports = GSet
// Export all supported CRDTs
exports.GCounter = GCounter = require('./G-Counter')
exports.CmRDTSet = CmRDTSet = require('./CmRDT-Set')
exports.GSet = GSet = require('./G-Set')
exports.TwoPSet = TwoPSet = require('./2P-Set')
exports.ORSet = ORSet = require('./OR-Set')
exports.LWWSet = LWWSet = require('./LWW-Set')
exports.PNCounter = PNCounter = require('./PN-Counter.js')
/**
* CRDTs
*
* Usage:
*
* const CRDTs = require('./index.js')
* const { GCounter, GSet, TwoPSet, ORSet, LWWSet } = CRDTs
*
* Or
*
* const GSet = require('./index.js').GSet
*/
module.exports = {
GCounter: GCounter,
PNCounter: PNCounter,
CmRDTSet: CmRDTSet,
GSet: GSet,
TwoPSet: TwoPSet,
ORSet: ORSet,
LWWSet: LWWSet,
}
export { default as GCounter } from './G-Counter.js'
export { default as CmRDTSet } from './CmRDT-Set.js'
export { default as GSet } from './G-Set.js'
export { default as TwoPSet } from './2P-Set.js'
export { default as ORSet } from './OR-Set.js'
export { default as LWWSet } from './LWW-Set.js'
export { default as PNCounter } from './PN-Counter.js'

@@ -1,5 +0,3 @@

'use strict'
import CRDTSet from './CmRDT-Set.js'
const CRDTSet = require('./CmRDT-Set')
/**

@@ -17,3 +15,3 @@ * LWWSet-Set

*/
class LWWSet extends CRDTSet {
export default class LWWSet extends CRDTSet {
/**

@@ -57,3 +55,1 @@ * @override

}
module.exports = LWWSet

@@ -1,5 +0,3 @@

'use strict'
import CRDTSet from './CmRDT-Set.js'
const CRDTSet = require('./CmRDT-Set')
/**

@@ -17,3 +15,3 @@ * OR-Set

*/
class ORSet extends CRDTSet {
export default class ORSet extends CRDTSet {
/**

@@ -86,3 +84,1 @@ * @override

}
module.exports = ORSet

@@ -1,10 +0,10 @@

'use strict'
import GCounter from '../src/G-Counter.js'
const GCounter = require('../src/G-Counter.js')
const isGCounter = (obj) => obj && obj instanceof GCounter
class PNCounter {
constructor (id, pCounter, nCounter) {
export default class PNCounter {
constructor (id, pCounters, nCounters) {
this.id = id
this.p = pCounter ? pCounter : new GCounter(id)
this.n = nCounter ? nCounter : new GCounter(id)
this.p = isGCounter(pCounters) ? pCounters : new GCounter(id, pCounters)
this.n = isGCounter(nCounters) ? nCounters : new GCounter(id, nCounters)
}

@@ -28,8 +28,8 @@

}
toJSON () {
return {
id: this.id,
p: this.p,
n: this.n
p: this.p._counters,
n: this.n._counters
}

@@ -40,3 +40,3 @@ }

return PNCounter.isEqual(this, other)
}
}

@@ -51,3 +51,1 @@ static from (json) {

}
module.exports = PNCounter

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

'use strict'
exports.deepEqual = (a, b) => {
const deepEqual = (a, b) => {
const propsA = Object.getOwnPropertyNames(a)

@@ -35,2 +33,5 @@ const propsB = Object.getOwnPropertyNames(b)

exports.OperationTuple3 = OperationTuple3
export {
deepEqual,
OperationTuple3
}

@@ -1,6 +0,4 @@

'use strict'
import assert from 'assert'
import { TwoPSet, GSet, CmRDTSet } from '../src/index.js'
const assert = require('assert')
const { TwoPSet, GSet, CmRDTSet } = require('../src')
describe('2P-Set', () => {

@@ -338,2 +336,1 @@ describe('Instance', () => {

})

@@ -1,9 +0,5 @@

'use strict'
import assert from 'assert'
import { GCounter, GSet, TwoPSet, ORSet, LWWSet } from '../src/index.js'
import CmRDTSet from '../src/CmRDT-Set.js'
const assert = require('assert')
const CRDTs = require('../src')
const { GCounter, GSet, TwoPSet, ORSet, LWWSet } = CRDTs
const CmRDTSet = require('../src/CmRDT-Set')
const added = [1, 2, 3]

@@ -186,3 +182,3 @@ const removed = [1]

const crdt2 = CRDT.create(CRDT.inputData)
const crdt3 = CRDT.create([])
const crdt3 = CRDT.create({})
assert.equal(CRDT.isEqual(crdt1, crdt2), true)

@@ -189,0 +185,0 @@ const isEqual = CRDT.isEqual(crdt1, crdt3)

@@ -1,9 +0,5 @@

'use strict'
import assert from 'assert'
import { GCounter, PNCounter, GSet, TwoPSet, ORSet, LWWSet } from '../src/index.js'
import CmRDTSet from '../src/CmRDT-Set.js'
const assert = require('assert')
const CRDTs = require('../src')
const { GCounter, GSet, TwoPSet, ORSet, LWWSet } = CRDTs
const CmRDTSet = require('../src/CmRDT-Set')
const crdts = [

@@ -20,2 +16,14 @@ {

{
type: 'PN-Counter',
class: PNCounter,
create: (id) => new PNCounter(id),
update: (crdt, value) => {
crdt.increment(value + 1)
crdt.decrement(value)
},
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => crdt.value,
getExpectedMergedValue: (values) => -values.reduce((acc, val) => acc + val, 0) + values.reduce((acc, val) => (acc + 1) + val, 0),
},
{
type: 'G-Set',

@@ -22,0 +30,0 @@ class: GSet,

@@ -1,11 +0,9 @@

'use strict'
import assert from 'assert'
import { GCounter } from '../src/index.js'
const assert = require('assert')
const Counter = require('../src').GCounter
describe('G-Counter', () => {
describe('G-GCounter', () => {
describe('Instance', () => {
describe('constructor', () => {
it('creates a counter', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
assert(counter, null)

@@ -20,3 +18,3 @@ assert.notEqual(counter.id, null)

it('returns the count', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
assert.equal(counter.value, 0)

@@ -26,3 +24,3 @@ })

it('returns the count after increment', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment(5)

@@ -35,3 +33,3 @@ assert.equal(counter.value, 5)

it('increments the count by 1', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment()

@@ -42,3 +40,3 @@ assert.equal(counter.value, 1)

it('increments the count by 2', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment()

@@ -50,3 +48,3 @@ counter.increment()

it('increments the count by 3', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment(3)

@@ -57,3 +55,3 @@ assert.equal(counter.value, 3)

it('increments the count by 42', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment()

@@ -65,3 +63,3 @@ counter.increment(42)

it('can\'t decrease the counter', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment(-1)

@@ -72,3 +70,3 @@ assert.equal(counter.value, 0)

it('can\'t decrease the counter', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
counter.increment(0)

@@ -81,4 +79,4 @@ assert.equal(counter.value, 0)

it('merges two counters with same id', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('A')
const counter1 = new GCounter('A')
const counter2 = new GCounter('A')
counter1.increment()

@@ -91,4 +89,4 @@ counter2.increment()

it('merges two counters with same values', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('B')
const counter1 = new GCounter('A')
const counter2 = new GCounter('B')
counter1.increment()

@@ -103,6 +101,6 @@ counter2.increment()

it('merges four different counters', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('B')
const counter3 = new Counter('C')
const counter4 = new Counter('D')
const counter1 = new GCounter('A')
const counter2 = new GCounter('B')
const counter3 = new GCounter('C')
const counter4 = new GCounter('D')
counter1.increment()

@@ -119,4 +117,4 @@ counter2.increment()

it('doesn\'t overwrite its own value on merge', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('B')
const counter1 = new GCounter('A')
const counter2 = new GCounter('B')
counter1.increment()

@@ -132,4 +130,4 @@ counter2.increment()

it('doesn\'t overwrite others\' values on merge', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('B')
const counter1 = new GCounter('A')
const counter2 = new GCounter('B')
counter1.increment()

@@ -148,3 +146,3 @@ counter2.increment()

it('returns the counter as JSON object', () => {
const counter = new Counter('A')
const counter = new GCounter('A')
assert.equal(counter.toJSON().id, 'A')

@@ -155,4 +153,4 @@ assert.equal(counter.toJSON().counters.A, 0)

it('returns a JSON object after a merge', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('B')
const counter1 = new GCounter('A')
const counter2 = new GCounter('B')
counter1.increment()

@@ -172,4 +170,4 @@ counter2.increment()

it('returns true for equal counters', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('A')
const counter1 = new GCounter('A')
const counter2 = new GCounter('A')
counter1.increment()

@@ -181,4 +179,4 @@ counter2.increment()

it('returns false for unequal counters - different id', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('B')
const counter1 = new GCounter('A')
const counter2 = new GCounter('B')
assert.equal(counter1.isEqual(counter2), false)

@@ -188,4 +186,4 @@ })

it('returns false for unequal counters - same id, different counts', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('A')
const counter1 = new GCounter('A')
const counter2 = new GCounter('A')
counter1.increment()

@@ -198,4 +196,4 @@ counter2.increment()

it('returns false for unequal counters - different counters', () => {
const counter1 = new Counter('A')
const counter2 = new Counter('A')
const counter1 = new GCounter('A')
const counter2 = new GCounter('A')
counter2._counters['extra'] = 'world'

@@ -207,5 +205,5 @@ assert.equal(counter1.isEqual(counter2), false)

describe('GCounter.from', () => {
describe('GGCounter.from', () => {
it('creates a new counter from JSON object', () => {
const counter1 = new Counter('A')
const counter1 = new GCounter('A')
counter1.increment()

@@ -220,4 +218,4 @@

const counter2 = Counter.from(input)
assert.equal(Counter.isEqual(counter1, counter2), true)
const counter2 = GCounter.from(input)
assert.equal(GCounter.isEqual(counter1, counter2), true)
assert.equal(counter2.id, 'A')

@@ -228,14 +226,14 @@ assert.equal(counter2.value, 1)

describe('GCounter.isEqual', () => {
describe('GGCounter.isEqual', () => {
it('returns true if to GSets are equal', () => {
const values = ['A', 'B', 'C']
const counter1 = new Counter('A')
const counter2 = new Counter('A')
const counter3 = new Counter('B')
const counter1 = new GCounter('A')
const counter2 = new GCounter('A')
const counter3 = new GCounter('B')
counter1.increment(2)
counter2.increment(2)
assert.equal(Counter.isEqual(counter1, counter2), true)
assert.equal(Counter.isEqual(counter1, counter3), false)
assert.equal(GCounter.isEqual(counter1, counter2), true)
assert.equal(GCounter.isEqual(counter1, counter3), false)
})
})
})

@@ -1,6 +0,4 @@

'use strict'
import assert from 'assert'
import { GSet, CmRDTSet } from '../src/index.js'
const assert = require('assert')
const { GSet, CmRDTSet } = require('../src')
describe('G-Set', () => {

@@ -7,0 +5,0 @@ describe('Instance', () => {

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

'use strict'
class LamportClock {
export default class LamportClock {
constructor (id, time) {

@@ -40,3 +38,1 @@ this.id = id

}
module.exports = LamportClock

@@ -1,7 +0,5 @@

'use strict'
import assert from 'assert'
import { LWWSet, CmRDTSet } from '../src/index.js'
import LamportClock from './lamport-clock.js'
const assert = require('assert')
const { LWWSet, CmRDTSet } = require('../src')
const LamportClock = require('./lamport-clock')
describe('LWW-Set', () => {

@@ -8,0 +6,0 @@ describe('Instance', () => {

@@ -1,7 +0,5 @@

'use strict'
import assert from 'assert'
import { ORSet, CmRDTSet } from '../src/index.js'
import LamportClock from './lamport-clock.js'
const assert = require('assert')
const { ORSet, CmRDTSet } = require('../src')
const LamportClock = require('./lamport-clock')
describe('OR-Set', () => {

@@ -8,0 +6,0 @@ describe('Instance', () => {

@@ -1,6 +0,4 @@

'use strict'
import assert from 'assert'
import { GCounter, PNCounter } from '../src/index.js'
const assert = require('assert')
const { GCounter, PNCounter } = require('../src')
describe('PN-Counter', () => {

@@ -187,4 +185,4 @@ describe('Instance', () => {

assert.equal(counter.toJSON().id, 'A')
assert.equal(counter.toJSON().p._counters.A, 0)
assert.equal(counter.toJSON().n._counters.A, 0)
assert.equal(counter.toJSON().p.A, 0)
assert.equal(counter.toJSON().n.A, 0)
})

@@ -201,12 +199,12 @@

counter2.merge(counter1)
assert.equal(Object.keys(counter1.toJSON().p._counters).length, 2)
assert.equal(Object.keys(counter1.toJSON().n._counters).length, 2)
assert.equal(counter1.toJSON().p._counters.A, 1)
assert.equal(counter1.toJSON().p._counters.B, 1)
assert.equal(counter2.toJSON().p._counters.A, 1)
assert.equal(counter2.toJSON().p._counters.B, 1)
assert.equal(counter1.toJSON().n._counters.A, 1)
assert.equal(counter1.toJSON().n._counters.B, 1)
assert.equal(counter2.toJSON().n._counters.A, 1)
assert.equal(counter2.toJSON().n._counters.B, 1)
assert.equal(Object.keys(counter1.toJSON().p).length, 2)
assert.equal(Object.keys(counter1.toJSON().n).length, 2)
assert.equal(counter1.toJSON().p.A, 1)
assert.equal(counter1.toJSON().p.B, 1)
assert.equal(counter2.toJSON().p.A, 1)
assert.equal(counter2.toJSON().p.B, 1)
assert.equal(counter1.toJSON().n.A, 1)
assert.equal(counter1.toJSON().n.B, 1)
assert.equal(counter2.toJSON().n.A, 1)
assert.equal(counter2.toJSON().n.B, 1)
})

@@ -213,0 +211,0 @@ })

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