@appwarden/middleware 1.4.3 → 1.5.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @appwarden/middleware
2
2
 
3
- ![Test Coverage](https://img.shields.io/badge/coverage-92.74%25-brightgreen)
3
+ ![Test Coverage](https://img.shields.io/badge/coverage-97%25-brightgreen)
4
4
  [![npm version](https://img.shields.io/npm/v/@appwarden/middleware.svg)](https://www.npmjs.com/package/@appwarden/middleware)
5
5
  [![npm provenance](https://img.shields.io/badge/npm-provenance-green)](https://docs.npmjs.com/generating-provenance-statements)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
@@ -49,6 +49,7 @@ var CSPModeSchema = z2.union([
49
49
  z2.literal("enforced")
50
50
  ]).optional().default("disabled");
51
51
  var UseCSPInputSchema = z2.object({
52
+ hostname: z2.string().optional(),
52
53
  mode: CSPModeSchema,
53
54
  directives: CSPDirectivesSchema.optional().refine(
54
55
  (val) => {
@@ -114,6 +115,9 @@ var useContentSecurityPolicy = (input) => {
114
115
  const config = parsedInput.data;
115
116
  return async (context, next) => {
116
117
  await next();
118
+ if (config.hostname && context.hostname !== config.hostname) {
119
+ return;
120
+ }
117
121
  const { response } = context;
118
122
  if (
119
123
  // if the csp is disabled
@@ -94,6 +94,7 @@ type Bindings = {
94
94
  CSP_MODE: "disabled" | "report-only" | "enforced";
95
95
  CSP_DIRECTIVES: string | ContentSecurityPolicyType;
96
96
  APPWARDEN_API_TOKEN: string;
97
+ APPWARDEN_API_HOSTNAME?: string;
97
98
  };
98
99
 
99
100
  export type { Bindings as B };
package/cloudflare.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { B as Bindings } from './cloudflare-2PkEr25r.js';
1
+ import { B as Bindings } from './cloudflare-0sboFQGC.js';
2
2
  import { z } from 'zod';
3
- import { M as Middleware } from './use-content-security-policy-C89AROtC.js';
4
- export { u as useContentSecurityPolicy } from './use-content-security-policy-C89AROtC.js';
3
+ import { M as Middleware } from './use-content-security-policy-D4ccKJVd.js';
4
+ export { u as useContentSecurityPolicy } from './use-content-security-policy-D4ccKJVd.js';
5
5
 
6
6
  declare const ConfigFnInputSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<{
7
7
  env: CloudflareEnv;
@@ -11,10 +11,18 @@ declare const ConfigFnInputSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<{
11
11
  env: CloudflareEnv;
12
12
  cf: Record<string, unknown>;
13
13
  ctx: unknown;
14
- }>], z.ZodUnknown>, z.ZodObject<{
14
+ }>], z.ZodUnknown>, z.ZodEffects<z.ZodObject<{
15
15
  debug: z.ZodDefault<z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodBoolean]>>, boolean, string | boolean | undefined>>;
16
- lockPageSlug: z.ZodString;
16
+ lockPageSlug: z.ZodOptional<z.ZodString>;
17
+ multidomainConfig: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
18
+ lockPageSlug: z.ZodString;
19
+ }, "strip", z.ZodTypeAny, {
20
+ lockPageSlug: string;
21
+ }, {
22
+ lockPageSlug: string;
23
+ }>>>;
17
24
  appwardenApiToken: z.ZodEffects<z.ZodString, string, string>;
25
+ appwardenApiHostname: z.ZodOptional<z.ZodString>;
18
26
  } & {
19
27
  middleware: z.ZodDefault<z.ZodObject<{
20
28
  before: z.ZodDefault<z.ZodArray<z.ZodType<Middleware, z.ZodTypeDef, Middleware>, "many">>;
@@ -25,19 +33,27 @@ declare const ConfigFnInputSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<{
25
33
  }>>;
26
34
  }, "strip", z.ZodTypeAny, {
27
35
  debug: boolean;
28
- lockPageSlug: string;
29
36
  appwardenApiToken: string;
30
37
  middleware: {
31
38
  before: Middleware[];
32
39
  };
40
+ lockPageSlug?: string | undefined;
41
+ multidomainConfig?: Record<string, {
42
+ lockPageSlug: string;
43
+ }> | undefined;
44
+ appwardenApiHostname?: string | undefined;
33
45
  }, {
34
- lockPageSlug: string;
35
46
  appwardenApiToken: string;
47
+ lockPageSlug?: string | undefined;
36
48
  debug?: string | boolean | undefined;
49
+ multidomainConfig?: Record<string, {
50
+ lockPageSlug: string;
51
+ }> | undefined;
52
+ appwardenApiHostname?: string | undefined;
37
53
  middleware?: {
38
54
  before?: Middleware[] | undefined;
39
55
  } | undefined;
40
- }>>;
56
+ }>, any, any>>;
41
57
  type CloudflareConfigType = ReturnType<z.infer<typeof ConfigFnInputSchema>>;
42
58
 
43
59
  declare const withAppwarden: (inputFn: CloudflareConfigType) => ExportedHandlerFetchHandler<Bindings>;
package/cloudflare.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  useContentSecurityPolicy
3
- } from "./chunk-GTGPYCQY.js";
3
+ } from "./chunk-TP5CHUTK.js";
4
4
  import {
5
5
  BooleanSchema,
6
6
  LockValue,
@@ -23,17 +23,33 @@ import { z as z2 } from "zod";
23
23
 
24
24
  // src/schemas/use-appwarden.ts
25
25
  import { z } from "zod";
26
+ var AppwardenMultidomainConfigSchema = z.record(
27
+ z.string(),
28
+ z.object({
29
+ lockPageSlug: z.string()
30
+ })
31
+ );
26
32
  var UseAppwardenInputSchema = z.object({
27
33
  debug: BooleanSchema.default(false),
28
- lockPageSlug: z.string(),
29
- appwardenApiToken: z.string().refine((val) => !!val, { path: ["appwardenApiToken"] })
34
+ lockPageSlug: z.string().optional(),
35
+ multidomainConfig: AppwardenMultidomainConfigSchema.optional(),
36
+ appwardenApiToken: z.string().refine((val) => !!val, { path: ["appwardenApiToken"] }),
37
+ appwardenApiHostname: z.string().optional()
30
38
  });
39
+ var lockPageSlugRefinement = (schema) => schema.refine(
40
+ (data) => data.lockPageSlug || data.multidomainConfig,
41
+ {
42
+ message: "lockPageSlug must be provided"
43
+ }
44
+ );
31
45
 
32
46
  // src/schemas/cloudflare.ts
33
47
  var ConfigFnInputSchema = z2.function().args(z2.custom()).returns(
34
- UseAppwardenInputSchema.extend({
35
- middleware: z2.object({ before: z2.custom().array().default([]) }).default({})
36
- })
48
+ lockPageSlugRefinement(
49
+ UseAppwardenInputSchema.extend({
50
+ middleware: z2.object({ before: z2.custom().array().default([]) }).default({})
51
+ })
52
+ )
37
53
  );
38
54
 
39
55
  // src/utils/middleware.ts
@@ -198,10 +214,12 @@ var APIError = class extends Error {
198
214
  this.name = "APIError";
199
215
  }
200
216
  };
217
+ var DEFAULT_API_HOSTNAME = "https://api.appwarden.io";
201
218
  var syncEdgeValue = async (context) => {
202
219
  debug(`syncing with api`);
203
220
  try {
204
- const response = await fetch(new URL("/v1/status/check", "https://api.appwarden.io"), {
221
+ const apiHostname = context.appwardenApiHostname ?? DEFAULT_API_HOSTNAME;
222
+ const response = await fetch(new URL("/v1/status/check", apiHostname), {
205
223
  method: "POST",
206
224
  headers: { "content-type": "application/json" },
207
225
  body: JSON.stringify({
@@ -312,6 +330,10 @@ var useAppwarden = (input) => async (context, next) => {
312
330
  const isHTMLRequest = response.headers.get("Content-Type")?.includes("text/html");
313
331
  const isMonitoringRequest = request.headers.get("User-Agent") === APPWARDEN_USER_AGENT;
314
332
  if (isHTMLRequest && !isMonitoringRequest) {
333
+ const lockPageSlug = input.multidomainConfig?.[requestUrl.hostname]?.lockPageSlug ?? input.lockPageSlug;
334
+ if (!lockPageSlug) {
335
+ return;
336
+ }
315
337
  const innerContext = {
316
338
  keyName,
317
339
  request,
@@ -319,8 +341,9 @@ var useAppwarden = (input) => async (context, next) => {
319
341
  requestUrl,
320
342
  provider,
321
343
  debug: input.debug,
322
- lockPageSlug: input.lockPageSlug,
344
+ lockPageSlug,
323
345
  appwardenApiToken: input.appwardenApiToken,
346
+ appwardenApiHostname: input.appwardenApiHostname,
324
347
  waitUntil: (fn) => context.waitUntil(fn)
325
348
  };
326
349
  await maybeQuarantine(innerContext, {
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { B as Bindings } from './cloudflare-2PkEr25r.js';
2
- export { C as CSPDirectivesSchema, a as CSPModeSchema, M as Middleware, u as useContentSecurityPolicy } from './use-content-security-policy-C89AROtC.js';
1
+ export { B as Bindings } from './cloudflare-0sboFQGC.js';
2
+ export { C as CSPDirectivesSchema, a as CSPModeSchema, M as Middleware, u as useContentSecurityPolicy } from './use-content-security-policy-D4ccKJVd.js';
3
3
  import { z } from 'zod';
4
4
 
5
5
  declare const LOCKDOWN_TEST_EXPIRY_MS: number;
package/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  CSPDirectivesSchema,
8
8
  CSPModeSchema,
9
9
  useContentSecurityPolicy
10
- } from "./chunk-GTGPYCQY.js";
10
+ } from "./chunk-TP5CHUTK.js";
11
11
  import {
12
12
  APPWARDEN_CACHE_KEY,
13
13
  APPWARDEN_USER_AGENT,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appwarden/middleware",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
4
4
  "description": "Instantly shut off access your app deployed on Cloudflare or Vercel",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1,4 +1,4 @@
1
- import './cloudflare-2PkEr25r.js';
1
+ import './cloudflare-0sboFQGC.js';
2
2
  import { z } from 'zod';
3
3
 
4
4
  interface MiddlewareContext {
@@ -95,6 +95,7 @@ declare const CSPDirectivesSchema: z.ZodUnion<[z.ZodString, z.ZodObject<{
95
95
  }>]>;
96
96
  declare const CSPModeSchema: z.ZodDefault<z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"disabled">, z.ZodLiteral<"report-only">, z.ZodLiteral<"enforced">]>>>;
97
97
  declare const UseCSPInputSchema: z.ZodEffects<z.ZodObject<{
98
+ hostname: z.ZodOptional<z.ZodString>;
98
99
  mode: z.ZodDefault<z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"disabled">, z.ZodLiteral<"report-only">, z.ZodLiteral<"enforced">]>>>;
99
100
  directives: z.ZodEffects<z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodObject<{
100
101
  "default-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
@@ -316,6 +317,7 @@ declare const UseCSPInputSchema: z.ZodEffects<z.ZodObject<{
316
317
  "trusted-types"?: string | boolean | string[] | undefined;
317
318
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
318
319
  } | undefined;
320
+ hostname?: string | undefined;
319
321
  }, {
320
322
  mode?: "disabled" | "report-only" | "enforced" | undefined;
321
323
  directives?: string | {
@@ -346,6 +348,7 @@ declare const UseCSPInputSchema: z.ZodEffects<z.ZodObject<{
346
348
  "trusted-types"?: string | boolean | string[] | undefined;
347
349
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
348
350
  } | undefined;
351
+ hostname?: string | undefined;
349
352
  }>, {
350
353
  mode: "disabled" | "report-only" | "enforced";
351
354
  directives?: {
@@ -376,6 +379,7 @@ declare const UseCSPInputSchema: z.ZodEffects<z.ZodObject<{
376
379
  "trusted-types"?: string | boolean | string[] | undefined;
377
380
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
378
381
  } | undefined;
382
+ hostname?: string | undefined;
379
383
  }, {
380
384
  mode?: "disabled" | "report-only" | "enforced" | undefined;
381
385
  directives?: string | {
@@ -406,6 +410,7 @@ declare const UseCSPInputSchema: z.ZodEffects<z.ZodObject<{
406
410
  "trusted-types"?: string | boolean | string[] | undefined;
407
411
  "require-trusted-types-for"?: string | boolean | string[] | undefined;
408
412
  } | undefined;
413
+ hostname?: string | undefined;
409
414
  }>;
410
415
  type UseCSPInput = z.infer<typeof UseCSPInputSchema>;
411
416
 
package/vercel.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as next_dist_server_web_types from 'next/dist/server/web/types';
2
2
  import * as next_server from 'next/server';
3
- import './cloudflare-2PkEr25r.js';
3
+ import './cloudflare-0sboFQGC.js';
4
4
  import { z } from 'zod';
5
5
 
6
6
  declare const BaseNextJsConfigSchema: z.ZodObject<{