hypercore 10.6.0 → 10.7.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 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
- Wait for the core to try and find a signed update to its length.
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,6 +135,16 @@ const updated = await core.update()
135
135
  console.log('core was updated?', updated, 'length is', core.length)
136
136
  ```
137
137
 
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
+
138
148
  #### `const [index, relativeOffset] = await core.seek(byteOffset)`
139
149
 
140
150
  Seek to a byte offset.
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 = typeof (opts && opts.wait) === 'boolean' ? opts.wait : this._findingPeers > 0
652
+ const remoteWait = typeof (opts && opts.wait) === 'boolean' ? opts.wait : this.replicator.findingPeers > 0
650
653
 
651
654
  let upgraded = false
652
655
 
@@ -706,7 +709,7 @@ module.exports = class Hypercore extends EventEmitter {
706
709
  let block = await req
707
710
  if (!block) return null
708
711
 
709
- if (this.encryption) {
712
+ if (this.encryption && (!opts || opts.decrypt !== false)) {
710
713
  // Copy the block as it might be shared with other sessions.
711
714
  block = b4a.from(block)
712
715
 
@@ -747,6 +750,9 @@ module.exports = class Hypercore extends EventEmitter {
747
750
 
748
751
  const req = this.replicator.addBlock(activeRequests, index)
749
752
 
753
+ const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
754
+ if (timeout) req.context.setTimeout(req, timeout)
755
+
750
756
  block = this._cacheOnResolve(index, req.promise, this.core.tree.fork)
751
757
  }
752
758
 
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.6.0",
3
+ "version": "10.7.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {