|
1 | | -var assert = require('assert') |
2 | | -var path = require('path') |
3 | | -var untildify = require('untildify') |
4 | | -var importFiles = require('./lib/import-files') |
5 | | -var createNetwork = require('./lib/network') |
6 | | -var stats = require('./lib/stats') |
7 | | -var serveHttp = require('./lib/serve') |
8 | | -var debug = require('debug')('dat-node') |
9 | | - |
10 | | -module.exports = Dat |
11 | | - |
12 | | -/** |
13 | | - * @class Dat |
14 | | - * @type {Object} |
15 | | - * @param {Object} archive - Hyperdrive archive |
16 | | - * @param {Object} [opts] - Options |
17 | | - * @param {String} [opts.dir] - Directory of archive |
18 | | - * |
19 | | - * @property {Object} archive - Hyperdrive Archive |
20 | | - * @property {String} path - Resolved path of archive |
21 | | - * @property {Buffer} key - Archive Key (`archive.key`) |
22 | | - * @property {Boolean} live - Archive is live (`archive.live`) |
23 | | - * @property {Boolean} writable - Archive is writable (`archive.metadata.writable`) |
24 | | - * @property {Boolean} version - Archive version (`archive.version`) |
25 | | - * @property {Object} options - Initial options and all options passed to childen functions. |
26 | | - * @returns {Object} Dat |
27 | | - */ |
28 | | -function Dat (archive, opts) { |
29 | | - if (!(this instanceof Dat)) return new Dat(archive, opts) |
30 | | - assert.ok(archive, 'archive required') |
31 | | - var self = this |
32 | | - |
33 | | - this.archive = archive |
34 | | - this.options = Object.assign({}, opts) |
35 | | - if (opts.dir) { |
36 | | - this.path = path.resolve(untildify(opts.dir)) |
| 1 | +const assert = require('assert') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +const untildify = require('untildify') |
| 5 | +const importFiles = require('./lib/import-files') |
| 6 | +const createNetwork = require('./lib/network') |
| 7 | +const stats = require('./lib/stats') |
| 8 | +const serveHttp = require('./lib/serve') |
| 9 | +const debug = require('debug')('dat-node') |
| 10 | + |
| 11 | +module.exports = (...args) => new Dat(...args) |
| 12 | + |
| 13 | +class Dat { |
| 14 | + constructor (archive, opts) { |
| 15 | + assert.ok(archive, 'archive required') |
| 16 | + |
| 17 | + this.archive = archive |
| 18 | + this.options = Object.assign({}, opts) |
| 19 | + if (opts.dir) { |
| 20 | + this.path = path.resolve(untildify(opts.dir)) |
| 21 | + } |
37 | 22 | } |
38 | 23 |
|
39 | | - // Getters for convenience accessors |
40 | | - Object.defineProperties(this, { |
41 | | - key: { |
42 | | - enumerable: true, |
43 | | - get: function () { |
44 | | - return self.archive.key |
45 | | - } |
46 | | - }, |
47 | | - live: { |
48 | | - enumerable: true, |
49 | | - get: function () { |
50 | | - return self.archive.live |
51 | | - } |
52 | | - }, |
53 | | - resumed: { |
54 | | - enumerable: true, |
55 | | - get: function () { |
56 | | - return self.archive.resumed |
57 | | - } |
58 | | - }, |
59 | | - writable: { |
60 | | - enumerable: true, |
61 | | - get: function () { |
62 | | - return self.archive.metadata.writable |
63 | | - } |
64 | | - }, |
65 | | - version: { |
66 | | - enumerable: true, |
67 | | - get: function () { |
68 | | - return self.archive.version |
69 | | - } |
70 | | - } |
71 | | - }) |
72 | | -} |
| 24 | + get key () { |
| 25 | + return this.archive.key |
| 26 | + } |
73 | 27 |
|
74 | | -/** |
75 | | - * Join Dat Network via Hyperdiscovery |
76 | | - * @type {Function} |
77 | | - * @param {Object} [opts] - Network options, passed to hyperdiscovery. |
78 | | - * @param {Function} [cb] - Callback after first round of discovery is finished. |
79 | | - * @returns {Object} Discovery Swarm Instance |
80 | | - */ |
81 | | -Dat.prototype.joinNetwork = |
82 | | -Dat.prototype.join = function (opts, cb) { |
83 | | - if (typeof opts === 'function') { |
84 | | - cb = opts |
85 | | - opts = {} |
| 28 | + get live () { |
| 29 | + return this.archive.live |
86 | 30 | } |
87 | 31 |
|
88 | | - var self = this |
89 | | - if (!opts && self.options.network) opts = self.options.network // use previous options |
90 | | - else opts = opts || {} |
91 | | - cb = cb || noop |
92 | | - |
93 | | - var netOpts = Object.assign({}, { |
94 | | - stream: function (peer) { |
95 | | - var stream = self.archive.replicate({ |
96 | | - upload: !(opts.upload === false), |
97 | | - download: !self.writable && opts.download, |
98 | | - live: !opts.end |
99 | | - }) |
100 | | - stream.on('close', function () { |
101 | | - debug('Stream close') |
102 | | - }) |
103 | | - stream.on('error', function (err) { |
104 | | - debug('Replication error:', err.message) |
105 | | - }) |
106 | | - stream.on('end', function () { |
107 | | - self.downloaded = true |
108 | | - debug('Replication stream ended') |
109 | | - }) |
110 | | - return stream |
| 32 | + get resumed () { |
| 33 | + return this.archive.resumed |
| 34 | + } |
| 35 | + |
| 36 | + get writable () { |
| 37 | + return this.archive.metadata.writable |
| 38 | + } |
| 39 | + |
| 40 | + get version () { |
| 41 | + return this.archive.version |
| 42 | + } |
| 43 | + |
| 44 | + join (opts, cb) { |
| 45 | + if (typeof opts === 'function') { |
| 46 | + cb = opts |
| 47 | + opts = {} |
111 | 48 | } |
112 | | - }, opts) |
113 | 49 |
|
114 | | - var network = self.network = createNetwork(self.archive, netOpts, cb) |
115 | | - self.options.network = netOpts |
| 50 | + var self = this |
| 51 | + if (!opts && self.options.network) opts = self.options.network // use previous options |
| 52 | + else opts = opts || {} |
| 53 | + cb = cb || noop |
| 54 | + |
| 55 | + var netOpts = Object.assign({}, { |
| 56 | + stream: function (peer) { |
| 57 | + var stream = self.archive.replicate({ |
| 58 | + upload: !(opts.upload === false), |
| 59 | + download: !self.writable && opts.download, |
| 60 | + live: !opts.end |
| 61 | + }) |
| 62 | + stream.on('close', function () { |
| 63 | + debug('Stream close') |
| 64 | + }) |
| 65 | + stream.on('error', function (err) { |
| 66 | + debug('Replication error:', err.message) |
| 67 | + }) |
| 68 | + stream.on('end', function () { |
| 69 | + self.downloaded = true |
| 70 | + debug('Replication stream ended') |
| 71 | + }) |
| 72 | + return stream |
| 73 | + } |
| 74 | + }, opts) |
116 | 75 |
|
117 | | - return network |
118 | | -} |
| 76 | + var network = self.network = createNetwork(self.archive, netOpts, cb) |
| 77 | + self.options.network = netOpts |
119 | 78 |
|
120 | | -/** |
121 | | - * Leave Dat Network |
122 | | - * @type {Function} |
123 | | - * @param {Function} [cb] - Callback after network is closed |
124 | | - */ |
125 | | -Dat.prototype.leaveNetwork = |
126 | | -Dat.prototype.leave = function (cb) { |
127 | | - if (!this.network) return |
128 | | - debug('leaveNetwork()') |
129 | | - // TODO: v8 unreplicate ? |
130 | | - // this.archive.unreplicate() |
131 | | - this.network.leave(this.archive.discoveryKey) |
132 | | - this.network.destroy(cb) |
133 | | - delete this.network |
134 | | -} |
| 79 | + return network |
| 80 | + } |
135 | 81 |
|
136 | | -/** |
137 | | - * Pause Dat Network |
138 | | - * @type {Function} |
139 | | - */ |
140 | | -Dat.prototype.pause = function () { |
141 | | - debug('pause()') |
142 | | - this.leave() |
143 | | -} |
| 82 | + leave (cb) { |
| 83 | + if (!cb) cb = noop |
| 84 | + if (!this || !this.network) return cb() |
| 85 | + debug('leaveNetwork()') |
| 86 | + // TODO: v8 unreplicate ? |
| 87 | + // this.archive.unreplicate() |
| 88 | + this.network.leave(this.archive.discoveryKey) |
| 89 | + this.network.destroy(cb) |
| 90 | + delete this.network |
| 91 | + } |
144 | 92 |
|
145 | | -/** |
146 | | - * Resume Dat Network |
147 | | - * @type {Function} |
148 | | - */ |
149 | | -Dat.prototype.resume = function () { |
150 | | - debug('resume()') |
151 | | - this.join() |
152 | | -} |
| 93 | + pause () { |
| 94 | + debug('pause()') |
| 95 | + this.leave() |
| 96 | + } |
153 | 97 |
|
154 | | -/** |
155 | | - * Track archive stats |
156 | | - * @type {Function} |
157 | | - */ |
158 | | -Dat.prototype.trackStats = function (opts) { |
159 | | - opts = Object.assign({}, opts) |
160 | | - this.stats = stats(this.archive, opts) |
161 | | - return this.stats |
162 | | -} |
| 98 | + resume () { |
| 99 | + debug('resume()') |
| 100 | + this.joinNetwork() |
| 101 | + } |
163 | 102 |
|
164 | | -/** |
165 | | - * Import files to archive via mirror-folder |
166 | | - * @type {Function} |
167 | | - * @param {String} [src=dat.path] - Directory or File to import to `archive`. |
168 | | - * @param {Function} [cb] - Callback after import is finished |
169 | | - * @param {Object} [opts] - Options passed to `mirror-folder` and `dat-ignore` |
170 | | - * @returns {Object} - Import progress |
171 | | - */ |
172 | | -Dat.prototype.importFiles = function (src, opts, cb) { |
173 | | - if (!this.writable) throw new Error('Must be archive owner to import files.') |
174 | | - if (typeof src !== 'string') return this.importFiles('', src, opts) |
175 | | - if (typeof opts === 'function') return this.importFiles(src, {}, opts) |
176 | | - |
177 | | - var self = this |
178 | | - src = src && src.length ? src : self.path |
179 | | - opts = Object.assign({ |
180 | | - indexing: (opts && opts.indexing) || (src === self.path) |
181 | | - }, opts) |
182 | | - |
183 | | - self.importer = importFiles(self.archive, src, opts, cb) |
184 | | - self.options.importer = self.importer.options |
185 | | - return self.importer |
186 | | -} |
| 103 | + trackStats (opts) { |
| 104 | + opts = Object.assign({}, opts) |
| 105 | + this.stats = stats(this.archive, opts) |
| 106 | + return this.stats |
| 107 | + } |
187 | 108 |
|
188 | | -/** |
189 | | - * Serve archive over http |
190 | | - * @type {Function} |
191 | | - * @param {Object} [opts] - Options passed to `mirror-folder` and `dat-ignore` |
192 | | - * @returns {Object} - node http server instance |
193 | | - */ |
194 | | -Dat.prototype.serveHttp = function (opts) { |
195 | | - this.server = serveHttp(this.archive, opts) |
196 | | - return this.server |
197 | | -} |
| 109 | + importFiles (src, opts, cb) { |
| 110 | + if (!this.writable) throw new Error('Must be archive owner to import files.') |
| 111 | + if (typeof src !== 'string') return this.importFiles('', src, opts) |
| 112 | + if (typeof opts === 'function') return this.importFiles(src, {}, opts) |
198 | 113 |
|
199 | | -/** |
200 | | - * Close Dat archive and other things |
201 | | - * @type {Function} |
202 | | - * @param {Function} [cb] - Callback after all items closed |
203 | | - */ |
204 | | -Dat.prototype.close = function (cb) { |
205 | | - cb = cb || noop |
206 | | - if (this._closed) return cb(new Error('Dat is already closed')) |
207 | | - |
208 | | - var self = this |
209 | | - self._closed = true |
210 | | - |
211 | | - debug('closing network') |
212 | | - closeNet(function (err) { |
213 | | - if (err) debug('Error while closing network:', err.message) |
214 | | - debug('closing closeFileWatch') |
215 | | - closeFileWatch(function () { |
216 | | - // self.archive.unreplicate() |
217 | | - debug('closing archive') |
218 | | - self.archive.close(cb) |
219 | | - }) |
220 | | - }) |
| 114 | + var self = this |
| 115 | + src = src && src.length ? src : self.path |
| 116 | + opts = Object.assign({ |
| 117 | + indexing: (opts && opts.indexing) || (src === self.path) |
| 118 | + }, opts) |
221 | 119 |
|
222 | | - function closeNet (cb) { |
223 | | - if (!self.network) return cb() |
224 | | - self.leave(cb) |
| 120 | + self.importer = importFiles(self.archive, src, opts, cb) |
| 121 | + self.options.importer = self.importer.options |
| 122 | + return self.importer |
225 | 123 | } |
226 | 124 |
|
227 | | - function closeFileWatch (cb) { |
228 | | - if (!self.importer) return cb() |
229 | | - // Emitting an event, as imported doesn't emit an event on |
230 | | - // destroy and there is no other means to see if this was called. |
231 | | - self.importer.emit('destroy') |
232 | | - self.importer.destroy() |
233 | | - delete self.importer |
234 | | - process.nextTick(cb) |
| 125 | + serveHttp (opts) { |
| 126 | + this.server = serveHttp(this.archive, opts) |
| 127 | + return this.server |
| 128 | + } |
| 129 | + |
| 130 | + close (cb) { |
| 131 | + cb = cb || noop |
| 132 | + if (this._closed) return cb(new Error('Dat is already closed')) |
| 133 | + |
| 134 | + var self = this |
| 135 | + self._closed = true |
| 136 | + |
| 137 | + debug('closing network') |
| 138 | + closeNet(function (err) { |
| 139 | + if (err) debug('Error while closing network:', err.message) |
| 140 | + debug('closing closeFileWatch') |
| 141 | + closeFileWatch(function () { |
| 142 | + // self.archive.unreplicate() |
| 143 | + debug('closing archive') |
| 144 | + self.archive.close(cb) |
| 145 | + }) |
| 146 | + }) |
| 147 | + |
| 148 | + function closeNet (cb) { |
| 149 | + if (!self.network) return cb() |
| 150 | + self.leave(cb) |
| 151 | + } |
| 152 | + |
| 153 | + function closeFileWatch (cb) { |
| 154 | + if (!self.importer) return cb() |
| 155 | + // Emitting an event, as imported doesn't emit an event on |
| 156 | + // destroy and there is no other means to see if this was called. |
| 157 | + self.importer.emit('destroy') |
| 158 | + self.importer.destroy() |
| 159 | + delete self.importer |
| 160 | + process.nextTick(cb) |
| 161 | + } |
235 | 162 | } |
236 | 163 | } |
| 164 | +Dat.prototype.joinNetwork = Dat.prototype.join |
| 165 | +Dat.prototype.leaveNetwork = Dat.prototype.leave |
237 | 166 |
|
238 | 167 | function noop () { } |
0 commit comments