@lavitre/apps 1.53.0 → 1.55.0

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.
@@ -3,6 +3,8 @@ import { randomUuid } from '../Common/Uuid.js';
3
3
  import { CrdtChannel, MIN_CRDT_HOST_VERSION } from '../Common/CrdtChannel.js';
4
4
  import { BlobStore } from '../Common/BlobStore.js';
5
5
  import { isMinVersion } from '../Common/index.js';
6
+ import { AppSettingsError, APP_SETTINGS_TIMEOUT_MS } from '../Common/AppSettings.js';
7
+ import { PublicStorageError, PUBLIC_STORAGE_TIMEOUT_MS, PUBLIC_STORAGE_PUT_TIMEOUT_MS, MAX_PUBLIC_FILE_SIZE_BYTES, validateExpiresIn, putPublicFileExpiryField, withDownloadUrlFallback, } from '../Common/PublicStorage.js';
6
8
  import { apiVersion } from '../Version.js';
7
9
  import { normalizeSettingsSchema } from './SettingsSchema.js';
8
10
  export class Connector {
@@ -145,6 +147,18 @@ export class Connector {
145
147
  writable: true,
146
148
  value: void 0
147
149
  });
150
+ /**
151
+ * Pushed whenever the AppSettings value at a given level changes — see
152
+ * {@link getSettings} / {@link setSettings}. Fires for changes made by
153
+ * this app itself as well as by another vitre/app instance sharing the
154
+ * same team/space. Added 1.52.
155
+ */
156
+ Object.defineProperty(this, "onSettingsChanged", {
157
+ enumerable: true,
158
+ configurable: true,
159
+ writable: true,
160
+ value: void 0
161
+ });
148
162
  Object.defineProperty(this, "onVitreSettingsUpdated", {
149
163
  enumerable: true,
150
164
  configurable: true,
@@ -379,6 +393,71 @@ export class Connector {
379
393
  writable: true,
380
394
  value: void 0
381
395
  });
396
+ /**
397
+ * Public, unauthenticated file storage scoped to the vitre's TEAM — see
398
+ * the README "Public storage" section for the full semantics (scopes,
399
+ * allowed file types, size cap, path rules). Files are readable by
400
+ * ANYONE at the returned URL — never put private data here.
401
+ */
402
+ Object.defineProperty(this, "publicStorage", {
403
+ enumerable: true,
404
+ configurable: true,
405
+ writable: true,
406
+ value: {
407
+ put: async (path, data, contentType, scope = 'app', options) => {
408
+ validateExpiresIn(options?.expiresIn);
409
+ const buffer = await this.toPublicFileBuffer(data);
410
+ if (buffer.byteLength > MAX_PUBLIC_FILE_SIZE_BYTES) {
411
+ throw new PublicStorageError('tooLarge');
412
+ }
413
+ const requestId = randomUuid();
414
+ return await this.postRequest({
415
+ type: 'putPublicFile',
416
+ requestId,
417
+ scope,
418
+ path,
419
+ contentType,
420
+ data: buffer,
421
+ ...putPublicFileExpiryField(options?.expiresIn),
422
+ }, (msg, resolve, reject) => {
423
+ if (msg.type === 'publicStorageResponse' && msg.requestId === requestId) {
424
+ if (msg.ok) {
425
+ resolve(withDownloadUrlFallback(msg.file ?? { path, url: msg.url ?? '', size: buffer.byteLength }));
426
+ }
427
+ else {
428
+ reject(new PublicStorageError(msg.error ?? 'failed'));
429
+ }
430
+ }
431
+ }, [buffer], { ms: PUBLIC_STORAGE_PUT_TIMEOUT_MS, onTimeout: () => new PublicStorageError('unsupported') });
432
+ },
433
+ delete: async (path, scope = 'app') => {
434
+ const requestId = randomUuid();
435
+ await this.postRequest({ type: 'deletePublicFile', requestId, scope, path }, (msg, resolve, reject) => {
436
+ if (msg.type === 'publicStorageResponse' && msg.requestId === requestId) {
437
+ if (msg.ok)
438
+ resolve(undefined);
439
+ else
440
+ reject(new PublicStorageError(msg.error ?? 'failed'));
441
+ }
442
+ }, undefined, { ms: PUBLIC_STORAGE_TIMEOUT_MS, onTimeout: () => new PublicStorageError('unsupported') });
443
+ },
444
+ list: async (prefix, scope = 'app') => {
445
+ const requestId = randomUuid();
446
+ return await this.postRequest({ type: 'listPublicFiles', requestId, scope, prefix }, (msg, resolve, reject) => {
447
+ if (msg.type === 'publicStorageResponse' && msg.requestId === requestId) {
448
+ if (msg.ok) {
449
+ resolve({
450
+ baseUrl: msg.baseUrl ?? '',
451
+ files: (msg.files ?? []).map(withDownloadUrlFallback),
452
+ });
453
+ }
454
+ else
455
+ reject(new PublicStorageError(msg.error ?? 'failed'));
456
+ }
457
+ }, undefined, { ms: PUBLIC_STORAGE_TIMEOUT_MS, onTimeout: () => new PublicStorageError('unsupported') });
458
+ },
459
+ }
460
+ });
382
461
  Object.defineProperty(this, "requestResolvers", {
383
462
  enumerable: true,
384
463
  configurable: true,
@@ -513,6 +592,15 @@ export class Connector {
513
592
  };
514
593
  this.onSettingsUpdated?.(msg.settings);
515
594
  break;
595
+ case 'appSettingsResponse':
596
+ // Handled by requestResolvers (postRequest pattern)
597
+ break;
598
+ case 'appSettingsChanged':
599
+ this.onSettingsChanged?.(msg);
600
+ break;
601
+ case 'publicStorageResponse':
602
+ // Handled by requestResolvers (postRequest pattern)
603
+ break;
516
604
  case 'vitreSettingsUpdated':
517
605
  this.hostInformation = {
518
606
  ...this.hostInformation,
@@ -762,15 +850,26 @@ export class Connector {
762
850
  });
763
851
  }
764
852
  }
765
- async postRequest(data, handler, transfer) {
853
+ async postRequest(data, handler, transfer,
854
+ // When set, rejects with `onTimeout()` if no matching response arrives
855
+ // within `ms` — AND removes the pending resolver from
856
+ // `requestResolvers`. Without that removal a host that never answers
857
+ // (e.g. one that predates the message type) would leak the resolver
858
+ // forever, since the only other removal path is a matching response.
859
+ timeout) {
766
860
  return await new Promise((resolve, reject) => {
767
861
  let isHandled = false;
862
+ let timer;
768
863
  const handleAndResolve = (data) => {
769
864
  isHandled = true;
865
+ if (timer !== undefined)
866
+ clearTimeout(timer);
770
867
  resolve(data);
771
868
  };
772
869
  const handleAndReject = (error) => {
773
870
  isHandled = true;
871
+ if (timer !== undefined)
872
+ clearTimeout(timer);
774
873
  reject(error);
775
874
  };
776
875
  const resolver = {
@@ -785,6 +884,14 @@ export class Connector {
785
884
  },
786
885
  };
787
886
  this.requestResolvers.push(resolver);
887
+ if (timeout) {
888
+ timer = setTimeout(() => {
889
+ if (isHandled)
890
+ return;
891
+ this.requestResolvers = this.requestResolvers.filter((rr) => rr !== resolver);
892
+ handleAndReject(timeout.onTimeout());
893
+ }, timeout.ms);
894
+ }
788
895
  this.postMessage(data, transfer);
789
896
  });
790
897
  }
@@ -891,6 +998,17 @@ export class Connector {
891
998
  canShareStream() {
892
999
  return typeof MediaStreamTrackProcessor !== 'undefined';
893
1000
  }
1001
+ /** Normalize `publicStorage.put`'s payload to a plain `ArrayBuffer` —
1002
+ * slicing out exactly a typed array's view (not its whole underlying
1003
+ * buffer, in case it's a subarray) so the transferred bytes match the
1004
+ * size the caller intended. */
1005
+ async toPublicFileBuffer(data) {
1006
+ if (data instanceof ArrayBuffer)
1007
+ return data;
1008
+ if (data instanceof Blob)
1009
+ return await data.arrayBuffer();
1010
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
1011
+ }
894
1012
  toShareTracks(stream) {
895
1013
  const tracks = [];
896
1014
  const transfer = [];
@@ -1093,6 +1211,61 @@ export class Connector {
1093
1211
  settings,
1094
1212
  });
1095
1213
  }
1214
+ /**
1215
+ * Read the app's settings at one of the three levels — `vitre` (this
1216
+ * vitre), `team` (the vitre's team), or `space` (the space the vitre is
1217
+ * currently in). Every vitre/app instance of the team can read the same
1218
+ * value; values are JSON objects capped at 64 KB — never store secrets.
1219
+ *
1220
+ * Distinct from the legacy `settingsSchema`/`saveSettings` mechanism (see
1221
+ * {@link setSettingsSchema}, {@link saveSettings}), which is a single
1222
+ * vitre-scoped blob rendered by the host's settings form.
1223
+ *
1224
+ * Rejects with an {@link AppSettingsError} of code `'unsupported'` if the
1225
+ * host doesn't answer within 10s — a vitre host older than this feature
1226
+ * never responds to `getAppSettings`.
1227
+ */
1228
+ async getSettings(level) {
1229
+ const requestId = randomUuid();
1230
+ return await this.postRequest({ type: 'getAppSettings', requestId, level }, (msg, resolve, reject) => {
1231
+ if (msg.type === 'appSettingsResponse' && msg.requestId === requestId) {
1232
+ if (msg.ok) {
1233
+ resolve({ level: msg.level, value: msg.value ?? {}, version: msg.version ?? 0 });
1234
+ }
1235
+ else {
1236
+ reject(new AppSettingsError(msg.error ?? 'failed', level));
1237
+ }
1238
+ }
1239
+ }, undefined, { ms: APP_SETTINGS_TIMEOUT_MS, onTimeout: () => new AppSettingsError('unsupported', level) });
1240
+ }
1241
+ /**
1242
+ * Write the app's settings at `level`, with optimistic concurrency:
1243
+ * `expectedVersion` must be the version last observed for that level (from
1244
+ * {@link getSettings} or {@link onSettingsChanged}) — `0` if never
1245
+ * written. A stale version rejects with an {@link AppSettingsError} of
1246
+ * code `'conflict'`; re-read and retry. An oversized `value` (> 64 KB)
1247
+ * rejects with `'tooLarge'`.
1248
+ *
1249
+ * Rejects with code `'unsupported'` if the host doesn't answer within
1250
+ * 10s — see {@link getSettings}.
1251
+ */
1252
+ async setSettings(level, value, expectedVersion) {
1253
+ const requestId = randomUuid();
1254
+ return await this.postRequest({ type: 'setAppSettings', requestId, level, value, expectedVersion }, (msg, resolve, reject) => {
1255
+ if (msg.type === 'appSettingsResponse' && msg.requestId === requestId) {
1256
+ if (msg.ok) {
1257
+ resolve({
1258
+ level: msg.level,
1259
+ value: msg.value ?? value,
1260
+ version: msg.version ?? expectedVersion,
1261
+ });
1262
+ }
1263
+ else {
1264
+ reject(new AppSettingsError(msg.error ?? 'failed', level));
1265
+ }
1266
+ }
1267
+ }, undefined, { ms: APP_SETTINGS_TIMEOUT_MS, onTimeout: () => new AppSettingsError('unsupported', level) });
1268
+ }
1096
1269
  changeTeamSpace(space) {
1097
1270
  this.postMessage({
1098
1271
  type: 'changeTeamSpace',