wrangler 0.0.0-fb43821 → 0.0.0-fe5dcfd
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/package.json +25 -10
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +39 -0
- package/pages/functions/filepath-routing.ts +228 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +158 -0
- package/pages/functions/template-worker.ts +136 -0
- package/src/__tests__/dev.test.tsx +67 -0
- package/src/__tests__/index.test.ts +42 -175
- package/src/__tests__/jest.setup.ts +22 -0
- package/src/__tests__/kv.test.ts +1199 -0
- package/src/__tests__/mock-cfetch.ts +173 -0
- package/src/__tests__/mock-dialogs.ts +65 -0
- package/src/__tests__/publish.test.ts +737 -0
- package/src/__tests__/run-in-tmp.ts +19 -0
- package/src/__tests__/run-wrangler.ts +32 -0
- package/src/__tests__/whoami.test.tsx +130 -0
- package/src/api/form_data.ts +66 -76
- package/src/api/preview.ts +4 -4
- package/src/api/worker.ts +38 -44
- package/src/cfetch/index.ts +110 -0
- package/src/cfetch/internal.ts +71 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +634 -110
- package/src/dev.tsx +275 -272
- package/src/dialogs.tsx +2 -2
- package/src/index.tsx +504 -403
- package/src/inspect.ts +543 -0
- package/src/kv.tsx +109 -44
- package/src/pages.tsx +444 -222
- package/src/proxy.ts +302 -0
- package/src/publish.ts +123 -87
- package/src/sites.tsx +169 -41
- package/src/tail.tsx +5 -3
- package/src/user.tsx +64 -74
- package/src/whoami.tsx +69 -0
- package/wrangler-dist/cli.js +42792 -42619
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/mock-cfetch.js +0 -42
- package/src/api/inspect.ts +0 -430
- package/src/cfetch.ts +0 -72
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
// define `__FALLBACK_SERVICE__` via ESBuild
|
|
33
|
+
declare const __FALLBACK_SERVICE__: string;
|
|
34
|
+
|
|
35
|
+
// expect an ASSETS fetcher binding pointing to the asset-server stage
|
|
36
|
+
type FetchEnv = {
|
|
37
|
+
[name: string]: { fetch: typeof fetch };
|
|
38
|
+
ASSETS: { fetch: typeof fetch };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type WorkerContext = {
|
|
42
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function* executeRequest(request: Request, _env: FetchEnv) {
|
|
46
|
+
const requestPath = new URL(request.url).pathname;
|
|
47
|
+
|
|
48
|
+
// First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
|
|
49
|
+
for (const route of [...routes].reverse()) {
|
|
50
|
+
if (
|
|
51
|
+
route.methods.length &&
|
|
52
|
+
!route.methods.includes(request.method as HTTPMethod)
|
|
53
|
+
) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const routeMatcher = match(route.routePath, { end: false });
|
|
58
|
+
const matchResult = routeMatcher(requestPath);
|
|
59
|
+
if (matchResult) {
|
|
60
|
+
for (const handler of route.middlewares.flat()) {
|
|
61
|
+
yield {
|
|
62
|
+
handler,
|
|
63
|
+
params: matchResult.params as Params,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Then look for the first exact route match and execute its "modules"
|
|
70
|
+
for (const route of routes) {
|
|
71
|
+
if (
|
|
72
|
+
route.methods.length &&
|
|
73
|
+
!route.methods.includes(request.method as HTTPMethod)
|
|
74
|
+
) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const routeMatcher = match(route.routePath, { end: true });
|
|
79
|
+
const matchResult = routeMatcher(requestPath);
|
|
80
|
+
if (matchResult && route.modules.length) {
|
|
81
|
+
for (const handler of route.modules.flat()) {
|
|
82
|
+
yield {
|
|
83
|
+
handler,
|
|
84
|
+
params: matchResult.params as Params,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default {
|
|
93
|
+
async fetch(request: Request, env: FetchEnv, workerContext: WorkerContext) {
|
|
94
|
+
const handlerIterator = executeRequest(request, env);
|
|
95
|
+
const data = {}; // arbitrary data the user can set between functions
|
|
96
|
+
const next = async (input?: RequestInfo, init?: RequestInit) => {
|
|
97
|
+
if (input !== undefined) {
|
|
98
|
+
request = new Request(input, init);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const result = handlerIterator.next();
|
|
102
|
+
// Note we can't use `!result.done` because this doesn't narrow to the correct type
|
|
103
|
+
if (result.done == false) {
|
|
104
|
+
const { handler, params } = result.value;
|
|
105
|
+
const context = {
|
|
106
|
+
request: new Request(request.clone()),
|
|
107
|
+
next,
|
|
108
|
+
params,
|
|
109
|
+
data,
|
|
110
|
+
env,
|
|
111
|
+
waitUntil: workerContext.waitUntil.bind(workerContext),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const response = await handler(context);
|
|
115
|
+
|
|
116
|
+
// https://fetch.spec.whatwg.org/#null-body-status
|
|
117
|
+
return new Response(
|
|
118
|
+
[101, 204, 205, 304].includes(response.status) ? null : response.body,
|
|
119
|
+
response
|
|
120
|
+
);
|
|
121
|
+
} else if (__FALLBACK_SERVICE__) {
|
|
122
|
+
// There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
|
|
123
|
+
return env[__FALLBACK_SERVICE__].fetch(request);
|
|
124
|
+
} else {
|
|
125
|
+
// There was not fallback service so actually make the request to the origin.
|
|
126
|
+
return fetch(request);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
return next();
|
|
132
|
+
} catch (err) {
|
|
133
|
+
return new Response("Internal Error", { status: 500 });
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
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()?.split("\n").slice(0, 2).join("\n"))
|
|
19
|
+
.toMatchInlineSnapshot(`
|
|
20
|
+
"Something went wrong:
|
|
21
|
+
Error: You cannot use the service worker format with a \`public\` directory."
|
|
22
|
+
`);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Helper function to make it easier to setup and render the `Dev` component.
|
|
28
|
+
*
|
|
29
|
+
* All the `Dev` props are optional here, with sensible defaults for testing.
|
|
30
|
+
*/
|
|
31
|
+
function renderDev({
|
|
32
|
+
name,
|
|
33
|
+
entry = "some/entry.ts",
|
|
34
|
+
port,
|
|
35
|
+
format,
|
|
36
|
+
accountId,
|
|
37
|
+
initialMode = "remote",
|
|
38
|
+
jsxFactory,
|
|
39
|
+
jsxFragment,
|
|
40
|
+
bindings = {},
|
|
41
|
+
public: publicDir,
|
|
42
|
+
assetPaths,
|
|
43
|
+
compatibilityDate,
|
|
44
|
+
compatibilityFlags,
|
|
45
|
+
usageModel,
|
|
46
|
+
buildCommand = {},
|
|
47
|
+
}: Partial<DevProps>) {
|
|
48
|
+
return render(
|
|
49
|
+
<Dev
|
|
50
|
+
name={name}
|
|
51
|
+
entry={entry}
|
|
52
|
+
port={port}
|
|
53
|
+
buildCommand={buildCommand}
|
|
54
|
+
format={format}
|
|
55
|
+
initialMode={initialMode}
|
|
56
|
+
jsxFactory={jsxFactory}
|
|
57
|
+
jsxFragment={jsxFragment}
|
|
58
|
+
accountId={accountId}
|
|
59
|
+
assetPaths={assetPaths}
|
|
60
|
+
public={publicDir}
|
|
61
|
+
compatibilityDate={compatibilityDate}
|
|
62
|
+
compatibilityFlags={compatibilityFlags}
|
|
63
|
+
usageModel={usageModel}
|
|
64
|
+
bindings={bindings}
|
|
65
|
+
/>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -1,73 +1,23 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
1
|
import * as fsp from "node:fs/promises";
|
|
3
|
-
import * as os from "node:os";
|
|
4
|
-
import * as path from "node:path";
|
|
5
2
|
import * as TOML from "@iarna/toml";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
|
|
11
|
-
|
|
12
|
-
jest.mock("../dialogs", () => {
|
|
13
|
-
return {
|
|
14
|
-
...jest.requireActual<object>("../dialogs"),
|
|
15
|
-
confirm: jest
|
|
16
|
-
.fn()
|
|
17
|
-
.mockName("confirmMock")
|
|
18
|
-
.mockImplementation(() => {
|
|
19
|
-
// By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
|
|
20
|
-
throw new Error("Unexpected call to `confirm()`.");
|
|
21
|
-
}),
|
|
22
|
-
};
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Mock the implementation of `confirm()` that will respond with configured results
|
|
27
|
-
* for configured confirmation text messages.
|
|
28
|
-
*
|
|
29
|
-
* If there is a call to `confirm()` that does not match any of the expectations
|
|
30
|
-
* then an error is thrown.
|
|
31
|
-
*/
|
|
32
|
-
function mockConfirm(...expectations: { text: string; result: boolean }[]) {
|
|
33
|
-
(confirm as jest.Mock).mockImplementation((text: string) => {
|
|
34
|
-
for (const { text: expectedText, result } of expectations) {
|
|
35
|
-
if (text === expectedText) {
|
|
36
|
-
return result;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
throw new Error(`Unexpected confirmation message: ${text}`);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async function w(cmd?: string) {
|
|
44
|
-
const logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
45
|
-
const errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
46
|
-
const warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
47
|
-
try {
|
|
48
|
-
await main(cmd?.split(" ") ?? []);
|
|
49
|
-
return {
|
|
50
|
-
stdout: logSpy.mock.calls.flat(2).join("\n"),
|
|
51
|
-
stderr: errorSpy.mock.calls.flat(2).join("\n"),
|
|
52
|
-
warnings: warnSpy.mock.calls.flat(2).join("\n"),
|
|
53
|
-
};
|
|
54
|
-
} finally {
|
|
55
|
-
logSpy.mockRestore();
|
|
56
|
-
errorSpy.mockRestore();
|
|
57
|
-
warnSpy.mockRestore();
|
|
58
|
-
}
|
|
59
|
-
}
|
|
3
|
+
import { mockConfirm } from "./mock-dialogs";
|
|
4
|
+
import { runWrangler } from "./run-wrangler";
|
|
5
|
+
import { runInTempDir } from "./run-in-tmp";
|
|
6
|
+
import * as fs from "node:fs";
|
|
60
7
|
|
|
61
8
|
describe("wrangler", () => {
|
|
9
|
+
runInTempDir();
|
|
10
|
+
|
|
62
11
|
describe("no command", () => {
|
|
63
12
|
it("should display a list of available commands", async () => {
|
|
64
|
-
const { stdout, stderr } = await
|
|
13
|
+
const { stdout, stderr } = await runWrangler();
|
|
65
14
|
|
|
66
15
|
expect(stdout).toMatchInlineSnapshot(`
|
|
67
16
|
"wrangler
|
|
68
17
|
|
|
69
18
|
Commands:
|
|
70
19
|
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
20
|
+
wrangler whoami 🕵️ Retrieve your user info and test your auth config
|
|
71
21
|
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
72
22
|
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
73
23
|
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
@@ -92,13 +42,15 @@ describe("wrangler", () => {
|
|
|
92
42
|
|
|
93
43
|
describe("invalid command", () => {
|
|
94
44
|
it("should display an error", async () => {
|
|
95
|
-
const { stdout, stderr } = await
|
|
45
|
+
const { error, stdout, stderr } = await runWrangler("invalid-command");
|
|
96
46
|
|
|
97
|
-
expect(stdout).toMatchInlineSnapshot(`
|
|
47
|
+
expect(stdout).toMatchInlineSnapshot(`""`);
|
|
48
|
+
expect(stderr).toMatchInlineSnapshot(`
|
|
98
49
|
"wrangler
|
|
99
50
|
|
|
100
51
|
Commands:
|
|
101
52
|
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
53
|
+
wrangler whoami 🕵️ Retrieve your user info and test your auth config
|
|
102
54
|
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
103
55
|
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
104
56
|
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
@@ -114,36 +66,23 @@ describe("wrangler", () => {
|
|
|
114
66
|
-v, --version Show version number [boolean]
|
|
115
67
|
|
|
116
68
|
Options:
|
|
117
|
-
-l, --local Run on my machine [boolean] [default: false]
|
|
118
|
-
`);
|
|
69
|
+
-l, --local Run on my machine [boolean] [default: false]
|
|
119
70
|
|
|
120
|
-
expect(stderr).toMatchInlineSnapshot(`
|
|
121
|
-
"
|
|
122
71
|
Unknown command: invalid-command."
|
|
123
72
|
`);
|
|
73
|
+
expect(error).toMatchInlineSnapshot(
|
|
74
|
+
`[Error: Unknown command: invalid-command.]`
|
|
75
|
+
);
|
|
124
76
|
});
|
|
125
77
|
});
|
|
126
78
|
|
|
127
79
|
describe("init", () => {
|
|
128
|
-
const ogcwd = process.cwd();
|
|
129
|
-
let tmpDir: string;
|
|
130
|
-
|
|
131
|
-
beforeEach(async () => {
|
|
132
|
-
tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "init-"));
|
|
133
|
-
process.chdir(tmpDir);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
afterEach(async () => {
|
|
137
|
-
process.chdir(ogcwd);
|
|
138
|
-
await fsp.rm(tmpDir, { recursive: true });
|
|
139
|
-
});
|
|
140
|
-
|
|
141
80
|
it("should create a wrangler.toml", async () => {
|
|
142
81
|
mockConfirm({
|
|
143
82
|
text: "No package.json found. Would you like to create one?",
|
|
144
83
|
result: false,
|
|
145
84
|
});
|
|
146
|
-
await
|
|
85
|
+
await runWrangler("init");
|
|
147
86
|
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
148
87
|
expect(typeof parsed.compatibility_date).toBe("string");
|
|
149
88
|
expect(fs.existsSync("./package.json")).toBe(false);
|
|
@@ -151,19 +90,19 @@ describe("wrangler", () => {
|
|
|
151
90
|
});
|
|
152
91
|
|
|
153
92
|
it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
|
|
154
|
-
fs.
|
|
93
|
+
fs.writeFileSync("./wrangler.toml", "", "utf-8");
|
|
155
94
|
mockConfirm({
|
|
156
95
|
text: "Do you want to continue initializing this project?",
|
|
157
96
|
result: false,
|
|
158
97
|
});
|
|
159
|
-
const {
|
|
160
|
-
expect(
|
|
98
|
+
const { warnings } = await runWrangler("init");
|
|
99
|
+
expect(warnings).toContain("wrangler.toml file already exists!");
|
|
161
100
|
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
162
101
|
expect(typeof parsed.compatibility_date).toBe("undefined");
|
|
163
102
|
});
|
|
164
103
|
|
|
165
104
|
it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
|
|
166
|
-
fs.
|
|
105
|
+
fs.writeFileSync("./wrangler.toml", "", "utf-8");
|
|
167
106
|
mockConfirm(
|
|
168
107
|
{
|
|
169
108
|
text: "Do you want to continue initializing this project?",
|
|
@@ -174,8 +113,8 @@ describe("wrangler", () => {
|
|
|
174
113
|
result: false,
|
|
175
114
|
}
|
|
176
115
|
);
|
|
177
|
-
const {
|
|
178
|
-
expect(
|
|
116
|
+
const { warnings } = await runWrangler("init");
|
|
117
|
+
expect(warnings).toContain("wrangler.toml file already exists!");
|
|
179
118
|
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
180
119
|
expect(typeof parsed.compatibility_date).toBe("string");
|
|
181
120
|
});
|
|
@@ -191,7 +130,7 @@ describe("wrangler", () => {
|
|
|
191
130
|
result: false,
|
|
192
131
|
}
|
|
193
132
|
);
|
|
194
|
-
await
|
|
133
|
+
await runWrangler("init");
|
|
195
134
|
expect(fs.existsSync("./package.json")).toBe(true);
|
|
196
135
|
const packageJson = JSON.parse(
|
|
197
136
|
fs.readFileSync("./package.json", "utf-8")
|
|
@@ -213,7 +152,7 @@ describe("wrangler", () => {
|
|
|
213
152
|
"utf-8"
|
|
214
153
|
);
|
|
215
154
|
|
|
216
|
-
await
|
|
155
|
+
await runWrangler("init");
|
|
217
156
|
const packageJson = JSON.parse(
|
|
218
157
|
fs.readFileSync("./package.json", "utf-8")
|
|
219
158
|
);
|
|
@@ -236,7 +175,7 @@ describe("wrangler", () => {
|
|
|
236
175
|
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
237
176
|
process.chdir("./sub-1/sub-2");
|
|
238
177
|
|
|
239
|
-
await
|
|
178
|
+
await runWrangler("init");
|
|
240
179
|
expect(fs.existsSync("./package.json")).toBe(false);
|
|
241
180
|
expect(fs.existsSync("../../package.json")).toBe(true);
|
|
242
181
|
|
|
@@ -258,7 +197,7 @@ describe("wrangler", () => {
|
|
|
258
197
|
result: true,
|
|
259
198
|
}
|
|
260
199
|
);
|
|
261
|
-
await
|
|
200
|
+
await runWrangler("init");
|
|
262
201
|
expect(fs.existsSync("./tsconfig.json")).toBe(true);
|
|
263
202
|
const tsconfigJson = JSON.parse(
|
|
264
203
|
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
@@ -286,7 +225,7 @@ describe("wrangler", () => {
|
|
|
286
225
|
"utf-8"
|
|
287
226
|
);
|
|
288
227
|
|
|
289
|
-
await
|
|
228
|
+
await runWrangler("init");
|
|
290
229
|
const tsconfigJson = JSON.parse(
|
|
291
230
|
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
292
231
|
);
|
|
@@ -308,7 +247,7 @@ describe("wrangler", () => {
|
|
|
308
247
|
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
309
248
|
process.chdir("./sub-1/sub-2");
|
|
310
249
|
|
|
311
|
-
await
|
|
250
|
+
await runWrangler("init");
|
|
312
251
|
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
313
252
|
expect(fs.existsSync("../../tsconfig.json")).toBe(true);
|
|
314
253
|
|
|
@@ -319,104 +258,32 @@ describe("wrangler", () => {
|
|
|
319
258
|
});
|
|
320
259
|
|
|
321
260
|
it("should error if `--type` is used", async () => {
|
|
322
|
-
const
|
|
323
|
-
expect(
|
|
324
|
-
`
|
|
261
|
+
const { error } = await runWrangler("init --type");
|
|
262
|
+
expect(error).toMatchInlineSnapshot(
|
|
263
|
+
`[Error: The --type option is no longer supported.]`
|
|
325
264
|
);
|
|
326
265
|
});
|
|
327
266
|
|
|
328
267
|
it("should error if `--type javascript` is used", async () => {
|
|
329
|
-
const
|
|
330
|
-
expect(
|
|
331
|
-
`
|
|
268
|
+
const { error } = await runWrangler("init --type javascript");
|
|
269
|
+
expect(error).toMatchInlineSnapshot(
|
|
270
|
+
`[Error: The --type option is no longer supported.]`
|
|
332
271
|
);
|
|
333
272
|
});
|
|
334
273
|
|
|
335
274
|
it("should error if `--type rust` is used", async () => {
|
|
336
|
-
const
|
|
337
|
-
expect(
|
|
338
|
-
`
|
|
275
|
+
const { error } = await runWrangler("init --type rust");
|
|
276
|
+
expect(error).toMatchInlineSnapshot(
|
|
277
|
+
`[Error: The --type option is no longer supported.]`
|
|
339
278
|
);
|
|
340
279
|
});
|
|
341
280
|
|
|
342
281
|
it("should error if `--type webpack` is used", async () => {
|
|
343
|
-
const
|
|
344
|
-
expect(
|
|
345
|
-
|
|
346
|
-
If you wish to use webpack then you will need to create a custom build.
|
|
282
|
+
const { error } = await runWrangler("init --type webpack");
|
|
283
|
+
expect(error).toMatchInlineSnapshot(`
|
|
284
|
+
[Error: The --type option is no longer supported.
|
|
285
|
+
If you wish to use webpack then you will need to create a custom build.]
|
|
347
286
|
`);
|
|
348
287
|
});
|
|
349
288
|
});
|
|
350
|
-
|
|
351
|
-
describe("kv:namespace", () => {
|
|
352
|
-
afterEach(() => {
|
|
353
|
-
unsetAllMocks();
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
it("can create a namespace", async () => {
|
|
357
|
-
const KVNamespaces: { title: string; id: string }[] = [];
|
|
358
|
-
setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
|
|
359
|
-
expect(init.method === "POST");
|
|
360
|
-
expect(uri[0]).toEqual(
|
|
361
|
-
"/accounts/some-account-id/storage/kv/namespaces"
|
|
362
|
-
);
|
|
363
|
-
const { title } = JSON.parse(init.body);
|
|
364
|
-
expect(title).toEqual("worker-UnitTestNamespace");
|
|
365
|
-
KVNamespaces.push({ title, id: "some-namespace-id" });
|
|
366
|
-
return { id: "some-namespace-id" };
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
await w("kv:namespace create UnitTestNamespace");
|
|
370
|
-
|
|
371
|
-
expect(KVNamespaces).toEqual([
|
|
372
|
-
{
|
|
373
|
-
title: "worker-UnitTestNamespace",
|
|
374
|
-
id: "some-namespace-id",
|
|
375
|
-
},
|
|
376
|
-
]);
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it("can list namespaces", async () => {
|
|
380
|
-
const KVNamespaces: { title: string; id: string }[] = [
|
|
381
|
-
{ title: "title-1", id: "id-1" },
|
|
382
|
-
{ title: "title-2", id: "id-2" },
|
|
383
|
-
];
|
|
384
|
-
setMock(
|
|
385
|
-
"/accounts/:accountId/storage/kv/namespaces\\?:qs",
|
|
386
|
-
(uri, init) => {
|
|
387
|
-
expect(uri[0]).toContain(
|
|
388
|
-
"/accounts/some-account-id/storage/kv/namespaces"
|
|
389
|
-
);
|
|
390
|
-
expect(uri[2]).toContain("per_page=100");
|
|
391
|
-
expect(uri[2]).toContain("order=title");
|
|
392
|
-
expect(uri[2]).toContain("direction=asc");
|
|
393
|
-
expect(uri[2]).toContain("page=1");
|
|
394
|
-
expect(init).toBe(undefined);
|
|
395
|
-
return KVNamespaces;
|
|
396
|
-
}
|
|
397
|
-
);
|
|
398
|
-
const { stdout } = await w("kv:namespace list");
|
|
399
|
-
const namespaces = JSON.parse(stdout) as { id: string; title: string }[];
|
|
400
|
-
expect(namespaces).toEqual(KVNamespaces);
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
it("can delete a namespace", async () => {
|
|
404
|
-
let accountId = "";
|
|
405
|
-
let namespaceId = "";
|
|
406
|
-
setMock(
|
|
407
|
-
"/accounts/:accountId/storage/kv/namespaces/:namespaceId",
|
|
408
|
-
(uri, init) => {
|
|
409
|
-
accountId = uri[1];
|
|
410
|
-
namespaceId = uri[2];
|
|
411
|
-
expect(uri[0]).toEqual(
|
|
412
|
-
"/accounts/some-account-id/storage/kv/namespaces/some-namespace-id"
|
|
413
|
-
);
|
|
414
|
-
expect(init.method).toBe("DELETE");
|
|
415
|
-
}
|
|
416
|
-
);
|
|
417
|
-
await w(`kv:namespace delete --namespace-id some-namespace-id`);
|
|
418
|
-
expect(accountId).toEqual("some-account-id");
|
|
419
|
-
expect(namespaceId).toEqual("some-namespace-id");
|
|
420
|
-
});
|
|
421
|
-
});
|
|
422
289
|
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { mockFetchInternal } from "./mock-cfetch";
|
|
2
|
+
import { confirm, prompt } from "../dialogs";
|
|
3
|
+
import { fetchInternal } from "../cfetch/internal";
|
|
4
|
+
|
|
5
|
+
jest.mock("../cfetch/internal");
|
|
6
|
+
(fetchInternal as jest.Mock).mockImplementation(mockFetchInternal);
|
|
7
|
+
|
|
8
|
+
jest.mock("../dialogs");
|
|
9
|
+
|
|
10
|
+
// By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
|
|
11
|
+
(confirm as jest.Mock).mockImplementation(() => {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more."
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// By default (if not configured by mockPrompt()) calls to `prompt()` should throw.
|
|
18
|
+
(prompt as jest.Mock).mockImplementation(() => {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more."
|
|
21
|
+
);
|
|
22
|
+
});
|