wrangler 0.0.0-ece06ea → 0.0.0-ecef68635

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.
Files changed (108) hide show
  1. package/README.md +50 -15
  2. package/bin/wrangler.js +94 -31
  3. package/config-schema.json +3074 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +155 -75
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +170 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-pretty-error.ts +40 -0
  23. package/templates/middleware/middleware-scheduled.ts +29 -0
  24. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  25. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  26. package/templates/modules-watch-stub.js +4 -0
  27. package/templates/new-worker-scheduled.js +17 -0
  28. package/templates/new-worker-scheduled.ts +32 -0
  29. package/templates/new-worker.js +15 -0
  30. package/templates/new-worker.ts +33 -0
  31. package/templates/no-op-worker.js +10 -0
  32. package/templates/pages-dev-pipeline.ts +32 -0
  33. package/templates/pages-dev-util.ts +55 -0
  34. package/templates/pages-shim.ts +9 -0
  35. package/templates/pages-template-plugin.ts +190 -0
  36. package/templates/pages-template-worker.ts +198 -0
  37. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  38. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  39. package/templates/tsconfig-sanity.ts +11 -0
  40. package/templates/tsconfig.init.json +22 -0
  41. package/templates/tsconfig.json +8 -0
  42. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  43. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  44. package/wrangler-dist/ProxyWorker.js +241 -0
  45. package/wrangler-dist/ProxyWorker.js.map +6 -0
  46. package/wrangler-dist/cli.d.ts +26463 -0
  47. package/wrangler-dist/cli.js +206335 -125492
  48. package/wrangler-dist/wasm-sync.wasm +0 -0
  49. package/import_meta_url.js +0 -3
  50. package/miniflare-config-stubs/.env.empty +0 -0
  51. package/miniflare-config-stubs/package.empty.json +0 -1
  52. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  53. package/src/__tests__/clipboardy-mock.js +0 -4
  54. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  55. package/src/__tests__/index.test.ts +0 -153
  56. package/src/__tests__/mock-cfetch.js +0 -46
  57. package/src/api/form_data.ts +0 -158
  58. package/src/api/inspect.ts +0 -441
  59. package/src/api/preview.ts +0 -123
  60. package/src/api/worker.ts +0 -161
  61. package/src/cfetch.ts +0 -72
  62. package/src/cli.ts +0 -10
  63. package/src/config.ts +0 -124
  64. package/src/dev.tsx +0 -736
  65. package/src/dialogs.tsx +0 -85
  66. package/src/index.tsx +0 -1845
  67. package/src/kv.tsx +0 -211
  68. package/src/pages.tsx +0 -344
  69. package/src/publish.ts +0 -354
  70. package/src/sites.tsx +0 -115
  71. package/src/tail.tsx +0 -71
  72. package/src/user.tsx +0 -1029
  73. package/src/util/fetch.ts +0 -74
  74. package/static-asset-facade.js +0 -47
  75. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  76. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  77. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  78. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  79. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  80. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  81. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  82. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  83. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  84. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  85. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  86. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  87. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  88. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  89. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  90. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  91. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  92. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  93. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  94. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  95. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  96. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  97. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  98. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  99. package/vendor/wrangler-mime/LICENSE +0 -21
  100. package/vendor/wrangler-mime/Mime.js +0 -97
  101. package/vendor/wrangler-mime/README.md +0 -187
  102. package/vendor/wrangler-mime/cli.js +0 -46
  103. package/vendor/wrangler-mime/index.js +0 -4
  104. package/vendor/wrangler-mime/lite.js +0 -4
  105. package/vendor/wrangler-mime/package.json +0 -52
  106. package/vendor/wrangler-mime/types/other.js +0 -1
  107. package/vendor/wrangler-mime/types/standard.js +0 -1
  108. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,56 @@
1
+ /// <reference path="middleware-serve-static-assets.d.ts"/>
2
+
3
+ import manifest from "__STATIC_CONTENT_MANIFEST";
4
+ import {
5
+ getAssetFromKV,
6
+ MethodNotAllowedError,
7
+ NotFoundError,
8
+ serveSinglePageApp,
9
+ } from "@cloudflare/kv-asset-handler";
10
+ import { cacheControl, spaMode } from "config:middleware/serve-static-assets";
11
+ import type { Middleware } from "./common";
12
+ import type { Options } from "@cloudflare/kv-asset-handler";
13
+ import type * as kvAssetHandler from "@cloudflare/kv-asset-handler";
14
+
15
+ const ASSET_MANIFEST = JSON.parse(manifest);
16
+
17
+ const staticAssets: Middleware = async (request, env, ctx, middlewareCtx) => {
18
+ let options: Partial<Options> = {
19
+ ASSET_MANIFEST,
20
+ ASSET_NAMESPACE: env.__STATIC_CONTENT,
21
+ cacheControl: cacheControl,
22
+ mapRequestToAsset: spaMode ? serveSinglePageApp : undefined,
23
+ };
24
+
25
+ try {
26
+ const page = await (getAssetFromKV as typeof kvAssetHandler.getAssetFromKV)(
27
+ {
28
+ request,
29
+ waitUntil(promise) {
30
+ return ctx.waitUntil(promise);
31
+ },
32
+ },
33
+ options
34
+ );
35
+
36
+ // allow headers to be altered
37
+ const response = new Response(page.body, page);
38
+
39
+ response.headers.set("X-XSS-Protection", "1; mode=block");
40
+ response.headers.set("X-Content-Type-Options", "nosniff");
41
+ response.headers.set("X-Frame-Options", "DENY");
42
+ response.headers.set("Referrer-Policy", "unsafe-url");
43
+ response.headers.set("Feature-Policy", "none");
44
+
45
+ return response;
46
+ } catch (e) {
47
+ if (e instanceof NotFoundError || e instanceof MethodNotAllowedError) {
48
+ // if a known error is thrown then serve from actual worker
49
+ return await middlewareCtx.next(request, env);
50
+ }
51
+ // otherwise it's a real error, so throw it
52
+ throw e;
53
+ }
54
+ };
55
+
56
+ export default staticAssets;
@@ -0,0 +1,4 @@
1
+ // `esbuild` doesn't support returning `watch*` options from `onStart()`
2
+ // plugin callbacks. Instead, we define an empty virtual module that is
3
+ // imported by this injected file. Importing the module registers watchers.
4
+ import "wrangler:modules-watch";
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first scheduled worker.
3
+ *
4
+ * - Run `wrangler dev --local` in your terminal to start a development server
5
+ * - Run `curl "http://localhost:8787/cdn-cgi/mf/scheduled"` to trigger the scheduled event
6
+ * - Go back to the console to see what your worker has logged
7
+ * - Update the Cron trigger in wrangler.toml (see https://developers.cloudflare.com/workers/wrangler/configuration/#triggers)
8
+ * - Run `wrangler publish --name my-worker` to publish your worker
9
+ *
10
+ * Learn more at https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/
11
+ */
12
+
13
+ export default {
14
+ async scheduled(controller, env, ctx) {
15
+ console.log(`Hello World!`);
16
+ },
17
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first scheduled worker.
3
+ *
4
+ * - Run `wrangler dev --local` in your terminal to start a development server
5
+ * - Run `curl "http://localhost:8787/cdn-cgi/mf/scheduled"` to trigger the scheduled event
6
+ * - Go back to the console to see what your worker has logged
7
+ * - Update the Cron trigger in wrangler.toml (see https://developers.cloudflare.com/workers/wrangler/configuration/#triggers)
8
+ * - Run `wrangler deploy --name my-worker` to deploy your worker
9
+ *
10
+ * Learn more at https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/
11
+ */
12
+
13
+ export interface Env {
14
+ // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
15
+ // MY_KV_NAMESPACE: KVNamespace;
16
+ //
17
+ // Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
18
+ // MY_DURABLE_OBJECT: DurableObjectNamespace;
19
+ //
20
+ // Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
21
+ // MY_BUCKET: R2Bucket;
22
+ }
23
+
24
+ export default {
25
+ async scheduled(
26
+ controller: ScheduledController,
27
+ env: Env,
28
+ ctx: ExecutionContext
29
+ ): Promise<void> {
30
+ console.log(`Hello World!`);
31
+ },
32
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first worker.
3
+ *
4
+ * - Run `npx wrangler dev src/index.js` in your terminal to start a development server
5
+ * - Open a browser tab at http://localhost:8787/ to see your worker in action
6
+ * - Run `npx wrangler publish src/index.js --name my-worker` to publish your worker
7
+ *
8
+ * Learn more at https://developers.cloudflare.com/workers/
9
+ */
10
+
11
+ export default {
12
+ async fetch(request, env, ctx) {
13
+ return new Response("Hello World!");
14
+ },
15
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Welcome to Cloudflare Workers! This is your first worker.
3
+ *
4
+ * - Run `wrangler dev src/index.ts` in your terminal to start a development server
5
+ * - Open a browser tab at http://localhost:8787/ to see your worker in action
6
+ * - Run `wrangler deploy src/index.ts --name my-worker` to deploy your worker
7
+ *
8
+ * Learn more at https://developers.cloudflare.com/workers/
9
+ */
10
+
11
+ export interface Env {
12
+ // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
13
+ // MY_KV_NAMESPACE: KVNamespace;
14
+ //
15
+ // Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
16
+ // MY_DURABLE_OBJECT: DurableObjectNamespace;
17
+ //
18
+ // Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
19
+ // MY_BUCKET: R2Bucket;
20
+ //
21
+ // Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
22
+ // MY_SERVICE: Fetcher;
23
+ }
24
+
25
+ export default {
26
+ async fetch(
27
+ request: Request,
28
+ env: Env,
29
+ ctx: ExecutionContext
30
+ ): Promise<Response> {
31
+ return new Response("Hello World!");
32
+ },
33
+ };
@@ -0,0 +1,10 @@
1
+ export default {
2
+ fetch() {
3
+ return new Response("Not found", {
4
+ status: 404,
5
+ headers: {
6
+ "Content-Type": "text/html",
7
+ },
8
+ });
9
+ },
10
+ };
@@ -0,0 +1,32 @@
1
+ // @ts-ignore entry point will get replaced
2
+ import worker from "__ENTRY_POINT__";
3
+ import { isRoutingRuleMatch } from "./pages-dev-util";
4
+
5
+ // @ts-ignore entry point will get replaced
6
+ export * from "__ENTRY_POINT__";
7
+
8
+ // @ts-ignore routes are injected
9
+ const routes = __ROUTES__;
10
+
11
+ export default <ExportedHandler<{ ASSETS: Fetcher }>>{
12
+ fetch(request, env, context) {
13
+ const { pathname } = new URL(request.url);
14
+
15
+ for (const exclude of routes.exclude) {
16
+ if (isRoutingRuleMatch(pathname, exclude)) {
17
+ return env.ASSETS.fetch(request);
18
+ }
19
+ }
20
+
21
+ for (const include of routes.include) {
22
+ if (isRoutingRuleMatch(pathname, include)) {
23
+ if (worker.fetch === undefined) {
24
+ throw new TypeError("Entry point missing `fetch` handler");
25
+ }
26
+ return worker.fetch(request, env, context);
27
+ }
28
+ }
29
+
30
+ return env.ASSETS.fetch(request);
31
+ },
32
+ };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @param pathname A pathname string, such as `/foo` or `/foo/bar`
3
+ * @param routingRule The routing rule, such as `/foo/*`
4
+ * @returns True if pathname matches the routing rule
5
+ *
6
+ * / -> /
7
+ * /* -> /*
8
+ * /foo -> /foo
9
+ * /foo* -> /foo, /foo-bar, /foo/*
10
+ * /foo/* -> /foo, /foo/bar
11
+ */
12
+ export function isRoutingRuleMatch(
13
+ pathname: string,
14
+ routingRule: string
15
+ ): boolean {
16
+ // sanity checks
17
+ if (!pathname) {
18
+ throw new Error("Pathname is undefined.");
19
+ }
20
+ if (!routingRule) {
21
+ throw new Error("Routing rule is undefined.");
22
+ }
23
+
24
+ const ruleRegExp = transformRoutingRuleToRegExp(routingRule);
25
+ return pathname.match(ruleRegExp) !== null;
26
+ }
27
+
28
+ function transformRoutingRuleToRegExp(rule: string): RegExp {
29
+ let transformedRule;
30
+
31
+ if (rule === "/" || rule === "/*") {
32
+ transformedRule = rule;
33
+ } else if (rule.endsWith("/*")) {
34
+ // make `/*` an optional group so we can match both /foo/* and /foo
35
+ // /foo/* => /foo(/*)?
36
+ transformedRule = `${rule.substring(0, rule.length - 2)}(/*)?`;
37
+ } else if (rule.endsWith("/")) {
38
+ // make `/` an optional group so we can match both /foo/ and /foo
39
+ // /foo/ => /foo(/)?
40
+ transformedRule = `${rule.substring(0, rule.length - 1)}(/)?`;
41
+ } else if (rule.endsWith("*")) {
42
+ transformedRule = rule;
43
+ } else {
44
+ transformedRule = `${rule}(/)?`;
45
+ }
46
+
47
+ // /foo* => /foo.* => ^/foo.*$
48
+ // /*.* => /*\.* => /.*\..* => ^/.*\..*$
49
+ transformedRule = `^${transformedRule
50
+ .replaceAll(/\./g, "\\.")
51
+ .replaceAll(/\*/g, ".*")}$`;
52
+
53
+ // ^/foo.*$ => /^\/foo.*$/
54
+ return new RegExp(transformedRule);
55
+ }
@@ -0,0 +1,9 @@
1
+ // This Worker is used as a default when no Pages Functions are present.
2
+ // It proxies the request directly on to the asset server binding.
3
+
4
+ export default <ExportedHandler<{ ASSETS: Fetcher }>>{
5
+ async fetch(request, env, context) {
6
+ const response = await env.ASSETS.fetch(request.url, request);
7
+ return new Response(response.body, response);
8
+ },
9
+ };
@@ -0,0 +1,190 @@
1
+ import { match } from "path-to-regexp";
2
+
3
+ //note: this explicitly does not include the * character, as pages requires this
4
+ const escapeRegex = /[.+?^${}()|[\]\\]/g;
5
+
6
+ type HTTPMethod =
7
+ | "HEAD"
8
+ | "OPTIONS"
9
+ | "GET"
10
+ | "POST"
11
+ | "PUT"
12
+ | "PATCH"
13
+ | "DELETE";
14
+
15
+ /* TODO: Grab these from @cloudflare/workers-types instead */
16
+ type Params<P extends string = string> = Record<P, string | string[]>;
17
+
18
+ type EventContext<Env, P extends string, Data> = {
19
+ request: Request;
20
+ functionPath: string;
21
+ waitUntil: (promise: Promise<unknown>) => void;
22
+ passThroughOnException: () => void;
23
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
24
+ env: Env & { ASSETS: { fetch: typeof fetch } };
25
+ params: Params<P>;
26
+ data: Data;
27
+ };
28
+
29
+ type EventPluginContext<Env, P extends string, Data, PluginArgs> = {
30
+ request: Request;
31
+ functionPath: string;
32
+ waitUntil: (promise: Promise<unknown>) => void;
33
+ passThroughOnException: () => void;
34
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
35
+ env: Env & { ASSETS: { fetch: typeof fetch } };
36
+ params: Params<P>;
37
+ data: Data;
38
+ pluginArgs: PluginArgs;
39
+ };
40
+
41
+ declare type PagesFunction<
42
+ Env = unknown,
43
+ P extends string = string,
44
+ Data extends Record<string, unknown> = Record<string, unknown>,
45
+ > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
46
+
47
+ declare type PagesPluginFunction<
48
+ Env = unknown,
49
+ P extends string = string,
50
+ Data extends Record<string, unknown> = Record<string, unknown>,
51
+ PluginArgs = unknown,
52
+ > = (
53
+ context: EventPluginContext<Env, P, Data, PluginArgs>
54
+ ) => Response | Promise<Response>;
55
+ /* end @cloudflare/workers-types */
56
+
57
+ type RouteHandler = {
58
+ routePath: string;
59
+ mountPath: string;
60
+ method?: HTTPMethod;
61
+ modules: PagesFunction[];
62
+ middlewares: PagesFunction[];
63
+ };
64
+
65
+ // inject `routes` via ESBuild
66
+ declare const routes: RouteHandler[];
67
+
68
+ function* executeRequest(request: Request, relativePathname: string) {
69
+ // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
70
+ for (const route of [...routes].reverse()) {
71
+ if (route.method && route.method !== request.method) {
72
+ continue;
73
+ }
74
+
75
+ // replaces with "\\$&", this prepends a backslash to the matched string, e.g. "[" becomes "\["
76
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
77
+ end: false,
78
+ });
79
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
80
+ end: false,
81
+ });
82
+ const matchResult = routeMatcher(relativePathname);
83
+ const mountMatchResult = mountMatcher(relativePathname);
84
+ if (matchResult && mountMatchResult) {
85
+ for (const handler of route.middlewares.flat()) {
86
+ yield {
87
+ handler,
88
+ params: matchResult.params as Params,
89
+ path: mountMatchResult.path,
90
+ };
91
+ }
92
+ }
93
+ }
94
+
95
+ // Then look for the first exact route match and execute its "modules"
96
+ for (const route of routes) {
97
+ if (route.method && route.method !== request.method) {
98
+ continue;
99
+ }
100
+
101
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
102
+ end: true,
103
+ });
104
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
105
+ end: false,
106
+ });
107
+ const matchResult = routeMatcher(relativePathname);
108
+ const mountMatchResult = mountMatcher(relativePathname);
109
+ if (matchResult && mountMatchResult && route.modules.length) {
110
+ for (const handler of route.modules.flat()) {
111
+ yield {
112
+ handler,
113
+ params: matchResult.params as Params,
114
+ path: matchResult.path,
115
+ };
116
+ }
117
+ break;
118
+ }
119
+ }
120
+ }
121
+
122
+ export default function (pluginArgs: unknown) {
123
+ const onRequest: PagesPluginFunction = async (workerContext) => {
124
+ let { request } = workerContext;
125
+ const { env, next } = workerContext;
126
+ let { data } = workerContext;
127
+
128
+ const url = new URL(request.url);
129
+ // TODO: Replace this with something actually legible.
130
+ const relativePathname = `/${
131
+ url.pathname.replace(workerContext.functionPath, "") || ""
132
+ }`.replace(/^\/\//, "/");
133
+
134
+ const handlerIterator = executeRequest(request, relativePathname);
135
+ const pluginNext = async (input?: RequestInfo, init?: RequestInit) => {
136
+ if (input !== undefined) {
137
+ let url = input;
138
+ if (typeof input === "string") {
139
+ url = new URL(input, request.url).toString();
140
+ }
141
+ request = new Request(url, init);
142
+ }
143
+
144
+ const result = handlerIterator.next();
145
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
146
+ if (result.done === false) {
147
+ const { handler, params, path } = result.value;
148
+ const context = {
149
+ request: new Request(request.clone()),
150
+ functionPath: workerContext.functionPath + path,
151
+ next: pluginNext,
152
+ params,
153
+ get data() {
154
+ return data;
155
+ },
156
+ set data(value) {
157
+ if (typeof value !== "object" || value === null) {
158
+ throw new Error("context.data must be an object");
159
+ }
160
+ // user has overriden context.data, so we need to merge it with the existing data
161
+ data = value;
162
+ },
163
+ pluginArgs,
164
+ env,
165
+ waitUntil: workerContext.waitUntil.bind(workerContext),
166
+ passThroughOnException:
167
+ workerContext.passThroughOnException.bind(workerContext),
168
+ };
169
+
170
+ const response = await handler(context);
171
+
172
+ return cloneResponse(response);
173
+ } else {
174
+ return next(request);
175
+ }
176
+ };
177
+
178
+ return pluginNext();
179
+ };
180
+
181
+ return onRequest;
182
+ }
183
+
184
+ // This makes a Response mutable
185
+ const cloneResponse = (response: Response) =>
186
+ // https://fetch.spec.whatwg.org/#null-body-status
187
+ new Response(
188
+ [101, 204, 205, 304].includes(response.status) ? null : response.body,
189
+ response
190
+ );
@@ -0,0 +1,198 @@
1
+ import { match } from "path-to-regexp";
2
+
3
+ //note: this explicitly does not include the * character, as pages requires this
4
+ const escapeRegex = /[.+?^${}()|[\]\\]/g;
5
+
6
+ type HTTPMethod =
7
+ | "HEAD"
8
+ | "OPTIONS"
9
+ | "GET"
10
+ | "POST"
11
+ | "PUT"
12
+ | "PATCH"
13
+ | "DELETE";
14
+
15
+ /* TODO: Grab these from @cloudflare/workers-types instead */
16
+ type Params<P extends string = string> = Record<P, string | string[]>;
17
+
18
+ type EventContext<Env, P extends string, Data> = {
19
+ request: Request;
20
+ functionPath: string;
21
+ waitUntil: (promise: Promise<unknown>) => void;
22
+ passThroughOnException: () => void;
23
+ next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
24
+ env: Env & { ASSETS: { fetch: typeof fetch } };
25
+ params: Params<P>;
26
+ data: Data;
27
+ };
28
+
29
+ declare type PagesFunction<
30
+ Env = unknown,
31
+ P extends string = string,
32
+ Data extends Record<string, unknown> = Record<string, unknown>,
33
+ > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
34
+ /* end @cloudflare/workers-types */
35
+
36
+ type RouteHandler = {
37
+ routePath: string;
38
+ mountPath: string;
39
+ method?: HTTPMethod;
40
+ modules: PagesFunction[];
41
+ middlewares: PagesFunction[];
42
+ };
43
+
44
+ // inject `routes` via ESBuild
45
+ declare const routes: RouteHandler[];
46
+ // define `__FALLBACK_SERVICE__` via ESBuild
47
+ declare const __FALLBACK_SERVICE__: string;
48
+
49
+ // expect an ASSETS fetcher binding pointing to the asset-server stage
50
+ type FetchEnv = {
51
+ [name: string]: { fetch: typeof fetch };
52
+ ASSETS: { fetch: typeof fetch };
53
+ };
54
+
55
+ type WorkerContext = {
56
+ waitUntil: (promise: Promise<unknown>) => void;
57
+ passThroughOnException: () => void;
58
+ };
59
+
60
+ function* executeRequest(request: Request) {
61
+ const requestPath = new URL(request.url).pathname;
62
+
63
+ // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
64
+ for (const route of [...routes].reverse()) {
65
+ if (route.method && route.method !== request.method) {
66
+ continue;
67
+ }
68
+
69
+ // replaces with "\\$&", this prepends a backslash to the matched string, e.g. "[" becomes "\["
70
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
71
+ end: false,
72
+ });
73
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
74
+ end: false,
75
+ });
76
+ const matchResult = routeMatcher(requestPath);
77
+ const mountMatchResult = mountMatcher(requestPath);
78
+ if (matchResult && mountMatchResult) {
79
+ for (const handler of route.middlewares.flat()) {
80
+ yield {
81
+ handler,
82
+ params: matchResult.params as Params,
83
+ path: mountMatchResult.path,
84
+ };
85
+ }
86
+ }
87
+ }
88
+
89
+ // Then look for the first exact route match and execute its "modules"
90
+ for (const route of routes) {
91
+ if (route.method && route.method !== request.method) {
92
+ continue;
93
+ }
94
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
95
+ end: true,
96
+ });
97
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
98
+ end: false,
99
+ });
100
+ const matchResult = routeMatcher(requestPath);
101
+ const mountMatchResult = mountMatcher(requestPath);
102
+ if (matchResult && mountMatchResult && route.modules.length) {
103
+ for (const handler of route.modules.flat()) {
104
+ yield {
105
+ handler,
106
+ params: matchResult.params as Params,
107
+ path: matchResult.path,
108
+ };
109
+ }
110
+ break;
111
+ }
112
+ }
113
+ }
114
+
115
+ export default {
116
+ async fetch(
117
+ originalRequest: Request,
118
+ env: FetchEnv,
119
+ workerContext: WorkerContext
120
+ ) {
121
+ let request = originalRequest;
122
+ const handlerIterator = executeRequest(request);
123
+ let data = {}; // arbitrary data the user can set between functions
124
+ let isFailOpen = false;
125
+
126
+ const next = async (input?: RequestInfo, init?: RequestInit) => {
127
+ if (input !== undefined) {
128
+ let url = input;
129
+ if (typeof input === "string") {
130
+ url = new URL(input, request.url).toString();
131
+ }
132
+ request = new Request(url, init);
133
+ }
134
+
135
+ const result = handlerIterator.next();
136
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
137
+ if (result.done === false) {
138
+ const { handler, params, path } = result.value;
139
+ const context = {
140
+ request: new Request(request.clone()),
141
+ functionPath: path,
142
+ next,
143
+ params,
144
+ get data() {
145
+ return data;
146
+ },
147
+ set data(value) {
148
+ if (typeof value !== "object" || value === null) {
149
+ throw new Error("context.data must be an object");
150
+ }
151
+ // user has overriden context.data, so we need to merge it with the existing data
152
+ data = value;
153
+ },
154
+ env,
155
+ waitUntil: workerContext.waitUntil.bind(workerContext),
156
+ passThroughOnException: () => {
157
+ isFailOpen = true;
158
+ },
159
+ };
160
+
161
+ const response = await handler(context);
162
+
163
+ if (!(response instanceof Response)) {
164
+ throw new Error("Your Pages function should return a Response");
165
+ }
166
+
167
+ return cloneResponse(response);
168
+ } else if (__FALLBACK_SERVICE__) {
169
+ // There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
170
+ const response = await env[__FALLBACK_SERVICE__].fetch(request);
171
+ return cloneResponse(response);
172
+ } else {
173
+ // There was not fallback service so actually make the request to the origin.
174
+ const response = await fetch(request);
175
+ return cloneResponse(response);
176
+ }
177
+ };
178
+
179
+ try {
180
+ return await next();
181
+ } catch (error) {
182
+ if (isFailOpen) {
183
+ const response = await env[__FALLBACK_SERVICE__].fetch(request);
184
+ return cloneResponse(response);
185
+ }
186
+
187
+ throw error;
188
+ }
189
+ },
190
+ };
191
+
192
+ // This makes a Response mutable
193
+ const cloneResponse = (response: Response) =>
194
+ // https://fetch.spec.whatwg.org/#null-body-status
195
+ new Response(
196
+ [101, 204, 205, 304].includes(response.status) ? null : response.body,
197
+ response
198
+ );