wrangler 0.0.0-ba6fc9c → 0.0.0-bc85682
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 +26 -13
- package/pages/functions/buildWorker.ts +1 -1
- package/pages/functions/filepath-routing.test.ts +112 -28
- package/pages/functions/filepath-routing.ts +80 -128
- package/pages/functions/routes.ts +11 -18
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/configuration.test.ts +2186 -0
- package/src/__tests__/dev.test.tsx +237 -57
- package/src/__tests__/guess-worker-format.test.ts +66 -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/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +30 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +50 -14
- package/src/__tests__/helpers/mock-console.ts +70 -0
- package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-user.ts +27 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/index.test.ts +655 -76
- package/src/__tests__/jest.setup.ts +49 -2
- package/src/__tests__/kv.test.ts +623 -437
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +2107 -70
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/secret.test.ts +323 -0
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +564 -0
- package/src/__tests__/whoami.test.tsx +86 -0
- package/src/api/form_data.ts +112 -26
- package/src/api/preview.ts +36 -19
- package/src/api/worker.ts +53 -22
- package/src/bundle.ts +133 -0
- package/src/cfetch/index.ts +21 -19
- package/src/cfetch/internal.ts +82 -18
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +270 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +305 -0
- package/src/config/index.ts +52 -0
- package/src/config/validation-helpers.ts +370 -0
- package/src/config/validation.ts +1019 -0
- package/src/dev.tsx +409 -232
- package/src/dialogs.tsx +2 -2
- package/src/entry.ts +57 -0
- package/src/environment-variables.ts +33 -0
- package/src/guess-worker-format.ts +68 -0
- package/src/index.tsx +1142 -637
- package/src/inspect.ts +94 -24
- package/src/kv.tsx +81 -44
- package/src/module-collection.ts +142 -36
- package/src/open-in-browser.ts +13 -0
- package/src/package-manager.ts +126 -0
- package/src/pages.tsx +29 -24
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +88 -14
- package/src/publish.ts +299 -311
- package/src/r2.ts +50 -0
- package/src/reporting.ts +115 -0
- package/src/sites.tsx +181 -61
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +62 -0
- package/src/user.tsx +120 -98
- package/src/whoami.tsx +69 -0
- 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/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +129889 -113634
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/run-in-tmp.ts +0 -19
- package/src/__tests__/run-wrangler.ts +0 -32
- package/src/config.ts +0 -487
- package/src/tail.tsx +0 -73
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-
|
|
3
|
+
"version": "0.0.0-bc85682",
|
|
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.
|
|
40
|
-
"miniflare": "2.
|
|
39
|
+
"esbuild": "0.14.23",
|
|
40
|
+
"miniflare": "2.3.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"
|
|
@@ -47,38 +48,47 @@
|
|
|
47
48
|
"devDependencies": {
|
|
48
49
|
"@babel/types": "^7.16.0",
|
|
49
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",
|
|
50
55
|
"@types/estree": "^0.0.50",
|
|
56
|
+
"@types/glob-to-regexp": "0.4.1",
|
|
51
57
|
"@types/mime": "^2.0.3",
|
|
58
|
+
"@types/prompts": "^2.0.14",
|
|
52
59
|
"@types/react": "^17.0.37",
|
|
53
60
|
"@types/serve-static": "^1.13.10",
|
|
54
61
|
"@types/signal-exit": "^3.0.1",
|
|
55
62
|
"@types/ws": "^8.2.1",
|
|
56
63
|
"@types/yargs": "^17.0.7",
|
|
57
|
-
"acorn": "^8.6.0",
|
|
58
|
-
"acorn-walk": "^8.2.0",
|
|
59
64
|
"chokidar": "^3.5.2",
|
|
60
65
|
"clipboardy": "^3.0.0",
|
|
66
|
+
"cmd-shim": "^4.1.0",
|
|
61
67
|
"command-exists": "^1.2.9",
|
|
62
68
|
"devtools-protocol": "^0.0.955664",
|
|
63
69
|
"execa": "^6.0.0",
|
|
64
70
|
"faye-websocket": "^0.11.4",
|
|
65
71
|
"finalhandler": "^1.1.2",
|
|
66
72
|
"find-up": "^6.2.0",
|
|
67
|
-
"
|
|
73
|
+
"get-port": "^6.1.2",
|
|
74
|
+
"glob-to-regexp": "0.4.1",
|
|
75
|
+
"ignore": "^5.2.0",
|
|
68
76
|
"ink": "^3.2.0",
|
|
69
77
|
"ink-select-input": "^4.2.1",
|
|
70
78
|
"ink-table": "^3.0.0",
|
|
71
79
|
"ink-testing-library": "^2.1.0",
|
|
72
80
|
"ink-text-input": "^4.0.2",
|
|
81
|
+
"jest-fetch-mock": "^3.0.3",
|
|
82
|
+
"jest-websocket-mock": "^2.3.0",
|
|
73
83
|
"mime": "^3.0.0",
|
|
74
|
-
"node-fetch": "^3.1.0",
|
|
75
84
|
"open": "^8.4.0",
|
|
85
|
+
"prompts": "^2.4.2",
|
|
76
86
|
"react": "^17.0.2",
|
|
77
87
|
"react-error-boundary": "^3.1.4",
|
|
78
88
|
"serve-static": "^1.14.1",
|
|
79
89
|
"signal-exit": "^3.0.6",
|
|
80
90
|
"tmp-promise": "^3.0.3",
|
|
81
|
-
"undici": "
|
|
91
|
+
"undici": "4.13.0",
|
|
82
92
|
"ws": "^8.3.0",
|
|
83
93
|
"yargs": "^17.3.0"
|
|
84
94
|
},
|
|
@@ -88,16 +98,18 @@
|
|
|
88
98
|
"pages",
|
|
89
99
|
"miniflare-config-stubs",
|
|
90
100
|
"wrangler-dist",
|
|
91
|
-
"
|
|
101
|
+
"templates",
|
|
92
102
|
"vendor",
|
|
93
103
|
"import_meta_url.js"
|
|
94
104
|
],
|
|
95
105
|
"scripts": {
|
|
96
106
|
"clean": "rm -rf wrangler-dist",
|
|
107
|
+
"check:type": "tsc",
|
|
97
108
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
98
109
|
"build": "npm run clean && npm run bundle",
|
|
110
|
+
"prepublishOnly": "npm run build",
|
|
99
111
|
"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
100
|
-
"test": "
|
|
112
|
+
"test": "jest --silent=false --verbose=true",
|
|
101
113
|
"test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
|
|
102
114
|
},
|
|
103
115
|
"engines": {
|
|
@@ -105,12 +117,13 @@
|
|
|
105
117
|
},
|
|
106
118
|
"jest": {
|
|
107
119
|
"restoreMocks": true,
|
|
120
|
+
"testTimeout": 30000,
|
|
108
121
|
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
|
|
109
122
|
"transformIgnorePatterns": [
|
|
110
|
-
"node_modules/(?!
|
|
123
|
+
"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|get-port)"
|
|
111
124
|
],
|
|
112
125
|
"moduleNameMapper": {
|
|
113
|
-
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
|
|
126
|
+
"clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
|
|
114
127
|
},
|
|
115
128
|
"transform": {
|
|
116
129
|
"^.+\\.c?(t|j)sx?$": [
|
|
@@ -1,39 +1,123 @@
|
|
|
1
|
-
import {
|
|
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("
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
38
|
+
test("routes with dynamic segments come after those without", () => {
|
|
39
|
+
expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
|
|
40
|
+
});
|
|
23
41
|
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
29
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
37
|
-
|
|
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: "DELETE",
|
|
94
|
+
module: ["bar.ts:onRequestDelete"],
|
|
95
|
+
routePath: "/base/bar",
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
method: "PUT",
|
|
99
|
+
module: ["bar.ts:onRequestPut"],
|
|
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,171 +1,123 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import type {
|
|
7
|
-
import type { ExportNamedDeclaration, Identifier } from "estree";
|
|
8
|
-
|
|
9
|
-
type Arguments = {
|
|
10
|
-
baseDir: string;
|
|
11
|
-
baseURL: string;
|
|
12
|
-
};
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { build } from "esbuild";
|
|
4
|
+
import { toUrlPath } from "../../src/paths";
|
|
5
|
+
import type { UrlPath } from "../../src/paths";
|
|
6
|
+
import type { HTTPMethod, RouteConfig } from "./routes";
|
|
13
7
|
|
|
14
8
|
export async function generateConfigFromFileTree({
|
|
15
9
|
baseDir,
|
|
16
10
|
baseURL,
|
|
17
|
-
}:
|
|
18
|
-
|
|
11
|
+
}: {
|
|
12
|
+
baseDir: string;
|
|
13
|
+
baseURL: UrlPath;
|
|
14
|
+
}) {
|
|
15
|
+
let routeEntries: RouteConfig[] = [];
|
|
19
16
|
|
|
20
17
|
if (!baseURL.startsWith("/")) {
|
|
21
|
-
baseURL = `/${baseURL}
|
|
18
|
+
baseURL = `/${baseURL}` as UrlPath;
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
if (baseURL.endsWith("/")) {
|
|
25
|
-
baseURL = baseURL.slice(0, -1);
|
|
22
|
+
baseURL = baseURL.slice(0, -1) as UrlPath;
|
|
26
23
|
}
|
|
27
24
|
|
|
28
25
|
await forEachFile(baseDir, async (filepath) => {
|
|
29
26
|
const ext = path.extname(filepath);
|
|
30
27
|
if (/^\.(mjs|js|ts|tsx|jsx)$/.test(ext)) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const { metafile } = await build({
|
|
29
|
+
metafile: true,
|
|
30
|
+
write: false,
|
|
31
|
+
bundle: false,
|
|
32
|
+
entryPoints: [path.resolve(filepath)],
|
|
34
33
|
});
|
|
34
|
+
const exportNames: string[] = [];
|
|
35
|
+
if (metafile) {
|
|
36
|
+
for (const output in metafile?.outputs) {
|
|
37
|
+
exportNames.push(...metafile.outputs[output].exports);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const exportName of exportNames) {
|
|
41
|
+
const [match, method = ""] = (exportName.match(
|
|
42
|
+
/^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
|
|
43
|
+
) ?? []) as (string | undefined)[];
|
|
35
44
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// This dynamic cast assumes that the AST generated by acornWalk will generate nodes that
|
|
44
|
-
// are compatible with the eslint AST nodes.
|
|
45
|
-
const node = _node as ExportNamedDeclaration;
|
|
46
|
-
|
|
47
|
-
// this is an array because multiple things can be exported from a single statement
|
|
48
|
-
// i.e. `export {foo, bar}` or `export const foo = "f", bar = "b"`
|
|
49
|
-
const exportNames: string[] = [];
|
|
50
|
-
|
|
51
|
-
if (node.declaration) {
|
|
52
|
-
const declaration = node.declaration;
|
|
53
|
-
|
|
54
|
-
// `export async function onRequest() {...}`
|
|
55
|
-
if (declaration.type === "FunctionDeclaration" && declaration.id) {
|
|
56
|
-
exportNames.push(declaration.id.name);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// `export const onRequestGet = () => {}, onRequestPost = () => {}`
|
|
60
|
-
if (declaration.type === "VariableDeclaration") {
|
|
61
|
-
exportNames.push(
|
|
62
|
-
...declaration.declarations.map(
|
|
63
|
-
(variableDeclarator) =>
|
|
64
|
-
(variableDeclarator.id as unknown as Identifier).name
|
|
65
|
-
)
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
45
|
+
if (match) {
|
|
46
|
+
const basename = path.basename(filepath).slice(0, -ext.length);
|
|
47
|
+
|
|
48
|
+
const isIndexFile = basename === "index";
|
|
49
|
+
// TODO: deprecate _middleware_ in favor of _middleware
|
|
50
|
+
const isMiddlewareFile =
|
|
51
|
+
basename === "_middleware" || basename === "_middleware_";
|
|
69
52
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
)
|
|
77
|
-
);
|
|
53
|
+
let routePath = path
|
|
54
|
+
.relative(baseDir, filepath)
|
|
55
|
+
.slice(0, -ext.length);
|
|
56
|
+
|
|
57
|
+
if (isIndexFile || isMiddlewareFile) {
|
|
58
|
+
routePath = path.dirname(routePath);
|
|
78
59
|
}
|
|
79
60
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
exportName.match(
|
|
83
|
-
/^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
|
|
84
|
-
) ?? [];
|
|
85
|
-
|
|
86
|
-
if (match) {
|
|
87
|
-
const basename = path.basename(filepath).slice(0, -ext.length);
|
|
88
|
-
|
|
89
|
-
const isIndexFile = basename === "index";
|
|
90
|
-
// TODO: deprecate _middleware_ in favor of _middleware
|
|
91
|
-
const isMiddlewareFile =
|
|
92
|
-
basename === "_middleware" || basename === "_middleware_";
|
|
93
|
-
|
|
94
|
-
let routePath = path
|
|
95
|
-
.relative(baseDir, filepath)
|
|
96
|
-
.slice(0, -ext.length);
|
|
97
|
-
|
|
98
|
-
if (isIndexFile || isMiddlewareFile) {
|
|
99
|
-
routePath = path.dirname(routePath);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (routePath === ".") {
|
|
103
|
-
routePath = "";
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
routePath = `${baseURL}/${routePath}`;
|
|
107
|
-
|
|
108
|
-
routePath = routePath.replace(/\[\[(.+)]]/g, ":$1*"); // transform [[id]] => :id*
|
|
109
|
-
routePath = routePath.replace(/\[(.+)]/g, ":$1"); // transform [id] => :id
|
|
110
|
-
|
|
111
|
-
if (method) {
|
|
112
|
-
routePath = `${method.toUpperCase()} ${routePath}`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
routeEntries.push([
|
|
116
|
-
routePath,
|
|
117
|
-
{
|
|
118
|
-
[isMiddlewareFile ? "middleware" : "module"]: [
|
|
119
|
-
`${path.relative(baseDir, filepath)}:${exportName}`,
|
|
120
|
-
],
|
|
121
|
-
},
|
|
122
|
-
]);
|
|
123
|
-
}
|
|
61
|
+
if (routePath === ".") {
|
|
62
|
+
routePath = "";
|
|
124
63
|
}
|
|
125
|
-
|
|
126
|
-
|
|
64
|
+
|
|
65
|
+
routePath = `${baseURL}/${routePath}`;
|
|
66
|
+
|
|
67
|
+
routePath = routePath.replace(/\[\[(.+)]]/g, ":$1*"); // transform [[id]] => :id*
|
|
68
|
+
routePath = routePath.replace(/\[(.+)]/g, ":$1"); // transform [id] => :id
|
|
69
|
+
|
|
70
|
+
const routeEntry: RouteConfig = {
|
|
71
|
+
routePath: toUrlPath(routePath),
|
|
72
|
+
method: method.toUpperCase() as HTTPMethod,
|
|
73
|
+
[isMiddlewareFile ? "middleware" : "module"]: [
|
|
74
|
+
`${path.relative(baseDir, filepath)}:${exportName}`,
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
routeEntries.push(routeEntry);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
127
81
|
}
|
|
128
82
|
});
|
|
129
|
-
|
|
130
83
|
// Combine together any routes (index routes) which contain both a module and a middleware
|
|
131
84
|
routeEntries = routeEntries.reduce(
|
|
132
|
-
(acc: typeof routeEntries,
|
|
85
|
+
(acc: typeof routeEntries, { routePath, ...rest }) => {
|
|
133
86
|
const existingRouteEntry = acc.find(
|
|
134
|
-
(routeEntry) =>
|
|
87
|
+
(routeEntry) =>
|
|
88
|
+
routeEntry.routePath === routePath &&
|
|
89
|
+
routeEntry.method === rest.method
|
|
135
90
|
);
|
|
136
91
|
if (existingRouteEntry !== undefined) {
|
|
137
|
-
existingRouteEntry
|
|
138
|
-
...existingRouteEntry[1],
|
|
139
|
-
...routeHandler,
|
|
140
|
-
};
|
|
92
|
+
Object.assign(existingRouteEntry, rest);
|
|
141
93
|
} else {
|
|
142
|
-
acc.push(
|
|
94
|
+
acc.push({ routePath, ...rest });
|
|
143
95
|
}
|
|
144
96
|
return acc;
|
|
145
97
|
},
|
|
146
98
|
[]
|
|
147
99
|
);
|
|
148
100
|
|
|
149
|
-
routeEntries.sort((
|
|
101
|
+
routeEntries.sort((a, b) => compareRoutes(a, b));
|
|
150
102
|
|
|
151
|
-
return {
|
|
103
|
+
return {
|
|
104
|
+
routes: routeEntries,
|
|
105
|
+
};
|
|
152
106
|
}
|
|
153
107
|
|
|
154
108
|
// Ensure routes are produced in order of precedence so that
|
|
155
109
|
// more specific routes aren't occluded from matching due to
|
|
156
110
|
// less specific routes appearing first in the route list.
|
|
157
|
-
export function compareRoutes(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const segments = segmentedPath.slice(1).split("/").filter(Boolean);
|
|
164
|
-
return [method, segments];
|
|
111
|
+
export function compareRoutes(
|
|
112
|
+
{ routePath: routePathA, method: methodA }: RouteConfig,
|
|
113
|
+
{ routePath: routePathB, method: methodB }: RouteConfig
|
|
114
|
+
) {
|
|
115
|
+
function parseRoutePath(routePath: UrlPath): string[] {
|
|
116
|
+
return routePath.slice(1).split("/").filter(Boolean);
|
|
165
117
|
}
|
|
166
118
|
|
|
167
|
-
const
|
|
168
|
-
const
|
|
119
|
+
const segmentsA = parseRoutePath(routePathA);
|
|
120
|
+
const segmentsB = parseRoutePath(routePathB);
|
|
169
121
|
|
|
170
122
|
// sort routes with fewer segments after those with more segments
|
|
171
123
|
if (segmentsA.length !== segmentsB.length) {
|
|
@@ -191,8 +143,8 @@ export function compareRoutes(a: string, b: string) {
|
|
|
191
143
|
if (methodA && !methodB) return -1;
|
|
192
144
|
if (!methodA && methodB) return 1;
|
|
193
145
|
|
|
194
|
-
// all else equal, just sort
|
|
195
|
-
return
|
|
146
|
+
// all else equal, just sort the paths lexicographically
|
|
147
|
+
return routePathA.localeCompare(routePathB);
|
|
196
148
|
}
|
|
197
149
|
|
|
198
150
|
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
|
|