hypercore 10.25.0 → 10.25.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/README.md +4 -4
- package/index.js +3 -3
- package/lib/batch.js +41 -4
- package/lib/replicator.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -283,15 +283,15 @@ A range can have the following properties:
|
|
|
283
283
|
}
|
|
284
284
|
```
|
|
285
285
|
|
|
286
|
-
To download the full core
|
|
286
|
+
To download the full core continuously (often referred to as non sparse mode) do
|
|
287
287
|
|
|
288
288
|
``` js
|
|
289
|
-
// Note that this will never be
|
|
289
|
+
// Note that this will never be considered downloaded as the range
|
|
290
290
|
// will keep waiting for new blocks to be appended.
|
|
291
291
|
core.download({ start: 0, end: -1 })
|
|
292
292
|
```
|
|
293
293
|
|
|
294
|
-
To
|
|
294
|
+
To download a discrete range of blocks pass a list of indices.
|
|
295
295
|
|
|
296
296
|
```js
|
|
297
297
|
core.download({ blocks: [4, 9, 7] })
|
|
@@ -434,7 +434,7 @@ How much padding is applied to each block of this core? Will be `0` unless block
|
|
|
434
434
|
|
|
435
435
|
Create a replication stream. You should pipe this to another Hypercore instance.
|
|
436
436
|
|
|
437
|
-
The `isInitiator` argument is a boolean indicating whether you are the
|
|
437
|
+
The `isInitiator` argument is a boolean indicating whether you are the initiator of the connection (ie the client)
|
|
438
438
|
or if you are the passive part (ie the server).
|
|
439
439
|
|
|
440
440
|
If you are using a P2P swarm like [Hyperswarm](https://github.com/hyperswarm/hyperswarm) you can know this by checking if the swarm connection is a client socket or server socket. In Hyperswarm you can check that using the [client property on the peer details object](https://github.com/hyperswarm/hyperswarm#swarmonconnection-socket-details--)
|
package/index.js
CHANGED
|
@@ -211,8 +211,8 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
211
211
|
|
|
212
212
|
session (opts = {}) {
|
|
213
213
|
if (this.closing) {
|
|
214
|
-
// This makes the closing logic
|
|
215
|
-
// in
|
|
214
|
+
// This makes the closing logic a lot easier. If this turns out to be a problem
|
|
215
|
+
// in practice, open an issue and we'll try to make a solution for it.
|
|
216
216
|
throw SESSION_CLOSED('Cannot make sessions on a closing core')
|
|
217
217
|
}
|
|
218
218
|
|
|
@@ -1100,7 +1100,7 @@ function preappend (blocks) {
|
|
|
1100
1100
|
|
|
1101
1101
|
function ensureEncryption (core, opts) {
|
|
1102
1102
|
if (!opts.encryptionKey) return
|
|
1103
|
-
// Only override the block encryption if
|
|
1103
|
+
// Only override the block encryption if it's either not already set or if
|
|
1104
1104
|
// the caller provided a different key.
|
|
1105
1105
|
if (core.encryption && b4a.equals(core.encryption.key, opts.encryptionKey)) return
|
|
1106
1106
|
core.encryption = new BlockEncryption(opts.encryptionKey, core.key)
|
package/lib/batch.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { BLOCK_NOT_AVAILABLE, SESSION_CLOSED } = require('hypercore-errors')
|
|
2
2
|
const EventEmitter = require('events')
|
|
3
3
|
const c = require('compact-encoding')
|
|
4
|
+
const b4a = require('b4a')
|
|
4
5
|
|
|
5
6
|
module.exports = class HypercoreBatch extends EventEmitter {
|
|
6
7
|
constructor (session, checkoutLength, autoClose) {
|
|
@@ -16,6 +17,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
16
17
|
this.fork = 0
|
|
17
18
|
|
|
18
19
|
this._appends = []
|
|
20
|
+
this._appendsActual = null
|
|
19
21
|
this._checkoutLength = checkoutLength
|
|
20
22
|
this._byteLength = 0
|
|
21
23
|
this._sessionLength = 0
|
|
@@ -80,6 +82,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
80
82
|
this._sessionBatch = this.session.createTreeBatch()
|
|
81
83
|
}
|
|
82
84
|
|
|
85
|
+
this._appendsActual = this.session.encryption ? [] : this._appends
|
|
83
86
|
this.fork = this.session.fork
|
|
84
87
|
this.opened = true
|
|
85
88
|
this.emit('ready')
|
|
@@ -174,13 +177,13 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
174
177
|
if (len < this._sessionLength || length > maxLength) return null
|
|
175
178
|
|
|
176
179
|
for (let i = 0; i < len - this._sessionLength; i++) {
|
|
177
|
-
b.append(this.
|
|
180
|
+
b.append(this._appendsActual[i])
|
|
178
181
|
}
|
|
179
182
|
|
|
180
183
|
if (len < this.length) return b
|
|
181
184
|
|
|
182
185
|
for (let i = 0; i < length - len; i++) {
|
|
183
|
-
b.append(blocks[i])
|
|
186
|
+
b.append(this._appendsActual === this._appends ? blocks[i] : this._encrypt(b.length + i, blocks[i]))
|
|
184
187
|
}
|
|
185
188
|
|
|
186
189
|
return b
|
|
@@ -232,11 +235,16 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
232
235
|
|
|
233
236
|
if (session.encodeBatch === null) {
|
|
234
237
|
for (let i = 0; i < blocks.length; i++) {
|
|
235
|
-
const buffer =
|
|
238
|
+
const buffer = this._encode(session.valueEncoding, blocks[i])
|
|
236
239
|
buffers[i] = buffer
|
|
237
240
|
this._byteLength += buffer.byteLength
|
|
238
241
|
}
|
|
239
242
|
}
|
|
243
|
+
if (this._appends !== this._appendsActual) {
|
|
244
|
+
for (let i = 0; i < buffers.length; i++) {
|
|
245
|
+
this._appendsActual.push(this._encrypt(this._sessionLength + this._appendsActual.length, buffers[i]))
|
|
246
|
+
}
|
|
247
|
+
}
|
|
240
248
|
|
|
241
249
|
this._appends.push(...buffers)
|
|
242
250
|
|
|
@@ -246,6 +254,35 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
246
254
|
return info
|
|
247
255
|
}
|
|
248
256
|
|
|
257
|
+
_encode (enc, val) {
|
|
258
|
+
const state = { start: 0, end: 0, buffer: null }
|
|
259
|
+
|
|
260
|
+
if (b4a.isBuffer(val)) {
|
|
261
|
+
if (state.start === 0) return val
|
|
262
|
+
state.end += val.byteLength
|
|
263
|
+
} else if (enc) {
|
|
264
|
+
enc.preencode(state, val)
|
|
265
|
+
} else {
|
|
266
|
+
val = b4a.from(val)
|
|
267
|
+
if (state.start === 0) return val
|
|
268
|
+
state.end += val.byteLength
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
state.buffer = b4a.allocUnsafe(state.end)
|
|
272
|
+
|
|
273
|
+
if (enc) enc.encode(state, val)
|
|
274
|
+
else state.buffer.set(val, state.start)
|
|
275
|
+
|
|
276
|
+
return state.buffer
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
_encrypt (index, buffer) {
|
|
280
|
+
const block = b4a.allocUnsafe(buffer.byteLength + 8)
|
|
281
|
+
block.set(buffer, 8)
|
|
282
|
+
this.session.encryption.encrypt(index, block, this.fork)
|
|
283
|
+
return block
|
|
284
|
+
}
|
|
285
|
+
|
|
249
286
|
async flush (opts = {}) {
|
|
250
287
|
if (this.opened === false) await this.opening
|
|
251
288
|
if (this.closing) throw SESSION_CLOSED()
|
|
@@ -275,7 +312,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
275
312
|
const batch = this.createTreeBatch(this._sessionLength + flushingLength)
|
|
276
313
|
if (batch === null) return false
|
|
277
314
|
|
|
278
|
-
const info = await this.core.insertBatch(batch, this.
|
|
315
|
+
const info = await this.core.insertBatch(batch, this._appendsActual, { keyPair, signature })
|
|
279
316
|
if (info === null) return false
|
|
280
317
|
|
|
281
318
|
const delta = info.byteLength - this._sessionByteLength
|
package/lib/replicator.js
CHANGED