-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
244 lines (209 loc) · 7.61 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
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
import Protomux from 'protomux'
import c from 'compact-encoding'
import b4a from 'b4a'
import Hypercore from 'hypercore'
import { difference } from './utils/set-operations.js'
import { EventEmitter } from 'events'
const DEBUG = false
export class AutobaseManager extends EventEmitter {
constructor (base, allow, get, storage, opts = {}) {
super()
this.base = base
this.allow = allow
this.get = get
this.storage = Hypercore.defaultStorage(storage)
this.id = opts.id || b4a.from('main')
// Ensure id is a buffer for protomux
if (typeof this.id === 'string') {
this.id = b4a.from(this.id)
}
this._inputKeys = new Set()
this._outputKeys = new Set()
this._streams = []
// Load storage
this._ready = this.base.ready().then(() => {
const coresToLoad = []
// Load local cores first
if (this.base.localInput) {
coresToLoad.push(this._addKeys([b4a.toString(this.base.localInput.key, 'hex')], 'input'))
}
if (this.base.localOutput) {
coresToLoad.push(this._addKeys([b4a.toString(this.base.localOutput.key, 'hex')], 'output'))
}
// Load storage cores
coresToLoad.push(this.readStorageKeys())
return Promise.all(coresToLoad)
})
.then(() => Promise.all([
this._addKeys(this._inputKeys, 'input'),
this._addKeys(this._outputKeys, 'output')
]))
}
ready () {
return this._ready
}
attachStream (stream) {
const self = this
const mux = Protomux.from(stream)
const channel = mux.createChannel({
protocol: 'autobase-manager',
id: this.id
})
if (channel === null) {
throw Error('Attempted to attach to a stream with either duplicate or already closed channel. Maybe select a different `id`?')
}
channel.open()
const inputAnnouncer = channel.addMessage({
encoding: c.array(c.string),
async onmessage (msgs, session) {
const allowedKeysComputed = await Promise.all(msgs.map((msg) => self.allow(msg, 'input', session)))
const allowedKeys = msgs.filter((msg, i) => allowedKeysComputed[i])
if (allowedKeys.length) {
DEBUG && console.log('[' +
(self.base.localOutput ? b4a.toString(self.base.localOutput.key, 'hex').slice(-6) : 'N/A') +
'] inputs allowedKeys ', allowedKeys.map((key) => key.slice(-6)))
// Check if any are new
const newKeys = difference(allowedKeys, self._inputKeys)
if (newKeys.size > 0) {
DEBUG && console.log('[' +
(self.base.localOutput ? b4a.toString(self.base.localOutput.key, 'hex').slice(-6) : 'N/A') +
'] new inputs', [...newKeys].map((key) => key.slice(-6)))
await self._addKeys(newKeys, 'input')
await self.updateStorageKeys()
}
}
}
})
const outputAnnouncer = channel.addMessage({
encoding: c.array(c.string),
async onmessage (msgs, session) {
const allowedKeysComputed = await Promise.all(msgs.map((msg) => self.allow(msg, 'output', session)))
const allowedKeys = msgs.filter((msg, i) => allowedKeysComputed[i])
if (allowedKeys.length) {
DEBUG && console.log('[' +
(self.base.localOutput ? b4a.toString(self.base.localOutput.key, 'hex').slice(-6) : 'N/A') +
'] outputs allowedKeys ', allowedKeys.map((key) => key.slice(-6)))
// Check if any are new
const newKeys = difference(allowedKeys, self._outputKeys)
if (newKeys.size > 0) {
DEBUG && console.log('[' +
(self.base.localOutput ? b4a.toString(self.base.localOutput.key, 'hex').slice(-6) : 'N/A') +
'] new outputs ', [...newKeys].map((key) => key.slice(-6)))
await self._addKeys(newKeys, 'output')
await self.updateStorageKeys()
}
}
}
})
const streamRecord = { stream, inputAnnouncer, outputAnnouncer }
this._streams.push(streamRecord)
stream.once('close', () => {
this._streams.splice(this._streams.indexOf(streamRecord), 1)
})
if (this.base.localInput || this.base.inputs || this.base.outputs || this.base.localOutput) this.announce(streamRecord)
}
async announce (stream) {
await this.ready()
const keys = this.base.inputs.map((core) => b4a.toString(core.key, 'hex'))
if (keys.length) {
DEBUG && console.log('[' +
(this.base.localOutput ? b4a.toString(this.base.localOutput.key, 'hex').slice(-6) : 'N/A') +
'] announce keys', keys.map((key) => key.slice(-6)))
stream.inputAnnouncer.send(keys)
}
const outputKeys = this.base.outputs.map((core) => b4a.toString(core.key, 'hex'))
if (outputKeys.length) {
DEBUG && console.log('[' +
(this.base.localOutput ? b4a.toString(this.base.localOutput.key, 'hex').slice(-6) : 'N/A') +
'] announce outputKeys', outputKeys.map((key) => key.slice(-6)))
stream.outputAnnouncer.send(outputKeys)
}
}
async announceAll () {
for (const stream of this._streams) {
await this.announce(stream)
}
}
async _addKeys (keys, destination) {
// Get & Ready Cores
const cores = await Promise.all(Array.from(keys).map(async (key) => {
const core = this.get(b4a.from(key, 'hex'))
// Necessary for autobase id (aka the core's id) setup
await core.ready()
return core
}))
// Add to the corresponding place in autobase
for (const core of cores) {
if (destination === 'output') {
this._outputKeys.add(b4a.toString(core.key, 'hex'))
// Skip local output lest we get a 'Batch is out-of-date' error
if (this.base.localOutput && this.base.localOutput.key === core.key) {
DEBUG && console.log('found local output, continuing')
continue
}
// Update output to ensure up to date before adding
// Get a 'Batch is out-of-date.' error otherwise
if (this.base.started) await this.base.view.update()
await this.base.addOutput(core)
} else {
this._inputKeys.add(b4a.toString(core.key, 'hex'))
await this.base.addInput(core)
}
this.emit('core-added', core, destination)
}
}
_getStorage (file) {
const MANAGER_DIR = ['autobase-manager', this.id.toString(), file].join('/')
return this.storage(MANAGER_DIR)
}
readStorageKeys () {
return Promise.all([
this._readStorageKey('inputs', this._inputKeys),
this._readStorageKey('outputs', this._outputKeys)
])
}
_readStorageKey (file, output) {
const store = this._getStorage(file)
return new Promise((resolve, reject) => {
store.stat(async (err, stat) => {
if (err) {
resolve()
return
}
const len = stat.size
for (let start = 0; start < len; start += 32) {
await new Promise((resolve, reject) => {
store.read(start, 32, function (err, buf) {
if (err) throw err
output.add(buf.toString('hex'))
resolve()
})
})
}
store.close()
resolve()
})
}
)
}
async updateStorageKeys () {
await this._updateStorageKey('inputs', this._inputKeys)
await this._updateStorageKey('outputs', this._outputKeys)
await this.announceAll()
}
async _updateStorageKey (file, input) {
const store = this._getStorage(file)
let i = 0
for (const data of input) {
const start = i * 32
await new Promise((resolve, reject) => {
store.write(start, b4a.from(data, 'hex'), (err) => {
if (err) return reject(err)
resolve()
})
})
i++
}
store.close()
}
}