wrangler 0.0.0-a417cb0 → 0.0.0-a73e32f

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 (56) hide show
  1. package/bin/wrangler.js +2 -2
  2. package/package.json +11 -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 +42 -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 +7 -3
  9. package/src/__tests__/guess-worker-format.test.ts +51 -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/mock-account-id.ts +30 -0
  13. package/src/__tests__/helpers/mock-bin.ts +33 -0
  14. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +43 -9
  15. package/src/__tests__/helpers/mock-console.ts +56 -0
  16. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  17. package/src/__tests__/helpers/mock-kv.ts +40 -0
  18. package/src/__tests__/helpers/mock-user.ts +27 -0
  19. package/src/__tests__/{run-in-tmp.ts → helpers/run-in-tmp.ts} +1 -1
  20. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  21. package/src/__tests__/index.test.ts +394 -69
  22. package/src/__tests__/jest.setup.ts +12 -4
  23. package/src/__tests__/kv.test.ts +114 -238
  24. package/src/__tests__/logout.test.ts +5 -19
  25. package/src/__tests__/package-manager.test.ts +206 -0
  26. package/src/__tests__/publish.test.ts +601 -136
  27. package/src/__tests__/secret.test.ts +9 -5
  28. package/src/__tests__/whoami.test.tsx +6 -47
  29. package/src/api/form_data.ts +27 -7
  30. package/src/api/preview.ts +2 -2
  31. package/src/api/worker.ts +18 -4
  32. package/src/cfetch/index.ts +3 -12
  33. package/src/cfetch/internal.ts +33 -3
  34. package/src/config.ts +19 -7
  35. package/src/dev.tsx +170 -101
  36. package/src/dialogs.tsx +2 -2
  37. package/src/guess-worker-format.ts +66 -0
  38. package/src/index.tsx +178 -78
  39. package/src/inspect.ts +7 -6
  40. package/src/kv.tsx +9 -1
  41. package/src/module-collection.ts +7 -8
  42. package/src/open-in-browser.ts +13 -0
  43. package/src/package-manager.ts +120 -0
  44. package/src/pages.tsx +23 -20
  45. package/src/paths.ts +26 -0
  46. package/src/proxy.ts +6 -6
  47. package/src/publish.ts +300 -286
  48. package/src/sites.tsx +27 -26
  49. package/src/user.tsx +18 -18
  50. package/templates/new-worker.js +15 -0
  51. package/templates/new-worker.ts +15 -0
  52. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  53. package/wrangler-dist/cli.js +111283 -110490
  54. package/wrangler-dist/cli.js.map +3 -3
  55. package/src/__tests__/mock-console.ts +0 -34
  56. 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.0-a417cb0",
3
+ "version": "0.0.0-a73e32f",
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"
@@ -59,13 +59,13 @@
59
59
  "acorn-walk": "^8.2.0",
60
60
  "chokidar": "^3.5.2",
61
61
  "clipboardy": "^3.0.0",
62
+ "cmd-shim": "^4.1.0",
62
63
  "command-exists": "^1.2.9",
63
64
  "devtools-protocol": "^0.0.955664",
64
65
  "execa": "^6.0.0",
65
66
  "faye-websocket": "^0.11.4",
66
67
  "finalhandler": "^1.1.2",
67
68
  "find-up": "^6.2.0",
68
- "formdata-node": "^4.3.1",
69
69
  "ignore": "^5.2.0",
70
70
  "ink": "^3.2.0",
71
71
  "ink-select-input": "^4.2.1",
@@ -74,14 +74,13 @@
74
74
  "ink-text-input": "^4.0.2",
75
75
  "jest-fetch-mock": "^3.0.3",
76
76
  "mime": "^3.0.0",
77
- "node-fetch": "3.1.1",
78
77
  "open": "^8.4.0",
79
78
  "react": "^17.0.2",
80
79
  "react-error-boundary": "^3.1.4",
81
80
  "serve-static": "^1.14.1",
82
81
  "signal-exit": "^3.0.6",
83
82
  "tmp-promise": "^3.0.3",
84
- "undici": "^4.11.1",
83
+ "undici": "4.13.0",
85
84
  "ws": "^8.3.0",
86
85
  "yargs": "^17.3.0"
87
86
  },
@@ -91,16 +90,17 @@
91
90
  "pages",
92
91
  "miniflare-config-stubs",
93
92
  "wrangler-dist",
94
- "static-asset-facade.js",
93
+ "templates",
95
94
  "vendor",
96
95
  "import_meta_url.js"
97
96
  ],
98
97
  "scripts": {
99
98
  "clean": "rm -rf wrangler-dist",
99
+ "check:type": "tsc",
100
100
  "bundle": "node -r esbuild-register scripts/bundle.ts",
101
101
  "build": "npm run clean && npm run bundle",
102
102
  "start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
103
- "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
104
  "test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
105
105
  },
106
106
  "engines": {
@@ -111,10 +111,10 @@
111
111
  "testTimeout": 30000,
112
112
  "testRegex": ".*.(test|spec)\\.[jt]sx?$",
113
113
  "transformIgnorePatterns": [
114
- "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)"
115
115
  ],
116
116
  "moduleNameMapper": {
117
- "clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
117
+ "clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
118
118
  },
119
119
  "transform": {
120
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) => {
@@ -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): [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];
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>(
@@ -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
 
@@ -5,11 +5,13 @@ import Dev from "../dev";
5
5
  import type { DevProps } from "../dev";
6
6
 
7
7
  describe("Dev component", () => {
8
- let restoreConsole;
8
+ let restoreConsole: ReturnType<typeof patchConsole>;
9
9
  beforeEach(() => (restoreConsole = patchConsole(() => {})));
10
10
  afterEach(() => restoreConsole());
11
11
 
12
- it("should throw if format is service-worker and there is a public directory", () => {
12
+ // This test needs to be rewritten because the error now throws asynchronously
13
+ // and the Ink framework does not yet have async testing support.
14
+ it.skip("should throw if format is service-worker and there is a public directory", () => {
13
15
  const { lastFrame } = renderDev({
14
16
  format: "service-worker",
15
17
  accountId: "some-account-id",
@@ -34,7 +36,7 @@ function renderDev({
34
36
  port,
35
37
  format,
36
38
  accountId,
37
- initialMode = "remote",
39
+ initialMode = "local",
38
40
  jsxFactory,
39
41
  jsxFragment,
40
42
  bindings = {},
@@ -45,11 +47,13 @@ function renderDev({
45
47
  usageModel,
46
48
  buildCommand = {},
47
49
  enableLocalPersistence = false,
50
+ env = undefined,
48
51
  }: Partial<DevProps>) {
49
52
  return render(
50
53
  <Dev
51
54
  name={name}
52
55
  entry={entry}
56
+ env={env}
53
57
  port={port}
54
58
  buildCommand={buildCommand}
55
59
  format={format}
@@ -0,0 +1,51 @@
1
+ import { writeFile } from "fs/promises";
2
+ import path from "path";
3
+ import guessWorkerFormat from "../guess-worker-format";
4
+ import { runInTempDir } from "./helpers/run-in-tmp";
5
+
6
+ describe("guess worker format", () => {
7
+ runInTempDir();
8
+ it('should detect a "modules" worker', async () => {
9
+ await writeFile("./index.ts", "export default {};");
10
+ // Note that this isn't actually a valid worker, because it's missing
11
+ // a fetch handler. Regardless, our heuristic is simply to check for exports.
12
+ const guess = await guessWorkerFormat(
13
+ path.join(process.cwd(), "./index.ts"),
14
+ undefined
15
+ );
16
+ expect(guess).toBe("modules");
17
+ });
18
+
19
+ it('should detect a "service-worker" worker', async () => {
20
+ await writeFile("./index.ts", "");
21
+ // Note that this isn't actually a valid worker, because it's missing
22
+ // a fetch listener. Regardless, our heuristic is simply to check for
23
+ // the lack of exports.
24
+ const guess = await guessWorkerFormat(
25
+ path.join(process.cwd(), "./index.ts"),
26
+ undefined
27
+ );
28
+ expect(guess).toBe("service-worker");
29
+ });
30
+
31
+ it("should throw an error when the hint doesn't match the guess (modules - service-worker)", async () => {
32
+ await writeFile("./index.ts", "export default {};");
33
+ await expect(
34
+ guessWorkerFormat(
35
+ path.join(process.cwd(), "./index.ts"),
36
+ "service-worker"
37
+ )
38
+ ).rejects.toThrow(
39
+ "You configured this worker to be a 'service-worker', but the file you are trying to build appears to have es module exports. Please pass `--format modules`, or simply remove the configuration."
40
+ );
41
+ });
42
+
43
+ it("should throw an error when the hint doesn't match the guess (service-worker - modules)", async () => {
44
+ await writeFile("./index.ts", "");
45
+ await expect(
46
+ guessWorkerFormat(path.join(process.cwd(), "./index.ts"), "modules")
47
+ ).rejects.toThrow(
48
+ "You configured this worker to be 'modules', but the file you are trying to build doesn't export a handler. Please pass `--format service-worker`, or simply remove the configuration."
49
+ );
50
+ });
51
+ });
@@ -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
+ }