forked from holepunchto/hyperdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
945 lines (762 loc) · 24.2 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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
var hypercore = require('hypercore')
var mutexify = require('mutexify')
var raf = require('random-access-file')
var thunky = require('thunky')
var tree = require('append-tree')
var collect = require('stream-collector')
var sodium = require('sodium-universal')
var inherits = require('inherits')
var events = require('events')
var duplexify = require('duplexify')
var from = require('from2')
var each = require('stream-each')
var uint64be = require('uint64be')
var unixify = require('unixify')
var path = require('path')
var messages = require('./lib/messages')
var stat = require('./lib/stat')
var cursor = require('./lib/cursor')
var DEFAULT_FMODE = (4 | 2 | 0) << 6 | ((4 | 0 | 0) << 3) | (4 | 0 | 0) // rw-r--r--
var DEFAULT_DMODE = (4 | 2 | 1) << 6 | ((4 | 0 | 1) << 3) | (4 | 0 | 1) // rwxr-xr-x
module.exports = Hyperdrive
function Hyperdrive (storage, key, opts) {
if (!(this instanceof Hyperdrive)) return new Hyperdrive(storage, key, opts)
events.EventEmitter.call(this)
if (isObject(key)) {
opts = key
key = null
}
if (!opts) opts = {}
this.key = null
this.discoveryKey = null
this.live = true
this.latest = !!opts.latest
this._storages = defaultStorage(this, storage, opts)
this.metadata = opts.metadata || hypercore(this._storages.metadata, key, {
secretKey: opts.secretKey,
sparse: opts.sparseMetadata,
createIfMissing: opts.createIfMissing,
storageCacheSize: opts.metadataStorageCacheSize
})
this.content = opts.content || null
this.maxRequests = opts.maxRequests || 16
this.readable = true
this.storage = storage
this.tree = tree(this.metadata, {
offset: 1,
valueEncoding: messages.Stat,
cache: opts.treeCacheSize !== 0,
cacheSize: opts.treeCacheSize
})
if (typeof opts.version === 'number') this.tree = this.tree.checkout(opts.version)
this.sparse = !!opts.sparse
this.sparseMetadata = !!opts.sparseMetadata
this.indexing = !!opts.indexing
this.contentStorageCacheSize = opts.contentStorageCacheSize
this._latestSynced = 0
this._latestVersion = 0
this._latestStorage = this.latest ? this._storages.metadata('latest') : null
this._checkout = opts._checkout
this._lock = mutexify()
this._openFiles = []
this._emittedContent = false
var self = this
this.metadata.on('append', update)
this.metadata.on('error', onerror)
this.ready = thunky(open)
this.ready(onready)
function onready (err) {
if (err) return onerror(err)
self.emit('ready')
self._oncontent()
if (self.latest && !self.metadata.writable) {
self._trackLatest(onerror)
}
}
function onerror (err) {
if (err) self.emit('error', err)
}
function update () {
self.emit('update')
}
function open (cb) {
self._open(cb)
}
}
inherits(Hyperdrive, events.EventEmitter)
Object.defineProperty(Hyperdrive.prototype, 'version', {
enumerable: true,
get: function () {
return this._checkout ? this.tree.version : (this.metadata.length ? this.metadata.length - 1 : 0)
}
})
Object.defineProperty(Hyperdrive.prototype, 'writable', {
enumerable: true,
get: function () {
return this.metadata.writable
}
})
Hyperdrive.prototype._oncontent = function () {
if (!this.content || this._emittedContent) return
this._emittedContent = true
this.emit('content')
}
Hyperdrive.prototype._trackLatest = function (cb) {
var self = this
this.ready(function (err) {
if (err) return cb(err)
self._latestStorage.read(0, 8, function (_, data) {
self._latestVersion = data ? uint64be.decode(data) : 0
loop()
})
})
function loop (err) {
if (err) return cb(err)
if (stableVersion()) return fetch()
// TODO: lock downloading while doing this
self._clearDangling(self._latestVersion, self.version, onclear)
}
function fetch () {
if (self.sparse) {
if (stableVersion()) return self.metadata.update(loop)
return loop(null)
}
self.emit('syncing')
self._fetchVersion(self._latestSynced, function (err, fullySynced) {
if (err) return cb(err)
if (fullySynced) {
self._latestSynced = self._latestVersion
self.emit('sync')
if (!self._checkout) self.metadata.update(loop) // TODO: only if live
return
}
loop(null)
})
}
function onclear (err, version) {
if (err) return cb(err)
self._latestVersion = version
self._latestStorage.write(0, uint64be.encode(self._latestVersion), loop)
}
function stableVersion () {
var latest = self.version
return latest < 0 || self._latestVersion === latest
}
}
Hyperdrive.prototype._fetchVersion = function (prev, cb) {
var self = this
var version = self.version
var updated = false
var done = false
var error = null
var stream = null
var queued = 0
var maxQueued = 64
var waitingData = null
var waitingCallback = null
this.metadata.update(function () {
updated = true
queued = 0
if (stream) stream.destroy()
kick()
})
this._ensureContent(function (err) {
if (err) return cb(err)
if (updated) return cb(null, false)
// var snapshot = self.checkout(version)
stream = self.tree.checkout(prev).diff(version, {puts: true, dels: false})
each(stream, ondata, ondone)
})
function ondata (data, next) {
if (updated || error) return callAndKick(next, new Error('Out of date'))
if (queued >= maxQueued) {
waitingData = data
waitingCallback = next
return
}
var start = data.value.offset
var end = start + data.value.blocks
if (start === end) return callAndKick(next, null)
queued++
self.content.download({start: start, end: end}, function (err) {
if (updated && !waitingCallback) return kick()
if (!updated) queued--
if (waitingCallback) {
data = waitingData
waitingData = null
next = waitingCallback
waitingCallback = null
return ondata(data, next)
}
if (err) {
stream.destroy(err)
error = err
}
kick()
})
process.nextTick(next)
}
function callAndKick (next, err) {
next(err)
kick()
}
function kick () {
if (!done || queued) return
queued = -1 // so we don't enter this twice
if (updated) return cb(null, false)
if (error) return cb(error)
cb(null, version === self.version)
}
function ondone (err) {
if (err) error = err
done = true
kick()
}
}
Hyperdrive.prototype._clearDangling = function (a, b, cb) {
var current = this.tree.checkout(a, {cached: true})
var latest = this.tree.checkout(b)
var stream = current.diff(latest, {dels: true, puts: false})
var self = this
this._ensureContent(oncontent)
function done (err) {
if (err) return cb(err)
cb(null, b)
}
function oncontent (err) {
if (err) return cb(err)
each(stream, ondata, done)
}
function ondata (data, next) {
var st = data.value
self.content.cancel(st.offset, st.offset + st.blocks)
self.content.clear(st.offset, st.offset + st.blocks, {byteOffset: st.byteOffset, byteLength: st.size}, next)
}
}
Hyperdrive.prototype.replicate = function (opts) {
if (!opts) opts = {}
opts.expectedFeeds = 2
var self = this
var stream = this.metadata.replicate(opts)
this._ensureContent(function (err) {
if (err) return stream.destroy(err)
if (stream.destroyed) return
self.content.replicate({
live: opts.live,
download: opts.download,
upload: opts.upload,
stream: stream
})
})
return stream
}
Hyperdrive.prototype.checkout = function (version, opts) {
if (!opts) opts = {}
opts._checkout = this._checkout || this
opts.metadata = this.metadata
opts.version = version
return Hyperdrive(null, null, opts)
}
Hyperdrive.prototype.createDiffStream = function (version, opts) {
if (!version) version = 0
if (typeof version === 'number') version = this.checkout(version)
return this.tree.diff(version.tree, opts)
}
Hyperdrive.prototype.download = function (dir, cb) {
if (typeof dir === 'function') return this.download('/', dir)
var downloadCount = 1
var self = this
download(dir || '/')
function download (entry) {
self.stat(entry, function (err, stat) {
if (err) {
if (cb) cb(err)
return
}
if (stat.isDirectory()) return downloadDir(entry, stat)
if (stat.isFile()) return downloadFile(entry, stat)
})
}
function downloadDir (dirname, stat) {
self.readdir(dirname, function (err, entries) {
if (err) {
if (cb) cb(err)
return
}
downloadCount -= 1
downloadCount += entries.length
entries.forEach(function (entry) {
download(path.join(dirname, entry))
})
if (downloadCount <= 0 && cb) cb()
})
}
function downloadFile (entry, stat) {
var start = stat.offset
var end = stat.offset + stat.blocks
if (start === 0 && end === 0) return
self.content.download({start, end}, function () {
downloadCount -= 1
if (downloadCount <= 0 && cb) cb()
})
}
}
Hyperdrive.prototype.history = function (opts) {
return this.tree.history(opts)
}
Hyperdrive.prototype.createCursor = function (name, opts) {
return cursor(this, name, opts)
}
// open -> fd
Hyperdrive.prototype.open = function (name, flags, mode, opts, cb) {
if (typeof mode === 'object' && mode) return this.open(name, flags, 0, mode, opts)
if (typeof mode === 'function') return this.open(name, flags, 0, mode)
if (typeof opts === 'function') return this.open(name, flags, mode, null, opts)
// TODO: use flags, only readable cursors are supported atm
var cursor = this.createCursor(name, opts)
var self = this
cursor.open(function (err) {
if (err) return cb(err)
var fd = self._openFiles.indexOf(null)
if (fd === -1) fd = self._openFiles.push(null) - 1
self._openFiles[fd] = cursor
cb(null, fd + 20) // offset all fds with 20, unsure what the actual good offset is
})
}
Hyperdrive.prototype.read = function (fd, buf, offset, len, pos, cb) {
var cursor = this._openFiles[fd - 20]
if (!cursor) return cb(new Error('Bad file descriptor'))
if (pos !== null) cursor.seek(pos)
cursor.next(function (err, next) {
if (err) return cb(err)
if (!next) return cb(null, 0, buf)
// if we read too much
if (next.length > len) {
next = next.slice(0, len)
cursor.seek(pos + len)
}
next.copy(buf, offset, 0, len)
cb(null, next.length, buf)
})
}
// TODO: move to ./lib
Hyperdrive.prototype.createReadStream = function (name, opts) {
if (!opts) opts = {}
name = unixify(name)
var self = this
var downloaded = false
var first = true
var start = 0
var end = 0
var offset = 0
var length = typeof opts.end === 'number' ? 1 + opts.end - (opts.start || 0) : typeof opts.length === 'number' ? opts.length : -1
var range = null
var ended = false
var stream = from(read)
var cached = opts && !!opts.cached
stream.on('close', cleanup)
stream.on('end', cleanup)
return stream
function cleanup () {
if (range) self.content.undownload(range, noop)
range = null
ended = true
}
function read (size, cb) {
if (first) return open(size, cb)
if (start === end || length === 0) return cb(null, null)
self.content.get(start++, {wait: !downloaded && !cached}, function (err, data) {
if (err) return cb(err)
if (offset) data = data.slice(offset)
offset = 0
if (length > -1) {
if (length < data.length) data = data.slice(0, length)
length -= data.length
}
cb(null, data)
})
}
function open (size, cb) {
first = false
self._ensureContent(function (err) {
if (err) return cb(err)
// if running latest === true and a delete happens while getting the tree data, the tree.get
// should finish before the delete so there shouldn't be an rc. we should test this though.
self.tree.get(name, ontree)
function ontree (err, stat) {
if (err) return cb(err)
if (ended || stream.destroyed) return
start = stat.offset
end = stat.offset + stat.blocks
var byteOffset = stat.byteOffset
var missing = 1
if (opts.start) self.content.seek(byteOffset + opts.start, {start: start, end: end}, onstart)
else onstart(null, start, 0)
function onend (err, index) {
if (err || !range) return
if (ended || stream.destroyed) return
missing++
self.content.undownload(range)
range = self.content.download({start: start, end: index, linear: true}, ondownload)
}
function onstart (err, index, off) {
if (err) return cb(err)
if (ended || stream.destroyed) return
offset = off
start = index
range = self.content.download({start: start, end: end, linear: true}, ondownload)
if (length > -1 && length < stat.size) {
self.content.seek(byteOffset + length, {start: start, end: end}, onend)
}
read(size, cb)
}
function ondownload (err) {
if (--missing) return
if (err && !ended && !downloaded) stream.destroy(err)
else downloaded = true
}
}
})
}
}
Hyperdrive.prototype.readFile = function (name, opts, cb) {
if (typeof opts === 'function') return this.readFile(name, null, opts)
if (typeof opts === 'string') opts = {encoding: opts}
if (!opts) opts = {}
name = unixify(name)
collect(this.createReadStream(name, opts), function (err, bufs) {
if (err) return cb(err)
var buf = bufs.length === 1 ? bufs[0] : Buffer.concat(bufs)
cb(null, opts.encoding && opts.encoding !== 'binary' ? buf.toString(opts.encoding) : buf)
})
}
Hyperdrive.prototype.createWriteStream = function (name, opts) {
if (!opts) opts = {}
name = unixify(name)
var self = this
var proxy = duplexify()
// TODO: support piping through a "split" stream like rabin
proxy.setReadable(false)
this._ensureContent(function (err) {
if (err) return proxy.destroy(err)
if (self._checkout) return proxy.destroy(new Error('Cannot write to a checkout'))
if (proxy.destroyed) return
self._lock(function (release) {
if (!self.latest || proxy.destroyed) return append(null)
self.tree.get(name, function (err, st) {
if (err && err.notFound) return append(null)
if (err) return append(err)
if (!st.size) return append(null)
self.content.clear(st.offset, st.offset + st.blocks, append)
})
function append (err) {
if (err) proxy.destroy(err)
if (proxy.destroyed) return release()
// No one should mutate the content other than us
var byteOffset = self.content.byteLength
var offset = self.content.length
self.emit('appending', name, opts)
// TODO: revert the content feed if this fails!!!! (add an option to the write stream for this (atomic: true))
var stream = self.content.createWriteStream()
proxy.on('close', done)
proxy.on('finish', done)
proxy.setWritable(stream)
proxy.on('prefinish', function () {
var st = {
mode: (opts.mode || DEFAULT_FMODE) | stat.IFREG,
uid: opts.uid || 0,
gid: opts.gid || 0,
size: self.content.byteLength - byteOffset,
blocks: self.content.length - offset,
offset: offset,
byteOffset: byteOffset,
mtime: getTime(opts.mtime),
ctime: getTime(opts.ctime)
}
proxy.cork()
self.tree.put(name, st, function (err) {
if (err) return proxy.destroy(err)
self.emit('append', name, opts)
proxy.uncork()
})
})
}
function done () {
proxy.removeListener('close', done)
proxy.removeListener('finish', done)
release()
}
})
})
return proxy
}
Hyperdrive.prototype.writeFile = function (name, buf, opts, cb) {
if (typeof opts === 'function') return this.writeFile(name, buf, null, opts)
if (typeof opts === 'string') opts = {encoding: opts}
if (!opts) opts = {}
if (typeof buf === 'string') buf = new Buffer(buf, opts.encoding || 'utf-8')
if (!cb) cb = noop
name = unixify(name)
var bufs = split(buf) // split the input incase it is a big buffer.
var stream = this.createWriteStream(name, opts)
stream.on('error', cb)
stream.on('finish', cb)
for (var i = 0; i < bufs.length; i++) stream.write(bufs[i])
stream.end()
}
Hyperdrive.prototype.mkdir = function (name, opts, cb) {
if (typeof opts === 'function') return this.mkdir(name, null, opts)
if (typeof opts === 'number') opts = {mode: opts}
if (!opts) opts = {}
if (!cb) cb = noop
name = unixify(name)
var self = this
this.ready(function (err) {
if (err) return cb(err)
if (self._checkout) return cb(new Error('Cannot write to a checkout'))
self._lock(function (release) {
var st = {
mode: (opts.mode || DEFAULT_DMODE) | stat.IFDIR,
uid: opts.uid,
gid: opts.gid,
mtime: getTime(opts.mtime),
ctime: getTime(opts.ctime),
offset: self.content.length,
byteOffset: self.content.byteLength
}
self.tree.put(name, st, function (err) {
release(cb, err)
})
})
})
}
Hyperdrive.prototype._statDirectory = function (name, opts, cb) {
this.tree.list(name, opts, function (err, list) {
if (name !== '/' && (err || !list.length)) return cb(err || new Error(name + ' could not be found'))
var st = stat()
st.mode = stat.IFDIR | DEFAULT_DMODE
cb(null, st)
})
}
Hyperdrive.prototype.access = function (name, opts, cb) {
if (typeof opts === 'function') return this.access(name, null, opts)
if (!opts) opts = {}
name = unixify(name)
this.stat(name, opts, function (err) {
cb(err)
})
}
Hyperdrive.prototype.exists = function (name, opts, cb) {
if (typeof opts === 'function') return this.exists(name, null, opts)
if (!opts) opts = {}
this.access(name, opts, function (err) {
cb(!err)
})
}
Hyperdrive.prototype.lstat = function (name, opts, cb) {
if (typeof opts === 'function') return this.lstat(name, null, opts)
if (!opts) opts = {}
var self = this
name = unixify(name)
this.tree.get(name, opts, function (err, st) {
if (err) return self._statDirectory(name, opts, cb)
cb(null, stat(st))
})
}
Hyperdrive.prototype.stat = function (name, opts, cb) {
if (typeof opts === 'function') return this.stat(name, null, opts)
if (!opts) opts = {}
this.lstat(name, opts, cb)
}
Hyperdrive.prototype.readdir = function (name, opts, cb) {
if (typeof opts === 'function') return this.readdir(name, null, opts)
name = unixify(name)
if (name === '/') return this._readdirRoot(opts, cb) // TODO: should be an option in append-tree prob
this.tree.list(name, opts, cb)
}
Hyperdrive.prototype._readdirRoot = function (opts, cb) {
this.tree.list('/', opts, function (_, list) {
if (list) return cb(null, list)
cb(null, [])
})
}
Hyperdrive.prototype.unlink = function (name, cb) {
name = unixify(name)
this._del(name, cb || noop)
}
Hyperdrive.prototype.rmdir = function (name, cb) {
if (!cb) cb = noop
name = unixify(name)
var self = this
this.readdir(name, function (err, list) {
if (err) return cb(err)
if (list.length) return cb(new Error('Directory is not empty'))
self._del(name, cb)
})
}
Hyperdrive.prototype._del = function (name, cb) {
var self = this
this._ensureContent(function (err) {
if (err) return cb(err)
self._lock(function (release) {
if (!self.latest) return del(null)
self.tree.get(name, function (err, value) {
if (err) return done(err)
self.content.clear(value.offset, value.offset + value.blocks, del)
})
function del (err) {
if (err) return done(err)
self.tree.del(name, done)
}
function done (err) {
release(cb, err)
}
})
})
}
Hyperdrive.prototype._closeFile = function (fd, cb) {
var cursor = this._openFiles[fd - 20]
if (!cursor) return cb(new Error('Bad file descriptor'))
this._openFiles[fd - 20] = null
cursor.close(cb)
}
Hyperdrive.prototype.close = function (fd, cb) {
if (typeof fd === 'number') return this._closeFile(fd, cb || noop)
else cb = fd
if (!cb) cb = noop
var self = this
this.ready(function (err) {
if (err) return cb(err)
self.metadata.close(function (err) {
if (!self.content) return cb(err)
self.content.close(cb)
})
})
}
Hyperdrive.prototype._ensureContent = function (cb) {
var self = this
this.ready(function (err) {
if (err) return cb(err)
if (!self.content) return self._loadIndex(cb)
cb(null)
})
}
Hyperdrive.prototype._loadIndex = function (cb) {
var self = this
if (this._checkout) this._checkout._loadIndex(done)
else this.metadata.get(0, {valueEncoding: messages.Index}, done)
function done (err, index) {
if (err) return cb(err)
if (self.content) return self.content.ready(cb)
var keyPair = self.metadata.writable && contentKeyPair(self.metadata.secretKey)
var opts = contentOptions(self, keyPair && keyPair.secretKey)
self.content = self._checkout ? self._checkout.content : hypercore(self._storages.content, index.content, opts)
self.content.on('error', function (err) {
self.emit('error', err)
})
self.content.ready(function (err) {
if (err) return cb(err)
self._oncontent()
cb()
})
}
}
Hyperdrive.prototype._open = function (cb) {
var self = this
this.tree.ready(function (err) {
if (err) return cb(err)
self.metadata.ready(function (err) {
if (err) return cb(err)
if (self.content) return cb(null)
self.key = self.metadata.key
self.discoveryKey = self.metadata.discoveryKey
if (!self.metadata.writable || self._checkout) onnotwriteable()
else onwritable()
})
})
function onnotwriteable () {
if (self.metadata.has(0)) return self._loadIndex(cb)
self._loadIndex(noop)
cb()
}
function onwritable () {
var wroteIndex = self.metadata.has(0)
if (wroteIndex) return self._loadIndex(cb)
if (!self.content) {
var keyPair = contentKeyPair(self.metadata.secretKey)
var opts = contentOptions(self, keyPair.secretKey)
self.content = hypercore(self._storages.content, keyPair.publicKey, opts)
self.content.on('error', function (err) {
self.emit('error', err)
})
}
self.content.ready(function () {
if (self.metadata.has(0)) return cb(new Error('Index already written'))
self.metadata.append(messages.Index.encode({type: 'hyperdrive', content: self.content.key}), cb)
})
}
}
function contentOptions (self, secretKey) {
return {
sparse: self.sparse || self.latest,
maxRequests: self.maxRequests,
secretKey: secretKey,
storeSecretKey: false,
indexing: self.metadata.writable && self.indexing,
storageCacheSize: self.contentStorageCacheSize
}
}
function isObject (val) {
return !!val && typeof val !== 'string' && !Buffer.isBuffer(val)
}
function wrap (self, storage) {
return {
metadata: function (name, opts) {
return storage.metadata(name, opts, self)
},
content: function (name, opts) {
return storage.content(name, opts, self)
}
}
}
function defaultStorage (self, storage, opts) {
var folder = ''
if (typeof storage === 'object' && storage) return wrap(self, storage)
if (typeof storage === 'string') {
folder = storage
storage = raf
}
return {
metadata: function (name) {
return storage(path.join(folder, 'metadata', name))
},
content: function (name) {
return storage(path.join(folder, 'content', name))
}
}
}
function noop () {}
function split (buf) {
var list = []
for (var i = 0; i < buf.length; i += 65536) {
list.push(buf.slice(i, i + 65536))
}
return list
}
function getTime (date) {
if (typeof date === 'number') return date
if (!date) return Date.now()
return date.getTime()
}
function contentKeyPair (secretKey) {
var seed = new Buffer(sodium.crypto_sign_SEEDBYTES)
var context = new Buffer('hyperdri') // 8 byte context
var keyPair = {
publicKey: new Buffer(sodium.crypto_sign_PUBLICKEYBYTES),
secretKey: new Buffer(sodium.crypto_sign_SECRETKEYBYTES)
}
sodium.crypto_kdf_derive_from_key(seed, 1, context, secretKey)
sodium.crypto_sign_seed_keypair(keyPair.publicKey, keyPair.secretKey, seed)
if (seed.fill) seed.fill(0)
return keyPair
}