@haex-space/vault-sdk 2.9.3 → 3.1.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.
@@ -1,6 +1,6 @@
1
1
  import * as nuxt_app from 'nuxt/app';
2
2
  import { ShallowRef } from 'vue';
3
- import { H as HaexVaultSdk } from '../client-CUZX5oKJ.mjs';
3
+ import { H as HaexVaultSdk } from '../client-C0DPNG62.mjs';
4
4
  import { A as ApplicationContext } from '../types-DmCSegdY.mjs';
5
5
  import 'drizzle-orm/sqlite-proxy';
6
6
 
@@ -1,6 +1,6 @@
1
1
  import * as nuxt_app from 'nuxt/app';
2
2
  import { ShallowRef } from 'vue';
3
- import { H as HaexVaultSdk } from '../client-BRsLRztg.js';
3
+ import { H as HaexVaultSdk } from '../client-B_B6rLIw.js';
4
4
  import { A as ApplicationContext } from '../types-DmCSegdY.js';
5
5
  import 'drizzle-orm/sqlite-proxy';
6
6
 
@@ -316,7 +316,7 @@ var DatabaseAPI = class {
316
316
  }
317
317
  async transaction(statements) {
318
318
  await this.client.request(DATABASE_COMMANDS.transaction, {
319
- statements
319
+ statements: statements.map((s) => [s.sql, s.params || []])
320
320
  });
321
321
  }
322
322
  async createTable(tableName, columns) {
@@ -1229,7 +1229,24 @@ var HAEXSPACE_MESSAGE_TYPES = {
1229
1229
  /** Debug message for development/troubleshooting */
1230
1230
  DEBUG: "haexspace:debug",
1231
1231
  /** Console forwarding from extension iframe */
1232
- CONSOLE_FORWARD: "console.forward"
1232
+ CONSOLE_FORWARD: "console.forward",
1233
+ /**
1234
+ * Sent from main window to iframe on the shared window listener, carrying
1235
+ * one `MessagePort` in `event.ports[0]`. Once received, the SDK switches
1236
+ * to port-based messaging and never reads the window listener again.
1237
+ *
1238
+ * Payload: `{ type: PORT_INIT }` — no data. The port itself is the payload.
1239
+ */
1240
+ PORT_INIT: "haexspace:port:init",
1241
+ /**
1242
+ * Sent from SDK to main window *over the MessagePort* after the port is
1243
+ * installed. Main uses this to mark the iframe as ready and flush any
1244
+ * events buffered during the handshake window. Only valid on the port —
1245
+ * a READY sent over window.postMessage is ignored.
1246
+ *
1247
+ * Payload: `{ type: PORT_READY }` — no data.
1248
+ */
1249
+ PORT_READY: "haexspace:port:ready"
1233
1250
  };
1234
1251
 
1235
1252
  // src/polyfills/consoleForwarding.ts
@@ -1384,6 +1401,7 @@ function parseTableName(fullTableName) {
1384
1401
  }
1385
1402
 
1386
1403
  // src/client/init.ts
1404
+ var PORT_HANDSHAKE_TIMEOUT_MS = 1e4;
1387
1405
  function isInIframe() {
1388
1406
  return window.self !== window.top;
1389
1407
  }
@@ -1640,11 +1658,14 @@ async function initIframeMode(ctx, log, messageHandler, request) {
1640
1658
  if (!isInIframe()) {
1641
1659
  throw new HaexVaultSdkError("NOT_IN_IFRAME" /* NOT_IN_IFRAME */, "errors.not_in_iframe");
1642
1660
  }
1661
+ const port = await waitForHostPortAsync(log);
1643
1662
  ctx.handlers.messageHandler = messageHandler;
1644
- window.addEventListener("message", messageHandler);
1663
+ port.addEventListener("message", messageHandler);
1664
+ port.start();
1665
+ port.postMessage({ type: HAEXSPACE_MESSAGE_TYPES.PORT_READY });
1645
1666
  ctx.state.isNativeWindow = false;
1646
1667
  ctx.state.initialized = true;
1647
- log("HaexVault SDK initialized in iframe mode");
1668
+ log("HaexVault SDK initialized in iframe mode (MessagePort transport)");
1648
1669
  if (ctx.config.manifest) {
1649
1670
  ctx.state.extensionInfo = {
1650
1671
  publicKey: ctx.config.manifest.publicKey,
@@ -1658,7 +1679,42 @@ async function initIframeMode(ctx, log, messageHandler, request) {
1658
1679
  const context = await request(EXTENSION_COMMANDS.getContext);
1659
1680
  ctx.state.context = context;
1660
1681
  log("Application context received:", context);
1661
- return { context };
1682
+ return { context, port };
1683
+ }
1684
+ function waitForHostPortAsync(log) {
1685
+ return new Promise((resolve, reject) => {
1686
+ let settled = false;
1687
+ const cleanup = () => {
1688
+ window.removeEventListener("message", handler);
1689
+ };
1690
+ const timeoutId = setTimeout(() => {
1691
+ if (settled) return;
1692
+ settled = true;
1693
+ cleanup();
1694
+ reject(
1695
+ new HaexVaultSdkError(
1696
+ "TIMEOUT" /* TIMEOUT */,
1697
+ "errors.port_handshake_timeout",
1698
+ { timeout: PORT_HANDSHAKE_TIMEOUT_MS }
1699
+ )
1700
+ );
1701
+ }, PORT_HANDSHAKE_TIMEOUT_MS);
1702
+ const handler = (event) => {
1703
+ const type = event.data?.type;
1704
+ if (type !== HAEXSPACE_MESSAGE_TYPES.PORT_INIT) return;
1705
+ const port = event.ports[0];
1706
+ if (!port) {
1707
+ log("PORT_INIT received but event.ports is empty \u2014 ignoring");
1708
+ return;
1709
+ }
1710
+ if (settled) return;
1711
+ settled = true;
1712
+ clearTimeout(timeoutId);
1713
+ cleanup();
1714
+ resolve(port);
1715
+ };
1716
+ window.addEventListener("message", handler);
1717
+ });
1662
1718
  }
1663
1719
  function sendDebugInfo(config) {
1664
1720
  if (!config.debug) return;
@@ -1682,7 +1738,15 @@ postMessage error: ${e}`);
1682
1738
  function generateRequestId(counter) {
1683
1739
  return `req_${counter}`;
1684
1740
  }
1685
- function sendPostMessage(method, params, requestId, config, extensionInfo, pendingRequests) {
1741
+ function sendPostMessage(method, params, requestId, config, extensionInfo, pendingRequests, port) {
1742
+ if (!port) {
1743
+ return Promise.reject(
1744
+ new HaexVaultSdkError(
1745
+ "EXTENSION_NOT_INITIALIZED" /* EXTENSION_NOT_INITIALIZED */,
1746
+ "errors.port_not_connected"
1747
+ )
1748
+ );
1749
+ }
1686
1750
  const request = {
1687
1751
  method,
1688
1752
  params,
@@ -1698,17 +1762,16 @@ function sendPostMessage(method, params, requestId, config, extensionInfo, pendi
1698
1762
  );
1699
1763
  }, config.timeout);
1700
1764
  pendingRequests.set(requestId, { resolve, reject, timeout });
1701
- const targetOrigin = "*";
1702
1765
  if (config.debug) {
1703
1766
  console.log("[SDK Debug] ========== Sending Request ==========");
1704
1767
  console.log("[SDK Debug] Request ID:", requestId);
1705
1768
  console.log("[SDK Debug] Method:", request.method);
1706
1769
  console.log("[SDK Debug] Params:", request.params);
1707
- console.log("[SDK Debug] Target origin:", targetOrigin);
1770
+ console.log("[SDK Debug] Transport: MessagePort");
1708
1771
  console.log("[SDK Debug] Extension info:", extensionInfo);
1709
1772
  console.log("[SDK Debug] ========================================");
1710
1773
  }
1711
- window.parent.postMessage({ id: requestId, ...request }, targetOrigin);
1774
+ port.postMessage({ id: requestId, ...request });
1712
1775
  });
1713
1776
  }
1714
1777
  async function sendInvoke(method, params, config, _log) {
@@ -1727,11 +1790,6 @@ function createMessageHandler(config, pendingRequests, extensionInfo, onEvent) {
1727
1790
  return (event) => {
1728
1791
  if (config.debug) {
1729
1792
  console.log("[SDK Debug] ========== Message Received ==========");
1730
- console.log("[SDK Debug] Event origin:", event.origin);
1731
- console.log(
1732
- "[SDK Debug] Event source:",
1733
- event.source === window.parent ? "parent window" : "unknown"
1734
- );
1735
1793
  console.log("[SDK Debug] Event data:", event.data);
1736
1794
  console.log("[SDK Debug] Extension info loaded:", !!extensionInfo());
1737
1795
  console.log(
@@ -1739,12 +1797,6 @@ function createMessageHandler(config, pendingRequests, extensionInfo, onEvent) {
1739
1797
  pendingRequests.size
1740
1798
  );
1741
1799
  }
1742
- if (event.source !== window.parent) {
1743
- if (config.debug) {
1744
- console.error("[SDK Debug] \u274C REJECTED: Message not from parent window!");
1745
- }
1746
- return;
1747
- }
1748
1800
  const data = event.data;
1749
1801
  if ("id" in data && pendingRequests.has(data.id)) {
1750
1802
  if (config.debug) {
@@ -1966,6 +2018,13 @@ var HaexVaultSdk = class {
1966
2018
  this.reactiveSubscribers = /* @__PURE__ */ new Set();
1967
2019
  // Handlers
1968
2020
  this.messageHandler = null;
2021
+ /**
2022
+ * MessagePort obtained from the main window during iframe-mode handshake.
2023
+ * `null` until `initIframe()` completes successfully. Every outbound
2024
+ * request in iframe mode flows through this port; `sendPostMessage` rejects
2025
+ * if called before the handshake finishes.
2026
+ */
2027
+ this.hostPort = null;
1969
2028
  this.setupPromise = null;
1970
2029
  this.setupHook = null;
1971
2030
  // Public APIs
@@ -2139,7 +2198,15 @@ var HaexVaultSdk = class {
2139
2198
  return sendInvoke(method, paramsWithCredentials, this.config, this.log.bind(this));
2140
2199
  }
2141
2200
  const requestId = generateRequestId(++this.requestCounter);
2142
- return sendPostMessage(method, resolvedParams, requestId, this.config, this._extensionInfo, this.pendingRequests);
2201
+ return sendPostMessage(
2202
+ method,
2203
+ resolvedParams,
2204
+ requestId,
2205
+ this.config,
2206
+ this._extensionInfo,
2207
+ this.pendingRequests,
2208
+ this.hostPort
2209
+ );
2143
2210
  }
2144
2211
  // ==========================================================================
2145
2212
  // Private: Initialization
@@ -2206,7 +2273,7 @@ var HaexVaultSdk = class {
2206
2273
  () => this._extensionInfo,
2207
2274
  this.handleEvent.bind(this)
2208
2275
  );
2209
- const { context } = await initIframeMode(
2276
+ const { context, port } = await initIframeMode(
2210
2277
  {
2211
2278
  config: this.config,
2212
2279
  state: {
@@ -2238,6 +2305,7 @@ var HaexVaultSdk = class {
2238
2305
  this.messageHandler,
2239
2306
  this.request.bind(this)
2240
2307
  );
2308
+ this.hostPort = port;
2241
2309
  if (this.config.manifest) {
2242
2310
  this._extensionInfo = {
2243
2311
  publicKey: this.config.manifest.publicKey,