@haex-space/vault-sdk 4.8.1 → 4.10.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",
@@ -339,7 +351,31 @@ var MODEL_COMMANDS = {
339
351
  /** Starts a job; the result follows as an event */
340
352
  start: "extension_model_job_start",
341
353
  /** Cancels a running job of this frame */
342
- cancel: "extension_model_job_cancel"
354
+ cancel: "extension_model_job_cancel",
355
+ /** Asks for a grant before the first job; nothing goes to a model */
356
+ requestAccess: "extension_model_access_request",
357
+ /** Opens the host's page of this extension's models */
358
+ openSettings: "extension_model_settings_open"
359
+ };
360
+
361
+ // src/commands/peers.ts
362
+ var PEER_COMMANDS = {
363
+ /** Invites a contact; the outcome follows as `peers:invite-status` */
364
+ invite: "extension_peers_invite",
365
+ /** Withdraws an open invitation on all devices of the contact */
366
+ inviteCancel: "extension_peers_invite_cancel",
367
+ /** Opens a lobby and returns its join code */
368
+ lobbyOpen: "extension_peers_lobby_open",
369
+ /** Closes a lobby; its code stops working, its channels stay open */
370
+ lobbyClose: "extension_peers_lobby_close",
371
+ /** Joins a lobby by its code; waits at most 15 s for the host */
372
+ join: "extension_peers_join",
373
+ /** Closes a channel of an own lobby and keeps the device out of that lobby */
374
+ remove: "extension_peers_remove",
375
+ /** Sends a fast or a reliable message over a channel */
376
+ send: "extension_peers_send",
377
+ /** Closes a channel on both sides */
378
+ close: "extension_peers_close"
343
379
  };
344
380
 
345
381
  // src/commands/mail.ts
@@ -1574,6 +1610,24 @@ var ModelsAPI = class {
1574
1610
  );
1575
1611
  return answer.models;
1576
1612
  }
1613
+ /**
1614
+ * Asks for a grant before the first job (holzi spec 055 US9) and resolves with the allowed
1615
+ * models that fit `needs`. Without one, the host asks the user as it would for a job, and the
1616
+ * SDK waits for the answer. Rejects with `no_capable_model` (8001) when no model of the host
1617
+ * fits and with a denial (1002) when the user refused. Nothing is sent to a model, and consent
1618
+ * for a destination outside this device is asked with the first job that sends data.
1619
+ */
1620
+ async requestAccessAsync(request) {
1621
+ const answer = await this.client.request(
1622
+ MODEL_COMMANDS.requestAccess,
1623
+ { ...request }
1624
+ );
1625
+ return answer.models;
1626
+ }
1627
+ /** Opens the host's page of this extension's models, where the user chooses models and effort. */
1628
+ async openSettingsAsync() {
1629
+ await this.client.request(MODEL_COMMANDS.openSettings, {});
1630
+ }
1577
1631
  /** Starts a job; its end comes as {@link onCompleted}. Use {@link runAsync} to await it. */
1578
1632
  async startAsync(job) {
1579
1633
  const answer = await this.client.request(MODEL_COMMANDS.start, {
@@ -1646,6 +1700,156 @@ var ModelsAPI = class {
1646
1700
  }
1647
1701
  };
1648
1702
 
1703
+ // src/api/peers.ts
1704
+ var PEER_ERROR_KINDS = {
1705
+ contact_unknown: "PEER_CONTACT_UNKNOWN",
1706
+ invite_not_open: "PEER_INVITE_UNKNOWN",
1707
+ channel_unknown: "PEER_CHANNEL_CLOSED",
1708
+ channel_closed: "PEER_CHANNEL_CLOSED",
1709
+ too_large: "PEER_PAYLOAD_TOO_LARGE",
1710
+ lobby_closed: "PEER_LOBBY_CLOSED",
1711
+ not_admitted: "PEER_NOT_ALLOWED",
1712
+ limit: "PEER_LIMIT",
1713
+ network_unavailable: "PEER_NETWORK_UNAVAILABLE",
1714
+ invalid_input: "PEER_INVALID_INPUT"
1715
+ };
1716
+ var PeerError = class extends Error {
1717
+ constructor(code, reason, message, details = {}, options) {
1718
+ super(message, options);
1719
+ this.code = code;
1720
+ this.reason = reason;
1721
+ this.details = details;
1722
+ this.name = "PeerError";
1723
+ this.kind = PEER_ERROR_KINDS[reason];
1724
+ if (typeof details.limit === "number") this.limit = details.limit;
1725
+ }
1726
+ };
1727
+ function isPeerErrorReason(value) {
1728
+ return typeof value === "string" && Object.hasOwn(PEER_ERROR_KINDS, value);
1729
+ }
1730
+ function toPeerError(error) {
1731
+ if (typeof error !== "object" || error === null) return error;
1732
+ const { code, message, details } = error;
1733
+ if (typeof code !== "number" || typeof details !== "object" || details === null) return error;
1734
+ const reason = details.reason;
1735
+ if (!isPeerErrorReason(reason)) return error;
1736
+ return new PeerError(
1737
+ code,
1738
+ reason,
1739
+ typeof message === "string" ? message : reason,
1740
+ details,
1741
+ { cause: error }
1742
+ );
1743
+ }
1744
+ var EVENT_NAMES = {
1745
+ "invite-status": PEERS_EVENTS.INVITE_STATUS,
1746
+ "channel-opened": PEERS_EVENTS.CHANNEL_OPENED,
1747
+ joined: PEERS_EVENTS.JOINED,
1748
+ message: PEERS_EVENTS.MESSAGE,
1749
+ "channel-closed": PEERS_EVENTS.CHANNEL_CLOSED
1750
+ };
1751
+ function decodeEvent(type, data) {
1752
+ if (typeof data !== "object" || data === null) return void 0;
1753
+ if (type !== "message") return data;
1754
+ const message = data;
1755
+ if (typeof message.channelId !== "string" || typeof message.data !== "string") return void 0;
1756
+ let decoded;
1757
+ try {
1758
+ decoded = base64ToArrayBuffer(message.data);
1759
+ } catch {
1760
+ return void 0;
1761
+ }
1762
+ if (arrayBufferToBase64(decoded) !== message.data) return void 0;
1763
+ return {
1764
+ channelId: message.channelId,
1765
+ data: decoded,
1766
+ reliable: message.reliable === true
1767
+ };
1768
+ }
1769
+ var PeersAPI = class {
1770
+ constructor(client) {
1771
+ this.client = client;
1772
+ /** Largest fast message (`reliable: false`) in bytes. */
1773
+ this.MAX_FAST_BYTES = 1024;
1774
+ /** Largest reliable message in bytes. */
1775
+ this.MAX_RELIABLE_BYTES = 65536;
1776
+ /** Largest `payload` of an invitation or a join, as JSON, in bytes. */
1777
+ this.MAX_PAYLOAD_BYTES = 4096;
1778
+ }
1779
+ async call(method, params) {
1780
+ try {
1781
+ return await this.client.request(method, params);
1782
+ } catch (error) {
1783
+ throw toPeerError(error);
1784
+ }
1785
+ }
1786
+ /** Invites a contact; the outcome follows as `invite-status` with this `inviteId`. */
1787
+ async inviteAsync(contactId, payload) {
1788
+ const answer = await this.call(PEER_COMMANDS.invite, { contactId, payload });
1789
+ return { inviteId: answer.inviteId };
1790
+ }
1791
+ /** Withdraws an open invitation on all devices of the contact; no `invite-status` follows. */
1792
+ async cancelInviteAsync(inviteId) {
1793
+ await this.call(PEER_COMMANDS.inviteCancel, { inviteId });
1794
+ }
1795
+ /**
1796
+ * Opens a lobby; `lobbyId` is 1–64 characters of `[A-Za-z0-9._-]`. The join code is opaque:
1797
+ * hand it on as it is.
1798
+ */
1799
+ async openLobbyAsync(lobbyId, visibility) {
1800
+ const answer = await this.call(PEER_COMMANDS.lobbyOpen, { lobbyId, visibility });
1801
+ return { joinCode: answer.joinCode };
1802
+ }
1803
+ /** Closes a lobby: its code stops working, its channels stay open. An unknown lobby is fine. */
1804
+ async closeLobbyAsync(lobbyId) {
1805
+ await this.call(PEER_COMMANDS.lobbyClose, { lobbyId });
1806
+ }
1807
+ /**
1808
+ * Joins a lobby by its code with a self-chosen `displayName` (1–64 characters). The host
1809
+ * answers within 15 s; a lobby that cannot be joined for any reason rejects as `lobby_closed`.
1810
+ */
1811
+ async joinAsync(joinCode, displayName, payload) {
1812
+ const answer = await this.call(PEER_COMMANDS.join, {
1813
+ joinCode,
1814
+ displayName,
1815
+ payload
1816
+ });
1817
+ return { channelId: answer.channelId, lobbyId: answer.lobbyId };
1818
+ }
1819
+ /** Closes a channel of an own lobby and keeps that device (and vault) out of the lobby. */
1820
+ async removeAsync(channelId) {
1821
+ await this.call(PEER_COMMANDS.remove, { channelId });
1822
+ }
1823
+ /**
1824
+ * Sends bytes over a channel: fast (default, at most {@link MAX_FAST_BYTES}, may be lost) or
1825
+ * `reliable` (at most {@link MAX_RELIABLE_BYTES}, in order; 16 may wait per channel).
1826
+ */
1827
+ async sendAsync(channelId, data, options = {}) {
1828
+ await this.call(PEER_COMMANDS.send, {
1829
+ channelId,
1830
+ data: { $bytes: arrayBufferToBase64(data) },
1831
+ reliable: options.reliable ?? false
1832
+ });
1833
+ }
1834
+ /** Closes a channel; both sides get `channel-closed` with `reason: "closed"`. */
1835
+ async closeAsync(channelId) {
1836
+ await this.call(PEER_COMMANDS.close, { channelId });
1837
+ }
1838
+ /**
1839
+ * Listens for one of the peers events (`invite-status`, `channel-opened`, `joined`, `message`,
1840
+ * `channel-closed`); `message.data` arrives as bytes. Returns an unsubscribe function.
1841
+ */
1842
+ on(type, handler) {
1843
+ const name = EVENT_NAMES[type];
1844
+ const wrapped = (event) => {
1845
+ const decoded = decodeEvent(type, event.data);
1846
+ if (decoded) handler(decoded);
1847
+ };
1848
+ this.client.on(name, wrapped);
1849
+ return () => this.client.off(name, wrapped);
1850
+ }
1851
+ };
1852
+
1649
1853
  // src/api/mail.ts
1650
1854
  var MailAPI = class {
1651
1855
  constructor(client) {
@@ -2840,6 +3044,7 @@ var HaexVaultSdk = class {
2840
3044
  this.passwords = new PasswordsAPI(this);
2841
3045
  this.contacts = new ContactsAPI(this);
2842
3046
  this.models = new ModelsAPI(this);
3047
+ this.peers = new PeersAPI(this);
2843
3048
  this.mail = new MailAPI(this);
2844
3049
  this.notifications = new NotificationsAPI(this);
2845
3050
  this.tab = new TabAPI(this);