wrangler 2.0.23 → 2.0.26

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 (80) hide show
  1. package/README.md +20 -2
  2. package/bin/wrangler.js +1 -1
  3. package/miniflare-dist/index.mjs +235 -47
  4. package/package.json +11 -6
  5. package/src/__tests__/configuration.test.ts +89 -17
  6. package/src/__tests__/dev.test.tsx +29 -4
  7. package/src/__tests__/generate.test.ts +93 -0
  8. package/src/__tests__/helpers/mock-cfetch.ts +87 -2
  9. package/src/__tests__/index.test.ts +10 -27
  10. package/src/__tests__/init.test.ts +537 -359
  11. package/src/__tests__/jest.setup.ts +34 -1
  12. package/src/__tests__/kv.test.ts +2 -2
  13. package/src/__tests__/metrics.test.ts +5 -0
  14. package/src/__tests__/pages.test.ts +14 -0
  15. package/src/__tests__/publish.test.ts +497 -254
  16. package/src/__tests__/r2.test.ts +173 -71
  17. package/src/__tests__/tail.test.ts +112 -42
  18. package/src/__tests__/user.test.ts +1 -0
  19. package/src/__tests__/validate-dev-props.test.ts +56 -0
  20. package/src/__tests__/whoami.test.tsx +60 -1
  21. package/src/api/dev.ts +7 -0
  22. package/src/bundle.ts +279 -44
  23. package/src/cfetch/internal.ts +73 -2
  24. package/src/config/config.ts +8 -3
  25. package/src/config/environment.ts +40 -8
  26. package/src/config/index.ts +13 -0
  27. package/src/config/validation.ts +102 -8
  28. package/src/create-worker-upload-form.ts +25 -0
  29. package/src/dev/dev.tsx +121 -28
  30. package/src/dev/local.tsx +88 -14
  31. package/src/dev/remote.tsx +39 -8
  32. package/src/dev/use-esbuild.ts +28 -0
  33. package/src/dev/validate-dev-props.ts +31 -0
  34. package/src/dev-registry.tsx +160 -0
  35. package/src/dev.tsx +107 -80
  36. package/src/generate.ts +112 -14
  37. package/src/index.tsx +212 -4
  38. package/src/init.ts +111 -38
  39. package/src/inspect.ts +90 -5
  40. package/src/metrics/index.ts +1 -0
  41. package/src/metrics/metrics-dispatcher.ts +1 -0
  42. package/src/metrics/metrics-usage-headers.ts +24 -0
  43. package/src/metrics/send-event.ts +2 -2
  44. package/src/miniflare-cli/assets.ts +27 -16
  45. package/src/miniflare-cli/index.ts +124 -2
  46. package/src/module-collection.ts +3 -3
  47. package/src/pages/build.tsx +75 -41
  48. package/src/pages/constants.ts +5 -0
  49. package/src/pages/deployments.tsx +10 -10
  50. package/src/pages/dev.tsx +177 -52
  51. package/src/pages/errors.ts +22 -0
  52. package/src/pages/functions/buildPlugin.ts +4 -0
  53. package/src/pages/functions/buildWorker.ts +4 -0
  54. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  55. package/src/pages/functions/routes-consolidation.ts +73 -0
  56. package/src/pages/functions/routes-transformation.test.ts +271 -0
  57. package/src/pages/functions/routes-transformation.ts +122 -0
  58. package/src/pages/functions.tsx +96 -0
  59. package/src/pages/index.tsx +65 -55
  60. package/src/pages/projects.tsx +9 -3
  61. package/src/pages/publish.tsx +76 -23
  62. package/src/pages/types.ts +9 -0
  63. package/src/pages/upload.tsx +38 -21
  64. package/src/publish.ts +126 -112
  65. package/src/r2.ts +81 -0
  66. package/src/tail/filters.ts +3 -1
  67. package/src/tail/index.ts +15 -2
  68. package/src/tail/printing.ts +43 -3
  69. package/src/user/user.tsx +20 -2
  70. package/src/whoami.tsx +79 -1
  71. package/src/worker.ts +12 -0
  72. package/templates/first-party-worker-module-facade.ts +18 -0
  73. package/templates/format-dev-errors.ts +32 -0
  74. package/templates/pages-template-plugin.ts +16 -4
  75. package/templates/pages-template-worker.ts +16 -5
  76. package/templates/{static-asset-facade.js → serve-static-assets.ts} +21 -7
  77. package/templates/service-bindings-module-facade.js +54 -0
  78. package/templates/service-bindings-sw-facade.js +42 -0
  79. package/wrangler-dist/cli.d.ts +7 -0
  80. package/wrangler-dist/cli.js +40851 -15332
package/src/whoami.tsx CHANGED
@@ -3,7 +3,7 @@ import Table from "ink-table";
3
3
  import React from "react";
4
4
  import { fetchListResult, fetchResult } from "./cfetch";
5
5
  import { logger } from "./logger";
6
- import { getAPIToken, getAuthFromEnv } from "./user";
6
+ import { getAPIToken, getAuthFromEnv, getScopes } from "./user";
7
7
 
8
8
  export async function whoami() {
9
9
  logger.log("Getting User settings...");
@@ -17,6 +17,10 @@ export function WhoAmI({ user }: { user: UserInfo | undefined }) {
17
17
  <>
18
18
  <Email tokenType={user.authType} email={user.email}></Email>
19
19
  <Accounts accounts={user.accounts}></Accounts>
20
+ <Permissions
21
+ tokenType={user.authType}
22
+ tokenPermissions={user.tokenPermissions}
23
+ />
20
24
  </>
21
25
  ) : (
22
26
  <Text>You are not authenticated. Please run `wrangler login`.</Text>
@@ -46,17 +50,51 @@ function Accounts(props: { accounts: AccountInfo[] }) {
46
50
  return <Table data={accounts}></Table>;
47
51
  }
48
52
 
53
+ function Permissions(props: {
54
+ tokenPermissions: string[] | undefined;
55
+ tokenType: string;
56
+ }) {
57
+ const permissions =
58
+ props.tokenPermissions?.map((scope) => scope.split(":")) || [];
59
+ return props.tokenType === "OAuth Token" ? (
60
+ props.tokenPermissions ? (
61
+ <>
62
+ <Text>
63
+ 🔓 Token Permissions: If scopes are missing, you may need to logout
64
+ and re-login.
65
+ </Text>
66
+ <Text>Scope (Access)</Text>
67
+ {permissions.map(([type, name]) => (
68
+ <>
69
+ <Text>
70
+ - {type} {name && `(${name})`}
71
+ </Text>
72
+ </>
73
+ ))}
74
+ </>
75
+ ) : null
76
+ ) : (
77
+ <Text>
78
+ 🔓 To see token permissions visit
79
+ https://dash.cloudflare.com/profile/api-tokens
80
+ </Text>
81
+ );
82
+ }
83
+
49
84
  export interface UserInfo {
50
85
  apiToken: string;
51
86
  authType: string;
52
87
  email: string | undefined;
53
88
  accounts: AccountInfo[];
89
+ tokenPermissions: string[] | undefined;
54
90
  }
55
91
 
56
92
  export async function getUserInfo(): Promise<UserInfo | undefined> {
57
93
  const apiToken = getAPIToken();
58
94
  if (!apiToken) return;
59
95
 
96
+ const tokenPermissions = await getTokenPermissions();
97
+
60
98
  const usingEnvAuth = !!getAuthFromEnv();
61
99
  const usingGlobalAuthKey = "authKey" in apiToken;
62
100
  return {
@@ -68,6 +106,7 @@ export async function getUserInfo(): Promise<UserInfo | undefined> {
68
106
  : "OAuth Token",
69
107
  email: "authEmail" in apiToken ? apiToken.authEmail : await getEmail(),
70
108
  accounts: await getAccounts(),
109
+ tokenPermissions,
71
110
  };
72
111
  }
73
112
 
@@ -89,3 +128,42 @@ type AccountInfo = { name: string; id: string };
89
128
  async function getAccounts(): Promise<AccountInfo[]> {
90
129
  return await fetchListResult<AccountInfo>("/accounts");
91
130
  }
131
+
132
+ async function getTokenPermissions(): Promise<string[] | undefined> {
133
+ // Tokens can either be API tokens or Oauth tokens.
134
+ // Here we only extract permissions from OAuth tokens.
135
+
136
+ return getScopes() as string[];
137
+
138
+ // In future we may be able to get the token permissions on an API token,
139
+ // but currently we cannot as that permission is not able to be added to
140
+ // an API token.
141
+
142
+ // try {
143
+ // // First we get the token identifier (only returned on API tokens)
144
+ // const { id } = await fetchResult<{ id: string }>("/user/tokens/verify");
145
+
146
+ // // Get the token permissions for the current token
147
+ // const { policies } = await fetchResult<{
148
+ // policies: { id: string; name: string }[];
149
+ // }>(`/user/tokens/${id}`);
150
+
151
+ // return policies.map((p) => p.name);
152
+ // } catch (e) {
153
+ // if ((e as { code?: number }).code === 1000) {
154
+ // // Invalid token - Oauth token
155
+
156
+ // // Get scopes
157
+ // const scopes = getScopes() as string[];
158
+ // // We may not get scopes back if they are not currently cached,
159
+ // // however next time an access token is requested,
160
+ // // the scopes will be added to the cache.
161
+ // return scopes;
162
+ // } else if ((e as { code?: number }).code === 9109) {
163
+ // // Token cannot view its permissions
164
+ // return undefined;
165
+ // } else {
166
+ // throw e;
167
+ // }
168
+ // }
169
+ }
package/src/worker.ts CHANGED
@@ -127,6 +127,16 @@ interface CfWorkerNamespace {
127
127
  namespace: string;
128
128
  }
129
129
 
130
+ interface CfLogfwdr {
131
+ schema: string | undefined;
132
+ bindings: CfLogfwdrBinding[];
133
+ }
134
+
135
+ interface CfLogfwdrBinding {
136
+ name: string;
137
+ destination: string;
138
+ }
139
+
130
140
  interface CfUnsafeBinding {
131
141
  name: string;
132
142
  type: string;
@@ -174,6 +184,7 @@ export interface CfWorkerInit {
174
184
  r2_buckets: CfR2Bucket[] | undefined;
175
185
  services: CfService[] | undefined;
176
186
  worker_namespaces: CfWorkerNamespace[] | undefined;
187
+ logfwdr: CfLogfwdr | undefined;
177
188
  unsafe: CfUnsafeBinding[] | undefined;
178
189
  };
179
190
  migrations: CfDurableObjectMigrations | undefined;
@@ -188,4 +199,5 @@ export interface CfWorkerContext {
188
199
  zone: string | undefined;
189
200
  host: string | undefined;
190
201
  routes: Route[] | undefined;
202
+ sendMetrics: boolean | undefined;
191
203
  }
@@ -0,0 +1,18 @@
1
+ // @ts-ignore entry point will get replaced
2
+ import worker from "__ENTRY_POINT__";
3
+ // @ts-ignore entry point will get replaced
4
+ export * from "__ENTRY_POINT__";
5
+
6
+ type Env = {
7
+ // TODO: type this
8
+ };
9
+
10
+ /**
11
+ * Setup globals/vars as required
12
+ */
13
+
14
+ export default {
15
+ async fetch(request: Request, env: Env, ctx: ExecutionContext) {
16
+ return worker.fetch(request, env, ctx);
17
+ },
18
+ };
@@ -0,0 +1,32 @@
1
+ // @ts-expect-error We'll swap in the entry point during build
2
+ import Worker from "__ENTRY_POINT__";
3
+
4
+ // @ts-expect-error
5
+ export * from "__ENTRY_POINT__";
6
+
7
+ export default {
8
+ async fetch(req: Request, env: unknown, ctx: ExecutionContext) {
9
+ try {
10
+ return await Worker.fetch(req, env, ctx);
11
+ } catch (err) {
12
+ return new Response(
13
+ `<!DOCTYPE html>
14
+ <html>
15
+ <head>
16
+ <meta charset="utf-8" />
17
+ <title>Error</title>
18
+ </head>
19
+ <body>
20
+ <pre>${(err as Error).stack}</pre>
21
+ </body>
22
+ </html>`,
23
+ {
24
+ status: 500,
25
+ headers: {
26
+ "Content-Type": "text/html",
27
+ },
28
+ }
29
+ );
30
+ }
31
+ },
32
+ };
@@ -1,5 +1,8 @@
1
1
  import { match } from "path-to-regexp";
2
2
 
3
+ //note: this explicitly does not include the * character, as pages requires this
4
+ const escapeRegex = /[.+?^${}()|[\]\\]/g;
5
+
3
6
  type HTTPMethod =
4
7
  | "HEAD"
5
8
  | "OPTIONS"
@@ -67,8 +70,13 @@ function* executeRequest(request: Request, relativePathname: string) {
67
70
  continue;
68
71
  }
69
72
 
70
- const routeMatcher = match(route.routePath, { end: false });
71
- const mountMatcher = match(route.mountPath, { end: false });
73
+ // replaces with "\\$&", this prepends a backslash to the matched string, e.g. "[" becomes "\["
74
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
75
+ end: false,
76
+ });
77
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
78
+ end: false,
79
+ });
72
80
  const matchResult = routeMatcher(relativePathname);
73
81
  const mountMatchResult = mountMatcher(relativePathname);
74
82
  if (matchResult && mountMatchResult) {
@@ -88,8 +96,12 @@ function* executeRequest(request: Request, relativePathname: string) {
88
96
  continue;
89
97
  }
90
98
 
91
- const routeMatcher = match(route.routePath, { end: true });
92
- const mountMatcher = match(route.mountPath, { end: false });
99
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
100
+ end: true,
101
+ });
102
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
103
+ end: false,
104
+ });
93
105
  const matchResult = routeMatcher(relativePathname);
94
106
  const mountMatchResult = mountMatcher(relativePathname);
95
107
  if (matchResult && mountMatchResult && route.modules.length) {
@@ -1,5 +1,8 @@
1
1
  import { match } from "path-to-regexp";
2
2
 
3
+ //note: this explicitly does not include the * character, as pages requires this
4
+ const escapeRegex = /[.+?^${}()|[\]\\]/g;
5
+
3
6
  type HTTPMethod =
4
7
  | "HEAD"
5
8
  | "OPTIONS"
@@ -61,8 +64,13 @@ function* executeRequest(request: Request) {
61
64
  continue;
62
65
  }
63
66
 
64
- const routeMatcher = match(route.routePath, { end: false });
65
- const mountMatcher = match(route.mountPath, { end: false });
67
+ // replaces with "\\$&", this prepends a backslash to the matched string, e.g. "[" becomes "\["
68
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
69
+ end: false,
70
+ });
71
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
72
+ end: false,
73
+ });
66
74
  const matchResult = routeMatcher(requestPath);
67
75
  const mountMatchResult = mountMatcher(requestPath);
68
76
  if (matchResult && mountMatchResult) {
@@ -81,9 +89,12 @@ function* executeRequest(request: Request) {
81
89
  if (route.method && route.method !== request.method) {
82
90
  continue;
83
91
  }
84
-
85
- const routeMatcher = match(route.routePath, { end: true });
86
- const mountMatcher = match(route.mountPath, { end: false });
92
+ const routeMatcher = match(route.routePath.replace(escapeRegex, "\\$&"), {
93
+ end: true,
94
+ });
95
+ const mountMatcher = match(route.mountPath.replace(escapeRegex, "\\$&"), {
96
+ end: false,
97
+ });
87
98
  const matchResult = routeMatcher(requestPath);
88
99
  const mountMatchResult = mountMatcher(requestPath);
89
100
  if (matchResult && mountMatchResult && route.modules.length) {
@@ -1,25 +1,40 @@
1
- // DO NOT IMPORT THIS DIRECTLY
1
+ // @ts-expect-error
2
2
  import worker from "__ENTRY_POINT__";
3
3
  import {
4
4
  getAssetFromKV,
5
5
  NotFoundError,
6
6
  MethodNotAllowedError,
7
+ // @ts-expect-error
7
8
  } from "__KV_ASSET_HANDLER__";
9
+ // @ts-expect-error
8
10
  import manifest from "__STATIC_CONTENT_MANIFEST";
11
+ import type * as kvAssetHandler from "@cloudflare/kv-asset-handler";
12
+
9
13
  const ASSET_MANIFEST = JSON.parse(manifest);
10
14
 
15
+ // @ts-expect-error
16
+ export * from "__ENTRY_POINT__";
17
+
11
18
  export default {
12
- async fetch(request, env, ctx) {
19
+ async fetch(
20
+ request: Request,
21
+ env: { __STATIC_CONTENT: string },
22
+ ctx: ExecutionContext
23
+ ) {
13
24
  let options = {
14
25
  ASSET_MANIFEST,
15
26
  ASSET_NAMESPACE: env.__STATIC_CONTENT,
27
+ cacheControl: __CACHE_CONTROL_OPTIONS__,
28
+ serveSinglePageApp: __SERVE_SINGLE_PAGE_APP__,
16
29
  };
17
30
 
18
31
  try {
19
- const page = await getAssetFromKV(
32
+ const page = await (
33
+ getAssetFromKV as typeof kvAssetHandler.getAssetFromKV
34
+ )(
20
35
  {
21
36
  request,
22
- waitUntil(promise) {
37
+ waitUntil(promise: Promise<unknown>) {
23
38
  return ctx.waitUntil(promise);
24
39
  },
25
40
  },
@@ -39,11 +54,10 @@ export default {
39
54
  } catch (e) {
40
55
  if (e instanceof NotFoundError || e instanceof MethodNotAllowedError) {
41
56
  // if a known error is thrown then serve from actual worker
42
- return worker.fetch(request, env, ctx);
57
+ return await worker.fetch(request, env, ctx);
43
58
  }
44
59
  // otherwise it's a real error, so throw it
45
- console.error(e);
46
- return new Response(e.message, { status: 500 });
60
+ throw e;
47
61
  }
48
62
  },
49
63
  };
@@ -0,0 +1,54 @@
1
+ import worker from "__ENTRY_POINT__";
2
+ const Workers = __WORKERS__;
3
+
4
+ export * from "__ENTRY_POINT__";
5
+
6
+ export default {
7
+ async fetch(req, env, ctx) {
8
+ const facadeEnv = { ...env };
9
+ // For every Worker definition that's available,
10
+ // create a fetcher for it on the facade env.
11
+ // for const [name, binding] of env
12
+ // if Workers[name]
13
+ // const details = Workers[name];
14
+
15
+ for (const [name, details] of Object.entries(Workers)) {
16
+ if (details) {
17
+ facadeEnv[name] = {
18
+ async fetch(...reqArgs) {
19
+ const reqFromArgs = new Request(...reqArgs);
20
+ if (details.headers) {
21
+ for (const [key, value] of Object.entries(details.headers)) {
22
+ // In remote mode, you need to add a couple of headers
23
+ // to make sure it's talking to the 'dev' preview session
24
+ // (much like wrangler dev already does via proxy.ts)
25
+ reqFromArgs.headers.set(key, value);
26
+ }
27
+ return env[name].fetch(reqFromArgs);
28
+ }
29
+
30
+ const url = new URL(reqFromArgs.url);
31
+ url.protocol = details.protocol;
32
+ url.host = details.host;
33
+ if (details.port !== undefined) url.port = details.port;
34
+
35
+ const request = new Request(url.toString(), reqFromArgs);
36
+ return fetch(request);
37
+ },
38
+ };
39
+ } else {
40
+ // This means there's no dev binding available.
41
+ // Let's use whatever's available, or put a shim with a message.
42
+ facadeEnv[name] = facadeEnv[name] || {
43
+ async fetch() {
44
+ return new Response(
45
+ `You should start up wrangler dev --local on the ${name} worker`,
46
+ { status: 404 }
47
+ );
48
+ },
49
+ };
50
+ }
51
+ }
52
+ return worker.fetch(req, facadeEnv, ctx);
53
+ },
54
+ };
@@ -0,0 +1,42 @@
1
+ import "__ENTRY_POINT__";
2
+ const Workers = __WORKERS__;
3
+
4
+ // For every Worker definition that's available,
5
+ // create a fetcher for it on the facade env.
6
+ for (const [name, details] of Object.entries(Workers)) {
7
+ if (details) {
8
+ globalThis[name] = {
9
+ async fetch(...reqArgs) {
10
+ const reqFromArgs = new Request(...reqArgs);
11
+ if (details.headers) {
12
+ for (const [key, value] of Object.entries(details.headers)) {
13
+ // In remote mode, you need to add a couple of headers
14
+ // to make sure it's talking to the 'dev' preview session
15
+ // (much like wrangler dev already does via proxy.ts)
16
+ reqFromArgs.headers.set(key, value);
17
+ }
18
+ return env[name].fetch(reqFromArgs);
19
+ }
20
+
21
+ const url = new URL(reqFromArgs.url);
22
+ url.protocol = details.protocol;
23
+ url.host = details.host;
24
+ if (details.port !== undefined) url.port = details.port;
25
+
26
+ const request = new Request(url.toString(), reqFromArgs);
27
+ return fetch(request);
28
+ },
29
+ };
30
+ } else {
31
+ // This means it's a local mode binding
32
+ // but hasn't started up locally yet.
33
+ globalThis[name] = {
34
+ async fetch() {
35
+ return new Response(
36
+ `You should start up wrangler dev --local on the ${name} worker`,
37
+ { status: 404 }
38
+ );
39
+ },
40
+ };
41
+ }
42
+ }
@@ -33,6 +33,7 @@ declare interface DevOptions {
33
33
  env?: string;
34
34
  ip?: string;
35
35
  port?: number;
36
+ inspectorPort?: number;
36
37
  localProtocol?: "http" | "https";
37
38
  assets?: string;
38
39
  site?: string;
@@ -40,6 +41,7 @@ declare interface DevOptions {
40
41
  siteExclude?: string[];
41
42
  nodeCompat?: boolean;
42
43
  compatibilityDate?: string;
44
+ compatibilityFlags?: string[];
43
45
  experimentalEnableLocalPersistence?: boolean;
44
46
  liveReload?: boolean;
45
47
  watch?: boolean;
@@ -57,6 +59,11 @@ declare interface DevOptions {
57
59
  script_name?: string | undefined;
58
60
  environment?: string | undefined;
59
61
  }[];
62
+ r2?: {
63
+ binding: string;
64
+ bucket_name: string;
65
+ preview_bucket_name?: string;
66
+ }[];
60
67
  showInteractiveDevSession?: boolean;
61
68
  logLevel?: "none" | "error" | "log" | "warn" | "debug";
62
69
  logPrefix?: string;