wrangler 0.0.0-e2a26a0 → 0.0.0-e3cab74

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 (68) hide show
  1. package/package.json +16 -7
  2. package/pages/functions/filepath-routing.test.ts +112 -36
  3. package/pages/functions/filepath-routing.ts +66 -119
  4. package/pages/functions/routes.ts +8 -17
  5. package/pages/functions/template-worker.ts +3 -9
  6. package/src/__tests__/configuration.test.ts +2185 -0
  7. package/src/__tests__/dev.test.tsx +48 -6
  8. package/src/__tests__/guess-worker-format.test.ts +66 -0
  9. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  10. package/src/__tests__/helpers/mock-account-id.ts +6 -6
  11. package/src/__tests__/helpers/mock-bin.ts +4 -1
  12. package/src/__tests__/helpers/mock-cfetch.ts +2 -2
  13. package/src/__tests__/helpers/mock-console.ts +11 -5
  14. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  15. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  16. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  17. package/src/__tests__/index.test.ts +343 -20
  18. package/src/__tests__/jest.setup.ts +21 -1
  19. package/src/__tests__/kv.test.ts +82 -80
  20. package/src/__tests__/package-manager.test.ts +2 -2
  21. package/src/__tests__/parse.test.ts +322 -0
  22. package/src/__tests__/publish.test.ts +1428 -244
  23. package/src/__tests__/r2.test.ts +198 -0
  24. package/src/__tests__/secret.test.ts +176 -60
  25. package/src/__tests__/sentry.test.ts +146 -0
  26. package/src/__tests__/tail.test.ts +564 -0
  27. package/src/api/form_data.ts +106 -23
  28. package/src/api/preview.ts +32 -13
  29. package/src/api/worker.ts +49 -18
  30. package/src/bundle.ts +133 -0
  31. package/src/cfetch/index.ts +8 -6
  32. package/src/cfetch/internal.ts +49 -17
  33. package/src/cli.ts +10 -0
  34. package/src/config/README.md +107 -0
  35. package/src/config/config.ts +265 -0
  36. package/src/config/diagnostics.ts +80 -0
  37. package/src/config/environment.ts +305 -0
  38. package/src/config/index.ts +52 -0
  39. package/src/config/validation-helpers.ts +370 -0
  40. package/src/config/validation.ts +1017 -0
  41. package/src/dev.tsx +338 -211
  42. package/src/entry.ts +57 -0
  43. package/src/environment-variables.ts +33 -0
  44. package/src/guess-worker-format.ts +68 -0
  45. package/src/index.tsx +860 -634
  46. package/src/inspect.ts +16 -3
  47. package/src/kv.tsx +20 -29
  48. package/src/module-collection.ts +141 -34
  49. package/src/open-in-browser.ts +13 -0
  50. package/src/package-manager.ts +21 -15
  51. package/src/pages.tsx +9 -10
  52. package/src/parse.ts +183 -0
  53. package/src/proxy.ts +11 -5
  54. package/src/publish.ts +272 -314
  55. package/src/r2.ts +50 -0
  56. package/src/reporting.ts +115 -0
  57. package/src/sites.tsx +16 -15
  58. package/src/tail/filters.ts +279 -0
  59. package/src/tail/index.ts +299 -0
  60. package/src/tail/printing.ts +62 -0
  61. package/src/user.tsx +74 -44
  62. package/templates/new-worker.js +1 -1
  63. package/templates/new-worker.ts +1 -1
  64. package/templates/tsconfig.json +105 -0
  65. package/wrangler-dist/cli.js +51308 -36440
  66. package/wrangler-dist/cli.js.map +3 -3
  67. package/src/config.ts +0 -641
  68. package/src/tail.tsx +0 -73
@@ -0,0 +1,107 @@
1
+ # Configuration validation
2
+
3
+ The files in this directory define and validate the configuration that is read from a `wrangler.toml` file.
4
+
5
+ The configuration for a Worker is complicated since we can define different "environments", and each environment can have its own configuration.
6
+ There is a default ("top-level") environment and then named environments that provide environment specific configuration.
7
+
8
+ This is further complicated by the fact that there are three kinds of environment configuration:
9
+
10
+ - **non-overridable**: these values are defined once in the top-level configuration, apply to all environments and cannot be overridden by an environment.
11
+ - **inheritable**: these values can be defined at the top-level but can also be overridden by environment specific values.
12
+ Named environments do not need to provide their own values, in which case they inherit the value from the top-level.
13
+ - **non-inheritable**: these values must be explicitly defined in each environment if they are defined at the top-level.
14
+ Named environments do not inherit such configuration and must provide their own values.
15
+
16
+ All configuration values in `wrangler.toml` are optional and will receive a default value if not defined.
17
+
18
+ ## Types
19
+
20
+ ### Environment
21
+
22
+ The fields that can be defined within the `env` containers are defined in the [`Environment`](./environment.ts) type.
23
+ This includes the `EnvironmentInheritable` and `EnvironmentNonInheritable` fields.
24
+
25
+ ### Config
26
+
27
+ The "non-overridable" types are defined in the [`ConfigFields`](./config.ts) type.
28
+ Notably this also contains a property called `env` which is the container for the named environment configuration.
29
+
30
+ The `Config` type is the overall configuration, which consists of the `ConfigFields` and also an `Environment`.
31
+ In this case the `Environment`, here, corresponds to the top level environment.
32
+ The fields in `Config` and `Environment` are not generally optional and so you can expect they have been filled with suitable inherited or default values.
33
+ These types should be used when you are working with fields that should be passed to commands.
34
+
35
+ ### RawConfig
36
+
37
+ The `RawConfig` type is a version of `Config`, where all the fields are optional.
38
+ The `RawConfig` type also includes `DeprecatedConfigFields` and extends and contains the `RawEnvironment` type,
39
+ which is a version of `Environment` where all the fields are optional.
40
+ These optional fields map to the actual fields found in the `wrangler.toml`.
41
+ These types should be used when you are working with raw configuration that is read or will be written to a `wrangler.toml`.
42
+
43
+ ## Validation
44
+
45
+ Validation is triggered by passing a `RawConfig` object to the `normalizeAndValidateConfig()` function.
46
+ This function will return:
47
+
48
+ - a `Config` object, where all the fields have suitable valid values
49
+ - a `Diagnostics` object, which contains any errors or warnings from the validation process
50
+
51
+ The field values may have been parsed directly from the `RawConfig`, inherited into a named environment from the top-level environment, or given a default value.
52
+ Generally, if there are any warnings they should be presented to the user via `console.warn()` messages,
53
+ and if there are any errors then an `Error` should be thrown describing these errors.
54
+
55
+ The `Diagnostics` object is hierarchical: each `Diagnostics` instance can contain a collection of child `Diagnostics` instance.
56
+ When checking for or rendering warnings and errors, the `Diagnostics` class will automatically traverse down to all its children.
57
+
58
+ ## Usage
59
+
60
+ The [high level API](./index.ts) for configuration processing consists of the `findWranglerToml()` and `readConfig()` functions.
61
+
62
+ ### readConfig()
63
+
64
+ The `readConfig()` function will find the nearest `wrangler.toml` file, load and parse it, then validate and normalize the values into a `Config` object.
65
+ If there are validation errors then it will throw a suitable error.
66
+
67
+ ## Changing configuration
68
+
69
+ ### Add a new configuration field
70
+
71
+ When a new field needs to be added to the `wrangler.toml` configuration you will need to add to the types and validation code in this directory.
72
+
73
+ Here are some steps that you should consider when doing this:
74
+
75
+ - add the new field to one of the interface:
76
+ - if the field is not overridable in an environment then add it to the `ConfigFields` interface.
77
+ - if the field can be inherited and overridden in a named environment then add it to the `EnvironmentInheritable` interface.
78
+ - if the field cannot be inherited and must be specified in each named environment then add it to the `EnvironmentNonInheritable` interface.
79
+ - if the field is experimental then add a call to the `experimental()` function:
80
+ - if the field is now in the `ConfigDeprecated` interface then add the call to the `normalizeAndValidateConfig()` function.
81
+ - if the field is now in the `EnvironmentDeprecated` interface then add the call to the `normalizeAndValidateEnvironment()` function.
82
+ - add validation and normalization to the interface
83
+ - if the field is in `ConfigFields` then add validation calls to `normalizeAndValidateConfig()` and assign the normalized value to the appropriate property in the `config` object.
84
+ - if the field is in `EnvironmentInheritable` then call `inheritable()` in `normalizeAndValidateEnvironment()` and assign the normalized value to the appropriate property in the `environment` object.
85
+ - if the field is in `EnvironmentNonInheritable` then call `notInheritable()` in `normalizeAndValidateEnvironment()` and assign the normalized value to the appropriate property in the `environment` object.
86
+ - update the tests in `configuration.test.ts`
87
+ - add to the `"should use defaults for empty configuration"` test to prove the correct default value is assigned
88
+ - if the field is in `ConfigFields` add tests to the `"top-level non-environment configuration"` block to check the validation and normalization of values
89
+ - if the field is in `EnvironmentInheritable` or `EnvironmentNonInheritable`
90
+ - add tests to the `"top-level environment configuration"` block to check the validation and normalization of values
91
+ - add tests to the `"named environment configuration"` block to check the inheritance of values
92
+
93
+ ### Remove a configuration field
94
+
95
+ We should not just remove a field from use, since users would not know that this field is no longer needed, nor how to migrate to any new usage.
96
+ Instead we should first deprecate it, and then then remove it in a future major release.
97
+
98
+ - move the field to a deprecation interface:
99
+ - if the field was originally in `ConfigFields` then move it to the `ConfigDeprecated` interface.
100
+ - if the field was originally in either `EnvironmentInheritable` or `EnvironmentNonInheritable` then move it to the `EnvironmentDeprecatedInterface`.
101
+ - remove the validation for the field
102
+ - the TypeScript compiler should indicate where there is now an unknown field, so that you can remove its validation and normalization from the code base.
103
+ - add a deprecation warning by calling `deprecated()`
104
+ - if the field is now in the `ConfigDeprecated` interface then add the call to the `normalizeAndValidateConfig()` function.
105
+ - if the field is now in the `EnvironmentDeprecated` interface then add the call to the `normalizeAndValidateEnvironment()` function.
106
+ - update the tests in `configuration.test.ts`
107
+ - Find tests where the field is mentioned and either remove them (e.g. validation) or update to show the deprecation message.
@@ -0,0 +1,265 @@
1
+ import type { Environment, RawEnvironment } from "./environment";
2
+
3
+ /**
4
+ * This is the static type definition for the configuration object.
5
+ *
6
+ * It reflects the configuration that you can write in wrangler.toml,
7
+ * and optionally augment with arguments passed directly to wrangler.
8
+ *
9
+ * For more information about the configuration object, see the
10
+ * documentation at https://developers.cloudflare.com/workers/cli-wrangler/configuration
11
+ *
12
+ * Notes:
13
+ *
14
+ * - Fields that are only specified in `ConfigFields` and not `Environment` can only appear
15
+ * in the top level config and should not appear in any environments.
16
+ * - All top level fields in config and environments are optional in the wrangler.toml file.
17
+ *
18
+ * Legend for the annotations:
19
+ *
20
+ * - `@breaking`: the deprecation/optionality is a breaking change from wrangler 1.
21
+ * - `@todo`: there's more work to be done (with details attached).
22
+ */
23
+ export type Config = ConfigFields<Environment> & Environment;
24
+
25
+ export type RawConfig = Partial<ConfigFields<RawEnvironment>> &
26
+ RawEnvironment &
27
+ DeprecatedConfigFields;
28
+
29
+ export interface ConfigFields<Env extends RawEnvironment> {
30
+ configPath: string | undefined;
31
+
32
+ /**
33
+ * The entrypoint/path to the JavaScript file that will be executed.
34
+ */
35
+ main: string | undefined;
36
+
37
+ /**
38
+ * A boolean to enable "legacy" style wrangler environments (from wrangler 1).
39
+ * These have been superseded by Services, but there may be projects that won't
40
+ * (or can't) use them. If you're using a legacy environment, you can set this
41
+ * to `true` to enable it.
42
+ */
43
+ legacy_env: boolean;
44
+
45
+ /**
46
+ * The `env` section defines overrides for the configuration for different environments.
47
+ *
48
+ * All environment fields can be specified at the top level of the config indicating the default environment settings.
49
+ *
50
+ * - Some fields are inherited and overridable in each environment.
51
+ * - But some are not inherited and must be explicitly specified in every environment, if they are specified at the top level.
52
+ *
53
+ * For more information, see the documentation at https://developers.cloudflare.com/workers/cli-wrangler/configuration#environments
54
+ *
55
+ * @default `{}`
56
+ */
57
+ env: {
58
+ [envName: string]: Env;
59
+ };
60
+
61
+ /**
62
+ * Options to configure the development server that your worker will use.
63
+ *
64
+ * @default `{}`
65
+ */
66
+ dev: {
67
+ /**
68
+ * IP address for the local dev server to listen on,
69
+ *
70
+ * @default `127.0.0.1`
71
+ * @todo this needs to be implemented
72
+ */
73
+ ip?: string;
74
+
75
+ /**
76
+ * Port for the local dev server to listen on
77
+ *
78
+ * @default `8787`
79
+ */
80
+ port?: number;
81
+
82
+ /**
83
+ * Protocol that local wrangler dev server listens to requests on.
84
+ *
85
+ * @default `http`
86
+ * @todo this needs to be implemented
87
+ */
88
+ local_protocol?: string;
89
+
90
+ /**
91
+ * Protocol that wrangler dev forwards requests on
92
+ *
93
+ * @default `https`
94
+ * @todo this needs to be implemented
95
+ */
96
+ upstream_protocol?: string;
97
+ };
98
+
99
+ /**
100
+ * A list of migrations that should be uploaded with your Worker.
101
+ *
102
+ * These define changes in your Durable Object declarations.
103
+ *
104
+ * More details at https://developers.cloudflare.com/workers/learning/using-durable-objects#configuring-durable-object-classes-with-migrations
105
+ *
106
+ * @default `[]`
107
+ */
108
+ migrations: {
109
+ /** A unique identifier for this migration. */
110
+ tag: string;
111
+ /** The new Durable Objects being defined. */
112
+ new_classes?: string[];
113
+ /** The Durable Objects being renamed. */
114
+ renamed_classes?: {
115
+ from: string;
116
+ to: string;
117
+ }[];
118
+ /** The Durable Objects being removed. */
119
+ deleted_classes?: string[];
120
+ }[];
121
+
122
+ /**
123
+ * The definition of a Worker Site, a feature that lets you upload
124
+ * static assets with your Worker.
125
+ *
126
+ * More details at https://developers.cloudflare.com/workers/platform/sites
127
+ */
128
+ site:
129
+ | {
130
+ /**
131
+ * The directory containing your static assets.
132
+ *
133
+ * It must be a path relative to your wrangler.toml file.
134
+ * Example: bucket = "./public"
135
+ *
136
+ * If there is a `site` field then it must contain this `bucket` field.
137
+ */
138
+ bucket: string;
139
+
140
+ /**
141
+ * The location of your Worker script.
142
+ *
143
+ * @deprecated DO NOT use this (it's a holdover from wrangler 1.x). Either use the top level `main` field, or pass the path to your entry file as a command line argument.
144
+ * @breaking
145
+ */
146
+ "entry-point"?: string;
147
+
148
+ /**
149
+ * An exclusive list of .gitignore-style patterns that match file
150
+ * or directory names from your bucket location. Only matched
151
+ * items will be uploaded. Example: include = ["upload_dir"]
152
+ *
153
+ * @optional
154
+ * @default `[]`
155
+ */
156
+ include?: string[];
157
+
158
+ /**
159
+ * A list of .gitignore-style patterns that match files or
160
+ * directories in your bucket that should be excluded from
161
+ * uploads. Example: exclude = ["ignore_dir"]
162
+ *
163
+ * @optional
164
+ * @default `[]`
165
+ */
166
+ exclude?: string[];
167
+ }
168
+ | undefined;
169
+
170
+ /**
171
+ * A list of wasm modules that your worker should be bound to. This is
172
+ * the "legacy" way of binding to a wasm module. ES module workers should
173
+ * do proper module imports.
174
+ */
175
+ wasm_modules:
176
+ | {
177
+ [key: string]: string;
178
+ }
179
+ | undefined;
180
+
181
+ /**
182
+ * A list of text files that your worker should be bound to. This is
183
+ * the "legacy" way of binding to a text file. ES module workers should
184
+ * do proper module imports.
185
+ */
186
+ text_blobs:
187
+ | {
188
+ [key: string]: string;
189
+ }
190
+ | undefined;
191
+
192
+ /**
193
+ * Configures a custom build step to be run by Wrangler when building your Worker.
194
+ *
195
+ * Refer to the [custom builds documentation](https://developers.cloudflare.com/workers/cli-wrangler/configuration#build)
196
+ * for more details.
197
+ *
198
+ * @default {}
199
+ */
200
+ build: {
201
+ /** The command used to build your Worker. On Linux and macOS, the command is executed in the `sh` shell and the `cmd` shell for Windows. The `&&` and `||` shell operators may be used. */
202
+ command?: string;
203
+ /** The directory in which the command is executed. */
204
+ cwd?: string;
205
+ /** The directory to watch for changes while using wrangler dev, defaults to the current working directory */
206
+ watch_dir?: string;
207
+ /** Deprecated field previously used to configure the build and upload of the script. */
208
+ upload?: DeprecatedUpload;
209
+ };
210
+ }
211
+
212
+ export interface DeprecatedConfigFields {
213
+ /**
214
+ * The project "type". A holdover from wrangler 1.x.
215
+ * Valid values were "webpack", "javascript", and "rust".
216
+ *
217
+ * @deprecated DO NOT USE THIS. Most common features now work out of the box with wrangler, including modules, jsx, typescript, etc. If you need anything more, use a custom build.
218
+ * @breaking
219
+ */
220
+ type?: "webpack" | "javascript" | "rust";
221
+
222
+ /**
223
+ * Path to the webpack config to use when building your worker.
224
+ * A holdover from wrangler 1.x, used with `type: "webpack"`.
225
+ *
226
+ * @deprecated DO NOT USE THIS. Most common features now work out of the box with wrangler, including modules, jsx, typescript, etc. If you need anything more, use a custom build.
227
+ * @breaking
228
+ */
229
+ webpack_config?: string;
230
+ }
231
+
232
+ /**
233
+ * Deprecated upload configuration.
234
+ */
235
+ export interface DeprecatedUpload {
236
+ /**
237
+ * The format of the Worker script.
238
+ *
239
+ * @deprecated We infer the format automatically now.
240
+ */
241
+ format?: "modules" | "service-worker";
242
+
243
+ /**
244
+ * The directory you wish to upload your worker from,
245
+ * relative to the wrangler.toml file.
246
+ *
247
+ * Defaults to the directory containing the wrangler.toml file.
248
+ *
249
+ * @deprecated
250
+ * @breaking In wrangler 1, this defaults to ./dist, whereas in wrangler 2 it defaults to ./
251
+ */
252
+ dir?: string;
253
+
254
+ /**
255
+ * The path to the Worker script, relative to `upload.dir`.
256
+ *
257
+ * @deprecated This will be replaced by a command line argument.
258
+ */
259
+ main?: string;
260
+
261
+ /**
262
+ * @deprecated This is now defined at the top level `rules` field.
263
+ */
264
+ rules?: Environment["rules"];
265
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Diagnostic errors and warnings.
3
+ *
4
+ * The structure is a tree, where each node can contain zero or more errors, warnings and child diagnostics objects.
5
+ * You can check whether the overall tree has errors or warnings, and you can render a string representation of the errors or warnings.
6
+ */
7
+ export class Diagnostics {
8
+ errors: string[] = [];
9
+ warnings: string[] = [];
10
+ children: Diagnostics[] = [];
11
+
12
+ /**
13
+ * Create a new Diagnostics object.
14
+ * @param description A general description of this collection of messages.
15
+ */
16
+ constructor(public description: string) {}
17
+
18
+ /**
19
+ * Merge the given `diagnostics` into this as a child.
20
+ */
21
+ addChild(diagnostics: Diagnostics): void {
22
+ if (diagnostics.hasErrors() || diagnostics.hasWarnings()) {
23
+ this.children.push(diagnostics);
24
+ }
25
+ }
26
+
27
+ /** Does this or any of its children have errors. */
28
+ hasErrors(): boolean {
29
+ if (this.errors.length > 0) {
30
+ return true;
31
+ } else {
32
+ return this.children.some((child) => child.hasErrors());
33
+ }
34
+ }
35
+
36
+ /** Render the errors of this and all its children. */
37
+ renderErrors(): string {
38
+ return this.render("errors");
39
+ }
40
+
41
+ /** Does this or any of its children have warnings. */
42
+ hasWarnings(): boolean {
43
+ if (this.warnings.length > 0) {
44
+ return true;
45
+ } else {
46
+ return this.children.some((child) => child.hasWarnings());
47
+ }
48
+ }
49
+
50
+ /** Render the warnings of this and all its children. */
51
+ renderWarnings(): string {
52
+ return this.render("warnings");
53
+ }
54
+
55
+ private render(field: "errors" | "warnings"): string {
56
+ const hasMethod = field === "errors" ? "hasErrors" : "hasWarnings";
57
+ return indentText(
58
+ `${this.description}\n` +
59
+ // Output all the fields (errors or warnings) at this level
60
+ this[field].map((message) => `- ${indentText(message)}`).join("\n") +
61
+ // Output all the child diagnostics at the next level
62
+ this.children
63
+ .map((child) =>
64
+ child[hasMethod]() ? "\n- " + child.render(field) : ""
65
+ )
66
+ .filter((output) => output !== "")
67
+ .join("\n")
68
+ );
69
+ }
70
+ }
71
+
72
+ /** Indent all the but the first line by two spaces. */
73
+ function indentText(str: string): string {
74
+ return str
75
+ .split("\n")
76
+ .map((line, index) =>
77
+ (index === 0 ? line : ` ${line}`).replace(/^\s*$/, "")
78
+ )
79
+ .join("\n");
80
+ }