wrangler 0.0.0-c05df0f → 0.0.0-c6f5f02

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 (54) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +16 -11
  3. package/pages/functions/buildWorker.ts +1 -1
  4. package/pages/functions/filepath-routing.test.ts +40 -13
  5. package/pages/functions/filepath-routing.ts +51 -53
  6. package/pages/functions/routes.ts +11 -18
  7. package/pages/functions/template-worker.ts +19 -33
  8. package/src/__tests__/dev.test.tsx +7 -3
  9. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  10. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  11. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  12. package/src/__tests__/helpers/mock-bin.ts +33 -0
  13. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +49 -13
  14. package/src/__tests__/helpers/mock-console.ts +56 -0
  15. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  16. package/src/__tests__/helpers/mock-kv.ts +40 -0
  17. package/src/__tests__/helpers/mock-user.ts +27 -0
  18. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  19. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  20. package/src/__tests__/index.test.ts +312 -56
  21. package/src/__tests__/jest.setup.ts +17 -2
  22. package/src/__tests__/kv.test.ts +344 -316
  23. package/src/__tests__/logout.test.ts +50 -0
  24. package/src/__tests__/package-manager.test.ts +206 -0
  25. package/src/__tests__/publish.test.ts +1222 -0
  26. package/src/__tests__/secret.test.ts +210 -0
  27. package/src/__tests__/whoami.test.tsx +86 -0
  28. package/src/api/form_data.ts +10 -7
  29. package/src/api/preview.ts +2 -2
  30. package/src/api/worker.ts +3 -3
  31. package/src/cfetch/index.ts +14 -14
  32. package/src/cfetch/internal.ts +40 -8
  33. package/src/config.ts +162 -8
  34. package/src/dev.tsx +105 -57
  35. package/src/dialogs.tsx +2 -2
  36. package/src/index.tsx +438 -311
  37. package/src/inspect.ts +80 -23
  38. package/src/kv.tsx +9 -1
  39. package/src/module-collection.ts +5 -6
  40. package/src/package-manager.ts +120 -0
  41. package/src/pages.tsx +43 -28
  42. package/src/paths.ts +26 -0
  43. package/src/proxy.ts +78 -10
  44. package/src/publish.ts +98 -52
  45. package/src/sites.tsx +179 -53
  46. package/src/user.tsx +51 -59
  47. package/src/whoami.tsx +69 -0
  48. package/templates/new-worker.js +15 -0
  49. package/templates/new-worker.ts +15 -0
  50. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  51. package/wrangler-dist/cli.js +111242 -109923
  52. package/wrangler-dist/cli.js.map +3 -3
  53. package/src/__tests__/run-in-tmp.ts +0 -19
  54. package/src/__tests__/run-wrangler.ts +0 -32
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.0-c05df0f",
3
+ "version": "0.0.0-c6f5f02",
4
4
  "author": "wrangler@cloudflare.com",
5
5
  "description": "Command-line interface for all things Cloudflare Workers",
6
6
  "bin": {
@@ -36,10 +36,11 @@
36
36
  "cli"
37
37
  ],
38
38
  "dependencies": {
39
- "esbuild": "0.14.1",
40
- "miniflare": "2.1.0",
39
+ "esbuild": "0.14.14",
40
+ "miniflare": "2.2.0",
41
41
  "path-to-regexp": "^6.2.0",
42
- "semiver": "^1.1.0"
42
+ "semiver": "^1.1.0",
43
+ "xxhash-wasm": "^1.0.1"
43
44
  },
44
45
  "optionalDependencies": {
45
46
  "fsevents": "~2.3.2"
@@ -58,27 +59,28 @@
58
59
  "acorn-walk": "^8.2.0",
59
60
  "chokidar": "^3.5.2",
60
61
  "clipboardy": "^3.0.0",
62
+ "cmd-shim": "^4.1.0",
61
63
  "command-exists": "^1.2.9",
62
64
  "devtools-protocol": "^0.0.955664",
63
65
  "execa": "^6.0.0",
64
66
  "faye-websocket": "^0.11.4",
65
67
  "finalhandler": "^1.1.2",
66
68
  "find-up": "^6.2.0",
67
- "formdata-node": "^4.3.1",
69
+ "ignore": "^5.2.0",
68
70
  "ink": "^3.2.0",
69
71
  "ink-select-input": "^4.2.1",
70
72
  "ink-table": "^3.0.0",
71
73
  "ink-testing-library": "^2.1.0",
72
74
  "ink-text-input": "^4.0.2",
75
+ "jest-fetch-mock": "^3.0.3",
73
76
  "mime": "^3.0.0",
74
- "node-fetch": "^3.1.0",
75
77
  "open": "^8.4.0",
76
78
  "react": "^17.0.2",
77
79
  "react-error-boundary": "^3.1.4",
78
80
  "serve-static": "^1.14.1",
79
81
  "signal-exit": "^3.0.6",
80
82
  "tmp-promise": "^3.0.3",
81
- "undici": "^4.11.1",
83
+ "undici": "4.13.0",
82
84
  "ws": "^8.3.0",
83
85
  "yargs": "^17.3.0"
84
86
  },
@@ -88,28 +90,31 @@
88
90
  "pages",
89
91
  "miniflare-config-stubs",
90
92
  "wrangler-dist",
91
- "static-asset-facade.js",
93
+ "templates",
92
94
  "vendor",
93
95
  "import_meta_url.js"
94
96
  ],
95
97
  "scripts": {
96
98
  "clean": "rm -rf wrangler-dist",
99
+ "check:type": "tsc",
97
100
  "bundle": "node -r esbuild-register scripts/bundle.ts",
98
101
  "build": "npm run clean && npm run bundle",
99
102
  "start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
100
- "test": "CF_API_TOKEN=some-api-token CF_ACCOUNT_ID=some-account-id jest --silent=false --verbose=true"
103
+ "test": "jest --silent=false --verbose=true",
104
+ "test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
101
105
  },
102
106
  "engines": {
103
107
  "node": ">=16.7.0"
104
108
  },
105
109
  "jest": {
106
110
  "restoreMocks": true,
111
+ "testTimeout": 30000,
107
112
  "testRegex": ".*.(test|spec)\\.[jt]sx?$",
108
113
  "transformIgnorePatterns": [
109
- "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)"
114
+ "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)"
110
115
  ],
111
116
  "moduleNameMapper": {
112
- "clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
117
+ "clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
113
118
  },
114
119
  "transform": {
115
120
  "^.+\\.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,66 @@
1
+ import { toUrlPath } from "../../src/paths";
1
2
  import { compareRoutes } from "./filepath-routing";
3
+ import type { HTTPMethod, RouteConfig } from "./routes";
2
4
 
3
5
  describe("compareRoutes()", () => {
4
6
  test("routes / last", () => {
5
- expect(compareRoutes("/", "/foo")).toBeGreaterThanOrEqual(1);
6
- expect(compareRoutes("/", "/:foo")).toBeGreaterThanOrEqual(1);
7
- expect(compareRoutes("/", "/:foo*")).toBeGreaterThanOrEqual(1);
7
+ expect(
8
+ compareRoutes(routeConfig("/"), routeConfig("/foo"))
9
+ ).toBeGreaterThanOrEqual(1);
10
+ expect(
11
+ compareRoutes(routeConfig("/"), routeConfig("/:foo"))
12
+ ).toBeGreaterThanOrEqual(1);
13
+ expect(
14
+ compareRoutes(routeConfig("/"), routeConfig("/:foo*"))
15
+ ).toBeGreaterThanOrEqual(1);
8
16
  });
9
17
 
10
18
  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);
19
+ expect(
20
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar"))
21
+ ).toBeGreaterThanOrEqual(1);
22
+ expect(
23
+ compareRoutes(routeConfig("/foo"), routeConfig("/foo/bar/cat"))
24
+ ).toBeGreaterThanOrEqual(1);
13
25
  });
14
26
 
15
27
  test("routes with wildcard segments come after those without", () => {
16
- expect(compareRoutes("/:foo*", "/foo")).toBe(1);
17
- expect(compareRoutes("/:foo*", "/:foo")).toBe(1);
28
+ expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
29
+ expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(1);
18
30
  });
19
31
 
20
32
  test("routes with dynamic segments come after those without", () => {
21
- expect(compareRoutes("/:foo", "/foo")).toBe(1);
33
+ expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
22
34
  });
23
35
 
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);
36
+ test("routes with dynamic segments occurring earlier come after those with dynamic segments in later positions", () => {
37
+ expect(
38
+ compareRoutes(routeConfig("/foo/:id/bar"), routeConfig("/foo/bar/:id"))
39
+ ).toBe(1);
26
40
  });
27
41
 
28
42
  test("routes with no HTTP method come after those specifying a method", () => {
29
- expect(compareRoutes("/foo", "GET /foo")).toBe(1);
43
+ expect(compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))).toBe(
44
+ 1
45
+ );
30
46
  });
31
47
 
32
48
  test("two equal routes are sorted according to their original position in the list", () => {
33
- expect(compareRoutes("GET /foo", "GET /foo")).toBe(0);
49
+ expect(
50
+ compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
51
+ ).toBe(0);
34
52
  });
35
53
 
36
54
  test("it returns -1 if the first argument should appear first in the list", () => {
37
- expect(compareRoutes("GET /foo", "/foo")).toBe(-1);
55
+ expect(compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))).toBe(
56
+ -1
57
+ );
38
58
  });
39
59
  });
60
+
61
+ function routeConfig(routePath: string, method?: string): RouteConfig {
62
+ return {
63
+ routePath: toUrlPath(routePath),
64
+ method: method as HTTPMethod,
65
+ };
66
+ }
@@ -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) => {
@@ -52,7 +52,7 @@ export async function generateConfigFromFileTree({
52
52
  const declaration = node.declaration;
53
53
 
54
54
  // `export async function onRequest() {...}`
55
- if (declaration.type === "FunctionDeclaration") {
55
+ if (declaration.type === "FunctionDeclaration" && declaration.id) {
56
56
  exportNames.push(declaration.id.name);
57
57
  }
58
58
 
@@ -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,40 @@ 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) => routeEntry.routePath === routePath
135
131
  );
136
132
  if (existingRouteEntry !== undefined) {
137
- existingRouteEntry[1] = {
138
- ...existingRouteEntry[1],
139
- ...routeHandler,
140
- };
133
+ Object.assign(existingRouteEntry, rest);
141
134
  } else {
142
- acc.push([routePath, routeHandler]);
135
+ acc.push({ routePath, ...rest });
143
136
  }
144
137
  return acc;
145
138
  },
146
139
  []
147
140
  );
148
141
 
149
- routeEntries.sort(([pathA], [pathB]) => compareRoutes(pathA, pathB));
142
+ routeEntries.sort((a, b) => compareRoutes(a, b));
150
143
 
151
- return { routes: Object.fromEntries(routeEntries) } as Config;
144
+ return {
145
+ routes: routeEntries,
146
+ };
152
147
  }
153
148
 
154
149
  // Ensure routes are produced in order of precedence so that
155
150
  // more specific routes aren't occluded from matching due to
156
151
  // less specific routes appearing first in the route list.
157
- export function compareRoutes(a: string, b: string) {
158
- function parseRoutePath(routePath: string) {
159
- let [method, segmentedPath] = routePath.split(" ");
160
- if (!segmentedPath) {
161
- segmentedPath = method;
162
- method = null;
163
- }
164
-
165
- const segments = segmentedPath.slice(1).split("/").filter(Boolean);
166
- return [method, segments];
152
+ export function compareRoutes(
153
+ { routePath: routePathA, method: methodA }: RouteConfig,
154
+ { routePath: routePathB, method: methodB }: RouteConfig
155
+ ) {
156
+ function parseRoutePath(routePath: UrlPath): string[] {
157
+ return routePath.slice(1).split("/").filter(Boolean);
167
158
  }
168
159
 
169
- const [methodA, segmentsA] = parseRoutePath(a);
170
- const [methodB, segmentsB] = parseRoutePath(b);
160
+ const segmentsA = parseRoutePath(routePathA);
161
+ const segmentsB = parseRoutePath(routePathB);
171
162
 
172
163
  // sort routes with fewer segments after those with more segments
173
164
  if (segmentsA.length !== segmentsB.length) {
@@ -193,8 +184,8 @@ export function compareRoutes(a: string, b: string) {
193
184
  if (methodA && !methodB) return -1;
194
185
  if (!methodA && methodB) return 1;
195
186
 
196
- // all else equal, just sort them lexicographically
197
- return a.localeCompare(b);
187
+ // all else equal, just sort the paths lexicographically
188
+ return routePathA.localeCompare(routePathB);
198
189
  }
199
190
 
200
191
  async function forEachFile<T>(
@@ -204,7 +195,7 @@ async function forEachFile<T>(
204
195
  const searchPaths = [baseDir];
205
196
  const returnValues: T[] = [];
206
197
 
207
- while (searchPaths.length) {
198
+ while (isNotEmpty(searchPaths)) {
208
199
  const cwd = searchPaths.shift();
209
200
  const dir = await fs.readdir(cwd, { withFileTypes: true });
210
201
  for (const entry of dir) {
@@ -219,3 +210,10 @@ async function forEachFile<T>(
219
210
 
220
211
  return returnValues;
221
212
  }
213
+
214
+ interface NonEmptyArray<T> extends Array<T> {
215
+ shift(): T;
216
+ }
217
+ function isNotEmpty<T>(array: T[]): array is NonEmptyArray<T> {
218
+ return array.length > 0;
219
+ }
@@ -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
  };
@@ -33,25 +33,21 @@ declare const routes: RouteHandler[];
33
33
  declare const __FALLBACK_SERVICE__: string;
34
34
 
35
35
  // expect an ASSETS fetcher binding pointing to the asset-server stage
36
- type Env = {
37
- [name: string]: unknown;
38
- ASSETS: { fetch(url: string, init: RequestInit): Promise<Response> };
36
+ type FetchEnv = {
37
+ [name: string]: { fetch: typeof fetch };
38
+ ASSETS: { fetch: typeof fetch };
39
39
  };
40
40
 
41
41
  type WorkerContext = {
42
42
  waitUntil: (promise: Promise<unknown>) => void;
43
43
  };
44
44
 
45
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- `env` can be used by __FALLBACK_SERVICE_FETCH__
46
- function* executeRequest(request: Request, env: Env) {
45
+ function* executeRequest(request: Request, _env: FetchEnv) {
47
46
  const requestPath = new URL(request.url).pathname;
48
47
 
49
48
  // First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
50
49
  for (const route of [...routes].reverse()) {
51
- if (
52
- route.methods.length &&
53
- !route.methods.includes(request.method as HTTPMethod)
54
- ) {
50
+ if (route.method && route.method !== request.method) {
55
51
  continue;
56
52
  }
57
53
 
@@ -69,10 +65,7 @@ function* executeRequest(request: Request, env: Env) {
69
65
 
70
66
  // Then look for the first exact route match and execute its "modules"
71
67
  for (const route of routes) {
72
- if (
73
- route.methods.length &&
74
- !route.methods.includes(request.method as HTTPMethod)
75
- ) {
68
+ if (route.method && route.method !== request.method) {
76
69
  continue;
77
70
  }
78
71
 
@@ -88,20 +81,10 @@ function* executeRequest(request: Request, env: Env) {
88
81
  break;
89
82
  }
90
83
  }
91
-
92
- // Finally, yield to the fallback service (`env.ASSETS.fetch` in Pages' case)
93
- return {
94
- handler: () =>
95
- __FALLBACK_SERVICE__
96
- ? // @ts-expect-error expecting __FALLBACK_SERVICE__ to be the name of a service binding, so fetch should be defined
97
- env[__FALLBACK_SERVICE__].fetch(request)
98
- : fetch(request),
99
- params: {} as Params,
100
- };
101
84
  }
102
85
 
103
86
  export default {
104
- async fetch(request: Request, env: Env, workerContext: WorkerContext) {
87
+ async fetch(request: Request, env: FetchEnv, workerContext: WorkerContext) {
105
88
  const handlerIterator = executeRequest(request, env);
106
89
  const data = {}; // arbitrary data the user can set between functions
107
90
  const next = async (input?: RequestInfo, init?: RequestInit) => {
@@ -109,14 +92,11 @@ export default {
109
92
  request = new Request(input, init);
110
93
  }
111
94
 
112
- const { value } = handlerIterator.next();
113
- if (value) {
114
- const { handler, params } = value;
115
- const context: EventContext<
116
- unknown,
117
- string,
118
- Record<string, unknown>
119
- > = {
95
+ const result = handlerIterator.next();
96
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
97
+ if (result.done == false) {
98
+ const { handler, params } = result.value;
99
+ const context = {
120
100
  request: new Request(request.clone()),
121
101
  next,
122
102
  params,
@@ -132,6 +112,12 @@ export default {
132
112
  [101, 204, 205, 304].includes(response.status) ? null : response.body,
133
113
  response
134
114
  );
115
+ } else if (__FALLBACK_SERVICE__) {
116
+ // There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
117
+ return env[__FALLBACK_SERVICE__].fetch(request);
118
+ } else {
119
+ // There was not fallback service so actually make the request to the origin.
120
+ return fetch(request);
135
121
  }
136
122
  };
137
123
 
@@ -15,7 +15,7 @@ describe("Dev component", () => {
15
15
  accountId: "some-account-id",
16
16
  public: "some/public/path",
17
17
  });
18
- expect(lastFrame().split("\n").slice(0, 2).join("\n"))
18
+ expect(lastFrame()?.split("\n").slice(0, 2).join("\n"))
19
19
  .toMatchInlineSnapshot(`
20
20
  "Something went wrong:
21
21
  Error: You cannot use the service worker format with a \`public\` directory."
@@ -39,16 +39,19 @@ function renderDev({
39
39
  jsxFragment,
40
40
  bindings = {},
41
41
  public: publicDir,
42
- site,
42
+ assetPaths,
43
43
  compatibilityDate,
44
44
  compatibilityFlags,
45
45
  usageModel,
46
46
  buildCommand = {},
47
+ enableLocalPersistence = false,
48
+ env = undefined,
47
49
  }: Partial<DevProps>) {
48
50
  return render(
49
51
  <Dev
50
52
  name={name}
51
53
  entry={entry}
54
+ env={env}
52
55
  port={port}
53
56
  buildCommand={buildCommand}
54
57
  format={format}
@@ -56,12 +59,13 @@ function renderDev({
56
59
  jsxFactory={jsxFactory}
57
60
  jsxFragment={jsxFragment}
58
61
  accountId={accountId}
59
- site={site}
62
+ assetPaths={assetPaths}
60
63
  public={publicDir}
61
64
  compatibilityDate={compatibilityDate}
62
65
  compatibilityFlags={compatibilityFlags}
63
66
  usageModel={usageModel}
64
67
  bindings={bindings}
68
+ enableLocalPersistence={enableLocalPersistence}
65
69
  />
66
70
  );
67
71
  }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The typings file available at `@types/cmd-shim` are out of date.
3
+ */
4
+ module "cmd-shim" {
5
+ /**
6
+ *
7
+ * Create a cmd shim at `to` for the command line program at `from`.
8
+ *
9
+ */
10
+ export default function cmdShim(from: string, to: string): Promise<void>;
11
+ }
@@ -0,0 +1,30 @@
1
+ const ORIGINAL_CF_API_TOKEN = process.env.CF_API_TOKEN;
2
+ const ORIGINAL_CF_ACCOUNT_ID = process.env.CF_ACCOUNT_ID;
3
+
4
+ /**
5
+ * Mock the API token so that we don't need to read it from user configuration files.
6
+ */
7
+ export function mockApiToken({
8
+ apiToken = "some-api-token",
9
+ }: { apiToken?: string } = {}) {
10
+ beforeEach(() => {
11
+ process.env.CF_API_TOKEN = apiToken;
12
+ });
13
+ afterEach(() => {
14
+ process.env.CF_API_TOKEN = ORIGINAL_CF_API_TOKEN;
15
+ });
16
+ }
17
+
18
+ /**
19
+ * Mock the current account ID so that we don't need to read it from configuration files.
20
+ */
21
+ export function mockAccountId({
22
+ accountId = "some-account-id",
23
+ }: { accountId?: string } = {}) {
24
+ beforeEach(() => {
25
+ process.env.CF_ACCOUNT_ID = accountId;
26
+ });
27
+ afterEach(() => {
28
+ process.env.CF_ACCOUNT_ID = ORIGINAL_CF_ACCOUNT_ID;
29
+ });
30
+ }
@@ -0,0 +1,33 @@
1
+ import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
+ import { basename, resolve } from "node:path";
3
+ import cmdShim from "cmd-shim";
4
+
5
+ const nodeShebang = "#!/usr/bin/env node";
6
+
7
+ /**
8
+ * Create a binary file in a temp directory and make it available on the PATH.
9
+ */
10
+ export async function mockBinary(binaryName: string, code: string) {
11
+ // Ensure there is a directory to put the mock binary in.
12
+ const tmpDir = resolve(mkdtempSync(".mock-binary-"));
13
+
14
+ // Use a fake extension on Windows because we will create a cmd-shim to run the binary.
15
+ const extension = process.platform === "win32" ? ".x-mock-bin" : "";
16
+ const filePath = resolve(tmpDir, `${binaryName}${extension}`);
17
+ writeFileSync(filePath, nodeShebang + "\n" + code);
18
+ chmodSync(filePath, 0o777);
19
+
20
+ if (process.platform === "win32") {
21
+ await cmdShim(filePath, basename(filePath, ".x-mock-bin"));
22
+ }
23
+
24
+ // Update PATH using the appropriate separator for the platform.
25
+ const oldPath = process.env.PATH;
26
+ const sep = process.platform === "win32" ? ";" : ":";
27
+ process.env.PATH = tmpDir + sep + oldPath;
28
+
29
+ return function unMock() {
30
+ rmSync(tmpDir, { recursive: true });
31
+ process.env.PATH = process.env.PATH?.replace(tmpDir + sep, "");
32
+ };
33
+ }