hypercore 10.0.0-alpha.46 → 10.0.0-alpha.49

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
@@ -66,7 +66,8 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
66
66
  valueEncoding: 'json' | 'utf-8' | 'binary', // defaults to binary
67
67
  encodeBatch: batch => { ... }, // optionally apply an encoding to complete batches
68
68
  keyPair: kp, // optionally pass the public key and secret key as a key pair
69
- encryptionKey: k // optionally pass an encryption key to enable block encryption
69
+ encryptionKey: k, // optionally pass an encryption key to enable block encryption
70
+ onwait: () => {} // hook that is called if gets are waiting for download
70
71
  }
71
72
  ```
72
73
 
package/index.js CHANGED
@@ -70,6 +70,7 @@ module.exports = class Hypercore extends EventEmitter {
70
70
  this.sessions = opts._sessions || [this]
71
71
  this.auth = opts.auth || null
72
72
  this.autoClose = !!opts.autoClose
73
+ this.onwait = opts.onwait || null
73
74
 
74
75
  this.closing = null
75
76
  this.opening = this._openSession(key, storage, opts)
@@ -113,6 +114,7 @@ module.exports = class Hypercore extends EventEmitter {
113
114
  indent + ' opened: ' + opts.stylize(this.opened, 'boolean') + '\n' +
114
115
  indent + ' closed: ' + opts.stylize(this.closed, 'boolean') + '\n' +
115
116
  indent + ' snapshotted: ' + opts.stylize(this.snapshotted, 'boolean') + '\n' +
117
+ indent + ' sparse: ' + opts.stylize(this.sparse, 'boolean') + '\n' +
116
118
  indent + ' writable: ' + opts.stylize(this.writable, 'boolean') + '\n' +
117
119
  indent + ' length: ' + opts.stylize(this.length, 'number') + '\n' +
118
120
  indent + ' byteLength: ' + opts.stylize(this.byteLength, 'number') + '\n' +
@@ -182,9 +184,13 @@ module.exports = class Hypercore extends EventEmitter {
182
184
  throw SESSION_CLOSED('Cannot make sessions on a closing core')
183
185
  }
184
186
 
187
+ const sparse = opts.sparse === false ? false : this.sparse
188
+ const onwait = opts.onwait === undefined ? this.onwait : opts.onwait
185
189
  const Clz = opts.class || Hypercore
186
190
  const s = new Clz(this.storage, this.key, {
187
191
  ...opts,
192
+ sparse,
193
+ onwait,
188
194
  _opening: this.opening,
189
195
  _sessions: this.sessions
190
196
  })
@@ -324,14 +330,27 @@ module.exports = class Hypercore extends EventEmitter {
324
330
  }
325
331
  }
326
332
 
327
- _updateSnapshot () {
328
- const prev = this._snapshot
329
- const next = this._snapshot = {
330
- length: this.core.tree.length,
331
- byteLength: this.core.tree.byteLength,
333
+ _getSnapshot () {
334
+ if (this.sparse) {
335
+ return {
336
+ length: this.core.tree.length,
337
+ byteLength: this.core.tree.byteLength,
338
+ fork: this.core.tree.fork,
339
+ compatLength: this.core.tree.length
340
+ }
341
+ }
342
+
343
+ return {
344
+ length: this.core.header.contiguousLength,
345
+ byteLength: 0,
332
346
  fork: this.core.tree.fork,
333
- compatLength: this.core.tree.length
347
+ compatLength: this.core.header.contiguousLength
334
348
  }
349
+ }
350
+
351
+ _updateSnapshot () {
352
+ const prev = this._snapshot
353
+ const next = this._snapshot = this._getSnapshot()
335
354
 
336
355
  if (!prev) return true
337
356
  return prev.length !== next.length || prev.fork !== next.fork
@@ -573,14 +592,17 @@ module.exports = class Hypercore extends EventEmitter {
573
592
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
574
593
  const req = this.replicator.addUpgrade(activeRequests)
575
594
 
576
- const upgraded = await req.promise
595
+ let upgraded = await req.promise
577
596
 
578
597
  if (!this.sparse) {
579
598
  // Download all available blocks in non-sparse mode
580
599
  const start = this.length
581
600
  const end = this.core.tree.length
601
+ const contig = this.contiguousLength
582
602
 
583
603
  await this.download({ start, end, ifAvailable: true }).downloaded()
604
+
605
+ if (!upgraded) upgraded = this.contiguousLength !== contig
584
606
  }
585
607
 
586
608
  if (!upgraded) return false
@@ -643,6 +665,7 @@ module.exports = class Hypercore extends EventEmitter {
643
665
  } else {
644
666
  if (opts && opts.wait === false) return null
645
667
  if (opts && opts.onwait) opts.onwait(index)
668
+ else if (this.onwait) this.onwait(index)
646
669
 
647
670
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
648
671
 
package/lib/errors.js CHANGED
@@ -1,42 +1,50 @@
1
1
  module.exports = class HypercoreError extends Error {
2
- constructor (msg, code) {
3
- super(msg)
2
+ constructor (msg, code, fn = HypercoreError) {
3
+ super(`${code}: ${msg}`)
4
4
  this.code = code
5
+
6
+ if (Error.captureStackTrace) {
7
+ Error.captureStackTrace(this, fn)
8
+ }
9
+ }
10
+
11
+ get name () {
12
+ return 'HypercoreError'
5
13
  }
6
14
 
7
15
  static BAD_ARGUMENT (msg) {
8
- return new HypercoreError(msg, 'BAD_ARGUMENT')
16
+ return new HypercoreError(msg, 'BAD_ARGUMENT', HypercoreError.BAD_ARGUMENT)
9
17
  }
10
18
 
11
19
  static STORAGE_EMPTY (msg) {
12
- return new HypercoreError(msg, 'STORAGE_EMPTY')
20
+ return new HypercoreError(msg, 'STORAGE_EMPTY', HypercoreError.STORAGE_EMPTY)
13
21
  }
14
22
 
15
23
  static STORAGE_CONFLICT (msg) {
16
- return new HypercoreError(msg, 'STORAGE_CONFLICT')
24
+ return new HypercoreError(msg, 'STORAGE_CONFLICT', HypercoreError.STORAGE_CONFLICT)
17
25
  }
18
26
 
19
27
  static INVALID_SIGNATURE (msg) {
20
- return new HypercoreError(msg, 'INVALID_SIGNATURE')
28
+ return new HypercoreError(msg, 'INVALID_SIGNATURE', HypercoreError.INVALID_SIGNATURE)
21
29
  }
22
30
 
23
31
  static INVALID_CAPABILITY (msg) {
24
- return new HypercoreError(msg, 'INVALID_CAPABILITY')
32
+ return new HypercoreError(msg, 'INVALID_CAPABILITY', HypercoreError.INVALID_CAPABILITY)
25
33
  }
26
34
 
27
35
  static SNAPSHOT_NOT_AVAILABLE (msg = 'Snapshot is not available') {
28
- return new HypercoreError(msg, 'SNAPSHOT_NOT_AVAILABLE')
36
+ return new HypercoreError(msg, 'SNAPSHOT_NOT_AVAILABLE', HypercoreError.SNAPSHOT_NOT_AVAILABLE)
29
37
  }
30
38
 
31
39
  static REQUEST_CANCELLED (msg = 'Request was cancelled') {
32
- return new HypercoreError(msg, 'REQUEST_CANCELLED')
40
+ return new HypercoreError(msg, 'REQUEST_CANCELLED', HypercoreError.REQUEST_CANCELLED)
33
41
  }
34
42
 
35
43
  static SESSION_NOT_WRITABLE (msg = 'Session is not writable') {
36
- return new HypercoreError(msg, 'SESSION_NOT_WRITABLE')
44
+ return new HypercoreError(msg, 'SESSION_NOT_WRITABLE', HypercoreError.SESSION_NOT_WRITABLE)
37
45
  }
38
46
 
39
47
  static SESSION_CLOSED (msg = 'Session is closed') {
40
- return new HypercoreError(msg, 'SESSION_CLOSED')
48
+ return new HypercoreError(msg, 'SESSION_CLOSED', HypercoreError.SESSION_CLOSED)
41
49
  }
42
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.46",
3
+ "version": "10.0.0-alpha.49",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {