wrangler 0.0.13 → 0.0.17

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 (67) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +20 -11
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +112 -28
  5. package/pages/functions/filepath-routing.ts +44 -51
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +3 -9
  8. package/src/__tests__/dev.test.tsx +42 -5
  9. package/src/__tests__/guess-worker-format.test.ts +66 -0
  10. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  11. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  12. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +36 -0
  15. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +43 -9
  16. package/src/__tests__/helpers/mock-console.ts +62 -0
  17. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/mock-web-socket.ts +37 -0
  21. package/src/__tests__/{run-in-tmp.ts → helpers/run-in-tmp.ts} +1 -1
  22. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  23. package/src/__tests__/helpers/write-wrangler-toml.ts +20 -0
  24. package/src/__tests__/index.test.ts +418 -71
  25. package/src/__tests__/jest.setup.ts +30 -2
  26. package/src/__tests__/kv.test.ts +147 -252
  27. package/src/__tests__/logout.test.ts +50 -0
  28. package/src/__tests__/package-manager.test.ts +206 -0
  29. package/src/__tests__/publish.test.ts +1136 -291
  30. package/src/__tests__/r2.test.ts +206 -0
  31. package/src/__tests__/secret.test.ts +210 -0
  32. package/src/__tests__/sentry.test.ts +146 -0
  33. package/src/__tests__/tail.test.ts +246 -0
  34. package/src/__tests__/whoami.test.tsx +6 -47
  35. package/src/api/form_data.ts +75 -25
  36. package/src/api/preview.ts +2 -2
  37. package/src/api/worker.ts +34 -15
  38. package/src/bundle.ts +127 -0
  39. package/src/cfetch/index.ts +7 -15
  40. package/src/cfetch/internal.ts +41 -6
  41. package/src/cli.ts +10 -0
  42. package/src/config.ts +125 -95
  43. package/src/dev.tsx +300 -193
  44. package/src/dialogs.tsx +2 -2
  45. package/src/guess-worker-format.ts +68 -0
  46. package/src/index.tsx +578 -192
  47. package/src/inspect.ts +29 -10
  48. package/src/kv.tsx +23 -17
  49. package/src/module-collection.ts +32 -12
  50. package/src/open-in-browser.ts +13 -0
  51. package/src/package-manager.ts +120 -0
  52. package/src/pages.tsx +28 -23
  53. package/src/paths.ts +26 -0
  54. package/src/proxy.ts +88 -14
  55. package/src/publish.ts +260 -297
  56. package/src/r2.ts +50 -0
  57. package/src/reporting.ts +115 -0
  58. package/src/sites.tsx +28 -27
  59. package/src/tail.tsx +178 -9
  60. package/src/user.tsx +58 -44
  61. package/templates/new-worker.js +15 -0
  62. package/templates/new-worker.ts +15 -0
  63. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  64. package/wrangler-dist/cli.js +124315 -104677
  65. package/wrangler-dist/cli.js.map +3 -3
  66. package/src/__tests__/mock-console.ts +0 -34
  67. package/src/__tests__/run-wrangler.ts +0 -8
package/bin/wrangler.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- const { spawn } = require("child_process");
3
- const { join } = require("path");
2
+ const { spawn } = require("node:child_process");
3
+ const { join } = require("node:path");
4
4
  const semiver = require("semiver");
5
5
 
6
6
  const MIN_NODE_VERSION = "16.7.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wrangler",
3
- "version": "0.0.13",
3
+ "version": "0.0.17",
4
4
  "author": "wrangler@cloudflare.com",
5
5
  "description": "Command-line interface for all things Cloudflare Workers",
6
6
  "bin": {
@@ -36,11 +36,11 @@
36
36
  "cli"
37
37
  ],
38
38
  "dependencies": {
39
- "esbuild": "0.14.1",
40
- "miniflare": "2.2.0",
39
+ "esbuild": "0.14.14",
40
+ "miniflare": "2.3.0",
41
41
  "path-to-regexp": "^6.2.0",
42
42
  "semiver": "^1.1.0",
43
- "xxhash-addon": "^1.4.0"
43
+ "xxhash-wasm": "^1.0.1"
44
44
  },
45
45
  "optionalDependencies": {
46
46
  "fsevents": "~2.3.2"
@@ -48,8 +48,13 @@
48
48
  "devDependencies": {
49
49
  "@babel/types": "^7.16.0",
50
50
  "@iarna/toml": "^2.2.5",
51
+ "@sentry/cli": "^1.71.0",
52
+ "@sentry/integrations": "^6.17.6",
53
+ "@sentry/node": "^6.17.6",
54
+ "@types/command-exists": "^1.2.0",
51
55
  "@types/estree": "^0.0.50",
52
56
  "@types/mime": "^2.0.3",
57
+ "@types/prompts": "^2.0.14",
53
58
  "@types/react": "^17.0.37",
54
59
  "@types/serve-static": "^1.13.10",
55
60
  "@types/signal-exit": "^3.0.1",
@@ -59,28 +64,30 @@
59
64
  "acorn-walk": "^8.2.0",
60
65
  "chokidar": "^3.5.2",
61
66
  "clipboardy": "^3.0.0",
67
+ "cmd-shim": "^4.1.0",
62
68
  "command-exists": "^1.2.9",
63
69
  "devtools-protocol": "^0.0.955664",
64
70
  "execa": "^6.0.0",
65
71
  "faye-websocket": "^0.11.4",
66
72
  "finalhandler": "^1.1.2",
67
73
  "find-up": "^6.2.0",
68
- "formdata-node": "^4.3.1",
69
74
  "ignore": "^5.2.0",
70
75
  "ink": "^3.2.0",
71
76
  "ink-select-input": "^4.2.1",
72
77
  "ink-table": "^3.0.0",
73
78
  "ink-testing-library": "^2.1.0",
74
79
  "ink-text-input": "^4.0.2",
80
+ "jest-fetch-mock": "^3.0.3",
81
+ "jest-websocket-mock": "^2.3.0",
75
82
  "mime": "^3.0.0",
76
- "node-fetch": "3.1.1",
77
83
  "open": "^8.4.0",
84
+ "prompts": "^2.4.2",
78
85
  "react": "^17.0.2",
79
86
  "react-error-boundary": "^3.1.4",
80
87
  "serve-static": "^1.14.1",
81
88
  "signal-exit": "^3.0.6",
82
89
  "tmp-promise": "^3.0.3",
83
- "undici": "^4.11.1",
90
+ "undici": "4.13.0",
84
91
  "ws": "^8.3.0",
85
92
  "yargs": "^17.3.0"
86
93
  },
@@ -90,16 +97,18 @@
90
97
  "pages",
91
98
  "miniflare-config-stubs",
92
99
  "wrangler-dist",
93
- "static-asset-facade.js",
100
+ "templates",
94
101
  "vendor",
95
102
  "import_meta_url.js"
96
103
  ],
97
104
  "scripts": {
98
105
  "clean": "rm -rf wrangler-dist",
106
+ "check:type": "tsc",
99
107
  "bundle": "node -r esbuild-register scripts/bundle.ts",
100
108
  "build": "npm run clean && npm run bundle",
109
+ "prepublishOnly": "npm run build",
101
110
  "start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
102
- "test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true",
111
+ "test": "jest --silent=false --verbose=true",
103
112
  "test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
104
113
  },
105
114
  "engines": {
@@ -110,10 +119,10 @@
110
119
  "testTimeout": 30000,
111
120
  "testRegex": ".*.(test|spec)\\.[jt]sx?$",
112
121
  "transformIgnorePatterns": [
113
- "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)"
122
+ "node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)"
114
123
  ],
115
124
  "moduleNameMapper": {
116
- "clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
125
+ "clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
117
126
  },
118
127
  "transform": {
119
128
  "^.+\\.c?(t|j)sx?$": [
@@ -1,4 +1,4 @@
1
- import path from "path";
1
+ import path from "node:path";
2
2
  import { build } from "esbuild";
3
3
 
4
4
  type Options = {
@@ -1,39 +1,123 @@
1
- import { compareRoutes } from "./filepath-routing";
1
+ import { writeFileSync } from "fs";
2
+ import { runInTempDir } from "../../src/__tests__/helpers/run-in-tmp";
3
+ import { toUrlPath } from "../../src/paths";
4
+ import { compareRoutes, generateConfigFromFileTree } from "./filepath-routing";
5
+ import type { UrlPath } from "../../src/paths";
6
+ import type { HTTPMethod, RouteConfig } from "./routes";
2
7
 
3
- describe("compareRoutes()", () => {
4
- test("routes / last", () => {
5
- expect(compareRoutes("/", "/foo")).toBeGreaterThanOrEqual(1);
6
- expect(compareRoutes("/", "/:foo")).toBeGreaterThanOrEqual(1);
7
- expect(compareRoutes("/", "/:foo*")).toBeGreaterThanOrEqual(1);
8
- });
8
+ describe("filepath-routing", () => {
9
+ describe("compareRoutes()", () => {
10
+ test("routes / last", () => {
11
+ expect(
12
+ compareRoutes(routeConfig("/"), routeConfig("/foo"))
13
+ ).toBeGreaterThanOrEqual(1);
14
+ expect(
15
+ compareRoutes(routeConfig("/"), routeConfig("/:foo"))
16
+ ).toBeGreaterThanOrEqual(1);
17
+ expect(
18
+ compareRoutes(routeConfig("/"), routeConfig("/:foo*"))
19
+ ).toBeGreaterThanOrEqual(1);
20
+ });
9
21
 
10
- test("routes with fewer segments come after those with more segments", () => {
11
- expect(compareRoutes("/foo", "/foo/bar")).toBeGreaterThanOrEqual(1);
12
- expect(compareRoutes("/foo", "/foo/bar/cat")).toBeGreaterThanOrEqual(1);
13
- });
22
+ test("routes with fewer segments come after those with more segments", () => {
23
+ expect(
24
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar"))
25
+ ).toBeGreaterThanOrEqual(1);
26
+ expect(
27
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat"))
28
+ ).toBeGreaterThanOrEqual(1);
29
+ });
14
30
 
15
- test("routes with wildcard segments come after those without", () => {
16
- expect(compareRoutes("/:foo*", "/foo")).toBe(1);
17
- expect(compareRoutes("/:foo*", "/:foo")).toBe(1);
18
- });
31
+ test("routes with wildcard segments come after those without", () => {
32
+ expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
33
+ expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(
34
+ 1
35
+ );
36
+ });
19
37
 
20
- test("routes with dynamic segments come after those without", () => {
21
- expect(compareRoutes("/:foo", "/foo")).toBe(1);
22
- });
38
+ test("routes with dynamic segments come after those without", () => {
39
+ expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
40
+ });
23
41
 
24
- test("routes with dynamic segments occuring earlier come after those with dynamic segments in later positions", () => {
25
- expect(compareRoutes("/foo/:id/bar", "/foo/bar/:id")).toBe(1);
26
- });
42
+ test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
43
+ expect(
44
+ compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id"))
45
+ ).toBe(1);
46
+ });
27
47
 
28
- test("routes with no HTTP method come after those specifying a method", () => {
29
- expect(compareRoutes("/foo", "GET /foo")).toBe(1);
30
- });
48
+ test("routes with no HTTP method come after those specifying a method", () => {
49
+ expect(
50
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))
51
+ ).toBe(1);
52
+ });
53
+
54
+ test("two equal routes are sorted according to their original position in the list", () => {
55
+ expect(
56
+ compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
57
+ ).toBe(0);
58
+ });
31
59
 
32
- test("two equal routes are sorted according to their original position in the list", () => {
33
- expect(compareRoutes("GET /foo", "GET /foo")).toBe(0);
60
+ test("it returns -1 if the first argument should appear first in the list", () => {
61
+ expect(
62
+ compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))
63
+ ).toBe(-1);
64
+ });
34
65
  });
35
66
 
36
- test("it returns -1 if the first argument should appear first in the list", () => {
37
- expect(compareRoutes("GET /foo", "/foo")).toBe(-1);
67
+ describe("generateConfigFromFileTree", () => {
68
+ runInTempDir();
69
+
70
+ it("should generate a route entry for each file in the tree", async () => {
71
+ writeFileSync(
72
+ "foo.ts",
73
+ `
74
+ export function onRequestGet() {}
75
+ export function onRequestPost() {}
76
+ `
77
+ );
78
+ writeFileSync(
79
+ "bar.ts",
80
+ `
81
+ export function onRequestPut() {}
82
+ export function onRequestDelete() {}
83
+ `
84
+ );
85
+
86
+ const entries = await generateConfigFromFileTree({
87
+ baseDir: ".",
88
+ baseURL: "/base" as UrlPath,
89
+ });
90
+ expect(entries).toEqual({
91
+ routes: [
92
+ {
93
+ method: "PUT",
94
+ module: ["bar.ts:onRequestPut"],
95
+ routePath: "/base/bar",
96
+ },
97
+ {
98
+ method: "DELETE",
99
+ module: ["bar.ts:onRequestDelete"],
100
+ routePath: "/base/bar",
101
+ },
102
+ {
103
+ method: "GET",
104
+ module: ["foo.ts:onRequestGet"],
105
+ routePath: "/base/foo",
106
+ },
107
+ {
108
+ method: "POST",
109
+ module: ["foo.ts:onRequestPost"],
110
+ routePath: "/base/foo",
111
+ },
112
+ ],
113
+ });
114
+ });
38
115
  });
39
116
  });
117
+
118
+ function routeConfig(routePath: string, method?: string): RouteConfig {
119
+ return {
120
+ routePath: toUrlPath(routePath),
121
+ method: method as HTTPMethod,
122
+ };
123
+ }
@@ -1,28 +1,28 @@
1
- import path from "path";
2
- import fs from "fs/promises";
3
- import { transform } from "esbuild";
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
4
3
  import * as acorn from "acorn";
5
4
  import * as acornWalk from "acorn-walk";
6
- import type { Config, RouteConfig } from "./routes";
5
+ import { transform } from "esbuild";
6
+ import { toUrlPath } from "../../src/paths";
7
+ import type { UrlPath } from "../../src/paths";
8
+ import type { HTTPMethod, RouteConfig } from "./routes";
7
9
  import type { ExportNamedDeclaration, Identifier } from "estree";
8
10
 
9
- type Arguments = {
10
- baseDir: string;
11
- baseURL: string;
12
- };
13
-
14
11
  export async function generateConfigFromFileTree({
15
12
  baseDir,
16
13
  baseURL,
17
- }: Arguments) {
18
- let routeEntries: [string, RouteConfig][] = [];
14
+ }: {
15
+ baseDir: string;
16
+ baseURL: UrlPath;
17
+ }) {
18
+ let routeEntries: RouteConfig[] = [];
19
19
 
20
20
  if (!baseURL.startsWith("/")) {
21
- baseURL = `/${baseURL}`;
21
+ baseURL = `/${baseURL}` as UrlPath;
22
22
  }
23
23
 
24
24
  if (baseURL.endsWith("/")) {
25
- baseURL = baseURL.slice(0, -1);
25
+ baseURL = baseURL.slice(0, -1) as UrlPath;
26
26
  }
27
27
 
28
28
  await forEachFile(baseDir, async (filepath) => {
@@ -78,10 +78,9 @@ export async function generateConfigFromFileTree({
78
78
  }
79
79
 
80
80
  for (const exportName of exportNames) {
81
- const [match, method] =
82
- exportName.match(
83
- /^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
84
- ) ?? [];
81
+ const [match, method = ""] = (exportName.match(
82
+ /^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
83
+ ) ?? []) as (string | undefined)[];
85
84
 
86
85
  if (match) {
87
86
  const basename = path.basename(filepath).slice(0, -ext.length);
@@ -108,18 +107,15 @@ export async function generateConfigFromFileTree({
108
107
  routePath = routePath.replace(/\[\[(.+)]]/g, ":$1*"); // transform [[id]] => :id*
109
108
  routePath = routePath.replace(/\[(.+)]/g, ":$1"); // transform [id] => :id
110
109
 
111
- if (method) {
112
- routePath = `${method.toUpperCase()} ${routePath}`;
113
- }
110
+ const routeEntry: RouteConfig = {
111
+ routePath: toUrlPath(routePath),
112
+ method: method.toUpperCase() as HTTPMethod,
113
+ [isMiddlewareFile ? "middleware" : "module"]: [
114
+ `${path.relative(baseDir, filepath)}:${exportName}`,
115
+ ],
116
+ };
114
117
 
115
- routeEntries.push([
116
- routePath,
117
- {
118
- [isMiddlewareFile ? "middleware" : "module"]: [
119
- `${path.relative(baseDir, filepath)}:${exportName}`,
120
- ],
121
- },
122
- ]);
118
+ routeEntries.push(routeEntry);
123
119
  }
124
120
  }
125
121
  },
@@ -129,45 +125,42 @@ export async function generateConfigFromFileTree({
129
125
 
130
126
  // Combine together any routes (index routes) which contain both a module and a middleware
131
127
  routeEntries = routeEntries.reduce(
132
- (acc: typeof routeEntries, [routePath, routeHandler]) => {
128
+ (acc: typeof routeEntries, { routePath, ...rest }) => {
133
129
  const existingRouteEntry = acc.find(
134
- (routeEntry) => routeEntry[0] === routePath
130
+ (routeEntry) =>
131
+ routeEntry.routePath === routePath &&
132
+ routeEntry.method === rest.method
135
133
  );
136
134
  if (existingRouteEntry !== undefined) {
137
- existingRouteEntry[1] = {
138
- ...existingRouteEntry[1],
139
- ...routeHandler,
140
- };
135
+ Object.assign(existingRouteEntry, rest);
141
136
  } else {
142
- acc.push([routePath, routeHandler]);
137
+ acc.push({ routePath, ...rest });
143
138
  }
144
139
  return acc;
145
140
  },
146
141
  []
147
142
  );
148
143
 
149
- routeEntries.sort(([pathA], [pathB]) => compareRoutes(pathA, pathB));
144
+ routeEntries.sort((a, b) => compareRoutes(a, b));
150
145
 
151
- return { routes: Object.fromEntries(routeEntries) } as Config;
146
+ return {
147
+ routes: routeEntries,
148
+ };
152
149
  }
153
150
 
154
151
  // Ensure routes are produced in order of precedence so that
155
152
  // more specific routes aren't occluded from matching due to
156
153
  // less specific routes appearing first in the route list.
157
- export function compareRoutes(a: string, b: string) {
158
- function parseRoutePath(routePath: string): [string | null, string[]] {
159
- const parts = routePath.split(" ", 2);
160
- // split() will guarantee at least one element.
161
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
162
- const segmentedPath = parts.pop()!;
163
- const method = parts.pop() ?? null;
164
-
165
- const segments = segmentedPath.slice(1).split("/").filter(Boolean);
166
- return [method, segments];
154
+ export function compareRoutes(
155
+ { routePath: routePathA, method: methodA }: RouteConfig,
156
+ { routePath: routePathB, method: methodB }: RouteConfig
157
+ ) {
158
+ function parseRoutePath(routePath: UrlPath): string[] {
159
+ return routePath.slice(1).split("/").filter(Boolean);
167
160
  }
168
161
 
169
- const [methodA, segmentsA] = parseRoutePath(a);
170
- const [methodB, segmentsB] = parseRoutePath(b);
162
+ const segmentsA = parseRoutePath(routePathA);
163
+ const segmentsB = parseRoutePath(routePathB);
171
164
 
172
165
  // sort routes with fewer segments after those with more segments
173
166
  if (segmentsA.length !== segmentsB.length) {
@@ -193,8 +186,8 @@ export function compareRoutes(a: string, b: string) {
193
186
  if (methodA && !methodB) return -1;
194
187
  if (!methodA && methodB) return 1;
195
188
 
196
- // all else equal, just sort them lexicographically
197
- return a.localeCompare(b);
189
+ // all else equal, just sort the paths lexicographically
190
+ return routePathA.localeCompare(routePathB);
198
191
  }
199
192
 
200
193
  async function forEachFile<T>(
@@ -1,6 +1,7 @@
1
- import path from "path";
2
- import fs from "fs/promises";
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
3
  import { isValidIdentifier, normalizeIdentifier } from "./identifiers";
4
+ import type { UrlPath } from "../../src/paths";
4
5
 
5
6
  export const HTTP_METHODS = [
6
7
  "HEAD",
@@ -19,22 +20,20 @@ export function isHTTPMethod(
19
20
  }
20
21
 
21
22
  export type RoutesCollection = Array<{
22
- routePath: string;
23
- methods: HTTPMethod[];
23
+ routePath: UrlPath;
24
+ method?: HTTPMethod;
24
25
  modules: string[];
25
26
  middlewares: string[];
26
27
  }>;
27
28
 
28
29
  export type Config = {
29
- routes?: RoutesConfig;
30
+ routes?: RouteConfig[];
30
31
  schedules?: unknown;
31
32
  };
32
33
 
33
- export type RoutesConfig = {
34
- [route: string]: RouteConfig;
35
- };
36
-
37
34
  export type RouteConfig = {
35
+ routePath: UrlPath;
36
+ method?: HTTPMethod;
38
37
  middleware?: string | string[];
39
38
  module?: string | string[];
40
39
  };
@@ -114,16 +113,10 @@ export function parseConfig(config: Config, baseDir: string) {
114
113
  });
115
114
  }
116
115
 
117
- for (const [route, props] of Object.entries(config.routes ?? {})) {
118
- let [_methods, routePath] = route.split(" ");
119
- if (!routePath) {
120
- routePath = _methods;
121
- _methods = "";
122
- }
123
-
116
+ for (const { routePath, method, ...props } of config.routes ?? []) {
124
117
  routes.push({
125
118
  routePath,
126
- methods: _methods.split("|").filter(isHTTPMethod),
119
+ method,
127
120
  middlewares: parseModuleIdentifiers(props.middleware),
128
121
  modules: parseModuleIdentifiers(props.module),
129
122
  });
@@ -148,7 +141,7 @@ export const routes = [
148
141
  .map(
149
142
  (route) => ` {
150
143
  routePath: "${route.routePath}",
151
- methods: ${JSON.stringify(route.methods)},
144
+ method: "${route.method}",
152
145
  middlewares: [${route.middlewares.join(", ")}],
153
146
  modules: [${route.modules.join(", ")}],
154
147
  },`
@@ -22,7 +22,7 @@ declare type PagesFunction<
22
22
 
23
23
  type RouteHandler = {
24
24
  routePath: string;
25
- methods: HTTPMethod[];
25
+ method?: HTTPMethod;
26
26
  modules: PagesFunction[];
27
27
  middlewares: PagesFunction[];
28
28
  };
@@ -47,10 +47,7 @@ function* executeRequest(request: Request, _env: FetchEnv) {
47
47
 
48
48
  // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
49
49
  for (const route of [...routes].reverse()) {
50
- if (
51
- route.methods.length &&
52
- !route.methods.includes(request.method as HTTPMethod)
53
- ) {
50
+ if (route.method && route.method !== request.method) {
54
51
  continue;
55
52
  }
56
53
 
@@ -68,10 +65,7 @@ function* executeRequest(request: Request, _env: FetchEnv) {
68
65
 
69
66
  // Then look for the first exact route match and execute its "modules"
70
67
  for (const route of routes) {
71
- if (
72
- route.methods.length &&
73
- !route.methods.includes(request.method as HTTPMethod)
74
- ) {
68
+ if (route.method && route.method !== request.method) {
75
69
  continue;
76
70
  }
77
71
 
@@ -2,14 +2,20 @@ import { render } from "ink-testing-library";
2
2
  import patchConsole from "patch-console";
3
3
  import React from "react";
4
4
  import Dev from "../dev";
5
+ import { mockConsoleMethods } from "./helpers/mock-console";
6
+ import { runWrangler } from "./helpers/run-wrangler";
7
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
5
8
  import type { DevProps } from "../dev";
6
9
 
7
10
  describe("Dev component", () => {
8
- let restoreConsole;
11
+ let restoreConsole: ReturnType<typeof patchConsole>;
9
12
  beforeEach(() => (restoreConsole = patchConsole(() => {})));
10
13
  afterEach(() => restoreConsole());
14
+ const std = mockConsoleMethods();
11
15
 
12
- it("should throw if format is service-worker and there is a public directory", () => {
16
+ // This test needs to be rewritten because the error now throws asynchronously
17
+ // and the Ink framework does not yet have async testing support.
18
+ it.skip("should throw if format is service-worker and there is a public directory", () => {
13
19
  const { lastFrame } = renderDev({
14
20
  format: "service-worker",
15
21
  accountId: "some-account-id",
@@ -21,6 +27,26 @@ describe("Dev component", () => {
21
27
  Error: You cannot use the service worker format with a \`public\` directory."
22
28
  `);
23
29
  });
30
+
31
+ describe("entry-points", () => {
32
+ it("should error if there is no entry-point specified", async () => {
33
+ writeWranglerToml();
34
+
35
+ await expect(
36
+ runWrangler("dev")
37
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
38
+ `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`build.upload.main\` config field."`
39
+ );
40
+
41
+ expect(std.out).toMatchInlineSnapshot(`""`);
42
+ expect(std.err).toMatchInlineSnapshot(`
43
+ "Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`build.upload.main\` config field.
44
+
45
+ %s
46
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
47
+ `);
48
+ });
49
+ });
24
50
  });
25
51
 
26
52
  /**
@@ -30,25 +56,35 @@ describe("Dev component", () => {
30
56
  */
31
57
  function renderDev({
32
58
  name,
33
- entry = "some/entry.ts",
59
+ entry = { file: "some/entry.ts", directory: process.cwd() },
34
60
  port,
35
61
  format,
36
62
  accountId,
37
- initialMode = "remote",
63
+ initialMode = "local",
38
64
  jsxFactory,
39
65
  jsxFragment,
40
- bindings = {},
66
+ bindings = {
67
+ kv_namespaces: [],
68
+ vars: {},
69
+ durable_objects: { bindings: [] },
70
+ r2_buckets: [],
71
+ wasm_modules: {},
72
+ unsafe: [],
73
+ },
41
74
  public: publicDir,
42
75
  assetPaths,
43
76
  compatibilityDate,
44
77
  compatibilityFlags,
45
78
  usageModel,
46
79
  buildCommand = {},
80
+ enableLocalPersistence = false,
81
+ env = undefined,
47
82
  }: Partial<DevProps>) {
48
83
  return render(
49
84
  <Dev
50
85
  name={name}
51
86
  entry={entry}
87
+ env={env}
52
88
  port={port}
53
89
  buildCommand={buildCommand}
54
90
  format={format}
@@ -62,6 +98,7 @@ function renderDev({
62
98
  compatibilityFlags={compatibilityFlags}
63
99
  usageModel={usageModel}
64
100
  bindings={bindings}
101
+ enableLocalPersistence={enableLocalPersistence}
65
102
  />
66
103
  );
67
104
  }