@appwarden/middleware 1.1.2 → 1.1.4

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.
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  APPWARDEN_CACHE_KEY,
3
3
  APPWARDEN_TEST_ROUTE,
4
+ APPWARDEN_USER_AGENT,
4
5
  LockValue,
5
6
  MemoryCache,
6
7
  UseCSPInputSchema,
@@ -9,7 +10,7 @@ import {
9
10
  printMessage,
10
11
  removedHeaders,
11
12
  renderLockPage
12
- } from "./chunk-RKUPO5Z3.js";
13
+ } from "./chunk-GC5FS43G.js";
13
14
 
14
15
  // src/utils/cloudflare/cloudflare-cache.ts
15
16
  var store = {
@@ -39,7 +40,7 @@ var getCacheValue = async (context, cacheKey) => {
39
40
  var updateCacheValue = async (context, cacheKey, value, cacheExpirationSeconds) => {
40
41
  debug(
41
42
  "updating cache...",
42
- cacheKey.pathname,
43
+ cacheKey.href,
43
44
  value,
44
45
  cacheExpirationSeconds ? `expires in ${cacheExpirationSeconds}s` : ""
45
46
  );
@@ -127,7 +128,7 @@ var getLockValue = async (context) => {
127
128
  var insertErrorLogs = async (context, error) => {
128
129
  const errors = getErrors(error);
129
130
  for (const err of errors) {
130
- console.error(printMessage(err));
131
+ console.log(printMessage(err));
131
132
  }
132
133
  return new HTMLRewriter().on("body", {
133
134
  element: (elem) => {
@@ -196,21 +197,20 @@ var syncEdgeValue = async (context) => {
196
197
  if (result.error) {
197
198
  throw new APIError(result.error.message);
198
199
  }
199
- if (result.content) {
200
- try {
201
- const parsedValue = LockValue.omit({ lastCheck: true }).parse(
202
- result.content
203
- );
204
- debug(
205
- `syncing with api...DONE ${JSON.stringify(parsedValue, null, 2)}`
206
- );
207
- await context.edgeCache.updateValue({
208
- ...parsedValue,
209
- lastCheck: Date.now()
210
- });
211
- } catch (error) {
212
- throw new APIError(`Failed to parse check endpoint result - ${error}`);
213
- }
200
+ if (!result.content) {
201
+ throw new APIError("no content from api");
202
+ }
203
+ try {
204
+ const parsedValue = LockValue.omit({ lastCheck: true }).parse(
205
+ result.content
206
+ );
207
+ debug(`syncing with api...DONE ${JSON.stringify(parsedValue, null, 2)}`);
208
+ await context.edgeCache.updateValue({
209
+ ...parsedValue,
210
+ lastCheck: Date.now()
211
+ });
212
+ } catch (error) {
213
+ throw new APIError(`Failed to parse check endpoint result - ${error}`);
214
214
  }
215
215
  }
216
216
  } catch (e) {
@@ -329,7 +329,8 @@ var useAppwarden = (input) => async (context, next) => {
329
329
  return;
330
330
  }
331
331
  const isHTMLRequest = response.headers.get("Content-Type")?.includes("text/html");
332
- if (isHTMLRequest) {
332
+ const isMonitoringRequest = request.headers.get("User-Agent") === APPWARDEN_USER_AGENT;
333
+ if (isHTMLRequest && !isMonitoringRequest) {
333
334
  const innerContext = {
334
335
  keyName,
335
336
  request,
@@ -4,6 +4,7 @@ var removedHeaders = ["X-Powered-By", "Server"];
4
4
  var errors = { badCacheConnection: "BAD_CACHE_CONNECTION" };
5
5
  var globalErrors = [errors.badCacheConnection];
6
6
  var APPWARDEN_TEST_ROUTE = "/_appwarden/test";
7
+ var APPWARDEN_USER_AGENT = "Appwarden-Monitor";
7
8
  var APPWARDEN_CACHE_KEY = "appwarden-lock";
8
9
 
9
10
  // src/utils/is-cache-url.ts
@@ -88,7 +89,7 @@ var errorsMap = {
88
89
  ["DirectivesRequired" /* DirectivesRequired */]: '`CSP_DIRECTIVES` must be provided when `CSP_MODE` is "report-only" or "enforced"',
89
90
  ["DirectivesBadParse" /* DirectivesBadParse */]: "Failed to parse `CSP_DIRECTIVES`. Is it a valid JSON string?"
90
91
  },
91
- appwardenApiToken: "Please provide a valid `appwardenApiToken`. Learn more at https://appwarden.com/docs/api-tokens."
92
+ appwardenApiToken: "Please provide a valid `appwardenApiToken`. Learn more at https://appwarden.com/docs/guides/api-token-management."
92
93
  };
93
94
  var getErrors = (error) => {
94
95
  const matches = [];
@@ -307,7 +308,7 @@ var AppwardenConfigSchema = BaseNextJsConfigSchema.refine(
307
308
  }
308
309
  ).refine((data) => !!data.appwardenApiToken, {
309
310
  message: printMessage(
310
- "Please provide a valid `appwardenApiToken`. Learn more at https://appwarden.com/docs/api-tokens."
311
+ "Please provide a valid `appwardenApiToken`. Learn more at https://appwarden.com/docs/guides/api-token-management."
311
312
  ),
312
313
  path: ["appwardenApiToken"]
313
314
  });
@@ -472,7 +473,11 @@ var CSPDirectivesSchema = z8.union([
472
473
  z8.string(),
473
474
  ContentSecurityPolicySchema
474
475
  ]);
475
- var CSPModeSchema = z8.literal("disabled").or(z8.literal("report-only")).or(z8.literal("enforced")).optional().default("disabled");
476
+ var CSPModeSchema = z8.union([
477
+ z8.literal("disabled"),
478
+ z8.literal("report-only"),
479
+ z8.literal("enforced")
480
+ ]).optional().default("disabled");
476
481
  var UseCSPInputSchema = z8.object({
477
482
  mode: CSPModeSchema,
478
483
  directives: CSPDirectivesSchema.optional().refine(
@@ -503,6 +508,7 @@ export {
503
508
  removedHeaders,
504
509
  globalErrors,
505
510
  APPWARDEN_TEST_ROUTE,
511
+ APPWARDEN_USER_AGENT,
506
512
  APPWARDEN_CACHE_KEY,
507
513
  debug,
508
514
  getErrors,
package/cloudflare.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as Middleware, B as Bindings } from './use-content-security-policy-DBWKjDEH.js';
2
- export { u as useContentSecurityPolicy } from './use-content-security-policy-DBWKjDEH.js';
1
+ import { M as Middleware, B as Bindings } from './use-content-security-policy-UyG9yvVs.js';
2
+ export { u as useContentSecurityPolicy } from './use-content-security-policy-UyG9yvVs.js';
3
3
  import { z } from 'zod';
4
4
 
5
5
  declare const ConfigFnInputSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<{
package/cloudflare.js CHANGED
@@ -2,11 +2,11 @@ import {
2
2
  insertErrorLogs,
3
3
  useAppwarden,
4
4
  useContentSecurityPolicy
5
- } from "./chunk-SDYUAIDZ.js";
5
+ } from "./chunk-6DI6L6AE.js";
6
6
  import {
7
7
  ConfigFnInputSchema,
8
8
  usePipeline
9
- } from "./chunk-RKUPO5Z3.js";
9
+ } from "./chunk-GC5FS43G.js";
10
10
 
11
11
  // src/runners/appwarden-on-cloudflare.ts
12
12
  import { ZodError } from "zod";
package/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- export { B as Bindings, C as CSPDirectivesSchema, a as CSPModeSchema, M as Middleware, u as useContentSecurityPolicy } from './use-content-security-policy-DBWKjDEH.js';
1
+ export { B as Bindings, C as CSPDirectivesSchema, a as CSPModeSchema, M as Middleware, u as useContentSecurityPolicy } from './use-content-security-policy-UyG9yvVs.js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  declare const LOCKDOWN_TEST_EXPIRY_MS: number;
5
+ declare const APPWARDEN_USER_AGENT: "Appwarden-Monitor";
5
6
  declare const APPWARDEN_CACHE_KEY: "appwarden-lock";
6
7
 
7
8
  declare const getEdgeConfigId: (value?: string) => string | undefined;
@@ -32,4 +33,4 @@ declare const LockValue: z.ZodObject<{
32
33
  }>;
33
34
  type LockValueType = z.infer<typeof LockValue>;
34
35
 
35
- export { APPWARDEN_CACHE_KEY, LOCKDOWN_TEST_EXPIRY_MS, type LockValueType, getEdgeConfigId, isCacheUrl, isValidCacheUrl };
36
+ export { APPWARDEN_CACHE_KEY, APPWARDEN_USER_AGENT, LOCKDOWN_TEST_EXPIRY_MS, type LockValueType, getEdgeConfigId, isCacheUrl, isValidCacheUrl };
package/index.js CHANGED
@@ -1,17 +1,19 @@
1
1
  import {
2
2
  useContentSecurityPolicy
3
- } from "./chunk-SDYUAIDZ.js";
3
+ } from "./chunk-6DI6L6AE.js";
4
4
  import {
5
5
  APPWARDEN_CACHE_KEY,
6
+ APPWARDEN_USER_AGENT,
6
7
  CSPDirectivesSchema,
7
8
  CSPModeSchema,
8
9
  LOCKDOWN_TEST_EXPIRY_MS,
9
10
  getEdgeConfigId,
10
11
  isCacheUrl,
11
12
  isValidCacheUrl
12
- } from "./chunk-RKUPO5Z3.js";
13
+ } from "./chunk-GC5FS43G.js";
13
14
  export {
14
15
  APPWARDEN_CACHE_KEY,
16
+ APPWARDEN_USER_AGENT,
15
17
  CSPDirectivesSchema,
16
18
  CSPModeSchema,
17
19
  LOCKDOWN_TEST_EXPIRY_MS,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appwarden/middleware",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Instantly shut off access your app deployed on Cloudflare or Vercel",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -16,7 +16,7 @@
16
16
  "monitoring"
17
17
  ],
18
18
  "dependencies": {
19
- "@cloudflare/next-on-pages": "1.13.2",
19
+ "@cloudflare/next-on-pages": "1.13.5",
20
20
  "@upstash/redis": "^1.30.0",
21
21
  "@vercel/edge-config": "^1.1.0",
22
22
  "zod": "^3"
@@ -188,9 +188,9 @@ declare const CSPDirectivesSchema: z.ZodUnion<[z.ZodString, z.ZodObject<{
188
188
  "trusted-types"?: string | boolean | string[] | undefined;
189
189
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
190
190
  }>]>;
191
- declare const CSPModeSchema: z.ZodDefault<z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodLiteral<"disabled">, z.ZodLiteral<"report-only">]>, z.ZodLiteral<"enforced">]>>>;
191
+ declare const CSPModeSchema: z.ZodDefault<z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"disabled">, z.ZodLiteral<"report-only">, z.ZodLiteral<"enforced">]>>>;
192
192
  declare const UseCSPInputSchema: z.ZodEffects<z.ZodObject<{
193
- mode: z.ZodDefault<z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodLiteral<"disabled">, z.ZodLiteral<"report-only">]>, z.ZodLiteral<"enforced">]>>>;
193
+ mode: z.ZodDefault<z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"disabled">, z.ZodLiteral<"report-only">, z.ZodLiteral<"enforced">]>>>;
194
194
  directives: z.ZodEffects<z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{
195
195
  "default-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
196
196
  "script-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
package/vercel.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  APPWARDEN_CACHE_KEY,
3
+ APPWARDEN_USER_AGENT,
3
4
  AppwardenConfigSchema,
4
5
  BaseNextJsConfigSchema,
5
6
  MemoryCache,
@@ -10,7 +11,7 @@ import {
10
11
  isCacheUrl,
11
12
  printMessage,
12
13
  syncEdgeValue
13
- } from "./chunk-RKUPO5Z3.js";
14
+ } from "./chunk-GC5FS43G.js";
14
15
 
15
16
  // src/runners/appwarden-on-vercel.ts
16
17
  import { NextResponse } from "next/server";
@@ -36,14 +37,13 @@ var appwardenOnVercel = (input) => async (req, event) => {
36
37
  }
37
38
  try {
38
39
  const requestUrl = new URL(req.url);
39
- const acceptHeader = req.headers.get("accept");
40
- const isHTMLRequest = acceptHeader?.includes("text/html");
40
+ const isHTMLRequest = req.headers.get("accept")?.includes("text/html");
41
+ const isMonitoringRequest = req.headers.get("User-Agent") === APPWARDEN_USER_AGENT;
41
42
  debug({
42
- acceptHeader,
43
43
  isHTMLRequest,
44
44
  url: requestUrl.pathname
45
45
  });
46
- if (isHTMLRequest) {
46
+ if (isHTMLRequest && !isMonitoringRequest) {
47
47
  let appwardenResponse = void 0;
48
48
  const context = {
49
49
  req,