wrangler 0.0.15 → 0.0.16
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.
- package/bin/wrangler.js +2 -2
- package/package.json +10 -10
- package/pages/functions/buildWorker.ts +1 -1
- package/pages/functions/filepath-routing.test.ts +40 -13
- package/pages/functions/filepath-routing.ts +42 -51
- package/pages/functions/routes.ts +11 -18
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/dev.test.tsx +2 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/mock-account-id.ts +30 -0
- package/src/__tests__/helpers/mock-bin.ts +33 -0
- package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +43 -9
- package/src/__tests__/helpers/mock-console.ts +56 -0
- package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
- package/src/__tests__/{mock-kv.ts → helpers/mock-kv.ts} +0 -0
- package/src/__tests__/{mock-user.ts → helpers/mock-user.ts} +2 -2
- package/src/__tests__/{run-in-tmp.ts → helpers/run-in-tmp.ts} +1 -1
- package/src/__tests__/helpers/run-wrangler.ts +14 -0
- package/src/__tests__/index.test.ts +187 -68
- package/src/__tests__/jest.setup.ts +12 -4
- package/src/__tests__/kv.test.ts +114 -199
- package/src/__tests__/logout.test.ts +5 -19
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/publish.test.ts +179 -108
- package/src/__tests__/secret.test.ts +9 -5
- package/src/__tests__/whoami.test.tsx +6 -21
- package/src/api/form_data.ts +5 -6
- package/src/api/preview.ts +2 -2
- package/src/api/worker.ts +3 -3
- package/src/cfetch/index.ts +3 -12
- package/src/cfetch/internal.ts +33 -3
- package/src/dev.tsx +93 -65
- package/src/dialogs.tsx +2 -2
- package/src/index.tsx +94 -70
- package/src/inspect.ts +7 -6
- package/src/kv.tsx +9 -1
- package/src/module-collection.ts +5 -5
- package/src/package-manager.ts +120 -0
- package/src/pages.tsx +21 -16
- package/src/paths.ts +26 -0
- package/src/proxy.ts +6 -6
- package/src/publish.ts +9 -8
- package/src/sites.tsx +27 -26
- package/src/user.tsx +13 -13
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/wrangler-dist/cli.js +111028 -110366
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/mock-console.ts +0 -34
- 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.
|
|
3
|
+
"version": "0.0.16",
|
|
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.
|
|
39
|
+
"esbuild": "0.14.14",
|
|
40
40
|
"miniflare": "2.2.0",
|
|
41
41
|
"path-to-regexp": "^6.2.0",
|
|
42
42
|
"semiver": "^1.1.0",
|
|
43
|
-
"xxhash-
|
|
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": "
|
|
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
|
-
"
|
|
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": "
|
|
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/(?!
|
|
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,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(
|
|
6
|
-
|
|
7
|
-
|
|
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(
|
|
12
|
-
|
|
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
|
|
25
|
-
expect(
|
|
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", "
|
|
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(
|
|
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("
|
|
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
|
|
2
|
-
import
|
|
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
|
|
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
|
-
}:
|
|
18
|
-
|
|
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
|
-
|
|
83
|
-
|
|
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
|
-
|
|
112
|
-
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,
|
|
128
|
+
(acc: typeof routeEntries, { routePath, ...rest }) => {
|
|
133
129
|
const existingRouteEntry = acc.find(
|
|
134
|
-
(routeEntry) => routeEntry
|
|
130
|
+
(routeEntry) => routeEntry.routePath === routePath
|
|
135
131
|
);
|
|
136
132
|
if (existingRouteEntry !== undefined) {
|
|
137
|
-
existingRouteEntry
|
|
138
|
-
...existingRouteEntry[1],
|
|
139
|
-
...routeHandler,
|
|
140
|
-
};
|
|
133
|
+
Object.assign(existingRouteEntry, rest);
|
|
141
134
|
} else {
|
|
142
|
-
acc.push(
|
|
135
|
+
acc.push({ routePath, ...rest });
|
|
143
136
|
}
|
|
144
137
|
return acc;
|
|
145
138
|
},
|
|
146
139
|
[]
|
|
147
140
|
);
|
|
148
141
|
|
|
149
|
-
routeEntries.sort((
|
|
142
|
+
routeEntries.sort((a, b) => compareRoutes(a, b));
|
|
150
143
|
|
|
151
|
-
return {
|
|
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(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
|
170
|
-
const
|
|
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
|
|
197
|
-
return
|
|
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
|
|
2
|
-
import
|
|
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:
|
|
23
|
-
|
|
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?:
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -45,11 +45,13 @@ function renderDev({
|
|
|
45
45
|
usageModel,
|
|
46
46
|
buildCommand = {},
|
|
47
47
|
enableLocalPersistence = false,
|
|
48
|
+
env = undefined,
|
|
48
49
|
}: Partial<DevProps>) {
|
|
49
50
|
return render(
|
|
50
51
|
<Dev
|
|
51
52
|
name={name}
|
|
52
53
|
entry={entry}
|
|
54
|
+
env={env}
|
|
53
55
|
port={port}
|
|
54
56
|
buildCommand={buildCommand}
|
|
55
57
|
format={format}
|
|
File without changes
|
|
@@ -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
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { URLSearchParams } from "node:url";
|
|
1
|
+
import { URL, URLSearchParams } from "node:url";
|
|
3
2
|
import { pathToRegexp } from "path-to-regexp";
|
|
4
|
-
import { CF_API_BASE_URL } from "
|
|
5
|
-
import type { FetchResult } from "
|
|
3
|
+
import { CF_API_BASE_URL } from "../../cfetch";
|
|
4
|
+
import type { FetchResult } from "../../cfetch";
|
|
5
|
+
import type { RequestInit } from "undici";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The signature of the function that will handle a mock request.
|
|
@@ -140,23 +140,23 @@ export function setMockResponse<ResponseType>(
|
|
|
140
140
|
/**
|
|
141
141
|
* A helper to make it easier to create `FetchResult` objects in tests.
|
|
142
142
|
*/
|
|
143
|
-
export function createFetchResult<ResponseType>(
|
|
144
|
-
result: ResponseType
|
|
143
|
+
export async function createFetchResult<ResponseType>(
|
|
144
|
+
result: ResponseType | Promise<ResponseType>,
|
|
145
145
|
success = true,
|
|
146
146
|
errors = [],
|
|
147
147
|
messages = [],
|
|
148
148
|
result_info?: unknown
|
|
149
|
-
): FetchResult<ResponseType
|
|
149
|
+
): Promise<FetchResult<ResponseType>> {
|
|
150
150
|
return result_info
|
|
151
151
|
? {
|
|
152
|
-
result,
|
|
152
|
+
result: await result,
|
|
153
153
|
success,
|
|
154
154
|
errors,
|
|
155
155
|
messages,
|
|
156
156
|
result_info,
|
|
157
157
|
}
|
|
158
158
|
: {
|
|
159
|
-
result,
|
|
159
|
+
result: await result,
|
|
160
160
|
success,
|
|
161
161
|
errors,
|
|
162
162
|
messages,
|
|
@@ -171,3 +171,37 @@ export function createFetchResult<ResponseType>(
|
|
|
171
171
|
export function unsetAllMocks() {
|
|
172
172
|
mocks.length = 0;
|
|
173
173
|
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* We special-case fetching the request for `kv:key get`, because it's
|
|
177
|
+
* the only cloudflare API endpoint that returns a plain string as the
|
|
178
|
+
* value, and not as the "standard" FetchResult-style json. Hence, we also
|
|
179
|
+
* special-case mocking it here.
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
const kvGetMocks = new Map<string, string>();
|
|
183
|
+
|
|
184
|
+
export function mockFetchKVGetValue(
|
|
185
|
+
accountId: string,
|
|
186
|
+
namespaceId: string,
|
|
187
|
+
key: string
|
|
188
|
+
) {
|
|
189
|
+
const mapKey = `${accountId}/${namespaceId}/${key}`;
|
|
190
|
+
if (kvGetMocks.has(mapKey)) {
|
|
191
|
+
return kvGetMocks.get(mapKey);
|
|
192
|
+
}
|
|
193
|
+
throw new Error(`no mock value found for \`kv:key get\` - ${mapKey}`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function setMockFetchKVGetValue(
|
|
197
|
+
accountId: string,
|
|
198
|
+
namespaceId: string,
|
|
199
|
+
key: string,
|
|
200
|
+
value: string
|
|
201
|
+
) {
|
|
202
|
+
kvGetMocks.set(`${accountId}/${namespaceId}/${key}`, value);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function unsetMockFetchKVGetValues() {
|
|
206
|
+
kvGetMocks.clear();
|
|
207
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* We use this module to mock console methods, and optionally
|
|
3
|
+
* assert on the values they're called with in our tests.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
let logSpy: jest.SpyInstance,
|
|
7
|
+
errorSpy: jest.SpyInstance,
|
|
8
|
+
warnSpy: jest.SpyInstance;
|
|
9
|
+
|
|
10
|
+
const std = {
|
|
11
|
+
get out() {
|
|
12
|
+
return normalizeSlashes(stripTimings(logSpy.mock.calls.flat(2).join("\n")));
|
|
13
|
+
},
|
|
14
|
+
get err() {
|
|
15
|
+
return normalizeSlashes(
|
|
16
|
+
stripTimings(errorSpy.mock.calls.flat(2).join("\n"))
|
|
17
|
+
);
|
|
18
|
+
},
|
|
19
|
+
get warn() {
|
|
20
|
+
return normalizeSlashes(
|
|
21
|
+
stripTimings(warnSpy.mock.calls.flat(2).join("\n"))
|
|
22
|
+
);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function mockConsoleMethods() {
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
29
|
+
errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
30
|
+
warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
31
|
+
});
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
logSpy.mockRestore();
|
|
34
|
+
errorSpy.mockRestore();
|
|
35
|
+
warnSpy.mockRestore();
|
|
36
|
+
});
|
|
37
|
+
return std;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ensure slashes in the `str` are OS file-system agnostic.
|
|
42
|
+
*
|
|
43
|
+
* Use this in snapshot tests to be resilient to file-system differences.
|
|
44
|
+
*/
|
|
45
|
+
export function normalizeSlashes(str: string): string {
|
|
46
|
+
return str.replace(/\\/g, "/");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Strip "timing data" out of the `stdout` string, since this is not always deterministic.
|
|
51
|
+
*
|
|
52
|
+
* Use this in snapshot tests to be resilient to slight changes in timing of processing.
|
|
53
|
+
*/
|
|
54
|
+
export function stripTimings(stdout: string): string {
|
|
55
|
+
return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
|
|
56
|
+
}
|