hypercore-list 0.0.1-alpha.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/index.js +68 -0
  3. package/package.json +40 -0
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # (WIP) Hypercore List
2
+
3
+ Bundle Hypercores in an ordered collection.
4
+
5
+ Includes rehoster support, so rehosting a collection means you keep all the hypercores it contains available too.
package/index.js ADDED
@@ -0,0 +1,68 @@
1
+ const ReadyResource = require('ready-resource')
2
+ const cenc = require('compact-encoding')
3
+ const SubEncoder = require('sub-encoder')
4
+ const idEnc = require('hypercore-id-encoding')
5
+ const RehosterDb = require('hypercore-rehoster/db')
6
+
7
+ class HypercoreList extends ReadyResource {
8
+ constructor (bee) {
9
+ super()
10
+
11
+ this.bee = bee
12
+
13
+ const subEncoder = new SubEncoder('collection')
14
+ this.entriesEncs = {
15
+ keyEncoding: subEncoder,
16
+ valueEncoding: cenc.array(cenc.fixed32)
17
+ }
18
+ this.titleEncs = {
19
+ keyEncoding: subEncoder,
20
+ valueEncoding: cenc.string
21
+ }
22
+
23
+ this.rehosterDb = new RehosterDb(this.bee)
24
+ }
25
+
26
+ async setTitle (title) {
27
+ await this.bee.put('TITLE', title, {
28
+ ...this.titleEncs,
29
+ cas: (prev, next) => prev !== next // no useless appends if it's the same
30
+ })
31
+ }
32
+
33
+ async getTitle () {
34
+ const entry = await this.bee.get('TITLE', this.titleEncs)
35
+ return entry?.value || null
36
+ }
37
+
38
+ async setEntries (orderedEntries) {
39
+ const bufferEntries = []
40
+ const rehosterEntries = new Map()
41
+ for (const key of orderedEntries) {
42
+ bufferEntries.push(idEnc.decode(key))
43
+ rehosterEntries.set(key, {})
44
+ }
45
+
46
+ let inserted = true
47
+ await this.bee.put('ENTRIES', bufferEntries, {
48
+ ...this.entriesEncs,
49
+ cas: (prev, next) => {
50
+ if (!prev) return true // only if none exists, for simplicity (TODO:)
51
+ inserted = false
52
+ return false
53
+ }
54
+ })
55
+
56
+ if (!inserted) throw new Error('Updating the entries is not yet supported (TODO)')
57
+
58
+ const synced = await this.rehosterDb.sync(rehosterEntries)
59
+ if (!synced) throw new Error('Logical error in hypercore-collection.setEntries')
60
+ }
61
+
62
+ async getEntries () {
63
+ const res = await this.bee.get('ENTRIES', this.entriesEncs)
64
+ return res.value || null
65
+ }
66
+ }
67
+
68
+ module.exports = HypercoreList
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "hypercore-list",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "An ordered collection of hypercores",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "standard"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+ssh://git@gitlab.com/dcent-tech/hypercore-list.git"
12
+ },
13
+ "files": [
14
+ "index.js"
15
+ ],
16
+ "keywords": [
17
+ "Hypercore",
18
+ "list",
19
+ "ordered",
20
+ "collection"
21
+ ],
22
+ "author": "H. Degroote",
23
+ "license": "Apache-2.0",
24
+ "bugs": {
25
+ "url": "https://gitlab.com/dcent-tech/hypercore-list/issues"
26
+ },
27
+ "homepage": "https://gitlab.com/dcent-tech/hypercore-list#readme",
28
+ "dependencies": {
29
+ "compact-encoding": "^2.15.0",
30
+ "hypercore-id-encoding": "^1.3.0",
31
+ "hypercore-rehoster": "^2.0.0",
32
+ "ready-resource": "^1.1.1",
33
+ "sub-encoder": "^2.1.3"
34
+ },
35
+ "devDependencies": {
36
+ "corestore": "^6.18.4",
37
+ "hyperbee": "^2.20.0",
38
+ "standard": "^17.1.2"
39
+ }
40
+ }