@telecrypt-io/storage 0.2.8 → 0.2.10

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
  }
@@ -61,12 +61,18 @@ export declare function isDeviceAccessTokenError(result: DeviceAccessTokenRespon
61
61
  * internally by matrix-js-sdk/oidc-client-ts (`mx_oidc_`-prefixed keys) — the
62
62
  * caller does not need to manage them; `completeAuthorizationCodeFlow` reads
63
63
  * them back after the redirect.
64
+ *
65
+ * `deviceId` is optional: when supplied, it is embedded in the requested
66
+ * scope (MSC2967) so MAS grants THAT device instead of minting a fresh one —
67
+ * the web UI passes a stable per-browser device id so repeated logins reuse
68
+ * the same Matrix device instead of accumulating one device per login.
64
69
  */
65
70
  export declare function beginAuthorizationCodeFlow(opts: {
66
71
  authMetadata: OidcClientConfig;
67
72
  clientId: string;
68
73
  homeserverUrl: string;
69
74
  redirectUri: string;
75
+ deviceId?: string;
70
76
  }): Promise<string>;
71
77
  /** Completes the authorization-code exchange after the `?code&state` redirect. */
72
78
  export declare function completeAuthorizationCodeFlow(code: string, state: string): Promise<{
package/dist/core/oidc.js CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import { createClient } from "matrix-js-sdk";
12
12
  import { registerOidcClient, generateScope, generateOidcAuthorizationUrl, completeAuthorizationCodeGrant, startDeviceAuthorization, waitForDeviceAuthorization, } from "matrix-js-sdk/lib/oidc/index.js";
13
+ import { OidcClient, WebStorageStateStore } from "oidc-client-ts";
13
14
  import { CliError } from "./errors.js";
14
15
  /**
15
16
  * Discovers the OIDC/MAS issuer + endpoints for a homeserver, via
@@ -84,9 +85,41 @@ export function isDeviceAccessTokenError(result) {
84
85
  * internally by matrix-js-sdk/oidc-client-ts (`mx_oidc_`-prefixed keys) — the
85
86
  * caller does not need to manage them; `completeAuthorizationCodeFlow` reads
86
87
  * them back after the redirect.
88
+ *
89
+ * `deviceId` is optional: when supplied, it is embedded in the requested
90
+ * scope (MSC2967) so MAS grants THAT device instead of minting a fresh one —
91
+ * the web UI passes a stable per-browser device id so repeated logins reuse
92
+ * the same Matrix device instead of accumulating one device per login.
87
93
  */
88
94
  export async function beginAuthorizationCodeFlow(opts) {
89
95
  const nonce = crypto.randomUUID();
96
+ if (opts.deviceId) {
97
+ // matrix-js-sdk's generateOidcAuthorizationUrl hardcodes
98
+ // generateScope() with no device id, so every login would mint a fresh
99
+ // Matrix device. Build the URL ourselves with the stable device id in
100
+ // the requested scope (MSC2967) so MAS grants THAT device — mirroring
101
+ // matrix-js-sdk's construction exactly (same state store, same user
102
+ // state shape) so completeAuthorizationCodeFlow still works unchanged.
103
+ const scope = generateScope(opts.deviceId);
104
+ const oidcClient = new OidcClient({
105
+ ...opts.authMetadata,
106
+ signingKeys: opts.authMetadata.signingKeys ?? undefined,
107
+ metadata: opts.authMetadata,
108
+ client_id: opts.clientId,
109
+ redirect_uri: opts.redirectUri,
110
+ authority: opts.authMetadata.issuer,
111
+ response_mode: "query",
112
+ response_type: "code",
113
+ scope,
114
+ stateStore: new WebStorageStateStore({ prefix: "mx_oidc_", store: window.sessionStorage }),
115
+ });
116
+ const userState = { homeserverUrl: opts.homeserverUrl, nonce };
117
+ const request = await oidcClient.createSigninRequest({
118
+ state: userState,
119
+ nonce,
120
+ });
121
+ return request.url;
122
+ }
90
123
  return generateOidcAuthorizationUrl({
91
124
  metadata: opts.authMetadata,
92
125
  clientId: opts.clientId,
@@ -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.8",
3
+ "version": "0.2.10",
4
4
  "license": "BUSL-1.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",