wrangler 0.0.0-e95bea2 → 0.0.0-e95e5a0

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 (93) hide show
  1. package/README.md +25 -14
  2. package/bin/wrangler.js +13 -1
  3. package/miniflare-dist/index.mjs +4886 -0
  4. package/package.json +33 -25
  5. package/pages/functions/buildPlugin.ts +92 -0
  6. package/pages/functions/buildWorker.ts +80 -4
  7. package/pages/functions/filepath-routing.test.ts +136 -25
  8. package/pages/functions/filepath-routing.ts +14 -3
  9. package/pages/functions/routes.ts +6 -1
  10. package/pages/functions/template-plugin.ts +146 -0
  11. package/pages/functions/template-worker.ts +21 -8
  12. package/src/__tests__/config-cache.test.ts +36 -0
  13. package/src/__tests__/configuration.test.ts +1179 -271
  14. package/src/__tests__/dev.test.tsx +382 -25
  15. package/src/__tests__/guess-worker-format.test.ts +28 -1
  16. package/src/__tests__/helpers/mock-account-id.ts +4 -0
  17. package/src/__tests__/helpers/mock-cfetch.ts +5 -1
  18. package/src/__tests__/helpers/mock-console.ts +20 -3
  19. package/src/__tests__/helpers/mock-dialogs.ts +26 -1
  20. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  21. package/src/__tests__/helpers/mock-istty.ts +27 -0
  22. package/src/__tests__/helpers/mock-kv.ts +9 -16
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  24. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  25. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  26. package/src/__tests__/https-options.test.ts +5 -2
  27. package/src/__tests__/index.test.ts +27 -769
  28. package/src/__tests__/init.test.ts +1516 -0
  29. package/src/__tests__/jest.setup.ts +4 -4
  30. package/src/__tests__/kv.test.ts +172 -126
  31. package/src/__tests__/logger.test.ts +115 -0
  32. package/src/__tests__/pages.test.ts +311 -26
  33. package/src/__tests__/publish.test.ts +2946 -524
  34. package/src/__tests__/r2.test.ts +26 -10
  35. package/src/__tests__/route.test.ts +4 -4
  36. package/src/__tests__/secret.test.ts +55 -34
  37. package/src/__tests__/tail.test.ts +146 -20
  38. package/src/__tests__/user.test.ts +131 -0
  39. package/src/__tests__/whoami.test.tsx +26 -7
  40. package/src/bundle.ts +30 -3
  41. package/src/cfetch/internal.ts +9 -7
  42. package/src/cli.ts +5 -11
  43. package/src/config/README.md +7 -7
  44. package/src/config/config.ts +33 -78
  45. package/src/config/environment.ts +93 -4
  46. package/src/config/index.ts +15 -4
  47. package/src/config/validation-helpers.ts +202 -15
  48. package/src/config/validation.ts +530 -133
  49. package/src/config-cache.ts +45 -0
  50. package/src/create-worker-preview.ts +22 -13
  51. package/src/create-worker-upload-form.ts +32 -9
  52. package/src/dev/dev-vars.ts +36 -0
  53. package/src/dev/dev.tsx +136 -75
  54. package/src/dev/local.tsx +196 -125
  55. package/src/dev/remote.tsx +25 -13
  56. package/src/dev/use-esbuild.ts +23 -5
  57. package/src/dialogs.tsx +16 -6
  58. package/src/entry.ts +119 -11
  59. package/src/environment-variables.ts +3 -1
  60. package/src/errors.ts +11 -0
  61. package/src/https-options.ts +3 -2
  62. package/src/index.tsx +739 -400
  63. package/src/inspect.ts +17 -11
  64. package/src/intl-polyfill.d.ts +139 -0
  65. package/src/{kv/index.tsx → kv.ts} +8 -28
  66. package/src/logger.ts +66 -0
  67. package/src/miniflare-cli/README.md +30 -0
  68. package/src/miniflare-cli/enum-keys.ts +17 -0
  69. package/src/miniflare-cli/index.ts +41 -0
  70. package/src/miniflare-cli/request-context.ts +40 -0
  71. package/src/module-collection.ts +63 -58
  72. package/src/open-in-browser.ts +5 -4
  73. package/src/package-manager.ts +9 -8
  74. package/src/pages.tsx +883 -62
  75. package/src/parse.ts +1 -1
  76. package/src/proxy.ts +32 -21
  77. package/src/publish.ts +276 -161
  78. package/src/sites.tsx +88 -33
  79. package/src/tail/index.ts +2 -2
  80. package/src/tail/printing.ts +13 -8
  81. package/src/update-check.ts +19 -0
  82. package/src/user.tsx +231 -103
  83. package/src/whoami.tsx +2 -1
  84. package/src/worker.ts +15 -6
  85. package/templates/gitignore +171 -0
  86. package/templates/static-asset-facade.js +1 -1
  87. package/wrangler-dist/cli.js +22087 -49340
  88. package/src/__tests__/dev2.test.tsx +0 -152
  89. package/src/__tests__/helpers/mock-user.ts +0 -27
  90. package/src/__tests__/logout.test.ts +0 -50
  91. package/src/__tests__/sentry.test.ts +0 -146
  92. package/src/reporting.ts +0 -131
  93. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,115 @@
1
+ import { logger } from "../logger";
2
+ import { mockConsoleMethods } from "./helpers/mock-console";
3
+
4
+ describe("logger", () => {
5
+ const std = mockConsoleMethods();
6
+
7
+ it("should add colored markers to error and warning messages", () => {
8
+ logger.loggerLevel = "debug";
9
+ logger.debug("This is a debug message");
10
+ logger.log("This is a log message");
11
+ logger.warn("This is a warn message");
12
+ logger.error("This is a error message");
13
+
14
+ expect(std.debug).toMatchInlineSnapshot(`"This is a debug message"`);
15
+ expect(std.out).toMatchInlineSnapshot(`"This is a log message"`);
16
+ expect(std.warn).toMatchInlineSnapshot(`
17
+ "▲ [WARNING] This is a warn message
18
+
19
+ "
20
+ `);
21
+ expect(std.err).toMatchInlineSnapshot(`
22
+ "X [ERROR] This is a error message
23
+
24
+ "
25
+ `);
26
+ });
27
+
28
+ describe("loggerLevel=debug", () => {
29
+ it("should render messages that are at or above the log level set in the logger", () => {
30
+ logger.loggerLevel = "debug";
31
+ logger.debug("This is a debug message");
32
+ logger.log("This is a log message");
33
+ logger.warn("This is a warn message");
34
+ logger.error("This is a error message");
35
+
36
+ expect(std.debug).toMatchInlineSnapshot(`"This is a debug message"`);
37
+ expect(std.out).toMatchInlineSnapshot(`"This is a log message"`);
38
+ expect(std.warn).toMatchInlineSnapshot(`
39
+ "▲ [WARNING] This is a warn message
40
+
41
+ "
42
+ `);
43
+ expect(std.err).toMatchInlineSnapshot(`
44
+ "X [ERROR] This is a error message
45
+
46
+ "
47
+ `);
48
+ });
49
+ });
50
+
51
+ describe("loggerLevel=log", () => {
52
+ it("should render messages that are at or above the log level set in the logger", () => {
53
+ logger.loggerLevel = "log";
54
+ logger.debug("This is a debug message");
55
+ logger.log("This is a log message");
56
+ logger.warn("This is a warn message");
57
+ logger.error("This is a error message");
58
+
59
+ expect(std.debug).toMatchInlineSnapshot(`""`);
60
+ expect(std.out).toMatchInlineSnapshot(`"This is a log message"`);
61
+ expect(std.warn).toMatchInlineSnapshot(`
62
+ "▲ [WARNING] This is a warn message
63
+
64
+ "
65
+ `);
66
+ expect(std.err).toMatchInlineSnapshot(`
67
+ "X [ERROR] This is a error message
68
+
69
+ "
70
+ `);
71
+ });
72
+ });
73
+
74
+ describe("loggerLevel=warn", () => {
75
+ it("should render messages that are at or above the log level set in the logger", () => {
76
+ logger.loggerLevel = "warn";
77
+ logger.debug("This is a debug message");
78
+ logger.log("This is a log message");
79
+ logger.warn("This is a warn message");
80
+ logger.error("This is a error message");
81
+
82
+ expect(std.debug).toMatchInlineSnapshot(`""`);
83
+ expect(std.out).toMatchInlineSnapshot(`""`);
84
+ expect(std.warn).toMatchInlineSnapshot(`
85
+ "▲ [WARNING] This is a warn message
86
+
87
+ "
88
+ `);
89
+ expect(std.err).toMatchInlineSnapshot(`
90
+ "X [ERROR] This is a error message
91
+
92
+ "
93
+ `);
94
+ });
95
+ });
96
+
97
+ describe("loggerLevel=error", () => {
98
+ it("should render messages that are at or above the log level set in the logger", () => {
99
+ logger.loggerLevel = "error";
100
+ logger.debug("This is a debug message");
101
+ logger.log("This is a log message");
102
+ logger.warn("This is a warn message");
103
+ logger.error("This is a error message");
104
+
105
+ expect(std.debug).toMatchInlineSnapshot(`""`);
106
+ expect(std.out).toMatchInlineSnapshot(`""`);
107
+ expect(std.warn).toMatchInlineSnapshot(`""`);
108
+ expect(std.err).toMatchInlineSnapshot(`
109
+ "X [ERROR] This is a error message
110
+
111
+ "
112
+ `);
113
+ });
114
+ });
115
+ });
@@ -1,44 +1,329 @@
1
- import { getPackageManager } from "../package-manager";
1
+ import { writeFileSync } from "node:fs";
2
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
3
+ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
2
4
  import { mockConsoleMethods } from "./helpers/mock-console";
3
5
  import { runInTempDir } from "./helpers/run-in-tmp";
4
6
  import { runWrangler } from "./helpers/run-wrangler";
5
- import type { PackageManager } from "../package-manager";
7
+ import type { Project, Deployment } from "../pages";
8
+ import type { File, FormData } from "undici";
6
9
 
7
- describe("subcommand implicit help ran on imcomplete command execution", () => {
8
- let mockPackageManager: PackageManager;
10
+ describe("pages", () => {
9
11
  runInTempDir();
10
-
11
- beforeEach(() => {
12
- mockPackageManager = {
13
- cwd: process.cwd(),
14
- // @ts-expect-error we're making a fake package manager here
15
- type: "mockpm",
16
- addDevDeps: jest.fn(),
17
- install: jest.fn(),
18
- };
19
- (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
20
- });
21
-
22
12
  const std = mockConsoleMethods();
23
13
  function endEventLoop() {
24
14
  return new Promise((resolve) => setImmediate(resolve));
25
15
  }
26
- it("no subcommand for 'pages' should display a list of available subcommands", async () => {
16
+
17
+ it("should should display a list of available subcommands, for pages with no subcommand", async () => {
27
18
  await runWrangler("pages");
28
19
  await endEventLoop();
20
+
29
21
  expect(std.out).toMatchInlineSnapshot(`
30
- "wrangler pages
22
+ "wrangler pages
23
+
24
+ ⚡️ Configure Cloudflare Pages
31
25
 
32
- ⚡️ Configure Cloudflare Pages
26
+ Commands:
27
+ wrangler pages dev [directory] [-- command] 🧑‍💻 Develop your full-stack Pages application locally
28
+ wrangler pages project ⚡️ Interact with your Pages projects
29
+ wrangler pages deployment 🚀 Interact with the deployments of a project
30
+ wrangler pages publish [directory] 🆙 Publish a directory of static assets as a Pages deployment
33
31
 
34
- Commands:
35
- wrangler pages dev [directory] [-- command] 🧑‍💻 Develop your full-stack Pages application locally
32
+ Flags:
33
+ -c, --config Path to .toml configuration file [string]
34
+ -h, --help Show help [boolean]
35
+ -v, --version Show version number [boolean]
36
+ --legacy-env Use legacy environments [boolean]
36
37
 
37
- Flags:
38
- -c, --config Path to .toml configuration file [string]
39
- -h, --help Show help [boolean]
40
- -v, --version Show version number [boolean]
41
- --legacy-env Use legacy environments [boolean]"
38
+ 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
42
39
  `);
43
40
  });
41
+
42
+ describe("beta message for subcommands", () => {
43
+ it("should display for pages:dev", async () => {
44
+ await expect(
45
+ runWrangler("pages dev")
46
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
47
+ `"Must specify a directory of static assets to serve or a command to run."`
48
+ );
49
+
50
+ expect(std.out).toMatchInlineSnapshot(`
51
+ "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
52
+
53
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
54
+ `);
55
+ });
56
+
57
+ it("should display for pages:functions:build", async () => {
58
+ await expect(runWrangler("pages functions build")).rejects.toThrowError();
59
+
60
+ expect(std.out).toMatchInlineSnapshot(`
61
+ "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
62
+
63
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
64
+ `);
65
+ });
66
+ });
67
+
68
+ describe("project list", () => {
69
+ mockAccountId();
70
+ mockApiToken();
71
+
72
+ afterEach(() => {
73
+ unsetAllMocks();
74
+ });
75
+
76
+ function mockListRequest(projects: unknown[]) {
77
+ const requests = { count: 0 };
78
+ setMockResponse(
79
+ "/accounts/:accountId/pages/projects",
80
+ ([_url, accountId], init, query) => {
81
+ requests.count++;
82
+ expect(accountId).toEqual("some-account-id");
83
+ expect(query.get("per_page")).toEqual("10");
84
+ expect(query.get("page")).toEqual(`${requests.count}`);
85
+ expect(init).toEqual({});
86
+ const pageSize = Number(query.get("per_page"));
87
+ const page = Number(query.get("page"));
88
+ return projects.slice((page - 1) * pageSize, page * pageSize);
89
+ }
90
+ );
91
+ return requests;
92
+ }
93
+
94
+ it("should make request to list projects", async () => {
95
+ const projects: Project[] = [
96
+ {
97
+ name: "dogs",
98
+ subdomain: "docs.pages.dev",
99
+ domains: ["dogs.pages.dev"],
100
+ source: {
101
+ type: "github",
102
+ },
103
+ latest_deployment: {
104
+ modified_on: "2021-11-17T14:52:26.133835Z",
105
+ },
106
+ created_on: "2021-11-17T14:52:26.133835Z",
107
+ production_branch: "main",
108
+ },
109
+ {
110
+ name: "cats",
111
+ subdomain: "cats.pages.dev",
112
+ domains: ["cats.pages.dev", "kitten.com"],
113
+ latest_deployment: {
114
+ modified_on: "2021-11-17T14:52:26.133835Z",
115
+ },
116
+ created_on: "2021-11-17T14:52:26.133835Z",
117
+ production_branch: "main",
118
+ },
119
+ ];
120
+
121
+ const requests = mockListRequest(projects);
122
+ await runWrangler("pages project list");
123
+
124
+ expect(requests.count).toBe(1);
125
+ });
126
+
127
+ it("should make multiple requests for paginated results", async () => {
128
+ const projects: Project[] = [];
129
+ for (let i = 0; i < 15; i++) {
130
+ projects.push({
131
+ name: "dogs" + i,
132
+ subdomain: i + "dogs.pages.dev",
133
+ domains: [i + "dogs.pages.dev"],
134
+ source: {
135
+ type: "github",
136
+ },
137
+ latest_deployment: {
138
+ modified_on: "2021-11-17T14:52:26.133835Z",
139
+ },
140
+ created_on: "2021-11-17T14:52:26.133835Z",
141
+ production_branch: "main",
142
+ });
143
+ }
144
+ const requests = mockListRequest(projects);
145
+ await runWrangler("pages project list");
146
+ expect(requests.count).toEqual(2);
147
+ });
148
+ });
149
+
150
+ describe("project create", () => {
151
+ mockAccountId();
152
+ mockApiToken();
153
+
154
+ afterEach(() => {
155
+ unsetAllMocks();
156
+ });
157
+
158
+ it("should create a project with a production branch", async () => {
159
+ setMockResponse(
160
+ "/accounts/:accountId/pages/projects",
161
+ ([_url, accountId], init) => {
162
+ expect(accountId).toEqual("some-account-id");
163
+ expect(init.method).toEqual("POST");
164
+ const body = JSON.parse(init.body as string);
165
+ expect(body).toEqual({
166
+ name: "a-new-project",
167
+ production_branch: "main",
168
+ });
169
+ return {
170
+ name: "a-new-project",
171
+ subdomain: "a-new-project.pages.dev",
172
+ production_branch: "main",
173
+ };
174
+ }
175
+ );
176
+ await runWrangler(
177
+ "pages project create a-new-project --production-branch=main"
178
+ );
179
+ expect(std.out).toMatchInlineSnapshot(`
180
+ "✨ Successfully created the 'a-new-project' project. It will be available at https://a-new-project.pages.dev/ once you create your first deployment.
181
+ To deploy a folder of assets, run 'wrangler pages publish [directory]'."
182
+ `);
183
+ });
184
+ });
185
+
186
+ describe("deployment list", () => {
187
+ mockAccountId();
188
+ mockApiToken();
189
+
190
+ afterEach(() => {
191
+ unsetAllMocks();
192
+ });
193
+ function mockListRequest(deployments: unknown[]) {
194
+ const requests = { count: 0 };
195
+ setMockResponse(
196
+ "/accounts/:accountId/pages/projects/:project/deployments",
197
+ ([_url, accountId, project]) => {
198
+ requests.count++;
199
+ expect(project).toEqual("images");
200
+ expect(accountId).toEqual("some-account-id");
201
+ return deployments;
202
+ }
203
+ );
204
+ return requests;
205
+ }
206
+
207
+ it("should make request to list deployments", async () => {
208
+ const deployments: Deployment[] = [
209
+ {
210
+ id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
211
+ url: "https://87bbc8fe.images.pages.dev",
212
+ environment: "preview",
213
+ latest_stage: {
214
+ ended_on: "2021-11-17T14:52:26.133835Z",
215
+ status: "success",
216
+ },
217
+ deployment_trigger: {
218
+ metadata: {
219
+ branch: "main",
220
+ commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
221
+ },
222
+ },
223
+ project_name: "images",
224
+ },
225
+ ];
226
+
227
+ const requests = mockListRequest(deployments);
228
+ await runWrangler("pages deployment list --project-name=images");
229
+
230
+ expect(requests.count).toBe(1);
231
+ });
232
+ });
233
+
234
+ describe("deployment create", () => {
235
+ let actualProcessEnvCI: string | undefined;
236
+
237
+ mockAccountId();
238
+ mockApiToken();
239
+ runInTempDir();
240
+
241
+ beforeEach(() => {
242
+ actualProcessEnvCI = process.env.CI;
243
+ process.env.CI = "true";
244
+ });
245
+
246
+ afterEach(() => {
247
+ unsetAllMocks();
248
+ process.env.CI = actualProcessEnvCI;
249
+ });
250
+
251
+ it("should be aliased with 'wrangler pages publish'", async () => {
252
+ await runWrangler("pages publish --help");
253
+ await endEventLoop();
254
+
255
+ expect(std.out).toMatchInlineSnapshot(`
256
+ "wrangler pages publish [directory]
257
+
258
+ 🆙 Publish a directory of static assets as a Pages deployment
259
+
260
+ Positionals:
261
+ directory The directory of Pages Functions [string] [default: \\"functions\\"]
262
+
263
+ Flags:
264
+ -c, --config Path to .toml configuration file [string]
265
+ -h, --help Show help [boolean]
266
+ -v, --version Show version number [boolean]
267
+ --legacy-env Use legacy environments [boolean]
268
+
269
+ Options:
270
+ --project-name The name of the project you want to list deployments for [string]
271
+ --branch The branch of the project you want to list deployments for [string]
272
+ --commit-hash The branch of the project you want to list deployments for [string]
273
+ --commit-message The branch of the project you want to list deployments for [string]
274
+ --commit-dirty The branch of the project you want to list deployments for [boolean]
275
+
276
+ 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose"
277
+ `);
278
+ });
279
+
280
+ it("should upload a directory of files", async () => {
281
+ writeFileSync("logo.png", "foobar");
282
+
283
+ setMockResponse(
284
+ "/accounts/:accountId/pages/projects/foo/file",
285
+ async ([_url, accountId], init) => {
286
+ expect(accountId).toEqual("some-account-id");
287
+ expect(init.method).toEqual("POST");
288
+ const body = init.body as FormData;
289
+ const logoPNGFile = body.get("file") as File;
290
+ expect(await logoPNGFile.text()).toEqual("foobar");
291
+ expect(logoPNGFile.name).toEqual("logo.png");
292
+
293
+ return {
294
+ id: "2082190357cfd3617ccfe04f340c6247",
295
+ };
296
+ }
297
+ );
298
+
299
+ setMockResponse(
300
+ "/accounts/:accountId/pages/projects/foo/deployments",
301
+ async ([_url, accountId], init) => {
302
+ expect(accountId).toEqual("some-account-id");
303
+ expect(init.method).toEqual("POST");
304
+ const body = init.body as FormData;
305
+ const manifest = JSON.parse(body.get("manifest") as string);
306
+ expect(manifest).toMatchInlineSnapshot(`
307
+ Object {
308
+ "/logo.png": "2082190357cfd3617ccfe04f340c6247",
309
+ }
310
+ `);
311
+
312
+ return {
313
+ url: "https://abcxyz.foo.pages.dev/",
314
+ };
315
+ }
316
+ );
317
+
318
+ await runWrangler("pages publish . --project-name=foo");
319
+
320
+ // TODO: Unmounting somehow loses this output
321
+
322
+ // expect(std.out).toMatchInlineSnapshot(`
323
+ // "✨ Success! Uploaded 1 files (TIMINGS)
324
+
325
+ // ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
326
+ // `);
327
+ });
328
+ });
44
329
  });