@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.
package/dist/react.mjs CHANGED
@@ -457,6 +457,18 @@ var MODEL_JOB_EVENTS = {
457
457
  /** A job of this frame ended: completed, failed or cancelled. */
458
458
  COMPLETED: "haextension:model-job:completed"
459
459
  };
460
+ var PEERS_EVENTS = {
461
+ /** The outcome of an invitation, once per invitation (inviting side). */
462
+ INVITE_STATUS: "peers:invite-status",
463
+ /** A channel opened after the user accepted an invitation in holzi's prompt (invited side). */
464
+ CHANNEL_OPENED: "peers:channel-opened",
465
+ /** A device joined one of this device's lobbies by code (host). */
466
+ JOINED: "peers:joined",
467
+ /** A message on a channel; `data` is Base64 on the bridge. */
468
+ MESSAGE: "peers:message",
469
+ /** A channel ended: closed, lost or timed out. */
470
+ CHANNEL_CLOSED: "peers:channel-closed"
471
+ };
460
472
  var EXTERNAL_EVENTS = {
461
473
  /** External request from authorized client */
462
474
  REQUEST: "haextension:external:request",
@@ -791,7 +803,32 @@ var MODEL_COMMANDS = {
791
803
  /** Starts a job; the result follows as an event */
792
804
  start: "extension_model_job_start",
793
805
  /** Cancels a running job of this frame */
794
- cancel: "extension_model_job_cancel"
806
+ cancel: "extension_model_job_cancel",
807
+ /** Asks for a grant before the first job; nothing goes to a model */
808
+ requestAccess: "extension_model_access_request",
809
+ /** Opens the host's page of this extension's models */
810
+ openSettings: "extension_model_settings_open"
811
+ };
812
+
813
+ // src/commands/peers.ts
814
+ init_esm_shims();
815
+ var PEER_COMMANDS = {
816
+ /** Invites a contact; the outcome follows as `peers:invite-status` */
817
+ invite: "extension_peers_invite",
818
+ /** Withdraws an open invitation on all devices of the contact */
819
+ inviteCancel: "extension_peers_invite_cancel",
820
+ /** Opens a lobby and returns its join code */
821
+ lobbyOpen: "extension_peers_lobby_open",
822
+ /** Closes a lobby; its code stops working, its channels stay open */
823
+ lobbyClose: "extension_peers_lobby_close",
824
+ /** Joins a lobby by its code; waits at most 15 s for the host */
825
+ join: "extension_peers_join",
826
+ /** Closes a channel of an own lobby and keeps the device out of that lobby */
827
+ remove: "extension_peers_remove",
828
+ /** Sends a fast or a reliable message over a channel */
829
+ send: "extension_peers_send",
830
+ /** Closes a channel on both sides */
831
+ close: "extension_peers_close"
795
832
  };
796
833
 
797
834
  // src/commands/mail.ts
@@ -2017,6 +2054,24 @@ var ModelsAPI = class {
2017
2054
  );
2018
2055
  return answer.models;
2019
2056
  }
2057
+ /**
2058
+ * Asks for a grant before the first job (holzi spec 055 US9) and resolves with the allowed
2059
+ * models that fit `needs`. Without one, the host asks the user as it would for a job, and the
2060
+ * SDK waits for the answer. Rejects with `no_capable_model` (8001) when no model of the host
2061
+ * fits and with a denial (1002) when the user refused. Nothing is sent to a model, and consent
2062
+ * for a destination outside this device is asked with the first job that sends data.
2063
+ */
2064
+ async requestAccessAsync(request) {
2065
+ const answer = await this.client.request(
2066
+ MODEL_COMMANDS.requestAccess,
2067
+ { ...request }
2068
+ );
2069
+ return answer.models;
2070
+ }
2071
+ /** Opens the host's page of this extension's models, where the user chooses models and effort. */
2072
+ async openSettingsAsync() {
2073
+ await this.client.request(MODEL_COMMANDS.openSettings, {});
2074
+ }
2020
2075
  /** Starts a job; its end comes as {@link onCompleted}. Use {@link runAsync} to await it. */
2021
2076
  async startAsync(job) {
2022
2077
  const answer = await this.client.request(MODEL_COMMANDS.start, {
@@ -2089,6 +2144,158 @@ var ModelsAPI = class {
2089
2144
  }
2090
2145
  };
2091
2146
 
2147
+ // src/api/peers.ts
2148
+ init_esm_shims();
2149
+ init_vaultKey();
2150
+ var PEER_ERROR_KINDS = {
2151
+ contact_unknown: "PEER_CONTACT_UNKNOWN",
2152
+ invite_not_open: "PEER_INVITE_UNKNOWN",
2153
+ channel_unknown: "PEER_CHANNEL_CLOSED",
2154
+ channel_closed: "PEER_CHANNEL_CLOSED",
2155
+ too_large: "PEER_PAYLOAD_TOO_LARGE",
2156
+ lobby_closed: "PEER_LOBBY_CLOSED",
2157
+ not_admitted: "PEER_NOT_ALLOWED",
2158
+ limit: "PEER_LIMIT",
2159
+ network_unavailable: "PEER_NETWORK_UNAVAILABLE",
2160
+ invalid_input: "PEER_INVALID_INPUT"
2161
+ };
2162
+ var PeerError = class extends Error {
2163
+ constructor(code, reason, message, details = {}, options) {
2164
+ super(message, options);
2165
+ this.code = code;
2166
+ this.reason = reason;
2167
+ this.details = details;
2168
+ this.name = "PeerError";
2169
+ this.kind = PEER_ERROR_KINDS[reason];
2170
+ if (typeof details.limit === "number") this.limit = details.limit;
2171
+ }
2172
+ };
2173
+ function isPeerErrorReason(value) {
2174
+ return typeof value === "string" && Object.hasOwn(PEER_ERROR_KINDS, value);
2175
+ }
2176
+ function toPeerError(error) {
2177
+ if (typeof error !== "object" || error === null) return error;
2178
+ const { code, message, details } = error;
2179
+ if (typeof code !== "number" || typeof details !== "object" || details === null) return error;
2180
+ const reason = details.reason;
2181
+ if (!isPeerErrorReason(reason)) return error;
2182
+ return new PeerError(
2183
+ code,
2184
+ reason,
2185
+ typeof message === "string" ? message : reason,
2186
+ details,
2187
+ { cause: error }
2188
+ );
2189
+ }
2190
+ var EVENT_NAMES = {
2191
+ "invite-status": PEERS_EVENTS.INVITE_STATUS,
2192
+ "channel-opened": PEERS_EVENTS.CHANNEL_OPENED,
2193
+ joined: PEERS_EVENTS.JOINED,
2194
+ message: PEERS_EVENTS.MESSAGE,
2195
+ "channel-closed": PEERS_EVENTS.CHANNEL_CLOSED
2196
+ };
2197
+ function decodeEvent(type, data) {
2198
+ if (typeof data !== "object" || data === null) return void 0;
2199
+ if (type !== "message") return data;
2200
+ const message = data;
2201
+ if (typeof message.channelId !== "string" || typeof message.data !== "string") return void 0;
2202
+ let decoded;
2203
+ try {
2204
+ decoded = base64ToArrayBuffer(message.data);
2205
+ } catch {
2206
+ return void 0;
2207
+ }
2208
+ if (arrayBufferToBase64(decoded) !== message.data) return void 0;
2209
+ return {
2210
+ channelId: message.channelId,
2211
+ data: decoded,
2212
+ reliable: message.reliable === true
2213
+ };
2214
+ }
2215
+ var PeersAPI = class {
2216
+ constructor(client) {
2217
+ this.client = client;
2218
+ /** Largest fast message (`reliable: false`) in bytes. */
2219
+ this.MAX_FAST_BYTES = 1024;
2220
+ /** Largest reliable message in bytes. */
2221
+ this.MAX_RELIABLE_BYTES = 65536;
2222
+ /** Largest `payload` of an invitation or a join, as JSON, in bytes. */
2223
+ this.MAX_PAYLOAD_BYTES = 4096;
2224
+ }
2225
+ async call(method, params) {
2226
+ try {
2227
+ return await this.client.request(method, params);
2228
+ } catch (error) {
2229
+ throw toPeerError(error);
2230
+ }
2231
+ }
2232
+ /** Invites a contact; the outcome follows as `invite-status` with this `inviteId`. */
2233
+ async inviteAsync(contactId, payload) {
2234
+ const answer = await this.call(PEER_COMMANDS.invite, { contactId, payload });
2235
+ return { inviteId: answer.inviteId };
2236
+ }
2237
+ /** Withdraws an open invitation on all devices of the contact; no `invite-status` follows. */
2238
+ async cancelInviteAsync(inviteId) {
2239
+ await this.call(PEER_COMMANDS.inviteCancel, { inviteId });
2240
+ }
2241
+ /**
2242
+ * Opens a lobby; `lobbyId` is 1–64 characters of `[A-Za-z0-9._-]`. The join code is opaque:
2243
+ * hand it on as it is.
2244
+ */
2245
+ async openLobbyAsync(lobbyId, visibility) {
2246
+ const answer = await this.call(PEER_COMMANDS.lobbyOpen, { lobbyId, visibility });
2247
+ return { joinCode: answer.joinCode };
2248
+ }
2249
+ /** Closes a lobby: its code stops working, its channels stay open. An unknown lobby is fine. */
2250
+ async closeLobbyAsync(lobbyId) {
2251
+ await this.call(PEER_COMMANDS.lobbyClose, { lobbyId });
2252
+ }
2253
+ /**
2254
+ * Joins a lobby by its code with a self-chosen `displayName` (1–64 characters). The host
2255
+ * answers within 15 s; a lobby that cannot be joined for any reason rejects as `lobby_closed`.
2256
+ */
2257
+ async joinAsync(joinCode, displayName, payload) {
2258
+ const answer = await this.call(PEER_COMMANDS.join, {
2259
+ joinCode,
2260
+ displayName,
2261
+ payload
2262
+ });
2263
+ return { channelId: answer.channelId, lobbyId: answer.lobbyId };
2264
+ }
2265
+ /** Closes a channel of an own lobby and keeps that device (and vault) out of the lobby. */
2266
+ async removeAsync(channelId) {
2267
+ await this.call(PEER_COMMANDS.remove, { channelId });
2268
+ }
2269
+ /**
2270
+ * Sends bytes over a channel: fast (default, at most {@link MAX_FAST_BYTES}, may be lost) or
2271
+ * `reliable` (at most {@link MAX_RELIABLE_BYTES}, in order; 16 may wait per channel).
2272
+ */
2273
+ async sendAsync(channelId, data, options = {}) {
2274
+ await this.call(PEER_COMMANDS.send, {
2275
+ channelId,
2276
+ data: { $bytes: arrayBufferToBase64(data) },
2277
+ reliable: options.reliable ?? false
2278
+ });
2279
+ }
2280
+ /** Closes a channel; both sides get `channel-closed` with `reason: "closed"`. */
2281
+ async closeAsync(channelId) {
2282
+ await this.call(PEER_COMMANDS.close, { channelId });
2283
+ }
2284
+ /**
2285
+ * Listens for one of the peers events (`invite-status`, `channel-opened`, `joined`, `message`,
2286
+ * `channel-closed`); `message.data` arrives as bytes. Returns an unsubscribe function.
2287
+ */
2288
+ on(type, handler) {
2289
+ const name = EVENT_NAMES[type];
2290
+ const wrapped = (event) => {
2291
+ const decoded = decodeEvent(type, event.data);
2292
+ if (decoded) handler(decoded);
2293
+ };
2294
+ this.client.on(name, wrapped);
2295
+ return () => this.client.off(name, wrapped);
2296
+ }
2297
+ };
2298
+
2092
2299
  // src/api/mail.ts
2093
2300
  init_esm_shims();
2094
2301
  var MailAPI = class {
@@ -3206,6 +3413,7 @@ var HaexVaultSdk = class {
3206
3413
  this.passwords = new PasswordsAPI(this);
3207
3414
  this.contacts = new ContactsAPI(this);
3208
3415
  this.models = new ModelsAPI(this);
3416
+ this.peers = new PeersAPI(this);
3209
3417
  this.mail = new MailAPI(this);
3210
3418
  this.notifications = new NotificationsAPI(this);
3211
3419
  this.tab = new TabAPI(this);