hypercore 10.7.0 → 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.
Files changed (3) hide show
  1. package/README.md +9 -2
  2. package/index.js +16 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -145,7 +145,7 @@ console.log('core was updated?', updated, 'length is', core.length)
145
145
 
146
146
  Use `core.findingPeers()` or `{ wait: true }` to make `await core.update()` blocking.
147
147
 
148
- #### `const [index, relativeOffset] = await core.seek(byteOffset)`
148
+ #### `const [index, relativeOffset] = await core.seek(byteOffset, [options])`
149
149
 
150
150
  Seek to a byte offset.
151
151
 
@@ -160,6 +160,13 @@ const second = await core.seek(3) // returns [1, 0]
160
160
  const third = await core.seek(5) // returns [2, 1]
161
161
  ```
162
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
+
163
170
  #### `const stream = core.createReadStream([options])`
164
171
 
165
172
  Make a read stream to read a range of data out at once.
@@ -292,7 +299,7 @@ Fully close this core.
292
299
 
293
300
  #### `core.on('close')`
294
301
 
295
- Emitted when then core has been fully closed.
302
+ Emitted when the core has been fully closed.
296
303
 
297
304
  #### `await core.ready()`
298
305
 
package/index.js CHANGED
@@ -649,7 +649,7 @@ module.exports = class Hypercore extends EventEmitter {
649
649
  return this._updateSnapshot()
650
650
  }
651
651
 
652
- const remoteWait = typeof (opts && opts.wait) === 'boolean' ? opts.wait : this.replicator.findingPeers > 0
652
+ const remoteWait = this._shouldWait(opts, this.replicator.findingPeers > 0)
653
653
 
654
654
  let upgraded = false
655
655
 
@@ -679,9 +679,14 @@ module.exports = class Hypercore extends EventEmitter {
679
679
 
680
680
  if (this.closing !== null) throw SESSION_CLOSED()
681
681
 
682
+ if (!this._shouldWait(opts, this.wait)) return null
683
+
682
684
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
683
685
  const req = this.replicator.addSeek(activeRequests, s)
684
686
 
687
+ const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
688
+ if (timeout) req.context.setTimeout(req, timeout)
689
+
685
690
  return req.promise
686
691
  }
687
692
 
@@ -741,8 +746,8 @@ module.exports = class Hypercore extends EventEmitter {
741
746
 
742
747
  if (this.cache) this.cache.set(index, block)
743
748
  } else {
744
- if (opts && opts.wait === false) return null
745
- if (this.wait === false && (!opts || !opts.wait)) return null
749
+ if (!this._shouldWait(opts, this.wait)) return null
750
+
746
751
  if (opts && opts.onwait) opts.onwait(index, this)
747
752
  if (this.onwait) this.onwait(index, this)
748
753
 
@@ -769,6 +774,14 @@ module.exports = class Hypercore extends EventEmitter {
769
774
  return block
770
775
  }
771
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
+
772
785
  createReadStream (opts) {
773
786
  return new ReadStream(this, opts)
774
787
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.7.0",
3
+ "version": "10.8.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {