This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
902 lines (707 loc) · 25.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
const { EventEmitter } = require('events')
const RAF = require('random-access-file')
const isOptions = require('is-options')
const hypercoreCrypto = require('hypercore-crypto')
const c = require('compact-encoding')
const b4a = require('b4a')
const Xache = require('xache')
const NoiseSecretStream = require('@hyperswarm/secret-stream')
const Protomux = require('protomux')
const codecs = require('codecs')
const Replicator = require('./lib/replicator')
const Core = require('./lib/core')
const BlockEncryption = require('./lib/block-encryption')
const Upgrader = require('./lib/upgrader')
const { ReadStream, WriteStream } = require('./lib/streams')
const { BAD_ARGUMENT, SESSION_CLOSED, SESSION_NOT_WRITABLE, SNAPSHOT_NOT_AVAILABLE } = require('./lib/errors')
const promises = Symbol.for('hypercore.promises')
const inspect = Symbol.for('nodejs.util.inspect.custom')
module.exports = class Hypercore extends EventEmitter {
constructor (storage, key, opts) {
super()
if (isOptions(storage)) {
opts = storage
storage = null
key = null
} else if (isOptions(key)) {
opts = key
key = null
}
if (key && typeof key === 'string') {
key = b4a.from(key, 'hex')
}
if (!opts) opts = {}
if (!opts.crypto && key && key.byteLength !== 32) {
throw BAD_ARGUMENT('Hypercore key should be 32 bytes')
}
if (!storage) storage = opts.storage
this[promises] = true
this.storage = null
this.crypto = opts.crypto || hypercoreCrypto
this.core = null
this.replicator = null
this.encryption = null
this.extensions = new Map()
this.cache = opts.cache === true ? new Xache({ maxSize: 65536, maxAge: 0 }) : (opts.cache || null)
this.valueEncoding = null
this.encodeBatch = null
this.activeRequests = []
this.key = key || null
this.keyPair = null
this.readable = true
this.writable = false
this.opened = false
this.closed = false
this.snapshotted = !!opts.snapshot
this.sparse = opts.sparse !== false
this.sessions = opts._sessions || [this]
this.auth = opts.auth || null
this.autoClose = !!opts.autoClose
this.onwait = opts.onwait || null
this.wait = opts.wait !== false
this.closing = null
this.opening = this._openSession(key, storage, opts)
this.opening.catch(noop)
this._preappend = preappend.bind(this)
this._snapshot = null
this._findingPeers = 0
this._upgrader = new Upgrader(this, opts)
}
[inspect] (depth, opts) {
let indent = ''
if (typeof opts.indentationLvl === 'number') {
while (indent.length < opts.indentationLvl) indent += ' '
}
let peers = ''
const min = Math.min(this.peers.length, 5)
for (let i = 0; i < min; i++) {
const peer = this.peers[i]
peers += indent + ' Peer(\n'
peers += indent + ' remotePublicKey: ' + opts.stylize(toHex(peer.remotePublicKey), 'string') + '\n'
peers += indent + ' remoteLength: ' + opts.stylize(peer.remoteLength, 'number') + '\n'
peers += indent + ' remoteFork: ' + opts.stylize(peer.remoteFork, 'number') + '\n'
peers += indent + ' remoteCanUpgrade: ' + opts.stylize(peer.remoteCanUpgrade, 'boolean') + '\n'
peers += indent + ' )' + '\n'
}
if (this.peers.length > 5) {
peers += indent + ' ... and ' + (this.peers.length - 5) + ' more\n'
}
if (peers) peers = '[\n' + peers + indent + ' ]'
else peers = '[ ' + opts.stylize(0, 'number') + ' ]'
return this.constructor.name + '(\n' +
indent + ' key: ' + opts.stylize(toHex(this.key), 'string') + '\n' +
indent + ' discoveryKey: ' + opts.stylize(toHex(this.discoveryKey), 'string') + '\n' +
indent + ' opened: ' + opts.stylize(this.opened, 'boolean') + '\n' +
indent + ' closed: ' + opts.stylize(this.closed, 'boolean') + '\n' +
indent + ' snapshotted: ' + opts.stylize(this.snapshotted, 'boolean') + '\n' +
indent + ' sparse: ' + opts.stylize(this.sparse, 'boolean') + '\n' +
indent + ' writable: ' + opts.stylize(this.writable, 'boolean') + '\n' +
indent + ' length: ' + opts.stylize(this.length, 'number') + '\n' +
indent + ' byteLength: ' + opts.stylize(this.byteLength, 'number') + '\n' +
indent + ' fork: ' + opts.stylize(this.fork, 'number') + '\n' +
indent + ' sessions: [ ' + opts.stylize(this.sessions.length, 'number') + ' ]\n' +
indent + ' activeRequests: [ ' + opts.stylize(this.activeRequests.length, 'number') + ' ]\n' +
indent + ' peers: ' + peers + '\n' +
indent + ')'
}
static getProtocolMuxer (stream) {
return stream.noiseStream.userData
}
static createProtocolStream (isInitiator, opts = {}) {
let outerStream = Protomux.isProtomux(isInitiator)
? isInitiator.stream
: isStream(isInitiator)
? isInitiator
: opts.stream
let noiseStream = null
if (outerStream) {
noiseStream = outerStream.noiseStream
} else {
noiseStream = new NoiseSecretStream(isInitiator, null, opts)
outerStream = noiseStream.rawStream
}
if (!noiseStream) throw BAD_ARGUMENT('Invalid stream')
if (!noiseStream.userData) {
const protocol = new Protomux(noiseStream)
if (opts.ondiscoverykey) {
protocol.pair({ protocol: 'hypercore/alpha' }, opts.ondiscoverykey)
}
if (opts.keepAlive !== false) {
noiseStream.setKeepAlive(5000)
}
noiseStream.userData = protocol
}
return outerStream
}
static defaultStorage (storage, opts = {}) {
if (typeof storage !== 'string') {
if (!isRandomAccessClass(storage)) return storage
const Cls = storage // just to satisfy standard...
return name => new Cls(name)
}
const directory = storage
const toLock = opts.lock || 'oplog'
return createFile
function createFile (name) {
const lock = isFile(name, toLock)
const sparse = isFile(name, 'data') || isFile(name, 'bitfield') || isFile(name, 'tree')
return new RAF(name, { directory, lock, sparse })
}
function isFile (name, n) {
return name === n || name.endsWith('/' + n)
}
}
snapshot (opts) {
return this.session({ ...opts, snapshot: true })
}
session (opts = {}) {
if (this.closing) {
// This makes the closing logic alot easier. If this turns out to be a problem
// in practive, open an issue and we'll try to make a solution for it.
throw SESSION_CLOSED('Cannot make sessions on a closing core')
}
const sparse = opts.sparse === false ? false : this.sparse
const wait = opts.wait === false ? false : this.wait
const onwait = opts.onwait === undefined ? this.onwait : opts.onwait
const Clz = opts.class || Hypercore
const s = new Clz(this.storage, this.key, {
...opts,
sparse,
wait,
onwait,
_opening: this.opening,
_sessions: this.sessions
})
s._passCapabilities(this)
// Configure the cache unless explicitly disabled.
if (opts.cache !== false) {
s.cache = opts.cache === true || !opts.cache ? this.cache : opts.cache
}
ensureEncryption(s, opts)
this.sessions.push(s)
return s
}
_passCapabilities (o) {
if (!this.auth) this.auth = o.auth
this.crypto = o.crypto
this.key = o.key
this.core = o.core
this.replicator = o.replicator
this.encryption = o.encryption
this.writable = !!(this.auth && this.auth.sign)
this.autoClose = o.autoClose
if (this.snapshotted && this.core && !this._snapshot) this._updateSnapshot()
}
async _openFromExisting (from, opts) {
await from.opening
this._passCapabilities(from)
this.sessions = from.sessions
this.storage = from.storage
this.replicator.findingPeers += this._findingPeers
ensureEncryption(this, opts)
this.sessions.push(this)
}
async _openSession (key, storage, opts) {
const isFirst = !opts._opening
if (!isFirst) await opts._opening
if (opts.preload) opts = { ...opts, ...(await opts.preload()) }
const keyPair = (key && opts.keyPair)
? { ...opts.keyPair, publicKey: key }
: key
? { publicKey: key, secretKey: null }
: opts.keyPair
// This only works if the hypercore was fully loaded,
// but we only do this to validate the keypair to help catch bugs so yolo
if (this.key && keyPair) keyPair.publicKey = this.key
if (opts.auth) {
this.auth = opts.auth
} else if (opts.sign) {
this.auth = Core.createAuth(this.crypto, keyPair, opts)
} else if (keyPair && keyPair.secretKey) {
this.auth = Core.createAuth(this.crypto, keyPair)
}
if (isFirst) {
await this._openCapabilities(keyPair, storage, opts)
// Only the root session should pass capabilities to other sessions.
for (let i = 0; i < this.sessions.length; i++) {
const s = this.sessions[i]
if (s !== this) s._passCapabilities(this)
}
}
if (!this.auth) this.auth = this.core.defaultAuth
this.writable = !!this.auth.sign
if (opts.valueEncoding) {
this.valueEncoding = c.from(codecs(opts.valueEncoding))
}
if (opts.encodeBatch) {
this.encodeBatch = opts.encodeBatch
}
// Start continous replication if not in sparse mode.
if (!this.sparse) this.download({ start: 0, end: -1 })
// This is a hidden option that's only used by Corestore.
// It's required so that corestore can load a name from userData before 'ready' is emitted.
if (opts._preready) await opts._preready(this)
this.opened = true
this.emit('ready')
}
async _openCapabilities (keyPair, storage, opts) {
if (opts.from) return this._openFromExisting(opts.from, opts)
this.storage = Hypercore.defaultStorage(opts.storage || storage)
this.core = await Core.open(this.storage, {
force: opts.force,
createIfMissing: opts.createIfMissing,
overwrite: opts.overwrite,
keyPair,
crypto: this.crypto,
legacy: opts.legacy,
auth: opts.auth,
onupdate: this._oncoreupdate.bind(this)
})
if (opts.userData) {
for (const [key, value] of Object.entries(opts.userData)) {
await this.core.userData(key, value)
}
}
this.key = this.core.header.signer.publicKey
this.keyPair = this.core.header.signer
this.replicator = new Replicator(this.core, this.key, {
eagerUpdate: true,
allowFork: opts.allowFork !== false,
onpeerupdate: this._onpeerupdate.bind(this),
onupload: this._onupload.bind(this)
})
this.replicator.findingPeers += this._findingPeers
if (!this.encryption && opts.encryptionKey) {
this.encryption = new BlockEncryption(opts.encryptionKey, this.key)
}
}
_getSnapshot () {
if (this.sparse) {
return {
length: this.core.tree.length,
byteLength: this.core.tree.byteLength,
fork: this.core.tree.fork,
compatLength: this.core.tree.length
}
}
return {
length: this.core.header.contiguousLength,
byteLength: 0,
fork: this.core.tree.fork,
compatLength: this.core.header.contiguousLength
}
}
_updateSnapshot () {
const prev = this._snapshot
const next = this._snapshot = this._getSnapshot()
if (!prev) return true
return prev.length !== next.length || prev.fork !== next.fork
}
close () {
if (this.closing) return this.closing
this.closing = this._close()
return this.closing
}
async _close () {
await this.opening
const i = this.sessions.indexOf(this)
if (i === -1) return
this.sessions.splice(i, 1)
this.readable = false
this.writable = false
this.closed = true
this.opened = false
const gc = []
for (const ext of this.extensions.values()) {
if (ext.session === this) gc.push(ext)
}
for (const ext of gc) ext.destroy()
if (this.replicator !== null) {
this.replicator.findingPeers -= this._findingPeers
this.replicator.clearRequests(this.activeRequests)
}
this._findingPeers = 0
if (this.sessions.length) {
// if this is the last session and we are auto closing, trigger that first to enforce error handling
if (this.sessions.length === 1 && this.autoClose) await this.sessions[0].close()
// emit "fake" close as this is a session
this.emit('close', false)
return
}
await this.core.close()
this.emit('close', true)
}
replicate (isInitiator, opts = {}) {
// Only limitation here is that ondiscoverykey doesn't work atm when passing a muxer directly,
// because it doesn't really make a lot of sense.
if (Protomux.isProtomux(isInitiator)) return this._attachToMuxer(isInitiator, opts)
const protocolStream = Hypercore.createProtocolStream(isInitiator, opts)
const noiseStream = protocolStream.noiseStream
const protocol = noiseStream.userData
this._attachToMuxer(protocol, opts)
return protocolStream
}
_attachToMuxer (mux, opts) {
// If the user wants to, we can make this replication run in a session
// that way the core wont close "under them" during replication
if (opts.session) {
const s = this.session()
mux.stream.on('close', () => s.close().catch(noop))
}
if (this.opened) {
this.replicator.attachTo(mux)
} else {
this.opening.then(() => this.replicator.attachTo(mux), mux.destroy.bind(mux))
}
return mux
}
get discoveryKey () {
return this.replicator === null ? null : this.replicator.discoveryKey
}
get length () {
if (this._snapshot) return this._snapshot.length
if (this.core === null) return 0
if (!this.sparse) return this.contiguousLength
return this.core.tree.length
}
get byteLength () {
if (this._snapshot) return this._snapshot.byteLength
if (this.core === null) return 0
if (!this.sparse) return this.contiguousByteLength
return this.core.tree.byteLength - (this.core.tree.length * this.padding)
}
get contiguousLength () {
return this.core === null ? 0 : this.core.header.contiguousLength
}
get contiguousByteLength () {
return 0
}
get fork () {
return this.core === null ? 0 : this.core.tree.fork
}
get peers () {
return this.replicator === null ? [] : this.replicator.peers
}
get encryptionKey () {
return this.encryption && this.encryption.key
}
get padding () {
return this.encryption === null ? 0 : this.encryption.padding
}
ready () {
return this.opening
}
_onupload (index, value, from) {
const byteLength = value.byteLength - this.padding
for (let i = 0; i < this.sessions.length; i++) {
this.sessions[i].emit('upload', index, byteLength, from)
}
}
_oncoreupdate (status, bitfield, value, from) {
if (status !== 0) {
const truncatedNonSparse = (status & 0b1000) !== 0
const appendedNonSparse = (status & 0b0100) !== 0
const truncated = (status & 0b0010) !== 0
const appended = (status & 0b0001) !== 0
if (truncated) {
this.replicator.ontruncate(bitfield.start)
}
if ((status & 0b0011) !== 0) {
this.replicator.onupgrade()
}
for (let i = 0; i < this.sessions.length; i++) {
const s = this.sessions[i]
if (truncated) {
if (s.cache) s.cache.clear()
// If snapshotted, make sure to update our compat so we can fail gets
if (s._snapshot && bitfield.start < s._snapshot.compatLength) s._snapshot.compatLength = bitfield.start
s._upgrader.ontruncate()
}
if (s.sparse ? truncated : truncatedNonSparse) {
s.emit('truncate', bitfield.start, this.core.tree.fork)
}
// For sparse sessions, immediately emit appends. If non-sparse, emit if contig length has updated
if (s.sparse ? appended : appendedNonSparse) {
s.emit('append')
}
}
const contig = this.core.header.contiguousLength
// When the contig length catches up, broadcast the non-sparse length to peers
if (appendedNonSparse && contig === this.core.tree.length) {
for (const peer of this.peers) {
if (peer.broadcastedNonSparse) continue
peer.broadcastRange(0, contig)
peer.broadcastedNonSparse = true
}
}
}
if (bitfield) {
this.replicator.onhave(bitfield.start, bitfield.length, bitfield.drop)
}
if (value) {
const byteLength = value.byteLength - this.padding
for (let i = 0; i < this.sessions.length; i++) {
this.sessions[i].emit('download', bitfield.start, byteLength, from)
}
}
}
_onpeerupdate (added, peer) {
const name = added ? 'peer-add' : 'peer-remove'
for (let i = 0; i < this.sessions.length; i++) {
this.sessions[i].emit(name, peer)
if (added) {
for (const ext of this.sessions[i].extensions.values()) {
peer.extensions.set(ext.name, ext)
}
}
}
}
async setUserData (key, value) {
if (this.opened === false) await this.opening
return this.core.userData(key, value)
}
async getUserData (key) {
if (this.opened === false) await this.opening
for (const { key: savedKey, value } of this.core.header.userData) {
if (key === savedKey) return value
}
return null
}
findingPeers () {
this._findingPeers++
if (this.replicator !== null && !this.closing) this.replicator.findingPeers++
let once = true
return () => {
if (this.closing || !once) return
once = false
this._findingPeers--
if (this.replicator !== null && --this.replicator.findingPeers === 0) {
this.replicator.updateAll()
}
}
}
async update (opts) {
if (this.opened === false) await this.opening
if (this.closing !== null) return false
// TODO: add an option where a writer can bootstrap it's state from the network also
if (this.writable) {
if (!this.snapshotted) return false
return this._updateSnapshot()
}
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
const req = this.replicator.addUpgrade(activeRequests)
return this._upgrader.onupgrade(req.promise)
}
async seek (bytes, opts) {
if (this.opened === false) await this.opening
const s = this.core.tree.seek(bytes, this.padding)
const offset = await s.update()
if (offset) return offset
if (this.closing !== null) throw SESSION_CLOSED()
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
const req = this.replicator.addSeek(activeRequests, s)
return req.promise
}
async has (index) {
if (this.opened === false) await this.opening
return this.core.bitfield.get(index)
}
async get (index, opts) {
if (this.opened === false) await this.opening
if (this.closing !== null) throw SESSION_CLOSED()
if (this._snapshot !== null && index >= this._snapshot.compatLength) throw SNAPSHOT_NOT_AVAILABLE()
const encoding = (opts && opts.valueEncoding && c.from(codecs(opts.valueEncoding))) || this.valueEncoding
let req = this.cache && this.cache.get(index)
if (!req) req = this._get(index, opts)
let block = await req
if (!block) return null
if (this.encryption) {
// Copy the block as it might be shared with other sessions.
block = b4a.from(block)
this.encryption.decrypt(index, block)
}
return this._decode(encoding, block)
}
async clear (start, end = start + 1, opts) {
if (this.opened === false) await this.opening
if (this.closing !== null) throw SESSION_CLOSED()
if (typeof end === 'object') {
opts = end
end = start + 1
}
if (start >= end) return
await this.core.clear(start, end)
}
async _get (index, opts) {
let block
if (this.core.bitfield.get(index)) {
block = this.core.blocks.get(index)
if (this.cache) this.cache.set(index, block)
} else {
if (opts && opts.wait === false) return null
if (this.wait === false && (!opts || !opts.wait)) return null
if (opts && opts.onwait) opts.onwait(index, this)
if (this.onwait) this.onwait(index, this)
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
const req = this.replicator.addBlock(activeRequests, index)
block = this._cacheOnResolve(index, req.promise, this.core.tree.fork)
}
return block
}
async _cacheOnResolve (index, req, fork) {
const block = await req
if (this.cache && fork === this.core.tree.fork) {
this.cache.set(index, Promise.resolve(block))
}
return block
}
createReadStream (opts) {
return new ReadStream(this, opts)
}
createWriteStream (opts) {
return new WriteStream(this, opts)
}
download (range) {
const reqP = this._download(range)
// do not crash in the background...
reqP.catch(noop)
// TODO: turn this into an actual object...
return {
async downloaded () {
const req = await reqP
return req.promise
},
destroy () {
reqP.then(req => req.context && req.context.detach(req), noop)
}
}
}
async _download (range) {
if (this.opened === false) await this.opening
const activeRequests = (range && range.activeRequests) || this.activeRequests
return this.replicator.addRange(activeRequests, range)
}
// TODO: get rid of this / deprecate it?
undownload (range) {
range.destroy(null)
}
// TODO: get rid of this / deprecate it?
cancel (request) {
// Do nothing for now
}
async truncate (newLength = 0, fork = -1) {
if (this.opened === false) await this.opening
if (this.writable === false) throw SESSION_NOT_WRITABLE()
if (fork === -1) fork = this.core.tree.fork + 1
await this.core.truncate(newLength, fork, this.auth)
// TODO: Should propagate from an event triggered by the oplog
this.replicator.updateAll()
}
async append (blocks) {
if (this.opened === false) await this.opening
if (this.writable === false) throw SESSION_NOT_WRITABLE()
blocks = Array.isArray(blocks) ? blocks : [blocks]
const preappend = this.encryption && this._preappend
const buffers = this.encodeBatch !== null ? this.encodeBatch(blocks) : new Array(blocks.length)
if (this.encodeBatch === null) {
for (let i = 0; i < blocks.length; i++) {
buffers[i] = this._encode(this.valueEncoding, blocks[i])
}
}
return await this.core.append(buffers, this.auth, { preappend })
}
async treeHash (length) {
if (length === undefined) {
await this.ready()
length = this.core.length
}
const roots = await this.core.tree.getRoots(length)
return this.crypto.tree(roots)
}
registerExtension (name, handlers = {}) {
if (this.extensions.has(name)) {
const ext = this.extensions.get(name)
ext.handlers = handlers
ext.encoding = c.from(codecs(handlers.encoding) || c.buffer)
ext.session = this
return ext
}
const ext = {
name,
handlers,
encoding: c.from(codecs(handlers.encoding) || c.buffer),
session: this,
send (message, peer) {
const buffer = c.encode(this.encoding, message)
peer.extension(name, buffer)
},
broadcast (message) {
const buffer = c.encode(this.encoding, message)
for (const peer of this.session.peers) {
peer.extension(name, buffer)
}
},
destroy () {
for (const peer of this.session.peers) {
if (peer.extensions.get(name) === ext) peer.extensions.delete(name)
}
this.session.extensions.delete(name)
},
_onmessage (state, peer) {
const m = this.encoding.decode(state)
if (this.handlers.onmessage) this.handlers.onmessage(m, peer)
}
}
this.extensions.set(name, ext)
for (const peer of this.peers) {
peer.extensions.set(name, ext)
}
return ext
}
_encode (enc, val) {
const state = { start: this.padding, end: this.padding, buffer: null }
if (b4a.isBuffer(val)) {
if (state.start === 0) return val
state.end += val.byteLength
} else if (enc) {
enc.preencode(state, val)
} else {
val = b4a.from(val)
if (state.start === 0) return val
state.end += val.byteLength
}
state.buffer = b4a.allocUnsafe(state.end)
if (enc) enc.encode(state, val)
else state.buffer.set(val, state.start)
return state.buffer
}
_decode (enc, block) {
if (this.padding) block = block.subarray(this.padding)
if (enc) return c.decode(enc, block)
return block
}
}
function noop () {}
function isStream (s) {
return typeof s === 'object' && s && typeof s.pipe === 'function'
}
function isRandomAccessClass (fn) {
return !!(typeof fn === 'function' && fn.prototype && typeof fn.prototype.open === 'function')
}
function toHex (buf) {
return buf && b4a.toString(buf, 'hex')
}
function preappend (blocks) {
const offset = this.core.tree.length
const fork = this.core.tree.fork
for (let i = 0; i < blocks.length; i++) {
this.encryption.encrypt(offset + i, blocks[i], fork)
}
}
function ensureEncryption (core, opts) {
if (!opts.encryptionKey) return
// Only override the block encryption if its either not already set or if
// the caller provided a different key.
if (core.encryption && b4a.equals(core.encryption.key, opts.encryptionKey)) return
core.encryption = new BlockEncryption(opts.encryptionKey, core.key)
}