hypercore-storage 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,198 @@ npm install hypercore-storage
10
10
 
11
11
  The following API is what Hypercore 11 binds to to do I/O.
12
12
 
13
- TODO
13
+ ```js
14
+ const Storage = require('hypercore-storage')
15
+ ```
16
+
17
+ #### `store = new Storage(dbOrPath)`
18
+
19
+ Make a new storage engine.
20
+
21
+ #### `core = await store.create({ key, discoveyKey, manifest?, keyPair?, encryptionKey?, userData? })`
22
+
23
+ Create a new core, returns a storage instance for that core.
24
+
25
+ #### `core = await store.resume(discoveryKey)`
26
+
27
+ Resume a previously make core. If it doesn't exist it returns `null`.
28
+
29
+ #### `atom = store.createAtom()`
30
+
31
+ Primitive for making atomic batches across ops. See below for `core.atomize` on how to use it.
32
+ When you wanna flush your changes to the underlying storage, use `await atom.flush()`.
33
+
34
+ Internally to "listen" for when that happens you can add an sync hook with `atom.onflush(fn)`
35
+
36
+ #### `bool = await store.has(discoveryKey)`
37
+
38
+ Check if a core exists.
39
+
40
+ #### `stream = store.createCoreStream()`
41
+
42
+ List all cores. Stream data looks like this `{ discoveryKey, core }` where core contains the core header.
43
+
44
+ #### `await store.close()`
45
+
46
+ Close the storage instance.
47
+
48
+ #### `rx = core.read()`
49
+
50
+ Make a read batch on a core storage.
51
+
52
+ **NOTE:** a read batch DOES NOT flush until you call `rx.tryFlush()`.
53
+
54
+ #### `await rx.getAuth()`
55
+
56
+ Returns the auth data around a core.
57
+
58
+ #### `await rx.getHead()`
59
+
60
+ Returns the head of the merkle tree.
61
+
62
+ #### `await rx.getSessions()`
63
+
64
+ Returns an array of all named sessions.
65
+
66
+ #### `await rx.getDependency()`
67
+
68
+ Returns the core this has a dependency on.
69
+
70
+ #### `await rx.getHints()`
71
+
72
+ Returns the various storage/replication hints.
73
+
74
+ #### `await rx.getBlock(index)`
75
+
76
+ Returns a block stored.
77
+
78
+ #### `await rx.getTreeNode(index)`
79
+
80
+ Returns a tree node stored.
81
+
82
+ #### `await rx.getBitfieldPage(index)`
83
+
84
+ Return a bitfield page.
85
+
86
+ #### `await rx.getUserData(key)`
87
+
88
+ Return a user stored buffer.
89
+
90
+ #### `rx.tryFlush()`
91
+
92
+ Flushes the read batch, non of the above promises will resolve until you call this.
93
+
94
+ #### `tx = core.write()`
95
+
96
+ Make a write batch on a core storage.
97
+
98
+ **NOTE:** all the apis below are sync as they just buffer mutations until you flush them.
99
+
100
+ #### `tx.setAuth(auth)`
101
+
102
+ Set the auth data around a core.
103
+
104
+ #### `tx.setHead(auth)`
105
+
106
+ Set the head of the merkle tree.
107
+
108
+ #### `tx.setSessions(sessions)`
109
+
110
+ Set an array of all named sessions.
111
+
112
+ #### `tx.setDependency(dep)`
113
+
114
+ Set the core this has a dependency on.
115
+
116
+ #### `tx.setHints(hints)`
117
+
118
+ Set the various storage/replication hints.
119
+
120
+ #### `tx.putBlock(index, buffer)`
121
+
122
+ Put a block at a specific index.
123
+
124
+ #### `tx.deleteBlock(index)`
125
+
126
+ Delete a block at a specific index.
127
+
128
+ #### `tx.deleteBlockRange(start, index)`
129
+
130
+ Delete blocks between two indexes.
131
+
132
+ #### `tx.putTreeNode(node)`
133
+
134
+ Put a tree node (at its described index).
135
+
136
+ #### `tx.deleteTreeNode(index)`
137
+
138
+ Delete a tree node at a specific index.
139
+
140
+ #### `tx.deleteTreeNodeRange(start, index)`
141
+
142
+ Delete blocks between two tree indexes.
143
+
144
+ #### `tx.putBitfieldPage(index, page)`
145
+
146
+ Put a bitfield page at its described index.
147
+
148
+ #### `tx.deleteBitfieldPage(index)`
149
+
150
+ Delete a bitfield page.
151
+
152
+ #### `tx.deleteBitfieldPageRange(start, end)`
153
+
154
+ Delete bitfield pages between two indexes.
155
+
156
+ #### `tx.putUserData(key, value)`
157
+
158
+ Put a user provided buffer at a user provided key.
159
+
160
+ #### `tx.deleteUserData(key)`
161
+
162
+ Delete a user provided key.
163
+
164
+ #### `await tx.flush()`
165
+
166
+ Flushes the write batch.
167
+
168
+ #### `stream = core.createBlockStream(opts)`
169
+
170
+ Create a stream of all blocks.
171
+
172
+ #### `stream = core.createTreeNodeStream(opts)`
173
+
174
+ Create a stream of all tree nodes.
175
+
176
+ #### `stream = core.createBitfieldStream(opts)`
177
+
178
+ Create a stream of all bitfield pages.
179
+
180
+ #### `stream = core.createUserDataStream(opts)`
181
+
182
+ Create a stream of all user data.
183
+
184
+ #### `await core.close()`
185
+
186
+ Close the core storage engine.
187
+
188
+ #### `atom = core.createAtom()`
189
+
190
+ Same as `store.createAtom()` but here again for conveinience.
191
+
192
+ #### `core = core.atomize(atom)`
193
+
194
+ Atomize a core. Allows you to build up cross core atomic batches and operations.
195
+ An atomized core will not flush its changes until you call `atom.flush()`, but you can still read your writes.
196
+
197
+ #### `core = core.createSession(name, head)`
198
+
199
+ Create a named session on top of a core. A named session points back to the previous storage,
200
+ but is otherwise independent and stored on disk, like a branch in git if you will.
201
+
202
+ #### `core.dependencies`
203
+
204
+ Array containing the full list of dependencies for this core (ie tree of named sessions).
14
205
 
15
206
  ## License
16
207
 
package/index.js CHANGED
@@ -28,6 +28,7 @@ class Atom {
28
28
  constructor (db) {
29
29
  this.db = db
30
30
  this.view = new View()
31
+ this.flushing = false
31
32
  this.flushes = []
32
33
  }
33
34
 
@@ -36,13 +37,21 @@ class Atom {
36
37
  }
37
38
 
38
39
  async flush () {
39
- await View.flush(this.view.changes, this.db)
40
- this.view.reset()
40
+ if (this.flushing) throw new Error('Atom already flushing')
41
+ this.flushing = true
41
42
 
42
- const promises = []
43
- while (this.flushes.length) promises.push(this.flushes.pop()())
43
+ try {
44
+ await View.flush(this.view.changes, this.db)
45
+ this.view.reset()
46
+
47
+ const promises = []
48
+ const len = this.flushing.length // in case of reentry
49
+ for (let i = 0; i < len; i++) promises.push(this.flushes[i]())
44
50
 
45
- await Promise.all(promises)
51
+ await Promise.all(promises)
52
+ } finally {
53
+ this.flushing = false
54
+ }
46
55
  }
47
56
  }
48
57
 
@@ -99,6 +108,7 @@ class HypercoreStorage {
99
108
  }
100
109
 
101
110
  atomize (atom) {
111
+ if (this.atom && this.atom !== atom) throw new Error('Cannot atomize and atomized session with a new atom')
102
112
  return new HypercoreStorage(this.store, this.db.session(), this.core, atom.view, atom)
103
113
  }
104
114
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
@@ -401,8 +401,8 @@ const encoding13 = {
401
401
  }
402
402
  }
403
403
 
404
- // @core/sessions
405
- const encoding14 = c.array({
404
+ // @core/session
405
+ const encoding14 = {
406
406
  preencode (state, m) {
407
407
  c.string.preencode(state, m.name)
408
408
  c.uint.preencode(state, m.dataPointer)
@@ -420,10 +420,13 @@ const encoding14 = c.array({
420
420
  dataPointer: r1
421
421
  }
422
422
  }
423
- })
423
+ }
424
+
425
+ // @core/sessions
426
+ const encoding15 = c.array(encoding14)
424
427
 
425
428
  // @core/dependency
426
- const encoding15 = {
429
+ const encoding16 = {
427
430
  preencode (state, m) {
428
431
  c.uint.preencode(state, m.dataPointer)
429
432
  c.uint.preencode(state, m.length)
@@ -481,8 +484,9 @@ function getEncoding (name) {
481
484
  case '@core/auth': return encoding11
482
485
  case '@core/head': return encoding12
483
486
  case '@core/hints': return encoding13
484
- case '@core/sessions': return encoding14
485
- case '@core/dependency': return encoding15
487
+ case '@core/session': return encoding14
488
+ case '@core/sessions': return encoding15
489
+ case '@core/dependency': return encoding16
486
490
  default: throw new Error('Encoder not found ' + name)
487
491
  }
488
492
  }