wrangler 0.0.0-e14e589 → 0.0.0-e1c615f
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/README.md +51 -0
- package/package.json +39 -26
- package/pages/functions/buildWorker.ts +57 -0
- package/pages/functions/filepath-routing.test.ts +39 -0
- package/pages/functions/filepath-routing.ts +221 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +158 -0
- package/pages/functions/template-worker.ts +137 -0
- package/src/__tests__/dev.test.tsx +66 -0
- package/src/__tests__/index.test.ts +254 -120
- package/src/__tests__/jest.setup.ts +22 -0
- package/src/__tests__/kv.test.ts +1098 -0
- package/src/__tests__/mock-cfetch.ts +171 -0
- package/src/__tests__/mock-dialogs.ts +65 -0
- package/src/__tests__/run-in-tmp.ts +19 -0
- package/src/__tests__/run-wrangler.ts +32 -0
- package/src/api/form_data.ts +78 -72
- package/src/api/inspect.ts +0 -11
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +46 -31
- package/src/cfetch/index.ts +102 -0
- package/src/cfetch/internal.ts +69 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +39 -4
- package/src/dev.tsx +332 -128
- package/src/dialogs.tsx +4 -12
- package/src/index.tsx +397 -222
- package/src/kv.tsx +101 -45
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +839 -211
- package/src/proxy.ts +103 -0
- package/src/publish.ts +117 -45
- package/src/sites.tsx +6 -7
- package/src/tail.tsx +6 -4
- package/src/user.tsx +9 -16
- package/wrangler-dist/cli.js +123794 -528
- 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/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
import { isValidIdentifier, normalizeIdentifier } from "./identifiers";
|
|
4
|
+
|
|
5
|
+
export const HTTP_METHODS = [
|
|
6
|
+
"HEAD",
|
|
7
|
+
"OPTIONS",
|
|
8
|
+
"GET",
|
|
9
|
+
"POST",
|
|
10
|
+
"PUT",
|
|
11
|
+
"PATCH",
|
|
12
|
+
"DELETE",
|
|
13
|
+
] as const;
|
|
14
|
+
export type HTTPMethod = typeof HTTP_METHODS[number];
|
|
15
|
+
export function isHTTPMethod(
|
|
16
|
+
maybeHTTPMethod: string
|
|
17
|
+
): maybeHTTPMethod is HTTPMethod {
|
|
18
|
+
return (HTTP_METHODS as readonly string[]).includes(maybeHTTPMethod);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type RoutesCollection = Array<{
|
|
22
|
+
routePath: string;
|
|
23
|
+
methods: HTTPMethod[];
|
|
24
|
+
modules: string[];
|
|
25
|
+
middlewares: string[];
|
|
26
|
+
}>;
|
|
27
|
+
|
|
28
|
+
export type Config = {
|
|
29
|
+
routes?: RoutesConfig;
|
|
30
|
+
schedules?: unknown;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type RoutesConfig = {
|
|
34
|
+
[route: string]: RouteConfig;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type RouteConfig = {
|
|
38
|
+
middleware?: string | string[];
|
|
39
|
+
module?: string | string[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type ImportMap = Map<
|
|
43
|
+
string,
|
|
44
|
+
{
|
|
45
|
+
filepath: string;
|
|
46
|
+
name: string;
|
|
47
|
+
identifier: string;
|
|
48
|
+
}
|
|
49
|
+
>;
|
|
50
|
+
|
|
51
|
+
type Arguments = {
|
|
52
|
+
config: Config;
|
|
53
|
+
outfile: string;
|
|
54
|
+
srcDir: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export async function writeRoutesModule({
|
|
58
|
+
config,
|
|
59
|
+
srcDir,
|
|
60
|
+
outfile = "_routes.js",
|
|
61
|
+
}: Arguments) {
|
|
62
|
+
const { importMap, routes } = parseConfig(config, srcDir);
|
|
63
|
+
const routesModule = generateRoutesModule(importMap, routes);
|
|
64
|
+
|
|
65
|
+
await fs.writeFile(outfile, routesModule);
|
|
66
|
+
|
|
67
|
+
return outfile;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function parseConfig(config: Config, baseDir: string) {
|
|
71
|
+
const routes: RoutesCollection = [];
|
|
72
|
+
const importMap: ImportMap = new Map();
|
|
73
|
+
const identifierCount = new Map<string, number>(); // to keep track of identifier collisions
|
|
74
|
+
|
|
75
|
+
function parseModuleIdentifiers(paths: string | string[] | undefined) {
|
|
76
|
+
if (typeof paths === "undefined") {
|
|
77
|
+
paths = [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (typeof paths === "string") {
|
|
81
|
+
paths = [paths];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return paths.map((modulePath) => {
|
|
85
|
+
const [filepath, name = "default"] = modulePath.split(":");
|
|
86
|
+
let { identifier } = importMap.get(modulePath) ?? {};
|
|
87
|
+
|
|
88
|
+
const resolvedPath = path.resolve(baseDir, filepath);
|
|
89
|
+
|
|
90
|
+
// ensure the filepath isn't attempting to resolve to anything outside of the project
|
|
91
|
+
if (path.relative(baseDir, resolvedPath).startsWith("..")) {
|
|
92
|
+
throw new Error(`Invalid module path "${filepath}"`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ensure the module name (if provided) is a valid identifier to guard against injection attacks
|
|
96
|
+
if (name !== "default" && !isValidIdentifier(name)) {
|
|
97
|
+
throw new Error(`Invalid module identifier "${name}"`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!identifier) {
|
|
101
|
+
identifier = normalizeIdentifier(`__${filepath}_${name}`);
|
|
102
|
+
|
|
103
|
+
let count = identifierCount.get(identifier) ?? 0;
|
|
104
|
+
identifierCount.set(identifier, ++count);
|
|
105
|
+
|
|
106
|
+
if (count > 1) {
|
|
107
|
+
identifier += `_${count}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
importMap.set(modulePath, { filepath: resolvedPath, name, identifier });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return identifier;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
for (const [route, props] of Object.entries(config.routes)) {
|
|
118
|
+
let [_methods, routePath] = route.split(" ");
|
|
119
|
+
if (!routePath) {
|
|
120
|
+
routePath = _methods;
|
|
121
|
+
_methods = "";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
routes.push({
|
|
125
|
+
routePath,
|
|
126
|
+
methods: _methods.split("|").filter(isHTTPMethod),
|
|
127
|
+
middlewares: parseModuleIdentifiers(props.middleware),
|
|
128
|
+
modules: parseModuleIdentifiers(props.module),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { routes, importMap };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function generateRoutesModule(
|
|
136
|
+
importMap: ImportMap,
|
|
137
|
+
routes: RoutesCollection
|
|
138
|
+
) {
|
|
139
|
+
return `${[...importMap.values()]
|
|
140
|
+
.map(
|
|
141
|
+
({ filepath, name, identifier }) =>
|
|
142
|
+
`import { ${name} as ${identifier} } from ${JSON.stringify(filepath)}`
|
|
143
|
+
)
|
|
144
|
+
.join("\n")}
|
|
145
|
+
|
|
146
|
+
export const routes = [
|
|
147
|
+
${routes
|
|
148
|
+
.map(
|
|
149
|
+
(route) => ` {
|
|
150
|
+
routePath: "${route.routePath}",
|
|
151
|
+
methods: ${JSON.stringify(route.methods)},
|
|
152
|
+
middlewares: [${route.middlewares.join(", ")}],
|
|
153
|
+
modules: [${route.modules.join(", ")}],
|
|
154
|
+
},`
|
|
155
|
+
)
|
|
156
|
+
.join("\n")}
|
|
157
|
+
]`;
|
|
158
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { match } from "path-to-regexp";
|
|
2
|
+
import type { HTTPMethod } from "./routes";
|
|
3
|
+
|
|
4
|
+
/* TODO: Grab these from @cloudflare/workers-types instead */
|
|
5
|
+
type Params<P extends string = string> = Record<P, string | string[]>;
|
|
6
|
+
|
|
7
|
+
type EventContext<Env, P extends string, Data> = {
|
|
8
|
+
request: Request;
|
|
9
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
10
|
+
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
|
|
11
|
+
env: Env & { ASSETS: { fetch: typeof fetch } };
|
|
12
|
+
params: Params<P>;
|
|
13
|
+
data: Data;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
declare type PagesFunction<
|
|
17
|
+
Env = unknown,
|
|
18
|
+
P extends string = string,
|
|
19
|
+
Data extends Record<string, unknown> = Record<string, unknown>
|
|
20
|
+
> = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
|
|
21
|
+
/* end @cloudflare/workers-types */
|
|
22
|
+
|
|
23
|
+
type RouteHandler = {
|
|
24
|
+
routePath: string;
|
|
25
|
+
methods: HTTPMethod[];
|
|
26
|
+
modules: PagesFunction[];
|
|
27
|
+
middlewares: PagesFunction[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// inject `routes` via ESBuild
|
|
31
|
+
declare const routes: RouteHandler[];
|
|
32
|
+
|
|
33
|
+
// expect an ASSETS fetcher binding pointing to the asset-server stage
|
|
34
|
+
type Env = {
|
|
35
|
+
[name: string]: unknown;
|
|
36
|
+
ASSETS: { fetch(url: string, init: RequestInit): Promise<Response> };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type WorkerContext = {
|
|
40
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function* executeRequest(request: Request, env: Env) {
|
|
44
|
+
const requestPath = new URL(request.url).pathname;
|
|
45
|
+
|
|
46
|
+
// First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
|
|
47
|
+
for (const route of [...routes].reverse()) {
|
|
48
|
+
if (
|
|
49
|
+
route.methods.length &&
|
|
50
|
+
!route.methods.includes(request.method as HTTPMethod)
|
|
51
|
+
) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const routeMatcher = match(route.routePath, { end: false });
|
|
56
|
+
const matchResult = routeMatcher(requestPath);
|
|
57
|
+
if (matchResult) {
|
|
58
|
+
for (const handler of route.middlewares.flat()) {
|
|
59
|
+
yield {
|
|
60
|
+
handler,
|
|
61
|
+
params: matchResult.params as Params,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Then look for the first exact route match and execute its "modules"
|
|
68
|
+
for (const route of routes) {
|
|
69
|
+
if (
|
|
70
|
+
route.methods.length &&
|
|
71
|
+
!route.methods.includes(request.method as HTTPMethod)
|
|
72
|
+
) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const routeMatcher = match(route.routePath, { end: true });
|
|
77
|
+
const matchResult = routeMatcher(requestPath);
|
|
78
|
+
if (matchResult && route.modules.length) {
|
|
79
|
+
for (const handler of route.modules.flat()) {
|
|
80
|
+
yield {
|
|
81
|
+
handler,
|
|
82
|
+
params: matchResult.params as Params,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Finally, yield to the asset-server
|
|
90
|
+
return {
|
|
91
|
+
handler: () => env.ASSETS.fetch(request.url, request),
|
|
92
|
+
params: {} as Params,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default {
|
|
97
|
+
async fetch(request: Request, env: Env, workerContext: WorkerContext) {
|
|
98
|
+
const handlerIterator = executeRequest(request, env);
|
|
99
|
+
const data = {}; // arbitrary data the user can set between functions
|
|
100
|
+
const next = async (input?: RequestInfo, init?: RequestInit) => {
|
|
101
|
+
if (input !== undefined) {
|
|
102
|
+
request = new Request(input, init);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const { value } = handlerIterator.next();
|
|
106
|
+
if (value) {
|
|
107
|
+
const { handler, params } = value;
|
|
108
|
+
const context: EventContext<
|
|
109
|
+
unknown,
|
|
110
|
+
string,
|
|
111
|
+
Record<string, unknown>
|
|
112
|
+
> = {
|
|
113
|
+
request: new Request(request.clone()),
|
|
114
|
+
next,
|
|
115
|
+
params,
|
|
116
|
+
data,
|
|
117
|
+
env,
|
|
118
|
+
waitUntil: workerContext.waitUntil.bind(workerContext),
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const response = await handler(context);
|
|
122
|
+
|
|
123
|
+
// https://fetch.spec.whatwg.org/#null-body-status
|
|
124
|
+
return new Response(
|
|
125
|
+
[101, 204, 205, 304].includes(response.status) ? null : response.body,
|
|
126
|
+
response
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
return next();
|
|
133
|
+
} catch (err) {
|
|
134
|
+
return new Response("Internal Error", { status: 500 });
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { render } from "ink-testing-library";
|
|
2
|
+
import patchConsole from "patch-console";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import Dev from "../dev";
|
|
5
|
+
import type { DevProps } from "../dev";
|
|
6
|
+
|
|
7
|
+
describe("Dev component", () => {
|
|
8
|
+
let restoreConsole;
|
|
9
|
+
beforeEach(() => (restoreConsole = patchConsole(() => {})));
|
|
10
|
+
afterEach(() => restoreConsole());
|
|
11
|
+
|
|
12
|
+
it("should throw if format is service-worker and there is a public directory", () => {
|
|
13
|
+
const { lastFrame } = renderDev({
|
|
14
|
+
format: "service-worker",
|
|
15
|
+
accountId: "some-account-id",
|
|
16
|
+
public: "some/public/path",
|
|
17
|
+
});
|
|
18
|
+
expect(lastFrame()).toMatchInlineSnapshot(`
|
|
19
|
+
"Something went wrong:
|
|
20
|
+
You cannot use the service worker format with a \`public\` directory."
|
|
21
|
+
`);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Helper function to make it easier to setup and render the `Dev` component.
|
|
27
|
+
*
|
|
28
|
+
* All the `Dev` props are optional here, with sensible defaults for testing.
|
|
29
|
+
*/
|
|
30
|
+
function renderDev({
|
|
31
|
+
name,
|
|
32
|
+
entry = "some/entry.ts",
|
|
33
|
+
port,
|
|
34
|
+
format,
|
|
35
|
+
accountId,
|
|
36
|
+
initialMode = "remote",
|
|
37
|
+
jsxFactory,
|
|
38
|
+
jsxFragment,
|
|
39
|
+
bindings = {},
|
|
40
|
+
public: publicDir,
|
|
41
|
+
site,
|
|
42
|
+
compatibilityDate,
|
|
43
|
+
compatibilityFlags,
|
|
44
|
+
usageModel,
|
|
45
|
+
buildCommand = {},
|
|
46
|
+
}: Partial<DevProps>) {
|
|
47
|
+
return render(
|
|
48
|
+
<Dev
|
|
49
|
+
name={name}
|
|
50
|
+
entry={entry}
|
|
51
|
+
port={port}
|
|
52
|
+
buildCommand={buildCommand}
|
|
53
|
+
format={format}
|
|
54
|
+
initialMode={initialMode}
|
|
55
|
+
jsxFactory={jsxFactory}
|
|
56
|
+
jsxFragment={jsxFragment}
|
|
57
|
+
accountId={accountId}
|
|
58
|
+
site={site}
|
|
59
|
+
public={publicDir}
|
|
60
|
+
compatibilityDate={compatibilityDate}
|
|
61
|
+
compatibilityFlags={compatibilityFlags}
|
|
62
|
+
usageModel={usageModel}
|
|
63
|
+
bindings={bindings}
|
|
64
|
+
/>
|
|
65
|
+
);
|
|
66
|
+
}
|