@swell/apps-sdk 1.0.139 → 1.0.140

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.mjs CHANGED
@@ -16,31 +16,44 @@ import {
16
16
 
17
17
  // src/cache/cf-worker-kv-keyv-adapter.ts
18
18
  var CFWorkerKVKeyvAdapter = class {
19
+ store;
19
20
  namespace;
20
21
  // magically passed in from Keyv
21
- store;
22
+ opts;
22
23
  constructor(store) {
23
24
  this.store = store;
25
+ this.opts = null;
24
26
  this.namespace = "dummy";
25
27
  }
26
28
  async has(key) {
27
- return this.get(key) !== void 0;
29
+ const stream = await this.store.get(key, "stream");
30
+ if (stream !== null) {
31
+ await stream.cancel();
32
+ return true;
33
+ }
34
+ return false;
28
35
  }
29
36
  async get(key) {
30
- return this.store.get(key);
37
+ const value = await this.store.get(key);
38
+ return value !== null ? value : void 0;
31
39
  }
32
- async set(key, value) {
33
- return this.store.put(key, value);
40
+ set(key, value, ttl) {
41
+ if (typeof ttl === "number") {
42
+ ttl = Math.max(60, ttl / 1e3);
43
+ }
44
+ return this.store.put(key, value, { expirationTtl: ttl });
34
45
  }
35
46
  async delete(key) {
36
47
  await this.store.delete(key);
48
+ return true;
37
49
  }
38
50
  async clear() {
39
51
  let cursor = "";
40
52
  let complete = false;
41
- while (!complete) {
53
+ const prefix = `${this.namespace}:`;
54
+ do {
42
55
  const response = await this.store.list({
43
- prefix: `${this.namespace}:`,
56
+ prefix,
44
57
  cursor: cursor || void 0
45
58
  });
46
59
  cursor = response.cursor ?? "";
@@ -52,7 +65,10 @@ var CFWorkerKVKeyvAdapter = class {
52
65
  })
53
66
  );
54
67
  }
55
- }
68
+ } while (!complete);
69
+ }
70
+ on(_event, _listener) {
71
+ return this;
56
72
  }
57
73
  };
58
74
 
@@ -6956,23 +6972,25 @@ var Cache = class {
6956
6972
  ...options
6957
6973
  });
6958
6974
  }
6959
- async fetch(key, fetchFn) {
6960
- return this.client.wrap(key, fetchFn);
6975
+ async fetch(key, fetchFn, ttl) {
6976
+ return this.client.wrap(key, fetchFn, ttl);
6961
6977
  }
6962
- // Fetch cache using SWR (stale-while-revalidate)
6963
- // This will always return the cached value immediately if exists
6978
+ /**
6979
+ * Fetch cache using SWR (stale-while-revalidate)
6980
+ *
6981
+ * This will always return the cached value immediately if exists
6982
+ */
6964
6983
  async fetchSWR(key, fetchFn, ttl = DEFAULT_SWR_TTL) {
6965
6984
  const cacheValue = await this.client.get(key);
6966
- const promiseValue = Promise.resolve().then(() => fetchFn()).then(async (value) => {
6985
+ const promiseValue = Promise.resolve().then(fetchFn).then(resolveAsyncResources).then(async (value) => {
6967
6986
  const isNull = value === null || value === void 0;
6968
- const valueResolved = await resolveAsyncResources(value);
6969
- await this.client.set(key, isNull ? NULL_VALUE : valueResolved, ttl);
6987
+ await this.client.set(key, isNull ? NULL_VALUE : value, ttl);
6970
6988
  return value;
6971
6989
  });
6972
6990
  if (this.workerCtx?.waitUntil) {
6973
6991
  this.workerCtx.waitUntil(promiseValue);
6974
6992
  }
6975
- if (cacheValue !== null) {
6993
+ if (cacheValue !== void 0) {
6976
6994
  return cacheValue === NULL_VALUE ? null : cacheValue;
6977
6995
  }
6978
6996
  const result = await promiseValue;
@@ -6989,8 +7007,9 @@ var Cache = class {
6989
7007
  }
6990
7008
  /**
6991
7009
  * Flushes the entire cache.
6992
- * WARNING: If the cache store is shared among many cache clients,
6993
- * this will flush entries for other clients.
7010
+ *
7011
+ * __WARNING__: If the cache store is shared among many cache clients,
7012
+ * this will flush entries for other clients.
6994
7013
  */
6995
7014
  async flushAll() {
6996
7015
  await this.client.clear();
@@ -7033,7 +7052,7 @@ var ResourceCache = class extends Cache {
7033
7052
  function buildStores2() {
7034
7053
  return [
7035
7054
  new Keyv2({
7036
- // Disabling serialization allows for pure memo-ization of class instances
7055
+ // Disabling serialization allows for pure memoization of class instances
7037
7056
  // at the tradeoff of no support for compression.
7038
7057
  serialize: void 0,
7039
7058
  deserialize: void 0
@@ -18799,28 +18818,30 @@ var ThemeLoader = class _ThemeLoader {
18799
18818
  * Fetches a theme config by file path.
18800
18819
  */
18801
18820
  async fetchThemeConfig(filePath) {
18802
- const themeConfig = this.configs.get(filePath);
18803
- if (themeConfig !== void 0) {
18804
- return themeConfig;
18821
+ const config = this.configs.get(filePath);
18822
+ if (config !== void 0) {
18823
+ return config;
18805
18824
  }
18806
18825
  const hash = this.manifest?.get(filePath);
18807
18826
  if (!hash) {
18808
18827
  return null;
18809
18828
  }
18810
18829
  const cache = this.getCache();
18811
- let config = await cache.get(`config:${hash}`);
18812
- if (config) {
18813
- const themeId = this.getThemeId();
18814
- if (themeId && config.file?.url) {
18815
- const fileUrl = await cache.get(
18816
- `file:${themeId}:${config.hash}`
18817
- );
18818
- if (fileUrl) {
18819
- config = { ...config, file: { ...config.file, url: fileUrl } };
18820
- }
18830
+ const themeId = this.getThemeId();
18831
+ const [themeConfig, fileUrl] = await Promise2.all([
18832
+ cache.get(`config:${hash}`),
18833
+ themeId ? cache.get(`file:${themeId}:${hash}`) : void 0
18834
+ ]);
18835
+ if (themeConfig) {
18836
+ let config2 = themeConfig;
18837
+ if (fileUrl && themeConfig.file?.url) {
18838
+ config2 = {
18839
+ ...themeConfig,
18840
+ file: { ...themeConfig.file, url: fileUrl }
18841
+ };
18821
18842
  }
18822
- this.configs.set(filePath, config);
18823
- return config;
18843
+ this.configs.set(filePath, config2);
18844
+ return config2;
18824
18845
  }
18825
18846
  return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18826
18847
  }
@@ -18869,18 +18890,20 @@ var ThemeLoader = class _ThemeLoader {
18869
18890
  await Promise2.map(
18870
18891
  manifest.values(),
18871
18892
  async (configHash) => {
18872
- let config = await cache.get(`config:${configHash}`);
18873
- if (!config) {
18893
+ const [themeConfig, fileUrl] = await Promise2.all([
18894
+ cache.get(`config:${configHash}`),
18895
+ themeId ? cache.get(`file:${themeId}:${configHash}`) : void 0
18896
+ ]);
18897
+ if (!themeConfig) {
18874
18898
  configHashesUnresolved.push(configHash);
18875
18899
  return;
18876
18900
  }
18877
- if (themeId && config.file?.url) {
18878
- const fileUrl = await cache.get(
18879
- `file:${themeId}:${configHash}`
18880
- );
18881
- if (fileUrl) {
18882
- config = { ...config, file: { ...config.file, url: fileUrl } };
18883
- }
18901
+ let config = themeConfig;
18902
+ if (fileUrl && themeConfig.file?.url) {
18903
+ config = {
18904
+ ...themeConfig,
18905
+ file: { ...themeConfig.file, url: fileUrl }
18906
+ };
18884
18907
  }
18885
18908
  configsByHash.set(config.hash, config);
18886
18909
  this.configs.set(config.file_path, config);
@@ -19264,13 +19287,15 @@ var SwellTheme3 = class {
19264
19287
  const [cart, account] = await Promise.all([
19265
19288
  this.fetchSingletonResourceCached(
19266
19289
  "cart",
19290
+ // The cached cart may be null, but we need the StorefrontResource
19267
19291
  () => this.fetchCart(),
19268
- {}
19292
+ // Default value (always StorefrontResource)
19293
+ () => this.fetchCart()
19269
19294
  ),
19270
19295
  this.fetchSingletonResourceCached(
19271
19296
  "account",
19272
19297
  () => this.fetchAccount(),
19273
- null
19298
+ () => null
19274
19299
  )
19275
19300
  ]);
19276
19301
  if (!cart) {
@@ -19293,13 +19318,14 @@ var SwellTheme3 = class {
19293
19318
  async fetchSingletonResourceCached(key, handler, defaultValue) {
19294
19319
  const cacheKey = this.swell.storefront.session.getCookie();
19295
19320
  if (!cacheKey) {
19296
- return defaultValue;
19321
+ return defaultValue();
19297
19322
  }
19298
- return this.swell.getCachedResource(
19323
+ const result = await this.swell.getCachedResource(
19299
19324
  `${key}-${cacheKey}`,
19300
19325
  [],
19301
- () => handler()
19326
+ handler
19302
19327
  );
19328
+ return result ?? defaultValue();
19303
19329
  }
19304
19330
  async fetchCart() {
19305
19331
  const CartResource = this.resources?.singletons?.cart;