wrangler 0.0.0-e14e589 → 0.0.0-e928f94

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.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ ## 🤠 wrangler
2
+
3
+ `wrangler` is a command line tool for building [Cloudflare Workers](https://workers.cloudflare.com/).
4
+
5
+ [(Read the full stack week launch blog post.)](https://blog.cloudflare.com/wrangler-v2-beta/)
6
+
7
+ **DISCLAIMER**: This is a work in progress, and is NOT recommended for use in production. We are opening this preview for feedback from the community, and to openly share our [roadmap](https://github.com/cloudflare/wrangler2/issues/12) for the future. As such, expect APIs and documentation to change before the end of the preview.
8
+
9
+ Further, we will NOT do a general release until we are both feature complete, and have a full backward compatibility and incremental migration plan in place. For more details, follow the [parent roadmap issue](https://github.com/cloudflare/wrangler2/issues/12).
10
+
11
+ ## Quick Start
12
+
13
+ ```bash
14
+ # Make a javascript file
15
+ $ echo "export default { fetch() { return new Response('hello world') } }" > index.js
16
+ # try it out
17
+ $ npx wrangler@beta dev index.js
18
+ # and then publish it
19
+ $ npx wrangler@beta publish index.js --name my-worker
20
+ # visit https://my-worker.<your workers subdomain>.workers.dev
21
+ ```
22
+
23
+ ## Installation:
24
+
25
+ ```bash
26
+ $ npm install wrangler@beta
27
+ ```
28
+
29
+ ## Commands
30
+
31
+ ### `wrangler init [name]`
32
+
33
+ Creates a `wrangler.toml` configuration file. For more details on the configuration keys and values, refer to the [documentation](https://developers.cloudflare.com/workers/cli-wrangler/configuration).
34
+
35
+ ### `wrangler dev [script]`
36
+
37
+ Start a local development server, with live reloading and devtools.
38
+
39
+ ### `wrangler publish [script] --name [name]`
40
+
41
+ Publish the given script to the worldwide Cloudflare network.
42
+
43
+ For more commands and options, refer to the [documentation](https://developers.cloudflare.com/workers/cli-wrangler/commands).
44
+
45
+ ### `wrangler pages dev [directory] [-- command]`
46
+
47
+ Either serves a static build asset directory, or proxies itself in front of a command.
48
+
49
+ Builds and runs functions from a `./functions` directory or uses a `_worker.js` file inside the static build asset directory.
50
+
51
+ For more commands and options, refer to the [documentation](https://developers.cloudflare.com/pages/platform/functions#develop-and-preview-locally) or run `wrangler pages dev --help`.
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "0.0.0-e14e589",
3
+ "version": "0.0.0-e928f94",
4
4
  "author": "wrangler@cloudflare.com",
5
5
  "description": "Command-line interface for all things Cloudflare Workers",
6
- "bin": "./bin/wrangler.js",
6
+ "bin": {
7
+ "wrangler": "./bin/wrangler.js",
8
+ "wrangler2": "./bin/wrangler.js"
9
+ },
7
10
  "license": "MIT OR Apache-2.0",
8
11
  "bugs": {
9
12
  "url": "https://github.com/cloudflare/wrangler/issues"
@@ -33,11 +36,10 @@
33
36
  "cli"
34
37
  ],
35
38
  "dependencies": {
36
- "@cloudflare/pages-functions-compiler": "0.3.4",
37
- "esbuild": "0.13.12",
38
- "miniflare": "2.0.0-rc.2",
39
- "semiver": "^1.1.0",
40
- "serve": "^13.0.2"
39
+ "@cloudflare/pages-functions-compiler": "0.3.8",
40
+ "esbuild": "0.14.1",
41
+ "miniflare": "2.0.0-rc.3",
42
+ "semiver": "^1.1.0"
41
43
  },
42
44
  "optionalDependencies": {
43
45
  "fsevents": "~2.3.2"
@@ -46,32 +48,32 @@
46
48
  "@iarna/toml": "^2.2.5",
47
49
  "@types/cloudflare": "^2.7.6",
48
50
  "@types/express": "^4.17.13",
49
- "@types/react": "^17.0.34",
51
+ "@types/react": "^17.0.37",
50
52
  "@types/signal-exit": "^3.0.1",
51
- "@types/ws": "^8.2.0",
52
- "@types/yargs": "^17.0.5",
53
+ "@types/ws": "^8.2.1",
54
+ "@types/yargs": "^17.0.7",
55
+ "chokidar": "^3.5.2",
53
56
  "clipboardy": "^3.0.0",
54
57
  "command-exists": "^1.2.9",
55
- "execa": "^5.1.1",
56
- "express": "^4.17.1",
58
+ "execa": "^6.0.0",
57
59
  "finalhandler": "^1.1.2",
58
60
  "find-up": "^6.2.0",
59
61
  "formdata-node": "^4.3.1",
60
62
  "http-proxy": "^1.18.1",
61
- "http-proxy-middleware": "^2.0.1",
62
63
  "ink": "^3.2.0",
63
- "ink-select-input": "^4.2.0",
64
+ "ink-select-input": "^4.2.1",
64
65
  "ink-table": "^3.0.0",
65
- "ink-text-input": "^4.0.1",
66
+ "ink-text-input": "^4.0.2",
66
67
  "node-fetch": "^3.1.0",
67
68
  "open": "^8.4.0",
68
69
  "path-to-regexp": "^6.2.0",
69
70
  "react": "^17.0.2",
70
71
  "serve-static": "^1.14.1",
71
- "signal-exit": "^3.0.5",
72
+ "signal-exit": "^3.0.6",
72
73
  "tmp-promise": "^3.0.3",
73
- "ws": "^8.2.3",
74
- "yargs": "^17.2.1"
74
+ "undici": "^4.11.1",
75
+ "ws": "^8.3.0",
76
+ "yargs": "^17.3.0"
75
77
  },
76
78
  "files": [
77
79
  "src",
@@ -90,12 +92,12 @@
90
92
  "test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
91
93
  },
92
94
  "engines": {
93
- "node": ">=16.0.0"
95
+ "node": ">=16.7.0"
94
96
  },
95
97
  "jest": {
96
98
  "testRegex": ".*.(test|spec)\\.[jt]sx?$",
97
99
  "transformIgnorePatterns": [
98
- "node_modules/(?!node-fetch|fetch-blob|find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|data-uri-to-buffer|formdata-polyfill)"
100
+ "node_modules/(?!node-fetch|fetch-blob|find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|data-uri-to-buffer|formdata-polyfill|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)"
99
101
  ],
100
102
  "moduleNameMapper": {
101
103
  "clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
@@ -62,12 +62,12 @@ describe("wrangler", () => {
62
62
  wrangler pages ⚡️ Configure Cloudflare Pages
63
63
 
64
64
  Flags:
65
- --config Path to .toml configuration file [string]
66
- --help Show help [boolean]
67
- --version Show version number [boolean]
65
+ -c, --config Path to .toml configuration file [string]
66
+ -h, --help Show help [boolean]
67
+ -v, --version Show version number [boolean]
68
68
 
69
69
  Options:
70
- --local Run on my machine [boolean] [default: false]"
70
+ -l, --local Run on my machine [boolean] [default: false]"
71
71
  `);
72
72
  });
73
73
 
@@ -110,7 +110,7 @@ describe("wrangler", () => {
110
110
  return { id: "some-namespace-id" };
111
111
  });
112
112
 
113
- await w('kv:namespace create "UnitTestNamespace"');
113
+ await w("kv:namespace create UnitTestNamespace");
114
114
  expect(
115
115
  KVNamespaces.find((ns) => ns.title === `worker-UnitTestNamespace`)
116
116
  ).toBeTruthy();
@@ -1,7 +1,7 @@
1
1
  // This file mocks ../cfetch.ts
2
2
  // so we can insert whatever responses we want from it
3
3
 
4
- const pathToRegexp = require("path-to-regexp");
4
+ const { pathToRegexp } = require("path-to-regexp");
5
5
  // TODO: add jsdoc style types here
6
6
 
7
7
  // type MockHandler = (resource: string, init?: RequestInit) => any; // TODO: use a generic here
@@ -3,8 +3,6 @@ import type {
3
3
  CfModuleType,
4
4
  CfVariable,
5
5
  CfModule,
6
- CfKvNamespace,
7
- CfCryptoKey,
8
6
  } from "./worker.js";
9
7
  import { FormData, Blob } from "formdata-node";
10
8
 
@@ -27,12 +25,26 @@ function toBinding(
27
25
  return { name, type: "plain_text", text: variable };
28
26
  }
29
27
 
30
- const { namespaceId } = variable as CfKvNamespace;
31
- if (namespaceId) {
32
- return { name, type: "kv_namespace", namespace_id: namespaceId };
28
+ if ("namespaceId" in variable) {
29
+ return {
30
+ name,
31
+ type: "kv_namespace",
32
+ namespace_id: variable.namespaceId,
33
+ };
33
34
  }
34
35
 
35
- const { format, algorithm, usages, data } = variable as CfCryptoKey;
36
+ if ("class_name" in variable) {
37
+ return {
38
+ name,
39
+ type: "durable_object_namespace",
40
+ class_name: variable.class_name,
41
+ ...(variable.script_name && {
42
+ script_name: variable.script_name,
43
+ }),
44
+ };
45
+ }
46
+
47
+ const { format, algorithm, usages, data } = variable;
36
48
  if (format) {
37
49
  let key_base64;
38
50
  let key_jwk;
@@ -84,7 +96,15 @@ function toModule(module: CfModule, entryType?: CfModuleType): Blob {
84
96
  */
85
97
  export function toFormData(worker: CfWorkerInit): FormData {
86
98
  const formData = new FormData();
87
- const { main, modules, variables, usage_model, compatibility_date } = worker;
99
+ const {
100
+ main,
101
+ modules,
102
+ variables,
103
+ migrations,
104
+ usage_model,
105
+ compatibility_date,
106
+ compatibility_flags,
107
+ } = worker;
88
108
  const { name, type: mainType } = main;
89
109
 
90
110
  const bindings = [];
@@ -93,23 +113,19 @@ export function toFormData(worker: CfWorkerInit): FormData {
93
113
  bindings.push(binding);
94
114
  }
95
115
 
96
- const singleton = mainType === "commonjs";
116
+ // TODO: this object should be typed
97
117
  const metadata = {
98
- main_module: singleton ? undefined : name,
99
- body_part: singleton ? name : undefined,
118
+ ...(mainType !== "commonjs" ? { main_module: name } : { body_part: name }),
100
119
  bindings,
120
+ ...(compatibility_date && { compatibility_date }),
121
+ ...(compatibility_flags && { compatibility_flags }),
122
+ ...(usage_model && { usage_model }),
123
+ ...(migrations && { migrations }),
101
124
  };
102
- if (compatibility_date) {
103
- // @ts-expect-error - we should type metadata
104
- metadata.compatibility_date = compatibility_date;
105
- }
106
- if (usage_model) {
107
- // @ts-expect-error - we should type metadata
108
- metadata.usage_model = usage_model;
109
- }
125
+
110
126
  formData.set("metadata", JSON.stringify(metadata));
111
127
 
112
- if (singleton && modules && modules.length > 0) {
128
+ if (mainType === "commonjs" && modules && modules.length > 0) {
113
129
  throw new TypeError(
114
130
  "More than one module can only be specified when type = 'esm'"
115
131
  );
@@ -1,7 +1,7 @@
1
1
  import cfetch from "../cfetch";
2
- import { fetchJson } from "../util/fetch";
3
2
  import { toFormData } from "./form_data";
4
3
  import type { CfAccount, CfWorkerInit } from "./worker";
4
+ import fetch from "node-fetch";
5
5
 
6
6
  /**
7
7
  * A preview mode.
@@ -9,7 +9,7 @@ import type { CfAccount, CfWorkerInit } from "./worker";
9
9
  * * If true, then using a `workers.dev` subdomain.
10
10
  * * Otherwise, a list of routes under a single zone.
11
11
  */
12
- export type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
12
+ type CfPreviewMode = { workers_dev: boolean } | { routes: string[] };
13
13
 
14
14
  /**
15
15
  * A preview token.
@@ -60,17 +60,17 @@ async function sessionToken(account: CfAccount): Promise<CfPreviewToken> {
60
60
  ? `/zones/${zoneId}/workers/edge-preview`
61
61
  : `/accounts/${accountId}/workers/subdomain/edge-preview`;
62
62
 
63
- const { exchange_url: tokenUrl } = await cfetch<{ exchange_url: string }>(
64
- initUrl
65
- );
66
- const { inspector_websocket: url, token } = await fetchJson()(tokenUrl);
67
- const { host } = new URL(url);
63
+ const { exchange_url } = await cfetch<{ exchange_url: string }>(initUrl);
64
+ const { inspector_websocket, token } = (await (
65
+ await fetch(exchange_url)
66
+ ).json()) as { inspector_websocket: string; token: string };
67
+ const { host } = new URL(inspector_websocket);
68
68
  const query = `cf_workers_preview_token=${token}`;
69
69
 
70
70
  return {
71
71
  value: token,
72
72
  host,
73
- inspectorUrl: new URL(`${url}?${query}`),
73
+ inspectorUrl: new URL(`${inspector_websocket}?${query}`),
74
74
  prewarmUrl: new URL(
75
75
  `https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
76
76
  ),
@@ -96,27 +96,32 @@ export async function previewToken(
96
96
  const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(account);
97
97
 
98
98
  const { accountId, zoneId } = account;
99
- const scriptId = zoneId ? randomId() : host.split(".")[0];
99
+ const scriptId = zoneId ? randomId() : worker.name || host.split(".")[0];
100
100
  const url = `/accounts/${accountId}/workers/scripts/${scriptId}/edge-preview`;
101
101
 
102
- const mode = zoneId ? { routes: ["*/*"] } : { workers_dev: true };
102
+ const mode: CfPreviewMode = zoneId
103
+ ? { routes: ["*/*"] }
104
+ : { workers_dev: true };
105
+
103
106
  const formData = toFormData(worker);
104
107
  formData.set("wrangler-session-config", JSON.stringify(mode));
105
108
 
106
- const { preview_token: token } = await cfetch<{ preview_token: string }>(
107
- url,
108
- {
109
- method: "POST",
110
- // @ts-expect-error TODO: fix this
111
- body: formData,
112
- headers: {
113
- "cf-preview-upload-config-token": value,
114
- },
115
- }
116
- );
109
+ const { preview_token } = await cfetch<{ preview_token: string }>(url, {
110
+ method: "POST",
111
+ // @ts-expect-error TODO: fix this
112
+ body: formData,
113
+ headers: {
114
+ "cf-preview-upload-config-token": value,
115
+ },
116
+ });
117
+
117
118
  return {
118
- value: token,
119
- host,
119
+ value: preview_token,
120
+ // TODO: verify this works with zoned workers
121
+ host:
122
+ worker.name && !zoneId
123
+ ? `${worker.name}.${host.split(".").slice(1).join(".")}`
124
+ : host,
120
125
  inspectorUrl,
121
126
  prewarmUrl,
122
127
  };
package/src/api/worker.ts CHANGED
@@ -72,6 +72,21 @@ export interface CfKvNamespace {
72
72
  namespaceId: string;
73
73
  }
74
74
 
75
+ export interface CfDurableObject {
76
+ class_name: string;
77
+ script_name?: string;
78
+ }
79
+
80
+ interface CfDOMigrations {
81
+ old_tag?: string;
82
+ new_tag: string;
83
+ steps: {
84
+ new_classes?: string[];
85
+ renamed_classes?: string[];
86
+ deleted_classes?: string[];
87
+ }[];
88
+ }
89
+
75
90
  /**
76
91
  * A `WebCrypto` key.
77
92
  *
@@ -99,12 +114,16 @@ export interface CfCryptoKey {
99
114
  /**
100
115
  * A variable (aka. environment variable).
101
116
  */
102
- export type CfVariable = string | CfKvNamespace | CfCryptoKey;
117
+ export type CfVariable = string | CfKvNamespace | CfCryptoKey | CfDurableObject;
103
118
 
104
119
  /**
105
120
  * Options for creating a `CfWorker`.
106
121
  */
107
122
  export interface CfWorkerInit {
123
+ /**
124
+ * The name of the worker.
125
+ */
126
+ name: string | void;
108
127
  /**
109
128
  * The entrypoint module.
110
129
  */
@@ -117,8 +136,10 @@ export interface CfWorkerInit {
117
136
  * The map of names to variables. (aka. environment variables)
118
137
  */
119
138
  variables?: { [name: string]: CfVariable };
120
- compatibility_date?: string;
121
- usage_model?: void | "bundled" | "unbound";
139
+ migrations: void | CfDOMigrations;
140
+ compatibility_date: string | void;
141
+ compatibility_flags: void | string[];
142
+ usage_model: void | "bundled" | "unbound";
122
143
  }
123
144
 
124
145
  /**
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 DOMigration = {
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 = {
@@ -28,6 +35,12 @@ type KVNamespace = {
28
35
  id: string;
29
36
  };
30
37
 
38
+ type DurableObject = {
39
+ name: string;
40
+ class_name: string;
41
+ script_name?: string;
42
+ };
43
+
31
44
  type Build = {
32
45
  command?: string;
33
46
  cwd?: string;
@@ -59,14 +72,21 @@ type Env = {
59
72
  name?: string; // inherited
60
73
  account_id?: string; // inherited
61
74
  workers_dev?: boolean; // inherited
75
+ compatibility_date?: string; // inherited
76
+ compatibility_flags?: string[]; // inherited
62
77
  zone_id?: string; // inherited
63
78
  routes?: string[]; // inherited
64
79
  route?: string; // inherited
65
80
  webpack_config?: string; // inherited
66
81
  site?: Site;
82
+ jsx_factory?: string; // inherited
83
+ jsx_fragment?: string; // inherited
67
84
  // we should use typescript to parse cron patterns
68
85
  triggers?: { crons: Cron[] }; // inherited
86
+ vars?: Vars;
87
+ durable_objects?: { bindings: DurableObject[] };
69
88
  kv_namespaces?: KVNamespace[];
89
+ usage_model?: UsageModel; // inherited
70
90
  };
71
91
 
72
92
  export type Config = {
@@ -74,6 +94,8 @@ export type Config = {
74
94
  account_id?: string; // inherited
75
95
  // @deprecated Don't use this
76
96
  type?: Project; // top level
97
+ compatibility_date?: string; // inherited
98
+ compatibility_flags?: string[]; // inherited
77
99
  // -- there's some mutually exclusive logic for this next block,
78
100
  // but I didn't bother for now
79
101
  workers_dev?: boolean; // inherited
@@ -83,7 +105,11 @@ export type Config = {
83
105
  // -- end mutually exclusive stuff
84
106
  // @deprecated Don't use this
85
107
  webpack_config?: string; // inherited
108
+ jsx_factory?: string; // inherited
109
+ jsx_fragment?: string; // inherited
86
110
  vars?: Vars;
111
+ migrations?: DOMigration[];
112
+ durable_objects?: { bindings: DurableObject[] };
87
113
  kv_namespaces?: KVNamespace[];
88
114
  site?: Site; // inherited
89
115
  // we should use typescript to parse cron patterns
@@ -92,5 +118,5 @@ export type Config = {
92
118
  usage_model?: UsageModel; // inherited
93
119
  // top level
94
120
  build?: Build;
95
- env?: { [envName: string]: Env };
121
+ env?: { [envName: string]: void | Env };
96
122
  };