This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
651 lines (567 loc) · 19.4 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
const crypto = require('crypto')
const EventEmitter = require('events')
const emitStream = require('emit-stream')
const CircularAppendFile = require('circular-append-file')
const through = require('through2')
const split = require('split2')
const concat = require('concat-stream')
const throttle = require('lodash.throttle')
const isEqual = require('lodash.isequal')
const pump = require('pump')
const jetpack = require('fs-jetpack')
const {join} = require('path')
// dat modules
const hyperdrive = require('hyperdrive')
const hypercoreProtocol = require('hypercore-protocol')
const pda = require('pauls-dat-api')
const datEncoding = require('dat-encoding')
// network modules
const swarmDefaults = require('datland-swarm-defaults')
const discoverySwarm = require('discovery-swarm')
const networkSpeed = require('hyperdrive-network-speed')
const {ThrottleGroup} = require('stream-throttle')
const datStorage = require('./storage')
const folderSync = require('./folder-sync')
const {addArchiveSwarmLogging} = require('./logging-utils')
const datExtensions = require('./extensions')
const scopedFSes = require('../../lib/scoped-fses')
const {DAT_SWARM_PORT} = require('../../lib/const')
const RPC_MANIFEST = require('./manifest')
// globals
// =
var datPath
var networkId = crypto.randomBytes(32)
var archives = {} // in-memory cache of archive objects. key -> archive
var archivesByDKey = {} // same, but discoveryKey -> archive
var archiveLoadPromises = {} // key -> promise
var daemonEvents = new EventEmitter()
var debugEvents = new EventEmitter()
var debugLogFile
var archiveSwarm
var upThrottleGroup
var downThrottleGroup
// exported api
// =
exports.setup = async function ({rpcAPI, logfilePath}) {
// export API
rpcAPI.exportAPI('dat-daemon', RPC_MANIFEST, RPC_API)
// setup storage
await datStorage.setup()
debugLogFile = CircularAppendFile(logfilePath, {maxSize: 1024 /* 1kb */ * 1024 /* 1mb */ * 50 /* 50mb */ })
// setup extension messages
datExtensions.setup()
// re-export events
folderSync.events.on('sync', (key, direction) => {
daemonEvents.emit('folder-synced', {
details: {
url: `dat://${datEncoding.toStr(key)}`,
direction
}
})
})
folderSync.events.on('error', (key, err) => {
daemonEvents.emit('folder-sync-error', {
details: {
url: `dat://${datEncoding.toStr(key)}`,
name: err.name,
message: err.message
}
})
})
// setup the archive swarm
archiveSwarm = discoverySwarm(swarmDefaults({
id: networkId,
hash: false,
utp: true,
tcp: true,
dht: false,
connect: connectReplicationStream,
stream: createReplicationStream
}))
addArchiveSwarmLogging({archivesByDKey, log, archiveSwarm})
archiveSwarm.once('error', () => archiveSwarm.listen(0))
archiveSwarm.listen(DAT_SWARM_PORT)
archiveSwarm.on('error', error => log(null, {event: 'swarm-error', message: error.toString()}))
}
// rpc api
// =
const RPC_API = {
// setup & config
// =
async setup (opts) {
datPath = opts.datPath
folderSync.setup(opts)
},
// up/down are in MB/s
async setBandwidthThrottle ({up, down}) {
if (typeof up !== 'undefined') {
upThrottleGroup = up ? new ThrottleGroup({rate: up * 1e6}) : null
}
if (typeof down !== 'undefined') {
downThrottleGroup = down ? new ThrottleGroup({rate: down * 1e6}) : null
}
},
// event streams & debug
// =
createEventStream () {
return emitStream(daemonEvents)
},
createDebugStream () {
return emitStream(debugEvents)
},
async getDebugLog (key) {
return new Promise((resolve, reject) => {
let rs = debugLogFile.createReadStream()
rs
.pipe(split())
.pipe(through({encoding: 'utf8', decodeStrings: false}, (data, _, cb) => {
if (data && (!key || data.startsWith(key))) {
return cb(null, data.slice(64) + '\n')
}
cb()
}))
.pipe(concat({encoding: 'string'}, resolve))
rs.on('error', reject)
})
},
// archive management
// =
async configureArchive (key, userSettings) {
var archive = getArchive(key)
if (archive) {
configureNetwork(archive, userSettings)
configureAutoDownload(archive, userSettings)
configureLocalSync(archive, userSettings)
}
},
async getArchiveInfo (key) {
var archive = getArchive(key)
if (!archive) return {}
return {
version: archive.version,
size: archive.size,
peers: archive.metadata.peers.length,
peerInfo: getArchivePeerInfos(archive),
peerHistory: archive.peerHistory,
networkStats: archive.networkStats
}
},
updateSizeTracking,
async loadArchive (opts) {
var {
key,
secretKey,
metaPath,
userSettings
} = opts
// create the archive instance
var archive = hyperdrive(datStorage.create(metaPath), key, {
sparse: true,
secretKey
// metadataStorageCacheSize: 0,
// contentStorageCacheSize: 0,
// treeCacheSize: 2048
})
archive.on('error', err => {
let k = key.toString('hex')
log(k, {event: 'archive-error', message: err.toString()})
console.error('Error in archive', k, err)
})
archive.metadata.on('peer-add', () => onNetworkChanged(archive))
archive.metadata.on('peer-remove', () => onNetworkChanged(archive))
archive.networkStats = networkSpeed(archive)
archive.replicationStreams = [] // list of all active replication streams
archive.peerHistory = [] // samples of the peer count
// wait for ready
await new Promise((resolve, reject) => {
archive.ready(err => {
if (err) reject(err)
else resolve()
})
})
await updateSizeTracking(archive)
// attach extensions
datExtensions.attach(archive)
// store in the discovery listing, so the swarmer can find it
// but not yet in the regular archives listing, because it's not fully loaded
var discoveryKey = datEncoding.toStr(archive.discoveryKey)
archivesByDKey[discoveryKey] = archive
// setup the archive based on current settings
configureNetwork(archive, userSettings)
configureAutoDownload(archive, userSettings)
configureLocalSync(archive, userSettings)
// await initial metadata sync if not the owner
if (!archive.writable && !archive.metadata.length) {
// wait to receive a first update
await new Promise((resolve, reject) => {
archive.metadata.update(err => {
if (err) reject(err)
else resolve()
})
})
}
if (!archive.writable) {
// always download all metadata
archive.metadata.download({start: 0, end: -1})
}
// watch for sync events
archive.fileActStream = pda.watch(archive)
archive.fileActStream.on('data', ([event, {path}]) => {
if (event === 'changed') {
if (!archive.localSyncSettings) return
// need to sync this change to the local folder
if (archive.localSyncSettings.autoPublish) {
// bidirectional sync: use the sync queue
folderSync.queueSyncEvent(archive, {toFolder: true})
} else {
// preview mode: just write this update to disk
folderSync.syncArchiveToFolder(archive, {paths: [path], shallow: false})
}
}
})
// store in the archives list
archives[datEncoding.toStr(archive.key)] = archive
// return some archive info
return {discoveryKey, writable: archive.writable}
},
async unloadArchive (key) {
const archive = archives[key]
if (!archive) {
return
}
// shutdown archive
leaveSwarm(key)
stopAutodownload(archive)
if (archive.fileActStream) {
archive.fileActStream.end()
archive.fileActStream = null
}
datExtensions.detach(archive)
await new Promise((resolve, reject) => {
archive.close(err => {
if (err) reject(err)
else resolve()
})
})
delete archivesByDKey[datEncoding.toStr(archive.discoveryKey)]
delete archives[key]
},
// archive methods
// =
callArchiveAsyncMethod (key, version, method, ...args) {
var cb = args.slice(-1)[0]
var checkout = getArchiveCheckout(key, version)
checkout[method](...args)
},
callArchiveReadStreamMethod (key, version, method, ...args) {
var checkout = getArchiveCheckout(key, version)
return checkout[method](...args)
},
callArchiveWriteStreamMethod (key, version, method, ...args) {
var checkout = getArchiveCheckout(key, version)
return checkout[method](...args)
},
callArchivePDAPromiseMethod (key, version, method, ...args) {
var checkout = getArchiveCheckout(key, version)
return pda[method](checkout, ...args)
},
callArchivePDAReadStreamMethod (key, version, method, ...args) {
var checkout = getArchiveCheckout(key, version)
return pda[method](checkout, ...args)
},
async clearFileCache (key, userSettings) {
var archive = await getArchive(key)
if (!archive || archive.writable) {
return // abort, only clear the content cache of downloaded archives
}
// clear the cache
await new Promise((resolve, reject) => {
archive.content.clear(0, archive.content.length, err => {
if (err) reject(err)
else resolve()
})
})
// force a reconfig of the autodownloader
stopAutodownload(archive)
configureAutoDownload(archive, userSettings)
},
async exportArchiveToArchive (opts) {
opts.srcArchive = getArchive(opts.srcArchive)
opts.dstArchive = getArchive(opts.dstArchive)
return pda.exportArchiveToArchive(opts)
},
// folder sync
// =
fs_assertSafePath: folderSync.assertSafePath,
fs_ensureSyncFinished: key => folderSync.ensureSyncFinished(getArchive(key)),
fs_diffListing: (key, ...args) => folderSync.diffListing(getArchive(key), ...args),
fs_diffFile: (key, ...args) => folderSync.diffFile(getArchive(key), ...args),
fe_queueSyncEvent: (key, ...args) => folderSync.queueSyncEvent(getArchive(key), ...args),
fs_syncFolderToArchive: (key, ...args) => folderSync.syncFolderToArchive(getArchive(key), ...args),
fs_syncArchiveToFolder: (key, ...args) => folderSync.syncArchiveToFolder(getArchive(key), ...args),
// dat extensions
// =
ext_listPeers: async (key, ...args) => datExtensions.listPeers(getArchive(key), ...args),
ext_getPeer: async (key, ...args) => datExtensions.getPeer(getArchive(key), ...args),
ext_getOwnPeerId: () => datEncoding.toStr(networkId),
ext_broadcastEphemeralMessage: async (key, ...args) => datExtensions.broadcastEphemeralMessage(getArchive(key), ...args),
ext_sendEphemeralMessage: async (key, ...args) => datExtensions.sendEphemeralMessage(getArchive(key), ...args),
ext_getSessionData: async (key, ...args) => datExtensions.getSessionData(getArchive(key), ...args),
ext_setSessionData: async (key, ...args) => datExtensions.setSessionData(getArchive(key), ...args),
ext_createDatPeersStream: async (key, ...args) => datExtensions.createDatPeersStream(getArchive(key), ...args)
}
// archive networking
// =
// set the networking of an archive based on settings
function configureNetwork (archive, settings) {
if (!settings || settings.networked) {
joinSwarm(archive)
} else {
leaveSwarm(archive)
}
}
// put the archive into the network, for upload and download
const joinSwarm = exports.joinSwarm = function joinSwarm (key, opts) {
var archive = (typeof key === 'object' && key.key) ? key : getArchive(key)
if (!archive || archive.isSwarming) return
archiveSwarm.join(archive.discoveryKey)
var keyStr = datEncoding.toStr(archive.key)
log(keyStr, {
event: 'swarming',
discoveryKey: datEncoding.toStr(archive.discoveryKey)
})
archive.isSwarming = true
}
// take the archive out of the network
const leaveSwarm = exports.leaveSwarm = function leaveSwarm (key) {
var archive = (typeof key === 'object' && key.discoveryKey) ? key : getArchive(key)
if (!archive || !archive.isSwarming) return
var keyStr = datEncoding.toStr(archive.key)
log(keyStr, {
event: 'unswarming',
message: `Disconnected ${archive.metadata.peers.length} peers`
})
archive.replicationStreams.forEach(stream => stream.destroy()) // stop all active replications
archive.replicationStreams.length = 0
archiveSwarm.leave(archive.discoveryKey)
archive.isSwarming = false
}
// internal methods
// =
function getArchive (key) {
if (key instanceof hyperdrive) return key
if (key.key) key = key.key
return archives[datEncoding.toStr(key)]
}
function getArchiveCheckout (key, version) {
var archive = getArchive(key)
var checkoutFS = archive
if (version) {
let seq = parseInt(version)
if (Number.isNaN(seq)) {
if (version === 'latest') {
// ignore, we use latest by default
} else if (version === 'preview') {
if (archive.localSyncSettings) {
// checkout local sync path
checkoutFS = scopedFSes.get(archive.localSyncSettings.path)
checkoutFS.setFilter(p => folderSync.applyDatIgnoreFilter(archive, p))
} else {
let err = new Error('Preview mode is not enabled for this dat')
err.noPreviewMode = true
throw err
}
} else {
throw new Error('Invalid version identifier:' + version)
}
} else {
if (seq <= 0) throw new Error('Version too low')
if (seq > archive.version) throw new Error('Version too high')
checkoutFS = archive.checkout(seq, {metadataStorageCacheSize: 0, contentStorageCacheSize: 0, treeCacheSize: 0})
}
}
return checkoutFS
}
async function updateSizeTracking (archive) {
archive = getArchive(archive)
archive.size = await pda.readSize(archive, '/')
return archive.size
}
function configureAutoDownload (archive, userSettings) {
if (archive.writable) {
return // abort, only used for unwritable
}
// HACK
// mafintosh is planning to put APIs for this inside of hyperdrive
// till then, we'll do our own inefficient downloader
// -prf
const isAutoDownloading = userSettings.isSaved && userSettings.autoDownload
if (!archive._autodownloader && isAutoDownloading) {
// setup the autodownload
archive._autodownloader = {
undownloadAll: () => {
if (archive.content) {
archive.content._selections.forEach(range => archive.content.undownload(range))
}
},
onUpdate: throttle(() => {
// cancel ALL previous, then prioritize ALL current
archive._autodownloader.undownloadAll()
pda.download(archive, '/').catch(e => { /* ignore cancels */ })
}, 5e3)
}
archive.metadata.on('download', archive._autodownloader.onUpdate)
pda.download(archive, '/').catch(e => { /* ignore cancels */ })
} else if (archive._autodownloader && !isAutoDownloading) {
stopAutodownload(archive)
}
}
function configureLocalSync (archive, userSettings) {
var oldLocalSyncSettings = archive.localSyncSettings
archive.localSyncSettings = getLocalSyncSettings(archive, userSettings)
if (!isEqual(archive.localSyncSettings, oldLocalSyncSettings)) {
// configure the local folder watcher if a change occurred
folderSync.configureFolderToArchiveWatcher(archive)
}
if (!archive.localSyncSettings || !archive.localSyncSettings.isUsingInternal) {
// clear the internal directory if it's not in use
jetpack.removeAsync(getInternalLocalSyncPath(archive))
}
}
function getLocalSyncSettings (archive, userSettings) {
if (!archive.writable || !userSettings.isSaved) {
return false
}
if (userSettings.localSyncPath) {
return {
path: userSettings.localSyncPath,
autoPublish: !userSettings.previewMode
}
}
if (userSettings.previewMode) {
return {
path: getInternalLocalSyncPath(archive),
autoPublish: false,
isUsingInternal: true
}
}
return false
}
function stopAutodownload (archive) {
if (archive._autodownloader) {
archive._autodownloader.undownloadAll()
archive.metadata.removeListener('download', archive._autodownloader.onUpdate)
archive._autodownloader = null
}
}
function connectReplicationStream (local, remote) {
var streams = [local, remote, local]
if (upThrottleGroup) streams.splice(1, 0, upThrottleGroup.throttle())
if (downThrottleGroup) streams.splice(-1, 0, downThrottleGroup.throttle())
pump(streams)
}
function createReplicationStream (info) {
// create the protocol stream
var streamKeys = [] // list of keys replicated over the streamd
var stream = hypercoreProtocol({
id: networkId,
live: true,
encrypt: true,
extensions: ['ephemeral', 'session-data']
})
stream.peerInfo = info
// add the archive if the discovery network gave us any info
if (info.channel) {
add(info.channel)
}
// add any requested archives
stream.on('feed', add)
function add (dkey) {
// lookup the archive
var dkeyStr = datEncoding.toStr(dkey)
var archive = archivesByDKey[dkeyStr]
if (!archive || !archive.isSwarming) {
return
}
if (archive.replicationStreams.indexOf(stream) !== -1) {
return // already replicating
}
// create the replication stream
archive.replicate({stream, live: true})
if (stream.destroyed) return // in case the stream was destroyed during setup
// track the stream
var keyStr = datEncoding.toStr(archive.key)
streamKeys.push(keyStr)
archive.replicationStreams.push(stream)
function onend () {
archive.replicationStreams = archive.replicationStreams.filter(s => (s !== stream))
}
stream.once('error', onend)
stream.once('end', onend)
stream.once('finish', onend)
stream.once('close', onend)
}
// debugging
stream.on('error', err => {
log(streamKeys, {
event: 'connection-error',
peer: `${info.host}:${info.port}`,
connectionType: info.type,
message: err.toString()
})
})
return stream
}
function onNetworkChanged (archive) {
var now = Date.now()
var lastHistory = archive.peerHistory.slice(-1)[0]
if (lastHistory && (now - lastHistory.ts) < 10e3) {
// if the last datapoint was < 10s ago, just update it
lastHistory.peers = archive.metadata.peers.length
} else {
archive.peerHistory.push({
ts: Date.now(),
peers: archive.metadata.peers.length
})
}
// keep peerHistory from getting too long
if (archive.peerHistory.length >= 500) {
// downsize to 360 points, which at 10s intervals covers one hour
archive.peerHistory = archive.peerHistory.slice(archive.peerHistory.length - 360)
}
// count # of peers
var totalPeerCount = 0
for (var k in archives) {
totalPeerCount += archives[k].metadata.peers.length
}
daemonEvents.emit('network-changed', {
details: {
url: `dat://${datEncoding.toStr(archive.key)}`,
peers: getArchivePeerInfos(archive),
connections: archive.metadata.peers.length,
totalPeerCount
}
})
}
function getArchivePeerInfos (archive) {
// old way, more accurate?
// archive.replicationStreams.map(s => ({host: s.peerInfo.host, port: s.peerInfo.port}))
return archive.metadata.peers.map(peer => peer.stream.stream.peerInfo).filter(Boolean)
}
function getInternalLocalSyncPath (archiveOrKey) {
var key = datEncoding.toStr(archiveOrKey.key || archiveOrKey)
return join(datPath, 'Archives', 'LocalCopy', key.slice(0, 2), key.slice(2))
}
// helpers
// =
function log (key, data) {
var keys = Array.isArray(key) ? key : [key]
keys.forEach(k => {
let data2 = Object.assign(data, {archiveKey: k})
debugEvents.emit(k, data2)
debugEvents.emit('all', data2)
})
if (keys[0]) {
debugLogFile.append(keys[0] + JSON.stringify(data) + '\n')
}
}