@haex-space/vault-sdk 4.8.1 → 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.
@@ -29,6 +29,18 @@ var MODEL_JOB_EVENTS = {
29
29
  /** A job of this frame ended: completed, failed or cancelled. */
30
30
  COMPLETED: "haextension:model-job:completed"
31
31
  };
32
+ var PEERS_EVENTS = {
33
+ /** The outcome of an invitation, once per invitation (inviting side). */
34
+ INVITE_STATUS: "peers:invite-status",
35
+ /** A channel opened after the user accepted an invitation in holzi's prompt (invited side). */
36
+ CHANNEL_OPENED: "peers:channel-opened",
37
+ /** A device joined one of this device's lobbies by code (host). */
38
+ JOINED: "peers:joined",
39
+ /** A message on a channel; `data` is Base64 on the bridge. */
40
+ MESSAGE: "peers:message",
41
+ /** A channel ended: closed, lost or timed out. */
42
+ CHANNEL_CLOSED: "peers:channel-closed"
43
+ };
32
44
  var EXTERNAL_EVENTS = {
33
45
  /** External request from authorized client */
34
46
  REQUEST: "haextension:external:request",
@@ -342,6 +354,26 @@ var MODEL_COMMANDS = {
342
354
  cancel: "extension_model_job_cancel"
343
355
  };
344
356
 
357
+ // src/commands/peers.ts
358
+ var PEER_COMMANDS = {
359
+ /** Invites a contact; the outcome follows as `peers:invite-status` */
360
+ invite: "extension_peers_invite",
361
+ /** Withdraws an open invitation on all devices of the contact */
362
+ inviteCancel: "extension_peers_invite_cancel",
363
+ /** Opens a lobby and returns its join code */
364
+ lobbyOpen: "extension_peers_lobby_open",
365
+ /** Closes a lobby; its code stops working, its channels stay open */
366
+ lobbyClose: "extension_peers_lobby_close",
367
+ /** Joins a lobby by its code; waits at most 15 s for the host */
368
+ join: "extension_peers_join",
369
+ /** Closes a channel of an own lobby and keeps the device out of that lobby */
370
+ remove: "extension_peers_remove",
371
+ /** Sends a fast or a reliable message over a channel */
372
+ send: "extension_peers_send",
373
+ /** Closes a channel on both sides */
374
+ close: "extension_peers_close"
375
+ };
376
+
345
377
  // src/commands/mail.ts
346
378
  var MAIL_COMMANDS = {
347
379
  /** LIST mailboxes + optional STATUS counts */
@@ -1646,6 +1678,156 @@ var ModelsAPI = class {
1646
1678
  }
1647
1679
  };
1648
1680
 
1681
+ // src/api/peers.ts
1682
+ var PEER_ERROR_KINDS = {
1683
+ contact_unknown: "PEER_CONTACT_UNKNOWN",
1684
+ invite_not_open: "PEER_INVITE_UNKNOWN",
1685
+ channel_unknown: "PEER_CHANNEL_CLOSED",
1686
+ channel_closed: "PEER_CHANNEL_CLOSED",
1687
+ too_large: "PEER_PAYLOAD_TOO_LARGE",
1688
+ lobby_closed: "PEER_LOBBY_CLOSED",
1689
+ not_admitted: "PEER_NOT_ALLOWED",
1690
+ limit: "PEER_LIMIT",
1691
+ network_unavailable: "PEER_NETWORK_UNAVAILABLE",
1692
+ invalid_input: "PEER_INVALID_INPUT"
1693
+ };
1694
+ var PeerError = class extends Error {
1695
+ constructor(code, reason, message, details = {}, options) {
1696
+ super(message, options);
1697
+ this.code = code;
1698
+ this.reason = reason;
1699
+ this.details = details;
1700
+ this.name = "PeerError";
1701
+ this.kind = PEER_ERROR_KINDS[reason];
1702
+ if (typeof details.limit === "number") this.limit = details.limit;
1703
+ }
1704
+ };
1705
+ function isPeerErrorReason(value) {
1706
+ return typeof value === "string" && Object.hasOwn(PEER_ERROR_KINDS, value);
1707
+ }
1708
+ function toPeerError(error) {
1709
+ if (typeof error !== "object" || error === null) return error;
1710
+ const { code, message, details } = error;
1711
+ if (typeof code !== "number" || typeof details !== "object" || details === null) return error;
1712
+ const reason = details.reason;
1713
+ if (!isPeerErrorReason(reason)) return error;
1714
+ return new PeerError(
1715
+ code,
1716
+ reason,
1717
+ typeof message === "string" ? message : reason,
1718
+ details,
1719
+ { cause: error }
1720
+ );
1721
+ }
1722
+ var EVENT_NAMES = {
1723
+ "invite-status": PEERS_EVENTS.INVITE_STATUS,
1724
+ "channel-opened": PEERS_EVENTS.CHANNEL_OPENED,
1725
+ joined: PEERS_EVENTS.JOINED,
1726
+ message: PEERS_EVENTS.MESSAGE,
1727
+ "channel-closed": PEERS_EVENTS.CHANNEL_CLOSED
1728
+ };
1729
+ function decodeEvent(type, data) {
1730
+ if (typeof data !== "object" || data === null) return void 0;
1731
+ if (type !== "message") return data;
1732
+ const message = data;
1733
+ if (typeof message.channelId !== "string" || typeof message.data !== "string") return void 0;
1734
+ let decoded;
1735
+ try {
1736
+ decoded = base64ToArrayBuffer(message.data);
1737
+ } catch {
1738
+ return void 0;
1739
+ }
1740
+ if (arrayBufferToBase64(decoded) !== message.data) return void 0;
1741
+ return {
1742
+ channelId: message.channelId,
1743
+ data: decoded,
1744
+ reliable: message.reliable === true
1745
+ };
1746
+ }
1747
+ var PeersAPI = class {
1748
+ constructor(client) {
1749
+ this.client = client;
1750
+ /** Largest fast message (`reliable: false`) in bytes. */
1751
+ this.MAX_FAST_BYTES = 1024;
1752
+ /** Largest reliable message in bytes. */
1753
+ this.MAX_RELIABLE_BYTES = 65536;
1754
+ /** Largest `payload` of an invitation or a join, as JSON, in bytes. */
1755
+ this.MAX_PAYLOAD_BYTES = 4096;
1756
+ }
1757
+ async call(method, params) {
1758
+ try {
1759
+ return await this.client.request(method, params);
1760
+ } catch (error) {
1761
+ throw toPeerError(error);
1762
+ }
1763
+ }
1764
+ /** Invites a contact; the outcome follows as `invite-status` with this `inviteId`. */
1765
+ async inviteAsync(contactId, payload) {
1766
+ const answer = await this.call(PEER_COMMANDS.invite, { contactId, payload });
1767
+ return { inviteId: answer.inviteId };
1768
+ }
1769
+ /** Withdraws an open invitation on all devices of the contact; no `invite-status` follows. */
1770
+ async cancelInviteAsync(inviteId) {
1771
+ await this.call(PEER_COMMANDS.inviteCancel, { inviteId });
1772
+ }
1773
+ /**
1774
+ * Opens a lobby; `lobbyId` is 1–64 characters of `[A-Za-z0-9._-]`. The join code is opaque:
1775
+ * hand it on as it is.
1776
+ */
1777
+ async openLobbyAsync(lobbyId, visibility) {
1778
+ const answer = await this.call(PEER_COMMANDS.lobbyOpen, { lobbyId, visibility });
1779
+ return { joinCode: answer.joinCode };
1780
+ }
1781
+ /** Closes a lobby: its code stops working, its channels stay open. An unknown lobby is fine. */
1782
+ async closeLobbyAsync(lobbyId) {
1783
+ await this.call(PEER_COMMANDS.lobbyClose, { lobbyId });
1784
+ }
1785
+ /**
1786
+ * Joins a lobby by its code with a self-chosen `displayName` (1–64 characters). The host
1787
+ * answers within 15 s; a lobby that cannot be joined for any reason rejects as `lobby_closed`.
1788
+ */
1789
+ async joinAsync(joinCode, displayName, payload) {
1790
+ const answer = await this.call(PEER_COMMANDS.join, {
1791
+ joinCode,
1792
+ displayName,
1793
+ payload
1794
+ });
1795
+ return { channelId: answer.channelId, lobbyId: answer.lobbyId };
1796
+ }
1797
+ /** Closes a channel of an own lobby and keeps that device (and vault) out of the lobby. */
1798
+ async removeAsync(channelId) {
1799
+ await this.call(PEER_COMMANDS.remove, { channelId });
1800
+ }
1801
+ /**
1802
+ * Sends bytes over a channel: fast (default, at most {@link MAX_FAST_BYTES}, may be lost) or
1803
+ * `reliable` (at most {@link MAX_RELIABLE_BYTES}, in order; 16 may wait per channel).
1804
+ */
1805
+ async sendAsync(channelId, data, options = {}) {
1806
+ await this.call(PEER_COMMANDS.send, {
1807
+ channelId,
1808
+ data: { $bytes: arrayBufferToBase64(data) },
1809
+ reliable: options.reliable ?? false
1810
+ });
1811
+ }
1812
+ /** Closes a channel; both sides get `channel-closed` with `reason: "closed"`. */
1813
+ async closeAsync(channelId) {
1814
+ await this.call(PEER_COMMANDS.close, { channelId });
1815
+ }
1816
+ /**
1817
+ * Listens for one of the peers events (`invite-status`, `channel-opened`, `joined`, `message`,
1818
+ * `channel-closed`); `message.data` arrives as bytes. Returns an unsubscribe function.
1819
+ */
1820
+ on(type, handler) {
1821
+ const name = EVENT_NAMES[type];
1822
+ const wrapped = (event) => {
1823
+ const decoded = decodeEvent(type, event.data);
1824
+ if (decoded) handler(decoded);
1825
+ };
1826
+ this.client.on(name, wrapped);
1827
+ return () => this.client.off(name, wrapped);
1828
+ }
1829
+ };
1830
+
1649
1831
  // src/api/mail.ts
1650
1832
  var MailAPI = class {
1651
1833
  constructor(client) {
@@ -2840,6 +3022,7 @@ var HaexVaultSdk = class {
2840
3022
  this.passwords = new PasswordsAPI(this);
2841
3023
  this.contacts = new ContactsAPI(this);
2842
3024
  this.models = new ModelsAPI(this);
3025
+ this.peers = new PeersAPI(this);
2843
3026
  this.mail = new MailAPI(this);
2844
3027
  this.notifications = new NotificationsAPI(this);
2845
3028
  this.tab = new TabAPI(this);