wrangler 0.0.0-ece06ea → 0.0.0-edc4b53

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/package.json +24 -15
  2. package/pages/functions/buildWorker.ts +62 -0
  3. package/pages/functions/filepath-routing.test.ts +39 -0
  4. package/pages/functions/filepath-routing.ts +221 -0
  5. package/pages/functions/identifiers.ts +78 -0
  6. package/pages/functions/routes.ts +158 -0
  7. package/pages/functions/template-worker.ts +144 -0
  8. package/src/__tests__/dev.test.tsx +66 -0
  9. package/src/__tests__/index.test.ts +254 -120
  10. package/src/__tests__/jest.setup.ts +22 -0
  11. package/src/__tests__/kv.test.ts +1106 -0
  12. package/src/__tests__/mock-cfetch.ts +171 -0
  13. package/src/__tests__/mock-dialogs.ts +65 -0
  14. package/src/__tests__/run-in-tmp.ts +19 -0
  15. package/src/__tests__/run-wrangler.ts +32 -0
  16. package/src/api/form_data.ts +71 -98
  17. package/src/api/preview.ts +29 -24
  18. package/src/api/worker.ts +38 -44
  19. package/src/cfetch/index.ts +102 -0
  20. package/src/cfetch/internal.ts +65 -0
  21. package/src/cli.ts +6 -7
  22. package/src/config.ts +476 -113
  23. package/src/dev.tsx +291 -256
  24. package/src/dialogs.tsx +4 -12
  25. package/src/index.tsx +381 -252
  26. package/src/inspect.ts +524 -0
  27. package/src/kv.tsx +110 -45
  28. package/src/module-collection.ts +64 -0
  29. package/src/pages.tsx +900 -213
  30. package/src/proxy.ts +294 -0
  31. package/src/publish.ts +67 -63
  32. package/src/sites.tsx +10 -7
  33. package/src/tail.tsx +5 -3
  34. package/src/user.tsx +31 -33
  35. package/wrangler-dist/cli.js +43444 -56347
  36. package/wrangler-dist/cli.js.map +3 -3
  37. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  38. package/src/__tests__/mock-cfetch.js +0 -46
  39. package/src/api/inspect.ts +0 -441
  40. package/src/cfetch.ts +0 -72
  41. package/src/util/fetch.ts +0 -74
package/src/config.ts CHANGED
@@ -1,124 +1,487 @@
1
- // we're going to manually write both the type definition AND
2
- // the validator for the config, so that we can give better error messages
3
-
4
- type DOMigration = {
5
- tag: string;
6
- new_classes?: string[];
7
- renamed_classes?: string[];
8
- deleted_classes?: string[];
9
- };
1
+ /**
2
+ * This is the static type definition for the configuration object.
3
+ * It reflects the configuration that you can write in wrangler.toml,
4
+ * and optionally augment with arguments passed directly to wrangler.
5
+ * The type definition doesn't fully reflect the constraints applied
6
+ * to the configuration, but it is a good starting point. Later, we
7
+ * also defined a validator function that will validate the configuration
8
+ * with the same rules as the type definition, as well as the extra
9
+ * constraints. The type definition is good for asserting correctness
10
+ * in the wrangler codebase, whereas the validator function is useful
11
+ * for signalling errors in the configuration to a user of wrangler.
12
+ *
13
+ * For more information about the configuration object, see the
14
+ * documentation at https://developers.cloudflare.com/workers/cli-wrangler/configuration
15
+ *
16
+ * Legend for the annotations:
17
+ *
18
+ * *:optional means providing a value isn't mandatory
19
+ * *:deprecated means the field itself isn't necessary anymore in wrangler.toml
20
+ * *:breaking means the deprecation/optionality is a breaking change from wrangler 1
21
+ * *:todo means there's more work to be done (with details attached)
22
+ * *:inherited means the field is copied to all environments
23
+ */
24
+ export type Config = {
25
+ /**
26
+ * The name of your worker. Alphanumeric + dashes only.
27
+ *
28
+ * @optional
29
+ * @inherited
30
+ */
31
+ name?: string;
10
32
 
11
- type Project = "webpack" | "javascript" | "rust";
33
+ /**
34
+ * The entrypoint/path to the JavaScript file that will be executed.
35
+ *
36
+ * @optional
37
+ * @inherited
38
+ * @todo this needs to be implemented!
39
+ */
40
+ entry?: string;
12
41
 
13
- type Site = {
14
- // inherited
15
- bucket: string;
16
- "entry-point": string;
17
- include?: string[];
18
- exclude?: string[];
19
- };
42
+ /**
43
+ * This is the ID of the account associated with your zone.
44
+ * You might have more than one account, so make sure to use
45
+ * the ID of the account associated with the zone/route you
46
+ * provide, if you provide one. It can also be specified through
47
+ * the CF_ACCOUNT_ID environment variable.
48
+ *
49
+ * @optional
50
+ * @inherited
51
+ */
52
+ account_id?: string;
20
53
 
21
- type Dev = {
22
- ip?: string;
23
- port?: number;
24
- local_protocol?: string;
25
- upstream_protocol?: string;
26
- };
54
+ /**
55
+ * The project "type". A holdover from wrangler 1.x.
56
+ * Valid values were "webpack", "javascript", and "rust".
57
+ *
58
+ * @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.
59
+ * @optional
60
+ * @inherited
61
+ * @breaking
62
+ */
63
+ type?: "webpack" | "javascript" | "rust";
27
64
 
28
- type Vars = { [key: string]: string };
65
+ /**
66
+ * A date in the form yyyy-mm-dd, which will be used to determine
67
+ * which version of the Workers runtime is used. More details at
68
+ * https://developers.cloudflare.com/workers/platform/compatibility-dates
69
+ * @optional true for `dev`, false for `publish`
70
+ * @inherited
71
+ */
72
+ compatibility_date?: string;
29
73
 
30
- type Cron = string; // TODO: we should be able to parse a cron pattern with ts
74
+ /**
75
+ * A list of flags that enable features from upcoming features of
76
+ * the Workers runtime, usually used together with compatibility_flags.
77
+ * More details at
78
+ * https://developers.cloudflare.com/workers/platform/compatibility-dates
79
+ *
80
+ * @optional
81
+ * @inherited
82
+ * @todo This could be an enum!
83
+ */
84
+ compatibility_flags?: string[];
31
85
 
32
- type KVNamespace = {
33
- binding?: string;
34
- preview_id: string;
35
- id: string;
36
- };
86
+ /**
87
+ * Whether we use <name>.<subdomain>.workers.dev to
88
+ * test and deploy your worker.
89
+ *
90
+ * @default `true` (This is a breaking change from wrangler 1)
91
+ * @optional
92
+ * @inherited
93
+ * @breaking
94
+ */
95
+ workers_dev?: boolean;
37
96
 
38
- type DurableObject = {
39
- name: string;
40
- class_name: string;
41
- script_name?: string;
42
- };
97
+ /**
98
+ * The zone ID of the zone you want to deploy to. You can find this
99
+ * in your domain page on the dashboard.
100
+ *
101
+ * @deprecated This is unnecessary since we can deduce this from routes directly.
102
+ * @optional
103
+ * @inherited
104
+ */
105
+ zone_id?: string;
106
+
107
+ /**
108
+ * A list of routes that your worker should be deployed to.
109
+ * Only one of `routes` or `route` is required.
110
+ *
111
+ * @optional false only when workers_dev is false, and there's no scheduled worker
112
+ * @inherited
113
+ */
114
+ routes?: string[];
115
+
116
+ /**
117
+ * A route that your worker should be deployed to. Literally
118
+ * the same as routes, but only one.
119
+ * Only one of `routes` or `route` is required.
120
+ *
121
+ * @optional false only when workers_dev is false, and there's no scheduled worker
122
+ * @inherited
123
+ */
124
+ route?: string;
125
+
126
+ /**
127
+ * Path to the webpack config to use when building your worker.
128
+ * A holdover from wrangler 1.x, used with `type: "webpack"`.
129
+ *
130
+ * @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.
131
+ * @inherited
132
+ * @breaking
133
+ */
134
+ webpack_config?: string;
135
+
136
+ /**
137
+ * The function to use to replace jsx syntax.
138
+ *
139
+ * @default `"React.createElement"`
140
+ * @optional
141
+ * @inherited
142
+ */
143
+ jsx_factory?: string;
144
+
145
+ /**
146
+ * The function to use to replace jsx fragment syntax.
147
+ *
148
+ * @default `"React.Fragment"`
149
+ * @optional
150
+ * @inherited
151
+ */
152
+ jsx_fragment?: string;
153
+
154
+ /**
155
+ * A map of environment variables to set when deploying your worker.
156
+ * Of note, they can only be strings. Which is unfortunate, really.
157
+ * (TODO: verify that they can only be strings?)
158
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
159
+ *
160
+ * @default `{}`
161
+ * @optional
162
+ * @inherited false
163
+ */
164
+ vars?: { [key: string]: string };
165
+
166
+ /**
167
+ * A list of durable objects that your worker should be bound to.
168
+ * For more information about Durable Objects, see the documentation at
169
+ * https://developers.cloudflare.com/workers/learning/using-durable-objects
170
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
171
+ *
172
+ * @default `{ bindings: [] }`
173
+ * @optional
174
+ * @inherited false
175
+ */
176
+ durable_objects?: {
177
+ bindings: {
178
+ /** The name of the binding used to refer to the Durable Object */
179
+ name: string;
180
+ /** The exported class name of the Durable Object */
181
+ class_name: string;
182
+ /** The script where the Durable Object is defined (if it's external to this worker) */
183
+ script_name?: string;
184
+ }[];
185
+ };
186
+
187
+ /**
188
+ * These specify any Workers KV Namespaces you want to
189
+ * access from inside your Worker. To learn more about KV Namespaces,
190
+ * see the documentation at https://developers.cloudflare.com/workers/learning/how-kv-works
191
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
192
+ *
193
+ * @default `[]`
194
+ * @optional
195
+ * @inherited false
196
+ */
197
+ kv_namespaces?: {
198
+ /** The binding name used to refer to the KV Namespace */
199
+ binding: string;
200
+ /** The ID of the KV namespace */
201
+ id: string;
202
+ /** The ID of the KV namespace used during `wrangler dev` */
203
+ preview_id?: string;
204
+ }[];
205
+
206
+ /**
207
+ * A list of services that your worker should be bound to.
208
+ * NB: these are not inherited, and HAVE to be duplicated across all environments.
209
+ *
210
+ * @default `[]`
211
+ * @optional
212
+ * @inherited false
213
+ */
214
+ experimental_services?: {
215
+ /** The binding name used to refer to the Service */
216
+ name: string;
217
+ /** The name of the Service being bound */
218
+ service: string;
219
+ /** The Service's environment */
220
+ environment: string;
221
+ }[];
222
+
223
+ /**
224
+ * A list of migrations that should be uploaded with your Worker.
225
+ * These define changes in your Durable Object declarations.
226
+ * More details at https://developers.cloudflare.com/workers/learning/using-durable-objects#configuring-durable-object-classes-with-migrations
227
+ * NB: these ARE inherited, and SHOULD NOT be duplicated across all environments.
228
+ *
229
+ * @default `[]`
230
+ * @optional
231
+ * @inherited true
232
+ */
233
+ migrations?: {
234
+ /** A unique identifier for this migration. */
235
+ tag: string;
236
+ /** The new Durable Objects being defined. */
237
+ new_classes?: string[];
238
+ /** The Durable Objects being renamed. */
239
+ renamed_classes?: {
240
+ from: string;
241
+ to: string;
242
+ }[];
243
+ /** The Durable Objects being removed. */
244
+ deleted_classes?: string[];
245
+ }[];
246
+
247
+ /**
248
+ * The definition of a Worker Site, a feature that lets you upload
249
+ * static assets with your Worker.
250
+ * More details at https://developers.cloudflare.com/workers/platform/sites
251
+ * NB: This IS inherited, and SHOULD NOT be duplicated across all environments.
252
+ *
253
+ * @default `undefined`
254
+ * @optional
255
+ * @inherited true
256
+ */
257
+ site?: {
258
+ /**
259
+ * The directory containing your static assets. It must be
260
+ * a path relative to your wrangler.toml file.
261
+ * Example: bucket = "./public"
262
+ *
263
+ * optional false
264
+ */
265
+ bucket: string;
266
+
267
+ /**
268
+ * The location of your Worker script.
269
+ *
270
+ * @deprecated DO NOT use this (it's a holdover from wrangler 1.x). Either use the top level `entry` field, or pass the path to your entry file as a command line argument.
271
+ * @todo we should use a top level "entry" property instead
272
+ * @breaking
273
+ */
274
+ "entry-point": string;
43
275
 
44
- type Build = {
45
- command?: string;
46
- cwd?: string;
47
- watch_dir?: string;
48
- } & (
49
- | {
50
- upload?: {
51
- format: "service-worker";
52
- main: string;
53
- };
54
- }
55
- | {
56
- upload?: {
57
- format: "modules";
58
- dir?: string;
59
- main?: string;
60
- rules?: {
61
- type: "ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm";
62
- globs: string[]; // can we use typescript for these patterns?
63
- fallthrough?: boolean;
276
+ /**
277
+ * An exclusive list of .gitignore-style patterns that match file
278
+ * or directory names from your bucket location. Only matched
279
+ * items will be uploaded. Example: include = ["upload_dir"]
280
+ *
281
+ * @optional
282
+ * @default `[]`
283
+ * @todo this needs to be implemented!
284
+ */
285
+ include?: string[];
286
+
287
+ /**
288
+ * A list of .gitignore-style patterns that match files or
289
+ * directories in your bucket that should be excluded from
290
+ * uploads. Example: exclude = ["ignore_dir"]
291
+ *
292
+ * @optional
293
+ * @default `[]`
294
+ * @todo this needs to be implemented!
295
+ */
296
+ exclude?: string[];
297
+ };
298
+
299
+ /**
300
+ * "Cron" definitions to trigger a worker's "scheduled" function.
301
+ * Lets you call workers periodically, much like a cron job.
302
+ * More details here https://developers.cloudflare.com/workers/platform/cron-triggers
303
+ *
304
+ * @inherited
305
+ * @default `{ crons: [] }`
306
+ * @optional
307
+ * @todo can we use typescript for cron patterns?
308
+ */
309
+ triggers?: { crons: string[] };
310
+
311
+ /**
312
+ * Options to configure the development server that your worker will use.
313
+ * NB: This is NOT inherited, and SHOULD NOT be duplicated across all environments.
314
+ *
315
+ * @default `{}`
316
+ * @optional
317
+ * @inherited false
318
+ */
319
+ dev?: {
320
+ /**
321
+ * IP address for the local dev server to listen on,
322
+ *
323
+ * @default `127.0.0.1`
324
+ * @todo this needs to be implemented
325
+ */
326
+ ip?: string;
327
+
328
+ /**
329
+ * Port for the local dev server to listen on
330
+ *
331
+ * @default `8787`
332
+ */
333
+ port?: number;
334
+
335
+ /**
336
+ * Protocol that local wrangler dev server listens to requests on.
337
+ *
338
+ * @default `http`
339
+ * @todo this needs to be implemented
340
+ */
341
+ local_protocol?: string;
342
+
343
+ /**
344
+ * Protocol that wrangler dev forwards requests on
345
+ *
346
+ * @default `https`
347
+ * @todo this needs to be implemented
348
+ */
349
+ upstream_protocol?: string;
350
+ };
351
+
352
+ /**
353
+ * Specifies the Usage Model for your Worker. There are two options -
354
+ * [bundled](https://developers.cloudflare.com/workers/platform/limits#bundled-usage-model) and
355
+ * [unbound](https://developers.cloudflare.com/workers/platform/limits#unbound-usage-model).
356
+ * For newly created Workers, if the Usage Model is omitted
357
+ * it will be set to the [default Usage Model set on the account](https://dash.cloudflare.com/?account=workers/default-usage-model).
358
+ * For existing Workers, if the Usage Model is omitted, it will be
359
+ * set to the Usage Model configured in the dashboard for that Worker.
360
+ */
361
+ usage_model?: undefined | "bundled" | "unbound";
362
+
363
+ /**
364
+ * Configures a custom build step to be run by Wrangler when
365
+ * building your Worker. Refer to the [custom builds documentation](https://developers.cloudflare.com/workers/cli-wrangler/configuration#build)
366
+ * for more details.
367
+ *
368
+ * @default `undefined`
369
+ * @optional
370
+ * @inherited false
371
+ */
372
+ build?: {
373
+ /** 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. */
374
+ command?: string;
375
+ /** The directory in which the command is executed. */
376
+ cwd?: string;
377
+ /** The directory to watch for changes while using wrangler dev, defaults to the current working directory */
378
+ watch_dir?: string;
379
+ } & /**
380
+ * Much of the rest of this configuration isn't necessary anymore
381
+ * in wrangler2. We infer the format automatically, and we can pass
382
+ * the path to the script either in the CLI (or, @todo, as the top level
383
+ * `entry` property).
384
+ */ (
385
+ | {
386
+ upload?: {
387
+ /**
388
+ * The format of the Worker script, must be "service-worker".
389
+ *
390
+ * @deprecated We infer the format automatically now.
391
+ */
392
+ format: "service-worker";
393
+
394
+ /**
395
+ * The path to the Worker script. This should be replaced
396
+ * by the top level `entry' property.
397
+ *
398
+ * @deprecated This will be replaced by the top level `entry' property.
399
+ */
400
+ main: string;
64
401
  };
65
- };
66
- }
67
- );
68
-
69
- type UsageModel = "bundled" | "unbound";
70
-
71
- type Env = {
72
- name?: string; // inherited
73
- account_id?: string; // inherited
74
- workers_dev?: boolean; // inherited
75
- compatibility_date?: string; // inherited
76
- compatibility_flags?: string[]; // inherited
77
- zone_id?: string; // inherited
78
- routes?: string[]; // inherited
79
- route?: string; // inherited
80
- webpack_config?: string; // inherited
81
- site?: Site;
82
- jsx_factory?: string; // inherited
83
- jsx_fragment?: string; // inherited
84
- polyfill_node?: boolean; //inherited
85
- // we should use typescript to parse cron patterns
86
- triggers?: { crons: Cron[] }; // inherited
87
- vars?: Vars;
88
- durable_objects?: { bindings: DurableObject[] };
89
- kv_namespaces?: KVNamespace[];
90
- usage_model?: UsageModel; // inherited
91
- };
402
+ }
403
+ | {
404
+ /**
405
+ * When we use the module format, we only really
406
+ * need to specify the entry point. The format is deduced
407
+ * automatically in wrangler2.
408
+ */
409
+ upload?: {
410
+ /**
411
+ * The format of the Worker script, must be "modules".
412
+ *
413
+ * @deprecated We infer the format automatically now.
414
+ */
415
+ format: "modules";
92
416
 
93
- export type Config = {
94
- name?: string; // inherited
95
- account_id?: string; // inherited
96
- // @deprecated Don't use this
97
- type?: Project; // top level
98
- compatibility_date?: string; // inherited
99
- compatibility_flags?: string[]; // inherited
100
- // -- there's some mutually exclusive logic for this next block,
101
- // but I didn't bother for now
102
- workers_dev?: boolean; // inherited
103
- zone_id?: string; // inherited
104
- routes?: string[]; // inherited
105
- route?: string; // inherited
106
- // -- end mutually exclusive stuff
107
- // @deprecated Don't use this
108
- webpack_config?: string; // inherited
109
- jsx_factory?: string; // inherited
110
- jsx_fragment?: string; // inherited
111
- polyfill_node?: boolean; //inherited
112
- vars?: Vars;
113
- migrations?: DOMigration[];
114
- durable_objects?: { bindings: DurableObject[] };
115
- kv_namespaces?: KVNamespace[];
116
- site?: Site; // inherited
117
- // we should use typescript to parse cron patterns
118
- triggers?: { crons: Cron[] }; // inherited
119
- dev?: Dev;
120
- usage_model?: UsageModel; // inherited
121
- // top level
122
- build?: Build;
123
- env?: { [envName: string]: void | Env };
417
+ /**
418
+ * The directory you wish to upload your modules from,
419
+ * defaults to the dist relative to the project root directory.
420
+ *
421
+ * @deprecated
422
+ * @breaking
423
+ */
424
+ dir?: string;
425
+
426
+ /**
427
+ * The path to the Worker script. This should be replaced
428
+ * by the top level `entry' property.
429
+ *
430
+ * @deprecated This will be replaced by the top level `entry' property.
431
+ */
432
+ main?: string;
433
+
434
+ /**
435
+ * An ordered list of rules that define which modules to import,
436
+ * and what type to import them as. You will need to specify rules
437
+ * to use Text, Data, and CompiledWasm modules, or when you wish to
438
+ * have a .js file be treated as an ESModule instead of CommonJS.
439
+ *
440
+ * @deprecated These are now inferred automatically for major file types, but you can still specify them manually.
441
+ * @todo this needs to be implemented!
442
+ * @breaking
443
+ */
444
+ rules?: {
445
+ type: "ESModule" | "CommonJS" | "Text" | "Data" | "CompiledWasm";
446
+ globs: string[];
447
+ fallthrough?: boolean;
448
+ };
449
+ };
450
+ }
451
+ );
452
+
453
+ /**
454
+ * The `env` section defines overrides for the configuration for
455
+ * different environments. Most fields can be overridden, while
456
+ * some have to be specifically duplicated in every environment.
457
+ * For more information, see the documentation at https://developers.cloudflare.com/workers/cli-wrangler/configuration#environments
458
+ */
459
+ env?: {
460
+ [envName: string]:
461
+ | undefined
462
+ | Omit<Config, "env" | "migrations" | "site" | "dev">;
463
+ };
124
464
  };
465
+
466
+ type ValidationResults = (
467
+ | { key: string; info: string }
468
+ | { key: string; error: string }
469
+ | { key: string; warning: string }
470
+ )[];
471
+
472
+ /**
473
+ * We also define a validation function that manually validates
474
+ * every field in the configuration as per the type definitions,
475
+ * as well as extra constraints we apply to some fields, as well
476
+ * as some constraints on combinations of fields. This is useful for
477
+ * presenting errors and messages to the user. Eventually, we will
478
+ * combine this with some automatic config rewriting tools.
479
+ *
480
+ */
481
+ export async function validateConfig(
482
+ _config: Partial<Config>
483
+ ): Promise<ValidationResults> {
484
+ const results: ValidationResults = [];
485
+
486
+ return results;
487
+ }