@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/vue.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",
@@ -794,6 +806,27 @@ var MODEL_COMMANDS = {
794
806
  cancel: "extension_model_job_cancel"
795
807
  };
796
808
 
809
+ // src/commands/peers.ts
810
+ init_esm_shims();
811
+ var PEER_COMMANDS = {
812
+ /** Invites a contact; the outcome follows as `peers:invite-status` */
813
+ invite: "extension_peers_invite",
814
+ /** Withdraws an open invitation on all devices of the contact */
815
+ inviteCancel: "extension_peers_invite_cancel",
816
+ /** Opens a lobby and returns its join code */
817
+ lobbyOpen: "extension_peers_lobby_open",
818
+ /** Closes a lobby; its code stops working, its channels stay open */
819
+ lobbyClose: "extension_peers_lobby_close",
820
+ /** Joins a lobby by its code; waits at most 15 s for the host */
821
+ join: "extension_peers_join",
822
+ /** Closes a channel of an own lobby and keeps the device out of that lobby */
823
+ remove: "extension_peers_remove",
824
+ /** Sends a fast or a reliable message over a channel */
825
+ send: "extension_peers_send",
826
+ /** Closes a channel on both sides */
827
+ close: "extension_peers_close"
828
+ };
829
+
797
830
  // src/commands/mail.ts
798
831
  init_esm_shims();
799
832
  var MAIL_COMMANDS = {
@@ -966,7 +999,8 @@ var FilesystemAPI = class {
966
999
  this.client = client;
967
1000
  }
968
1001
  /**
969
- * Opens a save file dialog and saves the provided data to the selected location
1002
+ * Opens a save file dialog and saves the provided data to the selected location.
1003
+ * Waits for the user without the request timeout.
970
1004
  * @param data The file data as Uint8Array
971
1005
  * @param options Options for the save dialog
972
1006
  * @returns The path where the file was saved, or null if cancelled
@@ -980,7 +1014,8 @@ var FilesystemAPI = class {
980
1014
  defaultPath: options.defaultPath,
981
1015
  title: options.title,
982
1016
  filters: options.filters
983
- }
1017
+ },
1018
+ { timeout: null }
984
1019
  );
985
1020
  return result;
986
1021
  }
@@ -1100,25 +1135,27 @@ var FilesystemAPI = class {
1100
1135
  );
1101
1136
  }
1102
1137
  /**
1103
- * Open a folder selection dialog
1138
+ * Open a folder selection dialog; waits for the user without the request timeout
1104
1139
  * @param options Dialog options
1105
1140
  * @returns Selected folder path, or null if cancelled
1106
1141
  */
1107
1142
  async selectFolder(options = {}) {
1108
1143
  return this.client.request(
1109
1144
  FILESYSTEM_COMMANDS.selectFolder,
1110
- options
1145
+ options,
1146
+ { timeout: null }
1111
1147
  );
1112
1148
  }
1113
1149
  /**
1114
- * Open a file selection dialog
1150
+ * Open a file selection dialog; waits for the user without the request timeout
1115
1151
  * @param options Dialog options
1116
1152
  * @returns Selected file paths, or null if cancelled
1117
1153
  */
1118
1154
  async selectFile(options = {}) {
1119
1155
  return this.client.request(
1120
1156
  FILESYSTEM_COMMANDS.selectFile,
1121
- options
1157
+ options,
1158
+ { timeout: null }
1122
1159
  );
1123
1160
  }
1124
1161
  /**
@@ -2085,6 +2122,158 @@ var ModelsAPI = class {
2085
2122
  }
2086
2123
  };
2087
2124
 
2125
+ // src/api/peers.ts
2126
+ init_esm_shims();
2127
+ init_vaultKey();
2128
+ var PEER_ERROR_KINDS = {
2129
+ contact_unknown: "PEER_CONTACT_UNKNOWN",
2130
+ invite_not_open: "PEER_INVITE_UNKNOWN",
2131
+ channel_unknown: "PEER_CHANNEL_CLOSED",
2132
+ channel_closed: "PEER_CHANNEL_CLOSED",
2133
+ too_large: "PEER_PAYLOAD_TOO_LARGE",
2134
+ lobby_closed: "PEER_LOBBY_CLOSED",
2135
+ not_admitted: "PEER_NOT_ALLOWED",
2136
+ limit: "PEER_LIMIT",
2137
+ network_unavailable: "PEER_NETWORK_UNAVAILABLE",
2138
+ invalid_input: "PEER_INVALID_INPUT"
2139
+ };
2140
+ var PeerError = class extends Error {
2141
+ constructor(code, reason, message, details = {}, options) {
2142
+ super(message, options);
2143
+ this.code = code;
2144
+ this.reason = reason;
2145
+ this.details = details;
2146
+ this.name = "PeerError";
2147
+ this.kind = PEER_ERROR_KINDS[reason];
2148
+ if (typeof details.limit === "number") this.limit = details.limit;
2149
+ }
2150
+ };
2151
+ function isPeerErrorReason(value) {
2152
+ return typeof value === "string" && Object.hasOwn(PEER_ERROR_KINDS, value);
2153
+ }
2154
+ function toPeerError(error) {
2155
+ if (typeof error !== "object" || error === null) return error;
2156
+ const { code, message, details } = error;
2157
+ if (typeof code !== "number" || typeof details !== "object" || details === null) return error;
2158
+ const reason = details.reason;
2159
+ if (!isPeerErrorReason(reason)) return error;
2160
+ return new PeerError(
2161
+ code,
2162
+ reason,
2163
+ typeof message === "string" ? message : reason,
2164
+ details,
2165
+ { cause: error }
2166
+ );
2167
+ }
2168
+ var EVENT_NAMES = {
2169
+ "invite-status": PEERS_EVENTS.INVITE_STATUS,
2170
+ "channel-opened": PEERS_EVENTS.CHANNEL_OPENED,
2171
+ joined: PEERS_EVENTS.JOINED,
2172
+ message: PEERS_EVENTS.MESSAGE,
2173
+ "channel-closed": PEERS_EVENTS.CHANNEL_CLOSED
2174
+ };
2175
+ function decodeEvent(type, data) {
2176
+ if (typeof data !== "object" || data === null) return void 0;
2177
+ if (type !== "message") return data;
2178
+ const message = data;
2179
+ if (typeof message.channelId !== "string" || typeof message.data !== "string") return void 0;
2180
+ let decoded;
2181
+ try {
2182
+ decoded = base64ToArrayBuffer(message.data);
2183
+ } catch {
2184
+ return void 0;
2185
+ }
2186
+ if (arrayBufferToBase64(decoded) !== message.data) return void 0;
2187
+ return {
2188
+ channelId: message.channelId,
2189
+ data: decoded,
2190
+ reliable: message.reliable === true
2191
+ };
2192
+ }
2193
+ var PeersAPI = class {
2194
+ constructor(client) {
2195
+ this.client = client;
2196
+ /** Largest fast message (`reliable: false`) in bytes. */
2197
+ this.MAX_FAST_BYTES = 1024;
2198
+ /** Largest reliable message in bytes. */
2199
+ this.MAX_RELIABLE_BYTES = 65536;
2200
+ /** Largest `payload` of an invitation or a join, as JSON, in bytes. */
2201
+ this.MAX_PAYLOAD_BYTES = 4096;
2202
+ }
2203
+ async call(method, params) {
2204
+ try {
2205
+ return await this.client.request(method, params);
2206
+ } catch (error) {
2207
+ throw toPeerError(error);
2208
+ }
2209
+ }
2210
+ /** Invites a contact; the outcome follows as `invite-status` with this `inviteId`. */
2211
+ async inviteAsync(contactId, payload) {
2212
+ const answer = await this.call(PEER_COMMANDS.invite, { contactId, payload });
2213
+ return { inviteId: answer.inviteId };
2214
+ }
2215
+ /** Withdraws an open invitation on all devices of the contact; no `invite-status` follows. */
2216
+ async cancelInviteAsync(inviteId) {
2217
+ await this.call(PEER_COMMANDS.inviteCancel, { inviteId });
2218
+ }
2219
+ /**
2220
+ * Opens a lobby; `lobbyId` is 1–64 characters of `[A-Za-z0-9._-]`. The join code is opaque:
2221
+ * hand it on as it is.
2222
+ */
2223
+ async openLobbyAsync(lobbyId, visibility) {
2224
+ const answer = await this.call(PEER_COMMANDS.lobbyOpen, { lobbyId, visibility });
2225
+ return { joinCode: answer.joinCode };
2226
+ }
2227
+ /** Closes a lobby: its code stops working, its channels stay open. An unknown lobby is fine. */
2228
+ async closeLobbyAsync(lobbyId) {
2229
+ await this.call(PEER_COMMANDS.lobbyClose, { lobbyId });
2230
+ }
2231
+ /**
2232
+ * Joins a lobby by its code with a self-chosen `displayName` (1–64 characters). The host
2233
+ * answers within 15 s; a lobby that cannot be joined for any reason rejects as `lobby_closed`.
2234
+ */
2235
+ async joinAsync(joinCode, displayName, payload) {
2236
+ const answer = await this.call(PEER_COMMANDS.join, {
2237
+ joinCode,
2238
+ displayName,
2239
+ payload
2240
+ });
2241
+ return { channelId: answer.channelId, lobbyId: answer.lobbyId };
2242
+ }
2243
+ /** Closes a channel of an own lobby and keeps that device (and vault) out of the lobby. */
2244
+ async removeAsync(channelId) {
2245
+ await this.call(PEER_COMMANDS.remove, { channelId });
2246
+ }
2247
+ /**
2248
+ * Sends bytes over a channel: fast (default, at most {@link MAX_FAST_BYTES}, may be lost) or
2249
+ * `reliable` (at most {@link MAX_RELIABLE_BYTES}, in order; 16 may wait per channel).
2250
+ */
2251
+ async sendAsync(channelId, data, options = {}) {
2252
+ await this.call(PEER_COMMANDS.send, {
2253
+ channelId,
2254
+ data: { $bytes: arrayBufferToBase64(data) },
2255
+ reliable: options.reliable ?? false
2256
+ });
2257
+ }
2258
+ /** Closes a channel; both sides get `channel-closed` with `reason: "closed"`. */
2259
+ async closeAsync(channelId) {
2260
+ await this.call(PEER_COMMANDS.close, { channelId });
2261
+ }
2262
+ /**
2263
+ * Listens for one of the peers events (`invite-status`, `channel-opened`, `joined`, `message`,
2264
+ * `channel-closed`); `message.data` arrives as bytes. Returns an unsubscribe function.
2265
+ */
2266
+ on(type, handler) {
2267
+ const name = EVENT_NAMES[type];
2268
+ const wrapped = (event) => {
2269
+ const decoded = decodeEvent(type, event.data);
2270
+ if (decoded) handler(decoded);
2271
+ };
2272
+ this.client.on(name, wrapped);
2273
+ return () => this.client.off(name, wrapped);
2274
+ }
2275
+ };
2276
+
2088
2277
  // src/api/mail.ts
2089
2278
  init_esm_shims();
2090
2279
  var MailAPI = class {
@@ -3202,6 +3391,7 @@ var HaexVaultSdk = class {
3202
3391
  this.passwords = new PasswordsAPI(this);
3203
3392
  this.contacts = new ContactsAPI(this);
3204
3393
  this.models = new ModelsAPI(this);
3394
+ this.peers = new PeersAPI(this);
3205
3395
  this.mail = new MailAPI(this);
3206
3396
  this.notifications = new NotificationsAPI(this);
3207
3397
  this.tab = new TabAPI(this);