@mastra/libsql 1.27.0-alpha.1 → 1.27.0-alpha.2

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/index.js CHANGED
@@ -1644,6 +1644,27 @@ function buildSelectColumnsWithAlias(tableName, alias) {
1644
1644
  function isLockError(error) {
1645
1645
  return error.code === "SQLITE_BUSY" || error.code === "SQLITE_LOCKED" || error.message?.toLowerCase().includes("database is locked") || error.message?.toLowerCase().includes("database table is locked") || error.message?.toLowerCase().includes("table is locked") || error.constructor.name === "SqliteError" && error.message?.toLowerCase().includes("locked");
1646
1646
  }
1647
+ const TRANSIENT_NETWORK_PATTERNS = [
1648
+ "fetch failed",
1649
+ "econnreset",
1650
+ "etimedout",
1651
+ "eai_again",
1652
+ "socket hang up",
1653
+ "und_err_"
1654
+ ];
1655
+ /**
1656
+ * Checks if an error (or any error in its `cause` chain) is a transient network failure
1657
+ * such as `fetch failed` from a remote libsql client.
1658
+ */
1659
+ function isTransientNetworkError(error) {
1660
+ let current = error;
1661
+ for (let depth = 0; current && depth < 5; depth++) {
1662
+ const text = `${current.message ?? ""} ${current.code ?? ""}`.toLowerCase();
1663
+ if (TRANSIENT_NETWORK_PATTERNS.some((pattern) => text.includes(pattern))) return true;
1664
+ current = current.cause;
1665
+ }
1666
+ return false;
1667
+ }
1647
1668
  function createExecuteWriteOperationWithRetry({ logger, maxRetries, initialBackoffMs }) {
1648
1669
  return async function executeWriteOperationWithRetry(operationFn, operationDescription) {
1649
1670
  let attempts = 0;
@@ -16583,6 +16604,8 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
16583
16604
  };
16584
16605
  //#endregion
16585
16606
  //#region src/storage/index.ts
16607
+ const REMOTE_INIT_CONCURRENCY = 5;
16608
+ const REMOTE_INIT_MAX_ATTEMPTS = 3;
16586
16609
  const DEFAULT_LOCAL_CACHE_SIZE = -16e3;
16587
16610
  const DEFAULT_LOCAL_MMAP_SIZE = 134217728;
16588
16611
  /**
@@ -16729,8 +16752,31 @@ var LibSQLStore = class extends MastraCompositeStore {
16729
16752
  for (const store of this.getStoresToInit()) await store.init();
16730
16753
  return true;
16731
16754
  }
16755
+ async initStoreWithRetry(store) {
16756
+ for (let attempt = 1;; attempt++) try {
16757
+ await store.init();
16758
+ return;
16759
+ } catch (error) {
16760
+ if (attempt >= REMOTE_INIT_MAX_ATTEMPTS || !isTransientNetworkError(error)) throw error;
16761
+ const backoff = this.initialBackoffMs * 2 ** (attempt - 1);
16762
+ this.logger.warn(`LibSQLStore: Transient network error during domain init, retrying in ${backoff}ms (attempt ${attempt}/${REMOTE_INIT_MAX_ATTEMPTS}).`, error);
16763
+ await new Promise((resolve) => setTimeout(resolve, backoff));
16764
+ }
16765
+ }
16732
16766
  async initDomainsInParallel() {
16733
- await Promise.all(this.getStoresToInit().map((store) => store.init()));
16767
+ const stores = this.getStoresToInit();
16768
+ let next = 0;
16769
+ let failed = false;
16770
+ const worker = async () => {
16771
+ while (!failed && next < stores.length) try {
16772
+ await this.initStoreWithRetry(stores[next++]);
16773
+ } catch (error) {
16774
+ failed = true;
16775
+ throw error;
16776
+ }
16777
+ };
16778
+ const rejected = (await Promise.allSettled(Array.from({ length: Math.min(REMOTE_INIT_CONCURRENCY, stores.length) }, worker))).find((r) => r.status === "rejected");
16779
+ if (rejected) throw rejected.reason;
16734
16780
  return true;
16735
16781
  }
16736
16782
  async init() {
@@ -16741,7 +16787,10 @@ var LibSQLStore = class extends MastraCompositeStore {
16741
16787
  await this.hasInitialized;
16742
16788
  return;
16743
16789
  }
16744
- this.hasInitialized = this.initDomainsInParallel();
16790
+ this.hasInitialized = this.initDomainsInParallel().catch((error) => {
16791
+ this.hasInitialized = null;
16792
+ throw error;
16793
+ });
16745
16794
  await this.hasInitialized;
16746
16795
  return;
16747
16796
  }
@@ -16753,7 +16802,10 @@ var LibSQLStore = class extends MastraCompositeStore {
16753
16802
  await this.hasInitialized;
16754
16803
  return;
16755
16804
  }
16756
- this.hasInitialized = this.initDomainsSequentially();
16805
+ this.hasInitialized = this.initDomainsSequentially().catch((error) => {
16806
+ this.hasInitialized = null;
16807
+ throw error;
16808
+ });
16757
16809
  await this.hasInitialized;
16758
16810
  return;
16759
16811
  }