-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync.js
785 lines (697 loc) · 22.5 KB
/
sync.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
const createMediaReplicationStream = require('blob-store-replication-stream')
const Syncfile = require('osm-p2p-syncfile')
const debug = require('debug')('mapeo-sync')
const Swarm = require('discovery-swarm')
const events = require('events')
const fs = require('fs')
const os = require('os')
const randombytes = require('randombytes')
const pump = require('pump')
const crypto = require('hypercore-crypto')
const datDefaults = require('dat-swarm-defaults')
const MapeoSync = require('./lib/sync-stream')
const progressSync = require('./lib/db-sync-progress')
const websocket = require('websocket-stream')
// This is an insecure discovery key that will discover all local devices that
// don't have a project key in their configuration
const SYNC_DEFAULT_KEY = 'mapeo-sync'
// hyperlog-sneakernet is an old format that is no longer supported.
// osm-p2p-syncfile is the supported implementation.
// See https://www.npmjs.com/package/osm-p2p-syncfile module for more details
const SYNCFILE_FORMATS = {
'hyperlog-sneakernet': 1,
'osm-p2p-syncfile': 2
}
// When connecting to a local network, use these
// options for discovery-swarm
const DEFAULT_LOCAL_DISCO = {
dns: {
interval: 3000
},
dht: false,
utp: false
}
// When internet connectivity is on, use these
// options for discovery-swarm
//
// Internet Discovery is not yet enabled by Desktop or Mobile
// TODO: in production, we should not use datDefaults because
// it depends on some servers that mafintosh maintains
const DEFAULT_INTERNET_DISCO = Object.assign({}, datDefaults(), {
dns: {
interval: 3000
}
})
// When a sync fails to finish, and one of the feeds in the database (a hypercore feed)
// is incomplete, sync will throw this error after 20 seconds of inactive replication
const ERR_MISSING_DATA = 'ERR_MISSING_DATA'
const DEFAULT_HEARTBEAT_INTERVAL = 1000 * 20 // 20 seconds
// Available states for displaying syncronization progress to clients
const ReplicationState = {
WIFI_READY: 'replication-wifi-ready',
PROGRESS: 'replication-progress',
COMPLETE: 'replication-complete',
ERROR: 'replication-error',
STARTED: 'replication-started'
}
// These properties will be passed to the client when there is an error
const multifeedErrorProps = [
'code',
'usVersion',
'themVersion',
'usClient',
'themClient'
]
// Simple wrapper for the PeerState object
// that requires a topic and message
function PeerState (topic, message, other) {
return { topic, message, ...other }
}
/**
* Called by Sync.addPeer to add a peer to the SyncState
* @param {DuplexStream} connection Duplex stream, but has only been tested in production using TCP
* @param {Object} info
*/
function WifiPeer (connection, info) {
info.type = 'wifi'
info.connection = connection
info.swarmId = info.id // XXX: not used; for backwards compatibility
info.id = info.id.toString('hex')
return info
}
/**
* Called by Sync.addPeer to add a peer to the SyncState
* @param {DuplexStream} connection Duplex stream, but has only been tested in production using TCP
* @param {Object} info
*/
function WebsocketPeer (connection, info) {
info.type = 'websocket'
info.connection = connection
info.swarmId = info.id
return info
}
// SyncState is a state machine that manages the list of peers
// and their states, represented by PeerState objects
class SyncState {
constructor () {
this._state = {}
}
_add (peer) {
peer.state = PeerState(ReplicationState.WIFI_READY)
this.init(peer)
this._state[peer.id] = peer
}
// Start tracking a peer's state
// and include that peer in the list of all peers
init (peer) {
peer.started = false
peer.connected = true
peer.sync = new events.EventEmitter()
var onsync = () => this.onsync(peer)
var onerror = error => this.onerror(peer, error)
var onend = () => {
this.onend(peer)
peer.sync.removeListener('sync-start', onsync)
peer.sync.removeListener('end', onend)
peer.sync.removeListener('error', onerror)
if (peer.sync.onprogress)
peer.sync.removeListener('progress', peer.sync.onprogress)
}
peer.sync.on('sync-start', onsync)
peer.sync.on('error', onerror)
peer.sync.on('end', onend)
}
// Progress event listeners are added after sync-start by
// the caller of peer.add
addProgressEventListeners (peer) {
peer.sync.onprogress = progress => this.onprogress(peer, progress)
peer.sync.on('progress', peer.sync.onprogress)
}
// Get a peer by it's host and port
get (host, port) {
var res = Object.values(this._state).filter(function (peer) {
return peer.port === port && peer.host === host
})
if (res.length) {
return res[0]
} else {
return null
}
}
// We need to know if a peer has been hanging for a period of time
// with no progress. This could happen if replication is opened
// in non-sparse mode or live mode. It will then continue hanging and waiting
// for updates until it is told to stop.
stale (peer) {
var NOT_STALE = [
ReplicationState.STARTED,
ReplicationState.ERROR,
ReplicationState.COMPLETE,
ReplicationState.WIFI_READY
]
if (NOT_STALE.indexOf(peer.state.topic) > -1) return false
// XXX: This is important because this peer can get in a state where it's in
// progress but the other side has not yet acknowlegded to us that it has
// finished downloading
// we can remove this check once we have ACKs or proper backpressure
var INCOMPLETE =
peer.state.message.db.sofar !== peer.state.message.db.total ||
peer.state.message.media.sofar !== peer.state.message.media.total
var staleDate = Date.now() - DEFAULT_HEARTBEAT_INTERVAL
return (
peer.state.topic === ReplicationState.PROGRESS &&
peer.state.message.timestamp < staleDate &&
INCOMPLETE
)
}
_isactive (peer) {
return (
peer.state.topic === ReplicationState.PROGRESS ||
peer.state.topic === ReplicationState.STARTED
)
}
_isclosed (peer) {
return (
peer.state.topic === ReplicationState.COMPLETE ||
peer.state.topic === ReplicationState.ERROR
)
}
_iscomplete (peer) {
return peer.state.topic === ReplicationState.COMPLETE
}
// Add a peer that was discovered via Wifi (contains a TCP socket)
addWifiPeer (connection, info) {
const peerId = info.id.toString('hex')
let peer = this._state[peerId]
if (!peer) {
peer = WifiPeer(connection, info)
this._add(peer)
} else {
// reuse peer
this.init(peer)
}
return peer
}
// Add a peer that is a file
addFilePeer (filename) {
const peer = {
id: filename,
name: filename,
filename
}
this._state[peer.id] = peer
this._add(peer)
this.addProgressEventListeners(peer)
this.onsync(peer)
return peer
}
addWebsocketPeer (connection, info) {
const peerId = info.id.toString('hex')
let peer = this._state[peerId]
if (!peer) {
peer = WebsocketPeer(connection, info)
this._add(peer)
} else {
// reuse peer
this.init(peer)
}
return peer
}
onsync (peer) {
peer.started = true
peer.state = PeerState(ReplicationState.STARTED)
this.addProgressEventListeners(peer)
}
onprogress (peer, progress) {
if (this._isclosed(peer)) return
progress.timestamp = Date.now()
peer.state = PeerState(ReplicationState.PROGRESS, progress)
}
onerror (peer, error) {
if (this._isclosed(peer)) return
peer.connected = false
const errorMetadata = {}
multifeedErrorProps.forEach(key => {
if (error[key]) errorMetadata[key] = error[key]
})
peer.state = PeerState(
ReplicationState.ERROR,
error ? error.toString() : 'Error',
errorMetadata
)
}
onend (peer) {
if (this._isclosed(peer)) return
if (peer.started) {
peer.state = PeerState(ReplicationState.COMPLETE, Date.now())
}
}
// XXX: depends on 'peer' being a persistent reference
peers () {
return Object.values(this._state).map(peer => {
if (this._iscomplete(peer))
peer.state.lastCompletedDate = peer.state.message
return peer
})
}
}
// Sync class manages discovery of peers and replication
class Sync extends events.EventEmitter {
constructor (osm, media, opts) {
super()
opts = Object.assign(
opts.internetDiscovery ? DEFAULT_INTERNET_DISCO : DEFAULT_LOCAL_DISCO,
opts
)
opts.writeFormat = opts.writeFormat || 'osm-p2p-syncfile'
if (!SYNCFILE_FORMATS[opts.writeFormat])
throw new Error('unknown syncfile write format: ' + opts.writeFormat)
this.osm = osm
this.media = media
this.name = opts.name
if (Buffer.isBuffer(opts.id)) this.id = opts.id.toString('hex')
else if (typeof opts.id === 'string') this.id = opts.id
else if (!opts.id) this.id = randombytes(32).toString('hex')
else throw new Error('opts.id must be a string or buffer')
this.opts = Object.assign({}, opts)
// track all peer states
this.state = new SyncState()
}
peers () {
return this.state.peers()
}
/**
* Replicate with a given filename or host/port.
* If you are using this function to replicate with a Wifi peer (tcp socket) then
* this function needs to be called AFTER addPeer(socket, info)
*
* This could be refactored.
*
* @param {PeerQuery} A query for the peer
* @param {Object} opts Options for the replication
*/
replicate ({ host, port, filename }, opts) {
if (!opts) opts = {}
var peer
if (host && port) {
port = parseInt(port)
peer = this.state.get(host, port)
if (!peer) {
var emitter = new events.EventEmitter()
process.nextTick(() => {
emitter.emit('error', new Error('trying to sync to unknown peer'))
})
return emitter
}
this.replicateNetwork(peer, opts)
} else if (filename) {
peer = this.state.addFilePeer(filename)
this.replicateFromFile(peer, opts)
} else throw new Error('Requires filename or host and port')
return peer.sync
}
connectWebsocket (url, projectKey) {
let { protocol, hostname: host, port: rawPort } = new URL(url)
let port = rawPort
if (!port) {
port = protocol === 'ws:' ? 80 : 443
} else {
port = parseInt(rawPort, 10)
}
const info = {
id: url,
name: url,
host,
port,
type: 'websocket'
}
var key = discoveryKey(projectKey)
const replicationURL = new URL(`./replicate/v1/${key}`, url).href
const connection = websocket(replicationURL, { binary: true })
const peer = this.state.addWebsocketPeer(connection, info)
this.addPeer(connection, info)
return peer
}
// replicateNetwork on a peer that already exists (from sync.peers())
replicateNetwork (peer, opts) {
if (!peer.handshake) {
process.nextTick(() => {
peer.sync.emit(
'error',
new Error('trying to sync before handshake has occurred')
)
})
return peer.sync
}
if (!peer.connected) {
process.nextTick(() => {
peer.sync.emit(
'error',
new Error('trying to sync to a peer that is not connected')
)
})
return peer.sync
}
peer.handshake.accept()
delete peer.handshake
return peer.sync
}
// Start listening on discovery swarm
listen (cb) {
if (!cb) cb = () => {}
if (this.swarm && !this._destroyingSwarm) {
return process.nextTick(cb)
}
const _listen = () => {
this.osm.ready(() => {
this.osm.core._logs.writer('default', (err, feed) => {
if (err) return cb(err)
this.swarm = this._swarm(feed.key)
this.swarm.listen(0, err => {
if (err) return cb(err)
cb()
this.emit('listen')
})
})
})
}
if (this._destroyingSwarm) this.once('close', _listen)
else _listen()
}
// Stop listening for project key
leave (projectKey) {
var key = discoveryKey(projectKey)
debug('sync leave')
if (this.swarm) this.swarm.leave(key)
else {
debug('sync leaving silently')
return
}
}
// Start listening for project key
join (projectKey) {
const key = discoveryKey(projectKey)
const join = () => this.swarm.join(key)
if (!this.swarm) this.once('listen', join)
else join()
}
// Alias for close
destroy (cb) {
this.close(cb)
}
// Destroy everything (all tcp sockets and swarms)
close (cb) {
if (!cb) cb = () => {}
if (!this.swarm || this._destroyingSwarm) return process.nextTick(cb)
this._destroyingSwarm = true
this.swarm.destroy(() => {
this.swarm = null
this._destroyingSwarm = false
this.emit('close')
cb()
})
}
/**
* Replicate from a given file. Use `replicate` instead.
* @param {String} peer A peer.
* @param {{createFile: ?boolean, projectKey: ?string, writeFormat: ?string}} [opts]
* @return {EventEmitter} Listen to 'error', 'end' and 'progress' events
*/
replicateFromFile (peer, opts) {
var self = this
var emitter = peer.sync
var filename = peer.filename
opts = opts || {}
fs.access(filename, function (err) {
if (err) {
// file doesn't exist, write
if (self.opts.writeFormat === 'osm-p2p-syncfile') sync()
else return emitError(new Error('unsupported syncfile type'))
} else {
// read
isGzipFile(filename, function (err, isGzip) {
if (err) return emitError(err)
if (!isGzip) sync()
else return emitError(new Error('unsupported syncfile type'))
})
}
})
function sync () {
const discoKey = discoveryKey(opts.projectKey)
const syncfile = new Syncfile(filename, os.tmpdir(), {
encryptionKey: opts.projectKey
})
function closeSyncfile (onClose) {
syncfile.close(function (closeErr) {
onClose(closeErr)
})
}
function createSyncfileErrorHandler (err) {
return function (closeErr) {
const errorToEmit = err || closeErr
if (errorToEmit) {
emitError(errorToEmit)
}
}
}
syncfile.ready(function (err) {
if (err) return closeSyncfile(createSyncfileErrorHandler(err))
syncfile.userdata(function (err, data) {
if (err) return closeSyncfile(handeSyncfileError(err))
if (data && data['p2p-db'] && data['p2p-db'] !== 'kappa-osm') {
return closeSyncfile(
createSyncfileErrorHandler(
new Error(
'trying to sync this kappa-osm database with a ' +
data['p2p-db'] +
' database!'
)
)
)
}
if (!opts.createFile) {
const ourProjectId = discoKey // This is SYNC_DEFAULT_KEY if opts.projectKey is undefined
const fileProjectId =
(data && data.discoveryKey) || SYNC_DEFAULT_KEY
if (ourProjectId !== fileProjectId) {
return closeSyncfile(
createSyncfileErrorHandler(
new Error(
`trying to sync two different projects (us=${formatId(
ourProjectId
)}) (syncfile=${formatId(fileProjectId)})`
)
)
)
function formatId (id) {
return id === SYNC_DEFAULT_KEY ? id : id.slice(0, 7)
}
}
}
start()
})
})
function start () {
const r1 = syncfile.replicateData({ live: false })
const r2 = progressSync(self, { live: false })
const m1 = syncfile.replicateMedia()
const m2 = createMediaReplicationStream(self.media)
var error
var pending = 2
pump(r1, r2, r1, fin)
pump(m1, m2, m1, fin)
function fin (err) {
// HACK(noffle): workaround for sync bug
if (err && err.message === 'premature close') err = undefined
if (err) error = err
if (!--pending) {
var userdata = {
'p2p-db': 'kappa-osm'
}
if (opts.projectKey) userdata.discoveryKey = discoKey
syncfile.userdata(userdata, function () {
closeSyncfile(function (closeErr) {
if (error || closeErr) {
createSyncfileErrorHandler(error)(closeErr)
return
}
self.osm.ready(function () {
self.emit('down', peer)
emitter.emit('end')
})
})
})
}
}
// track sync progress
var progress = {
db: { sofar: 0, total: 0 },
media: { sofar: 0, total: 0 }
}
r2.on('progress', function (sofar, total) {
progress.db.sofar = sofar
progress.db.total = total
emitter.emit('progress', progress)
})
m2.on('progress', function (sofar, total) {
progress.media.sofar = sofar
progress.media.total = total
emitter.emit('progress', progress)
})
}
}
function emitError (err) {
emitter.emit('error', err)
}
return emitter
}
setName (name) {
this.name = name
}
_swarm (id) {
var swarm = Swarm(Object.assign(this.opts, { id: id }))
swarm.on('connection', this.addPeer.bind(this))
return swarm
}
/**
* Add a peer to the list of available peers. Must be called before `replicate`
*
* @param {DuplexStream} connection This is a duplex stream, although it has only been tested in production with TCP sockets.
* @param {Object} info This is an object that contains information about the TCP socket, including a unique, persistent id, as well as host, and port
*/
addPeer (connection, info) {
var self = this
debug('connection', info.host, info.port, info.id.toString('hex'))
let peer
let deviceType
let disconnected = false
let heartbeat
connection.on('close', onClose)
connection.on('error', onClose)
var open = true
var stream
setTimeout(doSync, 500)
function onClose (err) {
disconnected = true
if (peer) peer.connected = false
if (heartbeat) clearInterval(heartbeat)
debug('onClose', info.host, info.port, err)
if (!open) return
open = false
if (peer) {
debug('emitting sync end/error event', info.host, info.port, err)
if (err) peer.sync.emit('error', err)
else peer.sync.emit('end')
self.emit('down', peer)
}
debug('down', info.host, info.port)
}
function doSync () {
if (!open || disconnected) return
debug('doSync', info.host, info.port)
// Set up the sync stream immediately, but don't do anything with it
// until one side initiates the sync operation.
deviceType = self.opts.deviceType
const id = Buffer.isBuffer(info.id) ? info.id.toString('hex') : info.id
stream = MapeoSync(self.osm, self.media, {
id: id,
deviceType: deviceType,
deviceName: self.name || os.hostname(),
handshake: onHandshake
})
stream.once('sync-start', function () {
debug('sync started', info.host, info.port)
if (peer) peer.sync.emit('sync-start')
self.osm.core.pause()
// XXX: This is a hack to ensure sync streams always end eventually
// Ideally, we'd open a sparse hypercore instead.
heartbeat = setInterval(() => {
var stale = self.state.stale(peer)
if (stale) {
var err = new Error('timed out due to missing data')
err.code = ERR_MISSING_DATA
connection.destroy(err)
}
debug('heartbeat', stale)
}, DEFAULT_HEARTBEAT_INTERVAL)
})
stream.on('progress', progress => {
debug('sync progress', info.host, info.port, progress)
if (peer) peer.sync.emit('progress', progress)
})
pump(stream, connection, stream, function (err) {
debug('pump ended', info.host, info.port, err)
if (peer && peer.started) {
self.osm.core.resume()
}
if (peer && peer.started && !stream.goodFinish && !err) {
err = new Error('sync stream terminated on remote side')
}
if (stream.goodFinish && err) err = undefined
onClose(err)
})
connection.removeListener('close', onClose)
connection.removeListener('error', onClose)
}
// Peers announce their device type, version, and name
// When a user hits 'SYNC' on the client, then the client calls the replicate
// function and accepts the handshake
function onHandshake (req, accept) {
debug('got handshake', info.host, info.port)
// as soon as any data is received, accept! Because this means that
// the other side just have accepted & wants to start.
stream.once('accepted', function () {
accept()
})
if (info.type === 'ws') {
peer = self.state.addWebsocketPeer(connection, info)
} else {
peer = self.state.addWifiPeer(connection, info)
}
peer.handshake = { accept: accept }
peer.deviceType = req.deviceType
peer.name = req.deviceName
self.emit('peer', peer)
}
}
}
function isGzipFile (filepath, cb) {
fs.exists(filepath, function (exists) {
if (!exists) return cb(null, false)
fs.open(filepath, 'r', function (err, fd) {
if (err) return cb(err)
var magic = Buffer.alloc(2)
fs.read(fd, magic, 0, 2, 0, function (err) {
if (err) return cb(err)
const isGzip = magic.toString('hex') === '1f8b'
fs.close(fd, function (err) {
if (err) return cb(err)
cb(null, isGzip)
})
})
})
})
}
/**
* Generate a discovery key for a given projectKey. If projectKey is undefined
* then it will use SYNC_DEFAULT_KEY as the discovery key (this is for backwards
* compatibility with clients that did not use projectKeys)
*
* @param {String|Buffer} projectKey A unique random key identifying the
* project. Must be 32-byte Buffer or a string hex encoding of a 32-Byte buffer
*/
function discoveryKey (projectKey) {
if (typeof projectKey === 'undefined') return SYNC_DEFAULT_KEY
if (typeof projectKey === 'string') {
projectKey = Buffer.from(projectKey, 'hex')
}
if (Buffer.isBuffer(projectKey) && projectKey.length === 32) {
return crypto.discoveryKey(projectKey).toString('hex')
} else {
throw new Error(
'projectKey must be undefined or a 32-byte Buffer, or a hex string encoding a 32-byte buffer'
)
}
}
Sync.discoveryKey = discoveryKey
module.exports = Sync