-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
215 lines (201 loc) · 6.81 KB
/
Copy pathindex.js
File metadata and controls
215 lines (201 loc) · 6.81 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
import Database from './lib/database.js'
import Corestore from 'corestore'
import b4a from 'b4a'
import Hyperswarm from 'hyperswarm'
import DocumentFactory from './lib/objects/index.js';
function logError(msg){
console.error(`** ERROR: ${msg} **`)
}
function logInfo(msg){
console.info(`** INFO: ${msg} **`)
}
class Mercury {
/**
* Main class for managing peer-to-peer network operations
* @param {Corestore} store - Corestore instance for persistent storage
*/
constructor(store){
this.store = store
this.db = new Database(store)
this.network = null
}
/**
* Destroy the instance and clean up resources
* @returns {Promise} - Promise that resolves when cleanup is complete
*/
async destroy(){
await this.db.close()
await this.network.destroy()
console.log('Instance has been destroyed')
}
/**
* Initialize the network and database
* @returns {Promise} - Promise that resolves when initialization is complete
*/
async initialize(){
try {
await this.store.ready()
await this.db.init()
const seed = await this.db.getSeed()
this.network = new Hyperswarm({seed:b4a.from(seed,'hex')})
const discovery = this.network.join(this.db.discoveryKey,{client:true,server:true})
discovery.flushed().then(()=>{
this.joinAllKnownRepositories()
})
} catch (err) {
logError(String(err))
throw err
}
}
/**
* Listens for incoming peer connections and handles events.
* @param {Function} onData - Callback when data is received from a peer.
* Takes (peer, data) as arguments.
* @param {Function} onError - Callback when an error occurs with a peer.
* Takes (peer, error) as arguments.
* @param {Function} onConnection - Callback when a peer connects.
* Takes (peer) as argument.
* @returns {void}
*
* @example
* obj.listen(
* (peer, data) => console.log(`Received: ${data}`),
* (peer, err) => console.error(`Error: ${err}`),
* (peer) => console.log(`Peer connected: ${peer.remotePublicKey}`))
*/
listen(onData=null,onError=null,onConnection=null){
console.log(`** Ready for connection **`)
this.network.on('connection',(peer)=>{
console.log(`*** Peer connected ${b4a.toString(peer.remotePublicKey, 'hex')} ***`)
if (onConnection) {
onConnection(peer)
}
this.db.replicate(peer)
peer.on('data',(data)=>{
if (onData) {
onData(peer,data)
}
})
peer.on('error',(err)=>{
if (onError) {
onError(peer,err)
}
console.error(`** Peer has beed disconected ${b4a.toString(peer.remotePublicKey, 'hex')}`)
})
})
}
/**
* Encode repository information into a base64 string
* @returns {string} - Base64 encoded repository information
*/
encodeRepository(){
const topic = b4a.toString(this.db.discoveryKey, 'hex')
const writerKey = b4a.toString(this.network.keyPair.publicKey, 'hex')
const baseKey = b4a.toString(this.db.key, 'hex')
const combined = b4a.from(`${topic}:${baseKey}:${writerKey}`)
return b4a.toString(combined, 'base64')
}
/**
* Decode a base64 encoded repository string
* @param {string} encodedRepo - Base64 encoded repository string
* @returns {Object} - Decoded repository information
* @throws {Error} - If the repository string is invalid
*/
decodeRepository(encodedRepo) {
try {
const decodedBuffer = b4a.from(encodedRepo, 'base64')
const decodedString = b4a.toString(decodedBuffer, 'utf-8')
const [topic, writer, peer] = decodedString.split(':')
if (!topic || !writer || !peer) throw new Error('Incomplete repository info')
return { topic, writer, peer }
} catch (err) {
throw new Error('Invalid repository string format')
}
}
/**
* Join a remote repository
* @param {string} encodedRepo - Base64 encoded repository string
* @param {string} name - Optional display name for the repository
* @returns {string} - Confirmation message
* @throws {Error} - If the repository join fails
*/
async joinRemoteRepository(encodedRepo, name) {
try {
const { topic, writer, peer } = this.decodeRepository(encodedRepo)
const id = peer.slice(0, 8)
const displayName = name || id
const topicBuffer = b4a.from(topic, 'hex')
const discovery = this.network.join(topicBuffer)
await discovery.flushed()
await this.db.appendRepository(id, topic, writer, peer, displayName)
console.log(`** Repository ${displayName} has been added **`)
console.log(
`
** Repository ${displayName} has been added
** Connected to ${topic} topic
`
)
return `Connected to ${displayName}'s repository`
} catch (err) {
console.error('Failed to join remote repository:', err)
throw err
}
}
/**
* Remove a repository
* @param {string} repoId - Repository ID
* @returns {Promise} - Promise that resolves when removal is complete
* @throws {Error} - If the repository removal fails
*/
async removeRepository(repoId) {
try {
const repo = await this.db.getChannel(repoId)
await this.db.removeChannel(repoId)
await this.network.leave(b4a.from(repo.topic, 'hex'))
console.log(`Repository ${repo.name} removed`)
} catch (err) {
console.error('Failed to remove repository:', err)
throw err
}
}
/**
* Join all known repositories
* @returns {Promise} - Promise that resolves when all repositories are joined
* @throws {Error} - If joining repositories fails
*/
async joinAllKnownRepositories() {
try {
const repos = await this.db.getAllRepositories()
for (const repo of repos) {
await this.db.createWriterCore(repo.writer)
const topicBuffer = b4a.from(repo.topic, 'hex')
const discovery = this.network.join(topicBuffer)
await discovery.flushed()
console.log(`** Joined repository: ${repo.name} | ${repo.topic} **`)
}
} catch (err) {
console.error('Error while joining known repositories:', err)
throw err
}
}
/**
* Creates a document instance based on the specified type using the database.
* @param {string} type - The type of document to create (e.g., "BASE", "NOTE").
* @param {Object} [metadata] - Optional metadata to set on the document.
* @returns {BaseObject} - The created document instance.
* @throws {Error} - If the document type is invalid or unsupported.
* @see DocumentFactory - The factory used to create the document.
*/
createDocument(type,metadata=null){
try {
const doc = DocumentFactory.createDocument(type,this.db);
if (metadata) {
doc.fromJson(metadata)
}
return doc
} catch (err) {
throw Error(String(err))
}
}
}
export default Mercury