@appwarden/middleware 1.1.1 → 1.1.3

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-YUM2I6AI.js";
13
+ } from "./chunk-5UMKWIJW.js";
13
14
 
14
15
  // src/utils/cloudflare/cloudflare-cache.ts
15
16
  var store = {
@@ -124,16 +125,22 @@ var getLockValue = async (context) => {
124
125
  };
125
126
 
126
127
  // src/utils/cloudflare/insert-errors-logs.ts
127
- var insertErrorLogs = async (context, error) => new HTMLRewriter().on("body", {
128
- element: (elem) => {
129
- elem.append(
130
- `<script>
131
- ${getErrors(error).map((err) => `console.error(\`${printMessage(err)}\`)`).join("\n")}
132
- </script>`,
133
- { html: true }
134
- );
128
+ var insertErrorLogs = async (context, error) => {
129
+ const errors = getErrors(error);
130
+ for (const err of errors) {
131
+ console.error(printMessage(err));
135
132
  }
136
- }).transform(await fetch(context.request));
133
+ return new HTMLRewriter().on("body", {
134
+ element: (elem) => {
135
+ elem.append(
136
+ `<script>
137
+ ${errors.map((err) => `console.error(\`${printMessage(err)}\`)`).join("\n")}
138
+ </script>`,
139
+ { html: true }
140
+ );
141
+ }
142
+ }).transform(await fetch(context.request));
143
+ };
137
144
 
138
145
  // src/utils/cloudflare/make-csp-header.ts
139
146
  var addNonce = (value, cspNonce) => value.replace("{{nonce}}", `'nonce-${cspNonce}'`);
@@ -323,7 +330,8 @@ var useAppwarden = (input) => async (context, next) => {
323
330
  return;
324
331
  }
325
332
  const isHTMLRequest = response.headers.get("Content-Type")?.includes("text/html");
326
- if (isHTMLRequest) {
333
+ const isMonitoringRequest = request.headers.get("User-Agent") === APPWARDEN_USER_AGENT;
334
+ if (isHTMLRequest && !isMonitoringRequest) {
327
335
  const innerContext = {
328
336
  keyName,
329
337
  request,
@@ -3,7 +3,8 @@ var LOCKDOWN_TEST_EXPIRY_MS = 5 * 60 * 1e3;
3
3
  var removedHeaders = ["X-Powered-By", "Server"];
4
4
  var errors = { badCacheConnection: "BAD_CACHE_CONNECTION" };
5
5
  var globalErrors = [errors.badCacheConnection];
6
- var APPWARDEN_TEST_ROUTE = "_appwarden/test";
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
@@ -503,6 +504,7 @@ export {
503
504
  removedHeaders,
504
505
  globalErrors,
505
506
  APPWARDEN_TEST_ROUTE,
507
+ APPWARDEN_USER_AGENT,
506
508
  APPWARDEN_CACHE_KEY,
507
509
  debug,
508
510
  getErrors,
package/cloudflare.js CHANGED
@@ -2,11 +2,11 @@ import {
2
2
  insertErrorLogs,
3
3
  useAppwarden,
4
4
  useContentSecurityPolicy
5
- } from "./chunk-MJ7JTAUY.js";
5
+ } from "./chunk-3STKCAEA.js";
6
6
  import {
7
7
  ConfigFnInputSchema,
8
8
  usePipeline
9
- } from "./chunk-YUM2I6AI.js";
9
+ } from "./chunk-5UMKWIJW.js";
10
10
 
11
11
  // src/runners/appwarden-on-cloudflare.ts
12
12
  import { ZodError } from "zod";
package/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export { B as Bindings, C as CSPDirectivesSchema, a as CSPModeSchema, M as Middl
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-MJ7JTAUY.js";
3
+ } from "./chunk-3STKCAEA.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-YUM2I6AI.js";
13
+ } from "./chunk-5UMKWIJW.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.1",
3
+ "version": "1.1.3",
4
4
  "description": "Instantly shut off access your app deployed on Cloudflare or Vercel",
5
5
  "type": "module",
6
6
  "license": "MIT",
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-YUM2I6AI.js";
14
+ } from "./chunk-5UMKWIJW.js";
14
15
 
15
16
  // src/runners/appwarden-on-vercel.ts
16
17
  import { NextResponse } from "next/server";
@@ -29,21 +30,20 @@ var appwardenOnVercel = (input) => async (req, event) => {
29
30
  event.passThroughOnException();
30
31
  const parsedConfig = AppwardenConfigSchema.safeParse(input);
31
32
  if (!parsedConfig.success) {
32
- getErrors(parsedConfig.error).map(
33
- (err) => `console.error(${printMessage(err)})`
34
- );
33
+ for (const error of getErrors(parsedConfig.error)) {
34
+ console.error(printMessage(error));
35
+ }
35
36
  return NextResponse.next();
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,