This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
dat.js
167 lines (139 loc) · 4.01 KB
/
dat.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
const assert = require('assert')
const path = require('path')
const untildify = require('untildify')
const importFiles = require('./lib/import-files')
const createNetwork = require('./lib/network')
const stats = require('./lib/stats')
const serveHttp = require('./lib/serve')
const debug = require('debug')('dat-node')
module.exports = (...args) => new Dat(...args)
class Dat {
constructor (archive, opts) {
assert.ok(archive, 'archive required')
this.archive = archive
this.options = Object.assign({}, opts)
if (opts.dir) {
this.path = path.resolve(untildify(opts.dir))
}
}
get key () {
return this.archive.key
}
get live () {
return this.archive.live
}
get resumed () {
return this.archive.resumed
}
get writable () {
return this.archive.metadata.writable
}
get version () {
return this.archive.version
}
join (opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
var self = this
if (!opts && self.options.network) opts = self.options.network // use previous options
else opts = opts || {}
cb = cb || noop
var netOpts = Object.assign({}, {
stream: function (peer) {
var stream = self.archive.replicate({
upload: !(opts.upload === false),
download: !self.writable && opts.download,
live: !opts.end
})
stream.on('close', function () {
debug('Stream close')
})
stream.on('error', function (err) {
debug('Replication error:', err.message)
})
stream.on('end', function () {
self.downloaded = true
debug('Replication stream ended')
})
return stream
}
}, opts)
var network = self.network = createNetwork(self.archive, netOpts, cb)
self.options.network = netOpts
return network
}
leave (cb) {
if (!cb) cb = noop
if (!this || !this.network) return cb()
debug('leaveNetwork()')
// TODO: v8 unreplicate ?
// this.archive.unreplicate()
this.network.leave(this.archive.discoveryKey)
this.network.destroy(cb)
delete this.network
}
pause () {
debug('pause()')
this.leave()
}
resume () {
debug('resume()')
this.joinNetwork()
}
trackStats (opts) {
opts = Object.assign({}, opts)
this.stats = stats(this.archive, opts)
return this.stats
}
importFiles (src, opts, cb) {
if (!this.writable) throw new Error('Must be archive owner to import files.')
if (typeof src !== 'string') return this.importFiles('', src, opts)
if (typeof opts === 'function') return this.importFiles(src, {}, opts)
var self = this
src = src && src.length ? src : self.path
opts = Object.assign({
indexing: (opts && opts.indexing) || (src === self.path)
}, opts)
self.importer = importFiles(self.archive, src, opts, cb)
self.options.importer = self.importer.options
return self.importer
}
serveHttp (opts) {
this.server = serveHttp(this.archive, opts)
return this.server
}
close (cb) {
cb = cb || noop
if (this._closed) return cb(new Error('Dat is already closed'))
var self = this
self._closed = true
debug('closing network')
closeNet(function (err) {
if (err) debug('Error while closing network:', err.message)
debug('closing closeFileWatch')
closeFileWatch(function () {
// self.archive.unreplicate()
debug('closing archive')
self.archive.close(cb)
})
})
function closeNet (cb) {
if (!self.network) return cb()
self.leave(cb)
}
function closeFileWatch (cb) {
if (!self.importer) return cb()
// Emitting an event, as imported doesn't emit an event on
// destroy and there is no other means to see if this was called.
self.importer.emit('destroy')
self.importer.destroy()
delete self.importer
process.nextTick(cb)
}
}
}
Dat.prototype.joinNetwork = Dat.prototype.join
Dat.prototype.leaveNetwork = Dat.prototype.leave
function noop () { }