@kodelyth/matrix 2026.5.42 → 2026.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/klaw.plugin.json +891 -3
  2. package/package.json +16 -4
  3. package/CHANGELOG.md +0 -321
  4. package/SPEC-SUPPORT.md +0 -116
  5. package/api.ts +0 -38
  6. package/auth-presence.ts +0 -56
  7. package/channel-plugin-api.ts +0 -3
  8. package/cli-metadata.ts +0 -11
  9. package/contract-api.ts +0 -17
  10. package/doctor-contract-api.ts +0 -1
  11. package/helper-api.ts +0 -3
  12. package/index.ts +0 -55
  13. package/plugin-entry.handlers.runtime.ts +0 -1
  14. package/runtime-api.ts +0 -72
  15. package/runtime-heavy-api.ts +0 -1
  16. package/runtime-setter-api.ts +0 -3
  17. package/secret-contract-api.ts +0 -5
  18. package/setup-entry.ts +0 -17
  19. package/setup-plugin-api.ts +0 -3
  20. package/src/account-selection.ts +0 -223
  21. package/src/actions.ts +0 -346
  22. package/src/approval-auth.ts +0 -25
  23. package/src/approval-handler.runtime.ts +0 -592
  24. package/src/approval-ids.ts +0 -6
  25. package/src/approval-native.ts +0 -345
  26. package/src/approval-reaction-auth.ts +0 -45
  27. package/src/approval-reactions.ts +0 -313
  28. package/src/auth-precedence.ts +0 -61
  29. package/src/channel-account-paths.ts +0 -97
  30. package/src/channel.runtime.ts +0 -17
  31. package/src/channel.setup.ts +0 -48
  32. package/src/channel.ts +0 -667
  33. package/src/cli-metadata.ts +0 -19
  34. package/src/cli.ts +0 -2298
  35. package/src/config-adapter.ts +0 -41
  36. package/src/config-schema.ts +0 -159
  37. package/src/config-ui-hints.ts +0 -56
  38. package/src/directory-live.ts +0 -238
  39. package/src/doctor-contract.ts +0 -287
  40. package/src/doctor.ts +0 -262
  41. package/src/env-vars.ts +0 -92
  42. package/src/exec-approval-resolver.ts +0 -23
  43. package/src/exec-approvals.ts +0 -287
  44. package/src/group-mentions.ts +0 -41
  45. package/src/legacy-crypto-inspector-availability.ts +0 -60
  46. package/src/legacy-crypto.ts +0 -531
  47. package/src/legacy-state.ts +0 -156
  48. package/src/matrix/account-config.ts +0 -175
  49. package/src/matrix/accounts.ts +0 -194
  50. package/src/matrix/actions/client.ts +0 -31
  51. package/src/matrix/actions/devices.ts +0 -34
  52. package/src/matrix/actions/limits.ts +0 -6
  53. package/src/matrix/actions/messages.ts +0 -129
  54. package/src/matrix/actions/pins.ts +0 -63
  55. package/src/matrix/actions/polls.ts +0 -109
  56. package/src/matrix/actions/profile.ts +0 -37
  57. package/src/matrix/actions/reactions.ts +0 -59
  58. package/src/matrix/actions/room.ts +0 -71
  59. package/src/matrix/actions/summary.ts +0 -88
  60. package/src/matrix/actions/types.ts +0 -63
  61. package/src/matrix/actions/verification.ts +0 -589
  62. package/src/matrix/actions.ts +0 -37
  63. package/src/matrix/active-client.ts +0 -26
  64. package/src/matrix/async-lock.ts +0 -18
  65. package/src/matrix/backup-health.ts +0 -124
  66. package/src/matrix/client/config-runtime-api.ts +0 -9
  67. package/src/matrix/client/config-secret-input.runtime.ts +0 -1
  68. package/src/matrix/client/config.ts +0 -853
  69. package/src/matrix/client/create-client.ts +0 -105
  70. package/src/matrix/client/env-auth.ts +0 -95
  71. package/src/matrix/client/file-sync-store.ts +0 -289
  72. package/src/matrix/client/logging.ts +0 -140
  73. package/src/matrix/client/migration-snapshot.runtime.ts +0 -1
  74. package/src/matrix/client/private-network-host.ts +0 -1
  75. package/src/matrix/client/runtime.ts +0 -4
  76. package/src/matrix/client/shared.ts +0 -316
  77. package/src/matrix/client/storage.ts +0 -543
  78. package/src/matrix/client/types.ts +0 -50
  79. package/src/matrix/client/url-validation.ts +0 -73
  80. package/src/matrix/client-bootstrap.ts +0 -173
  81. package/src/matrix/client.ts +0 -23
  82. package/src/matrix/config-paths.ts +0 -31
  83. package/src/matrix/config-update.ts +0 -292
  84. package/src/matrix/credentials-read.ts +0 -208
  85. package/src/matrix/credentials-write.runtime.ts +0 -35
  86. package/src/matrix/credentials.ts +0 -95
  87. package/src/matrix/deps.ts +0 -309
  88. package/src/matrix/device-health.ts +0 -29
  89. package/src/matrix/direct-management.ts +0 -349
  90. package/src/matrix/direct-room.ts +0 -128
  91. package/src/matrix/draft-stream.ts +0 -225
  92. package/src/matrix/encryption-guidance.ts +0 -24
  93. package/src/matrix/errors.ts +0 -21
  94. package/src/matrix/format.ts +0 -426
  95. package/src/matrix/legacy-crypto-inspector.ts +0 -95
  96. package/src/matrix/media-errors.ts +0 -20
  97. package/src/matrix/media-text.ts +0 -162
  98. package/src/matrix/monitor/access-state.ts +0 -145
  99. package/src/matrix/monitor/ack-config.ts +0 -27
  100. package/src/matrix/monitor/allowlist.ts +0 -89
  101. package/src/matrix/monitor/auto-join.ts +0 -86
  102. package/src/matrix/monitor/config.ts +0 -569
  103. package/src/matrix/monitor/context-summary.ts +0 -43
  104. package/src/matrix/monitor/direct.ts +0 -296
  105. package/src/matrix/monitor/events.ts +0 -397
  106. package/src/matrix/monitor/handler.ts +0 -2266
  107. package/src/matrix/monitor/inbound-dedupe.ts +0 -267
  108. package/src/matrix/monitor/index.ts +0 -540
  109. package/src/matrix/monitor/legacy-crypto-restore.ts +0 -139
  110. package/src/matrix/monitor/location.ts +0 -108
  111. package/src/matrix/monitor/media.ts +0 -119
  112. package/src/matrix/monitor/mentions.ts +0 -256
  113. package/src/matrix/monitor/reaction-events.ts +0 -197
  114. package/src/matrix/monitor/recent-invite.ts +0 -30
  115. package/src/matrix/monitor/replies.ts +0 -136
  116. package/src/matrix/monitor/reply-context.ts +0 -92
  117. package/src/matrix/monitor/room-history.ts +0 -301
  118. package/src/matrix/monitor/room-info.ts +0 -126
  119. package/src/matrix/monitor/rooms.ts +0 -52
  120. package/src/matrix/monitor/route.ts +0 -179
  121. package/src/matrix/monitor/runtime-api.ts +0 -28
  122. package/src/matrix/monitor/startup-verification.ts +0 -237
  123. package/src/matrix/monitor/startup.ts +0 -218
  124. package/src/matrix/monitor/status.ts +0 -120
  125. package/src/matrix/monitor/sync-lifecycle.ts +0 -91
  126. package/src/matrix/monitor/task-runner.ts +0 -38
  127. package/src/matrix/monitor/test-events.ts +0 -21
  128. package/src/matrix/monitor/thread-context.ts +0 -108
  129. package/src/matrix/monitor/threads.ts +0 -85
  130. package/src/matrix/monitor/types.ts +0 -30
  131. package/src/matrix/monitor/verification-events.ts +0 -643
  132. package/src/matrix/monitor/verification-utils.ts +0 -46
  133. package/src/matrix/outbound-media-runtime.ts +0 -1
  134. package/src/matrix/poll-summary.ts +0 -110
  135. package/src/matrix/poll-types.ts +0 -429
  136. package/src/matrix/probe.runtime.ts +0 -4
  137. package/src/matrix/probe.ts +0 -97
  138. package/src/matrix/profile.ts +0 -184
  139. package/src/matrix/reaction-common.ts +0 -147
  140. package/src/matrix/sdk/crypto-bootstrap.ts +0 -438
  141. package/src/matrix/sdk/crypto-facade.ts +0 -242
  142. package/src/matrix/sdk/crypto-node.runtime.ts +0 -17
  143. package/src/matrix/sdk/crypto-runtime.ts +0 -14
  144. package/src/matrix/sdk/decrypt-bridge.ts +0 -410
  145. package/src/matrix/sdk/event-helpers.ts +0 -83
  146. package/src/matrix/sdk/http-client.ts +0 -87
  147. package/src/matrix/sdk/idb-persistence-lock.ts +0 -51
  148. package/src/matrix/sdk/idb-persistence.ts +0 -288
  149. package/src/matrix/sdk/logger.ts +0 -108
  150. package/src/matrix/sdk/read-response-with-limit.ts +0 -19
  151. package/src/matrix/sdk/recovery-key-store.ts +0 -453
  152. package/src/matrix/sdk/timeout-abort-signal.ts +0 -1
  153. package/src/matrix/sdk/transport-runtime-api.ts +0 -18
  154. package/src/matrix/sdk/transport.ts +0 -352
  155. package/src/matrix/sdk/types.ts +0 -245
  156. package/src/matrix/sdk/verification-manager.ts +0 -795
  157. package/src/matrix/sdk/verification-status.ts +0 -23
  158. package/src/matrix/sdk.ts +0 -2152
  159. package/src/matrix/send/client.ts +0 -93
  160. package/src/matrix/send/formatting.ts +0 -189
  161. package/src/matrix/send/media.ts +0 -244
  162. package/src/matrix/send/targets.ts +0 -104
  163. package/src/matrix/send/types.ts +0 -131
  164. package/src/matrix/send.ts +0 -660
  165. package/src/matrix/session-store-metadata.ts +0 -108
  166. package/src/matrix/startup-abort.ts +0 -44
  167. package/src/matrix/subagent-hooks.ts +0 -308
  168. package/src/matrix/sync-state.ts +0 -27
  169. package/src/matrix/target-ids.ts +0 -79
  170. package/src/matrix/thread-bindings-shared.ts +0 -206
  171. package/src/matrix/thread-bindings.ts +0 -580
  172. package/src/matrix-migration.runtime.ts +0 -9
  173. package/src/migration-config.ts +0 -243
  174. package/src/migration-snapshot-backup.ts +0 -116
  175. package/src/migration-snapshot.ts +0 -53
  176. package/src/onboarding.ts +0 -775
  177. package/src/outbound.ts +0 -248
  178. package/src/plugin-entry.runtime.js +0 -115
  179. package/src/plugin-entry.runtime.ts +0 -70
  180. package/src/profile-update.ts +0 -71
  181. package/src/record-shared.ts +0 -3
  182. package/src/resolve-targets.ts +0 -175
  183. package/src/resolver.runtime.ts +0 -5
  184. package/src/resolver.ts +0 -21
  185. package/src/runtime-api.ts +0 -106
  186. package/src/runtime.ts +0 -13
  187. package/src/secret-contract.ts +0 -174
  188. package/src/session-route.ts +0 -126
  189. package/src/setup-bootstrap.ts +0 -102
  190. package/src/setup-config.ts +0 -222
  191. package/src/setup-contract.ts +0 -90
  192. package/src/setup-core.ts +0 -146
  193. package/src/setup-dm-policy.ts +0 -15
  194. package/src/setup-surface.ts +0 -4
  195. package/src/startup-maintenance.ts +0 -114
  196. package/src/storage-paths.ts +0 -92
  197. package/src/thread-binding-api.ts +0 -23
  198. package/src/tool-actions.runtime.ts +0 -1
  199. package/src/tool-actions.ts +0 -498
  200. package/src/types.ts +0 -257
  201. package/subagent-hooks-api.ts +0 -31
  202. package/test-api.ts +0 -21
  203. package/thread-binding-api.ts +0 -4
  204. package/thread-bindings-runtime.ts +0 -4
  205. package/tsconfig.json +0 -16
@@ -1,14 +0,0 @@
1
- import "fake-indexeddb/auto";
2
-
3
- export { MatrixCryptoBootstrapper } from "./crypto-bootstrap.js";
4
- export type { MatrixCryptoBootstrapResult } from "./crypto-bootstrap.js";
5
- export { createMatrixCryptoFacade } from "./crypto-facade.js";
6
- export type { MatrixCryptoFacade } from "./crypto-facade.js";
7
- export { MatrixDecryptBridge } from "./decrypt-bridge.js";
8
- export { persistIdbToDisk, restoreIdbFromDisk } from "./idb-persistence.js";
9
- export { MatrixVerificationManager } from "./verification-manager.js";
10
- export type { MatrixVerificationSummary } from "./verification-manager.js";
11
- export {
12
- isMatrixDeviceOwnerVerified,
13
- isMatrixDeviceVerifiedInCurrentClient,
14
- } from "./verification-status.js";
@@ -1,410 +0,0 @@
1
- import { CryptoEvent } from "matrix-js-sdk/lib/crypto-api/CryptoEvent.js";
2
- import { DecryptionFailureCode } from "matrix-js-sdk/lib/crypto-api/index.js";
3
- import { MatrixEventEvent, type MatrixEvent } from "matrix-js-sdk/lib/matrix.js";
4
- import { LogService, noop } from "./logger.js";
5
-
6
- type MatrixDecryptIfNeededClient = {
7
- decryptEventIfNeeded?: (
8
- event: MatrixEvent,
9
- opts?: {
10
- isRetry?: boolean;
11
- },
12
- ) => Promise<void>;
13
- };
14
-
15
- type MatrixDecryptRetryState = {
16
- event: MatrixEvent;
17
- roomId: string;
18
- eventId: string;
19
- attempts: number;
20
- inFlight: boolean;
21
- timer: ReturnType<typeof setTimeout> | null;
22
- };
23
-
24
- type DecryptBridgeRawEvent = {
25
- event_id: string;
26
- };
27
-
28
- type MatrixCryptoRetrySignalSource = {
29
- on: (eventName: string, listener: (...args: unknown[]) => void) => void;
30
- };
31
-
32
- const MATRIX_DECRYPT_RETRY_BASE_DELAY_MS = 1_500;
33
- const MATRIX_DECRYPT_RETRY_MAX_DELAY_MS = 30_000;
34
- const MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS = 8;
35
-
36
- function resolveDecryptRetryKey(roomId: string, eventId: string): string | null {
37
- if (!roomId || !eventId) {
38
- return null;
39
- }
40
- return `${roomId}|${eventId}`;
41
- }
42
-
43
- function isDecryptionFailure(event: MatrixEvent): boolean {
44
- return (
45
- typeof (event as { isDecryptionFailure?: () => boolean }).isDecryptionFailure === "function" &&
46
- (event as { isDecryptionFailure: () => boolean }).isDecryptionFailure()
47
- );
48
- }
49
-
50
- function getDecryptionFailureReason(event: MatrixEvent): DecryptionFailureCode | null {
51
- const reason = (event as { decryptionFailureReason?: unknown }).decryptionFailureReason;
52
- return typeof reason === "string" && reason in DecryptionFailureCode
53
- ? (reason as DecryptionFailureCode)
54
- : null;
55
- }
56
-
57
- function shouldRetryDecryptionFailure(event: MatrixEvent): boolean {
58
- if (!isDecryptionFailure(event)) {
59
- return false;
60
- }
61
- const reason = getDecryptionFailureReason(event);
62
- if (!reason) {
63
- return true;
64
- }
65
- return (
66
- reason === DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID ||
67
- reason === DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX ||
68
- reason === DecryptionFailureCode.UNKNOWN_ERROR
69
- );
70
- }
71
-
72
- export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
73
- private readonly trackedEncryptedEvents = new WeakSet<object>();
74
- private readonly decryptedMessageDedupe = new Map<string, number>();
75
- private readonly decryptRetries = new Map<string, MatrixDecryptRetryState>();
76
- private readonly failedDecryptionsNotified = new Set<string>();
77
- private readonly exhaustedDecryptRetries = new Set<string>();
78
- private activeRetryRuns = 0;
79
- private readonly retryIdleResolvers = new Set<() => void>();
80
- private cryptoRetrySignalsBound = false;
81
-
82
- constructor(
83
- private readonly deps: {
84
- client: MatrixDecryptIfNeededClient;
85
- toRaw: (event: MatrixEvent) => TRawEvent;
86
- emitDecryptedEvent: (roomId: string, event: TRawEvent) => void;
87
- emitMessage: (roomId: string, event: TRawEvent) => void;
88
- emitFailedDecryption: (roomId: string, event: TRawEvent, error: Error) => void;
89
- },
90
- ) {}
91
-
92
- shouldEmitUnencryptedMessage(roomId: string, eventId: string): boolean {
93
- if (!eventId) {
94
- return true;
95
- }
96
- const key = `${roomId}|${eventId}`;
97
- const createdAt = this.decryptedMessageDedupe.get(key);
98
- if (createdAt === undefined) {
99
- return true;
100
- }
101
- this.decryptedMessageDedupe.delete(key);
102
- return false;
103
- }
104
-
105
- attachEncryptedEvent(event: MatrixEvent, roomId: string): void {
106
- if (this.trackedEncryptedEvents.has(event)) {
107
- return;
108
- }
109
- this.trackedEncryptedEvents.add(event);
110
- event.on(MatrixEventEvent.Decrypted, (decryptedEvent: MatrixEvent, err?: Error) => {
111
- this.handleEncryptedEventDecrypted({
112
- roomId,
113
- encryptedEvent: event,
114
- decryptedEvent,
115
- err,
116
- });
117
- });
118
- if (shouldRetryDecryptionFailure(event)) {
119
- const raw = this.deps.toRaw(event);
120
- const eventId = raw.event_id || event.getId() || "";
121
- this.scheduleDecryptRetry({ event, roomId, eventId });
122
- }
123
- }
124
-
125
- retryPendingNow(reason: string): void {
126
- const pending = Array.from(this.decryptRetries.entries());
127
- if (pending.length === 0) {
128
- return;
129
- }
130
- LogService.debug("MatrixClientLite", `Retrying pending decryptions due to ${reason}`);
131
- for (const [retryKey, state] of pending) {
132
- if (state.timer) {
133
- clearTimeout(state.timer);
134
- state.timer = null;
135
- }
136
- if (state.inFlight) {
137
- continue;
138
- }
139
- this.runDecryptRetry(retryKey).catch(noop);
140
- }
141
- }
142
-
143
- bindCryptoRetrySignals(crypto: MatrixCryptoRetrySignalSource | undefined): void {
144
- if (!crypto || this.cryptoRetrySignalsBound) {
145
- return;
146
- }
147
- this.cryptoRetrySignalsBound = true;
148
-
149
- const trigger = (reason: string): void => {
150
- this.retryPendingNow(reason);
151
- };
152
-
153
- crypto.on(CryptoEvent.KeyBackupDecryptionKeyCached, () => {
154
- trigger("crypto.keyBackupDecryptionKeyCached");
155
- });
156
- crypto.on(CryptoEvent.RehydrationCompleted, () => {
157
- trigger("dehydration.RehydrationCompleted");
158
- });
159
- crypto.on(CryptoEvent.DevicesUpdated, () => {
160
- trigger("crypto.devicesUpdated");
161
- });
162
- crypto.on(CryptoEvent.KeysChanged, () => {
163
- trigger("crossSigning.keysChanged");
164
- });
165
- }
166
-
167
- stop(): void {
168
- for (const retryKey of this.decryptRetries.keys()) {
169
- this.clearDecryptRetry(retryKey);
170
- }
171
- }
172
-
173
- async drainPendingDecryptions(reason: string): Promise<void> {
174
- for (let attempts = 0; attempts < MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS; attempts += 1) {
175
- if (this.decryptRetries.size === 0) {
176
- return;
177
- }
178
- this.retryPendingNow(reason);
179
- await this.waitForActiveRetryRunsToFinish();
180
- const hasPendingRetryTimers = Array.from(this.decryptRetries.values()).some(
181
- (state) => state.timer || state.inFlight,
182
- );
183
- if (!hasPendingRetryTimers) {
184
- return;
185
- }
186
- }
187
- }
188
-
189
- private handleEncryptedEventDecrypted(params: {
190
- roomId: string;
191
- encryptedEvent: MatrixEvent;
192
- decryptedEvent: MatrixEvent;
193
- err?: Error;
194
- }): void {
195
- const decryptedRoomId = params.decryptedEvent.getRoomId() || params.roomId;
196
- const decryptedRaw = this.deps.toRaw(params.decryptedEvent);
197
- const retryEventId = decryptedRaw.event_id || params.encryptedEvent.getId() || "";
198
- const retryKey = resolveDecryptRetryKey(decryptedRoomId, retryEventId);
199
-
200
- if (params.err) {
201
- this.emitFailedDecryptionOnce(retryKey, decryptedRoomId, decryptedRaw, params.err);
202
- if (shouldRetryDecryptionFailure(params.decryptedEvent)) {
203
- this.scheduleDecryptRetry({
204
- event: params.encryptedEvent,
205
- roomId: decryptedRoomId,
206
- eventId: retryEventId,
207
- });
208
- } else if (retryKey) {
209
- this.clearDecryptRetry(retryKey);
210
- }
211
- return;
212
- }
213
-
214
- if (isDecryptionFailure(params.decryptedEvent)) {
215
- this.emitFailedDecryptionOnce(
216
- retryKey,
217
- decryptedRoomId,
218
- decryptedRaw,
219
- new Error("Matrix event failed to decrypt"),
220
- );
221
- if (shouldRetryDecryptionFailure(params.decryptedEvent)) {
222
- this.scheduleDecryptRetry({
223
- event: params.encryptedEvent,
224
- roomId: decryptedRoomId,
225
- eventId: retryEventId,
226
- });
227
- } else if (retryKey) {
228
- this.clearDecryptRetry(retryKey);
229
- }
230
- return;
231
- }
232
-
233
- if (retryKey) {
234
- this.clearDecryptRetry(retryKey);
235
- }
236
- this.rememberDecryptedMessage(decryptedRoomId, decryptedRaw.event_id);
237
- this.deps.emitDecryptedEvent(decryptedRoomId, decryptedRaw);
238
- this.deps.emitMessage(decryptedRoomId, decryptedRaw);
239
- }
240
-
241
- private emitFailedDecryptionOnce(
242
- retryKey: string | null,
243
- roomId: string,
244
- event: TRawEvent,
245
- error: Error,
246
- ): void {
247
- if (retryKey) {
248
- if (this.failedDecryptionsNotified.has(retryKey)) {
249
- return;
250
- }
251
- this.failedDecryptionsNotified.add(retryKey);
252
- }
253
- this.deps.emitFailedDecryption(roomId, event, error);
254
- }
255
-
256
- private scheduleDecryptRetry(params: {
257
- event: MatrixEvent;
258
- roomId: string;
259
- eventId: string;
260
- }): void {
261
- const retryKey = resolveDecryptRetryKey(params.roomId, params.eventId);
262
- if (!retryKey) {
263
- return;
264
- }
265
- const existing = this.decryptRetries.get(retryKey);
266
- if (this.exhaustedDecryptRetries.has(retryKey)) {
267
- return;
268
- }
269
- if (existing?.timer || existing?.inFlight) {
270
- return;
271
- }
272
- const attempts = (existing?.attempts ?? 0) + 1;
273
- if (attempts > MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS) {
274
- const retry = this.decryptRetries.get(retryKey);
275
- if (retry?.timer) {
276
- clearTimeout(retry.timer);
277
- }
278
- this.decryptRetries.delete(retryKey);
279
- this.exhaustedDecryptRetries.add(retryKey);
280
- LogService.debug(
281
- "MatrixClientLite",
282
- `Giving up decryption retry for ${params.eventId} in ${params.roomId} after ${attempts - 1} attempts`,
283
- );
284
- return;
285
- }
286
- const delayMs = Math.min(
287
- MATRIX_DECRYPT_RETRY_BASE_DELAY_MS * 2 ** (attempts - 1),
288
- MATRIX_DECRYPT_RETRY_MAX_DELAY_MS,
289
- );
290
- const next: MatrixDecryptRetryState = {
291
- event: params.event,
292
- roomId: params.roomId,
293
- eventId: params.eventId,
294
- attempts,
295
- inFlight: false,
296
- timer: null,
297
- };
298
- next.timer = setTimeout(() => {
299
- this.runDecryptRetry(retryKey).catch(noop);
300
- }, delayMs);
301
- this.decryptRetries.set(retryKey, next);
302
- }
303
-
304
- private async runDecryptRetry(retryKey: string): Promise<void> {
305
- const state = this.decryptRetries.get(retryKey);
306
- if (!state || state.inFlight) {
307
- return;
308
- }
309
-
310
- state.inFlight = true;
311
- state.timer = null;
312
- this.activeRetryRuns += 1;
313
- const canDecrypt = typeof this.deps.client.decryptEventIfNeeded === "function";
314
- if (!canDecrypt) {
315
- this.clearDecryptRetry(retryKey);
316
- this.activeRetryRuns = Math.max(0, this.activeRetryRuns - 1);
317
- this.resolveRetryIdleIfNeeded();
318
- return;
319
- }
320
-
321
- try {
322
- await this.deps.client.decryptEventIfNeeded?.(state.event, {
323
- isRetry: true,
324
- });
325
- } catch {
326
- // Retry with backoff until we hit the configured retry cap.
327
- } finally {
328
- state.inFlight = false;
329
- this.activeRetryRuns = Math.max(0, this.activeRetryRuns - 1);
330
- this.resolveRetryIdleIfNeeded();
331
- }
332
-
333
- if (this.decryptRetries.get(retryKey) !== state) {
334
- return;
335
- }
336
- if (isDecryptionFailure(state.event)) {
337
- if (!shouldRetryDecryptionFailure(state.event)) {
338
- this.clearDecryptRetry(retryKey);
339
- return;
340
- }
341
- this.scheduleDecryptRetry(state);
342
- return;
343
- }
344
-
345
- this.clearDecryptRetry(retryKey);
346
- const raw = this.deps.toRaw(state.event);
347
- this.rememberDecryptedMessage(state.roomId, raw.event_id);
348
- this.deps.emitDecryptedEvent(state.roomId, raw);
349
- this.deps.emitMessage(state.roomId, raw);
350
- }
351
-
352
- private clearDecryptRetry(retryKey: string): void {
353
- const state = this.decryptRetries.get(retryKey);
354
- if (state?.timer) {
355
- clearTimeout(state.timer);
356
- }
357
- this.decryptRetries.delete(retryKey);
358
- this.exhaustedDecryptRetries.delete(retryKey);
359
- this.failedDecryptionsNotified.delete(retryKey);
360
- }
361
-
362
- private rememberDecryptedMessage(roomId: string, eventId: string): void {
363
- if (!eventId) {
364
- return;
365
- }
366
- const now = Date.now();
367
- this.pruneDecryptedMessageDedupe(now);
368
- this.decryptedMessageDedupe.set(`${roomId}|${eventId}`, now);
369
- }
370
-
371
- private pruneDecryptedMessageDedupe(now: number): void {
372
- const ttlMs = 30_000;
373
- for (const [key, createdAt] of this.decryptedMessageDedupe) {
374
- if (now - createdAt > ttlMs) {
375
- this.decryptedMessageDedupe.delete(key);
376
- }
377
- }
378
- const maxEntries = 2048;
379
- while (this.decryptedMessageDedupe.size > maxEntries) {
380
- const oldest = this.decryptedMessageDedupe.keys().next().value;
381
- if (oldest === undefined) {
382
- break;
383
- }
384
- this.decryptedMessageDedupe.delete(oldest);
385
- }
386
- }
387
-
388
- private async waitForActiveRetryRunsToFinish(): Promise<void> {
389
- if (this.activeRetryRuns === 0) {
390
- return;
391
- }
392
- await new Promise<void>((resolve) => {
393
- this.retryIdleResolvers.add(resolve);
394
- if (this.activeRetryRuns === 0) {
395
- this.retryIdleResolvers.delete(resolve);
396
- resolve();
397
- }
398
- });
399
- }
400
-
401
- private resolveRetryIdleIfNeeded(): void {
402
- if (this.activeRetryRuns !== 0) {
403
- return;
404
- }
405
- for (const resolve of this.retryIdleResolvers) {
406
- resolve();
407
- }
408
- this.retryIdleResolvers.clear();
409
- }
410
- }
@@ -1,83 +0,0 @@
1
- import type { MatrixEvent } from "matrix-js-sdk/lib/matrix.js";
2
- import type { MatrixRawEvent } from "./types.js";
3
-
4
- type MatrixEventContentMode = "current" | "original";
5
-
6
- export function matrixEventToRaw(
7
- event: MatrixEvent,
8
- opts: { contentMode?: MatrixEventContentMode } = {},
9
- ): MatrixRawEvent {
10
- const unsigned = (event.getUnsigned?.() ?? {}) as {
11
- age?: number;
12
- redacted_because?: unknown;
13
- };
14
- const eventWithOriginalContent = event as {
15
- getOriginalContent?: () => Record<string, unknown>;
16
- };
17
- const content =
18
- opts.contentMode === "original"
19
- ? (eventWithOriginalContent.getOriginalContent?.() ?? event.getContent?.() ?? {})
20
- : (event.getContent?.() ?? eventWithOriginalContent.getOriginalContent?.() ?? {});
21
- const raw: MatrixRawEvent = {
22
- event_id: event.getId() ?? "",
23
- sender: event.getSender() ?? "",
24
- type: event.getType() ?? "",
25
- origin_server_ts: event.getTs() ?? 0,
26
- content: content || {},
27
- unsigned,
28
- };
29
- const stateKey = resolveMatrixStateKey(event);
30
- if (typeof stateKey === "string") {
31
- raw.state_key = stateKey;
32
- }
33
- return raw;
34
- }
35
-
36
- export function parseMxc(url: string): { server: string; mediaId: string } | null {
37
- const match = /^mxc:\/\/([^/]+)\/(.+)$/.exec(url.trim());
38
- if (!match) {
39
- return null;
40
- }
41
- return {
42
- server: match[1],
43
- mediaId: match[2],
44
- };
45
- }
46
-
47
- export function buildHttpError(
48
- statusCode: number,
49
- bodyText: string,
50
- ): Error & { statusCode: number } {
51
- let message = `Matrix HTTP ${statusCode}`;
52
- if (bodyText.trim()) {
53
- try {
54
- const parsed = JSON.parse(bodyText) as { error?: string };
55
- if (typeof parsed.error === "string" && parsed.error.trim()) {
56
- message = parsed.error.trim();
57
- } else {
58
- message = bodyText.slice(0, 500);
59
- }
60
- } catch {
61
- message = bodyText.slice(0, 500);
62
- }
63
- }
64
- return Object.assign(new Error(message), { statusCode });
65
- }
66
-
67
- function resolveMatrixStateKey(event: MatrixEvent): string | undefined {
68
- const direct = event.getStateKey?.();
69
- if (typeof direct === "string") {
70
- return direct;
71
- }
72
- const wireContent = (
73
- event as { getWireContent?: () => { state_key?: unknown } }
74
- ).getWireContent?.();
75
- if (wireContent && typeof wireContent.state_key === "string") {
76
- return wireContent.state_key;
77
- }
78
- const rawEvent = (event as { event?: { state_key?: unknown } }).event;
79
- if (rawEvent && typeof rawEvent.state_key === "string") {
80
- return rawEvent.state_key;
81
- }
82
- return undefined;
83
- }
@@ -1,87 +0,0 @@
1
- import type { PinnedDispatcherPolicy } from "klaw/plugin-sdk/ssrf-dispatcher";
2
- import type { SsrFPolicy } from "../../runtime-api.js";
3
- import { buildHttpError } from "./event-helpers.js";
4
- import { type HttpMethod, type QueryParams, performMatrixRequest } from "./transport.js";
5
-
6
- type MatrixAuthedHttpClientParams = {
7
- homeserver: string;
8
- accessToken: string;
9
- ssrfPolicy?: SsrFPolicy;
10
- dispatcherPolicy?: PinnedDispatcherPolicy;
11
- };
12
-
13
- export class MatrixAuthedHttpClient {
14
- private readonly homeserver: string;
15
- private readonly accessToken: string;
16
- private readonly ssrfPolicy?: SsrFPolicy;
17
- private readonly dispatcherPolicy?: PinnedDispatcherPolicy;
18
-
19
- constructor(params: MatrixAuthedHttpClientParams) {
20
- this.homeserver = params.homeserver;
21
- this.accessToken = params.accessToken;
22
- this.ssrfPolicy = params.ssrfPolicy;
23
- this.dispatcherPolicy = params.dispatcherPolicy;
24
- }
25
-
26
- async requestJson(params: {
27
- method: HttpMethod;
28
- endpoint: string;
29
- qs?: QueryParams;
30
- body?: unknown;
31
- timeoutMs: number;
32
- allowAbsoluteEndpoint?: boolean;
33
- }): Promise<unknown> {
34
- const { response, text } = await performMatrixRequest({
35
- homeserver: this.homeserver,
36
- accessToken: this.accessToken,
37
- method: params.method,
38
- endpoint: params.endpoint,
39
- qs: params.qs,
40
- body: params.body,
41
- timeoutMs: params.timeoutMs,
42
- ssrfPolicy: this.ssrfPolicy,
43
- dispatcherPolicy: this.dispatcherPolicy,
44
- allowAbsoluteEndpoint: params.allowAbsoluteEndpoint,
45
- });
46
- if (!response.ok) {
47
- throw buildHttpError(response.status, text);
48
- }
49
- const contentType = response.headers.get("content-type") ?? "";
50
- if (contentType.includes("application/json")) {
51
- if (!text.trim()) {
52
- return {};
53
- }
54
- return JSON.parse(text);
55
- }
56
- return text;
57
- }
58
-
59
- async requestRaw(params: {
60
- method: HttpMethod;
61
- endpoint: string;
62
- qs?: QueryParams;
63
- timeoutMs: number;
64
- maxBytes?: number;
65
- readIdleTimeoutMs?: number;
66
- allowAbsoluteEndpoint?: boolean;
67
- }): Promise<Buffer> {
68
- const { response, buffer } = await performMatrixRequest({
69
- homeserver: this.homeserver,
70
- accessToken: this.accessToken,
71
- method: params.method,
72
- endpoint: params.endpoint,
73
- qs: params.qs,
74
- timeoutMs: params.timeoutMs,
75
- raw: true,
76
- maxBytes: params.maxBytes,
77
- readIdleTimeoutMs: params.readIdleTimeoutMs,
78
- ssrfPolicy: this.ssrfPolicy,
79
- dispatcherPolicy: this.dispatcherPolicy,
80
- allowAbsoluteEndpoint: params.allowAbsoluteEndpoint,
81
- });
82
- if (!response.ok) {
83
- throw buildHttpError(response.status, buffer.toString("utf8"));
84
- }
85
- return buffer;
86
- }
87
- }
@@ -1,51 +0,0 @@
1
- import type { FileLockOptions } from "klaw/plugin-sdk/file-lock";
2
-
3
- export const MATRIX_IDB_PERSIST_INTERVAL_MS = 60_000;
4
-
5
- const IDB_SNAPSHOT_LOCK_STALE_MS = 5 * 60_000;
6
- const IDB_SNAPSHOT_LOCK_RETRY_BASE = {
7
- factor: 2,
8
- minTimeout: 50,
9
- maxTimeout: 5_000,
10
- randomize: true,
11
- } satisfies Omit<FileLockOptions["retries"], "retries">;
12
-
13
- function computeRetryDelayMs(retries: FileLockOptions["retries"], attempt: number): number {
14
- return Math.min(
15
- retries.maxTimeout,
16
- Math.max(retries.minTimeout, retries.minTimeout * retries.factor ** attempt),
17
- );
18
- }
19
-
20
- export function computeMinimumRetryWindowMs(retries: FileLockOptions["retries"]): number {
21
- let total = 0;
22
- const attempts = Math.max(1, retries.retries + 1);
23
- for (let attempt = 0; attempt < attempts - 1; attempt += 1) {
24
- total += computeRetryDelayMs(retries, attempt);
25
- }
26
- return total;
27
- }
28
-
29
- function resolveRetriesForMinimumWindowMs(
30
- retries: Omit<FileLockOptions["retries"], "retries">,
31
- minimumWindowMs: number,
32
- ): FileLockOptions["retries"] {
33
- const resolved: FileLockOptions["retries"] = {
34
- ...retries,
35
- retries: 0,
36
- };
37
- while (computeMinimumRetryWindowMs(resolved) < minimumWindowMs) {
38
- resolved.retries += 1;
39
- }
40
- return resolved;
41
- }
42
-
43
- export const MATRIX_IDB_SNAPSHOT_LOCK_OPTIONS: FileLockOptions = {
44
- // Wait longer than one periodic persist interval so a concurrent restore
45
- // or large snapshot dump finishes instead of forcing warn-and-continue.
46
- retries: resolveRetriesForMinimumWindowMs(
47
- IDB_SNAPSHOT_LOCK_RETRY_BASE,
48
- MATRIX_IDB_PERSIST_INTERVAL_MS,
49
- ),
50
- stale: IDB_SNAPSHOT_LOCK_STALE_MS,
51
- };