wrangler 2.0.17 → 2.0.21

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 (38) hide show
  1. package/bin/wrangler.js +36 -5
  2. package/package.json +2 -2
  3. package/src/__tests__/configuration.test.ts +11 -7
  4. package/src/__tests__/dev.test.tsx +44 -0
  5. package/src/__tests__/helpers/run-in-tmp.ts +18 -26
  6. package/src/__tests__/https-options.test.ts +50 -17
  7. package/src/__tests__/jest.setup.ts +13 -0
  8. package/src/__tests__/metrics.test.ts +415 -0
  9. package/src/__tests__/pages.test.ts +15 -8
  10. package/src/__tests__/publish.test.ts +37 -1
  11. package/src/__tests__/test-old-node-version.js +31 -0
  12. package/src/__tests__/user.test.ts +5 -2
  13. package/src/config/config.ts +8 -0
  14. package/src/config/validation.ts +9 -0
  15. package/src/config-cache.ts +2 -1
  16. package/src/dev/dev.tsx +28 -18
  17. package/src/dev/local.tsx +7 -4
  18. package/src/dev/remote.tsx +5 -1
  19. package/src/dev.tsx +7 -1
  20. package/src/global-wrangler-config-path.ts +26 -0
  21. package/src/https-options.ts +2 -1
  22. package/src/index.tsx +73 -0
  23. package/src/metrics/index.ts +4 -0
  24. package/src/metrics/metrics-config.ts +222 -0
  25. package/src/metrics/metrics-dispatcher.ts +95 -0
  26. package/src/metrics/send-event.ts +92 -0
  27. package/src/pages/build.tsx +2 -0
  28. package/src/pages/deployments.tsx +6 -1
  29. package/src/pages/dev.tsx +4 -0
  30. package/src/pages/projects.tsx +7 -1
  31. package/src/pages/publish.tsx +3 -0
  32. package/src/pages/upload.tsx +26 -14
  33. package/src/publish.ts +28 -15
  34. package/src/pubsub/pubsub-commands.tsx +46 -0
  35. package/src/user/user.tsx +13 -6
  36. package/src/whoami.tsx +2 -1
  37. package/src/worker-namespace.ts +16 -0
  38. package/wrangler-dist/cli.js +2114 -1238
package/bin/wrangler.js CHANGED
@@ -3,7 +3,6 @@ const { spawn } = require("child_process");
3
3
  const path = require("path");
4
4
  const fs = require("fs");
5
5
  const os = require("os");
6
- const semiver = require("semiver");
7
6
 
8
7
  const MIN_NODE_VERSION = "16.7.0";
9
8
  const debug =
@@ -21,8 +20,8 @@ function runWrangler() {
21
20
  // Note Volta and nvm are also recommended in the official docs:
22
21
  // https://developers.cloudflare.com/workers/get-started/guide#2-install-the-workers-cli
23
22
  console.error(
24
- `Wrangler requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}.
25
- You should use the latest Node.js version if possible, as Cloudflare Workers use a very up-to-date version of V8.
23
+ `Wrangler requires at least node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}. Please update your version of node.js.
24
+
26
25
  Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
27
26
  );
28
27
  process.exitCode = 1;
@@ -128,10 +127,42 @@ async function main() {
128
127
  }
129
128
 
130
129
  process.on("SIGINT", () => {
131
- wranglerProcess?.kill();
130
+ wranglerProcess && wranglerProcess.kill();
132
131
  });
133
132
  process.on("SIGTERM", () => {
134
- wranglerProcess?.kill();
133
+ wranglerProcess && wranglerProcess.kill();
135
134
  });
136
135
 
136
+ // semiver implementation via https://github.com/lukeed/semiver/blob/ae7eebe6053c96be63032b14fb0b68e2553fcac4/src/index.js
137
+
138
+ /**
139
+ MIT License
140
+
141
+ Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com)
142
+
143
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
144
+
145
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
146
+
147
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
148
+
149
+ */
150
+
151
+ var fn = new Intl.Collator(0, { numeric: 1 }).compare;
152
+
153
+ function semiver(a, b, bool) {
154
+ a = a.split(".");
155
+ b = b.split(".");
156
+
157
+ return (
158
+ fn(a[0], b[0]) ||
159
+ fn(a[1], b[1]) ||
160
+ ((b[2] = b.slice(2).join(".")),
161
+ (bool = /[.-]/.test((a[2] = a.slice(2).join(".")))),
162
+ bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1)
163
+ );
164
+ }
165
+
166
+ // end semiver implementation
167
+
137
168
  void main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "2.0.17",
3
+ "version": "2.0.21",
4
4
  "description": "Command-line interface for all things Cloudflare Workers",
5
5
  "keywords": [
6
6
  "wrangler",
@@ -93,7 +93,6 @@
93
93
  "nanoid": "^3.3.3",
94
94
  "path-to-regexp": "^6.2.0",
95
95
  "selfsigned": "^2.0.1",
96
- "semiver": "^1.1.0",
97
96
  "xxhash-wasm": "^1.0.1"
98
97
  },
99
98
  "devDependencies": {
@@ -149,6 +148,7 @@
149
148
  "undici": "^5.5.1",
150
149
  "update-check": "^1.5.4",
151
150
  "ws": "^8.5.0",
151
+ "xdg-app-paths": "^7.3.0",
152
152
  "yargs": "^17.4.1"
153
153
  },
154
154
  "optionalDependencies": {
@@ -40,6 +40,7 @@ describe("normalizeAndValidateConfig()", () => {
40
40
  tsconfig: undefined,
41
41
  kv_namespaces: [],
42
42
  legacy_env: true,
43
+ send_metrics: undefined,
43
44
  main: undefined,
44
45
  migrations: [],
45
46
  name: undefined,
@@ -76,6 +77,7 @@ describe("normalizeAndValidateConfig()", () => {
76
77
  it("should override config defaults with provided values", () => {
77
78
  const expectedConfig: Partial<ConfigFields<RawDevConfig>> = {
78
79
  legacy_env: true,
80
+ send_metrics: false,
79
81
  dev: {
80
82
  ip: "255.255.255.255",
81
83
  port: 9999,
@@ -98,6 +100,7 @@ describe("normalizeAndValidateConfig()", () => {
98
100
  it("should error on invalid top level fields", () => {
99
101
  const expectedConfig = {
100
102
  legacy_env: "FOO",
103
+ send_metrics: "BAD",
101
104
  dev: {
102
105
  ip: 222,
103
106
  port: "FOO",
@@ -117,13 +120,14 @@ describe("normalizeAndValidateConfig()", () => {
117
120
  );
118
121
  expect(diagnostics.hasWarnings()).toBe(false);
119
122
  expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
120
- "Processing wrangler configuration:
121
- - Expected \\"legacy_env\\" to be of type boolean but got \\"FOO\\".
122
- - Expected \\"dev.ip\\" to be of type string but got 222.
123
- - Expected \\"dev.port\\" to be of type number but got \\"FOO\\".
124
- - Expected \\"dev.local_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"wss\\".
125
- - Expected \\"dev.upstream_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"ws\\"."
126
- `);
123
+ "Processing wrangler configuration:
124
+ - Expected \\"legacy_env\\" to be of type boolean but got \\"FOO\\".
125
+ - Expected \\"send_metrics\\" to be of type boolean but got \\"BAD\\".
126
+ - Expected \\"dev.ip\\" to be of type string but got 222.
127
+ - Expected \\"dev.port\\" to be of type number but got \\"FOO\\".
128
+ - Expected \\"dev.local_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"wss\\".
129
+ - Expected \\"dev.upstream_protocol\\" field to be one of [\\"http\\",\\"https\\"] but got \\"ws\\"."
130
+ `);
127
131
  });
128
132
 
129
133
  it("should warn on and remove unexpected top level fields", () => {
@@ -1049,6 +1049,50 @@ describe("wrangler dev", () => {
1049
1049
  }
1050
1050
  `);
1051
1051
  });
1052
+
1053
+ it("should default to true, without a warning", async () => {
1054
+ fs.writeFileSync("index.js", `export default {};`);
1055
+ await runWrangler("dev index.js");
1056
+ expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(true);
1057
+ expect(std).toMatchInlineSnapshot(`
1058
+ Object {
1059
+ "debug": "",
1060
+ "err": "",
1061
+ "out": "",
1062
+ "warn": "",
1063
+ }
1064
+ `);
1065
+ });
1066
+
1067
+ it("should pass true, with a warning", async () => {
1068
+ fs.writeFileSync("index.js", `export default {};`);
1069
+ await runWrangler("dev index.js --inspect");
1070
+ expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(true);
1071
+ expect(std).toMatchInlineSnapshot(`
1072
+ Object {
1073
+ "debug": "",
1074
+ "err": "",
1075
+ "out": "",
1076
+ "warn": "▲ [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
1077
+
1078
+ ",
1079
+ }
1080
+ `);
1081
+ });
1082
+
1083
+ it("should pass false, without a warning", async () => {
1084
+ fs.writeFileSync("index.js", `export default {};`);
1085
+ await runWrangler("dev index.js --inspect false");
1086
+ expect((Dev as jest.Mock).mock.calls[0][0].inspect).toEqual(false);
1087
+ expect(std).toMatchInlineSnapshot(`
1088
+ Object {
1089
+ "debug": "",
1090
+ "err": "",
1091
+ "out": "",
1092
+ "warn": "",
1093
+ }
1094
+ `);
1095
+ });
1052
1096
  });
1053
1097
 
1054
1098
  describe("service bindings", () => {
@@ -5,42 +5,34 @@ import { reinitialiseAuthTokens } from "../../user";
5
5
 
6
6
  const originalCwd = process.cwd();
7
7
 
8
- export function runInTempDir(
9
- { homedir }: { homedir?: string } = { homedir: "./home" }
10
- ) {
8
+ export function runInTempDir({ homedir } = { homedir: "./home" }) {
11
9
  let tmpDir: string;
12
10
 
13
- beforeAll(() => {
14
- if (tmpDir !== undefined) {
15
- process.chdir(originalCwd);
16
- fs.rmSync(tmpDir, { recursive: true });
17
- }
18
- });
19
-
20
11
  beforeEach(() => {
21
12
  // Use realpath because the temporary path can point to a symlink rather than the actual path.
22
13
  tmpDir = fs.realpathSync(
23
14
  fs.mkdtempSync(path.join(os.tmpdir(), "wrangler-tests"))
24
15
  );
16
+
25
17
  process.chdir(tmpDir);
26
- if (homedir !== undefined) {
27
- // The path that is returned from `homedir()` should be absolute.
28
- const absHomedir = path.resolve(tmpDir, homedir);
29
- // Override where the home directory is so that we can write our own user config,
30
- // without destroying the real thing.
31
- fs.mkdirSync(absHomedir, { recursive: true });
32
- // Note it is very important that we use the "default" value from "node:os" (e.g. `import os from "node:os";`)
33
- // rather than an alias to the module (e.g. `import * as os from "node:os";`).
34
- // This is because the module gets transpiled so that the "method" `homedir()` gets converted to a
35
- // getter that is not configurable (and so cannot be spied upon).
36
- jest.spyOn(os, "homedir").mockReturnValue(absHomedir);
37
- // Now that we have changed the home directory location, we must reinitialize the user auth state
38
- reinitialiseAuthTokens();
39
- }
18
+ // The path that is returned from `homedir()` should be absolute.
19
+ const absHomedir = path.resolve(tmpDir, homedir);
20
+ // Override where the home directory is so that we can write our own user config,
21
+ // without destroying the real thing.
22
+ fs.mkdirSync(absHomedir, { recursive: true });
23
+ // Note it is very important that we use the "default" value from "node:os" (e.g. `import os from "node:os";`)
24
+ // rather than an alias to the module (e.g. `import * as os from "node:os";`).
25
+ // This is because the module gets transpiled so that the "method" `homedir()` gets converted to a
26
+ // getter that is not configurable (and so cannot be spied upon).
27
+ jest.spyOn(os, "homedir")?.mockReturnValue(absHomedir);
28
+ // Now that we have changed the home directory location, we must reinitialize the user auth state
29
+ reinitialiseAuthTokens();
40
30
  });
41
31
 
42
32
  afterEach(() => {
43
- process.chdir(originalCwd);
44
- fs.rmSync(tmpDir, { recursive: true });
33
+ if (fs.existsSync(tmpDir)) {
34
+ process.chdir(originalCwd);
35
+ fs.rmSync(tmpDir, { recursive: true });
36
+ }
45
37
  });
46
38
  }
@@ -1,6 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import os from "node:os";
3
- import { resolve } from "node:path";
3
+ import path from "node:path";
4
+ import { getGlobalWranglerConfigPath } from "../global-wrangler-config-path";
4
5
  import { getHttpsOptions } from "../https-options";
5
6
  import { mockConsoleMethods } from "./helpers/mock-console";
6
7
  import { runInTempDir } from "./helpers/run-in-tmp";
@@ -10,15 +11,15 @@ describe("getHttpsOptions()", () => {
10
11
  const std = mockConsoleMethods();
11
12
 
12
13
  it("should use cached values if they have not expired", async () => {
13
- fs.mkdirSync(resolve(os.homedir(), ".wrangler/local-cert"), {
14
+ fs.mkdirSync(path.resolve(getGlobalWranglerConfigPath(), "local-cert"), {
14
15
  recursive: true,
15
16
  });
16
17
  fs.writeFileSync(
17
- resolve(os.homedir(), ".wrangler/local-cert/key.pem"),
18
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/key.pem"),
18
19
  "PRIVATE KEY"
19
20
  );
20
21
  fs.writeFileSync(
21
- resolve(os.homedir(), ".wrangler/local-cert/cert.pem"),
22
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/cert.pem"),
22
23
  "PUBLIC KEY"
23
24
  );
24
25
  const result = await getHttpsOptions();
@@ -32,11 +33,11 @@ describe("getHttpsOptions()", () => {
32
33
  it("should generate and cache new keys if none are cached", async () => {
33
34
  const result = await getHttpsOptions();
34
35
  const key = fs.readFileSync(
35
- resolve(os.homedir(), ".wrangler/local-cert/key.pem"),
36
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/key.pem"),
36
37
  "utf8"
37
38
  );
38
39
  const cert = fs.readFileSync(
39
- resolve(os.homedir(), ".wrangler/local-cert/cert.pem"),
40
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/cert.pem"),
40
41
  "utf8"
41
42
  );
42
43
  expect(result.key).toEqual(key);
@@ -49,28 +50,28 @@ describe("getHttpsOptions()", () => {
49
50
  });
50
51
 
51
52
  it("should generate and cache new keys if cached files have expired", async () => {
52
- fs.mkdirSync(resolve(os.homedir(), ".wrangler/local-cert"), {
53
+ fs.mkdirSync(path.resolve(getGlobalWranglerConfigPath(), "local-cert"), {
53
54
  recursive: true,
54
55
  });
55
56
  const ORIGINAL_KEY = "EXPIRED PRIVATE KEY";
56
57
  const ORIGINAL_CERT = "EXPIRED PUBLIC KEY";
57
58
  fs.writeFileSync(
58
- resolve(os.homedir(), ".wrangler/local-cert/key.pem"),
59
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/key.pem"),
59
60
  ORIGINAL_KEY
60
61
  );
61
62
  fs.writeFileSync(
62
- resolve(os.homedir(), ".wrangler/local-cert/cert.pem"),
63
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/cert.pem"),
63
64
  ORIGINAL_CERT
64
65
  );
65
66
  mockStatSync(/\.pem$/, { mtimeMs: new Date(2000).valueOf() });
66
67
 
67
68
  const result = await getHttpsOptions();
68
69
  const key = fs.readFileSync(
69
- resolve(os.homedir(), ".wrangler/local-cert/key.pem"),
70
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/key.pem"),
70
71
  "utf8"
71
72
  );
72
73
  const cert = fs.readFileSync(
73
- resolve(os.homedir(), ".wrangler/local-cert/cert.pem"),
74
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/cert.pem"),
74
75
  "utf8"
75
76
  );
76
77
  expect(key).not.toEqual(ORIGINAL_KEY);
@@ -84,25 +85,57 @@ describe("getHttpsOptions()", () => {
84
85
  expect(std.err).toMatchInlineSnapshot(`""`);
85
86
  });
86
87
 
88
+ it("should warn if not able to write to the cache (legacy config path)", async () => {
89
+ fs.mkdirSync(path.join(os.homedir(), ".wrangler"));
90
+ mockWriteFileSyncThrow(/\.pem$/);
91
+ await getHttpsOptions();
92
+ expect(
93
+ fs.existsSync(
94
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/key.pem")
95
+ )
96
+ ).toBe(false);
97
+ expect(
98
+ fs.existsSync(
99
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/cert.pem")
100
+ )
101
+ ).toBe(false);
102
+ expect(std.out).toMatchInlineSnapshot(
103
+ `"Generating new self-signed certificate..."`
104
+ );
105
+ expect(std.warn).toMatchInlineSnapshot(`
106
+ "▲ [WARNING] Unable to cache generated self-signed certificate in home/.wrangler/local-cert.
107
+
108
+ ERROR: Cannot write file
109
+
110
+ "
111
+ `);
112
+ expect(std.err).toMatchInlineSnapshot(`""`);
113
+ fs.rmSync(path.join(os.homedir(), ".wrangler"), { recursive: true });
114
+ });
115
+
87
116
  it("should warn if not able to write to the cache", async () => {
88
117
  mockWriteFileSyncThrow(/\.pem$/);
89
118
  await getHttpsOptions();
90
119
  expect(
91
- fs.existsSync(resolve(os.homedir(), ".wrangler/local-cert/key.pem"))
120
+ fs.existsSync(
121
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/key.pem")
122
+ )
92
123
  ).toBe(false);
93
124
  expect(
94
- fs.existsSync(resolve(os.homedir(), ".wrangler/local-cert/cert.pem"))
125
+ fs.existsSync(
126
+ path.resolve(getGlobalWranglerConfigPath(), "local-cert/cert.pem")
127
+ )
95
128
  ).toBe(false);
96
129
  expect(std.out).toMatchInlineSnapshot(
97
130
  `"Generating new self-signed certificate..."`
98
131
  );
99
132
  expect(std.warn).toMatchInlineSnapshot(`
100
- "▲ [WARNING] Unable to cache generated self-signed certificate in home/.wrangler/local-cert.
133
+ "▲ [WARNING] Unable to cache generated self-signed certificate in test-xdg-config/local-cert.
101
134
 
102
- ERROR: Cannot write file
135
+ ERROR: Cannot write file
103
136
 
104
- "
105
- `);
137
+ "
138
+ `);
106
139
  expect(std.err).toMatchInlineSnapshot(`""`);
107
140
  });
108
141
  });
@@ -105,3 +105,16 @@ jest.mock("../user/generate-random-state", () => {
105
105
  generateRandomState: jest.fn().mockImplementation(() => "MOCK_STATE_PARAM"),
106
106
  };
107
107
  });
108
+
109
+ jest.mock("xdg-app-paths", () => {
110
+ return {
111
+ __esModule: true,
112
+ default: jest.fn().mockImplementation(() => {
113
+ return {
114
+ config() {
115
+ return jest.requireActual("node:path").resolve("test-xdg-config");
116
+ },
117
+ };
118
+ }),
119
+ };
120
+ });