wrangler 0.0.0-e1c615f → 0.0.0-e3cab74
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 +28 -13
- package/pages/functions/buildWorker.ts +6 -1
- package/pages/functions/filepath-routing.test.ts +112 -28
- package/pages/functions/filepath-routing.ts +88 -131
- package/pages/functions/routes.ts +11 -18
- package/pages/functions/template-worker.ts +21 -28
- package/src/__tests__/configuration.test.ts +2185 -0
- package/src/__tests__/dev.test.tsx +56 -9
- 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} +53 -17
- package/src/__tests__/helpers/mock-console.ts +62 -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 +37 -2
- package/src/__tests__/kv.test.ts +441 -398
- 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 +2406 -0
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/secret.test.ts +326 -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 +34 -15
- package/src/api/worker.ts +58 -27
- package/src/bundle.ts +133 -0
- package/src/cfetch/index.ts +21 -19
- package/src/cfetch/internal.ts +85 -21
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +265 -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 +1017 -0
- package/src/dev.tsx +455 -368
- 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 +1044 -646
- package/src/inspect.ts +609 -0
- package/src/kv.tsx +47 -39
- 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 +139 -62
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +350 -77
- package/src/publish.ts +307 -289
- package/src/r2.ts +50 -0
- package/src/reporting.ts +115 -0
- package/src/sites.tsx +183 -56
- 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 +137 -110
- 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 +127861 -111486
- 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/api/inspect.ts +0 -430
- package/src/config.ts +0 -131
- 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-e3cab74",
|
|
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,37 +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",
|
|
68
|
+
"devtools-protocol": "^0.0.955664",
|
|
62
69
|
"execa": "^6.0.0",
|
|
63
70
|
"faye-websocket": "^0.11.4",
|
|
64
71
|
"finalhandler": "^1.1.2",
|
|
65
72
|
"find-up": "^6.2.0",
|
|
66
|
-
"
|
|
73
|
+
"get-port": "^6.1.2",
|
|
74
|
+
"glob-to-regexp": "0.4.1",
|
|
75
|
+
"ignore": "^5.2.0",
|
|
67
76
|
"ink": "^3.2.0",
|
|
68
77
|
"ink-select-input": "^4.2.1",
|
|
69
78
|
"ink-table": "^3.0.0",
|
|
70
79
|
"ink-testing-library": "^2.1.0",
|
|
71
80
|
"ink-text-input": "^4.0.2",
|
|
81
|
+
"jest-fetch-mock": "^3.0.3",
|
|
82
|
+
"jest-websocket-mock": "^2.3.0",
|
|
72
83
|
"mime": "^3.0.0",
|
|
73
|
-
"node-fetch": "^3.1.0",
|
|
74
84
|
"open": "^8.4.0",
|
|
85
|
+
"prompts": "^2.4.2",
|
|
75
86
|
"react": "^17.0.2",
|
|
76
87
|
"react-error-boundary": "^3.1.4",
|
|
77
88
|
"serve-static": "^1.14.1",
|
|
78
89
|
"signal-exit": "^3.0.6",
|
|
79
90
|
"tmp-promise": "^3.0.3",
|
|
80
|
-
"undici": "
|
|
91
|
+
"undici": "4.13.0",
|
|
81
92
|
"ws": "^8.3.0",
|
|
82
93
|
"yargs": "^17.3.0"
|
|
83
94
|
},
|
|
@@ -87,28 +98,32 @@
|
|
|
87
98
|
"pages",
|
|
88
99
|
"miniflare-config-stubs",
|
|
89
100
|
"wrangler-dist",
|
|
90
|
-
"
|
|
101
|
+
"templates",
|
|
91
102
|
"vendor",
|
|
92
103
|
"import_meta_url.js"
|
|
93
104
|
],
|
|
94
105
|
"scripts": {
|
|
95
106
|
"clean": "rm -rf wrangler-dist",
|
|
107
|
+
"check:type": "tsc",
|
|
96
108
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
97
109
|
"build": "npm run clean && npm run bundle",
|
|
110
|
+
"prepublishOnly": "npm run build",
|
|
98
111
|
"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
99
|
-
"test": "
|
|
112
|
+
"test": "jest --silent=false --verbose=true",
|
|
113
|
+
"test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
|
|
100
114
|
},
|
|
101
115
|
"engines": {
|
|
102
116
|
"node": ">=16.7.0"
|
|
103
117
|
},
|
|
104
118
|
"jest": {
|
|
105
119
|
"restoreMocks": true,
|
|
120
|
+
"testTimeout": 30000,
|
|
106
121
|
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
|
|
107
122
|
"transformIgnorePatterns": [
|
|
108
|
-
"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)"
|
|
109
124
|
],
|
|
110
125
|
"moduleNameMapper": {
|
|
111
|
-
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
|
|
126
|
+
"clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
|
|
112
127
|
},
|
|
113
128
|
"transform": {
|
|
114
129
|
"^.+\\.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 = {
|
|
@@ -6,6 +6,7 @@ type Options = {
|
|
|
6
6
|
outfile: string;
|
|
7
7
|
minify?: boolean;
|
|
8
8
|
sourcemap?: boolean;
|
|
9
|
+
fallbackService?: string;
|
|
9
10
|
watch?: boolean;
|
|
10
11
|
onEnd?: () => void;
|
|
11
12
|
};
|
|
@@ -15,6 +16,7 @@ export function buildWorker({
|
|
|
15
16
|
outfile = "bundle.js",
|
|
16
17
|
minify = false,
|
|
17
18
|
sourcemap = false,
|
|
19
|
+
fallbackService = "ASSETS",
|
|
18
20
|
watch = false,
|
|
19
21
|
onEnd = () => {},
|
|
20
22
|
}: Options) {
|
|
@@ -31,6 +33,9 @@ export function buildWorker({
|
|
|
31
33
|
sourcemap,
|
|
32
34
|
watch,
|
|
33
35
|
allowOverwrite: true,
|
|
36
|
+
define: {
|
|
37
|
+
__FALLBACK_SERVICE__: JSON.stringify(fallbackService),
|
|
38
|
+
},
|
|
34
39
|
plugins: [
|
|
35
40
|
{
|
|
36
41
|
name: "wrangler notifier and monitor",
|
|
@@ -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,173 +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
|
-
ecmaVersion: "latest",
|
|
39
|
-
sourceType: "module",
|
|
40
|
-
});
|
|
41
|
-
acornWalk.simple(ast, {
|
|
42
|
-
ExportNamedDeclaration(_node: unknown) {
|
|
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") {
|
|
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);
|
|
69
47
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
);
|
|
48
|
+
const isIndexFile = basename === "index";
|
|
49
|
+
// TODO: deprecate _middleware_ in favor of _middleware
|
|
50
|
+
const isMiddlewareFile =
|
|
51
|
+
basename === "_middleware" || basename === "_middleware_";
|
|
52
|
+
|
|
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
|
-
}
|
|
164
|
-
|
|
165
|
-
const segments = segmentedPath.slice(1).split("/").filter(Boolean);
|
|
166
|
-
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);
|
|
167
117
|
}
|
|
168
118
|
|
|
169
|
-
const
|
|
170
|
-
const
|
|
119
|
+
const segmentsA = parseRoutePath(routePathA);
|
|
120
|
+
const segmentsB = parseRoutePath(routePathB);
|
|
171
121
|
|
|
172
122
|
// sort routes with fewer segments after those with more segments
|
|
173
123
|
if (segmentsA.length !== segmentsB.length) {
|
|
@@ -193,8 +143,8 @@ export function compareRoutes(a: string, b: string) {
|
|
|
193
143
|
if (methodA && !methodB) return -1;
|
|
194
144
|
if (!methodA && methodB) return 1;
|
|
195
145
|
|
|
196
|
-
// all else equal, just sort
|
|
197
|
-
return
|
|
146
|
+
// all else equal, just sort the paths lexicographically
|
|
147
|
+
return routePathA.localeCompare(routePathB);
|
|
198
148
|
}
|
|
199
149
|
|
|
200
150
|
async function forEachFile<T>(
|
|
@@ -204,7 +154,7 @@ async function forEachFile<T>(
|
|
|
204
154
|
const searchPaths = [baseDir];
|
|
205
155
|
const returnValues: T[] = [];
|
|
206
156
|
|
|
207
|
-
while (searchPaths
|
|
157
|
+
while (isNotEmpty(searchPaths)) {
|
|
208
158
|
const cwd = searchPaths.shift();
|
|
209
159
|
const dir = await fs.readdir(cwd, { withFileTypes: true });
|
|
210
160
|
for (const entry of dir) {
|
|
@@ -219,3 +169,10 @@ async function forEachFile<T>(
|
|
|
219
169
|
|
|
220
170
|
return returnValues;
|
|
221
171
|
}
|
|
172
|
+
|
|
173
|
+
interface NonEmptyArray<T> extends Array<T> {
|
|
174
|
+
shift(): T;
|
|
175
|
+
}
|
|
176
|
+
function isNotEmpty<T>(array: T[]): array is NonEmptyArray<T> {
|
|
177
|
+
return array.length > 0;
|
|
178
|
+
}
|
|
@@ -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
|
},`
|