wrangler 0.0.0-e14e589 → 0.0.0-e1c615f

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 (41) hide show
  1. package/README.md +51 -0
  2. package/package.json +39 -26
  3. package/pages/functions/buildWorker.ts +57 -0
  4. package/pages/functions/filepath-routing.test.ts +39 -0
  5. package/pages/functions/filepath-routing.ts +221 -0
  6. package/pages/functions/identifiers.ts +78 -0
  7. package/pages/functions/routes.ts +158 -0
  8. package/pages/functions/template-worker.ts +137 -0
  9. package/src/__tests__/dev.test.tsx +66 -0
  10. package/src/__tests__/index.test.ts +254 -120
  11. package/src/__tests__/jest.setup.ts +22 -0
  12. package/src/__tests__/kv.test.ts +1098 -0
  13. package/src/__tests__/mock-cfetch.ts +171 -0
  14. package/src/__tests__/mock-dialogs.ts +65 -0
  15. package/src/__tests__/run-in-tmp.ts +19 -0
  16. package/src/__tests__/run-wrangler.ts +32 -0
  17. package/src/api/form_data.ts +78 -72
  18. package/src/api/inspect.ts +0 -11
  19. package/src/api/preview.ts +29 -24
  20. package/src/api/worker.ts +46 -31
  21. package/src/cfetch/index.ts +102 -0
  22. package/src/cfetch/internal.ts +69 -0
  23. package/src/cli.ts +6 -7
  24. package/src/config.ts +39 -4
  25. package/src/dev.tsx +332 -128
  26. package/src/dialogs.tsx +4 -12
  27. package/src/index.tsx +397 -222
  28. package/src/kv.tsx +101 -45
  29. package/src/module-collection.ts +64 -0
  30. package/src/pages.tsx +839 -211
  31. package/src/proxy.ts +103 -0
  32. package/src/publish.ts +117 -45
  33. package/src/sites.tsx +6 -7
  34. package/src/tail.tsx +6 -4
  35. package/src/user.tsx +9 -16
  36. package/wrangler-dist/cli.js +123794 -528
  37. package/wrangler-dist/cli.js.map +3 -3
  38. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  39. package/src/__tests__/mock-cfetch.js +0 -46
  40. package/src/cfetch.ts +0 -72
  41. package/src/util/fetch.ts +0 -74
@@ -0,0 +1,102 @@
1
+ import type { RequestInit } from "node-fetch";
2
+ import { URLSearchParams } from "url";
3
+ import { fetchInternal } from "./internal";
4
+
5
+ // Check out https://api.cloudflare.com/ for API docs.
6
+
7
+ export { CF_API_BASE_URL } from "./internal";
8
+
9
+ export interface FetchError {
10
+ code: number;
11
+ message: string;
12
+ }
13
+ export interface FetchResult<ResponseType = unknown> {
14
+ success: boolean;
15
+ result: ResponseType;
16
+ errors: FetchError[];
17
+ messages: string[];
18
+ result_info?: unknown;
19
+ }
20
+
21
+ /**
22
+ * Make a fetch request for a raw JSON value.
23
+ */
24
+ export async function fetchRaw<ResponseType>(
25
+ resource: string,
26
+ init: RequestInit = {},
27
+ queryParams?: URLSearchParams
28
+ ): Promise<ResponseType> {
29
+ return fetchInternal<ResponseType>(resource, init, queryParams);
30
+ }
31
+
32
+ /**
33
+ * Make a fetch request, and extract the `result` from the JSON response.
34
+ */
35
+ export async function fetchResult<ResponseType>(
36
+ resource: string,
37
+ init: RequestInit = {},
38
+ queryParams?: URLSearchParams
39
+ ): Promise<ResponseType> {
40
+ const json = await fetchInternal<FetchResult<ResponseType>>(
41
+ resource,
42
+ init,
43
+ queryParams
44
+ );
45
+ if (json.success) {
46
+ return json.result;
47
+ } else {
48
+ throwFetchError(resource, json);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Make a fetch request for a list of values,
54
+ * extracting the `result` from the JSON response,
55
+ * and repeating the request if the results are paginated.
56
+ */
57
+ export async function fetchListResult<ResponseType>(
58
+ resource: string,
59
+ init: RequestInit = {},
60
+ queryParams?: URLSearchParams
61
+ ): Promise<ResponseType[]> {
62
+ const results: ResponseType[] = [];
63
+ let getMoreResults = true;
64
+ let cursor: string | undefined;
65
+ while (getMoreResults) {
66
+ if (cursor) {
67
+ queryParams = new URLSearchParams(queryParams);
68
+ queryParams.set("cursor", cursor);
69
+ }
70
+ const json = await fetchInternal<FetchResult<ResponseType[]>>(
71
+ resource,
72
+ init,
73
+ queryParams
74
+ );
75
+ if (json.success) {
76
+ results.push(...json.result);
77
+ if (hasCursor(json.result_info)) {
78
+ cursor = json.result_info?.cursor;
79
+ } else {
80
+ getMoreResults = false;
81
+ }
82
+ } else {
83
+ throwFetchError(resource, json);
84
+ }
85
+ }
86
+ return results;
87
+ }
88
+
89
+ function throwFetchError(
90
+ resource: string,
91
+ response: FetchResult<unknown>
92
+ ): never {
93
+ response.messages.forEach((message) => console.warn(message));
94
+ const errors = response.errors
95
+ .map((error) => `${error.code}: ${error.message}`)
96
+ .join("\n");
97
+ throw new Error(`Failed to fetch ${resource} - ${errors}`);
98
+ }
99
+
100
+ function hasCursor(result_info: unknown): result_info is { cursor: string } {
101
+ return (result_info as { cursor } | undefined)?.cursor !== undefined;
102
+ }
@@ -0,0 +1,69 @@
1
+ import fetch from "node-fetch";
2
+ import type { RequestInit, HeadersInit } from "node-fetch";
3
+ import { getAPIToken, loginOrRefreshIfRequired } from "../user";
4
+
5
+ export const CF_API_BASE_URL =
6
+ process.env.CF_API_BASE_URL || "https://api.cloudflare.com/client/v4";
7
+
8
+ /**
9
+ * Make a fetch request to the Cloudflare API.
10
+ *
11
+ * This function handles acquiring the API token and logging the caller in, as necessary.
12
+ *
13
+ * Check out https://api.cloudflare.com/ for API docs.
14
+ *
15
+ * This function should not be used directly, instead use the functions in `cfetch/index.ts`.
16
+ */
17
+ export async function fetchInternal<ResponseType>(
18
+ resource: string,
19
+ init: RequestInit = {},
20
+ queryParams?: URLSearchParams
21
+ ): Promise<ResponseType> {
22
+ await requireLoggedIn();
23
+ const apiToken = requireApiToken();
24
+ const headers = cloneHeaders(init.headers);
25
+ addAuthorizationHeader(headers, apiToken);
26
+
27
+ const queryString = queryParams ? `?${queryParams.toString()}` : "";
28
+ const response = await fetch(`${CF_API_BASE_URL}${resource}${queryString}`, {
29
+ method: "GET",
30
+ ...init,
31
+ headers,
32
+ });
33
+
34
+ if (response.ok) {
35
+ return (await response.json()) as ResponseType;
36
+ } else {
37
+ throw new Error(
38
+ `Failed to fetch ${resource} - ${response.status}: ${response.statusText}`
39
+ );
40
+ }
41
+ }
42
+
43
+ async function requireLoggedIn(): Promise<void> {
44
+ const loggedIn = await loginOrRefreshIfRequired();
45
+ if (!loggedIn) {
46
+ throw new Error("Not logged in.");
47
+ }
48
+ }
49
+
50
+ function requireApiToken(): string {
51
+ const apiToken = getAPIToken();
52
+ if (!apiToken) {
53
+ throw new Error("No API token found.");
54
+ }
55
+ return apiToken;
56
+ }
57
+
58
+ function cloneHeaders(headers: HeadersInit): HeadersInit {
59
+ return { ...headers };
60
+ }
61
+
62
+ function addAuthorizationHeader(headers: HeadersInit, apiToken: string): void {
63
+ if (headers["Authorization"]) {
64
+ throw new Error(
65
+ "The request already specifies an authorisation header - cannot add a new one."
66
+ );
67
+ }
68
+ headers["Authorization"] = `Bearer ${apiToken}`;
69
+ }
package/src/cli.ts CHANGED
@@ -1,10 +1,9 @@
1
+ import { hideBin } from "yargs/helpers";
1
2
  import { main } from ".";
2
3
 
3
- main(process.argv.slice(2)).catch((cause) => {
4
- const { name, message } = cause;
5
- if (name === "CloudflareError") {
6
- console.error("\x1b[31m", message);
7
- return;
8
- }
9
- throw cause;
4
+ main(hideBin(process.argv)).catch(() => {
5
+ // The logging of any error that was thrown from `main()` is handled in the `yargs.fail()` handler.
6
+ // Here we just want to ensure that the process exits with a non-zero code.
7
+ // We don't want to do this inside the `main()` function, since that would kill the process when running our tests.
8
+ process.exit(1);
10
9
  });
package/src/config.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  // we're going to manually write both the type definition AND
2
2
  // the validator for the config, so that we can give better error messages
3
3
 
4
+ type DurableObjectMigration = {
5
+ tag: string;
6
+ new_classes?: string[];
7
+ renamed_classes?: string[];
8
+ deleted_classes?: string[];
9
+ };
10
+
4
11
  type Project = "webpack" | "javascript" | "rust";
5
12
 
6
13
  type Site = {
@@ -18,16 +25,28 @@ type Dev = {
18
25
  upstream_protocol?: string;
19
26
  };
20
27
 
21
- type Vars = { [key: string]: string };
28
+ export type Vars = { [key: string]: string };
22
29
 
23
30
  type Cron = string; // TODO: we should be able to parse a cron pattern with ts
24
31
 
25
32
  type KVNamespace = {
26
- binding?: string;
27
- preview_id: string;
33
+ binding: string;
34
+ preview_id?: string;
28
35
  id: string;
29
36
  };
30
37
 
38
+ type DurableObject = {
39
+ name: string;
40
+ class_name: string;
41
+ script_name?: string;
42
+ };
43
+
44
+ type Service = {
45
+ name: string;
46
+ service: string;
47
+ environment: string;
48
+ };
49
+
31
50
  type Build = {
32
51
  command?: string;
33
52
  cwd?: string;
@@ -59,14 +78,23 @@ type Env = {
59
78
  name?: string; // inherited
60
79
  account_id?: string; // inherited
61
80
  workers_dev?: boolean; // inherited
81
+ compatibility_date?: string; // inherited
82
+ compatibility_flags?: string[]; // inherited
62
83
  zone_id?: string; // inherited
63
84
  routes?: string[]; // inherited
64
85
  route?: string; // inherited
65
86
  webpack_config?: string; // inherited
66
87
  site?: Site;
88
+ jsx_factory?: string; // inherited
89
+ jsx_fragment?: string; // inherited
67
90
  // we should use typescript to parse cron patterns
68
91
  triggers?: { crons: Cron[] }; // inherited
92
+ vars?: Vars;
93
+ durable_objects?: { bindings: DurableObject[] };
69
94
  kv_namespaces?: KVNamespace[];
95
+ experimental_services?: Service[];
96
+ migrations?: DurableObjectMigration[];
97
+ usage_model?: UsageModel; // inherited
70
98
  };
71
99
 
72
100
  export type Config = {
@@ -74,6 +102,8 @@ export type Config = {
74
102
  account_id?: string; // inherited
75
103
  // @deprecated Don't use this
76
104
  type?: Project; // top level
105
+ compatibility_date?: string; // inherited
106
+ compatibility_flags?: string[]; // inherited
77
107
  // -- there's some mutually exclusive logic for this next block,
78
108
  // but I didn't bother for now
79
109
  workers_dev?: boolean; // inherited
@@ -83,8 +113,13 @@ export type Config = {
83
113
  // -- end mutually exclusive stuff
84
114
  // @deprecated Don't use this
85
115
  webpack_config?: string; // inherited
116
+ jsx_factory?: string; // inherited
117
+ jsx_fragment?: string; // inherited
86
118
  vars?: Vars;
119
+ durable_objects?: { bindings: DurableObject[] };
87
120
  kv_namespaces?: KVNamespace[];
121
+ experimental_services?: Service[];
122
+ migrations?: DurableObjectMigration[];
88
123
  site?: Site; // inherited
89
124
  // we should use typescript to parse cron patterns
90
125
  triggers?: { crons: Cron[] }; // inherited
@@ -92,5 +127,5 @@ export type Config = {
92
127
  usage_model?: UsageModel; // inherited
93
128
  // top level
94
129
  build?: Build;
95
- env?: { [envName: string]: Env };
130
+ env?: { [envName: string]: void | Env };
96
131
  };