hypercore 11.17.0 → 11.18.1

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/index.js CHANGED
@@ -10,6 +10,7 @@ const id = require('hypercore-id-encoding')
10
10
  const safetyCatch = require('safety-catch')
11
11
  const unslab = require('unslab')
12
12
 
13
+ const inspect = require('./lib/inspect')
13
14
  const Core = require('./lib/core')
14
15
  const Info = require('./lib/info')
15
16
  const Download = require('./lib/download')
@@ -29,7 +30,7 @@ const {
29
30
  DECODING_ERROR
30
31
  } = require('hypercore-errors')
31
32
 
32
- const inspect = Symbol.for('nodejs.util.inspect.custom')
33
+ const inspectSymbol = Symbol.for('nodejs.util.inspect.custom')
33
34
 
34
35
  // Hypercore actually does not have any notion of max/min block sizes
35
36
  // but we enforce 15mb to ensure smooth replication (each block is transmitted atomically)
@@ -96,92 +97,8 @@ class Hypercore extends EventEmitter {
96
97
  this.on('newListener', maybeAddMonitor)
97
98
  }
98
99
 
99
- [inspect](depth, opts) {
100
- let indent = ''
101
- if (typeof opts.indentationLvl === 'number') {
102
- while (indent.length < opts.indentationLvl) indent += ' '
103
- }
104
-
105
- let peers = ''
106
- const min = Math.min(this.peers.length, 5)
107
-
108
- for (let i = 0; i < min; i++) {
109
- const peer = this.peers[i]
110
-
111
- peers += indent + ' Peer(\n'
112
- peers +=
113
- indent +
114
- ' remotePublicKey: ' +
115
- opts.stylize(toHex(peer.remotePublicKey), 'string') +
116
- '\n'
117
- peers += indent + ' remoteLength: ' + opts.stylize(peer.remoteLength, 'number') + '\n'
118
- peers += indent + ' remoteFork: ' + opts.stylize(peer.remoteFork, 'number') + '\n'
119
- peers +=
120
- indent + ' remoteCanUpgrade: ' + opts.stylize(peer.remoteCanUpgrade, 'boolean') + '\n'
121
- peers += indent + ' )' + '\n'
122
- }
123
-
124
- if (this.peers.length > 5) {
125
- peers += indent + ' ... and ' + (this.peers.length - 5) + ' more\n'
126
- }
127
-
128
- if (peers) peers = '[\n' + peers + indent + ' ]'
129
- else peers = '[ ' + opts.stylize(0, 'number') + ' ]'
130
-
131
- return (
132
- this.constructor.name +
133
- '(\n' +
134
- indent +
135
- ' id: ' +
136
- opts.stylize(this.id, 'string') +
137
- '\n' +
138
- indent +
139
- ' key: ' +
140
- opts.stylize(toHex(this.key), 'string') +
141
- '\n' +
142
- indent +
143
- ' discoveryKey: ' +
144
- opts.stylize(toHex(this.discoveryKey), 'string') +
145
- '\n' +
146
- indent +
147
- ' opened: ' +
148
- opts.stylize(this.opened, 'boolean') +
149
- '\n' +
150
- indent +
151
- ' closed: ' +
152
- opts.stylize(this.closed, 'boolean') +
153
- '\n' +
154
- indent +
155
- ' snapshotted: ' +
156
- opts.stylize(this.snapshotted, 'boolean') +
157
- '\n' +
158
- indent +
159
- ' writable: ' +
160
- opts.stylize(this.writable, 'boolean') +
161
- '\n' +
162
- indent +
163
- ' length: ' +
164
- opts.stylize(this.length, 'number') +
165
- '\n' +
166
- indent +
167
- ' fork: ' +
168
- opts.stylize(this.fork, 'number') +
169
- '\n' +
170
- indent +
171
- ' sessions: [ ' +
172
- opts.stylize(this.sessions.length, 'number') +
173
- ' ]\n' +
174
- indent +
175
- ' activeRequests: [ ' +
176
- opts.stylize(this.activeRequests.length, 'number') +
177
- ' ]\n' +
178
- indent +
179
- ' peers: ' +
180
- peers +
181
- '\n' +
182
- indent +
183
- ')'
184
- )
100
+ [inspectSymbol](depth, opts) {
101
+ return inspect(this, depth, opts)
185
102
  }
186
103
 
187
104
  static MAX_SUGGESTED_BLOCK_SIZE = MAX_SUGGESTED_BLOCK_SIZE
@@ -442,17 +359,12 @@ class Hypercore extends EventEmitter {
442
359
  throw ASSERTION('Checkouts must be named or atomized', this.discoveryKey)
443
360
  if (checkout > this.state.length)
444
361
  throw ASSERTION(
445
- 'Invalid checkout ' + checkout + ' for ' + opts.name + ', length is ' + this.state.length,
362
+ `Invalid checkout ${checkout} for ${opts.name}, length is ${this.state.length}`,
446
363
  this.discoveryKey
447
364
  )
448
365
  if (this.state.prologue && checkout < this.state.prologue.length) {
449
366
  throw ASSERTION(
450
- 'Invalid checkout ' +
451
- checkout +
452
- ' for ' +
453
- opts.name +
454
- ', prologue length is ' +
455
- this.state.prologue.length,
367
+ `Invalid checkout ${checkout} for ${opts.name}, prologue length is ${this.state.prologue.length}`,
456
368
  this.discoveryKey
457
369
  )
458
370
  }
@@ -1057,12 +969,26 @@ class Hypercore extends EventEmitter {
1057
969
  return crypto.tree(roots)
1058
970
  }
1059
971
 
972
+ async missingNodes(index) {
973
+ if (this.opened === false) await this.opening
974
+ return await MerkleTree.missingNodes(this.core.state, 2 * index, this.core.state.length)
975
+ }
976
+
1060
977
  async proof(opts) {
1061
978
  if (this.opened === false) await this.opening
1062
979
  const rx = this.state.storage.read()
1063
- const promise = MerkleTree.proof(this.state, rx, opts)
980
+ const proofPromise = MerkleTree.proof(this.state, rx, opts)
981
+ const blockPromise = opts && opts.block ? rx.getBlock(opts.block.index) : null
1064
982
  rx.tryFlush()
1065
- return promise
983
+ const [proof, block] = await Promise.all([proofPromise, blockPromise])
984
+ const settled = await proof.settle()
985
+ if (block) settled.block.value = block
986
+ return settled
987
+ }
988
+
989
+ async applyProof(proof, from) {
990
+ if (this.opened === false) await this.opening
991
+ return this.core.verify(proof, from)
1066
992
  }
1067
993
 
1068
994
  async verifyFullyRemote(proof) {
@@ -1165,10 +1091,6 @@ function isStream(s) {
1165
1091
  return typeof s === 'object' && s && typeof s.pipe === 'function'
1166
1092
  }
1167
1093
 
1168
- function toHex(buf) {
1169
- return buf && b4a.toString(buf, 'hex')
1170
- }
1171
-
1172
1094
  async function preappend(blocks) {
1173
1095
  const offset = this.state.length
1174
1096
  const fork = this.state.encryptionFork
package/lib/inspect.js ADDED
@@ -0,0 +1,50 @@
1
+ const b4a = require('b4a')
2
+
3
+ module.exports = function (core, depth, opts) {
4
+ let indent = ''
5
+ if (typeof opts.indentationLvl === 'number') {
6
+ while (indent.length < opts.indentationLvl) indent += ' '
7
+ }
8
+
9
+ let peers = ''
10
+ const min = Math.min(core.peers.length, 5)
11
+
12
+ for (let i = 0; i < min; i++) {
13
+ const peer = core.peers[i]
14
+
15
+ peers += `${indent} Peer(\n`
16
+ peers += `${indent} remotePublicKey: ${opts.stylize(toHex(peer.remotePublicKey), 'string')}\n`
17
+ peers += `${indent} remoteLength: ${opts.stylize(peer.remoteLength, 'number')}\n`
18
+ peers += `${indent} remoteFork: ${opts.stylize(peer.remoteFork, 'number')}\n`
19
+ peers += `${indent} remoteCanUpgrade: ${opts.stylize(peer.remoteCanUpgrade, 'boolean')}\n`
20
+ peers += `${indent} )\n`
21
+ }
22
+
23
+ if (core.peers.length > 5) {
24
+ peers += `${indent} ... and ${core.peers.length - 5} more\n`
25
+ }
26
+
27
+ if (peers) peers = `[\n${peers}${indent} ]`
28
+ else peers = `[ ${opts.stylize(0, 'number')} ]`
29
+
30
+ return (
31
+ `${core.constructor.name}(\n` +
32
+ `${indent} id: ${opts.stylize(core.id, 'string')}\n` +
33
+ `${indent} key: ${opts.stylize(toHex(core.key), 'string')}\n` +
34
+ `${indent} discoveryKey: ${opts.stylize(toHex(core.discoveryKey), 'string')}\n` +
35
+ `${indent} opened: ${opts.stylize(core.opened, 'boolean')}\n` +
36
+ `${indent} closed: ${opts.stylize(core.closed, 'boolean')}\n` +
37
+ `${indent} snapshotted: ${opts.stylize(core.snapshotted, 'boolean')}\n` +
38
+ `${indent} writable: ${opts.stylize(core.writable, 'boolean')}\n` +
39
+ `${indent} length: ${opts.stylize(core.length, 'number')}\n` +
40
+ `${indent} fork: ${opts.stylize(core.fork, 'number')}\n` +
41
+ `${indent} sessions: [ ${opts.stylize(core.sessions.length, 'number')} ]\n` +
42
+ `${indent} activeRequests: [ ${opts.stylize(core.activeRequests.length, 'number')} ]\n` +
43
+ `${indent} peers: ${peers}\n` +
44
+ `${indent})'`
45
+ )
46
+ }
47
+
48
+ function toHex(buf) {
49
+ return buf && b4a.toString(buf, 'hex')
50
+ }
@@ -0,0 +1,10 @@
1
+ class HypercoreReadBatch {
2
+ constructor(core) {
3
+ this.core = core
4
+ this.gets = []
5
+ }
6
+
7
+ get(index) {}
8
+
9
+ tryFlush() {}
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.17.0",
3
+ "version": "11.18.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {