{"id":15497384,"url":"https://github.com/heapwolf/level-2pc","last_synced_at":"2025-12-17T00:11:56.691Z","repository":{"id":24830802,"uuid":"28245440","full_name":"heapwolf/level-2pc","owner":"heapwolf","description":"A two-phase-commit protocol for leveldb.","archived":false,"fork":false,"pushed_at":"2015-10-26T11:22:38.000Z","size":492,"stargazers_count":26,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-07T23:51:03.851Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/heapwolf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-19T20:37:55.000Z","updated_at":"2024-10-08T13:57:08.000Z","dependencies_parsed_at":"2022-08-22T15:30:20.564Z","dependency_job_id":null,"html_url":"https://github.com/heapwolf/level-2pc","commit_stats":null,"previous_names":["hij1nx/level-2pc"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/heapwolf/level-2pc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heapwolf%2Flevel-2pc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heapwolf%2Flevel-2pc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heapwolf%2Flevel-2pc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heapwolf%2Flevel-2pc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heapwolf","download_url":"https://codeload.github.com/heapwolf/level-2pc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heapwolf%2Flevel-2pc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263296443,"owners_count":23444489,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T08:33:17.814Z","updated_at":"2025-12-17T00:11:56.644Z","avatar_url":"https://github.com/heapwolf.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SYNOPSIS\nA two-phase-commit protocol for leveldb.\n\n# DESCRIPTION\nProvides strong-consistency for local-cluster replication.\n\nEvery node in your cluster can be writable and all reads\nfrom any node will be consistent.\n\nUses [`reconnect-core`](https://github.com/juliangruber/reconnect-core) to support an injectable transport for e.g. browser compatibility.\n\n# BUILD STATUS\n[![Build Status](http://img.shields.io/travis/hij1nx/level-2pc.svg?style=flat)](https://travis-ci.org/hij1nx/level-2pc)\n\n# SPECIFICATION\nThe algorithm for how this works is [`here`](/SPEC.md).\n\n# USAGE\n\n## EXAMPLE\n\n### SERVER A\n```js\nvar level = require('level');\nvar Replicator = require('level-2pc');\nvar net = require('net');\n\nvar db1 = level('./db', { valueEncoding: 'json' });\n\nvar opts = {\n  peers: [\n    { host: 'localhost', port: 3001 },\n    { host: 'localhost', port: 3002 }\n  ]\n};\n\nvar r = Replicator(db1, opts);\n\nnet.createServer(function(con) {\n  var server = r.createServer();\n  server.pipe(con).pipe(server);\n}).listen(3000);\n```\n\n### SERVER B\n```js\n\nvar opts = {\n  peers: [\n    { host: 'localhost', port: 3000 },\n    { host: 'localhost', port: 3002 }\n  ]\n};\n\nvar r = Replicator(db2, opts);\n\nnet.createServer(function(con) {\n  var server = r.createServer();\n  server.pipe(con).pipe(server);\n}).listen(3001);\n```\n\n### SERVER C\n```js\nvar opts = {\n  peers: [\n    { host: 'localhost', port: 3000 },\n    { host: 'localhost', port: 3001 }\n  ]\n};\n\nvar r = Replicator(db3, opts);\n\nnet.createServer(function(con) {\n  var server = r.createServer();\n  server.pipe(con).pipe(server);\n}).listen(3002);\n```\n\n### WRITE SOME DATA\nNow go ahead and write some data to one of the\nservers and watch the data magically appear in\nthe other servers!\n\n```js\nsetTimeout(function() {\n\n  db1.put('x', 100, function(err) {\n    console.log(err || 'ok');\n  });\n\n  setTimeout(function() {\n    db2.get('x', function() {\n      console.log(arguments);\n      db3.get('x', function() {\n        console.log(arguments);\n      });\n    });\n  }, 100);\n\n}, 100);\n```\n\n# TRANSPORT\nWhen the server wants to connect to the peers\nthat have been specified, it defaults to using\ntcp from the `net` module. You can inject any\ntransportation layer you like by setting the\n`transport` property in the options object:\n\n```js\nvar net = require('net');\n\nvar opts = {\n  transport: function() {\n    return net.connect.apply(null, arguments);\n  },\n  peers: [ /* .. */ ]\n};\n\nvar r = Replicator(db, opts);\n```\n\n# API\n\n### Replicator(db, opts)\n\nReturns a `Replicator` object, which is an `EventEmitter`.\n\n* `db` leveldb database object\n* `opts` options object with the following properties:\n\n  * `host` host that other peers should connect to\n  * `port` port that other peers should connect to\n  * `peers` an array of objects that specify the host and port of each peer\n  * `minConsensus` how many peers must connect initially or respond to quorum\n\n### Replicator#createServer()\n\nReturns a duplex [`rpc-stream`](https://github.com/dominictarr/rpc-stream) that can be served over e.g. `http` or `tcp` or any other transport supporting node streams.\n\n### Replicator#close()\n\nCloses connections to all peers.\n\n### Event: 'ready'\n\nEmitted when the replicator is ready to replicate with other peers. Happens when the replicator has enough connections for the quorum, i.e. when the number of peers is above `minConsensus`.\n\n### Event: 'notready'\n\nEmitted when the replicator is not ready to replicate with other peers. Happens when the replicator doesn't have enough connections for the quorum, i.e. when the number of peers goes below `minConsensus`.\n\n### Event: 'connect'\n\nEmitted when the replicator has connected to a peer.\n\n* `host` host of the connected peer\n* `port` port of the connected peer\n\n### Event: 'error'\n\nEmitted when there was an error in the connection between the replicator and a peer.\n\n* `err` error object\n\n### Event: 'disconnect'\n\nEmitted when the replicator has disconnected from a peer.\n\n* `host` host of the disconnected peer\n* `port` port of the disconnected peer\n\n### Event: 'reconnect'\n\nEmitted when the replicator tries to reconnect to a peer.\n\n* `host` retrying connection to this host\n* `port` retrying connection to this port\n\n### Event: 'fail'\n\nEmitted when the replicator has tried to reconnect but failed too many times. There might be a problem with the connection, or the peer is simply offline.\n\n* `host` host of the failing peer\n* `port` port of the failing peer","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheapwolf%2Flevel-2pc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheapwolf%2Flevel-2pc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheapwolf%2Flevel-2pc/lists"}