-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
677 lines (565 loc) · 17.6 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
const EventEmitter = require('events')
const { PassThrough } = require('stream')
const { dirname, join } = require('path').posix
const { Tombstone } = require('./messages')
const THE_TOMB = '.tombstones'
class MultiHyperdrive extends EventEmitter {
constructor (primary) {
super()
this.primary = primary
this.sources = new Map()
if (!primary) throw new TypeError('Must provide a primary drive')
// TODO: Listen on events from primary drive and re-emit them
this.addDrive(primary)
const onContentFeed = (feed) => this.emit('content-feed', feed)
const onMetadataFeed = (feed) => this.emit('metadata-feed', feed)
const onMount = (trie) => this.emit('mount', trie)
const onClose = () => this.emit('close')
const onError = (err) => this.emit('error', err)
const onUpdate = () => this.emit('update')
const onPeerAdd = (peer) => this.emit('peer-add', peer)
const onPeerOpen = (peer) => this.emit('peer-open', peer)
const onPeerRemove = (peer) => this.emit('peer-remove', peer)
primary.on('content-feed', onContentFeed)
primary.on('metadata-feed', onMetadataFeed)
primary.on('mount', onMount)
primary.on('close', onClose)
primary.on('error', onError)
primary.on('update', onUpdate)
primary.on('peer-add', onPeerAdd)
primary.on('peer-open', onPeerOpen)
primary.on('peer-remove', onPeerRemove)
this._unlisten = () => {
primary.removeListener('content-feed', onContentFeed)
primary.removeListener('metadata-feed', onMetadataFeed)
primary.removeListener('mount', onMount)
primary.removeListener('close', onClose)
primary.removeListener('error', onError)
primary.removeListener('update', onUpdate)
primary.removeListener('peer-add', onPeerAdd)
primary.removeListener('peer-open', onPeerOpen)
primary.removeListener('peer-remove', onPeerRemove)
}
}
get key () {
return this.primary.key
}
get discoveryKey () {
return this.primary.discoveryKey
}
get version () {
// TODO: Figure out what versions mean for multiple drives
throw new Error('Not Supported')
}
get writable () {
return !!this.writer
}
get contentWritable () {
return this.writerOrPrimary.contentWritable
}
get peers () {
return this.primary.peers
}
get writerOrPrimary () {
return this.writer || this.primary
}
get writer () {
for (const drive of this.sources.values()) {
if (drive.writable) return drive
}
}
get drives () {
return [...this.sources.values()]
}
addDrive (drive, cb) {
drive.ready(() => {
this.sources.set(drive.key.toString('hex'), drive)
if (cb) cb()
this.emit('drive-add', drive)
})
drive.once('close', () => {
this.removeDrive(drive.key)
})
}
hasDrive (key) {
return this.sources.get(key.toString('hex'))
}
removeDrive (key) {
const keyString = key.toString('hex')
const drive = this.sources.get(keyString)
if (!drive) return false
this.sources.delete(keyString)
process.nextTick(() => {
this.emit('drive-remove', drive)
})
return drive
}
_runAll (method, args, cb) {
const all = this.drives
const total = all.length
const results = []
if (!total) return setTimeout(() => cb(null, []))
for (const drive of all) {
drive[method](...args, (err, value) => {
results.push({ drive, err, value })
if (results.length === total) cb(null, results)
})
}
}
_runAllDBs (method, args, cb) {
const all = this.drives
const total = all.length
const results = []
if (!total) return setTimeout(() => cb(null, []))
for (const drive of all) {
drive.db[method](...args, (err, value) => {
results.push({ drive, err, value })
if (results.length === total) cb(null, results)
})
}
}
// Default checker function
// Check the stats, and return the drive with the latest data for this path
// If drive1 is newer, return < 0
// If drive1 is older, return > 0
// If they are the same return 0
compareStats (stat1, stat2) {
if (stat1 && stat2) {
const { ctime: date1 } = stat1
const { ctime: date2 } = stat2
const time1 = date1.getTime()
const time2 = date2.getTime()
return time2 - time1
}
if (!stat1 && !stat2) return 0
if (stat1 && !stat2) return -1
if (!stat1 && stat2) return 1
}
resolveLatest (name, cb) {
this.existsTombstone(name, (err, exists) => {
if (err) return cb(err)
if (exists) return cb(null, exists)
this._runAll('stat', [name], (err, results) => {
if (err) return cb(err)
try {
const sorted = results
.filter(({ value }) => !!value)
.sort(({ value: stat1 }, { value: stat2 }) => this.compareStats(stat1, stat2))
if (!sorted.length) return cb(null, this.primary)
const { drive } = sorted[0]
cb(null, drive)
} catch (e) {
cb(e)
}
})
})
}
// Resolves to the drive that says a tombstone exists for this path
existsTombstone (name, cb) {
const casket = join(THE_TOMB, name)
this._runAllDBs('get', [casket, { hidden: true }], (err, results) => {
if (err) return cb(err)
const lastTime = 0
let exists = null
for (const { value: result, drive } of results) {
if (!result) continue
const { value } = result
if (!value) continue
const tombstone = Tombstone.decode(value)
const { timestamp, active } = tombstone
if ((timestamp - lastTime) > 0) {
exists = active ? drive : null
}
}
cb(null, exists)
})
}
setTombstone (name, active, cb) {
const timestamp = Date.now()
const tombstone = Tombstone.encode({ timestamp, active })
const casket = join(THE_TOMB, name)
this.writerOrPrimary.db.put(casket, tombstone, { hidden: true }, cb)
}
eraseTombstone (name, cb) {
const parent = dirname(name)
const doErase = () => {
this.existsTombstone(name, (err, exists) => {
if (err) return cb(err)
if (!exists) return cb(null)
this.setTombstone(name, false, cb)
})
}
if (parent === name) {
doErase()
} else {
this.eraseTombstone(parent, (err) => {
if (err) return cb(err)
doErase()
})
}
}
getContent (cb) {
throw new Error('Not Supported')
}
// Open a file descriptor, special cases for writing and reading
// See if it's writable or readable
// If readable, resolve to latest and open
// If writable, open on writer
// FD should be an object with the raw FD and the drive to use
// cb(null, {fd, drive})
open (name, flags, cb) {
if (typeof flags === 'function') return this.open(name, 'r', cb)
const writable = flags.includes('a') || flags.includes('w')
if (writable) {
ensureDir(dirname(name), this.writerOrPrimary, (err) => {
if (err) return cb(err)
openFD(this.writerOrPrimary)
})
} else {
this.resolveLatest(name, (err, drive) => {
if (err) return cb(err)
openFD(drive)
})
}
function openFD (drive) {
drive.open(name, flags, (err, fd) => {
if (err) return cb(err)
cb(null, { fd, drive })
})
}
}
read (fd, buf, offset, len, pos, cb) {
return fd.drive.read(fd.fd, buf, offset, len, pos, cb)
}
write (fd, buf, offset, len, pos, cb) {
return fd.drive.write(fd.fd, buf, offset, len, pos, cb)
}
createReadStream (name, opts) {
if (!opts) opts = {}
const stream = new PassThrough()
// Resolve to latest, and call read stream
this.resolveLatest(name, (err, drive) => {
if (err) stream.destroy(err)
else {
const source = drive.createReadStream(name, opts)
source.pipe(stream)
}
})
return stream
}
createDiffStream (other, prefix, opts) {
throw new Error('Not supported')
}
createDirectoryStream (name, opts) {
throw new Error('Not supported')
}
createWriteStream (name, opts) {
if (!opts) opts = {}
const stream = new PassThrough()
ensureDir(dirname(name), this.writerOrPrimary, (err) => {
if (err) return stream.destroy(err)
const dest = this.writerOrPrimary.createWriteStream(name, opts)
stream.pipe(dest)
this.eraseTombstone(name, (err) => {
if (err) stream.destroy(err)
})
})
return stream
}
readFile (name, opts, cb) {
if (typeof opts === 'function') return this.readFile(name, null, opts)
if (typeof opts === 'string') opts = { encoding: opts }
if (!opts) opts = {}
return this.resolveLatest(name, (err, drive) => {
if (err) cb(err)
else drive.readFile(name, opts, cb)
})
}
writeFile (name, buf, opts, cb) {
if (typeof opts === 'function') return this.writeFile(name, buf, null, opts)
ensureDir(dirname(name), this.writerOrPrimary, (err) => {
if (err) return cb(err)
this.writerOrPrimary.writeFile(name, buf, opts, (err, result) => {
if (err) return cb(err)
this.eraseTombstone(name, (err) => {
if (err) return err
cb(null, result)
})
})
})
}
truncate (name, size, cb) {
return this.writerOrPrimary.truncate(name, size, cb)
}
ftruncate (fd, size, cb) {
return fd.drive.ftruncate(fd.fd, size, cb)
}
mkdir (name, opts, cb) {
if (typeof opts === 'function') return this.mkdir(name, null, cb)
ensureDir(name, this.writerOrPrimary, (err) => {
if (err) return cb(err)
this.eraseTombstone(name, cb)
})
}
readlink (name, cb) {
return this.resolveLatest(name, (err, drive) => {
if (err) return cb(err)
drive.readlink(name, cb)
})
}
lstat (name, opts, cb) {
if (typeof opts === 'function') return this.lstat(name, null, opts)
if (!opts) opts = {}
return this.resolveLatest(name, (err, drive) => {
if (err) return cb(err)
drive.lstat(name, opts, (err, stat) => {
if (err) return cb(err)
stat.drive = drive
cb(null, stat)
})
})
}
stat (name, opts, cb) {
if (typeof opts === 'function') return this.stat(name, null, opts)
if (!opts) opts = {}
return this.resolveLatest(name, (err, drive) => {
if (err) return cb(err)
drive.stat(name, opts, (err, stat) => {
if (err) return cb(err)
stat.drive = drive
cb(null, stat)
})
})
}
access (name, opts, cb) {
if (typeof opts === 'function') return this.exists(name, null, opts)
this._runAll('access', [name, opts], (err, results) => {
if (err) cb(err)
const exists = results.find(({ err }) => !err)
if (exists) return cb(null)
const firstError = results.find(({ err }) => err)
const gotErr = firstError ? firstError.err : null
cb(gotErr)
})
}
exists (name, opts = {}, cb) {
if (typeof opts === 'function') return this.exists(name, null, opts)
this.access(name, opts, (err) => {
const exists = !err
cb(exists)
})
}
readdir (name, opts = {}, cb) {
if (typeof opts === 'function') return this.readdir(name, null, opts)
this._runAll('readdir', [name, opts], (err, results) => {
if (err) return cb(err)
// Honestly, I'm sorry for this code but it's a complex task
if (opts && opts.includeStats) {
// Map seen files to the latest stat for it
// This is so that we can determine the latest item individually
const seenItems = new Map()
let lastError = null
for (const { value, err, drive } of results) {
if (err) {
lastError = err
continue
}
for (const { stat, name } of value) {
stat.drive = drive
if (seenItems.has(name)) {
const existing = seenItems.get(name)
// If the new stat is "newer" than the old one, replace it
if (this.compareStats(existing, stat) > 0) seenItems.set(name, stat)
} else seenItems.set(name, stat)
}
}
const items = [...seenItems.entries()].map(([name, stat]) => {
return { name, stat }
})
if (!items.length) return cb(lastError, items)
let checked = 0
const existingItems = []
items.forEach((item) => {
const { name: itemName } = item
this.existsTombstone(join(name, itemName), (err, tombstone) => {
if (err) {
if (checked !== items.length) cb(err)
checked = items.length
return
}
if (!tombstone) existingItems.push(item)
checked++
if (checked === items.length) cb(null, existingItems)
})
})
} else {
let lastError = null
const knownItems = new Set()
for (const { value, err } of results) {
if (err) {
lastError = err
continue
}
for (const item of value) knownItems.add(item)
}
const items = [...knownItems]
if (!items.length) return cb(lastError, items)
let checked = 0
const existingItems = []
items.forEach((item) => {
this.existsTombstone(join(name, item), (err, tombstone) => {
if (err) {
if (checked !== items.length) cb(err)
checked = items.length
return
}
if (!tombstone) existingItems.push(item)
checked++
if (checked === items.length) cb(null, existingItems)
})
})
}
})
}
unlink (name, cb) {
this.setTombstone(name, true, (err) => {
if (err) return cb(err)
return this.writerOrPrimary.unlink(name, () => {
// Even if we couldn't delete it, we added a tombstone so it's kind of a success
cb(null)
})
})
}
rmdir (name, cb) {
if (!cb) cb = noop
// Even if we couldn't delete it, we added a tombstone so it's kind of a success
this.writerOrPrimary.rmdir(name, (err) => {
// If the directory isn't empty, we can't delete it.
if (err.code === 'ENOTEMPTY') cb(err)
this.setTombstone(name, true, (err) => {
if (err) return cb(err)
else cb(null)
})
})
}
replicate (isInitiator, opts) {
// TODO: Maybe add replication eventually?
// PRs welcome. 😂
throw new Error('Not supported')
}
checkout (version, opts) {
// TODO: Figure out along with version
throw new Error('Not Supported')
}
close (fd, cb) {
if (typeof fd === 'function') {
this._runAll('close', [], (err) => {
this._unlisten()
cb(err)
})
} else {
fd.drive.close(fd.fd, cb)
}
}
destroyStorage (cb) {
throw new Error('Not Supported')
}
// This isn't really documented anywhere and I'm not sure if it's safe to use. 🤔
stats (name, opts, cb) {
if (typeof opts === 'function') return this.stats(name, null, opts)
this.resolveLatest(name, (err, drive) => {
if (err) return cb(err)
drive.stats(name, opts, cb)
})
}
watchStats (name, opts) {
throw Error('Not Supported')
}
mirror () {
throw Error('Not Supported')
}
clear (name, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = null
}
opts = opts || {}
if (!cb) cb = noop
this._runAll('clear', [name, opts], cb)
}
download (name, opts, cb) {
// TODO: This needs to be reimplemented
// We only want to download the latest files accross the board
if (typeof opts === 'function') {
cb = opts
opts = null
}
opts = opts || {}
if (!cb) cb = noop
this._runAll('download', [name, opts], cb)
}
watch (name, onchange) {
// TODO: Should we blindly take all updates?
const watchers = this.drives.map((drive) => drive.watch(name, onchange))
return { destroy: destroy }
function destroy () {
watchers.map((watcher) => watcher.destroy())
}
}
mount (name, key, opts, cb) {
if (typeof opts === 'function') return this.mount(name, key, null, opts)
return this.writerOrPrimary.mount(name, key, opts, cb)
}
unmount (name, cb) {
return this.writerOrPrimary.unmount(name, cb)
}
symlink (target, linkName, cb) {
return this.writerOrPrimary.symlink(target, linkName, cb)
}
createMountStream (opts) {
// TODO: Combine mount streams from others
}
getAllMounts (opts, cb) {
// TODO: Combine mount list from others
}
registerExtension (name, handlers) {
return this.primary.registerExtension(name, handlers)
}
setMetadata (name, key, value, cb) {
return this.writerOrPrimary.setMetadata(name, key, value, cb)
}
removeMetadata (name, key, cb) {
return this.writerOrPrimary.removeMetadata(name, key, cb)
}
createTag (name, version, cb) {
// TODO: Figure out what versions mean for multiple drives
throw new Error('Not Supported')
}
getAllTags (cb) {
// TODO: Figure out what versions mean for multiple drives
throw new Error('Not Supported')
}
deleteTag (name, cb) {
// TODO: Figure out what versions mean for multiple drives
throw new Error('Not Supported')
}
getTaggedVersion (name, cb) {
// TODO: Figure out what versions mean for multiple drives
throw new Error('Not Supported')
}
}
module.exports = function multiHyperdrive (primary) {
return new MultiHyperdrive(primary)
}
module.exports.MultiHyperdrive = MultiHyperdrive
function noop () {}
function ensureDir (path, drive, cb) {
drive.exists(path, (exists) => {
if (exists) return cb(null)
const parent = dirname(path)
ensureDir(parent, drive, (err) => {
if (err) return cb(err)
drive.mkdir(path, cb)
})
})
}