@haex-space/vault-sdk 4.8.0 → 4.9.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.
package/dist/index.mjs CHANGED
@@ -765,6 +765,18 @@ var MODEL_JOB_EVENTS = {
765
765
  /** A job of this frame ended: completed, failed or cancelled. */
766
766
  COMPLETED: "haextension:model-job:completed"
767
767
  };
768
+ var PEERS_EVENTS = {
769
+ /** The outcome of an invitation, once per invitation (inviting side). */
770
+ INVITE_STATUS: "peers:invite-status",
771
+ /** A channel opened after the user accepted an invitation in holzi's prompt (invited side). */
772
+ CHANNEL_OPENED: "peers:channel-opened",
773
+ /** A device joined one of this device's lobbies by code (host). */
774
+ JOINED: "peers:joined",
775
+ /** A message on a channel; `data` is Base64 on the bridge. */
776
+ MESSAGE: "peers:message",
777
+ /** A channel ended: closed, lost or timed out. */
778
+ CHANNEL_CLOSED: "peers:channel-closed"
779
+ };
768
780
  var EXTERNAL_EVENTS = {
769
781
  /** External request from authorized client */
770
782
  REQUEST: "haextension:external:request",
@@ -1206,6 +1218,27 @@ var MODEL_COMMANDS = {
1206
1218
  cancel: "extension_model_job_cancel"
1207
1219
  };
1208
1220
 
1221
+ // src/commands/peers.ts
1222
+ init_esm_shims();
1223
+ var PEER_COMMANDS = {
1224
+ /** Invites a contact; the outcome follows as `peers:invite-status` */
1225
+ invite: "extension_peers_invite",
1226
+ /** Withdraws an open invitation on all devices of the contact */
1227
+ inviteCancel: "extension_peers_invite_cancel",
1228
+ /** Opens a lobby and returns its join code */
1229
+ lobbyOpen: "extension_peers_lobby_open",
1230
+ /** Closes a lobby; its code stops working, its channels stay open */
1231
+ lobbyClose: "extension_peers_lobby_close",
1232
+ /** Joins a lobby by its code; waits at most 15 s for the host */
1233
+ join: "extension_peers_join",
1234
+ /** Closes a channel of an own lobby and keeps the device out of that lobby */
1235
+ remove: "extension_peers_remove",
1236
+ /** Sends a fast or a reliable message over a channel */
1237
+ send: "extension_peers_send",
1238
+ /** Closes a channel on both sides */
1239
+ close: "extension_peers_close"
1240
+ };
1241
+
1209
1242
  // src/commands/mail.ts
1210
1243
  init_esm_shims();
1211
1244
  var MAIL_COMMANDS = {
@@ -1265,6 +1298,7 @@ var TAURI_COMMANDS = {
1265
1298
  passwords: PASSWORD_COMMANDS,
1266
1299
  contacts: CONTACT_COMMANDS,
1267
1300
  models: MODEL_COMMANDS,
1301
+ peers: PEER_COMMANDS,
1268
1302
  mail: MAIL_COMMANDS,
1269
1303
  notifications: NOTIFICATION_COMMANDS
1270
1304
  };
@@ -1408,7 +1442,8 @@ var FilesystemAPI = class {
1408
1442
  this.client = client;
1409
1443
  }
1410
1444
  /**
1411
- * Opens a save file dialog and saves the provided data to the selected location
1445
+ * Opens a save file dialog and saves the provided data to the selected location.
1446
+ * Waits for the user without the request timeout.
1412
1447
  * @param data The file data as Uint8Array
1413
1448
  * @param options Options for the save dialog
1414
1449
  * @returns The path where the file was saved, or null if cancelled
@@ -1422,7 +1457,8 @@ var FilesystemAPI = class {
1422
1457
  defaultPath: options.defaultPath,
1423
1458
  title: options.title,
1424
1459
  filters: options.filters
1425
- }
1460
+ },
1461
+ { timeout: null }
1426
1462
  );
1427
1463
  return result;
1428
1464
  }
@@ -1542,25 +1578,27 @@ var FilesystemAPI = class {
1542
1578
  );
1543
1579
  }
1544
1580
  /**
1545
- * Open a folder selection dialog
1581
+ * Open a folder selection dialog; waits for the user without the request timeout
1546
1582
  * @param options Dialog options
1547
1583
  * @returns Selected folder path, or null if cancelled
1548
1584
  */
1549
1585
  async selectFolder(options = {}) {
1550
1586
  return this.client.request(
1551
1587
  FILESYSTEM_COMMANDS.selectFolder,
1552
- options
1588
+ options,
1589
+ { timeout: null }
1553
1590
  );
1554
1591
  }
1555
1592
  /**
1556
- * Open a file selection dialog
1593
+ * Open a file selection dialog; waits for the user without the request timeout
1557
1594
  * @param options Dialog options
1558
1595
  * @returns Selected file paths, or null if cancelled
1559
1596
  */
1560
1597
  async selectFile(options = {}) {
1561
1598
  return this.client.request(
1562
1599
  FILESYSTEM_COMMANDS.selectFile,
1563
- options
1600
+ options,
1601
+ { timeout: null }
1564
1602
  );
1565
1603
  }
1566
1604
  /**
@@ -2527,6 +2565,158 @@ var ModelsAPI = class {
2527
2565
  }
2528
2566
  };
2529
2567
 
2568
+ // src/api/peers.ts
2569
+ init_esm_shims();
2570
+ init_vaultKey();
2571
+ var PEER_ERROR_KINDS = {
2572
+ contact_unknown: "PEER_CONTACT_UNKNOWN",
2573
+ invite_not_open: "PEER_INVITE_UNKNOWN",
2574
+ channel_unknown: "PEER_CHANNEL_CLOSED",
2575
+ channel_closed: "PEER_CHANNEL_CLOSED",
2576
+ too_large: "PEER_PAYLOAD_TOO_LARGE",
2577
+ lobby_closed: "PEER_LOBBY_CLOSED",
2578
+ not_admitted: "PEER_NOT_ALLOWED",
2579
+ limit: "PEER_LIMIT",
2580
+ network_unavailable: "PEER_NETWORK_UNAVAILABLE",
2581
+ invalid_input: "PEER_INVALID_INPUT"
2582
+ };
2583
+ var PeerError = class extends Error {
2584
+ constructor(code, reason, message, details = {}, options) {
2585
+ super(message, options);
2586
+ this.code = code;
2587
+ this.reason = reason;
2588
+ this.details = details;
2589
+ this.name = "PeerError";
2590
+ this.kind = PEER_ERROR_KINDS[reason];
2591
+ if (typeof details.limit === "number") this.limit = details.limit;
2592
+ }
2593
+ };
2594
+ function isPeerErrorReason(value) {
2595
+ return typeof value === "string" && Object.hasOwn(PEER_ERROR_KINDS, value);
2596
+ }
2597
+ function toPeerError(error) {
2598
+ if (typeof error !== "object" || error === null) return error;
2599
+ const { code, message, details } = error;
2600
+ if (typeof code !== "number" || typeof details !== "object" || details === null) return error;
2601
+ const reason = details.reason;
2602
+ if (!isPeerErrorReason(reason)) return error;
2603
+ return new PeerError(
2604
+ code,
2605
+ reason,
2606
+ typeof message === "string" ? message : reason,
2607
+ details,
2608
+ { cause: error }
2609
+ );
2610
+ }
2611
+ var EVENT_NAMES = {
2612
+ "invite-status": PEERS_EVENTS.INVITE_STATUS,
2613
+ "channel-opened": PEERS_EVENTS.CHANNEL_OPENED,
2614
+ joined: PEERS_EVENTS.JOINED,
2615
+ message: PEERS_EVENTS.MESSAGE,
2616
+ "channel-closed": PEERS_EVENTS.CHANNEL_CLOSED
2617
+ };
2618
+ function decodeEvent(type, data) {
2619
+ if (typeof data !== "object" || data === null) return void 0;
2620
+ if (type !== "message") return data;
2621
+ const message = data;
2622
+ if (typeof message.channelId !== "string" || typeof message.data !== "string") return void 0;
2623
+ let decoded;
2624
+ try {
2625
+ decoded = base64ToArrayBuffer(message.data);
2626
+ } catch {
2627
+ return void 0;
2628
+ }
2629
+ if (arrayBufferToBase64(decoded) !== message.data) return void 0;
2630
+ return {
2631
+ channelId: message.channelId,
2632
+ data: decoded,
2633
+ reliable: message.reliable === true
2634
+ };
2635
+ }
2636
+ var PeersAPI = class {
2637
+ constructor(client) {
2638
+ this.client = client;
2639
+ /** Largest fast message (`reliable: false`) in bytes. */
2640
+ this.MAX_FAST_BYTES = 1024;
2641
+ /** Largest reliable message in bytes. */
2642
+ this.MAX_RELIABLE_BYTES = 65536;
2643
+ /** Largest `payload` of an invitation or a join, as JSON, in bytes. */
2644
+ this.MAX_PAYLOAD_BYTES = 4096;
2645
+ }
2646
+ async call(method, params) {
2647
+ try {
2648
+ return await this.client.request(method, params);
2649
+ } catch (error) {
2650
+ throw toPeerError(error);
2651
+ }
2652
+ }
2653
+ /** Invites a contact; the outcome follows as `invite-status` with this `inviteId`. */
2654
+ async inviteAsync(contactId, payload) {
2655
+ const answer = await this.call(PEER_COMMANDS.invite, { contactId, payload });
2656
+ return { inviteId: answer.inviteId };
2657
+ }
2658
+ /** Withdraws an open invitation on all devices of the contact; no `invite-status` follows. */
2659
+ async cancelInviteAsync(inviteId) {
2660
+ await this.call(PEER_COMMANDS.inviteCancel, { inviteId });
2661
+ }
2662
+ /**
2663
+ * Opens a lobby; `lobbyId` is 1–64 characters of `[A-Za-z0-9._-]`. The join code is opaque:
2664
+ * hand it on as it is.
2665
+ */
2666
+ async openLobbyAsync(lobbyId, visibility) {
2667
+ const answer = await this.call(PEER_COMMANDS.lobbyOpen, { lobbyId, visibility });
2668
+ return { joinCode: answer.joinCode };
2669
+ }
2670
+ /** Closes a lobby: its code stops working, its channels stay open. An unknown lobby is fine. */
2671
+ async closeLobbyAsync(lobbyId) {
2672
+ await this.call(PEER_COMMANDS.lobbyClose, { lobbyId });
2673
+ }
2674
+ /**
2675
+ * Joins a lobby by its code with a self-chosen `displayName` (1–64 characters). The host
2676
+ * answers within 15 s; a lobby that cannot be joined for any reason rejects as `lobby_closed`.
2677
+ */
2678
+ async joinAsync(joinCode, displayName, payload) {
2679
+ const answer = await this.call(PEER_COMMANDS.join, {
2680
+ joinCode,
2681
+ displayName,
2682
+ payload
2683
+ });
2684
+ return { channelId: answer.channelId, lobbyId: answer.lobbyId };
2685
+ }
2686
+ /** Closes a channel of an own lobby and keeps that device (and vault) out of the lobby. */
2687
+ async removeAsync(channelId) {
2688
+ await this.call(PEER_COMMANDS.remove, { channelId });
2689
+ }
2690
+ /**
2691
+ * Sends bytes over a channel: fast (default, at most {@link MAX_FAST_BYTES}, may be lost) or
2692
+ * `reliable` (at most {@link MAX_RELIABLE_BYTES}, in order; 16 may wait per channel).
2693
+ */
2694
+ async sendAsync(channelId, data, options = {}) {
2695
+ await this.call(PEER_COMMANDS.send, {
2696
+ channelId,
2697
+ data: { $bytes: arrayBufferToBase64(data) },
2698
+ reliable: options.reliable ?? false
2699
+ });
2700
+ }
2701
+ /** Closes a channel; both sides get `channel-closed` with `reason: "closed"`. */
2702
+ async closeAsync(channelId) {
2703
+ await this.call(PEER_COMMANDS.close, { channelId });
2704
+ }
2705
+ /**
2706
+ * Listens for one of the peers events (`invite-status`, `channel-opened`, `joined`, `message`,
2707
+ * `channel-closed`); `message.data` arrives as bytes. Returns an unsubscribe function.
2708
+ */
2709
+ on(type, handler) {
2710
+ const name = EVENT_NAMES[type];
2711
+ const wrapped = (event) => {
2712
+ const decoded = decodeEvent(type, event.data);
2713
+ if (decoded) handler(decoded);
2714
+ };
2715
+ this.client.on(name, wrapped);
2716
+ return () => this.client.off(name, wrapped);
2717
+ }
2718
+ };
2719
+
2530
2720
  // src/api/mail.ts
2531
2721
  init_esm_shims();
2532
2722
  var MailAPI = class {
@@ -3644,6 +3834,7 @@ var HaexVaultSdk = class {
3644
3834
  this.passwords = new PasswordsAPI(this);
3645
3835
  this.contacts = new ContactsAPI(this);
3646
3836
  this.models = new ModelsAPI(this);
3837
+ this.peers = new PeersAPI(this);
3647
3838
  this.mail = new MailAPI(this);
3648
3839
  this.notifications = new NotificationsAPI(this);
3649
3840
  this.tab = new TabAPI(this);
@@ -4513,6 +4704,6 @@ function createHaexVaultSdk(config = {}) {
4513
4704
  return new HaexVaultSdk(config);
4514
4705
  }
4515
4706
 
4516
- export { CONTACT_COMMANDS, COSE_ALGORITHM, ContactsAPI, DEFAULT_TIMEOUT, DatabaseAPI, DialogAPI, ENCRYPTED_FOLDER_COMMANDS, EXTERNAL_EVENTS, EncryptedFoldersAPI, ErrorCode, ExternalConnectionErrorCode, ExternalConnectionState, FilesystemAPI, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_EVENTS, HaexVaultSdk, HaexVaultSdkError, HostErrorCode, KEY_AGREEMENT_ALGO, KnownPath, LOCALSEND_EVENTS, LocalSendAPI, MAIL_COMMANDS, MAIL_EVENTS, MODEL_COMMANDS, MODEL_JOB_EVENTS, MailAPI, ModelsAPI, NOTIFICATION_COMMANDS, NOTIFICATION_EVENTS, NotificationsAPI, PASSWORD_COMMANDS, PasswordsAPI, PermissionErrorCode, PermissionStatus, PermissionsAPI, RemoteStorageAPI, SHELL_EVENTS, SIGNING_ALGO, SPACE_COMMANDS, ShellAPI, SpacesAPI, TABLE_SEPARATOR, TAURI_COMMANDS, TabAPI, ToolsAPI, WebAPI, arrayBufferToBase64, base58btcDecode, base58btcEncode, base64ToArrayBuffer, canExternalClientSendRequests, createHaexVaultSdk, decryptCrdtData, decryptPrivateKeyAsync, decryptSpaceNameAsync, decryptString, decryptVaultKey, decryptVaultName, decryptWithPrivateKeyAsync, deriveKeyFromPassword, didKeyToPublicKeyAsync, encryptCrdtData, encryptPrivateKeyAsync, encryptSpaceNameAsync, encryptString, encryptVaultKey, encryptWithPublicKeyAsync, exportKeyPairAsync, exportPrivateKeyAsync, exportPublicKeyAsync, exportPublicKeyCoseAsync, exportUserKeypairAsync, generateCredentialId, generateIdentityAsync, generatePasskeyPairAsync, generateSpaceKey, generateUserKeypairAsync, generateVaultKey, getTableName, importPrivateKeyAsync, importPrivateKeyForKeyAgreementAsync, importPublicKeyAsync, importPublicKeyForKeyAgreementAsync, importUserPrivateKeyAsync, importUserPublicKeyAsync, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, isExternalClientConnected, isPermissionDeniedError, isPermissionError, isPermissionPromptError, publicKeyToDidKeyAsync, signClaimPresentationAsync, signRecordAsync, signSpaceChallengeAsync, signWithPasskeyAsync, unwrapKey, verifyClaimPresentationAsync, verifyRecordSignatureAsync, verifySpaceChallengeAsync, verifyWithPasskeyAsync, wrapKey };
4707
+ export { CONTACT_COMMANDS, COSE_ALGORITHM, ContactsAPI, DEFAULT_TIMEOUT, DatabaseAPI, DialogAPI, ENCRYPTED_FOLDER_COMMANDS, EXTERNAL_EVENTS, EncryptedFoldersAPI, ErrorCode, ExternalConnectionErrorCode, ExternalConnectionState, FilesystemAPI, HAEXSPACE_MESSAGE_TYPES, HAEXTENSION_EVENTS, HaexVaultSdk, HaexVaultSdkError, HostErrorCode, KEY_AGREEMENT_ALGO, KnownPath, LOCALSEND_EVENTS, LocalSendAPI, MAIL_COMMANDS, MAIL_EVENTS, MODEL_COMMANDS, MODEL_JOB_EVENTS, MailAPI, ModelsAPI, NOTIFICATION_COMMANDS, NOTIFICATION_EVENTS, NotificationsAPI, PASSWORD_COMMANDS, PEERS_EVENTS, PEER_COMMANDS, PEER_ERROR_KINDS, PasswordsAPI, PeerError, PeersAPI, PermissionErrorCode, PermissionStatus, PermissionsAPI, RemoteStorageAPI, SHELL_EVENTS, SIGNING_ALGO, SPACE_COMMANDS, ShellAPI, SpacesAPI, TABLE_SEPARATOR, TAURI_COMMANDS, TabAPI, ToolsAPI, WebAPI, arrayBufferToBase64, base58btcDecode, base58btcEncode, base64ToArrayBuffer, canExternalClientSendRequests, createHaexVaultSdk, decryptCrdtData, decryptPrivateKeyAsync, decryptSpaceNameAsync, decryptString, decryptVaultKey, decryptVaultName, decryptWithPrivateKeyAsync, deriveKeyFromPassword, didKeyToPublicKeyAsync, encryptCrdtData, encryptPrivateKeyAsync, encryptSpaceNameAsync, encryptString, encryptVaultKey, encryptWithPublicKeyAsync, exportKeyPairAsync, exportPrivateKeyAsync, exportPublicKeyAsync, exportPublicKeyCoseAsync, exportUserKeypairAsync, generateCredentialId, generateIdentityAsync, generatePasskeyPairAsync, generateSpaceKey, generateUserKeypairAsync, generateVaultKey, getTableName, importPrivateKeyAsync, importPrivateKeyForKeyAgreementAsync, importPublicKeyAsync, importPublicKeyForKeyAgreementAsync, importUserPrivateKeyAsync, importUserPublicKeyAsync, installBaseTag, installCookiePolyfill, installHistoryPolyfill, installLocalStoragePolyfill, installPolyfills, installSessionStoragePolyfill, isExternalClientConnected, isPermissionDeniedError, isPermissionError, isPermissionPromptError, publicKeyToDidKeyAsync, signClaimPresentationAsync, signRecordAsync, signSpaceChallengeAsync, signWithPasskeyAsync, unwrapKey, verifyClaimPresentationAsync, verifyRecordSignatureAsync, verifySpaceChallengeAsync, verifyWithPasskeyAsync, wrapKey };
4517
4708
  //# sourceMappingURL=index.mjs.map
4518
4709
  //# sourceMappingURL=index.mjs.map