-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
173 lines (161 loc) · 6.33 KB
/
index.mjs
File metadata and controls
173 lines (161 loc) · 6.33 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
import { HttpAPI } from './src/http-api.mjs'
import { StructureAPI } from './src/structure-api.mjs'
import { TimelineAPI } from './src/timeline-api.mjs'
import { CommandAPI } from './src/command-api.mjs'
import { ProjectList } from './src/project-list.mjs'
import { Project } from './src/project.mjs'
import { discover, errors } from './src/discover-api.mjs'
import { setLogger, LEVELS, consoleLogger, noopLogger } from './src/logger.mjs'
import { chill } from './src/convenience.mjs'
import { CryptoManager, TrustRequirement, VerificationMethod, VerificationRequestPhase } from './src/crypto.mjs'
import { CryptoFacade } from './src/crypto-facade.mjs'
import { RoomMemberCache } from './src/room-members.mjs'
/*
connect() resolves if the home_server can be connected. It does
not fail but tries to connect endlessly
*/
const connect = (home_server_url) => async (controller) => {
const MAX_CHILL_FACTOR = 64
let chillFactor = 0
let connected = false
while (!connected || controller?.signal?.aborted) {
await chill(chillFactor, controller?.signal)
try {
await discover({ home_server_url })
connected = true
} catch (error) {
if (error.name === 'AbortError') throw error
if (error.code === errors.FAIL_PROMPT) {
connected = true
continue
}
if (chillFactor < MAX_CHILL_FACTOR) chillFactor++
}
}
}
/**
* @typedef {Object} LoginData
* @property {String} user_id
* @property {String} password
* @property {String} home_server_url
* @property {Object} [encryption] - Optional encryption configuration
* @property {boolean} [encryption.enabled=false] - Enable E2EE
* @property {string} [encryption.storeName] - IndexedDB store name for persistent crypto state (e.g. 'crypto-<projectUUID>')
* @property {string} [encryption.passphrase] - Passphrase to encrypt the IndexedDB store
* @property {Object} db - A levelup-compatible database instance for the persistent command queue
*
* @param {LoginData} loginData
* @returns {Object} matrixClient
*/
const MatrixClient = (loginData) => {
const encryption = loginData.encryption || null
// Shared CryptoFacade instance – initialized once, reused across projectList/project calls
let sharedFacade = null
let cryptoInitialized = false
/**
* Get or create the shared CryptoFacade.
* If encryption.storeName is provided, uses IndexedDB-backed persistent store.
* Otherwise, uses in-memory store (keys lost on restart).
* @param {HttpAPI} httpAPI
* @returns {Promise<CryptoFacade|null>}
*/
const getCrypto = async (httpAPI) => {
if (!encryption?.enabled) return null
if (sharedFacade) {
// Reuse existing facade, just process any pending outgoing requests
if (!cryptoInitialized) {
await sharedFacade.processOutgoingRequests()
cryptoInitialized = true
}
return sharedFacade
}
const credentials = httpAPI.credentials
if (!credentials.device_id) {
throw new Error('E2EE requires a device_id in credentials. Ensure a fresh login (delete .state.json if reusing saved credentials).')
}
const cryptoManager = new CryptoManager()
if (encryption.storeName) {
// Persistent store: crypto state survives restarts (requires IndexedDB, i.e. Electron/browser)
await cryptoManager.initializeWithStore(
credentials.user_id,
credentials.device_id,
encryption.storeName,
encryption.passphrase
)
} else {
// In-memory: keys are lost on restart (for testing or non-browser environments)
await cryptoManager.initialize(credentials.user_id, credentials.device_id)
}
sharedFacade = new CryptoFacade(cryptoManager, httpAPI)
await sharedFacade.processOutgoingRequests()
cryptoInitialized = true
return sharedFacade
}
return {
connect: connect(loginData.home_server_url),
projectList: async mostRecentCredentials => {
const credentials = mostRecentCredentials ? mostRecentCredentials : (await HttpAPI.loginWithPassword(loginData))
const httpAPI = new HttpAPI(credentials)
const facade = await getCrypto(httpAPI)
const projectListParames = {
structureAPI: new StructureAPI(httpAPI),
timelineAPI: new TimelineAPI(httpAPI, facade ? {
onSyncResponse: (data) => facade.processSyncResponse(data),
decryptEvent: (event, roomId) => facade.decryptEvent(event, roomId)
} : {})
}
const projectList = new ProjectList(projectListParames)
projectList.tokenRefreshed = handler => httpAPI.tokenRefreshed(handler)
projectList.credentials = () => (httpAPI.credentials)
return projectList
},
project: async mostRecentCredentials => {
const credentials = mostRecentCredentials ? mostRecentCredentials : (await HttpAPI.loginWithPassword(loginData))
const httpAPI = new HttpAPI(credentials)
const facade = await getCrypto(httpAPI)
const memberCache = new RoomMemberCache(async (roomId) => {
const members = await httpAPI.members(roomId)
return (members.chunk || [])
.filter(e => e.content?.membership === 'join')
.map(e => e.state_key)
.filter(Boolean)
})
const projectParams = {
structureAPI: new StructureAPI(httpAPI),
timelineAPI: new TimelineAPI(httpAPI, facade ? {
onSyncResponse: (data) => facade.processSyncResponse(data),
decryptEvent: (event, roomId) => facade.decryptEvent(event, roomId)
} : {}),
commandAPI: new CommandAPI(httpAPI, memberCache, {
encryptEvent: facade
? (roomId, type, content, memberIds) => facade.encryptEvent(roomId, type, content, memberIds)
: null,
db: loginData.db
}),
memberCache,
crypto: facade ? {
isEnabled: true,
registerRoom: (roomId, enc) => facade.registerRoom(roomId, enc),
shareHistoricalKeys: (roomId, userIds) => facade.shareHistoricalKeys(roomId, userIds)
} : { isEnabled: false }
}
const project = new Project(projectParams)
project.tokenRefreshed = handler => httpAPI.tokenRefreshed(handler)
project.credentials = () => (httpAPI.credentials)
return project
}
}
}
export {
MatrixClient,
CryptoManager,
TrustRequirement,
VerificationMethod,
VerificationRequestPhase,
connect,
discover,
setLogger,
LEVELS,
consoleLogger,
noopLogger
}