wrangler 0.0.0-e61907d → 0.0.0-e9ab55a

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 (42) hide show
  1. package/README.md +51 -0
  2. package/bin/wrangler.js +36 -2
  3. package/import_meta_url.js +3 -0
  4. package/package.json +62 -19
  5. package/pages/functions/buildWorker.ts +62 -0
  6. package/pages/functions/filepath-routing.test.ts +39 -0
  7. package/pages/functions/filepath-routing.ts +228 -0
  8. package/pages/functions/identifiers.ts +78 -0
  9. package/pages/functions/routes.ts +158 -0
  10. package/pages/functions/template-worker.ts +136 -0
  11. package/src/__tests__/clipboardy-mock.js +4 -0
  12. package/src/__tests__/dev.test.tsx +67 -0
  13. package/src/__tests__/index.test.ts +287 -0
  14. package/src/__tests__/jest.setup.ts +22 -0
  15. package/src/__tests__/kv.test.ts +1126 -0
  16. package/src/__tests__/mock-cfetch.ts +171 -0
  17. package/src/__tests__/mock-dialogs.ts +65 -0
  18. package/src/__tests__/publish.test.ts +102 -0
  19. package/src/__tests__/run-in-tmp.ts +19 -0
  20. package/src/__tests__/run-wrangler.ts +32 -0
  21. package/src/api/form_data.ts +131 -0
  22. package/src/api/preview.ts +128 -0
  23. package/src/api/worker.ts +155 -0
  24. package/src/cfetch/index.ts +102 -0
  25. package/src/cfetch/internal.ts +69 -0
  26. package/src/cli.ts +9 -0
  27. package/src/config.ts +646 -0
  28. package/src/dev.tsx +784 -0
  29. package/src/dialogs.tsx +77 -0
  30. package/src/index.tsx +1928 -0
  31. package/src/inspect.ts +543 -0
  32. package/src/kv.tsx +276 -0
  33. package/src/module-collection.ts +64 -0
  34. package/src/pages.tsx +1040 -0
  35. package/src/proxy.ts +302 -0
  36. package/src/publish.ts +382 -0
  37. package/src/sites.tsx +118 -0
  38. package/src/tail.tsx +73 -0
  39. package/src/user.tsx +1027 -0
  40. package/wrangler-dist/{index_node.js → cli.js} +57871 -35979
  41. package/wrangler-dist/cli.js.map +7 -0
  42. package/wrangler-dist/index_node.js.map +0 -7
@@ -0,0 +1,155 @@
1
+ import type { CfPreviewToken } from "./preview";
2
+ import { previewToken } from "./preview";
3
+ import fetch from "node-fetch";
4
+
5
+ /**
6
+ * A Cloudflare account.
7
+ */
8
+ export interface CfAccount {
9
+ /**
10
+ * An API token.
11
+ *
12
+ * @link https://api.cloudflare.com/#user-api-tokens-properties
13
+ */
14
+ apiToken: string;
15
+ /**
16
+ * An account ID.
17
+ */
18
+ accountId: string;
19
+ /**
20
+ * A zone ID, only required if not using `workers.dev`.
21
+ */
22
+ zoneId?: string;
23
+ }
24
+
25
+ /**
26
+ * A module type.
27
+ */
28
+ export type CfModuleType =
29
+ | "esm"
30
+ | "commonjs"
31
+ | "compiled-wasm"
32
+ | "text"
33
+ | "buffer";
34
+
35
+ /**
36
+ * An imported module.
37
+ */
38
+ export interface CfModule {
39
+ /**
40
+ * The module name.
41
+ *
42
+ * @example
43
+ * './src/index.js'
44
+ */
45
+ name: string;
46
+ /**
47
+ * The module content, usually JavaScript or WASM code.
48
+ *
49
+ * @example
50
+ * export default {
51
+ * async fetch(request) {
52
+ * return new Response('Ok')
53
+ * }
54
+ * }
55
+ */
56
+ content: string | BufferSource;
57
+ /**
58
+ * The module type.
59
+ *
60
+ * If absent, will default to the main module's type.
61
+ */
62
+ type?: CfModuleType;
63
+ }
64
+
65
+ /**
66
+ * A map of variable names to values.
67
+ */
68
+ interface CfVars {
69
+ [key: string]: string;
70
+ }
71
+
72
+ /**
73
+ * A KV namespace.
74
+ */
75
+ interface CfKvNamespace {
76
+ binding: string;
77
+ id: string;
78
+ }
79
+
80
+ /**
81
+ * A Durable Object.
82
+ */
83
+ interface CfDurableObject {
84
+ name: string;
85
+ class_name: string;
86
+ script_name?: string;
87
+ }
88
+
89
+ /**
90
+ * A Service.
91
+ */
92
+ interface CfService {
93
+ name: string;
94
+ service: string;
95
+ environment: string;
96
+ }
97
+
98
+ export interface CfDurableObjectMigrations {
99
+ old_tag?: string;
100
+ new_tag: string;
101
+ steps: {
102
+ new_classes?: string[];
103
+ renamed_classes?: string[];
104
+ deleted_classes?: string[];
105
+ }[];
106
+ }
107
+
108
+ /**
109
+ * Options for creating a `CfWorker`.
110
+ */
111
+ export interface CfWorkerInit {
112
+ /**
113
+ * The name of the worker.
114
+ */
115
+ name: string | undefined;
116
+ /**
117
+ * The entrypoint module.
118
+ */
119
+ main: CfModule;
120
+ /**
121
+ * The list of additional modules.
122
+ */
123
+ modules: undefined | CfModule[];
124
+ /**
125
+ * All the bindings
126
+ */
127
+ bindings: {
128
+ kv_namespaces?: CfKvNamespace[];
129
+ durable_objects?: { bindings: CfDurableObject[] };
130
+ vars?: CfVars;
131
+ services?: CfService[];
132
+ };
133
+ migrations: undefined | CfDurableObjectMigrations;
134
+ compatibility_date: string | undefined;
135
+ compatibility_flags: undefined | string[];
136
+ usage_model: undefined | "bundled" | "unbound";
137
+ }
138
+
139
+ /**
140
+ * A stub to create a Cloudflare Worker preview.
141
+ *
142
+ * @example
143
+ * const {value, host} = await createWorker(init, acct);
144
+ */
145
+ export async function createWorker(
146
+ init: CfWorkerInit,
147
+ account: CfAccount
148
+ ): Promise<CfPreviewToken> {
149
+ const token = await previewToken(account, init);
150
+ const response = await fetch(token.prewarmUrl.href, { method: "POST" });
151
+ if (!response.ok) {
152
+ // console.error("worker failed to prewarm: ", response.statusText);
153
+ }
154
+ return token;
155
+ }
@@ -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
+ function cloneHeaders(headers: HeadersInit | undefined): HeadersInit {
44
+ return { ...headers };
45
+ }
46
+
47
+ async function requireLoggedIn(): Promise<void> {
48
+ const loggedIn = await loginOrRefreshIfRequired();
49
+ if (!loggedIn) {
50
+ throw new Error("Not logged in.");
51
+ }
52
+ }
53
+
54
+ function requireApiToken(): string {
55
+ const apiToken = getAPIToken();
56
+ if (!apiToken) {
57
+ throw new Error("No API token found.");
58
+ }
59
+ return apiToken;
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 ADDED
@@ -0,0 +1,9 @@
1
+ import { hideBin } from "yargs/helpers";
2
+ import { main } from ".";
3
+
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);
9
+ });