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