@appwarden/middleware 3.11.6 → 3.12.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.
@@ -0,0 +1,36 @@
1
+ // src/utils/errors.ts
2
+ var errorsMap = {
3
+ mode: '`CSP_MODE` must be one of "disabled", "report-only", or "enforced"',
4
+ directives: {
5
+ ["DirectivesRequired" /* DirectivesRequired */]: '`CSP_DIRECTIVES` must be provided when `CSP_MODE` is "report-only" or "enforced"',
6
+ ["DirectivesBadParse" /* DirectivesBadParse */]: "Failed to parse `CSP_DIRECTIVES`. Is it a valid JSON string?"
7
+ },
8
+ appwardenApiToken: "Please provide a valid `appwardenApiToken`. Learn more at https://appwarden.com/docs/guides/api-token-management."
9
+ };
10
+ var getErrors = (error) => {
11
+ const matches = [];
12
+ const errors = [...Object.entries(error.flatten().fieldErrors)];
13
+ for (const issue of error.issues) {
14
+ errors.push(
15
+ ...Object.entries(
16
+ "returnTypeError" in issue ? issue.returnTypeError.flatten().fieldErrors : {}
17
+ )
18
+ );
19
+ }
20
+ for (const [field, maybeSchemaErrorKey] of errors) {
21
+ let match = errorsMap[field];
22
+ if (match) {
23
+ if (match instanceof Object) {
24
+ if (maybeSchemaErrorKey) {
25
+ match = match[maybeSchemaErrorKey[0]];
26
+ }
27
+ }
28
+ matches.push(match);
29
+ }
30
+ }
31
+ return matches;
32
+ };
33
+
34
+ export {
35
+ getErrors
36
+ };
@@ -3,10 +3,41 @@ var LOCKDOWN_TEST_EXPIRY_MS = 5 * 60 * 1e3;
3
3
  var errors = { badCacheConnection: "BAD_CACHE_CONNECTION" };
4
4
  var globalErrors = [errors.badCacheConnection];
5
5
  var APPWARDEN_TEST_ROUTE = "/_appwarden/test";
6
+ var APPWARDEN_HEARTBEAT_ROUTE = "/_appwarden/heartbeat";
7
+ var HEARTBEAT_CONTRACT_VERSION = 1;
8
+ var HEARTBEAT_CONFIG_ERROR_MAX_COUNT = 10;
9
+ var HEARTBEAT_CONFIG_ERROR_MAX_PATH_DEPTH = 10;
10
+ var HEARTBEAT_CONFIG_ERROR_MAX_CODE_LENGTH = 100;
11
+ var HEARTBEAT_CONFIG_ERROR_MAX_MESSAGE_LENGTH = 500;
12
+ var HEARTBEAT_CONFIG_ERROR_MAX_PATH_SEGMENT_LENGTH = 100;
6
13
  var APPWARDEN_CACHE_KEY = "appwarden-lock";
14
+ var HEARTBEAT_SERVICE_VALUES = [
15
+ "cloudflare",
16
+ "cloudflare-astro",
17
+ "cloudflare-react-router",
18
+ "cloudflare-tanstack-start",
19
+ "cloudflare-nextjs",
20
+ "vercel"
21
+ ];
22
+ var [
23
+ CLOUDFLARE,
24
+ CLOUDFLARE_ASTRO,
25
+ CLOUDFLARE_REACT_ROUTER,
26
+ CLOUDFLARE_TANSTACK_START,
27
+ CLOUDFLARE_NEXTJS,
28
+ VERCEL
29
+ ] = HEARTBEAT_SERVICE_VALUES;
30
+ var HEARTBEAT_SERVICES = {
31
+ CLOUDFLARE,
32
+ CLOUDFLARE_ASTRO,
33
+ CLOUDFLARE_REACT_ROUTER,
34
+ CLOUDFLARE_TANSTACK_START,
35
+ CLOUDFLARE_NEXTJS,
36
+ VERCEL
37
+ };
7
38
 
8
39
  // src/schemas/use-content-security-policy.ts
9
- import { z as z2 } from "zod";
40
+ import { z as z3 } from "zod";
10
41
 
11
42
  // src/types/csp.ts
12
43
  import { z } from "zod";
@@ -40,17 +71,40 @@ var ContentSecurityPolicySchema = z.object({
40
71
  "require-trusted-types-for": stringySchema.optional()
41
72
  });
42
73
 
74
+ // src/schemas/helpers.ts
75
+ import { z as z2 } from "zod";
76
+ var BoolOrStringSchema = z2.union([z2.string(), z2.boolean()]).optional();
77
+ var BooleanSchema = BoolOrStringSchema.transform((val) => {
78
+ if (val === "true" || val === true) {
79
+ return true;
80
+ } else if (val === "false" || val === false) {
81
+ return false;
82
+ }
83
+ throw new Error("Invalid value");
84
+ });
85
+ var AppwardenApiTokenSchema = z2.string().refine((val) => !!val, { message: "appwardenApiToken is required" });
86
+ var AppwardenApiHostnameSchema = z2.string().url({
87
+ message: "Invalid `appwardenApiHostname`. Please provide an absolute URL (e.g. https://api.appwarden.io)."
88
+ }).refine((value) => value.startsWith("https://"), {
89
+ message: "`appwardenApiHostname` must use the https:// scheme (e.g. https://api.appwarden.io)."
90
+ });
91
+ var LockValue = z2.object({
92
+ isLocked: z2.number(),
93
+ isLockedTest: z2.number(),
94
+ lastCheck: z2.number()
95
+ });
96
+
43
97
  // src/schemas/use-content-security-policy.ts
44
- var CSPDirectivesSchema = z2.union([
45
- z2.string(),
98
+ var CSPDirectivesSchema = z3.union([
99
+ z3.string(),
46
100
  ContentSecurityPolicySchema
47
101
  ]);
48
- var CSPModeSchema = z2.union([
49
- z2.literal("disabled"),
50
- z2.literal("report-only"),
51
- z2.literal("enforced")
102
+ var CSPModeSchema = z3.union([
103
+ z3.literal("disabled"),
104
+ z3.literal("report-only"),
105
+ z3.literal("enforced")
52
106
  ]);
53
- var UseCSPInputSchema = z2.object({
107
+ var UseCSPInputSchema = z3.object({
54
108
  mode: CSPModeSchema,
55
109
  directives: CSPDirectivesSchema.refine(
56
110
  (val) => {
@@ -74,7 +128,19 @@ export {
74
128
  errors,
75
129
  globalErrors,
76
130
  APPWARDEN_TEST_ROUTE,
131
+ APPWARDEN_HEARTBEAT_ROUTE,
132
+ HEARTBEAT_CONTRACT_VERSION,
133
+ HEARTBEAT_CONFIG_ERROR_MAX_COUNT,
134
+ HEARTBEAT_CONFIG_ERROR_MAX_PATH_DEPTH,
135
+ HEARTBEAT_CONFIG_ERROR_MAX_CODE_LENGTH,
136
+ HEARTBEAT_CONFIG_ERROR_MAX_MESSAGE_LENGTH,
137
+ HEARTBEAT_CONFIG_ERROR_MAX_PATH_SEGMENT_LENGTH,
77
138
  APPWARDEN_CACHE_KEY,
139
+ HEARTBEAT_SERVICES,
140
+ BooleanSchema,
141
+ AppwardenApiTokenSchema,
142
+ AppwardenApiHostnameSchema,
143
+ LockValue,
78
144
  CSPDirectivesSchema,
79
145
  CSPModeSchema,
80
146
  UseCSPInputSchema
@@ -270,8 +270,8 @@ declare const AstroCloudflareConfigSchema: z.ZodObject<{
270
270
  };
271
271
  }>>>;
272
272
  }, "strip", z.ZodTypeAny, {
273
- debug: boolean;
274
273
  lockPageSlug: string;
274
+ debug: boolean;
275
275
  appwardenApiToken: string;
276
276
  contentSecurityPolicy?: {
277
277
  mode: "disabled" | "report-only" | "enforced";
@@ -308,7 +308,6 @@ declare const AstroCloudflareConfigSchema: z.ZodObject<{
308
308
  }, {
309
309
  lockPageSlug: string;
310
310
  appwardenApiToken: string;
311
- debug?: string | boolean | undefined;
312
311
  contentSecurityPolicy?: {
313
312
  mode: "disabled" | "report-only" | "enforced";
314
313
  directives: string | {
@@ -340,6 +339,7 @@ declare const AstroCloudflareConfigSchema: z.ZodObject<{
340
339
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
341
340
  };
342
341
  } | undefined;
342
+ debug?: string | boolean | undefined;
343
343
  appwardenApiHostname?: string | undefined;
344
344
  }>;
345
345
  type AstroCloudflareConfig = z.infer<typeof AstroCloudflareConfigSchema>;
@@ -1,34 +1,35 @@
1
1
  import {
2
2
  applyContentSecurityPolicyToResponse,
3
3
  isResponseLike
4
- } from "../chunk-UFWJYCX6.js";
5
- import "../chunk-WBWF3PPX.js";
4
+ } from "../chunk-M2YVPCTG.js";
5
+ import "../chunk-ILIYP3TG.js";
6
6
  import {
7
7
  getNowMs,
8
8
  logElapsed
9
9
  } from "../chunk-G6BMPIYD.js";
10
10
  import {
11
11
  checkLockStatus
12
- } from "../chunk-QC2ZUZWY.js";
12
+ } from "../chunk-EXGUJ5XK.js";
13
13
  import {
14
14
  TEMPORARY_REDIRECT_STATUS,
15
15
  buildLockPageUrl,
16
+ createHeartbeatConfigError,
16
17
  createRedirect,
17
18
  debug,
19
+ handleHeartbeatRequest,
18
20
  isHTMLRequest,
19
- isOnLockPage
20
- } from "../chunk-AY4ZKZTF.js";
21
- import {
22
- UseCSPInputSchema
23
- } from "../chunk-ZTVJBORU.js";
24
- import {
25
- printMessage
26
- } from "../chunk-R7TXTHSG.js";
21
+ isHeartbeatRequest,
22
+ isOnLockPage,
23
+ printMessage,
24
+ sanitizeConfigErrors
25
+ } from "../chunk-HTSD4WPC.js";
27
26
  import {
28
27
  AppwardenApiHostnameSchema,
29
28
  AppwardenApiTokenSchema,
30
- BooleanSchema
31
- } from "../chunk-WEM7GS4M.js";
29
+ BooleanSchema,
30
+ HEARTBEAT_SERVICES,
31
+ UseCSPInputSchema
32
+ } from "../chunk-Z7P4QVEY.js";
32
33
 
33
34
  // src/adapters/astro-cloudflare.ts
34
35
  import { waitUntil } from "cloudflare:workers";
@@ -49,13 +50,50 @@ var AstroCloudflareConfigSchema = z.object({
49
50
  });
50
51
 
51
52
  // src/adapters/astro-cloudflare.ts
53
+ var createAstroHeartbeatResponse = (request, runtime, configFn) => {
54
+ if (!runtime) {
55
+ return handleHeartbeatRequest(
56
+ request,
57
+ HEARTBEAT_SERVICES.CLOUDFLARE_ASTRO,
58
+ [
59
+ createHeartbeatConfigError(
60
+ ["runtime"],
61
+ "custom",
62
+ "Cloudflare runtime unavailable"
63
+ )
64
+ ]
65
+ );
66
+ }
67
+ try {
68
+ const validationResult = AstroCloudflareConfigSchema.safeParse(
69
+ configFn(runtime)
70
+ );
71
+ return handleHeartbeatRequest(
72
+ request,
73
+ HEARTBEAT_SERVICES.CLOUDFLARE_ASTRO,
74
+ validationResult.success ? [] : sanitizeConfigErrors(validationResult.error)
75
+ );
76
+ } catch {
77
+ return handleHeartbeatRequest(
78
+ request,
79
+ HEARTBEAT_SERVICES.CLOUDFLARE_ASTRO,
80
+ [
81
+ createHeartbeatConfigError(
82
+ ["config"],
83
+ "custom",
84
+ "Appwarden config evaluation failed"
85
+ )
86
+ ]
87
+ );
88
+ }
89
+ };
52
90
  function createAppwardenMiddleware(configFn) {
53
91
  return async (context, next) => {
54
92
  const startTime = getNowMs();
55
93
  const { request } = context;
56
94
  let config;
57
95
  let debugFn;
58
- let requestUrl;
96
+ const requestUrl = new URL(request.url);
59
97
  const applyCspToResponse = async (response2) => {
60
98
  if (!config.contentSecurityPolicy || !isResponseLike(response2)) {
61
99
  return response2;
@@ -79,6 +117,9 @@ function createAppwardenMiddleware(configFn) {
79
117
  }
80
118
  };
81
119
  const locals = context.locals;
120
+ if (isHeartbeatRequest(request, requestUrl)) {
121
+ return createAstroHeartbeatResponse(request, locals.runtime, configFn);
122
+ }
82
123
  try {
83
124
  const runtime = locals.runtime;
84
125
  if (!runtime) {
@@ -101,7 +142,6 @@ function createAppwardenMiddleware(configFn) {
101
142
  }
102
143
  config = validationResult.data;
103
144
  debugFn = debug(config.debug);
104
- requestUrl = new URL(request.url);
105
145
  const isHTML = isHTMLRequest(request);
106
146
  debugFn(
107
147
  `Appwarden middleware invoked for ${requestUrl.pathname}`,
@@ -329,8 +329,8 @@ declare const NextJsCloudflareConfigSchema: z.ZodObject<{
329
329
  };
330
330
  }>>>;
331
331
  }, "strip", z.ZodTypeAny, {
332
- debug: boolean;
333
332
  lockPageSlug: string;
333
+ debug: boolean;
334
334
  appwardenApiToken: string;
335
335
  contentSecurityPolicy?: {
336
336
  mode: "disabled" | "report-only" | "enforced";
@@ -367,7 +367,6 @@ declare const NextJsCloudflareConfigSchema: z.ZodObject<{
367
367
  }, {
368
368
  lockPageSlug: string;
369
369
  appwardenApiToken: string;
370
- debug?: string | boolean | undefined;
371
370
  contentSecurityPolicy?: {
372
371
  mode: "disabled" | "report-only" | "enforced";
373
372
  directives: string | {
@@ -399,6 +398,7 @@ declare const NextJsCloudflareConfigSchema: z.ZodObject<{
399
398
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
400
399
  };
401
400
  } | undefined;
401
+ debug?: string | boolean | undefined;
402
402
  appwardenApiHostname?: string | undefined;
403
403
  }>;
404
404
  type NextJsCloudflareConfig = z.infer<typeof NextJsCloudflareConfigSchema>;
@@ -1,30 +1,36 @@
1
+ import {
2
+ toNextResponse
3
+ } from "../chunk-HUWGPM4M.js";
1
4
  import {
2
5
  getNowMs,
3
6
  logElapsed
4
7
  } from "../chunk-G6BMPIYD.js";
5
8
  import {
6
9
  checkLockStatus
7
- } from "../chunk-QC2ZUZWY.js";
10
+ } from "../chunk-EXGUJ5XK.js";
8
11
  import {
9
12
  TEMPORARY_REDIRECT_STATUS,
10
13
  buildLockPageUrl,
14
+ createHeartbeatConfigError,
11
15
  debug,
16
+ handleHeartbeatRequest,
12
17
  isHTMLRequest,
13
- isOnLockPage
14
- } from "../chunk-AY4ZKZTF.js";
15
- import {
16
- UseCSPInputSchema
17
- } from "../chunk-ZTVJBORU.js";
18
- import {
19
- printMessage
20
- } from "../chunk-R7TXTHSG.js";
18
+ isHeartbeatRequest,
19
+ isOnLockPage,
20
+ makeCSPHeader,
21
+ printMessage,
22
+ sanitizeConfigErrors
23
+ } from "../chunk-HTSD4WPC.js";
21
24
  import {
22
25
  AppwardenApiHostnameSchema,
23
26
  AppwardenApiTokenSchema,
24
- BooleanSchema
25
- } from "../chunk-WEM7GS4M.js";
27
+ BooleanSchema,
28
+ HEARTBEAT_SERVICES,
29
+ UseCSPInputSchema
30
+ } from "../chunk-Z7P4QVEY.js";
26
31
 
27
32
  // src/adapters/nextjs-cloudflare.ts
33
+ import { getCloudflareContext } from "@opennextjs/cloudflare";
28
34
  import {
29
35
  NextResponse
30
36
  } from "next/server";
@@ -56,12 +62,53 @@ var NextJsCloudflareConfigSchema = z.object({
56
62
  });
57
63
 
58
64
  // src/adapters/nextjs-cloudflare.ts
65
+ var createNextJsHeartbeatResponse = (request, configFn) => {
66
+ let runtime;
67
+ try {
68
+ runtime = getCloudflareContext();
69
+ } catch {
70
+ return toNextResponse(
71
+ handleHeartbeatRequest(request, HEARTBEAT_SERVICES.CLOUDFLARE_NEXTJS, [
72
+ createHeartbeatConfigError(
73
+ ["context"],
74
+ "custom",
75
+ "Cloudflare context unavailable"
76
+ )
77
+ ])
78
+ );
79
+ }
80
+ try {
81
+ const validationResult = NextJsCloudflareConfigSchema.safeParse(
82
+ configFn(runtime)
83
+ );
84
+ return toNextResponse(
85
+ handleHeartbeatRequest(
86
+ request,
87
+ HEARTBEAT_SERVICES.CLOUDFLARE_NEXTJS,
88
+ validationResult.success ? [] : sanitizeConfigErrors(validationResult.error)
89
+ )
90
+ );
91
+ } catch {
92
+ return toNextResponse(
93
+ handleHeartbeatRequest(request, HEARTBEAT_SERVICES.CLOUDFLARE_NEXTJS, [
94
+ createHeartbeatConfigError(
95
+ ["config"],
96
+ "custom",
97
+ "Appwarden config evaluation failed"
98
+ )
99
+ ])
100
+ );
101
+ }
102
+ };
59
103
  function createAppwardenMiddleware(configFn) {
60
104
  return async (request, _event) => {
61
105
  const startTime = getNowMs();
106
+ const requestUrl = new URL(request.url);
107
+ if (isHeartbeatRequest(request, requestUrl)) {
108
+ return createNextJsHeartbeatResponse(request, configFn);
109
+ }
62
110
  try {
63
- const { getCloudflareContext } = await import("@opennextjs/cloudflare");
64
- const { env, ctx } = await getCloudflareContext();
111
+ const { env, ctx } = getCloudflareContext();
65
112
  const rawConfig = configFn({ env, ctx });
66
113
  const validationResult = NextJsCloudflareConfigSchema.safeParse(rawConfig);
67
114
  if (!validationResult.success) {
@@ -74,7 +121,6 @@ function createAppwardenMiddleware(configFn) {
74
121
  }
75
122
  const config = validationResult.data;
76
123
  const debugFn = debug(config.debug);
77
- const requestUrl = new URL(request.url);
78
124
  const isHTML = isHTMLRequest(request);
79
125
  debugFn(
80
126
  `Appwarden middleware invoked for ${requestUrl.pathname}`,
@@ -105,7 +151,6 @@ function createAppwardenMiddleware(configFn) {
105
151
  debugFn(
106
152
  `Applying CSP headers in ${config.contentSecurityPolicy.mode} mode`
107
153
  );
108
- const { makeCSPHeader } = await import("../cloudflare-MAHYENA6.js");
109
154
  const [headerName, headerValue] = makeCSPHeader(
110
155
  "",
111
156
  config.contentSecurityPolicy.directives,
@@ -268,8 +268,8 @@ declare const ReactRouterCloudflareConfigSchema: z.ZodObject<{
268
268
  };
269
269
  }>>>;
270
270
  }, "strip", z.ZodTypeAny, {
271
- debug: boolean;
272
271
  lockPageSlug: string;
272
+ debug: boolean;
273
273
  appwardenApiToken: string;
274
274
  contentSecurityPolicy?: {
275
275
  mode: "disabled" | "report-only" | "enforced";
@@ -306,7 +306,6 @@ declare const ReactRouterCloudflareConfigSchema: z.ZodObject<{
306
306
  }, {
307
307
  lockPageSlug: string;
308
308
  appwardenApiToken: string;
309
- debug?: string | boolean | undefined;
310
309
  contentSecurityPolicy?: {
311
310
  mode: "disabled" | "report-only" | "enforced";
312
311
  directives: string | {
@@ -338,6 +337,7 @@ declare const ReactRouterCloudflareConfigSchema: z.ZodObject<{
338
337
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
339
338
  };
340
339
  } | undefined;
340
+ debug?: string | boolean | undefined;
341
341
  appwardenApiHostname?: string | undefined;
342
342
  }>;
343
343
  type ReactRouterCloudflareConfig = z.infer<typeof ReactRouterCloudflareConfigSchema>;
@@ -1,33 +1,34 @@
1
1
  import {
2
2
  applyContentSecurityPolicyToResponse,
3
3
  isResponseLike
4
- } from "../chunk-UFWJYCX6.js";
5
- import "../chunk-WBWF3PPX.js";
4
+ } from "../chunk-M2YVPCTG.js";
5
+ import "../chunk-ILIYP3TG.js";
6
6
  import {
7
7
  getNowMs,
8
8
  logElapsed
9
9
  } from "../chunk-G6BMPIYD.js";
10
10
  import {
11
11
  checkLockStatus
12
- } from "../chunk-QC2ZUZWY.js";
12
+ } from "../chunk-EXGUJ5XK.js";
13
13
  import {
14
14
  buildLockPageUrl,
15
+ createHeartbeatConfigError,
15
16
  createRedirect,
16
17
  debug,
18
+ handleHeartbeatRequest,
17
19
  isHTMLRequest,
18
- isOnLockPage
19
- } from "../chunk-AY4ZKZTF.js";
20
- import {
21
- UseCSPInputSchema
22
- } from "../chunk-ZTVJBORU.js";
23
- import {
24
- printMessage
25
- } from "../chunk-R7TXTHSG.js";
20
+ isHeartbeatRequest,
21
+ isOnLockPage,
22
+ printMessage,
23
+ sanitizeConfigErrors
24
+ } from "../chunk-HTSD4WPC.js";
26
25
  import {
27
26
  AppwardenApiHostnameSchema,
28
27
  AppwardenApiTokenSchema,
29
- BooleanSchema
30
- } from "../chunk-WEM7GS4M.js";
28
+ BooleanSchema,
29
+ HEARTBEAT_SERVICES,
30
+ UseCSPInputSchema
31
+ } from "../chunk-Z7P4QVEY.js";
31
32
 
32
33
  // src/adapters/react-router-cloudflare.ts
33
34
  import { waitUntil } from "cloudflare:workers";
@@ -48,13 +49,38 @@ var ReactRouterCloudflareConfigSchema = z.object({
48
49
  });
49
50
 
50
51
  // src/adapters/react-router-cloudflare.ts
52
+ var createConfigEvaluationHeartbeatResponse = (request) => {
53
+ return handleHeartbeatRequest(
54
+ request,
55
+ HEARTBEAT_SERVICES.CLOUDFLARE_REACT_ROUTER,
56
+ [
57
+ createHeartbeatConfigError(
58
+ ["config"],
59
+ "custom",
60
+ "Appwarden config evaluation failed"
61
+ )
62
+ ]
63
+ );
64
+ };
65
+ var handleReactRouterHeartbeatRequest = (request, configFn) => {
66
+ try {
67
+ const validationResult = ReactRouterCloudflareConfigSchema.safeParse(configFn());
68
+ return handleHeartbeatRequest(
69
+ request,
70
+ HEARTBEAT_SERVICES.CLOUDFLARE_REACT_ROUTER,
71
+ validationResult.success ? [] : sanitizeConfigErrors(validationResult.error)
72
+ );
73
+ } catch {
74
+ return createConfigEvaluationHeartbeatResponse(request);
75
+ }
76
+ };
51
77
  function createAppwardenMiddleware(configFn) {
52
78
  return async (args, next) => {
53
79
  const startTime = getNowMs();
54
80
  const { request } = args;
55
81
  let config;
56
82
  let debugFn;
57
- let requestUrl;
83
+ const requestUrl = new URL(request.url);
58
84
  const applyCspToResponse = async (response2) => {
59
85
  if (!config.contentSecurityPolicy || !isResponseLike(response2)) {
60
86
  return response2;
@@ -77,6 +103,9 @@ function createAppwardenMiddleware(configFn) {
77
103
  return response2;
78
104
  }
79
105
  };
106
+ if (isHeartbeatRequest(request, requestUrl)) {
107
+ return handleReactRouterHeartbeatRequest(request, configFn);
108
+ }
80
109
  try {
81
110
  const configInput = configFn();
82
111
  const validationResult = ReactRouterCloudflareConfigSchema.safeParse(configInput);
@@ -90,7 +119,6 @@ function createAppwardenMiddleware(configFn) {
90
119
  }
91
120
  config = validationResult.data;
92
121
  debugFn = debug(config.debug);
93
- requestUrl = new URL(request.url);
94
122
  const isHTML = isHTMLRequest(request);
95
123
  debugFn(
96
124
  `Appwarden middleware invoked for ${requestUrl.pathname}`,
@@ -268,8 +268,8 @@ declare const TanStackStartCloudflareConfigSchema: z.ZodObject<{
268
268
  };
269
269
  }>>>;
270
270
  }, "strip", z.ZodTypeAny, {
271
- debug: boolean;
272
271
  lockPageSlug: string;
272
+ debug: boolean;
273
273
  appwardenApiToken: string;
274
274
  contentSecurityPolicy?: {
275
275
  mode: "disabled" | "report-only" | "enforced";
@@ -306,7 +306,6 @@ declare const TanStackStartCloudflareConfigSchema: z.ZodObject<{
306
306
  }, {
307
307
  lockPageSlug: string;
308
308
  appwardenApiToken: string;
309
- debug?: string | boolean | undefined;
310
309
  contentSecurityPolicy?: {
311
310
  mode: "disabled" | "report-only" | "enforced";
312
311
  directives: string | {
@@ -338,6 +337,7 @@ declare const TanStackStartCloudflareConfigSchema: z.ZodObject<{
338
337
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
339
338
  };
340
339
  } | undefined;
340
+ debug?: string | boolean | undefined;
341
341
  appwardenApiHostname?: string | undefined;
342
342
  }>;
343
343
  type TanStackStartCloudflareConfig = z.infer<typeof TanStackStartCloudflareConfigSchema>;
@@ -1,33 +1,34 @@
1
1
  import {
2
2
  applyContentSecurityPolicyToResponse,
3
3
  isResponseLike
4
- } from "../chunk-UFWJYCX6.js";
5
- import "../chunk-WBWF3PPX.js";
4
+ } from "../chunk-M2YVPCTG.js";
5
+ import "../chunk-ILIYP3TG.js";
6
6
  import {
7
7
  getNowMs,
8
8
  logElapsed
9
9
  } from "../chunk-G6BMPIYD.js";
10
10
  import {
11
11
  checkLockStatus
12
- } from "../chunk-QC2ZUZWY.js";
12
+ } from "../chunk-EXGUJ5XK.js";
13
13
  import {
14
14
  buildLockPageUrl,
15
+ createHeartbeatConfigError,
15
16
  createRedirect,
16
17
  debug,
18
+ handleHeartbeatRequest,
17
19
  isHTMLRequest,
18
- isOnLockPage
19
- } from "../chunk-AY4ZKZTF.js";
20
- import {
21
- UseCSPInputSchema
22
- } from "../chunk-ZTVJBORU.js";
23
- import {
24
- printMessage
25
- } from "../chunk-R7TXTHSG.js";
20
+ isHeartbeatRequest,
21
+ isOnLockPage,
22
+ printMessage,
23
+ sanitizeConfigErrors
24
+ } from "../chunk-HTSD4WPC.js";
26
25
  import {
27
26
  AppwardenApiHostnameSchema,
28
27
  AppwardenApiTokenSchema,
29
- BooleanSchema
30
- } from "../chunk-WEM7GS4M.js";
28
+ BooleanSchema,
29
+ HEARTBEAT_SERVICES,
30
+ UseCSPInputSchema
31
+ } from "../chunk-Z7P4QVEY.js";
31
32
 
32
33
  // src/adapters/tanstack-start-cloudflare.ts
33
34
  import { waitUntil } from "cloudflare:workers";
@@ -48,13 +49,35 @@ var TanStackStartCloudflareConfigSchema = z.object({
48
49
  });
49
50
 
50
51
  // src/adapters/tanstack-start-cloudflare.ts
52
+ var createTanStackHeartbeatResponse = (request, configFn) => {
53
+ try {
54
+ const validationResult = TanStackStartCloudflareConfigSchema.safeParse(configFn());
55
+ return handleHeartbeatRequest(
56
+ request,
57
+ HEARTBEAT_SERVICES.CLOUDFLARE_TANSTACK_START,
58
+ validationResult.success ? [] : sanitizeConfigErrors(validationResult.error)
59
+ );
60
+ } catch {
61
+ return handleHeartbeatRequest(
62
+ request,
63
+ HEARTBEAT_SERVICES.CLOUDFLARE_TANSTACK_START,
64
+ [
65
+ createHeartbeatConfigError(
66
+ ["config"],
67
+ "custom",
68
+ "Appwarden config evaluation failed"
69
+ )
70
+ ]
71
+ );
72
+ }
73
+ };
51
74
  function createAppwardenMiddleware(configFn) {
52
75
  const middleware = async (args) => {
53
76
  const startTime = getNowMs();
54
77
  const { request, next } = args;
55
78
  let config;
56
79
  let debugFn;
57
- let requestUrl;
80
+ const requestUrl = new URL(request.url);
58
81
  const applyCspToResponse = async (response2) => {
59
82
  if (!config.contentSecurityPolicy || !isResponseLike(response2)) {
60
83
  return response2;
@@ -77,6 +100,9 @@ function createAppwardenMiddleware(configFn) {
77
100
  return response2;
78
101
  }
79
102
  };
103
+ if (isHeartbeatRequest(request, requestUrl)) {
104
+ throw createTanStackHeartbeatResponse(request, configFn);
105
+ }
80
106
  try {
81
107
  const rawConfig = configFn();
82
108
  const validationResult = TanStackStartCloudflareConfigSchema.safeParse(rawConfig);
@@ -90,7 +116,6 @@ function createAppwardenMiddleware(configFn) {
90
116
  }
91
117
  config = validationResult.data;
92
118
  debugFn = debug(config.debug ?? false);
93
- requestUrl = new URL(request.url);
94
119
  const isHTML = isHTMLRequest(request);
95
120
  debugFn(
96
121
  `Appwarden middleware invoked for ${requestUrl.pathname}`,