hypercore 10.6.1 → 10.8.0
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 +23 -6
- package/index.js +23 -4
- package/lib/errors.js +4 -0
- package/lib/replicator.js +20 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,7 +116,8 @@ const blockLocal = await core.get(44, { wait: false })
|
|
|
116
116
|
wait: true, // wait for block to be downloaded
|
|
117
117
|
onwait: () => {}, // hook that is called if the get is waiting for download
|
|
118
118
|
timeout: 0, // wait at max some milliseconds (0 means no timeout)
|
|
119
|
-
valueEncoding: 'json' | 'utf-8' | 'binary' // defaults to the core's valueEncoding
|
|
119
|
+
valueEncoding: 'json' | 'utf-8' | 'binary', // defaults to the core's valueEncoding
|
|
120
|
+
decrypt: true // automatically decrypts the block if encrypted
|
|
120
121
|
}
|
|
121
122
|
```
|
|
122
123
|
|
|
@@ -124,10 +125,9 @@ const blockLocal = await core.get(44, { wait: false })
|
|
|
124
125
|
|
|
125
126
|
Check if the core has all blocks between `start` and `end`.
|
|
126
127
|
|
|
127
|
-
#### `const updated = await core.update()`
|
|
128
|
+
#### `const updated = await core.update([options])`
|
|
128
129
|
|
|
129
|
-
|
|
130
|
-
Does not download any data from peers except for a proof of the new core length.
|
|
130
|
+
Waits for initial proof of the new core length until all `findingPeers` calls has finished.
|
|
131
131
|
|
|
132
132
|
``` js
|
|
133
133
|
const updated = await core.update()
|
|
@@ -135,7 +135,17 @@ const updated = await core.update()
|
|
|
135
135
|
console.log('core was updated?', updated, 'length is', core.length)
|
|
136
136
|
```
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
`options` include:
|
|
139
|
+
|
|
140
|
+
``` js
|
|
141
|
+
{
|
|
142
|
+
wait: false
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Use `core.findingPeers()` or `{ wait: true }` to make `await core.update()` blocking.
|
|
147
|
+
|
|
148
|
+
#### `const [index, relativeOffset] = await core.seek(byteOffset, [options])`
|
|
139
149
|
|
|
140
150
|
Seek to a byte offset.
|
|
141
151
|
|
|
@@ -150,6 +160,13 @@ const second = await core.seek(3) // returns [1, 0]
|
|
|
150
160
|
const third = await core.seek(5) // returns [2, 1]
|
|
151
161
|
```
|
|
152
162
|
|
|
163
|
+
``` js
|
|
164
|
+
{
|
|
165
|
+
wait: true, // wait for data to be downloaded
|
|
166
|
+
timeout: 0 // wait at max some milliseconds (0 means no timeout)
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
153
170
|
#### `const stream = core.createReadStream([options])`
|
|
154
171
|
|
|
155
172
|
Make a read stream to read a range of data out at once.
|
|
@@ -282,7 +299,7 @@ Fully close this core.
|
|
|
282
299
|
|
|
283
300
|
#### `core.on('close')`
|
|
284
301
|
|
|
285
|
-
Emitted when
|
|
302
|
+
Emitted when the core has been fully closed.
|
|
286
303
|
|
|
287
304
|
#### `await core.ready()`
|
|
288
305
|
|
package/index.js
CHANGED
|
@@ -73,6 +73,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
73
73
|
this.autoClose = !!opts.autoClose
|
|
74
74
|
this.onwait = opts.onwait || null
|
|
75
75
|
this.wait = opts.wait !== false
|
|
76
|
+
this.timeout = opts.timeout || 0
|
|
76
77
|
|
|
77
78
|
this.closing = null
|
|
78
79
|
this.opening = this._openSession(key, storage, opts)
|
|
@@ -200,12 +201,14 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
200
201
|
const sparse = opts.sparse === false ? false : this.sparse
|
|
201
202
|
const wait = opts.wait === false ? false : this.wait
|
|
202
203
|
const onwait = opts.onwait === undefined ? this.onwait : opts.onwait
|
|
204
|
+
const timeout = opts.timeout === undefined ? this.timeout : opts.timeout
|
|
203
205
|
const Clz = opts.class || Hypercore
|
|
204
206
|
const s = new Clz(this.storage, this.key, {
|
|
205
207
|
...opts,
|
|
206
208
|
sparse,
|
|
207
209
|
wait,
|
|
208
210
|
onwait,
|
|
211
|
+
timeout,
|
|
209
212
|
_opening: this.opening,
|
|
210
213
|
_sessions: this.sessions
|
|
211
214
|
})
|
|
@@ -646,7 +649,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
646
649
|
return this._updateSnapshot()
|
|
647
650
|
}
|
|
648
651
|
|
|
649
|
-
const remoteWait =
|
|
652
|
+
const remoteWait = this._shouldWait(opts, this.replicator.findingPeers > 0)
|
|
650
653
|
|
|
651
654
|
let upgraded = false
|
|
652
655
|
|
|
@@ -676,9 +679,14 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
676
679
|
|
|
677
680
|
if (this.closing !== null) throw SESSION_CLOSED()
|
|
678
681
|
|
|
682
|
+
if (!this._shouldWait(opts, this.wait)) return null
|
|
683
|
+
|
|
679
684
|
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
|
|
680
685
|
const req = this.replicator.addSeek(activeRequests, s)
|
|
681
686
|
|
|
687
|
+
const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
|
|
688
|
+
if (timeout) req.context.setTimeout(req, timeout)
|
|
689
|
+
|
|
682
690
|
return req.promise
|
|
683
691
|
}
|
|
684
692
|
|
|
@@ -706,7 +714,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
706
714
|
let block = await req
|
|
707
715
|
if (!block) return null
|
|
708
716
|
|
|
709
|
-
if (this.encryption) {
|
|
717
|
+
if (this.encryption && (!opts || opts.decrypt !== false)) {
|
|
710
718
|
// Copy the block as it might be shared with other sessions.
|
|
711
719
|
block = b4a.from(block)
|
|
712
720
|
|
|
@@ -738,8 +746,8 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
738
746
|
|
|
739
747
|
if (this.cache) this.cache.set(index, block)
|
|
740
748
|
} else {
|
|
741
|
-
if (opts
|
|
742
|
-
|
|
749
|
+
if (!this._shouldWait(opts, this.wait)) return null
|
|
750
|
+
|
|
743
751
|
if (opts && opts.onwait) opts.onwait(index, this)
|
|
744
752
|
if (this.onwait) this.onwait(index, this)
|
|
745
753
|
|
|
@@ -747,6 +755,9 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
747
755
|
|
|
748
756
|
const req = this.replicator.addBlock(activeRequests, index)
|
|
749
757
|
|
|
758
|
+
const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
|
|
759
|
+
if (timeout) req.context.setTimeout(req, timeout)
|
|
760
|
+
|
|
750
761
|
block = this._cacheOnResolve(index, req.promise, this.core.tree.fork)
|
|
751
762
|
}
|
|
752
763
|
|
|
@@ -763,6 +774,14 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
763
774
|
return block
|
|
764
775
|
}
|
|
765
776
|
|
|
777
|
+
_shouldWait (opts, defaultValue) {
|
|
778
|
+
if (opts) {
|
|
779
|
+
if (opts.wait === false) return false
|
|
780
|
+
if (opts.wait === true) return true
|
|
781
|
+
}
|
|
782
|
+
return defaultValue
|
|
783
|
+
}
|
|
784
|
+
|
|
766
785
|
createReadStream (opts) {
|
|
767
786
|
return new ReadStream(this, opts)
|
|
768
787
|
}
|
package/lib/errors.js
CHANGED
|
@@ -52,6 +52,10 @@ module.exports = class HypercoreError extends Error {
|
|
|
52
52
|
return new HypercoreError(msg, 'REQUEST_CANCELLED', HypercoreError.REQUEST_CANCELLED)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
static REQUEST_TIMEOUT (msg = 'Request timed out') {
|
|
56
|
+
return new HypercoreError(msg, 'REQUEST_TIMEOUT', HypercoreError.REQUEST_TIMEOUT)
|
|
57
|
+
}
|
|
58
|
+
|
|
55
59
|
static SESSION_NOT_WRITABLE (msg = 'Session is not writable') {
|
|
56
60
|
return new HypercoreError(msg, 'SESSION_NOT_WRITABLE', HypercoreError.SESSION_NOT_WRITABLE)
|
|
57
61
|
}
|
package/lib/replicator.js
CHANGED
|
@@ -2,7 +2,7 @@ const b4a = require('b4a')
|
|
|
2
2
|
const safetyCatch = require('safety-catch')
|
|
3
3
|
const RandomIterator = require('random-array-iterator')
|
|
4
4
|
const RemoteBitfield = require('./remote-bitfield')
|
|
5
|
-
const { REQUEST_CANCELLED, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE } = require('./errors')
|
|
5
|
+
const { REQUEST_CANCELLED, REQUEST_TIMEOUT, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE } = require('./errors')
|
|
6
6
|
const m = require('./messages')
|
|
7
7
|
const caps = require('./caps')
|
|
8
8
|
|
|
@@ -24,7 +24,8 @@ class Attachable {
|
|
|
24
24
|
snapshot: true,
|
|
25
25
|
resolve: null,
|
|
26
26
|
reject: null,
|
|
27
|
-
promise: null
|
|
27
|
+
promise: null,
|
|
28
|
+
timeout: null
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
r.sindex = session.push(r) - 1
|
|
@@ -54,6 +55,7 @@ class Attachable {
|
|
|
54
55
|
if (r.rindex < this.refs.length) this.refs[rh.rindex = r.rindex] = rh
|
|
55
56
|
if (r.sindex < r.session.length) r.session[sh.sindex = r.sindex] = sh
|
|
56
57
|
|
|
58
|
+
destroyRequestTimeout(r)
|
|
57
59
|
r.context = null
|
|
58
60
|
|
|
59
61
|
return r
|
|
@@ -84,6 +86,11 @@ class Attachable {
|
|
|
84
86
|
this._detach(this.refs[this.refs.length - 1]).reject(err)
|
|
85
87
|
}
|
|
86
88
|
}
|
|
89
|
+
|
|
90
|
+
setTimeout (r, ms) {
|
|
91
|
+
destroyRequestTimeout(r)
|
|
92
|
+
r.timeout = setTimeout(onrequesttimeout, ms, r)
|
|
93
|
+
}
|
|
87
94
|
}
|
|
88
95
|
|
|
89
96
|
class BlockRequest extends Attachable {
|
|
@@ -1684,6 +1691,17 @@ function clampRange (core, r) {
|
|
|
1684
1691
|
}
|
|
1685
1692
|
}
|
|
1686
1693
|
|
|
1694
|
+
function onrequesttimeout (req) {
|
|
1695
|
+
if (req.context) req.context.detach(req, REQUEST_TIMEOUT())
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
function destroyRequestTimeout (req) {
|
|
1699
|
+
if (req.timeout !== null) {
|
|
1700
|
+
clearTimeout(req.timeout)
|
|
1701
|
+
req.timeout = null
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1687
1705
|
function onwireopen (m, c) {
|
|
1688
1706
|
return c.userData.onopen(m)
|
|
1689
1707
|
}
|