@swell/apps-sdk 1.0.183 → 1.0.184

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
@@ -17460,14 +17460,14 @@ ${injects.join("\n")}<\/script>`;
17460
17460
  default_groups: [
17461
17461
  {
17462
17462
  user_agent: RobotsRule.from("User-agent", "*"),
17463
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17463
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17464
17464
  rules: defaultRules.map(
17465
17465
  (rule) => RobotsRule.from(rule.directive, rule.value)
17466
17466
  )
17467
17467
  },
17468
17468
  {
17469
17469
  user_agent: RobotsRule.from("User-agent", "AhrefsBot"),
17470
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17470
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17471
17471
  rules: [
17472
17472
  RobotsRule.from("Crawl-delay", "10"),
17473
17473
  ...defaultRules.map(
@@ -17477,7 +17477,7 @@ ${injects.join("\n")}<\/script>`;
17477
17477
  },
17478
17478
  {
17479
17479
  user_agent: RobotsRule.from("User-agent", "AhrefsSiteAudit"),
17480
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17480
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17481
17481
  rules: [
17482
17482
  RobotsRule.from("Crawl-delay", "10"),
17483
17483
  ...defaultRules.map(
@@ -19437,6 +19437,35 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19437
19437
  },
19438
19438
  pathRules: [{ path: "/checkout/*", skip: true }]
19439
19439
  };
19440
+ var SANITIZE_CLIENT_HEADERS = Object.freeze([
19441
+ "connection",
19442
+ "proxy-connection",
19443
+ "keep-alive",
19444
+ "transfer-encoding",
19445
+ "upgrade",
19446
+ "proxy-authenticate",
19447
+ "proxy-authorization",
19448
+ "te",
19449
+ "trailers",
19450
+ "via",
19451
+ "alt-svc",
19452
+ "content-length",
19453
+ "content-encoding",
19454
+ // Also strip any legacy internal metadata keys
19455
+ "x-original-ttl",
19456
+ "x-original-swr",
19457
+ "x-cache-time"
19458
+ ]);
19459
+ var CACHE_KEY_HEADERS = Object.freeze([
19460
+ "cookie",
19461
+ "swell-storefront-id",
19462
+ "swell-app-id",
19463
+ "swell-access-token",
19464
+ "swell-theme-version-hash",
19465
+ "swell-cache-modified",
19466
+ "x-locale",
19467
+ "swell-storefront-context"
19468
+ ]);
19440
19469
  var HtmlCache = class {
19441
19470
  epoch;
19442
19471
  backend;
@@ -19444,7 +19473,23 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19444
19473
  constructor(epoch, backend, cacheRules = DEFAULT_CACHE_RULES) {
19445
19474
  this.epoch = epoch;
19446
19475
  this.backend = backend;
19447
- this.cacheRules = cacheRules;
19476
+ this.cacheRules = {
19477
+ ...cacheRules,
19478
+ pathRules: cacheRules.pathRules?.map((rule) => ({
19479
+ ...rule,
19480
+ regex: this.convertPathToRegex(rule.path)
19481
+ }))
19482
+ };
19483
+ }
19484
+ /**
19485
+ * Converts wildcard pattern to regex and tests against path.
19486
+ *
19487
+ * - `*` matches any characters except `/`
19488
+ * - `**` matches any characters including `/`
19489
+ */
19490
+ convertPathToRegex(pattern) {
19491
+ const regex = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "___DOUBLE_STAR___").replace(/\*/g, "[^/]*").replace(/___DOUBLE_STAR___/g, ".*");
19492
+ return new RegExp(`^${regex}$`);
19448
19493
  }
19449
19494
  async get(request) {
19450
19495
  const trace = createTraceId();
@@ -19535,18 +19580,18 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19535
19580
  );
19536
19581
  return;
19537
19582
  }
19538
- const cacheTimeISO = (/* @__PURE__ */ new Date()).toISOString();
19583
+ const date = /* @__PURE__ */ new Date();
19539
19584
  const headers = this.normalizeHeaders(response.headers);
19540
19585
  const entry = {
19541
19586
  status: response.status,
19542
19587
  statusText: response.statusText,
19543
19588
  headers,
19544
19589
  body,
19545
- cacheTimeISO,
19590
+ cacheTimeISO: date.toISOString(),
19546
19591
  ttl,
19547
19592
  swr,
19548
19593
  etag: this.quoteETag(headers["etag"] || md5(body)),
19549
- lastModifiedUTC: headers["last-modified"] || new Date(cacheTimeISO).toUTCString()
19594
+ lastModifiedUTC: headers["last-modified"] || date.toUTCString()
19550
19595
  };
19551
19596
  const hardExpireSeconds = ttl + swr;
19552
19597
  await this.backend.write(cacheKey, entry, hardExpireSeconds);
@@ -19571,8 +19616,13 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19571
19616
  }
19572
19617
  }
19573
19618
  canReadFromCache(request) {
19574
- const method = request.method.toUpperCase();
19575
- if (!(method === "GET" || method === "HEAD")) return false;
19619
+ switch (request.method.toUpperCase()) {
19620
+ case "GET":
19621
+ case "HEAD":
19622
+ break;
19623
+ default:
19624
+ return false;
19625
+ }
19576
19626
  const res = this.resolvePathRule(request);
19577
19627
  if (res.skip) return false;
19578
19628
  return this.isRequestCacheable(request);
@@ -19638,7 +19688,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19638
19688
  try {
19639
19689
  const ifModDate = new Date(ifModifiedSince);
19640
19690
  const lastModDate = new Date(lastModified);
19641
- if (isNaN(ifModDate.getTime()) || isNaN(lastModDate.getTime())) {
19691
+ if (Number.isNaN(ifModDate.getTime()) || Number.isNaN(lastModDate.getTime())) {
19642
19692
  return false;
19643
19693
  }
19644
19694
  return ifModDate >= lastModDate;
@@ -19668,31 +19718,25 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19668
19718
  return `"${value}"`;
19669
19719
  }
19670
19720
  sanitizeClientHeaders(headers) {
19671
- const HOP_BY_HOP = [
19672
- "connection",
19673
- "proxy-connection",
19674
- "keep-alive",
19675
- "transfer-encoding",
19676
- "upgrade",
19677
- "proxy-authenticate",
19678
- "proxy-authorization",
19679
- "te",
19680
- "trailers",
19681
- "via",
19682
- "alt-svc",
19683
- "content-length"
19684
- ];
19685
- for (const h of HOP_BY_HOP) headers.delete(h);
19686
- headers.delete("content-encoding");
19687
- headers.delete("x-original-ttl");
19688
- headers.delete("x-original-swr");
19689
- headers.delete("x-cache-time");
19721
+ for (const name of SANITIZE_CLIENT_HEADERS) {
19722
+ headers.delete(name);
19723
+ }
19724
+ }
19725
+ getCacheKeyHeaders(headers) {
19726
+ const acc = new Headers();
19727
+ for (const name of CACHE_KEY_HEADERS) {
19728
+ const value = headers.get(name);
19729
+ if (value) {
19730
+ acc.set(name, value);
19731
+ }
19732
+ }
19733
+ return acc;
19690
19734
  }
19691
19735
  generateVersionHash(headers) {
19692
- const swellData = this.extractSwellData(headers);
19736
+ const swellData = this.extractSwellData(headers.get("cookie"));
19693
19737
  const versionFactors = {
19694
19738
  store: headers.get("swell-storefront-id") || "",
19695
- app: (headers.get("swell-app-id") || "") + "@" + (headers.get("host") || ""),
19739
+ app: headers.get("swell-app-id") || "",
19696
19740
  auth: headers.get("swell-access-token") || "",
19697
19741
  theme: headers.get("swell-theme-version-hash") || "",
19698
19742
  modified: headers.get("swell-cache-modified") || "",
@@ -19703,8 +19747,7 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19703
19747
  };
19704
19748
  return md5(JSON.stringify(versionFactors));
19705
19749
  }
19706
- extractSwellData(headers) {
19707
- const cookie = headers.get("cookie");
19750
+ extractSwellData(cookie) {
19708
19751
  if (!cookie) return {};
19709
19752
  const swellDataMatch = cookie.match(/swell-data=([^;]+)/);
19710
19753
  if (!swellDataMatch) return {};
@@ -19756,15 +19799,6 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19756
19799
  const age = (Date.now() - t) / 1e3;
19757
19800
  return age < 0 ? 0 : age;
19758
19801
  }
19759
- /**
19760
- * Converts wildcard pattern to regex and tests against path.
19761
- * - * matches any characters except /
19762
- * - ** matches any characters including /
19763
- */
19764
- pathMatches(pattern, path) {
19765
- const regex = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "___DOUBLE_STAR___").replace(/\*/g, "[^/]*").replace(/___DOUBLE_STAR___/g, ".*");
19766
- return new RegExp(`^${regex}$`).test(path);
19767
- }
19768
19802
  normalizeHeaders(headers) {
19769
19803
  const normalized = {};
19770
19804
  headers.forEach((value, key) => {
@@ -19775,16 +19809,10 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19775
19809
  resolvePathRule(request) {
19776
19810
  const url = new URL(request.url);
19777
19811
  const mode = this.getDeploymentMode(request.headers);
19778
- const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults[mode];
19779
- let rule;
19780
- if (this.cacheRules.pathRules) {
19781
- for (const r of this.cacheRules.pathRules) {
19782
- if (this.pathMatches(r.path, url.pathname)) {
19783
- rule = r;
19784
- break;
19785
- }
19786
- }
19787
- }
19812
+ const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults?.[mode];
19813
+ const rule = this.cacheRules.pathRules?.find(
19814
+ (r) => r.regex.test(url.pathname)
19815
+ );
19788
19816
  const effectiveTTL = (rule?.ttl !== void 0 ? rule.ttl : defaults?.ttl) ?? defaults.ttl;
19789
19817
  const effectiveSWR = (rule?.swr !== void 0 ? rule.swr : defaults?.swr) ?? defaults.swr;
19790
19818
  return {
@@ -19796,27 +19824,29 @@ ${this.shopifyCompatibility.getContentForHeader()}`;
19796
19824
  };
19797
19825
  }
19798
19826
  normalizeSearchParams(searchParams) {
19799
- const ignoredParams = [
19800
- "utm_source",
19801
- "utm_medium",
19802
- "utm_campaign",
19803
- "utm_content",
19804
- "utm_term",
19805
- "fbclid",
19806
- "gclid",
19807
- "gbraid",
19808
- "wbraid",
19809
- "ref",
19810
- "source",
19811
- "mc_cid",
19812
- "mc_eid"
19813
- ];
19814
19827
  const relevantParams = [];
19815
19828
  searchParams.forEach((value, key) => {
19816
- if (!ignoredParams.includes(key)) {
19817
- relevantParams.push(
19818
- `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
19819
- );
19829
+ switch (key) {
19830
+ case "utm_source":
19831
+ case "utm_medium":
19832
+ case "utm_campaign":
19833
+ case "utm_content":
19834
+ case "utm_term":
19835
+ case "fbclid":
19836
+ case "gclid":
19837
+ case "gbraid":
19838
+ case "wbraid":
19839
+ case "ref":
19840
+ case "source":
19841
+ case "mc_cid":
19842
+ case "mc_eid":
19843
+ break;
19844
+ default: {
19845
+ relevantParams.push(
19846
+ `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
19847
+ );
19848
+ break;
19849
+ }
19820
19850
  }
19821
19851
  });
19822
19852
  return relevantParams.sort().join("&");