-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharchive.js
More file actions
234 lines (197 loc) · 5.78 KB
/
archive.js
File metadata and controls
234 lines (197 loc) · 5.78 KB
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
const EventEmitter = require('events').EventEmitter
const inherits = require('inherits')
const hyperdiscovery = require('hyperdiscovery')
const datenc = require('dat-encoding')
const { hex, asyncThunky, prom } = require('./util')
module.exports = Archive
function Archive (library, type, instance, state) {
if (!(this instanceof Archive)) return new Archive(library, type, instance, state)
const self = this
this._opened = false
this.instance = instance
this.db = instance.db
this.key = hex(instance.key)
this.library = library
this.state = state || {}
this.type = type
this.mounts = []
this.ready = asyncThunky(this._ready.bind(this))
this.ready()
}
inherits(Archive, EventEmitter)
Archive.prototype._ready = async function (done) {
const self = this
this.instance.ready(async () => {
if (!self._opened) await init()
})
async function init () {
self._opened = true
await self.loadMounts()
self.localKey = self.db.local.key
self.setState({ localKey: datenc.toStr(self.localKey) })
if (self.getState().share) {
self.startShare()
}
// The following callback is executed once the hyperdb has any heads
// available. For locally created dbs this is executed immediately,
// for remote dbs once the first data comes in.
self.db.heads(async () => {
await self._isAuthorized()
self.setState({ loaded: true })
self.emit('loaded')
})
done()
}
}
Archive.prototype.makePersistentMount = async function (type, prefix, key, opts) {
await this.ready()
if (!(await this.isAuthorized())) throw new Error('Archive is not writable.')
const archive = await this.addMount({ type, prefix, key, opts })
await this.instance.addMount({ type, prefix, key: archive.key })
return archive
}
Archive.prototype.addMount = async function ({ type, prefix, key, opts }) {
// todo: handle opts?
opts = opts || {}
const status = {
primary: false,
parent: this.key,
}
const archive = await this.library.addArchive(type, key, opts, status)
const mountInfo = { prefix, type, key: archive.key }
this.pushMount(mountInfo)
return archive
}
Archive.prototype.pushMount = function (mount) {
this.mounts.push(mount)
this.emit('mount', mount)
}
Archive.prototype.loadMounts = async function () {
const self = this
await this.instance.ready()
let mounts
if (this.instance.getMounts) {
mounts = await this.instance.getMounts()
mounts.forEach(mount => self.addMount(mount))
}
}
Archive.prototype.getMounts = async function () {
await this.ready()
if (!this.mounts) return []
return this.mounts.map(mount => this.library.getArchive(mount.key))
}
Archive.prototype.getMount = async function (prefix) {
await this.ready()
const mounts = this.mounts.filter(m => m.prefix === prefix)
if (mounts.length) return this.library.getArchive(mounts[0].key)
return null
}
Archive.prototype.getMountInstance = async function (prefix) {
const mount = await this.getMount(prefix)
if (!mount) return null
return mount.getInstance()
}
Archive.prototype.getInstance = function () {
return this.instance
}
Archive.prototype.getInfo = async function () {
if (this.instance.getInfo) return this.instance.getInfo()
return {}
}
Archive.prototype.setInfo = async function (info) {
if (!this.isLoaded()) return
if (this.instance.setInfo) return this.instance.setInfo(info)
this.emit('info:set', info)
return null
}
Archive.prototype.getState = function () {
return this.state
}
Archive.prototype.setState = function (state) {
this.state = { ...this.state, ...state }
this.emit('state:set', this.state)
}
Archive.prototype.isPrimary = function () {
return this.state.primary
}
Archive.prototype.isLoaded = function () {
return this.state.loaded
}
Archive.prototype.isAuthorized = async function () {
await this.ready()
return this._isAuthorized()
}
Archive.prototype._isAuthorized = async function () {
const self = this
return new Promise((resolve, reject) => {
this.db.authorized(this.localKey, (err, res) => {
if (err) reject(err)
self.setState({ authorized: res })
resolve(res)
})
})
}
Archive.prototype.setShare = async function (share) {
this.setState({ share })
if (share) {
this.startShare()
} else {
this.stopShare()
}
return
}
Archive.prototype.startShare = async function () {
await this.ready()
const instance = this.getInstance()
// todo: make pluggable.
const network = hyperdiscovery(instance.db)
this.network = network
this.emit('network:open')
network.on('connection', (peer) => {
this.emit('network:peer', peer)
})
// todo: really always share all mounts? If decided compare with this.stopShare()!
let mounts = await this.getMounts()
mounts.forEach(mount => {
mount.startShare()
})
}
Archive.prototype.stopShare = async function () {
await this.ready()
if (!this.network) return
const [promise, done] = prom()
// Stop sharing mounts.
let mounts = await this.getMounts()
mounts.forEach(mount => {
mount.stopShare()
})
// Close own network.
this.network.close(() => {
this.emit('network:close')
done()
})
return promise
}
Archive.prototype.authorizeWriter = function (key) {
const self = this
const db = this.db
return new Promise((resolve, reject) => {
key = Buffer.from(key, 'hex')
db.authorized(key, (err, auth) => {
if (err) return reject(err)
if (auth === true) {
resolve(true)
}
db.authorize(key, async (err, res) => {
if (err) return reject(err)
if (res) {
// Hack: Do a write after the auth is complete.
// Without this, hyperdrive breaks when loading the stat
// for the root folder (/). I think this is a bug in hyperdb.
await self.setInfo({})
resolve(true)
}
})
})
})
}