wrangler 2.0.22 → 2.0.25

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 (75) hide show
  1. package/README.md +20 -2
  2. package/bin/wrangler.js +1 -1
  3. package/miniflare-dist/index.mjs +643 -7
  4. package/package.json +17 -5
  5. package/src/__tests__/configuration.test.ts +89 -17
  6. package/src/__tests__/dev.test.tsx +121 -8
  7. package/src/__tests__/generate.test.ts +93 -0
  8. package/src/__tests__/helpers/mock-cfetch.ts +54 -2
  9. package/src/__tests__/index.test.ts +10 -27
  10. package/src/__tests__/jest.setup.ts +31 -1
  11. package/src/__tests__/kv.test.ts +82 -61
  12. package/src/__tests__/metrics.test.ts +5 -0
  13. package/src/__tests__/publish.test.ts +573 -254
  14. package/src/__tests__/r2.test.ts +173 -71
  15. package/src/__tests__/tail.test.ts +93 -39
  16. package/src/__tests__/user.test.ts +1 -0
  17. package/src/__tests__/validate-dev-props.test.ts +56 -0
  18. package/src/__tests__/version.test.ts +35 -0
  19. package/src/__tests__/whoami.test.tsx +60 -1
  20. package/src/api/dev.ts +49 -9
  21. package/src/bundle.ts +298 -37
  22. package/src/cfetch/internal.ts +34 -2
  23. package/src/config/config.ts +15 -3
  24. package/src/config/environment.ts +40 -8
  25. package/src/config/index.ts +13 -0
  26. package/src/config/validation.ts +111 -9
  27. package/src/create-worker-preview.ts +3 -1
  28. package/src/create-worker-upload-form.ts +25 -0
  29. package/src/dev/dev.tsx +145 -31
  30. package/src/dev/local.tsx +116 -24
  31. package/src/dev/remote.tsx +39 -12
  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 +148 -67
  36. package/src/generate.ts +112 -14
  37. package/src/index.tsx +252 -7
  38. package/src/inspect.ts +90 -5
  39. package/src/metrics/index.ts +1 -0
  40. package/src/metrics/metrics-dispatcher.ts +1 -0
  41. package/src/metrics/metrics-usage-headers.ts +24 -0
  42. package/src/metrics/send-event.ts +2 -2
  43. package/src/miniflare-cli/assets.ts +546 -0
  44. package/src/miniflare-cli/index.ts +157 -6
  45. package/src/module-collection.ts +3 -3
  46. package/src/pages/build.tsx +36 -28
  47. package/src/pages/constants.ts +4 -0
  48. package/src/pages/deployments.tsx +10 -10
  49. package/src/pages/dev.tsx +155 -651
  50. package/src/pages/functions/buildPlugin.ts +4 -0
  51. package/src/pages/functions/buildWorker.ts +4 -0
  52. package/src/pages/functions/routes-consolidation.test.ts +66 -0
  53. package/src/pages/functions/routes-consolidation.ts +29 -0
  54. package/src/pages/functions/routes-transformation.test.ts +271 -0
  55. package/src/pages/functions/routes-transformation.ts +125 -0
  56. package/src/pages/projects.tsx +9 -3
  57. package/src/pages/publish.tsx +57 -15
  58. package/src/pages/types.ts +9 -0
  59. package/src/pages/upload.tsx +38 -21
  60. package/src/publish.ts +139 -112
  61. package/src/r2.ts +81 -0
  62. package/src/tail/index.ts +15 -2
  63. package/src/tail/printing.ts +41 -3
  64. package/src/user/choose-account.tsx +20 -11
  65. package/src/user/user.tsx +20 -2
  66. package/src/whoami.tsx +79 -1
  67. package/src/worker.ts +12 -0
  68. package/templates/first-party-worker-module-facade.ts +18 -0
  69. package/templates/format-dev-errors.ts +32 -0
  70. package/templates/pages-shim.ts +9 -0
  71. package/templates/{static-asset-facade.js → serve-static-assets.ts} +21 -7
  72. package/templates/service-bindings-module-facade.js +51 -0
  73. package/templates/service-bindings-sw-facade.js +39 -0
  74. package/wrangler-dist/cli.d.ts +38 -3
  75. package/wrangler-dist/cli.js +45244 -25199
package/src/user/user.tsx CHANGED
@@ -262,6 +262,7 @@ interface State extends AuthTokens {
262
262
  interface AuthTokens {
263
263
  accessToken?: AccessToken;
264
264
  refreshToken?: RefreshToken;
265
+ scopes?: Scope[];
265
266
  /** @deprecated - this field was only provided by the deprecated `wrangler1 config` command. */
266
267
  apiToken?: string;
267
268
  }
@@ -279,6 +280,7 @@ export interface UserAuthConfig {
279
280
  oauth_token?: string;
280
281
  refresh_token?: string;
281
282
  expiration_time?: string;
283
+ scopes?: string[];
282
284
  /** @deprecated - this field was only provided by the deprecated `wrangler1 config` command. */
283
285
  api_token?: string;
284
286
  }
@@ -355,7 +357,7 @@ function getAuthTokens(config?: UserAuthConfig): AuthTokens | undefined {
355
357
  if (getAuthFromEnv()) return;
356
358
 
357
359
  // otherwise try loading from the user auth config file.
358
- const { oauth_token, refresh_token, expiration_time, api_token } =
360
+ const { oauth_token, refresh_token, expiration_time, scopes, api_token } =
359
361
  config || readAuthConfigFile();
360
362
 
361
363
  if (oauth_token) {
@@ -366,6 +368,7 @@ function getAuthTokens(config?: UserAuthConfig): AuthTokens | undefined {
366
368
  expiry: expiration_time ?? "2000-01-01:00:00:00+00:00",
367
369
  },
368
370
  refreshToken: { value: refresh_token ?? "" },
371
+ scopes: scopes as Scope[],
369
372
  };
370
373
  } else if (api_token) {
371
374
  logger.warn(
@@ -959,6 +962,7 @@ export async function login(props?: LoginProps): Promise<boolean> {
959
962
  oauth_token: exchange.token?.value ?? "",
960
963
  expiration_time: exchange.token?.expiry,
961
964
  refresh_token: exchange.refreshToken?.value,
965
+ scopes: exchange.scopes,
962
966
  });
963
967
  res.writeHead(307, {
964
968
  Location:
@@ -1003,8 +1007,14 @@ async function refreshToken(): Promise<boolean> {
1003
1007
  expiry: "",
1004
1008
  },
1005
1009
  refreshToken: { value: refresh_token } = {},
1010
+ scopes,
1006
1011
  } = await exchangeRefreshTokenForAccessToken();
1007
- writeAuthConfigFile({ oauth_token, expiration_time, refresh_token });
1012
+ writeAuthConfigFile({
1013
+ oauth_token,
1014
+ expiration_time,
1015
+ refresh_token,
1016
+ scopes,
1017
+ });
1008
1018
  return true;
1009
1019
  } catch (err) {
1010
1020
  return false;
@@ -1169,3 +1179,11 @@ export function getAccountFromCache():
1169
1179
  "wrangler-account.json"
1170
1180
  ).account;
1171
1181
  }
1182
+
1183
+ /**
1184
+ * Get the scopes of the following token, will only return scopes
1185
+ * if the token is an OAuth token.
1186
+ */
1187
+ export function getScopes(): Scope[] | undefined {
1188
+ return LocalState.scopes;
1189
+ }
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
+ };
@@ -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 {
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
+ };
@@ -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,51 @@
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
+ if (details.headers) {
20
+ const req = new Request(...reqArgs);
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
+ req.headers.set(key, value);
26
+ }
27
+ return env[name].fetch(req);
28
+ }
29
+ const url = `${details.protocol}://${details.host}${
30
+ details.port ? `:${details.port}` : ""
31
+ }`;
32
+ const request = new Request(url, ...reqArgs);
33
+ return fetch(request);
34
+ },
35
+ };
36
+ } else {
37
+ // This means there's no dev binding available.
38
+ // Let's use whatever's available, or put a shim with a message.
39
+ facadeEnv[name] = facadeEnv[name] || {
40
+ async fetch() {
41
+ return new Response(
42
+ `You should start up wrangler dev --local on the ${name} worker`,
43
+ { status: 404 }
44
+ );
45
+ },
46
+ };
47
+ }
48
+ }
49
+ return worker.fetch(req, facadeEnv, ctx);
50
+ },
51
+ };
@@ -0,0 +1,39 @@
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
+ if (details.headers) {
11
+ const req = new Request(...reqArgs);
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
+ req.headers.set(key, value);
17
+ }
18
+ return env[name].fetch(req);
19
+ }
20
+ const url = `${details.protocol}://${details.host}${
21
+ details.port ? `:${details.port}` : ""
22
+ }`;
23
+ const request = new Request(url, ...reqArgs);
24
+ return fetch(request);
25
+ },
26
+ };
27
+ } else {
28
+ // This means it's a local mode binding
29
+ // but hasn't started up locally yet.
30
+ globalThis[name] = {
31
+ async fetch() {
32
+ return new Response(
33
+ `You should start up wrangler dev --local on the ${name} worker`,
34
+ { status: 404 }
35
+ );
36
+ },
37
+ };
38
+ }
39
+ }
@@ -33,15 +33,49 @@ 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;
39
40
  siteInclude?: string[];
40
41
  siteExclude?: string[];
41
42
  nodeCompat?: boolean;
43
+ compatibilityDate?: string;
42
44
  experimentalEnableLocalPersistence?: boolean;
43
- _: (string | number)[];
44
- $0: string;
45
+ liveReload?: boolean;
46
+ watch?: boolean;
47
+ vars: {
48
+ [key: string]: unknown;
49
+ };
50
+ kv?: {
51
+ binding: string;
52
+ id: string;
53
+ preview_id?: string;
54
+ }[];
55
+ durableObjects?: {
56
+ name: string;
57
+ class_name: string;
58
+ script_name?: string | undefined;
59
+ environment?: string | undefined;
60
+ }[];
61
+ r2?: {
62
+ binding: string;
63
+ bucket_name: string;
64
+ preview_bucket_name?: string;
65
+ }[];
66
+ showInteractiveDevSession?: boolean;
67
+ logLevel?: "none" | "error" | "log" | "warn" | "debug";
68
+ logPrefix?: string;
69
+ inspect?: boolean;
70
+ forceLocal?: boolean;
71
+ enablePagesAssetsServiceBinding?: EnablePagesAssetsServiceBindingOptions;
72
+ _?: (string | number)[];
73
+ $0?: string;
74
+ }
75
+
76
+ declare interface EnablePagesAssetsServiceBindingOptions {
77
+ proxyPort?: number;
78
+ directory?: string;
45
79
  }
46
80
 
47
81
  declare class File extends Blob_2 {
@@ -290,9 +324,10 @@ declare interface SpecIterator<T, TReturn = any, TNext = undefined> {
290
324
  * @param {string} script
291
325
  * @param {DevOptions} options
292
326
  */
293
- export declare function unstable_dev(script: string, options: DevOptions): Promise<{
327
+ export declare function unstable_dev(script: string, options: DevOptions, disableExperimentalWarning?: boolean): Promise<{
294
328
  stop: () => void;
295
329
  fetch: (init?: RequestInit | undefined) => Promise<Response | undefined>;
330
+ waitUntilExit: () => Promise<void>;
296
331
  }>;
297
332
 
298
333
  export { }