wrangler 0.0.0-ece06ea → 0.0.0-ef13735
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 +35 -21
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +66 -0
- package/pages/functions/filepath-routing.ts +219 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +151 -0
- package/pages/functions/template-worker.ts +130 -0
- package/src/__tests__/dev.test.tsx +73 -0
- package/src/__tests__/guess-worker-format.test.ts +51 -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__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +56 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-user.ts +27 -0
- package/src/__tests__/helpers/run-in-tmp.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +14 -0
- package/src/__tests__/index.test.ts +715 -119
- package/src/__tests__/jest.setup.ts +37 -0
- package/src/__tests__/kv.test.ts +1139 -0
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/publish.test.ts +1381 -0
- package/src/__tests__/secret.test.ts +210 -0
- package/src/__tests__/whoami.test.tsx +86 -0
- package/src/api/form_data.ts +93 -100
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +55 -47
- package/src/cfetch/index.ts +102 -0
- package/src/cfetch/internal.ts +101 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +641 -112
- package/src/dev.tsx +476 -355
- package/src/dialogs.tsx +6 -14
- package/src/guess-worker-format.ts +66 -0
- package/src/index.tsx +828 -496
- package/src/inspect.ts +596 -0
- package/src/kv.tsx +118 -45
- package/src/module-collection.ts +63 -0
- package/src/open-in-browser.ts +13 -0
- package/src/package-manager.ts +120 -0
- package/src/pages.tsx +915 -215
- package/src/paths.ts +26 -0
- package/src/proxy.ts +370 -0
- package/src/publish.ts +349 -290
- package/src/sites.tsx +184 -55
- package/src/tail.tsx +5 -3
- package/src/user.tsx +81 -91
- 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/wrangler-dist/cli.js +119231 -130682
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/inspect.ts +0 -441
- package/src/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
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-ef13735",
|
|
4
4
|
"author": "wrangler@cloudflare.com",
|
|
5
5
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
6
6
|
"bin": {
|
|
@@ -36,74 +36,85 @@
|
|
|
36
36
|
"cli"
|
|
37
37
|
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
39
|
+
"esbuild": "0.14.14",
|
|
40
|
+
"miniflare": "2.3.0",
|
|
41
|
+
"path-to-regexp": "^6.2.0",
|
|
42
42
|
"semiver": "^1.1.0",
|
|
43
|
-
"
|
|
44
|
-
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
|
45
|
-
"@esbuild-plugins/node-modules-polyfill": "^0.1.2"
|
|
43
|
+
"xxhash-wasm": "^1.0.1"
|
|
46
44
|
},
|
|
47
45
|
"optionalDependencies": {
|
|
48
46
|
"fsevents": "~2.3.2"
|
|
49
47
|
},
|
|
50
48
|
"devDependencies": {
|
|
49
|
+
"@babel/types": "^7.16.0",
|
|
51
50
|
"@iarna/toml": "^2.2.5",
|
|
52
|
-
"@types/
|
|
53
|
-
"@types/
|
|
51
|
+
"@types/estree": "^0.0.50",
|
|
52
|
+
"@types/mime": "^2.0.3",
|
|
54
53
|
"@types/react": "^17.0.37",
|
|
54
|
+
"@types/serve-static": "^1.13.10",
|
|
55
55
|
"@types/signal-exit": "^3.0.1",
|
|
56
56
|
"@types/ws": "^8.2.1",
|
|
57
57
|
"@types/yargs": "^17.0.7",
|
|
58
|
+
"acorn": "^8.6.0",
|
|
59
|
+
"acorn-walk": "^8.2.0",
|
|
60
|
+
"chokidar": "^3.5.2",
|
|
58
61
|
"clipboardy": "^3.0.0",
|
|
62
|
+
"cmd-shim": "^4.1.0",
|
|
59
63
|
"command-exists": "^1.2.9",
|
|
64
|
+
"devtools-protocol": "^0.0.955664",
|
|
60
65
|
"execa": "^6.0.0",
|
|
61
|
-
"
|
|
66
|
+
"faye-websocket": "^0.11.4",
|
|
62
67
|
"finalhandler": "^1.1.2",
|
|
63
68
|
"find-up": "^6.2.0",
|
|
64
|
-
"
|
|
65
|
-
"http-proxy": "^1.18.1",
|
|
66
|
-
"http-proxy-middleware": "^2.0.1",
|
|
69
|
+
"ignore": "^5.2.0",
|
|
67
70
|
"ink": "^3.2.0",
|
|
68
71
|
"ink-select-input": "^4.2.1",
|
|
69
72
|
"ink-table": "^3.0.0",
|
|
73
|
+
"ink-testing-library": "^2.1.0",
|
|
70
74
|
"ink-text-input": "^4.0.2",
|
|
71
|
-
"
|
|
75
|
+
"jest-fetch-mock": "^3.0.3",
|
|
76
|
+
"mime": "^3.0.0",
|
|
72
77
|
"open": "^8.4.0",
|
|
73
|
-
"path-to-regexp": "^6.2.0",
|
|
74
78
|
"react": "^17.0.2",
|
|
79
|
+
"react-error-boundary": "^3.1.4",
|
|
75
80
|
"serve-static": "^1.14.1",
|
|
76
81
|
"signal-exit": "^3.0.6",
|
|
77
82
|
"tmp-promise": "^3.0.3",
|
|
83
|
+
"undici": "4.13.0",
|
|
78
84
|
"ws": "^8.3.0",
|
|
79
85
|
"yargs": "^17.3.0"
|
|
80
86
|
},
|
|
81
87
|
"files": [
|
|
82
88
|
"src",
|
|
83
89
|
"bin",
|
|
90
|
+
"pages",
|
|
84
91
|
"miniflare-config-stubs",
|
|
85
92
|
"wrangler-dist",
|
|
86
|
-
"
|
|
93
|
+
"templates",
|
|
87
94
|
"vendor",
|
|
88
95
|
"import_meta_url.js"
|
|
89
96
|
],
|
|
90
97
|
"scripts": {
|
|
91
98
|
"clean": "rm -rf wrangler-dist",
|
|
99
|
+
"check:type": "tsc",
|
|
92
100
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
93
101
|
"build": "npm run clean && npm run bundle",
|
|
94
102
|
"start": "npm run bundle && NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
95
|
-
"test": "
|
|
103
|
+
"test": "jest --silent=false --verbose=true",
|
|
104
|
+
"test-watch": "npm run test -- --runInBand --testTimeout=50000 --watch"
|
|
96
105
|
},
|
|
97
106
|
"engines": {
|
|
98
107
|
"node": ">=16.7.0"
|
|
99
108
|
},
|
|
100
109
|
"jest": {
|
|
110
|
+
"restoreMocks": true,
|
|
111
|
+
"testTimeout": 30000,
|
|
101
112
|
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
|
|
102
113
|
"transformIgnorePatterns": [
|
|
103
|
-
"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)"
|
|
104
115
|
],
|
|
105
116
|
"moduleNameMapper": {
|
|
106
|
-
"clipboardy": "<rootDir>/src/__tests__/clipboardy-mock.js"
|
|
117
|
+
"clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js"
|
|
107
118
|
},
|
|
108
119
|
"transform": {
|
|
109
120
|
"^.+\\.c?(t|j)sx?$": [
|
|
@@ -112,6 +123,9 @@
|
|
|
112
123
|
"sourcemap": true
|
|
113
124
|
}
|
|
114
125
|
]
|
|
115
|
-
}
|
|
126
|
+
},
|
|
127
|
+
"setupFilesAfterEnv": [
|
|
128
|
+
"<rootDir>/src/__tests__/jest.setup.ts"
|
|
129
|
+
]
|
|
116
130
|
}
|
|
117
|
-
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { build } from "esbuild";
|
|
3
|
+
|
|
4
|
+
type Options = {
|
|
5
|
+
routesModule: string;
|
|
6
|
+
outfile: string;
|
|
7
|
+
minify?: boolean;
|
|
8
|
+
sourcemap?: boolean;
|
|
9
|
+
fallbackService?: string;
|
|
10
|
+
watch?: boolean;
|
|
11
|
+
onEnd?: () => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function buildWorker({
|
|
15
|
+
routesModule,
|
|
16
|
+
outfile = "bundle.js",
|
|
17
|
+
minify = false,
|
|
18
|
+
sourcemap = false,
|
|
19
|
+
fallbackService = "ASSETS",
|
|
20
|
+
watch = false,
|
|
21
|
+
onEnd = () => {},
|
|
22
|
+
}: Options) {
|
|
23
|
+
return build({
|
|
24
|
+
entryPoints: [
|
|
25
|
+
path.resolve(__dirname, "../pages/functions/template-worker.ts"),
|
|
26
|
+
],
|
|
27
|
+
inject: [routesModule],
|
|
28
|
+
bundle: true,
|
|
29
|
+
format: "esm",
|
|
30
|
+
target: "esnext",
|
|
31
|
+
outfile,
|
|
32
|
+
minify,
|
|
33
|
+
sourcemap,
|
|
34
|
+
watch,
|
|
35
|
+
allowOverwrite: true,
|
|
36
|
+
define: {
|
|
37
|
+
__FALLBACK_SERVICE__: JSON.stringify(fallbackService),
|
|
38
|
+
},
|
|
39
|
+
plugins: [
|
|
40
|
+
{
|
|
41
|
+
name: "wrangler notifier and monitor",
|
|
42
|
+
setup(pluginBuild) {
|
|
43
|
+
pluginBuild.onEnd((result) => {
|
|
44
|
+
if (result.errors.length > 0) {
|
|
45
|
+
console.error(
|
|
46
|
+
`${result.errors.length} error(s) and ${result.warnings.length} warning(s) when compiling Worker.`
|
|
47
|
+
);
|
|
48
|
+
} else if (result.warnings.length > 0) {
|
|
49
|
+
console.warn(
|
|
50
|
+
`${result.warnings.length} warning(s) when compiling Worker.`
|
|
51
|
+
);
|
|
52
|
+
onEnd();
|
|
53
|
+
} else {
|
|
54
|
+
console.log("Compiled Worker successfully.");
|
|
55
|
+
onEnd();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { toUrlPath } from "../../src/paths";
|
|
2
|
+
import { compareRoutes } from "./filepath-routing";
|
|
3
|
+
import type { HTTPMethod, RouteConfig } from "./routes";
|
|
4
|
+
|
|
5
|
+
describe("compareRoutes()", () => {
|
|
6
|
+
test("routes / last", () => {
|
|
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);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("routes with fewer segments come after those with more segments", () => {
|
|
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);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("routes with wildcard segments come after those without", () => {
|
|
28
|
+
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/foo"))).toBe(1);
|
|
29
|
+
expect(compareRoutes(routeConfig("/:foo*"), routeConfig("/:foo"))).toBe(1);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("routes with dynamic segments come after those without", () => {
|
|
33
|
+
expect(compareRoutes(routeConfig("/:foo"), routeConfig("/foo"))).toBe(1);
|
|
34
|
+
});
|
|
35
|
+
|
|
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);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("routes with no HTTP method come after those specifying a method", () => {
|
|
43
|
+
expect(compareRoutes(routeConfig("/foo"), routeConfig("/foo", "GET"))).toBe(
|
|
44
|
+
1
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("two equal routes are sorted according to their original position in the list", () => {
|
|
49
|
+
expect(
|
|
50
|
+
compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo", "GET"))
|
|
51
|
+
).toBe(0);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("it returns -1 if the first argument should appear first in the list", () => {
|
|
55
|
+
expect(compareRoutes(routeConfig("/foo", "GET"), routeConfig("/foo"))).toBe(
|
|
56
|
+
-1
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
function routeConfig(routePath: string, method?: string): RouteConfig {
|
|
62
|
+
return {
|
|
63
|
+
routePath: toUrlPath(routePath),
|
|
64
|
+
method: method as HTTPMethod,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import * as acorn from "acorn";
|
|
4
|
+
import * as acornWalk from "acorn-walk";
|
|
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";
|
|
9
|
+
import type { ExportNamedDeclaration, Identifier } from "estree";
|
|
10
|
+
|
|
11
|
+
export async function generateConfigFromFileTree({
|
|
12
|
+
baseDir,
|
|
13
|
+
baseURL,
|
|
14
|
+
}: {
|
|
15
|
+
baseDir: string;
|
|
16
|
+
baseURL: UrlPath;
|
|
17
|
+
}) {
|
|
18
|
+
let routeEntries: RouteConfig[] = [];
|
|
19
|
+
|
|
20
|
+
if (!baseURL.startsWith("/")) {
|
|
21
|
+
baseURL = `/${baseURL}` as UrlPath;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (baseURL.endsWith("/")) {
|
|
25
|
+
baseURL = baseURL.slice(0, -1) as UrlPath;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await forEachFile(baseDir, async (filepath) => {
|
|
29
|
+
const ext = path.extname(filepath);
|
|
30
|
+
if (/^\.(mjs|js|ts|tsx|jsx)$/.test(ext)) {
|
|
31
|
+
// transform the code to ensure we're working with vanilla JS + ESM
|
|
32
|
+
const { code } = await transform(await fs.readFile(filepath, "utf-8"), {
|
|
33
|
+
loader: ext === ".ts" ? "ts" : "js",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// parse each file into an AST and search for module exports that match "onRequest" and friends
|
|
37
|
+
const ast = acorn.parse(code, {
|
|
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" && 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
|
+
}
|
|
69
|
+
|
|
70
|
+
// `export {foo, bar}`
|
|
71
|
+
if (node.specifiers.length) {
|
|
72
|
+
exportNames.push(
|
|
73
|
+
...node.specifiers.map(
|
|
74
|
+
(exportSpecifier) =>
|
|
75
|
+
(exportSpecifier.exported as unknown as Identifier).name
|
|
76
|
+
)
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const exportName of exportNames) {
|
|
81
|
+
const [match, method = ""] = (exportName.match(
|
|
82
|
+
/^onRequest(Get|Post|Put|Patch|Delete|Options|Head)?$/
|
|
83
|
+
) ?? []) as (string | undefined)[];
|
|
84
|
+
|
|
85
|
+
if (match) {
|
|
86
|
+
const basename = path.basename(filepath).slice(0, -ext.length);
|
|
87
|
+
|
|
88
|
+
const isIndexFile = basename === "index";
|
|
89
|
+
// TODO: deprecate _middleware_ in favor of _middleware
|
|
90
|
+
const isMiddlewareFile =
|
|
91
|
+
basename === "_middleware" || basename === "_middleware_";
|
|
92
|
+
|
|
93
|
+
let routePath = path
|
|
94
|
+
.relative(baseDir, filepath)
|
|
95
|
+
.slice(0, -ext.length);
|
|
96
|
+
|
|
97
|
+
if (isIndexFile || isMiddlewareFile) {
|
|
98
|
+
routePath = path.dirname(routePath);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (routePath === ".") {
|
|
102
|
+
routePath = "";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
routePath = `${baseURL}/${routePath}`;
|
|
106
|
+
|
|
107
|
+
routePath = routePath.replace(/\[\[(.+)]]/g, ":$1*"); // transform [[id]] => :id*
|
|
108
|
+
routePath = routePath.replace(/\[(.+)]/g, ":$1"); // transform [id] => :id
|
|
109
|
+
|
|
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
|
+
};
|
|
117
|
+
|
|
118
|
+
routeEntries.push(routeEntry);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Combine together any routes (index routes) which contain both a module and a middleware
|
|
127
|
+
routeEntries = routeEntries.reduce(
|
|
128
|
+
(acc: typeof routeEntries, { routePath, ...rest }) => {
|
|
129
|
+
const existingRouteEntry = acc.find(
|
|
130
|
+
(routeEntry) => routeEntry.routePath === routePath
|
|
131
|
+
);
|
|
132
|
+
if (existingRouteEntry !== undefined) {
|
|
133
|
+
Object.assign(existingRouteEntry, rest);
|
|
134
|
+
} else {
|
|
135
|
+
acc.push({ routePath, ...rest });
|
|
136
|
+
}
|
|
137
|
+
return acc;
|
|
138
|
+
},
|
|
139
|
+
[]
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
routeEntries.sort((a, b) => compareRoutes(a, b));
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
routes: routeEntries,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Ensure routes are produced in order of precedence so that
|
|
150
|
+
// more specific routes aren't occluded from matching due to
|
|
151
|
+
// less specific routes appearing first in the route list.
|
|
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);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const segmentsA = parseRoutePath(routePathA);
|
|
161
|
+
const segmentsB = parseRoutePath(routePathB);
|
|
162
|
+
|
|
163
|
+
// sort routes with fewer segments after those with more segments
|
|
164
|
+
if (segmentsA.length !== segmentsB.length) {
|
|
165
|
+
return segmentsB.length - segmentsA.length;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
for (let i = 0; i < segmentsA.length; i++) {
|
|
169
|
+
const isWildcardA = segmentsA[i].includes("*");
|
|
170
|
+
const isWildcardB = segmentsB[i].includes("*");
|
|
171
|
+
const isParamA = segmentsA[i].includes(":");
|
|
172
|
+
const isParamB = segmentsB[i].includes(":");
|
|
173
|
+
|
|
174
|
+
// sort wildcard segments after non-wildcard segments
|
|
175
|
+
if (isWildcardA && !isWildcardB) return 1;
|
|
176
|
+
if (!isWildcardA && isWildcardB) return -1;
|
|
177
|
+
|
|
178
|
+
// sort dynamic param segments after non-param segments
|
|
179
|
+
if (isParamA && !isParamB) return 1;
|
|
180
|
+
if (!isParamA && isParamB) return -1;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// sort routes that specify an HTTP before those that don't
|
|
184
|
+
if (methodA && !methodB) return -1;
|
|
185
|
+
if (!methodA && methodB) return 1;
|
|
186
|
+
|
|
187
|
+
// all else equal, just sort the paths lexicographically
|
|
188
|
+
return routePathA.localeCompare(routePathB);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function forEachFile<T>(
|
|
192
|
+
baseDir: string,
|
|
193
|
+
fn: (filepath: string) => T | Promise<T>
|
|
194
|
+
) {
|
|
195
|
+
const searchPaths = [baseDir];
|
|
196
|
+
const returnValues: T[] = [];
|
|
197
|
+
|
|
198
|
+
while (isNotEmpty(searchPaths)) {
|
|
199
|
+
const cwd = searchPaths.shift();
|
|
200
|
+
const dir = await fs.readdir(cwd, { withFileTypes: true });
|
|
201
|
+
for (const entry of dir) {
|
|
202
|
+
const pathname = path.join(cwd, entry.name);
|
|
203
|
+
if (entry.isDirectory()) {
|
|
204
|
+
searchPaths.push(pathname);
|
|
205
|
+
} else if (entry.isFile()) {
|
|
206
|
+
returnValues.push(await fn(pathname));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return returnValues;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
interface NonEmptyArray<T> extends Array<T> {
|
|
215
|
+
shift(): T;
|
|
216
|
+
}
|
|
217
|
+
function isNotEmpty<T>(array: T[]): array is NonEmptyArray<T> {
|
|
218
|
+
return array.length > 0;
|
|
219
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export const RESERVED_KEYWORDS = [
|
|
2
|
+
"do",
|
|
3
|
+
"if",
|
|
4
|
+
"in",
|
|
5
|
+
"for",
|
|
6
|
+
"let",
|
|
7
|
+
"new",
|
|
8
|
+
"try",
|
|
9
|
+
"var",
|
|
10
|
+
"case",
|
|
11
|
+
"else",
|
|
12
|
+
"enum",
|
|
13
|
+
"eval",
|
|
14
|
+
"null",
|
|
15
|
+
"this",
|
|
16
|
+
"true",
|
|
17
|
+
"void",
|
|
18
|
+
"with",
|
|
19
|
+
"await",
|
|
20
|
+
"break",
|
|
21
|
+
"catch",
|
|
22
|
+
"class",
|
|
23
|
+
"const",
|
|
24
|
+
"false",
|
|
25
|
+
"super",
|
|
26
|
+
"throw",
|
|
27
|
+
"while",
|
|
28
|
+
"yield",
|
|
29
|
+
"delete",
|
|
30
|
+
"export",
|
|
31
|
+
"import",
|
|
32
|
+
"public",
|
|
33
|
+
"return",
|
|
34
|
+
"static",
|
|
35
|
+
"switch",
|
|
36
|
+
"typeof",
|
|
37
|
+
"default",
|
|
38
|
+
"extends",
|
|
39
|
+
"finally",
|
|
40
|
+
"package",
|
|
41
|
+
"private",
|
|
42
|
+
"continue",
|
|
43
|
+
"debugger",
|
|
44
|
+
"function",
|
|
45
|
+
"arguments",
|
|
46
|
+
"interface",
|
|
47
|
+
"protected",
|
|
48
|
+
"implements",
|
|
49
|
+
"instanceof",
|
|
50
|
+
"NaN",
|
|
51
|
+
"Infinity",
|
|
52
|
+
"undefined",
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
export const reservedKeywordRegex = new RegExp(
|
|
56
|
+
`^${RESERVED_KEYWORDS.join("|")}$`
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
export const identifierStartRegex = /[$_\p{ID_Start}]/u;
|
|
60
|
+
|
|
61
|
+
export const identifierPartRegex = /[$_\u200C\u200D\p{ID_Continue}]/u;
|
|
62
|
+
|
|
63
|
+
export const identifierNameRegex =
|
|
64
|
+
/^(?:[$_\p{ID_Start}])(?:[$_\u200C\u200D\p{ID_Continue}])*$/u;
|
|
65
|
+
|
|
66
|
+
export const validIdentifierRegex = new RegExp(
|
|
67
|
+
`(?!(${reservedKeywordRegex.source})$)${identifierNameRegex.source}`,
|
|
68
|
+
"u"
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
export const isValidIdentifier = (identifier: string) =>
|
|
72
|
+
validIdentifierRegex.test(identifier);
|
|
73
|
+
|
|
74
|
+
export const normalizeIdentifier = (identifier: string) =>
|
|
75
|
+
identifier.replace(
|
|
76
|
+
/(?:^[^$_\p{ID_Start}])|[^$_\u200C\u200D\p{ID_Continue}]/gu,
|
|
77
|
+
"_"
|
|
78
|
+
);
|