@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.mjs CHANGED
@@ -17441,14 +17441,14 @@ function getRobotsGlobals(canonicalUrl) {
17441
17441
  default_groups: [
17442
17442
  {
17443
17443
  user_agent: RobotsRule.from("User-agent", "*"),
17444
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17444
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17445
17445
  rules: defaultRules.map(
17446
17446
  (rule) => RobotsRule.from(rule.directive, rule.value)
17447
17447
  )
17448
17448
  },
17449
17449
  {
17450
17450
  user_agent: RobotsRule.from("User-agent", "AhrefsBot"),
17451
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17451
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17452
17452
  rules: [
17453
17453
  RobotsRule.from("Crawl-delay", "10"),
17454
17454
  ...defaultRules.map(
@@ -17458,7 +17458,7 @@ function getRobotsGlobals(canonicalUrl) {
17458
17458
  },
17459
17459
  {
17460
17460
  user_agent: RobotsRule.from("User-agent", "AhrefsSiteAudit"),
17461
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17461
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17462
17462
  rules: [
17463
17463
  RobotsRule.from("Crawl-delay", "10"),
17464
17464
  ...defaultRules.map(
@@ -19418,6 +19418,35 @@ var DEFAULT_CACHE_RULES = {
19418
19418
  },
19419
19419
  pathRules: [{ path: "/checkout/*", skip: true }]
19420
19420
  };
19421
+ var SANITIZE_CLIENT_HEADERS = Object.freeze([
19422
+ "connection",
19423
+ "proxy-connection",
19424
+ "keep-alive",
19425
+ "transfer-encoding",
19426
+ "upgrade",
19427
+ "proxy-authenticate",
19428
+ "proxy-authorization",
19429
+ "te",
19430
+ "trailers",
19431
+ "via",
19432
+ "alt-svc",
19433
+ "content-length",
19434
+ "content-encoding",
19435
+ // Also strip any legacy internal metadata keys
19436
+ "x-original-ttl",
19437
+ "x-original-swr",
19438
+ "x-cache-time"
19439
+ ]);
19440
+ var CACHE_KEY_HEADERS = Object.freeze([
19441
+ "cookie",
19442
+ "swell-storefront-id",
19443
+ "swell-app-id",
19444
+ "swell-access-token",
19445
+ "swell-theme-version-hash",
19446
+ "swell-cache-modified",
19447
+ "x-locale",
19448
+ "swell-storefront-context"
19449
+ ]);
19421
19450
  var HtmlCache = class {
19422
19451
  epoch;
19423
19452
  backend;
@@ -19425,7 +19454,23 @@ var HtmlCache = class {
19425
19454
  constructor(epoch, backend, cacheRules = DEFAULT_CACHE_RULES) {
19426
19455
  this.epoch = epoch;
19427
19456
  this.backend = backend;
19428
- this.cacheRules = cacheRules;
19457
+ this.cacheRules = {
19458
+ ...cacheRules,
19459
+ pathRules: cacheRules.pathRules?.map((rule) => ({
19460
+ ...rule,
19461
+ regex: this.convertPathToRegex(rule.path)
19462
+ }))
19463
+ };
19464
+ }
19465
+ /**
19466
+ * Converts wildcard pattern to regex and tests against path.
19467
+ *
19468
+ * - `*` matches any characters except `/`
19469
+ * - `**` matches any characters including `/`
19470
+ */
19471
+ convertPathToRegex(pattern) {
19472
+ const regex = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "___DOUBLE_STAR___").replace(/\*/g, "[^/]*").replace(/___DOUBLE_STAR___/g, ".*");
19473
+ return new RegExp(`^${regex}$`);
19429
19474
  }
19430
19475
  async get(request) {
19431
19476
  const trace = createTraceId();
@@ -19516,18 +19561,18 @@ var HtmlCache = class {
19516
19561
  );
19517
19562
  return;
19518
19563
  }
19519
- const cacheTimeISO = (/* @__PURE__ */ new Date()).toISOString();
19564
+ const date = /* @__PURE__ */ new Date();
19520
19565
  const headers = this.normalizeHeaders(response.headers);
19521
19566
  const entry = {
19522
19567
  status: response.status,
19523
19568
  statusText: response.statusText,
19524
19569
  headers,
19525
19570
  body,
19526
- cacheTimeISO,
19571
+ cacheTimeISO: date.toISOString(),
19527
19572
  ttl,
19528
19573
  swr,
19529
19574
  etag: this.quoteETag(headers["etag"] || md5(body)),
19530
- lastModifiedUTC: headers["last-modified"] || new Date(cacheTimeISO).toUTCString()
19575
+ lastModifiedUTC: headers["last-modified"] || date.toUTCString()
19531
19576
  };
19532
19577
  const hardExpireSeconds = ttl + swr;
19533
19578
  await this.backend.write(cacheKey, entry, hardExpireSeconds);
@@ -19552,8 +19597,13 @@ var HtmlCache = class {
19552
19597
  }
19553
19598
  }
19554
19599
  canReadFromCache(request) {
19555
- const method = request.method.toUpperCase();
19556
- if (!(method === "GET" || method === "HEAD")) return false;
19600
+ switch (request.method.toUpperCase()) {
19601
+ case "GET":
19602
+ case "HEAD":
19603
+ break;
19604
+ default:
19605
+ return false;
19606
+ }
19557
19607
  const res = this.resolvePathRule(request);
19558
19608
  if (res.skip) return false;
19559
19609
  return this.isRequestCacheable(request);
@@ -19619,7 +19669,7 @@ var HtmlCache = class {
19619
19669
  try {
19620
19670
  const ifModDate = new Date(ifModifiedSince);
19621
19671
  const lastModDate = new Date(lastModified);
19622
- if (isNaN(ifModDate.getTime()) || isNaN(lastModDate.getTime())) {
19672
+ if (Number.isNaN(ifModDate.getTime()) || Number.isNaN(lastModDate.getTime())) {
19623
19673
  return false;
19624
19674
  }
19625
19675
  return ifModDate >= lastModDate;
@@ -19649,31 +19699,25 @@ var HtmlCache = class {
19649
19699
  return `"${value}"`;
19650
19700
  }
19651
19701
  sanitizeClientHeaders(headers) {
19652
- const HOP_BY_HOP = [
19653
- "connection",
19654
- "proxy-connection",
19655
- "keep-alive",
19656
- "transfer-encoding",
19657
- "upgrade",
19658
- "proxy-authenticate",
19659
- "proxy-authorization",
19660
- "te",
19661
- "trailers",
19662
- "via",
19663
- "alt-svc",
19664
- "content-length"
19665
- ];
19666
- for (const h of HOP_BY_HOP) headers.delete(h);
19667
- headers.delete("content-encoding");
19668
- headers.delete("x-original-ttl");
19669
- headers.delete("x-original-swr");
19670
- headers.delete("x-cache-time");
19702
+ for (const name of SANITIZE_CLIENT_HEADERS) {
19703
+ headers.delete(name);
19704
+ }
19705
+ }
19706
+ getCacheKeyHeaders(headers) {
19707
+ const acc = new Headers();
19708
+ for (const name of CACHE_KEY_HEADERS) {
19709
+ const value = headers.get(name);
19710
+ if (value) {
19711
+ acc.set(name, value);
19712
+ }
19713
+ }
19714
+ return acc;
19671
19715
  }
19672
19716
  generateVersionHash(headers) {
19673
- const swellData = this.extractSwellData(headers);
19717
+ const swellData = this.extractSwellData(headers.get("cookie"));
19674
19718
  const versionFactors = {
19675
19719
  store: headers.get("swell-storefront-id") || "",
19676
- app: (headers.get("swell-app-id") || "") + "@" + (headers.get("host") || ""),
19720
+ app: headers.get("swell-app-id") || "",
19677
19721
  auth: headers.get("swell-access-token") || "",
19678
19722
  theme: headers.get("swell-theme-version-hash") || "",
19679
19723
  modified: headers.get("swell-cache-modified") || "",
@@ -19684,8 +19728,7 @@ var HtmlCache = class {
19684
19728
  };
19685
19729
  return md5(JSON.stringify(versionFactors));
19686
19730
  }
19687
- extractSwellData(headers) {
19688
- const cookie = headers.get("cookie");
19731
+ extractSwellData(cookie) {
19689
19732
  if (!cookie) return {};
19690
19733
  const swellDataMatch = cookie.match(/swell-data=([^;]+)/);
19691
19734
  if (!swellDataMatch) return {};
@@ -19737,15 +19780,6 @@ var HtmlCache = class {
19737
19780
  const age = (Date.now() - t) / 1e3;
19738
19781
  return age < 0 ? 0 : age;
19739
19782
  }
19740
- /**
19741
- * Converts wildcard pattern to regex and tests against path.
19742
- * - * matches any characters except /
19743
- * - ** matches any characters including /
19744
- */
19745
- pathMatches(pattern, path) {
19746
- const regex = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "___DOUBLE_STAR___").replace(/\*/g, "[^/]*").replace(/___DOUBLE_STAR___/g, ".*");
19747
- return new RegExp(`^${regex}$`).test(path);
19748
- }
19749
19783
  normalizeHeaders(headers) {
19750
19784
  const normalized = {};
19751
19785
  headers.forEach((value, key) => {
@@ -19756,16 +19790,10 @@ var HtmlCache = class {
19756
19790
  resolvePathRule(request) {
19757
19791
  const url = new URL(request.url);
19758
19792
  const mode = this.getDeploymentMode(request.headers);
19759
- const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults[mode];
19760
- let rule;
19761
- if (this.cacheRules.pathRules) {
19762
- for (const r of this.cacheRules.pathRules) {
19763
- if (this.pathMatches(r.path, url.pathname)) {
19764
- rule = r;
19765
- break;
19766
- }
19767
- }
19768
- }
19793
+ const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults?.[mode];
19794
+ const rule = this.cacheRules.pathRules?.find(
19795
+ (r) => r.regex.test(url.pathname)
19796
+ );
19769
19797
  const effectiveTTL = (rule?.ttl !== void 0 ? rule.ttl : defaults?.ttl) ?? defaults.ttl;
19770
19798
  const effectiveSWR = (rule?.swr !== void 0 ? rule.swr : defaults?.swr) ?? defaults.swr;
19771
19799
  return {
@@ -19777,27 +19805,29 @@ var HtmlCache = class {
19777
19805
  };
19778
19806
  }
19779
19807
  normalizeSearchParams(searchParams) {
19780
- const ignoredParams = [
19781
- "utm_source",
19782
- "utm_medium",
19783
- "utm_campaign",
19784
- "utm_content",
19785
- "utm_term",
19786
- "fbclid",
19787
- "gclid",
19788
- "gbraid",
19789
- "wbraid",
19790
- "ref",
19791
- "source",
19792
- "mc_cid",
19793
- "mc_eid"
19794
- ];
19795
19808
  const relevantParams = [];
19796
19809
  searchParams.forEach((value, key) => {
19797
- if (!ignoredParams.includes(key)) {
19798
- relevantParams.push(
19799
- `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
19800
- );
19810
+ switch (key) {
19811
+ case "utm_source":
19812
+ case "utm_medium":
19813
+ case "utm_campaign":
19814
+ case "utm_content":
19815
+ case "utm_term":
19816
+ case "fbclid":
19817
+ case "gclid":
19818
+ case "gbraid":
19819
+ case "wbraid":
19820
+ case "ref":
19821
+ case "source":
19822
+ case "mc_cid":
19823
+ case "mc_eid":
19824
+ break;
19825
+ default: {
19826
+ relevantParams.push(
19827
+ `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
19828
+ );
19829
+ break;
19830
+ }
19801
19831
  }
19802
19832
  });
19803
19833
  return relevantParams.sort().join("&");