@appwarden/middleware 1.0.16

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,186 @@
1
+ import {
2
+ AppwardenConfigSchema,
3
+ CloudflareConfigFnOutputSchema,
4
+ MemoryCache,
5
+ NextJsConfigFnOutputSchema,
6
+ createResponse,
7
+ debug,
8
+ globalErrors,
9
+ handleResetCache,
10
+ handleVercelRequest,
11
+ isResetCacheRequest,
12
+ maybeQuarantine,
13
+ printMessage,
14
+ renderLockPage,
15
+ store,
16
+ syncEdgeValue,
17
+ useAppwarden,
18
+ usePipeline
19
+ } from "./chunk-C7APN7T6.js";
20
+
21
+ // src/runners/appwarden-on-cloudflare.ts
22
+ var appwardenOnCloudflare = (inputFn) => async (request, env, ctx) => {
23
+ const parsedInput = CloudflareConfigFnOutputSchema.safeParse(inputFn);
24
+ if (!parsedInput.success) {
25
+ throw new Error(
26
+ printMessage(`Input validation failed ${parsedInput.error.message}`)
27
+ );
28
+ }
29
+ const context = {
30
+ request,
31
+ hostname: new URL(request.url).host,
32
+ response: new Response("Unhandled response"),
33
+ // https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors
34
+ waitUntil: (fn) => ctx.waitUntil(fn)
35
+ };
36
+ try {
37
+ const input = parsedInput.data({ env, ctx, cf: {} });
38
+ const pipeline = [...input.middleware.before, useAppwarden(input)];
39
+ await usePipeline(...pipeline).execute(context);
40
+ } catch (error) {
41
+ if (error instanceof Error) {
42
+ context.response = createResponse(error.message, 500);
43
+ }
44
+ }
45
+ return context.response;
46
+ };
47
+
48
+ // src/runners/appwarden-on-pages-next-js.ts
49
+ import { getRequestContext } from "@cloudflare/next-on-pages";
50
+ import { NextResponse } from "next/server";
51
+ debug("Instantiating isolate");
52
+ var appwardenOnPagesNextJs = (inputFn) => async (request, event) => {
53
+ const parsedInput = NextJsConfigFnOutputSchema.safeParse(inputFn);
54
+ if (!parsedInput.success) {
55
+ console.error(
56
+ printMessage(`Input validation failed ${parsedInput.error.message}`)
57
+ );
58
+ return null;
59
+ }
60
+ const input = parsedInput.data(getRequestContext());
61
+ try {
62
+ const requestUrl = new URL(request.url);
63
+ const provider = "cloudflare-cache";
64
+ const keyName = "appwarden-lock";
65
+ const edgeCache = store.json(
66
+ {
67
+ serviceOrigin: requestUrl.origin,
68
+ cache: await caches.open("appwarden:lock")
69
+ },
70
+ keyName
71
+ );
72
+ if (isResetCacheRequest(request)) {
73
+ await handleResetCache(keyName, provider, edgeCache, request);
74
+ return NextResponse.next();
75
+ }
76
+ const acceptHeader = request.headers.get("accept");
77
+ const isHTMLRequest = acceptHeader?.includes("text/html");
78
+ debug({
79
+ acceptHeader,
80
+ isHTMLRequest,
81
+ url: requestUrl.pathname
82
+ });
83
+ if (isHTMLRequest) {
84
+ let appwardenResponse = void 0;
85
+ const context = {
86
+ keyName,
87
+ request,
88
+ edgeCache,
89
+ requestUrl,
90
+ provider,
91
+ waitUntil: (fn) => event.waitUntil(fn),
92
+ ...input
93
+ };
94
+ await maybeQuarantine(context, {
95
+ onLocked: async () => {
96
+ appwardenResponse = renderLockPage(context);
97
+ }
98
+ });
99
+ if (appwardenResponse) {
100
+ return appwardenResponse;
101
+ }
102
+ }
103
+ } catch (e) {
104
+ const message = "Appwarden encountered an unknown error. Please contact Appwarden support at https://appwarden.io/join-community.";
105
+ console.error(
106
+ printMessage(
107
+ e instanceof Error ? `${message} - ${e.message}
108
+ ${e.stack}` : message
109
+ )
110
+ );
111
+ }
112
+ return NextResponse.next();
113
+ };
114
+
115
+ // src/runners/appwarden-on-vercel.ts
116
+ import { NextResponse as NextResponse2 } from "next/server";
117
+ debug("Instantiating isolate");
118
+ var renderLockPage2 = (context) => {
119
+ context.req.nextUrl.pathname = context.lockPageSlug;
120
+ return NextResponse2.rewrite(context.req.nextUrl, {
121
+ headers: {
122
+ // no browser caching, otherwise we need to hard refresh to disable lock screen
123
+ "Cache-Control": "no-store"
124
+ }
125
+ });
126
+ };
127
+ var memoryCache = new MemoryCache({ maxSize: 1 });
128
+ var appwardenOnVercel = (input) => async (req, event) => {
129
+ const parsedConfig = AppwardenConfigSchema.safeParse(input);
130
+ if (!parsedConfig.success) {
131
+ console.error(
132
+ printMessage(`Input validation failed ${parsedConfig.error.message}`)
133
+ );
134
+ return null;
135
+ }
136
+ try {
137
+ const requestUrl = new URL(req.url);
138
+ const acceptHeader = req.headers.get("accept");
139
+ const isHTMLRequest = acceptHeader?.includes("text/html");
140
+ debug({
141
+ acceptHeader,
142
+ isHTMLRequest,
143
+ url: requestUrl.pathname
144
+ });
145
+ if (isHTMLRequest) {
146
+ let appwardenResponse = void 0;
147
+ const context = {
148
+ req,
149
+ event,
150
+ requestUrl,
151
+ memoryCache,
152
+ waitUntil: (fn) => event.waitUntil(fn),
153
+ keyName: "appwarden-lock",
154
+ ...parsedConfig.data
155
+ };
156
+ const cacheValue = await handleVercelRequest(context, {
157
+ onLocked: () => {
158
+ appwardenResponse = renderLockPage2(context);
159
+ }
160
+ });
161
+ const shouldRecheck = MemoryCache.isExpired(cacheValue);
162
+ if (!cacheValue || shouldRecheck) {
163
+ event.waitUntil(syncEdgeValue(context));
164
+ }
165
+ if (appwardenResponse) {
166
+ return appwardenResponse;
167
+ }
168
+ }
169
+ } catch (e) {
170
+ const message = "Appwarden encountered an unknown error. Please contact Appwarden support at https://appwarden.io/join-community.";
171
+ if (e instanceof Error) {
172
+ if (!globalErrors.includes(e.message)) {
173
+ console.error(printMessage(`${message} - ${e.message}`));
174
+ }
175
+ } else {
176
+ console.error(printMessage(message));
177
+ }
178
+ }
179
+ return NextResponse2.next();
180
+ };
181
+
182
+ export {
183
+ appwardenOnCloudflare,
184
+ appwardenOnPagesNextJs,
185
+ appwardenOnVercel
186
+ };
@@ -0,0 +1,99 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const ContentSecurityPolicySchema: z.ZodObject<{
4
+ "default-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
5
+ "script-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
6
+ "style-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
7
+ "img-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
8
+ "connect-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
9
+ "font-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
10
+ "object-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
11
+ "media-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
12
+ "frame-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
13
+ sandbox: z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
14
+ "report-uri": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
15
+ "child-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
16
+ "form-action": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
17
+ "frame-ancestors": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
18
+ "plugin-types": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
19
+ "base-uri": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
20
+ "report-to": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
21
+ "worker-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
22
+ "manifest-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
23
+ "prefetch-src": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
24
+ "navigate-to": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
25
+ "require-sri-for": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
26
+ "block-all-mixed-content": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
27
+ "upgrade-insecure-requests": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
28
+ "trusted-types": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
29
+ "require-trusted-types-for": z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString, z.ZodBoolean]>>;
30
+ }, "strip", z.ZodTypeAny, {
31
+ "default-src"?: string | boolean | string[] | undefined;
32
+ "script-src"?: string | boolean | string[] | undefined;
33
+ "style-src"?: string | boolean | string[] | undefined;
34
+ "img-src"?: string | boolean | string[] | undefined;
35
+ "connect-src"?: string | boolean | string[] | undefined;
36
+ "font-src"?: string | boolean | string[] | undefined;
37
+ "object-src"?: string | boolean | string[] | undefined;
38
+ "media-src"?: string | boolean | string[] | undefined;
39
+ "frame-src"?: string | boolean | string[] | undefined;
40
+ sandbox?: string | boolean | string[] | undefined;
41
+ "report-uri"?: string | boolean | string[] | undefined;
42
+ "child-src"?: string | boolean | string[] | undefined;
43
+ "form-action"?: string | boolean | string[] | undefined;
44
+ "frame-ancestors"?: string | boolean | string[] | undefined;
45
+ "plugin-types"?: string | boolean | string[] | undefined;
46
+ "base-uri"?: string | boolean | string[] | undefined;
47
+ "report-to"?: string | boolean | string[] | undefined;
48
+ "worker-src"?: string | boolean | string[] | undefined;
49
+ "manifest-src"?: string | boolean | string[] | undefined;
50
+ "prefetch-src"?: string | boolean | string[] | undefined;
51
+ "navigate-to"?: string | boolean | string[] | undefined;
52
+ "require-sri-for"?: string | boolean | string[] | undefined;
53
+ "block-all-mixed-content"?: string | boolean | string[] | undefined;
54
+ "upgrade-insecure-requests"?: string | boolean | string[] | undefined;
55
+ "trusted-types"?: string | boolean | string[] | undefined;
56
+ "require-trusted-types-for"?: string | boolean | string[] | undefined;
57
+ }, {
58
+ "default-src"?: string | boolean | string[] | undefined;
59
+ "script-src"?: string | boolean | string[] | undefined;
60
+ "style-src"?: string | boolean | string[] | undefined;
61
+ "img-src"?: string | boolean | string[] | undefined;
62
+ "connect-src"?: string | boolean | string[] | undefined;
63
+ "font-src"?: string | boolean | string[] | undefined;
64
+ "object-src"?: string | boolean | string[] | undefined;
65
+ "media-src"?: string | boolean | string[] | undefined;
66
+ "frame-src"?: string | boolean | string[] | undefined;
67
+ sandbox?: string | boolean | string[] | undefined;
68
+ "report-uri"?: string | boolean | string[] | undefined;
69
+ "child-src"?: string | boolean | string[] | undefined;
70
+ "form-action"?: string | boolean | string[] | undefined;
71
+ "frame-ancestors"?: string | boolean | string[] | undefined;
72
+ "plugin-types"?: string | boolean | string[] | undefined;
73
+ "base-uri"?: string | boolean | string[] | undefined;
74
+ "report-to"?: string | boolean | string[] | undefined;
75
+ "worker-src"?: string | boolean | string[] | undefined;
76
+ "manifest-src"?: string | boolean | string[] | undefined;
77
+ "prefetch-src"?: string | boolean | string[] | undefined;
78
+ "navigate-to"?: string | boolean | string[] | undefined;
79
+ "require-sri-for"?: string | boolean | string[] | undefined;
80
+ "block-all-mixed-content"?: string | boolean | string[] | undefined;
81
+ "upgrade-insecure-requests"?: string | boolean | string[] | undefined;
82
+ "trusted-types"?: string | boolean | string[] | undefined;
83
+ "require-trusted-types-for"?: string | boolean | string[] | undefined;
84
+ }>;
85
+ type ContentSecurityPolicyType = z.infer<typeof ContentSecurityPolicySchema>;
86
+
87
+ declare global {
88
+ interface CloudflareEnv extends Bindings {
89
+ }
90
+ }
91
+ type Bindings = {
92
+ DEBUG: string | boolean;
93
+ LOCK_PAGE_SLUG: string;
94
+ CSP_ENFORCED: string | boolean;
95
+ CSP_DIRECTIVES: string | ContentSecurityPolicyType;
96
+ APPWARDEN_API_TOKEN: string;
97
+ };
98
+
99
+ export type { Bindings as B };
@@ -0,0 +1,47 @@
1
+ import { B as Bindings } from './cloudflare-hVS30fDq.js';
2
+ import { z } from 'zod';
3
+ import { M as Middleware } from './use-content-security-policy-BtEGGIeu.js';
4
+ export { u as useContentSecurityPolicy } from './use-content-security-policy-BtEGGIeu.js';
5
+
6
+ declare const ConfigFnInputSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<{
7
+ env: CloudflareEnv;
8
+ cf: Record<string, unknown>;
9
+ ctx: unknown;
10
+ }, z.ZodTypeDef, {
11
+ env: CloudflareEnv;
12
+ cf: Record<string, unknown>;
13
+ ctx: unknown;
14
+ }>], z.ZodUnknown>, z.ZodObject<z.objectUtil.extendShape<z.objectUtil.extendShape<{
15
+ debug: z.ZodDefault<z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodBoolean]>>, boolean, string | boolean | undefined>>;
16
+ appwardenApiToken: z.ZodEffects<z.ZodString, string, string>;
17
+ lockPageSlug: z.ZodString;
18
+ }, {
19
+ middleware: z.ZodDefault<z.ZodObject<{
20
+ before: z.ZodDefault<z.ZodArray<z.ZodType<Middleware, z.ZodTypeDef, Middleware>, "many">>;
21
+ }, "strip", z.ZodTypeAny, {
22
+ before: Middleware[];
23
+ }, {
24
+ before?: Middleware[] | undefined;
25
+ }>>;
26
+ }>, {
27
+ debug: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodBoolean]>>;
28
+ }>, "strip", z.ZodTypeAny, {
29
+ lockPageSlug: string;
30
+ appwardenApiToken: string;
31
+ middleware: {
32
+ before: Middleware[];
33
+ };
34
+ debug?: string | boolean | undefined;
35
+ }, {
36
+ lockPageSlug: string;
37
+ appwardenApiToken: string;
38
+ debug?: string | boolean | undefined;
39
+ middleware?: {
40
+ before?: Middleware[] | undefined;
41
+ } | undefined;
42
+ }>>;
43
+ type CloudflareConfigInputFnType = z.infer<typeof ConfigFnInputSchema>;
44
+
45
+ declare const withAppwardenOnCloudflare: (inputFn: CloudflareConfigInputFnType) => ExportedHandlerFetchHandler<Bindings>;
46
+
47
+ export { withAppwardenOnCloudflare };
package/cloudflare.js ADDED
@@ -0,0 +1,13 @@
1
+ import {
2
+ appwardenOnCloudflare
3
+ } from "./chunk-JXIVUR6E.js";
4
+ import {
5
+ useContentSecurityPolicy
6
+ } from "./chunk-C7APN7T6.js";
7
+
8
+ // src/bundles/cloudflare.ts
9
+ var withAppwardenOnCloudflare = appwardenOnCloudflare;
10
+ export {
11
+ useContentSecurityPolicy,
12
+ withAppwardenOnCloudflare
13
+ };
package/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ export { B as Bindings } from './cloudflare-hVS30fDq.js';
2
+ export { C as CSPDirectivesSchema, a as CSPEnforcedSchema, M as Middleware, u as useContentSecurityPolicy } from './use-content-security-policy-BtEGGIeu.js';
3
+ import { z } from 'zod';
4
+
5
+ declare const LOCKDOWN_TEST_EXPIRY_MS: number;
6
+
7
+ declare const LockValue: z.ZodObject<{
8
+ isLocked: z.ZodNumber;
9
+ isLockedTest: z.ZodNumber;
10
+ lastCheck: z.ZodNumber;
11
+ code: z.ZodString;
12
+ }, "strip", z.ZodTypeAny, {
13
+ isLocked: number;
14
+ isLockedTest: number;
15
+ lastCheck: number;
16
+ code: string;
17
+ }, {
18
+ isLocked: number;
19
+ isLockedTest: number;
20
+ lastCheck: number;
21
+ code: string;
22
+ }>;
23
+ type LockValueType = z.infer<typeof LockValue>;
24
+
25
+ export { LOCKDOWN_TEST_EXPIRY_MS, type LockValueType };
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import {
2
+ CSPDirectivesSchema,
3
+ CSPEnforcedSchema,
4
+ LOCKDOWN_TEST_EXPIRY_MS,
5
+ useContentSecurityPolicy
6
+ } from "./chunk-C7APN7T6.js";
7
+ export {
8
+ CSPDirectivesSchema,
9
+ CSPEnforcedSchema,
10
+ LOCKDOWN_TEST_EXPIRY_MS,
11
+ useContentSecurityPolicy
12
+ };
@@ -0,0 +1,30 @@
1
+ import * as next_server from 'next/server';
2
+ import './cloudflare-hVS30fDq.js';
3
+ import { z } from 'zod';
4
+
5
+ declare const NextJsConfigFnOutputSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<{
6
+ env: CloudflareEnv;
7
+ cf: Record<string, unknown>;
8
+ ctx: unknown;
9
+ }, z.ZodTypeDef, {
10
+ env: CloudflareEnv;
11
+ cf: Record<string, unknown>;
12
+ ctx: unknown;
13
+ }>], z.ZodUnknown>, z.ZodObject<{
14
+ debug: z.ZodDefault<z.ZodEffects<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodBoolean]>>, boolean, string | boolean | undefined>>;
15
+ appwardenApiToken: z.ZodEffects<z.ZodString, string, string>;
16
+ lockPageSlug: z.ZodString;
17
+ }, "strip", z.ZodTypeAny, {
18
+ lockPageSlug: string;
19
+ appwardenApiToken: string;
20
+ debug: boolean;
21
+ }, {
22
+ lockPageSlug: string;
23
+ appwardenApiToken: string;
24
+ debug?: string | boolean | undefined;
25
+ }>>;
26
+ type NextJsConfigFnType = z.infer<typeof NextJsConfigFnOutputSchema>;
27
+
28
+ declare const withAppwardenOnPagesNextJs: (inputFn: NextJsConfigFnType) => next_server.NextMiddleware;
29
+
30
+ export { withAppwardenOnPagesNextJs };
@@ -0,0 +1,10 @@
1
+ import {
2
+ appwardenOnPagesNextJs
3
+ } from "./chunk-JXIVUR6E.js";
4
+ import "./chunk-C7APN7T6.js";
5
+
6
+ // src/bundles/pages-nextjs.ts
7
+ var withAppwardenOnPagesNextJs = appwardenOnPagesNextJs;
8
+ export {
9
+ withAppwardenOnPagesNextJs
10
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@appwarden/middleware",
3
+ "version": "1.0.16",
4
+ "description": "Instantly shut off access your app deployed on Cloudflare or Vercel",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Appwarden <support@appwarden.io>",
8
+ "main": "./index.js",
9
+ "types": "./index.d.ts",
10
+ "keywords": [
11
+ "appwarden",
12
+ "nextjs",
13
+ "remixjs",
14
+ "cloudflare",
15
+ "web3",
16
+ "monitoring"
17
+ ],
18
+ "dependencies": {
19
+ "@cloudflare/next-on-pages": "1.13.2",
20
+ "@upstash/redis": "^1.30.0",
21
+ "@vercel/edge-config": "^1.1.0",
22
+ "zod": "^3"
23
+ },
24
+ "peerDependencies": {
25
+ "next": ">=13"
26
+ },
27
+ "peerDependenciesMeta": {
28
+ "next": {
29
+ "optional": true
30
+ }
31
+ },
32
+ "publishConfig": {
33
+ "registry": "https://registry.npmjs.org",
34
+ "access": "public"
35
+ }
36
+ }