wrangler 2.0.12 → 2.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.
- package/README.md +7 -1
- package/bin/wrangler.js +111 -57
- package/miniflare-dist/index.mjs +9 -2
- package/package.json +156 -154
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +30 -24
- package/src/__tests__/configuration.test.ts +3935 -3476
- package/src/__tests__/dev.test.tsx +1128 -979
- package/src/__tests__/guess-worker-format.test.ts +68 -68
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/faye-websocket.d.ts +4 -4
- package/src/__tests__/helpers/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-cfetch.ts +92 -92
- package/src/__tests__/helpers/mock-console.ts +49 -39
- package/src/__tests__/helpers/mock-dialogs.ts +94 -71
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- package/src/__tests__/helpers/mock-kv.ts +26 -26
- package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
- package/src/__tests__/helpers/mock-process.ts +39 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/run-in-tmp.ts +27 -27
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/write-worker-source.ts +16 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +104 -104
- package/src/__tests__/index.test.ts +239 -234
- package/src/__tests__/init.test.ts +1605 -1250
- package/src/__tests__/jest.setup.ts +63 -33
- package/src/__tests__/kv.test.ts +1128 -1011
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages.test.ts +1152 -652
- package/src/__tests__/parse.test.ts +252 -252
- package/src/__tests__/publish.test.ts +6371 -5622
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +133 -133
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +382 -377
- package/src/__tests__/tail.test.ts +530 -530
- package/src/__tests__/user.test.ts +123 -111
- package/src/__tests__/whoami.test.tsx +198 -117
- package/src/__tests__/worker-namespace.test.ts +327 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +49 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +157 -149
- package/src/cfetch/index.ts +80 -80
- package/src/cfetch/internal.ts +90 -83
- package/src/cli.ts +21 -7
- package/src/config/config.ts +204 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +390 -357
- package/src/config/index.ts +206 -193
- package/src/config/validation-helpers.ts +366 -366
- package/src/config/validation.ts +1573 -1376
- package/src/config-cache.ts +79 -41
- package/src/create-worker-preview.ts +206 -136
- package/src/create-worker-upload-form.ts +247 -238
- package/src/dev/dev-vars.ts +13 -13
- package/src/dev/dev.tsx +329 -307
- package/src/dev/local.tsx +304 -275
- package/src/dev/remote.tsx +366 -224
- package/src/dev/use-esbuild.ts +126 -91
- package/src/dev.tsx +538 -0
- package/src/dialogs.tsx +97 -97
- package/src/durable.ts +87 -87
- package/src/entry.ts +234 -228
- package/src/environment-variables.ts +23 -23
- package/src/errors.ts +6 -6
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/https-options.ts +79 -79
- package/src/index.tsx +1775 -2763
- package/src/init.ts +549 -0
- package/src/inspect.ts +593 -593
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +277 -277
- package/src/logger.ts +46 -39
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +42 -31
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +212 -212
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +123 -123
- package/src/pages/build.tsx +202 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +101 -0
- package/src/pages/dev.tsx +964 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -0
- package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/index.tsx +84 -0
- package/src/pages/projects.tsx +157 -0
- package/src/pages/publish.tsx +335 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +384 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +202 -138
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +400 -402
- package/src/publish.ts +667 -621
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +577 -0
- package/src/r2.ts +19 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +271 -225
- package/src/tail/filters.ts +108 -108
- package/src/tail/index.ts +217 -217
- package/src/tail/printing.ts +45 -45
- package/src/update-check.ts +11 -11
- package/src/user/choose-account.tsx +60 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1161 -0
- package/src/whoami.tsx +61 -42
- package/src/worker-namespace.ts +190 -0
- package/src/worker.ts +110 -100
- package/src/zones.ts +39 -36
- package/templates/checked-fetch.js +17 -0
- package/templates/new-worker-scheduled.js +3 -3
- package/templates/new-worker-scheduled.ts +15 -15
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +15 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +31 -31
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.js +55383 -54138
- package/pages/functions/buildPlugin.ts +0 -105
- package/pages/functions/buildWorker.ts +0 -151
- package/pages/functions/filepath-routing.ts +0 -189
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -156
- package/pages/functions/template-plugin.ts +0 -147
- package/pages/functions/template-worker.ts +0 -143
- package/src/pages.tsx +0 -2093
- package/src/user.tsx +0 -1214
package/src/zones.ts
CHANGED
|
@@ -5,8 +5,18 @@ import type { Route } from "./config/environment";
|
|
|
5
5
|
* An object holding information about a zone for publishing.
|
|
6
6
|
*/
|
|
7
7
|
export interface Zone {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
id: string;
|
|
9
|
+
host: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getHostFromRoute(route: Route): string | undefined {
|
|
13
|
+
return typeof route === "string"
|
|
14
|
+
? getHostFromUrl(route)
|
|
15
|
+
: typeof route === "object"
|
|
16
|
+
? "zone_name" in route
|
|
17
|
+
? getHostFromUrl(route.zone_name)
|
|
18
|
+
: getHostFromUrl(route.pattern)
|
|
19
|
+
: undefined;
|
|
10
20
|
}
|
|
11
21
|
|
|
12
22
|
/**
|
|
@@ -17,34 +27,27 @@ export interface Zone {
|
|
|
17
27
|
* - We try to get a zone id from the host
|
|
18
28
|
*/
|
|
19
29
|
export async function getZoneForRoute(route: Route): Promise<Zone | undefined> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const id =
|
|
29
|
-
typeof route === "object" && "zone_id" in route
|
|
30
|
-
? route.zone_id
|
|
31
|
-
: host
|
|
32
|
-
? await getZoneIdFromHost(host)
|
|
33
|
-
: undefined;
|
|
34
|
-
return id && host ? { id, host } : undefined;
|
|
30
|
+
const host = getHostFromRoute(route);
|
|
31
|
+
const id =
|
|
32
|
+
typeof route === "object" && "zone_id" in route
|
|
33
|
+
? route.zone_id
|
|
34
|
+
: host
|
|
35
|
+
? await getZoneIdFromHost(host)
|
|
36
|
+
: undefined;
|
|
37
|
+
return id && host ? { id, host } : undefined;
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
/**
|
|
38
41
|
* Given something that resembles a URL, try to extract a host from it.
|
|
39
42
|
*/
|
|
40
43
|
function getHostFromUrl(urlLike: string): string | undefined {
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
// strip leading * / *.
|
|
45
|
+
urlLike = urlLike.replace(/^\*(\.)?/g, "");
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
if (!(urlLike.startsWith("http://") || urlLike.startsWith("https://"))) {
|
|
48
|
+
urlLike = "http://" + urlLike;
|
|
49
|
+
}
|
|
50
|
+
return new URL(urlLike).host;
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
/**
|
|
@@ -55,19 +58,19 @@ function getHostFromUrl(urlLike: string): string | undefined {
|
|
|
55
58
|
* lopping off subdomains until we get a hit from the API.
|
|
56
59
|
*/
|
|
57
60
|
export async function getZoneIdFromHost(host: string): Promise<string> {
|
|
58
|
-
|
|
61
|
+
const hostPieces = host.split(".");
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
while (hostPieces.length > 1) {
|
|
64
|
+
const zones = await fetchListResult<{ id: string }>(
|
|
65
|
+
`/zones`,
|
|
66
|
+
{},
|
|
67
|
+
new URLSearchParams({ name: hostPieces.join(".") })
|
|
68
|
+
);
|
|
69
|
+
if (zones.length > 0) {
|
|
70
|
+
return zones[0].id;
|
|
71
|
+
}
|
|
72
|
+
hostPieces.shift();
|
|
73
|
+
}
|
|
71
74
|
|
|
72
|
-
|
|
75
|
+
throw new Error(`Could not find zone for ${host}`);
|
|
73
76
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const urls = new Set();
|
|
2
|
+
|
|
3
|
+
export function checkedFetch(request, init) {
|
|
4
|
+
const url = new URL(
|
|
5
|
+
(typeof request === "string" ? new Request(request, init) : request).url
|
|
6
|
+
);
|
|
7
|
+
if (url.port && url.port !== "443" && url.protocol === "https:") {
|
|
8
|
+
if (!urls.has(url.toString())) {
|
|
9
|
+
urls.add(url.toString());
|
|
10
|
+
console.warn(
|
|
11
|
+
`WARNING: known issue with \`fetch()\` requests to custom HTTPS ports in published Workers:\n` +
|
|
12
|
+
` - ${url.toString()} - the custom port will be ignored when the Worker is published using the \`wrangler publish\` command.\n`
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return globalThis.fetch(request, init);
|
|
17
|
+
}
|
|
@@ -11,22 +11,22 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
export interface Env {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
22
|
}
|
|
23
23
|
|
|
24
24
|
export default {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
async scheduled(
|
|
26
|
+
controller: ScheduledController,
|
|
27
|
+
env: Env,
|
|
28
|
+
ctx: ExecutionContext
|
|
29
|
+
): Promise<void> {
|
|
30
|
+
console.log(`Hello World!`);
|
|
31
|
+
},
|
|
32
32
|
};
|
package/templates/new-worker.js
CHANGED
package/templates/new-worker.ts
CHANGED
|
@@ -9,22 +9,22 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
export interface Env {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
20
|
}
|
|
21
21
|
|
|
22
22
|
export default {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
async fetch(
|
|
24
|
+
request: Request,
|
|
25
|
+
env: Env,
|
|
26
|
+
ctx: ExecutionContext
|
|
27
|
+
): Promise<Response> {
|
|
28
|
+
return new Response("Hello World!");
|
|
29
|
+
},
|
|
30
30
|
};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
|
|
3
|
+
type HTTPMethod =
|
|
4
|
+
| "HEAD"
|
|
5
|
+
| "OPTIONS"
|
|
6
|
+
| "GET"
|
|
7
|
+
| "POST"
|
|
8
|
+
| "PUT"
|
|
9
|
+
| "PATCH"
|
|
10
|
+
| "DELETE";
|
|
11
|
+
|
|
12
|
+
/* TODO: Grab these from @cloudflare/workers-types instead */
|
|
13
|
+
type Params<P extends string = string> = Record<P, string | string[]>;
|
|
14
|
+
|
|
15
|
+
type EventContext<Env, P extends string, Data> = {
|
|
16
|
+
request: Request;
|
|
17
|
+
functionPath: string;
|
|
18
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
19
|
+
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
|
|
20
|
+
env: Env & { ASSETS: { fetch: typeof fetch } };
|
|
21
|
+
params: Params<P>;
|
|
22
|
+
data: Data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type EventPluginContext<Env, P extends string, Data, PluginArgs> = {
|
|
26
|
+
request: Request;
|
|
27
|
+
functionPath: string;
|
|
28
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
29
|
+
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
|
|
30
|
+
env: Env & { ASSETS: { fetch: typeof fetch } };
|
|
31
|
+
params: Params<P>;
|
|
32
|
+
data: Data;
|
|
33
|
+
pluginArgs: PluginArgs;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
declare type PagesFunction<
|
|
37
|
+
Env = unknown,
|
|
38
|
+
P extends string = string,
|
|
39
|
+
Data extends Record<string, unknown> = Record<string, unknown>
|
|
40
|
+
> = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
|
|
41
|
+
|
|
42
|
+
declare type PagesPluginFunction<
|
|
43
|
+
Env = unknown,
|
|
44
|
+
P extends string = string,
|
|
45
|
+
Data extends Record<string, unknown> = Record<string, unknown>,
|
|
46
|
+
PluginArgs = unknown
|
|
47
|
+
> = (
|
|
48
|
+
context: EventPluginContext<Env, P, Data, PluginArgs>
|
|
49
|
+
) => Response | Promise<Response>;
|
|
50
|
+
/* end @cloudflare/workers-types */
|
|
51
|
+
|
|
52
|
+
type RouteHandler = {
|
|
53
|
+
routePath: string;
|
|
54
|
+
mountPath: string;
|
|
55
|
+
method?: HTTPMethod;
|
|
56
|
+
modules: PagesFunction[];
|
|
57
|
+
middlewares: PagesFunction[];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// inject `routes` via ESBuild
|
|
61
|
+
declare const routes: RouteHandler[];
|
|
62
|
+
|
|
63
|
+
function* executeRequest(request: Request, relativePathname: string) {
|
|
64
|
+
// First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
|
|
65
|
+
for (const route of [...routes].reverse()) {
|
|
66
|
+
if (route.method && route.method !== request.method) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const routeMatcher = match(route.routePath, { end: false });
|
|
71
|
+
const mountMatcher = match(route.mountPath, { end: false });
|
|
72
|
+
const matchResult = routeMatcher(relativePathname);
|
|
73
|
+
const mountMatchResult = mountMatcher(relativePathname);
|
|
74
|
+
if (matchResult && mountMatchResult) {
|
|
75
|
+
for (const handler of route.middlewares.flat()) {
|
|
76
|
+
yield {
|
|
77
|
+
handler,
|
|
78
|
+
params: matchResult.params as Params,
|
|
79
|
+
path: mountMatchResult.path,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Then look for the first exact route match and execute its "modules"
|
|
86
|
+
for (const route of routes) {
|
|
87
|
+
if (route.method && route.method !== request.method) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const routeMatcher = match(route.routePath, { end: true });
|
|
92
|
+
const mountMatcher = match(route.mountPath, { end: false });
|
|
93
|
+
const matchResult = routeMatcher(relativePathname);
|
|
94
|
+
const mountMatchResult = mountMatcher(relativePathname);
|
|
95
|
+
if (matchResult && mountMatchResult && route.modules.length) {
|
|
96
|
+
for (const handler of route.modules.flat()) {
|
|
97
|
+
yield {
|
|
98
|
+
handler,
|
|
99
|
+
params: matchResult.params as Params,
|
|
100
|
+
path: matchResult.path,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default function (pluginArgs) {
|
|
109
|
+
const onRequest: PagesPluginFunction = async (workerContext) => {
|
|
110
|
+
let { request } = workerContext;
|
|
111
|
+
const { env, next, data } = workerContext;
|
|
112
|
+
|
|
113
|
+
const url = new URL(request.url);
|
|
114
|
+
const relativePathname = `/${
|
|
115
|
+
url.pathname.split(workerContext.functionPath)[1] || ""
|
|
116
|
+
}`.replace(/^\/\//, "/");
|
|
117
|
+
|
|
118
|
+
const handlerIterator = executeRequest(request, relativePathname);
|
|
119
|
+
const pluginNext = async (input?: RequestInfo, init?: RequestInit) => {
|
|
120
|
+
if (input !== undefined) {
|
|
121
|
+
request = new Request(input, init);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const result = handlerIterator.next();
|
|
125
|
+
// Note we can't use `!result.done` because this doesn't narrow to the correct type
|
|
126
|
+
if (result.done === false) {
|
|
127
|
+
const { handler, params, path } = result.value;
|
|
128
|
+
const context = {
|
|
129
|
+
request,
|
|
130
|
+
functionPath: workerContext.functionPath + path,
|
|
131
|
+
next: pluginNext,
|
|
132
|
+
params,
|
|
133
|
+
data,
|
|
134
|
+
pluginArgs,
|
|
135
|
+
env,
|
|
136
|
+
waitUntil: workerContext.waitUntil.bind(workerContext),
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const response = await handler(context);
|
|
140
|
+
|
|
141
|
+
// https://fetch.spec.whatwg.org/#null-body-status
|
|
142
|
+
return new Response(
|
|
143
|
+
[101, 204, 205, 304].includes(response.status) ? null : response.body,
|
|
144
|
+
{ ...response, headers: new Headers(response.headers) }
|
|
145
|
+
);
|
|
146
|
+
} else {
|
|
147
|
+
return next();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
return pluginNext();
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return onRequest;
|
|
155
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
|
|
3
|
+
type HTTPMethod =
|
|
4
|
+
| "HEAD"
|
|
5
|
+
| "OPTIONS"
|
|
6
|
+
| "GET"
|
|
7
|
+
| "POST"
|
|
8
|
+
| "PUT"
|
|
9
|
+
| "PATCH"
|
|
10
|
+
| "DELETE";
|
|
11
|
+
|
|
12
|
+
/* TODO: Grab these from @cloudflare/workers-types instead */
|
|
13
|
+
type Params<P extends string = string> = Record<P, string | string[]>;
|
|
14
|
+
|
|
15
|
+
type EventContext<Env, P extends string, Data> = {
|
|
16
|
+
request: Request;
|
|
17
|
+
functionPath: string;
|
|
18
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
19
|
+
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
|
|
20
|
+
env: Env & { ASSETS: { fetch: typeof fetch } };
|
|
21
|
+
params: Params<P>;
|
|
22
|
+
data: Data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
declare type PagesFunction<
|
|
26
|
+
Env = unknown,
|
|
27
|
+
P extends string = string,
|
|
28
|
+
Data extends Record<string, unknown> = Record<string, unknown>
|
|
29
|
+
> = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
|
|
30
|
+
/* end @cloudflare/workers-types */
|
|
31
|
+
|
|
32
|
+
type RouteHandler = {
|
|
33
|
+
routePath: string;
|
|
34
|
+
mountPath: string;
|
|
35
|
+
method?: HTTPMethod;
|
|
36
|
+
modules: PagesFunction[];
|
|
37
|
+
middlewares: PagesFunction[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// inject `routes` via ESBuild
|
|
41
|
+
declare const routes: RouteHandler[];
|
|
42
|
+
// define `__FALLBACK_SERVICE__` via ESBuild
|
|
43
|
+
declare const __FALLBACK_SERVICE__: string;
|
|
44
|
+
|
|
45
|
+
// expect an ASSETS fetcher binding pointing to the asset-server stage
|
|
46
|
+
type FetchEnv = {
|
|
47
|
+
[name: string]: { fetch: typeof fetch };
|
|
48
|
+
ASSETS: { fetch: typeof fetch };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type WorkerContext = {
|
|
52
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
function* executeRequest(request: Request) {
|
|
56
|
+
const requestPath = new URL(request.url).pathname;
|
|
57
|
+
|
|
58
|
+
// First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
|
|
59
|
+
for (const route of [...routes].reverse()) {
|
|
60
|
+
if (route.method && route.method !== request.method) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const routeMatcher = match(route.routePath, { end: false });
|
|
65
|
+
const mountMatcher = match(route.mountPath, { end: false });
|
|
66
|
+
const matchResult = routeMatcher(requestPath);
|
|
67
|
+
const mountMatchResult = mountMatcher(requestPath);
|
|
68
|
+
if (matchResult && mountMatchResult) {
|
|
69
|
+
for (const handler of route.middlewares.flat()) {
|
|
70
|
+
yield {
|
|
71
|
+
handler,
|
|
72
|
+
params: matchResult.params as Params,
|
|
73
|
+
path: mountMatchResult.path,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Then look for the first exact route match and execute its "modules"
|
|
80
|
+
for (const route of routes) {
|
|
81
|
+
if (route.method && route.method !== request.method) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const routeMatcher = match(route.routePath, { end: true });
|
|
86
|
+
const mountMatcher = match(route.mountPath, { end: false });
|
|
87
|
+
const matchResult = routeMatcher(requestPath);
|
|
88
|
+
const mountMatchResult = mountMatcher(requestPath);
|
|
89
|
+
if (matchResult && mountMatchResult && route.modules.length) {
|
|
90
|
+
for (const handler of route.modules.flat()) {
|
|
91
|
+
yield {
|
|
92
|
+
handler,
|
|
93
|
+
params: matchResult.params as Params,
|
|
94
|
+
path: matchResult.path,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export default {
|
|
103
|
+
async fetch(request: Request, env: FetchEnv, workerContext: WorkerContext) {
|
|
104
|
+
const handlerIterator = executeRequest(request);
|
|
105
|
+
const data = {}; // arbitrary data the user can set between functions
|
|
106
|
+
const next = async (input?: RequestInfo, init?: RequestInit) => {
|
|
107
|
+
if (input !== undefined) {
|
|
108
|
+
let url = input;
|
|
109
|
+
if (typeof input === "string") {
|
|
110
|
+
url = new URL(input, request.url).toString();
|
|
111
|
+
}
|
|
112
|
+
request = new Request(url, init);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const result = handlerIterator.next();
|
|
116
|
+
// Note we can't use `!result.done` because this doesn't narrow to the correct type
|
|
117
|
+
if (result.done === false) {
|
|
118
|
+
const { handler, params, path } = result.value;
|
|
119
|
+
const context = {
|
|
120
|
+
request: new Request(request.clone()),
|
|
121
|
+
functionPath: path,
|
|
122
|
+
next,
|
|
123
|
+
params,
|
|
124
|
+
data,
|
|
125
|
+
env,
|
|
126
|
+
waitUntil: workerContext.waitUntil.bind(workerContext),
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const response = await handler(context);
|
|
130
|
+
|
|
131
|
+
if (!(response instanceof Response)) {
|
|
132
|
+
throw new Error("Your Pages function should return a Response");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return cloneResponse(response);
|
|
136
|
+
} else if (__FALLBACK_SERVICE__) {
|
|
137
|
+
// There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
|
|
138
|
+
const response = await env[__FALLBACK_SERVICE__].fetch(request);
|
|
139
|
+
return cloneResponse(response);
|
|
140
|
+
} else {
|
|
141
|
+
// There was not fallback service so actually make the request to the origin.
|
|
142
|
+
const response = await fetch(request);
|
|
143
|
+
return cloneResponse(response);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
return next();
|
|
149
|
+
} catch (err) {
|
|
150
|
+
return new Response("Internal Error", { status: 500 });
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// This makes a Response mutable
|
|
156
|
+
const cloneResponse = (response: Response) =>
|
|
157
|
+
// https://fetch.spec.whatwg.org/#null-body-status
|
|
158
|
+
new Response(
|
|
159
|
+
[101, 204, 205, 304].includes(response.status) ? null : response.body,
|
|
160
|
+
response
|
|
161
|
+
);
|
|
@@ -5,39 +5,39 @@ import manifest from "__STATIC_CONTENT_MANIFEST";
|
|
|
5
5
|
const ASSET_MANIFEST = JSON.parse(manifest);
|
|
6
6
|
|
|
7
7
|
export default {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
async fetch(request, env, ctx) {
|
|
9
|
+
let options = {
|
|
10
|
+
ASSET_MANIFEST,
|
|
11
|
+
ASSET_NAMESPACE: env.__STATIC_CONTENT,
|
|
12
|
+
};
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
try {
|
|
15
|
+
const page = await getAssetFromKV(
|
|
16
|
+
{
|
|
17
|
+
request,
|
|
18
|
+
waitUntil(promise) {
|
|
19
|
+
return ctx.waitUntil(promise);
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
options
|
|
23
|
+
);
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
// allow headers to be altered
|
|
26
|
+
const response = new Response(page.body, page);
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
response.headers.set("X-XSS-Protection", "1; mode=block");
|
|
29
|
+
response.headers.set("X-Content-Type-Options", "nosniff");
|
|
30
|
+
response.headers.set("X-Frame-Options", "DENY");
|
|
31
|
+
response.headers.set("Referrer-Policy", "unsafe-url");
|
|
32
|
+
response.headers.set("Feature-Policy", "none");
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
return response;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error(e);
|
|
37
|
+
// if an error is thrown then serve from actual worker
|
|
38
|
+
return worker.fetch(request, env, ctx);
|
|
39
|
+
// TODO: throw here if worker is not available
|
|
40
|
+
// (which implies it may be a service-worker)
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
43
|
};
|