hypercore 10.33.4 → 10.34.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
@@ -9,6 +9,7 @@ const NoiseSecretStream = require('@hyperswarm/secret-stream')
9
9
  const Protomux = require('protomux')
10
10
  const z32 = require('z32')
11
11
  const id = require('hypercore-id-encoding')
12
+ const safetyCatch = require('safety-catch')
12
13
  const { createTracer } = require('hypertrace')
13
14
 
14
15
  const Replicator = require('./lib/replicator')
@@ -92,7 +93,7 @@ module.exports = class Hypercore extends EventEmitter {
92
93
  this._active = opts.active !== false
93
94
 
94
95
  this.opening = this._openSession(key, storage, opts)
95
- this.opening.catch(noop)
96
+ this.opening.catch(safetyCatch)
96
97
  }
97
98
 
98
99
  [inspect] (depth, opts) {
@@ -874,6 +875,8 @@ module.exports = class Hypercore extends EventEmitter {
874
875
  let block = await req
875
876
  if (!block) return null
876
877
 
878
+ if (opts && opts.raw) return block
879
+
877
880
  if (this.encryption && (!opts || opts.decrypt !== false)) {
878
881
  // Copy the block as it might be shared with other sessions.
879
882
  block = b4a.from(block)
@@ -972,7 +975,7 @@ module.exports = class Hypercore extends EventEmitter {
972
975
  const req = this._download(range)
973
976
 
974
977
  // do not crash in the background...
975
- req.catch(noop)
978
+ req.catch(safetyCatch)
976
979
 
977
980
  return new Download(req)
978
981
  }
@@ -1127,8 +1130,6 @@ module.exports = class Hypercore extends EventEmitter {
1127
1130
  }
1128
1131
  }
1129
1132
 
1130
- function noop () {}
1131
-
1132
1133
  function isStream (s) {
1133
1134
  return typeof s === 'object' && s && typeof s.pipe === 'function'
1134
1135
  }
package/lib/batch.js CHANGED
@@ -2,6 +2,7 @@ const { BLOCK_NOT_AVAILABLE, SESSION_CLOSED } = require('hypercore-errors')
2
2
  const EventEmitter = require('events')
3
3
  const c = require('compact-encoding')
4
4
  const b4a = require('b4a')
5
+ const safetyCatch = require('safety-catch')
5
6
 
6
7
  module.exports = class HypercoreBatch extends EventEmitter {
7
8
  constructor (session, checkoutLength, autoClose, restore, clear) {
@@ -28,7 +29,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
28
29
  this._clear = clear
29
30
 
30
31
  this.opening = this._open()
31
- this.opening.catch(noop)
32
+ this.opening.catch(safetyCatch)
32
33
  }
33
34
 
34
35
  get id () {
@@ -169,12 +170,19 @@ module.exports = class HypercoreBatch extends EventEmitter {
169
170
  throw BLOCK_NOT_AVAILABLE()
170
171
  }
171
172
 
172
- async get (index, opts) {
173
+ async get (index, opts = {}) {
173
174
  if (this.opened === false) await this.opening
174
175
  if (this.closing) throw SESSION_CLOSED()
175
176
 
176
177
  const length = this._sessionLength
177
- if (index < length) return this.session.get(index, { ...opts, tree: this._sessionBatch })
178
+
179
+ if (index < length) {
180
+ return this.session.get(index, { ...opts, tree: this._sessionBatch })
181
+ }
182
+
183
+ if (opts && opts.raw) {
184
+ return this._appendsActual[index - length] || null
185
+ }
178
186
 
179
187
  const buffer = this._appends[index - length] || null
180
188
  if (!buffer) throw BLOCK_NOT_AVAILABLE()
@@ -404,5 +412,3 @@ module.exports = class HypercoreBatch extends EventEmitter {
404
412
  this.fork = 0
405
413
  }
406
414
  }
407
-
408
- function noop () {}
package/lib/core.js CHANGED
@@ -124,6 +124,8 @@ module.exports = class Core {
124
124
  if (!opts.key && !Verifier.isValidManifest(header.key, Verifier.createManifest(opts.manifest))) {
125
125
  throw STORAGE_CONFLICT('Manifest does not hash to provided key')
126
126
  }
127
+
128
+ if (!header.manifest) header.manifest = opts.manifest
127
129
  }
128
130
 
129
131
  if (opts.key && !b4a.equals(header.key, opts.key)) {
@@ -224,7 +226,7 @@ module.exports = class Core {
224
226
  return false
225
227
  }
226
228
 
227
- async copyFrom (src, signature, { length = src.tree.length, fork = src.tree.fork, additional = [] } = {}) {
229
+ async copyFrom (src, signature, { length = src.tree.length, sourceLength = src.tree.length, fork = src.tree.fork, additional = [] } = {}) {
228
230
  await this._mutex.lock()
229
231
 
230
232
  try {
@@ -279,13 +281,16 @@ module.exports = class Core {
279
281
 
280
282
  await this.tree.flush()
281
283
 
284
+ this.tree.fork = fork
285
+
282
286
  let batch = this.tree.batch()
283
287
 
284
288
  // add additional blocks
285
289
  if (length > src.tree.length) {
286
290
  const missing = length - src.tree.length
291
+ const offset = src.tree.length - sourceLength
287
292
 
288
- if (additional.length < missing) {
293
+ if (additional.length < missing + offset) {
289
294
  throw INVALID_OPERATION('Insufficient additional nodes were passed')
290
295
  }
291
296
 
@@ -295,7 +300,7 @@ module.exports = class Core {
295
300
  batch.length = source.length
296
301
  batch.byteLength = source.byteLength
297
302
 
298
- const blocks = additional.length === missing ? additional : additional.slice(0, missing)
303
+ const blocks = additional.length === missing ? additional : additional.slice(offset, offset + missing)
299
304
 
300
305
  await this.blocks.putBatch(source.length, blocks, source.byteLength)
301
306
  this.bitfield.setRange(source.length, missing, true)
@@ -334,9 +339,8 @@ module.exports = class Core {
334
339
 
335
340
  await batch.commit()
336
341
 
337
- this.tree.fork = fork
338
-
339
342
  this.header.tree.length = this.tree.length
343
+ this.header.tree.fork = this.tree.fork
340
344
  this.header.tree.rootHash = this.tree.hash()
341
345
  this.header.tree.signature = this.tree.signature
342
346
 
package/lib/replicator.js CHANGED
@@ -1087,7 +1087,7 @@ class Peer {
1087
1087
  return false
1088
1088
  }
1089
1089
 
1090
- const end = Math.min(r.end === -1 ? this.remoteLength : r.end, this.remoteLength)
1090
+ const end = Math.min(this.core.tree.length, Math.min(r.end === -1 ? this.remoteLength : r.end, this.remoteLength))
1091
1091
  if (end <= r.start || fork !== this.remoteFork) return false
1092
1092
 
1093
1093
  const len = end - r.start
@@ -2012,7 +2012,7 @@ module.exports = class Replicator {
2012
2012
  }
2013
2013
 
2014
2014
  _closeSession (session) {
2015
- session.close().catch(noop)
2015
+ session.close().catch(safetyCatch)
2016
2016
  }
2017
2017
 
2018
2018
  attached (protomux) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.33.4",
3
+ "version": "10.34.1",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {