@telecrypt-io/storage 0.2.9 → 0.2.11

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.
@@ -213,6 +213,21 @@ export declare class TeleCryptIOStorage {
213
213
  */
214
214
  createTree(name: string): Promise<TreeSpace>;
215
215
  listTrees(): Promise<TreeSpace[]>;
216
+ /**
217
+ * Creates a subdirectory (nested tree space) under `parent` and links it
218
+ * with the parent's space.child / the child's space.parent state events.
219
+ *
220
+ * Mirrors `createTree`'s workaround for the same matrix-js-sdk race:
221
+ * `MSC3089TreeSpace.createDirectory()` calls `unstableCreateFileTree()`,
222
+ * which creates the room via a plain `createRoom()` HTTP call and then
223
+ * immediately wraps the result by looking the room up in the *local*
224
+ * client store — throwing `Error("Unknown room")` when the background
225
+ * sync loop hasn't surfaced the new room yet. Without the workaround the
226
+ * subfolder room is created server-side but never linked (no space.child
227
+ * event), leaving an orphaned room and a spurious failure for a folder
228
+ * that in fact exists.
229
+ */
230
+ createSubtree(parent: TreeSpace, name: string): Promise<TreeSpace>;
216
231
  getTree(roomId: string): TreeSpace | null;
217
232
  /**
218
233
  * Lists the participants of a shared folder (tree space) and their role,
@@ -1,4 +1,4 @@
1
- import { ClientEvent, createClient, MatrixEvent, SyncState } from "matrix-js-sdk";
1
+ import { ClientEvent, createClient, EventType, MatrixEvent, SyncState } from "matrix-js-sdk";
2
2
  import { Method } from "matrix-js-sdk/lib/http-api/method.js";
3
3
  import { ClientPrefix } from "matrix-js-sdk/lib/http-api/prefix.js";
4
4
  import { encryptAttachment, decryptAttachment } from "matrix-encrypt-attachment";
@@ -306,6 +306,59 @@ export class TeleCryptIOStorage {
306
306
  }
307
307
  return trees;
308
308
  }
309
+ /**
310
+ * Creates a subdirectory (nested tree space) under `parent` and links it
311
+ * with the parent's space.child / the child's space.parent state events.
312
+ *
313
+ * Mirrors `createTree`'s workaround for the same matrix-js-sdk race:
314
+ * `MSC3089TreeSpace.createDirectory()` calls `unstableCreateFileTree()`,
315
+ * which creates the room via a plain `createRoom()` HTTP call and then
316
+ * immediately wraps the result by looking the room up in the *local*
317
+ * client store — throwing `Error("Unknown room")` when the background
318
+ * sync loop hasn't surfaced the new room yet. Without the workaround the
319
+ * subfolder room is created server-side but never linked (no space.child
320
+ * event), leaving an orphaned room and a spurious failure for a folder
321
+ * that in fact exists.
322
+ */
323
+ async createSubtree(parent, name) {
324
+ const knownRoomIdsBefore = new Set(this.client.getRooms().map((r) => r.roomId));
325
+ try {
326
+ // The `await` here is load-bearing: without it, the promise's
327
+ // eventual rejection would happen after this try/catch has already
328
+ // returned, bypassing the recovery logic below entirely.
329
+ return (await parent.createDirectory(name));
330
+ }
331
+ catch (err) {
332
+ if (!(err instanceof Error) || err.message !== "Unknown room")
333
+ throw err;
334
+ const newRoomId = await new Promise((resolve, reject) => {
335
+ const deadline = Date.now() + 15000;
336
+ const poll = () => {
337
+ const found = this.client.getRooms().find((r) => !knownRoomIdsBefore.has(r.roomId));
338
+ if (found) {
339
+ resolve(found.roomId);
340
+ return;
341
+ }
342
+ if (Date.now() >= deadline) {
343
+ reject(new Error("createSubtree: room was created server-side but never appeared in this client's local sync state"));
344
+ return;
345
+ }
346
+ setTimeout(poll, 200);
347
+ };
348
+ poll();
349
+ });
350
+ const tree = this.client.unstableGetFileTreeSpace(newRoomId);
351
+ if (!tree) {
352
+ throw new Error(`createSubtree: room ${newRoomId} appeared but is not a valid file tree space`);
353
+ }
354
+ // The room appeared in sync, but `createDirectory` threw before it
355
+ // could send the space.child / space.parent link events. Send them
356
+ // now so the subfolder is actually reachable from its parent.
357
+ await this.client.sendStateEvent(parent.id, EventType.SpaceChild, { via: [this.client.getDomain()] }, newRoomId);
358
+ await this.client.sendStateEvent(newRoomId, EventType.SpaceParent, { via: [this.client.getDomain()] }, parent.id);
359
+ return tree;
360
+ }
361
+ }
309
362
  getTree(roomId) {
310
363
  return this.client.unstableGetFileTreeSpace(roomId);
311
364
  }
@@ -92,7 +92,7 @@ export function getMyFolderRole(storage, folderId) {
92
92
  }
93
93
  export async function joinFolder(storage, folderId) {
94
94
  try {
95
- await storage.getClient().joinRoom(folderId);
95
+ await withRateLimitRetry("joinFolder", () => storage.getClient().joinRoom(folderId));
96
96
  }
97
97
  catch (err) {
98
98
  throw new CliError(`join failed: ${err.message}`);
@@ -173,20 +173,20 @@ export async function shareFolder(storage, folderId, userId, role) {
173
173
  }
174
174
  const tree = await resolveTree(storage, folderId);
175
175
  try {
176
- await tree.invite(userId);
176
+ await withRateLimitRetry("shareFolder.invite", () => tree.invite(userId));
177
177
  }
178
178
  catch (err) {
179
179
  if (!/already in the room/i.test(err.message)) {
180
180
  throw err;
181
181
  }
182
182
  }
183
- await tree.setPermissions(userId, role);
183
+ await withRateLimitRetry("shareFolder.setPermissions", () => tree.setPermissions(userId, role));
184
184
  return { folderId, userId, role };
185
185
  }
186
186
  export async function unshareFolder(storage, folderId, userId) {
187
187
  await resolveTree(storage, folderId);
188
188
  try {
189
- await storage.getClient().kick(folderId, userId, "unshared");
189
+ await withRateLimitRetry("unshareFolder", () => storage.getClient().kick(folderId, userId, "unshared"));
190
190
  }
191
191
  catch (err) {
192
192
  throw new CliError(`unshare failed: ${err.message}`);
@@ -208,7 +208,7 @@ export async function listSubfolders(storage, folderId) {
208
208
  }
209
209
  export async function createSubfolder(storage, folderId, name) {
210
210
  const tree = await resolveTree(storage, folderId);
211
- const sub = await withRateLimitRetry("createSubfolder", () => tree.createDirectory(name));
211
+ const sub = await withRateLimitRetry("createSubfolder", () => storage.createSubtree(tree, name));
212
212
  return { id: sub.id, name };
213
213
  }
214
214
  export async function renameFolder(storage, folderId, name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telecrypt-io/storage",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "license": "BUSL-1.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",