-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
72 lines (62 loc) · 1.67 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
var path = require('path')
var xtend = require('xtend')
var mkdirp = require('mkdirp')
var hyperdrive = require('hyperdrive')
var network = require('hyperdiscovery')
var ram = require('random-access-memory')
var mirror = require('mirror-folder')
var countDir = require('count-files')
var datIgnore = require('dat-ignore')
var debug = require('debug')('dat')
module.exports = run
function run (src, dest, opts, cb) {
var key
if (dest) {
key = src
src = null
mkdirp.sync(dest)
}
var dir = dest || src
opts = xtend({
storage: opts.sleep ? path.join(dir, '.dat') : ram
}, opts)
var archive = hyperdrive(opts.storage, key, opts)
archive.on('ready', function () {
var progress = importFiles()
var swarm = joinNetwork()
cb(archive, swarm, progress)
})
function importFiles () {
if (!archive.metadata.writable) return
var progress
var ignore = datIgnore(dir)
progress = mirror(dir, {name: '/', fs: archive}, {
ignore: ignore
})
progress.on('error', function (err) {
debug('IMPORT ERROR:', err)
})
countDir(dir, { ignore: ignore }, function (err, data) {
if (err) throw err
progress.emit('count', data)
})
return progress
}
function joinNetwork () {
var swarm = network(archive, xtend({
stream: function (peer) {
var stream = archive.replicate({
live: !archive.metadata.writable && opts.sync
})
stream.on('error', function (err) {
debug('Replication error:', err.message)
})
stream.on('end', function () {
archive.downloaded = true
})
return stream
}
}, opts))
return swarm
}
}