@authhero/cloudflare-adapter 3.0.12 → 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.
@@ -142,7 +142,40 @@ async function ie(e, t, n) {
142
142
  let a = i.data.result.find((e) => e.hostname.toLowerCase() === n.toLowerCase());
143
143
  return !a || e.enterprise && a.custom_metadata?.tenant_id !== t ? null : a;
144
144
  }
145
- function ae(e) {
145
+ async function ae(e, t, n, r) {
146
+ let i = n.custom_domain_id, a;
147
+ try {
148
+ a = C({
149
+ ...n,
150
+ ...r
151
+ });
152
+ } catch (e) {
153
+ return console.warn(`[custom-domains] mapCustomDomainResponse failed for ${i} (tenant=${t}); keeping the stored row.`, e instanceof Error ? e.message : e), {
154
+ domain: n,
155
+ outcome: "failed"
156
+ };
157
+ }
158
+ if (a.status === n.status && JSON.stringify(a.verification) === JSON.stringify(n.verification)) return {
159
+ domain: a,
160
+ outcome: "unchanged"
161
+ };
162
+ try {
163
+ await e.customDomainAdapter.update(t, i, {
164
+ status: a.status,
165
+ verification: a.verification
166
+ });
167
+ } catch (e) {
168
+ return console.warn(`[custom-domains] DB writeback failed for ${i} (tenant=${t}); returning merged response without DB sync.`, e instanceof Error ? e.message : e), {
169
+ domain: a,
170
+ outcome: "failed"
171
+ };
172
+ }
173
+ return {
174
+ domain: a,
175
+ outcome: "updated"
176
+ };
177
+ }
178
+ function oe(e) {
146
179
  return {
147
180
  create: async (t, n) => {
148
181
  let { sslOverrides: r, rest: i } = S(n.domain_metadata);
@@ -199,23 +232,7 @@ function ae(e) {
199
232
  }), r;
200
233
  let { result: o } = a.data;
201
234
  if (e.enterprise && o.custom_metadata?.tenant_id !== t) throw new u(404);
202
- let s;
203
- try {
204
- s = C({
205
- ...r,
206
- ...o
207
- });
208
- } catch (e) {
209
- return console.warn(`[custom-domains] mapCustomDomainResponse failed for ${n} (tenant=${t}); returning stale DB row.`, e instanceof Error ? e.message : e), r;
210
- }
211
- if (s.status !== r.status || JSON.stringify(s.verification) !== JSON.stringify(r.verification)) try {
212
- await e.customDomainAdapter.update(t, n, {
213
- status: s.status,
214
- verification: s.verification
215
- });
216
- } catch (e) {
217
- console.warn(`[custom-domains] DB writeback failed for ${n} (tenant=${t}); returning merged response without DB sync.`, e instanceof Error ? e.message : e);
218
- }
235
+ let { domain: s } = await ae(e, t, r, o);
219
236
  return s;
220
237
  },
221
238
  getByDomain: async (t) => e.customDomainAdapter.getByDomain(t),
@@ -275,7 +292,7 @@ function ae(e) {
275
292
  }
276
293
  //#endregion
277
294
  //#region src/cache/index.ts
278
- var oe = class {
295
+ var se = class {
279
296
  config;
280
297
  cache = null;
281
298
  constructor(e) {
@@ -349,8 +366,8 @@ var oe = class {
349
366
  console.warn("CloudflareCache.clear() is not implemented - Cloudflare Cache API does not support clearing all entries");
350
367
  }
351
368
  };
352
- function se(e = {}) {
353
- return new oe({
369
+ function ce(e = {}) {
370
+ return new se({
354
371
  defaultTtlSeconds: 300,
355
372
  keyPrefix: "authhero",
356
373
  getTimeoutMs: 200,
@@ -359,9 +376,9 @@ function se(e = {}) {
359
376
  }
360
377
  //#endregion
361
378
  //#region ../../node_modules/.pnpm/nanoid@5.1.11/node_modules/nanoid/url-alphabet/index.js
362
- var ce = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict", w = (e = 21) => {
379
+ var le = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict", w = (e = 21) => {
363
380
  let t = "", n = crypto.getRandomValues(new Uint8Array(e |= 0));
364
- for (; e--;) t += ce[n[e] & 63];
381
+ for (; e--;) t += le[n[e] & 63];
365
382
  return t;
366
383
  };
367
384
  //#endregion
@@ -434,16 +451,16 @@ function O(e) {
434
451
  } : void 0
435
452
  };
436
453
  }
437
- function le(e) {
454
+ function ue(e) {
438
455
  return async (t, n) => {
439
456
  let r = n.log_id || w(), i = {
440
457
  ...n,
441
458
  log_id: r
442
459
  };
443
- return await ue(e, t, i), i;
460
+ return await de(e, t, i), i;
444
461
  };
445
462
  }
446
- async function ue(e, t, n) {
463
+ async function de(e, t, n) {
447
464
  let r = (e) => e ? JSON.stringify(e) : void 0, i = {
448
465
  id: n.log_id,
449
466
  tenant_id: t,
@@ -494,7 +511,7 @@ async function ue(e, t, n) {
494
511
  throw console.error("Failed to send log to Pipeline:", e), e;
495
512
  }
496
513
  }
497
- function de(e) {
514
+ function fe(e) {
498
515
  return async (t, n) => {
499
516
  let r = e.namespace || "default", i = e.tableName || "logs", a = await T(e, `
500
517
  SELECT * FROM ${D(r)}.${D(i)}
@@ -509,7 +526,7 @@ function de(e) {
509
526
  }
510
527
  //#endregion
511
528
  //#region src/r2-sql-logs/list.ts
512
- function fe(e) {
529
+ function pe(e) {
513
530
  let t = {}, n = /(\w+):(?:"([^"]*)"|(\S+))/g, r;
514
531
  for (; (r = n.exec(e)) !== null;) {
515
532
  let e = r[1], n = r[2] === void 0 ? r[3] : r[2];
@@ -517,7 +534,7 @@ function fe(e) {
517
534
  }
518
535
  return t;
519
536
  }
520
- function pe(e) {
537
+ function me(e) {
521
538
  let t = [];
522
539
  for (let [n, r] of Object.entries(e)) {
523
540
  let e = n.replace(/[^a-zA-Z0-9_]/g, "");
@@ -525,12 +542,12 @@ function pe(e) {
525
542
  }
526
543
  return t;
527
544
  }
528
- function me(e) {
545
+ function he(e) {
529
546
  return async (t, n = {}) => {
530
547
  let { page: r = 0, per_page: i = 50, include_totals: a = !1, sort: o, q: s } = n, c = e.namespace || "default", l = e.tableName || "logs", u = [`tenant_id = ${E(t)}`];
531
548
  if (s) {
532
- let e = fe(s);
533
- u.push(...pe(e));
549
+ let e = pe(s);
550
+ u.push(...me(e));
534
551
  }
535
552
  let d = u.join(" AND "), f = "ORDER BY date DESC";
536
553
  if (o && o.sort_by) {
@@ -563,7 +580,7 @@ function me(e) {
563
580
  }
564
581
  //#endregion
565
582
  //#region src/r2-sql-logs/stats.ts
566
- function he() {
583
+ function ge() {
567
584
  return {
568
585
  async getDaily() {
569
586
  throw Error("Stats queries are not supported by R2 SQL logs adapter. Use Analytics Engine or Kysely adapter instead.");
@@ -575,20 +592,20 @@ function he() {
575
592
  }
576
593
  //#endregion
577
594
  //#region src/r2-sql-logs/index.ts
578
- function k(e) {
595
+ function _e(e) {
579
596
  let t = !!e.pipelineEndpoint, n = !!e.pipelineBinding;
580
597
  if (!t && !n) throw Error("R2 SQL logs adapter requires one of: \"pipelineEndpoint\" or \"pipelineBinding\"");
581
598
  if (!e.authToken) throw Error("R2 SQL logs adapter requires \"authToken\" configuration");
582
599
  if (!e.warehouseName) throw Error("R2 SQL logs adapter requires \"warehouseName\" configuration");
583
600
  return {
584
- create: le(e),
585
- list: me(e),
586
- get: de(e)
601
+ create: ue(e),
602
+ list: he(e),
603
+ get: fe(e)
587
604
  };
588
605
  }
589
606
  //#endregion
590
607
  //#region src/analytics-engine-logs/query.ts
591
- async function A(e, t) {
608
+ async function k(e, t) {
592
609
  let n = e.timeout || 3e4, r = new AbortController(), i = setTimeout(() => r.abort(), n);
593
610
  try {
594
611
  let n = `https://api.cloudflare.com/client/v4/accounts/${e.accountId}/analytics_engine/sql`, i = await fetch(n, {
@@ -611,15 +628,15 @@ async function A(e, t) {
611
628
  clearTimeout(i);
612
629
  }
613
630
  }
614
- function j(e) {
631
+ function A(e) {
615
632
  return `'${e.replace(/'/g, "''").replace(/\\/g, "\\\\")}'`;
616
633
  }
617
- function M(e) {
634
+ function j(e) {
618
635
  return `"${e.replace(/"/g, "\"\"")}"`;
619
636
  }
620
637
  //#endregion
621
638
  //#region src/analytics-engine-logs/logs.ts
622
- function N(e) {
639
+ function M(e) {
623
640
  let t = (e) => {
624
641
  if (e) try {
625
642
  return JSON.parse(e);
@@ -651,16 +668,16 @@ function N(e) {
651
668
  isMobile: e.double1 === 1
652
669
  };
653
670
  }
654
- function ge(e) {
671
+ function ve(e) {
655
672
  return async (t, n) => {
656
673
  let r = n.log_id || w(), i = {
657
674
  ...n,
658
675
  log_id: r
659
676
  };
660
- return _e(e, t, i), i;
677
+ return ye(e, t, i), i;
661
678
  };
662
679
  }
663
- function _e(e, t, n) {
680
+ function ye(e, t, n) {
664
681
  if (!e.analyticsEngineBinding) {
665
682
  console.error("Analytics Engine binding not configured");
666
683
  return;
@@ -697,34 +714,34 @@ function _e(e, t, n) {
697
714
  console.error("Failed to write log to Analytics Engine:", e);
698
715
  }
699
716
  }
700
- function P(e) {
717
+ function be(e) {
701
718
  return async (t, n) => {
702
- let r = await A(e, `
719
+ let r = await k(e, `
703
720
  SELECT *
704
- FROM ${M(e.dataset || "authhero_logs")}
705
- WHERE index1 = ${j(t)}
706
- AND blob1 = ${j(n)}
721
+ FROM ${j(e.dataset || "authhero_logs")}
722
+ WHERE index1 = ${A(t)}
723
+ AND blob1 = ${A(n)}
707
724
  LIMIT 1
708
725
  `);
709
- return r.length === 0 || !r[0] ? null : N(r[0]);
726
+ return r.length === 0 || !r[0] ? null : M(r[0]);
710
727
  };
711
728
  }
712
729
  //#endregion
713
730
  //#region src/analytics-engine-logs/list.ts
714
- function F(e) {
731
+ function N(e) {
715
732
  let t = e;
716
733
  return t.startsWith("\"") && t.endsWith("\"") && t.length > 1 && (t = t.slice(1, -1)), s(t);
717
734
  }
718
- function I(e) {
735
+ function xe(e) {
719
736
  let t = {}, n = [];
720
737
  for (let r of o(e)) {
721
738
  if (r === "OR" || r === "AND") continue;
722
739
  let e = r.match(/^(\w+):([\s\S]*)$/);
723
740
  if (e) {
724
- let n = e[1], r = F(e[2]);
741
+ let n = e[1], r = N(e[2]);
725
742
  t[n] || (t[n] = []), t[n].push(r);
726
743
  } else {
727
- let e = F(r);
744
+ let e = N(r);
728
745
  e && n.push(e);
729
746
  }
730
747
  }
@@ -733,7 +750,7 @@ function I(e) {
733
750
  terms: n
734
751
  };
735
752
  }
736
- function L(e) {
753
+ function P(e) {
737
754
  return {
738
755
  log_id: "blob1",
739
756
  tenant_id: "blob2",
@@ -754,7 +771,7 @@ function L(e) {
754
771
  hostname: "blob17"
755
772
  }[e] || null;
756
773
  }
757
- function ve(e) {
774
+ function Se(e) {
758
775
  let t = [];
759
776
  for (let [n, r] of Object.entries(e)) {
760
777
  let e = r.filter((e) => e !== "");
@@ -765,41 +782,41 @@ function ve(e) {
765
782
  n.length > 0 && t.push(`(${n.join(" OR ")})`);
766
783
  continue;
767
784
  }
768
- let i = L(n);
769
- if (i) if (e.length === 1) t.push(`${i} = ${j(e[0])}`);
785
+ let i = P(n);
786
+ if (i) if (e.length === 1) t.push(`${i} = ${A(e[0])}`);
770
787
  else {
771
- let n = e.map((e) => j(e)).join(", ");
788
+ let n = e.map((e) => A(e)).join(", ");
772
789
  t.push(`${i} IN (${n})`);
773
790
  }
774
791
  }
775
792
  return t;
776
793
  }
777
- function ye(e) {
794
+ function Ce(e) {
778
795
  return e.filter((e) => e !== "").map((e) => {
779
- let t = j(e), n = j(`%${e}%`);
796
+ let t = A(e), n = A(`%${e}%`);
780
797
  return `(blob7 = ${t} OR blob5 LIKE ${n} OR blob4 LIKE ${n})`;
781
798
  });
782
799
  }
783
- function be(e) {
784
- return e === "date" ? "double2" : L(e) || "timestamp";
800
+ function we(e) {
801
+ return e === "date" ? "double2" : P(e) || "timestamp";
785
802
  }
786
- function xe(e) {
803
+ function Te(e) {
787
804
  return async (t, n = {}) => {
788
- let { page: r = 0, per_page: i = 50, include_totals: a = !1, sort: o, q: s, from_date: c, to_date: l } = n, u = e.dataset || "authhero_logs", d = [`index1 = ${j(t)}`];
805
+ let { page: r = 0, per_page: i = 50, include_totals: a = !1, sort: o, q: s, from_date: c, to_date: l } = n, u = e.dataset || "authhero_logs", d = [`index1 = ${A(t)}`];
789
806
  if (s) {
790
- let { fields: e, terms: t } = I(s);
791
- d.push(...ve(e)), d.push(...ye(t));
807
+ let { fields: e, terms: t } = xe(s);
808
+ d.push(...Se(e)), d.push(...Ce(t));
792
809
  }
793
810
  typeof c == "number" && Number.isFinite(c) && d.push(`double2 >= ${Math.floor(c) * 1e3}`), typeof l == "number" && Number.isFinite(l) && d.push(`double2 <= ${Math.floor(l) * 1e3}`);
794
811
  let f = d.join(" AND "), p = "ORDER BY timestamp DESC";
795
- o && o.sort_by && (p = `ORDER BY ${be(o.sort_by)} ${o.sort_order === "asc" ? "ASC" : "DESC"}`);
796
- let m = r * i, h = `LIMIT ${i} OFFSET ${m}`, g = (await A(e, `
812
+ o && o.sort_by && (p = `ORDER BY ${we(o.sort_by)} ${o.sort_order === "asc" ? "ASC" : "DESC"}`);
813
+ let m = r * i, h = `LIMIT ${i} OFFSET ${m}`, g = (await k(e, `
797
814
  SELECT *
798
- FROM ${M(u)}
815
+ FROM ${j(u)}
799
816
  WHERE ${f}
800
817
  ${p}
801
818
  ${h}
802
- `)).map(N);
819
+ `)).map(M);
803
820
  if (!a) return {
804
821
  logs: g,
805
822
  start: 0,
@@ -808,11 +825,11 @@ function xe(e) {
808
825
  };
809
826
  let _ = `
810
827
  SELECT count() as count
811
- FROM ${M(u)}
828
+ FROM ${j(u)}
812
829
  WHERE ${f}
813
830
  `;
814
831
  try {
815
- let t = (await A(e, _))[0]?.count || 0;
832
+ let t = (await k(e, _))[0]?.count || 0;
816
833
  return {
817
834
  logs: g,
818
835
  start: m,
@@ -831,14 +848,14 @@ function xe(e) {
831
848
  }
832
849
  //#endregion
833
850
  //#region src/analytics-engine-logs/stats.ts
834
- var R = ["s"], Se = ["pwd_leak"];
835
- function z(e) {
851
+ var F = ["s"], Ee = ["pwd_leak"];
852
+ function I(e) {
836
853
  return /^\d{8}$/.test(e) ? `${e.slice(0, 4)}-${e.slice(4, 6)}-${e.slice(6, 8)}` : e;
837
854
  }
838
- function B(e) {
855
+ function L(e) {
839
856
  return e.toISOString().split("T")[0];
840
857
  }
841
- function V(e, t) {
858
+ function R(e, t) {
842
859
  let n = (/* @__PURE__ */ new Date(`${e}T${t === "end" ? "23:59:59.999" : "00:00:00.000"}Z`)).getTime();
843
860
  if (Number.isNaN(n)) return NaN;
844
861
  let r = /^(\d{4})-(\d{2})-(\d{2})$/.exec(e);
@@ -846,24 +863,24 @@ function V(e, t) {
846
863
  let i = new Date(n);
847
864
  return i.getUTCFullYear() !== Number(r[1]) || i.getUTCMonth() + 1 !== Number(r[2]) || i.getUTCDate() !== Number(r[3]) ? NaN : n;
848
865
  }
849
- function H(e) {
866
+ function z(e) {
850
867
  let t = e.dataset || "authhero_logs";
851
868
  return {
852
869
  async getDaily(n, r = {}) {
853
870
  let { from: i, to: a } = r, o = /* @__PURE__ */ new Date(), s = new Date(o);
854
871
  s.setDate(s.getDate() - 30);
855
- let c = i ? z(i) : B(s), l = a ? z(a) : B(o), u = V(c, "start"), d = V(l, "end");
872
+ let c = i ? I(i) : L(s), l = a ? I(a) : L(o), u = R(c, "start"), d = R(l, "end");
856
873
  if (Number.isNaN(u) || Number.isNaN(d)) throw Error(`Invalid stats date range: from='${c}' to='${l}'`);
857
- return (await A(e, `
874
+ return (await k(e, `
858
875
  SELECT
859
876
  toDate(toDateTime(double2 / 1000)) AS date,
860
- SUM(CASE WHEN blob3 IN (${R.map((e) => j(e)).join(", ")}) THEN 1 ELSE 0 END) AS logins,
877
+ SUM(CASE WHEN blob3 IN (${F.map((e) => A(e)).join(", ")}) THEN 1 ELSE 0 END) AS logins,
861
878
  SUM(CASE WHEN blob3 = 'ss' THEN 1 ELSE 0 END) AS signups,
862
- SUM(CASE WHEN blob3 IN (${Se.map((e) => j(e)).join(", ")}) THEN 1 ELSE 0 END) AS leaked_passwords,
879
+ SUM(CASE WHEN blob3 IN (${Ee.map((e) => A(e)).join(", ")}) THEN 1 ELSE 0 END) AS leaked_passwords,
863
880
  MIN(double2) AS first_event,
864
881
  MAX(double2) AS last_event
865
882
  FROM "${t}"
866
- WHERE index1 = ${j(n)}
883
+ WHERE index1 = ${A(n)}
867
884
  AND double2 >= ${u}
868
885
  AND double2 <= ${d}
869
886
  GROUP BY date
@@ -880,10 +897,10 @@ function H(e) {
880
897
  async getActiveUsers(n) {
881
898
  let r = /* @__PURE__ */ new Date();
882
899
  r.setDate(r.getDate() - 30);
883
- let i = r.getTime(), a = R.map((e) => j(e)).join(", "), o = (await A(e, `
900
+ let i = r.getTime(), a = F.map((e) => A(e)).join(", "), o = (await k(e, `
884
901
  SELECT COUNT(DISTINCT blob7) AS count
885
902
  FROM "${t}"
886
- WHERE index1 = ${j(n)}
903
+ WHERE index1 = ${A(n)}
887
904
  AND double2 >= ${i}
888
905
  AND blob3 IN (${a})
889
906
  AND blob7 IS NOT NULL
@@ -895,7 +912,7 @@ function H(e) {
895
912
  }
896
913
  //#endregion
897
914
  //#region src/analytics-engine-logs/analytics.ts
898
- var Ce = {
915
+ var De = {
899
916
  "active-users": ["s", "seacft"],
900
917
  logins: [
901
918
  "s",
@@ -925,17 +942,17 @@ var Ce = {
925
942
  "fvr"
926
943
  ],
927
944
  "codes-sent": ["cls", "cs"]
928
- }, we = {
945
+ }, Oe = {
929
946
  connection: "blob9",
930
947
  client_id: "blob11",
931
948
  user_type: "blob16",
932
949
  event: "blob3"
933
- }, Te = {
950
+ }, ke = {
934
951
  connection: "blob9",
935
952
  client_id: "blob11",
936
953
  user_type: "blob16",
937
954
  user_id: "blob7"
938
- }, Ee = {
955
+ }, Ae = {
939
956
  "active-users": {
940
957
  expr: "count(DISTINCT blob7)",
941
958
  alias: "active_users",
@@ -992,8 +1009,8 @@ var Ce = {
992
1009
  type: "UInt64"
993
1010
  }
994
1011
  };
995
- function De(e, t) {
996
- let n = `toDateTime(intDiv(double2, 1000), ${j(t)})`;
1012
+ function je(e, t) {
1013
+ let n = `toDateTime(intDiv(double2, 1000), ${A(t)})`;
997
1014
  switch (e) {
998
1015
  case "hour": return `toStartOfHour(${n})`;
999
1016
  case "week": return `toStartOfWeek(${n})`;
@@ -1001,45 +1018,45 @@ function De(e, t) {
1001
1018
  default: return `toStartOfDay(${n})`;
1002
1019
  }
1003
1020
  }
1004
- function Oe(e) {
1021
+ function Me(e) {
1005
1022
  return e === "hour" ? "DateTime" : "Date";
1006
1023
  }
1007
- function ke(e, t, n) {
1008
- let r = new Date(e.from).getTime(), i = new Date(e.to).getTime(), a = n.map((e) => j(e)).join(", "), o = [
1009
- `index1 = ${j(t)}`,
1024
+ function Ne(e, t, n) {
1025
+ let r = new Date(e.from).getTime(), i = new Date(e.to).getTime(), a = n.map((e) => A(e)).join(", "), o = [
1026
+ `index1 = ${A(t)}`,
1010
1027
  `double2 >= ${r}`,
1011
1028
  `double2 < ${i}`,
1012
1029
  `blob3 IN (${a})`
1013
1030
  ];
1014
- for (let [t, n] of Object.entries(Te)) {
1031
+ for (let [t, n] of Object.entries(ke)) {
1015
1032
  let r = e.filters[t];
1016
1033
  if (r && r.length > 0) {
1017
- let e = r.map((e) => j(e)).join(", ");
1034
+ let e = r.map((e) => A(e)).join(", ");
1018
1035
  o.push(`${n} IN (${e})`);
1019
1036
  }
1020
1037
  }
1021
1038
  return o.join(" AND ");
1022
1039
  }
1023
- function Ae(e, t, n) {
1040
+ function Pe(e, t, n) {
1024
1041
  if (e.order_by) {
1025
1042
  let t = e.order_by.startsWith("-");
1026
- return `${M(t ? e.order_by.slice(1) : e.order_by)} ${t ? "DESC" : "ASC"}`;
1043
+ return `${j(t ? e.order_by.slice(1) : e.order_by)} ${t ? "DESC" : "ASC"}`;
1027
1044
  }
1028
- return t.length > 0 && t[0] === "time" ? `${M("time")} ASC` : `${M(n)} DESC`;
1045
+ return t.length > 0 && t[0] === "time" ? `${j("time")} ASC` : `${j(n)} DESC`;
1029
1046
  }
1030
- function U(e) {
1047
+ function B(e) {
1031
1048
  let t = e.dataset || "authhero_logs";
1032
1049
  return { async query(n, r, i) {
1033
- let a = Ce[r], o = Ee[r], s = [], c = [];
1050
+ let a = De[r], o = Ae[r], s = [], c = [];
1034
1051
  for (let e of i.group_by) if (e === "time") {
1035
- let e = De(i.interval, i.tz);
1036
- s.push(`${e} AS ${M("time")}`), c.push({
1052
+ let e = je(i.interval, i.tz);
1053
+ s.push(`${e} AS ${j("time")}`), c.push({
1037
1054
  name: "time",
1038
- type: Oe(i.interval)
1055
+ type: Me(i.interval)
1039
1056
  });
1040
1057
  } else {
1041
- let t = we[e];
1042
- s.push(`${t} AS ${M(e)}`), c.push({
1058
+ let t = Oe[e];
1059
+ s.push(`${t} AS ${j(e)}`), c.push({
1043
1060
  name: e,
1044
1061
  type: "String"
1045
1062
  });
@@ -1048,10 +1065,10 @@ function U(e) {
1048
1065
  name: o.alias,
1049
1066
  type: o.type
1050
1067
  });
1051
- let l = [...s, `${o.expr} AS ${M(o.alias)}`], u = i.group_by.length ? `GROUP BY ${i.group_by.map((e) => M(e === "time" ? "time" : e)).join(", ")}` : "", d = Ae(i, i.group_by.map((e) => e === "time" ? "time" : e), o.alias), f = `
1068
+ let l = [...s, `${o.expr} AS ${j(o.alias)}`], u = i.group_by.length ? `GROUP BY ${i.group_by.map((e) => j(e === "time" ? "time" : e)).join(", ")}` : "", d = Pe(i, i.group_by.map((e) => e === "time" ? "time" : e), o.alias), f = `
1052
1069
  SELECT ${l.join(", ")}
1053
- FROM ${M(t)}
1054
- WHERE ${ke(i, n, a)}
1070
+ FROM ${j(t)}
1071
+ WHERE ${Ne(i, n, a)}
1055
1072
  ${u}
1056
1073
  ORDER BY ${d}
1057
1074
  LIMIT ${Math.max(0, i.limit)} OFFSET ${Math.max(0, i.offset)}
@@ -1088,47 +1105,47 @@ function U(e) {
1088
1105
  }
1089
1106
  //#endregion
1090
1107
  //#region src/analytics-engine-logs/index.ts
1091
- function W(e) {
1108
+ function V(e) {
1092
1109
  return e.analyticsEngineBinding || console.warn("Analytics Engine: No binding configured. Logs will not be written to Analytics Engine."), (!e.accountId || !e.apiToken) && console.warn("Analytics Engine: accountId and apiToken are required for querying logs via SQL API."), {
1093
- create: ge(e),
1094
- list: xe(e),
1095
- get: P(e)
1110
+ create: ve(e),
1111
+ list: Te(e),
1112
+ get: be(e)
1096
1113
  };
1097
1114
  }
1098
1115
  //#endregion
1099
1116
  //#region src/analytics-engine-action-executions/actionExecutions.ts
1100
- var je = "authhero_action_executions", Me = 1024, Ne = "[truncated]";
1101
- function G(e) {
1117
+ var Fe = "authhero_action_executions", Ie = 1024, Le = "[truncated]";
1118
+ function H(e) {
1102
1119
  if (!(typeof e != "string" || e.length === 0)) try {
1103
1120
  return JSON.parse(e);
1104
1121
  } catch {
1105
1122
  return;
1106
1123
  }
1107
1124
  }
1108
- function Pe(e) {
1125
+ function Re(e) {
1109
1126
  let t = a.safeParse(e);
1110
1127
  return t.success ? t.data : "unspecified";
1111
1128
  }
1112
- function K(e, t = Me) {
1113
- return e.length <= t ? e : `${e.substring(0, t - 11)}${Ne}`;
1129
+ function U(e, t = Ie) {
1130
+ return e.length <= t ? e : `${e.substring(0, t - 11)}${Le}`;
1114
1131
  }
1115
- function q(e) {
1132
+ function W(e) {
1116
1133
  return e == null ? "" : JSON.stringify(e);
1117
1134
  }
1118
- function Fe(e) {
1135
+ function ze(e) {
1119
1136
  let t = typeof e.blob6 == "string" && e.blob6.length > 0 ? e.blob6 : typeof e.double1 == "number" ? new Date(e.double1).toISOString() : "", n = typeof e.blob7 == "string" && e.blob7.length > 0 ? e.blob7 : t;
1120
1137
  return {
1121
1138
  id: typeof e.blob1 == "string" ? e.blob1 : "",
1122
1139
  tenant_id: typeof e.index1 == "string" ? e.index1 : "",
1123
1140
  trigger_id: typeof e.blob2 == "string" ? e.blob2 : "",
1124
- status: Pe(e.blob3),
1125
- results: G(e.blob4) ?? [],
1126
- logs: G(e.blob5),
1141
+ status: Re(e.blob3),
1142
+ results: H(e.blob4) ?? [],
1143
+ logs: H(e.blob5),
1127
1144
  created_at: t,
1128
1145
  updated_at: n
1129
1146
  };
1130
1147
  }
1131
- function Ie(e) {
1148
+ function Be(e) {
1132
1149
  return async (t, n) => {
1133
1150
  let r = Date.now(), i = new Date(r).toISOString(), a = {
1134
1151
  id: n.id,
@@ -1140,10 +1157,10 @@ function Ie(e) {
1140
1157
  created_at: i,
1141
1158
  updated_at: i
1142
1159
  };
1143
- return Le(e, t, a, r), a;
1160
+ return Ve(e, t, a, r), a;
1144
1161
  };
1145
1162
  }
1146
- function Le(e, t, n, r) {
1163
+ function Ve(e, t, n, r) {
1147
1164
  if (!e.analyticsEngineBinding) {
1148
1165
  console.error("Analytics Engine action_executions binding not configured; skipping write");
1149
1166
  return;
@@ -1151,13 +1168,13 @@ function Le(e, t, n, r) {
1151
1168
  try {
1152
1169
  e.analyticsEngineBinding.writeDataPoint({
1153
1170
  blobs: [
1154
- K(n.id),
1155
- K(n.trigger_id),
1156
- K(n.status),
1157
- K(q(n.results)),
1158
- K(q(n.logs)),
1159
- K(n.created_at),
1160
- K(n.updated_at)
1171
+ U(n.id),
1172
+ U(n.trigger_id),
1173
+ U(n.status),
1174
+ U(W(n.results)),
1175
+ U(W(n.logs)),
1176
+ U(n.created_at),
1177
+ U(n.updated_at)
1161
1178
  ],
1162
1179
  doubles: [r],
1163
1180
  indexes: [t.substring(0, 96)]
@@ -1166,29 +1183,29 @@ function Le(e, t, n, r) {
1166
1183
  console.error("Failed to write action_execution to Analytics Engine:", e);
1167
1184
  }
1168
1185
  }
1169
- function Re(e) {
1186
+ function He(e) {
1170
1187
  return async (t, n) => {
1171
- let r = await A(e, `
1188
+ let r = await k(e, `
1172
1189
  SELECT *
1173
- FROM ${M(e.dataset || je)}
1174
- WHERE index1 = ${j(t)}
1175
- AND blob1 = ${j(n)}
1190
+ FROM ${j(e.dataset || Fe)}
1191
+ WHERE index1 = ${A(t)}
1192
+ AND blob1 = ${A(n)}
1176
1193
  LIMIT 1
1177
1194
  `);
1178
- return r.length === 0 || !r[0] ? null : Fe(r[0]);
1195
+ return r.length === 0 || !r[0] ? null : ze(r[0]);
1179
1196
  };
1180
1197
  }
1181
1198
  //#endregion
1182
1199
  //#region src/analytics-engine-action-executions/index.ts
1183
- function J(e) {
1200
+ function G(e) {
1184
1201
  return e.analyticsEngineBinding || console.warn("Analytics Engine: No action_executions binding configured. Executions will not be written."), (!e.accountId || !e.apiToken) && console.warn("Analytics Engine: accountId and apiToken are required to read action_executions via the SQL API."), {
1185
- create: Ie(e),
1186
- get: Re(e)
1202
+ create: Be(e),
1203
+ get: He(e)
1187
1204
  };
1188
1205
  }
1189
1206
  //#endregion
1190
1207
  //#region src/geo/index.ts
1191
- function ze() {
1208
+ function Ue() {
1192
1209
  return { async getGeoInfo(e) {
1193
1210
  try {
1194
1211
  let t = e["cf-ipcountry"], n = e["cf-ipcity"], r = e["cf-iplatitude"], i = e["cf-iplongitude"], a = e["cf-timezone"], o = e["cf-ipcontinent"];
@@ -1207,7 +1224,7 @@ function ze() {
1207
1224
  }
1208
1225
  //#endregion
1209
1226
  //#region src/rate-limit/index.ts
1210
- var Be = class {
1227
+ var We = class {
1211
1228
  bindings;
1212
1229
  constructor(e) {
1213
1230
  this.bindings = e;
@@ -1223,12 +1240,61 @@ var Be = class {
1223
1240
  }
1224
1241
  }
1225
1242
  };
1226
- function Y(e) {
1227
- if (e && Object.keys(e).some((t) => e[t])) return new Be(e);
1243
+ function K(e) {
1244
+ if (e && Object.keys(e).some((t) => e[t])) return new We(e);
1245
+ }
1246
+ //#endregion
1247
+ //#region src/customDomains/sync.ts
1248
+ var Ge = 5, Ke = 1e3, q = 50;
1249
+ async function qe(e, t = {}) {
1250
+ let n = Math.trunc(t.perPage ?? q), r = Number.isFinite(n) ? Math.min(Ke, Math.max(Ge, n)) : q, i = t.maxPages ?? 200, a = {
1251
+ scanned: 0,
1252
+ matched: 0,
1253
+ updated: 0,
1254
+ unknown: 0,
1255
+ mismatched: 0,
1256
+ errors: 0
1257
+ };
1258
+ for (let t = 1; t <= i; t++) {
1259
+ let n;
1260
+ try {
1261
+ n = await x(e).get(`/custom_hostnames?page=${t}&per_page=${r}`).json();
1262
+ } catch (n) {
1263
+ return console.warn(`[custom-domains] sync failed to list page ${t} of zone ${e.zoneId}; stopping the sweep:`, n instanceof Error ? n.message : n), a.errors++, a;
1264
+ }
1265
+ let i = b.safeParse(n);
1266
+ if (!i.success || !i.data.success) return console.warn(`[custom-domains] sync got an unparseable listing for page ${t} of zone ${e.zoneId}; stopping the sweep.`, i.success ? { cfErrors: i.data.errors } : { zodIssues: i.error.issues }), a.errors++, a;
1267
+ let o = i.data.result;
1268
+ for (let t of o) {
1269
+ a.scanned++;
1270
+ try {
1271
+ let n = await e.customDomainAdapter.getByDomain(t.hostname);
1272
+ if (!n) {
1273
+ a.unknown++;
1274
+ continue;
1275
+ }
1276
+ if (e.enterprise && t.custom_metadata?.tenant_id !== n.tenant_id) {
1277
+ console.warn(`[custom-domains] sync skipping ${t.hostname}: zone says tenant=${t.custom_metadata?.tenant_id}, database says tenant=${n.tenant_id}.`), a.mismatched++;
1278
+ continue;
1279
+ }
1280
+ if (n.custom_domain_id !== t.id) {
1281
+ console.warn(`[custom-domains] sync skipping ${t.hostname} (tenant=${n.tenant_id}): stored id ${n.custom_domain_id} no longer matches the zone's ${t.id}.`), a.mismatched++;
1282
+ continue;
1283
+ }
1284
+ a.matched++;
1285
+ let { outcome: r } = await ae(e, n.tenant_id, n, t);
1286
+ r === "updated" ? a.updated++ : r === "failed" && a.errors++;
1287
+ } catch (e) {
1288
+ console.warn(`[custom-domains] sync failed for ${t.hostname}:`, e instanceof Error ? e.message : e), a.errors++;
1289
+ }
1290
+ }
1291
+ if (o.length < r) return a;
1292
+ }
1293
+ return console.warn(`[custom-domains] sync hit the ${i}-page limit for zone ${e.zoneId}; the tail of the zone was not swept.`), a;
1228
1294
  }
1229
1295
  //#endregion
1230
1296
  //#region src/code-executor/worker-template.ts
1231
- function X(e) {
1297
+ function J(e) {
1232
1298
  return `// Auto-generated AuthHero code hook worker
1233
1299
 
1234
1300
  const fnNames = {
@@ -1320,7 +1386,7 @@ export default {
1320
1386
  }
1321
1387
  //#endregion
1322
1388
  //#region src/code-executor/index.ts
1323
- var Z = class {
1389
+ var Y = class {
1324
1390
  config;
1325
1391
  constructor(e) {
1326
1392
  this.config = e;
@@ -1381,7 +1447,7 @@ var Z = class {
1381
1447
  }
1382
1448
  }
1383
1449
  async deploy(e, t) {
1384
- let n = `hook-${e}`, r = X(t), i = `https://api.cloudflare.com/client/v4/accounts/${this.config.accountId}/workers/dispatch/namespaces/${this.config.dispatchNamespace}/scripts/${n}`, a = JSON.stringify({
1450
+ let n = `hook-${e}`, r = J(t), i = `https://api.cloudflare.com/client/v4/accounts/${this.config.accountId}/workers/dispatch/namespaces/${this.config.dispatchNamespace}/scripts/${n}`, a = JSON.stringify({
1385
1451
  main_module: "index.js",
1386
1452
  compatibility_date: this.config.compatibilityDate ?? "2024-11-20"
1387
1453
  }), o = new FormData();
@@ -1406,21 +1472,21 @@ var Z = class {
1406
1472
  throw Error(`Failed to remove hook worker ${t}: ${r.status} ${e}`);
1407
1473
  }
1408
1474
  }
1409
- }, Ve = Z, He = {
1475
+ }, Je = Y, Ye = {
1410
1476
  "post-user-login": "onExecutePostLogin",
1411
1477
  "credentials-exchange": "onExecuteCredentialsExchange",
1412
1478
  "pre-user-registration": "onExecutePreUserRegistration",
1413
1479
  "post-user-registration": "onExecutePostUserRegistration"
1414
- }, Ue = i;
1415
- async function We(e) {
1480
+ }, Xe = i;
1481
+ async function Ze(e) {
1416
1482
  let t = new TextEncoder().encode(e), n = await crypto.subtle.digest("SHA-256", t), r = new Uint8Array(n), i = "";
1417
1483
  for (let e of r) i += e.toString(16).padStart(2, "0");
1418
1484
  return i;
1419
1485
  }
1420
- function Ge(e) {
1486
+ function Qe(e) {
1421
1487
  return `
1422
- const TRIGGER_FN_NAMES = ${JSON.stringify(He)};
1423
- const API_SHAPES = ${JSON.stringify(Ue)};
1488
+ const TRIGGER_FN_NAMES = ${JSON.stringify(Ye)};
1489
+ const API_SHAPES = ${JSON.stringify(Xe)};
1424
1490
  const MAX_LOG_ENTRIES = 50;
1425
1491
  const MAX_LOG_LENGTH = 500;
1426
1492
 
@@ -1520,7 +1586,7 @@ export default {
1520
1586
  };
1521
1587
  `;
1522
1588
  }
1523
- var Q = class {
1589
+ var $e = class {
1524
1590
  loader;
1525
1591
  compatibilityDate;
1526
1592
  constructor(e) {
@@ -1529,11 +1595,11 @@ var Q = class {
1529
1595
  async execute(e) {
1530
1596
  let t = Date.now();
1531
1597
  try {
1532
- let t = Ge(e.code), n = {
1598
+ let t = Qe(e.code), n = {
1533
1599
  compatibilityDate: this.compatibilityDate,
1534
1600
  mainModule: "hook.js",
1535
1601
  modules: { "hook.js": t }
1536
- }, r = e.hookCodeId ? `${e.hookCodeId}-${await We(e.code)}` : null;
1602
+ }, r = e.hookCodeId ? `${e.hookCodeId}-${await Ze(e.code)}` : null;
1537
1603
  return await (await (r ? this.loader.get(r, async () => n) : this.loader.load(n)).getEntrypoint().fetch(new Request("https://hook/execute", {
1538
1604
  method: "POST",
1539
1605
  headers: { "Content-Type": "application/json" },
@@ -1551,10 +1617,46 @@ var Q = class {
1551
1617
  };
1552
1618
  }
1553
1619
  }
1554
- };
1620
+ }, X = new TextEncoder();
1621
+ function Z(e, t) {
1622
+ if (X.encode(e).length <= t) return e;
1623
+ let n = 0, r = "";
1624
+ for (let i of e) {
1625
+ let e = X.encode(i).length;
1626
+ if (n + e > t) break;
1627
+ n += e, r += i;
1628
+ }
1629
+ return r;
1630
+ }
1631
+ var Q = (e, t = 1024) => Z(e, t);
1632
+ function et(e) {
1633
+ let t = e.analyticsEngineBinding;
1634
+ return (e) => {
1635
+ if (t) try {
1636
+ t.writeDataPoint({
1637
+ blobs: [
1638
+ Q(e.name),
1639
+ Q(e.tenantId),
1640
+ Q(e.eventType),
1641
+ Q(e.source),
1642
+ Q(e.destination ?? ""),
1643
+ Q(e.error ?? "")
1644
+ ],
1645
+ doubles: [
1646
+ e.value,
1647
+ e.retryCount ?? 0,
1648
+ Date.now()
1649
+ ],
1650
+ indexes: [Z(e.tenantId, 96)]
1651
+ });
1652
+ } catch (e) {
1653
+ console.error("Failed to write outbox metric to Analytics Engine:", e);
1654
+ }
1655
+ };
1656
+ }
1555
1657
  //#endregion
1556
1658
  //#region src/wfp-provisioner/provisioner.ts
1557
- function Ke(e) {
1659
+ function tt(e) {
1558
1660
  let t = n(e);
1559
1661
  return {
1560
1662
  async onProvision(e) {
@@ -1575,10 +1677,10 @@ function Ke(e) {
1575
1677
  }
1576
1678
  //#endregion
1577
1679
  //#region src/wfp-provisioner/tenant-hook.ts
1578
- function qe(e) {
1680
+ function nt(e) {
1579
1681
  return e.deployment_type === "wfp";
1580
1682
  }
1581
- var Je = 2048;
1683
+ var rt = 2048;
1582
1684
  function $(e) {
1583
1685
  let t;
1584
1686
  if (e instanceof Error) t = e.message;
@@ -1587,10 +1689,10 @@ function $(e) {
1587
1689
  } catch {
1588
1690
  t = Object.prototype.toString.call(e);
1589
1691
  }
1590
- return t.slice(0, Je);
1692
+ return t.slice(0, rt);
1591
1693
  }
1592
- function Ye(e) {
1593
- let { provisioner: t, tenants: n, syncDefaults: i } = e, a = e.shouldProvision ?? qe, o = e.logger;
1694
+ function it(e) {
1695
+ let { provisioner: t, tenants: n, syncDefaults: i } = e, a = e.shouldProvision ?? nt, o = e.logger;
1594
1696
  async function s(e, t) {
1595
1697
  try {
1596
1698
  await n.update(e, {
@@ -1684,15 +1786,15 @@ function Ye(e) {
1684
1786
  }
1685
1787
  //#endregion
1686
1788
  //#region src/wfp-provisioner/wfp-forward.ts
1687
- var Xe = "DISPATCHER", Ze = "tenant-{tenant_id}-auth", Qe = ["/u/widget/"];
1688
- function $e(e) {
1789
+ var at = "DISPATCHER", ot = "tenant-{tenant_id}-auth", st = ["/u/widget/"];
1790
+ function ct(e) {
1689
1791
  return typeof e == "object" && !!e && "get" in e && typeof Reflect.get(e, "get") == "function";
1690
1792
  }
1691
- function et(e, t) {
1793
+ function lt(e, t) {
1692
1794
  return e.replace(/\{tenant_id\}/g, t);
1693
1795
  }
1694
- function tt(e) {
1695
- let { tenants: t, controlPlaneTenantId: n, dispatcherBinding: r = Xe, scriptNameTemplate: i = Ze, resolveTenantId: a = (e) => e.req.header("tenant-id"), localPaths: o = Qe } = e;
1796
+ function ut(e) {
1797
+ let { tenants: t, controlPlaneTenantId: n, dispatcherBinding: r = at, scriptNameTemplate: i = ot, resolveTenantId: a = (e) => e.req.header("tenant-id"), localPaths: o = st } = e;
1696
1798
  return async (e, s) => {
1697
1799
  if (o.length > 0) {
1698
1800
  let t = e.req.path;
@@ -1703,8 +1805,8 @@ function tt(e) {
1703
1805
  let l = await t.get(c);
1704
1806
  if (!l || l.deployment_type !== "wfp" || l.provisioning_state && l.provisioning_state !== "ready") return s();
1705
1807
  let u = Reflect.get(e.env ?? {}, r);
1706
- if (!$e(u)) return s();
1707
- let d = et(i, c), f;
1808
+ if (!ct(u)) return s();
1809
+ let d = lt(i, c), f;
1708
1810
  try {
1709
1811
  f = await u.get(d).fetch(e.req.raw);
1710
1812
  } catch (t) {
@@ -1727,19 +1829,19 @@ function tt(e) {
1727
1829
  }
1728
1830
  //#endregion
1729
1831
  //#region src/index.ts
1730
- function nt(e) {
1832
+ function dt(e) {
1731
1833
  let t = {
1732
- customDomains: ae(e),
1733
- cache: se({
1834
+ customDomains: oe(e),
1835
+ cache: ce({
1734
1836
  ...e.cacheName && { cacheName: e.cacheName },
1735
1837
  ...e.defaultTtlSeconds !== void 0 && { defaultTtlSeconds: e.defaultTtlSeconds },
1736
1838
  ...e.keyPrefix && { keyPrefix: e.keyPrefix }
1737
1839
  }),
1738
- geo: ze()
1840
+ geo: Ue()
1739
1841
  };
1740
- e.r2SqlLogs ? t.logs = k(e.r2SqlLogs) : e.analyticsEngineLogs && (t.logs = W(e.analyticsEngineLogs)), e.analyticsEngineLogs && (t.analytics = U(e.analyticsEngineLogs), t.stats = H(e.analyticsEngineLogs)), e.analyticsEngineActionExecutions && (t.actionExecutions = J(e.analyticsEngineActionExecutions));
1741
- let n = Y(e.rateLimitBindings);
1842
+ e.r2SqlLogs ? t.logs = _e(e.r2SqlLogs) : e.analyticsEngineLogs && (t.logs = V(e.analyticsEngineLogs)), e.analyticsEngineLogs && (t.analytics = B(e.analyticsEngineLogs), t.stats = z(e.analyticsEngineLogs)), e.analyticsEngineActionExecutions && (t.actionExecutions = G(e.analyticsEngineActionExecutions));
1843
+ let n = K(e.rateLimitBindings);
1742
1844
  return n && (t.rateLimit = n), t;
1743
1845
  }
1744
1846
  //#endregion
1745
- export { t as CloudflareApiClient, e as CloudflareApiError, Ve as CloudflareCodeExecutor, Z as DispatchNamespaceCodeExecutor, Q as WorkerLoaderCodeExecutor, J as createAnalyticsEngineActionExecutionsAdapter, U as createAnalyticsEngineAnalyticsAdapter, W as createAnalyticsEngineLogsAdapter, H as createAnalyticsEngineStatsAdapter, Y as createCloudflareRateLimitAdapter, Ke as createCloudflareWfpD1Provisioner, k as createR2SQLLogsAdapter, he as createR2SQLStatsAdapter, tt as createWfpForwardMiddleware, n as createWfpProvisionerSteps, Ye as createWfpTenantProvisioningHook, nt as default, X as generateWorkerScript };
1847
+ export { t as CloudflareApiClient, e as CloudflareApiError, Je as CloudflareCodeExecutor, Y as DispatchNamespaceCodeExecutor, $e as WorkerLoaderCodeExecutor, G as createAnalyticsEngineActionExecutionsAdapter, B as createAnalyticsEngineAnalyticsAdapter, V as createAnalyticsEngineLogsAdapter, et as createAnalyticsEngineOutboxMetricsSink, z as createAnalyticsEngineStatsAdapter, K as createCloudflareRateLimitAdapter, tt as createCloudflareWfpD1Provisioner, _e as createR2SQLLogsAdapter, ge as createR2SQLStatsAdapter, ut as createWfpForwardMiddleware, n as createWfpProvisionerSteps, it as createWfpTenantProvisioningHook, dt as default, J as generateWorkerScript, qe as syncCustomDomains };