wrangler 0.0.2 → 0.0.6
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 -55
- package/bin/wrangler.js +36 -0
- package/import_meta_url.js +3 -0
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +1 -0
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/package.json +111 -9
- package/src/__tests__/clipboardy-mock.js +4 -0
- package/src/__tests__/index.test.ts +391 -0
- package/src/__tests__/jest.setup.ts +17 -0
- package/src/__tests__/mock-cfetch.js +42 -0
- package/src/__tests__/mock-dialogs.ts +65 -0
- package/src/api/form_data.ts +141 -0
- package/src/api/inspect.ts +430 -0
- package/src/api/preview.ts +128 -0
- package/src/api/worker.ts +161 -0
- package/src/cfetch.ts +72 -0
- package/src/cli.ts +10 -0
- package/src/config.ts +122 -0
- package/src/dev.tsx +867 -0
- package/src/dialogs.tsx +77 -0
- package/src/index.tsx +1875 -0
- package/src/kv.tsx +211 -0
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +818 -0
- package/src/proxy.ts +104 -0
- package/src/publish.ts +358 -0
- package/src/sites.tsx +115 -0
- package/src/tail.tsx +71 -0
- package/src/user.tsx +1029 -0
- package/static-asset-facade.js +47 -0
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +332 -0
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +176 -0
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +25 -0
- package/vendor/@cloudflare/kv-asset-handler/README.md +245 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +32 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +354 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +13 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +148 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +1 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +436 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +1 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +40 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +1 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +42 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +26 -0
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +31 -0
- package/vendor/@cloudflare/kv-asset-handler/package.json +52 -0
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +296 -0
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +136 -0
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +464 -0
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +33 -0
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +42 -0
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +39 -0
- package/vendor/wrangler-mime/CHANGELOG.md +289 -0
- package/vendor/wrangler-mime/LICENSE +21 -0
- package/vendor/wrangler-mime/Mime.js +97 -0
- package/vendor/wrangler-mime/README.md +187 -0
- package/vendor/wrangler-mime/cli.js +46 -0
- package/vendor/wrangler-mime/index.js +4 -0
- package/vendor/wrangler-mime/lite.js +4 -0
- package/vendor/wrangler-mime/package.json +52 -0
- package/vendor/wrangler-mime/types/other.js +1 -0
- package/vendor/wrangler-mime/types/standard.js +1 -0
- package/wrangler-dist/cli.js +125758 -0
- package/wrangler-dist/cli.js.map +7 -0
- package/.npmignore +0 -15
- package/index.js +0 -250
- package/tests/is.spec.js +0 -1155
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as fsp from "node:fs/promises";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as TOML from "@iarna/toml";
|
|
6
|
+
import { main } from "../index";
|
|
7
|
+
import { setMock, unsetAllMocks } from "./mock-cfetch";
|
|
8
|
+
import { mockConfirm } from "./mock-dialogs";
|
|
9
|
+
|
|
10
|
+
jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
|
|
11
|
+
|
|
12
|
+
async function w(cmd?: string) {
|
|
13
|
+
const logSpy = jest.spyOn(console, "log").mockImplementation();
|
|
14
|
+
const errorSpy = jest.spyOn(console, "error").mockImplementation();
|
|
15
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
16
|
+
try {
|
|
17
|
+
await main(cmd?.split(" ") ?? []);
|
|
18
|
+
return {
|
|
19
|
+
stdout: logSpy.mock.calls.flat(2).join("\n"),
|
|
20
|
+
stderr: errorSpy.mock.calls.flat(2).join("\n"),
|
|
21
|
+
warnings: warnSpy.mock.calls.flat(2).join("\n"),
|
|
22
|
+
};
|
|
23
|
+
} finally {
|
|
24
|
+
logSpy.mockRestore();
|
|
25
|
+
errorSpy.mockRestore();
|
|
26
|
+
warnSpy.mockRestore();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("wrangler", () => {
|
|
31
|
+
describe("no command", () => {
|
|
32
|
+
it("should display a list of available commands", async () => {
|
|
33
|
+
const { stdout, stderr } = await w();
|
|
34
|
+
|
|
35
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
36
|
+
"wrangler
|
|
37
|
+
|
|
38
|
+
Commands:
|
|
39
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
40
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
41
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
42
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
43
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
44
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
45
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
46
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
47
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
48
|
+
|
|
49
|
+
Flags:
|
|
50
|
+
-c, --config Path to .toml configuration file [string]
|
|
51
|
+
-h, --help Show help [boolean]
|
|
52
|
+
-v, --version Show version number [boolean]
|
|
53
|
+
|
|
54
|
+
Options:
|
|
55
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
56
|
+
`);
|
|
57
|
+
|
|
58
|
+
expect(stderr).toMatchInlineSnapshot(`""`);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("invalid command", () => {
|
|
63
|
+
it("should display an error", async () => {
|
|
64
|
+
const { stdout, stderr } = await w("invalid-command");
|
|
65
|
+
|
|
66
|
+
expect(stdout).toMatchInlineSnapshot(`
|
|
67
|
+
"wrangler
|
|
68
|
+
|
|
69
|
+
Commands:
|
|
70
|
+
wrangler init [name] 📥 Create a wrangler.toml configuration file
|
|
71
|
+
wrangler dev <filename> 👂 Start a local server for developing your worker
|
|
72
|
+
wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
|
|
73
|
+
wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
|
|
74
|
+
wrangler secret 🤫 Generate a secret that can be referenced in the worker script
|
|
75
|
+
wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
|
|
76
|
+
wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
|
|
77
|
+
wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
|
|
78
|
+
wrangler pages ⚡️ Configure Cloudflare Pages
|
|
79
|
+
|
|
80
|
+
Flags:
|
|
81
|
+
-c, --config Path to .toml configuration file [string]
|
|
82
|
+
-h, --help Show help [boolean]
|
|
83
|
+
-v, --version Show version number [boolean]
|
|
84
|
+
|
|
85
|
+
Options:
|
|
86
|
+
-l, --local Run on my machine [boolean] [default: false]"
|
|
87
|
+
`);
|
|
88
|
+
|
|
89
|
+
expect(stderr).toMatchInlineSnapshot(`
|
|
90
|
+
"
|
|
91
|
+
Unknown command: invalid-command."
|
|
92
|
+
`);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("init", () => {
|
|
97
|
+
const ogcwd = process.cwd();
|
|
98
|
+
let tmpDir: string;
|
|
99
|
+
|
|
100
|
+
beforeEach(async () => {
|
|
101
|
+
tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "init-"));
|
|
102
|
+
process.chdir(tmpDir);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
afterEach(async () => {
|
|
106
|
+
process.chdir(ogcwd);
|
|
107
|
+
await fsp.rm(tmpDir, { recursive: true });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should create a wrangler.toml", async () => {
|
|
111
|
+
mockConfirm({
|
|
112
|
+
text: "No package.json found. Would you like to create one?",
|
|
113
|
+
result: false,
|
|
114
|
+
});
|
|
115
|
+
await w("init");
|
|
116
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
117
|
+
expect(typeof parsed.compatibility_date).toBe("string");
|
|
118
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
119
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
|
|
123
|
+
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
124
|
+
mockConfirm({
|
|
125
|
+
text: "Do you want to continue initializing this project?",
|
|
126
|
+
result: false,
|
|
127
|
+
});
|
|
128
|
+
const { stderr } = await w("init");
|
|
129
|
+
expect(stderr).toContain("wrangler.toml file already exists!");
|
|
130
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
131
|
+
expect(typeof parsed.compatibility_date).toBe("undefined");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
|
|
135
|
+
fs.closeSync(fs.openSync("./wrangler.toml", "w"));
|
|
136
|
+
mockConfirm(
|
|
137
|
+
{
|
|
138
|
+
text: "Do you want to continue initializing this project?",
|
|
139
|
+
result: true,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
text: "No package.json found. Would you like to create one?",
|
|
143
|
+
result: false,
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
const { stderr } = await w("init");
|
|
147
|
+
expect(stderr).toContain("wrangler.toml file already exists!");
|
|
148
|
+
const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
|
|
149
|
+
expect(typeof parsed.compatibility_date).toBe("string");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("should create a package.json if none is found and user confirms", async () => {
|
|
153
|
+
mockConfirm(
|
|
154
|
+
{
|
|
155
|
+
text: "No package.json found. Would you like to create one?",
|
|
156
|
+
result: true,
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
text: "Would you like to use typescript?",
|
|
160
|
+
result: false,
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
await w("init");
|
|
164
|
+
expect(fs.existsSync("./package.json")).toBe(true);
|
|
165
|
+
const packageJson = JSON.parse(
|
|
166
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
167
|
+
);
|
|
168
|
+
expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
|
|
169
|
+
expect(packageJson.version).toEqual("0.0.1");
|
|
170
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("should not touch an existing package.json in the same directory", async () => {
|
|
174
|
+
mockConfirm({
|
|
175
|
+
text: "Would you like to use typescript?",
|
|
176
|
+
result: false,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
fs.writeFileSync(
|
|
180
|
+
"./package.json",
|
|
181
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
182
|
+
"utf-8"
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
await w("init");
|
|
186
|
+
const packageJson = JSON.parse(
|
|
187
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
188
|
+
);
|
|
189
|
+
expect(packageJson.name).toEqual("test");
|
|
190
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
194
|
+
mockConfirm({
|
|
195
|
+
text: "Would you like to use typescript?",
|
|
196
|
+
result: false,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
fs.writeFileSync(
|
|
200
|
+
"./package.json",
|
|
201
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
202
|
+
"utf-8"
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
206
|
+
process.chdir("./sub-1/sub-2");
|
|
207
|
+
|
|
208
|
+
await w("init");
|
|
209
|
+
expect(fs.existsSync("./package.json")).toBe(false);
|
|
210
|
+
expect(fs.existsSync("../../package.json")).toBe(true);
|
|
211
|
+
|
|
212
|
+
const packageJson = JSON.parse(
|
|
213
|
+
fs.readFileSync("../../package.json", "utf-8")
|
|
214
|
+
);
|
|
215
|
+
expect(packageJson.name).toEqual("test");
|
|
216
|
+
expect(packageJson.version).toEqual("1.0.0");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
|
|
220
|
+
mockConfirm(
|
|
221
|
+
{
|
|
222
|
+
text: "No package.json found. Would you like to create one?",
|
|
223
|
+
result: true,
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
text: "Would you like to use typescript?",
|
|
227
|
+
result: true,
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
await w("init");
|
|
231
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(true);
|
|
232
|
+
const tsconfigJson = JSON.parse(
|
|
233
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
234
|
+
);
|
|
235
|
+
expect(tsconfigJson.compilerOptions.types).toEqual([
|
|
236
|
+
"@cloudflare/workers-types",
|
|
237
|
+
]);
|
|
238
|
+
const packageJson = JSON.parse(
|
|
239
|
+
fs.readFileSync("./package.json", "utf-8")
|
|
240
|
+
);
|
|
241
|
+
expect(packageJson.devDependencies).toEqual({
|
|
242
|
+
"@cloudflare/workers-types": expect.any(String),
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should not touch an existing tsconfig.json in the same directory", async () => {
|
|
247
|
+
fs.writeFileSync(
|
|
248
|
+
"./package.json",
|
|
249
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
250
|
+
"utf-8"
|
|
251
|
+
);
|
|
252
|
+
fs.writeFileSync(
|
|
253
|
+
"./tsconfig.json",
|
|
254
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
255
|
+
"utf-8"
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await w("init");
|
|
259
|
+
const tsconfigJson = JSON.parse(
|
|
260
|
+
fs.readFileSync("./tsconfig.json", "utf-8")
|
|
261
|
+
);
|
|
262
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("should not touch an existing package.json in an ancestor directory", async () => {
|
|
266
|
+
fs.writeFileSync(
|
|
267
|
+
"./package.json",
|
|
268
|
+
JSON.stringify({ name: "test", version: "1.0.0" }),
|
|
269
|
+
"utf-8"
|
|
270
|
+
);
|
|
271
|
+
fs.writeFileSync(
|
|
272
|
+
"./tsconfig.json",
|
|
273
|
+
JSON.stringify({ compilerOptions: {} }),
|
|
274
|
+
"utf-8"
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
fs.mkdirSync("./sub-1/sub-2", { recursive: true });
|
|
278
|
+
process.chdir("./sub-1/sub-2");
|
|
279
|
+
|
|
280
|
+
await w("init");
|
|
281
|
+
expect(fs.existsSync("./tsconfig.json")).toBe(false);
|
|
282
|
+
expect(fs.existsSync("../../tsconfig.json")).toBe(true);
|
|
283
|
+
|
|
284
|
+
const tsconfigJson = JSON.parse(
|
|
285
|
+
fs.readFileSync("../../tsconfig.json", "utf-8")
|
|
286
|
+
);
|
|
287
|
+
expect(tsconfigJson.compilerOptions).toEqual({});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("should error if `--type` is used", async () => {
|
|
291
|
+
const noValue = await w("init --type");
|
|
292
|
+
expect(noValue.stderr).toMatchInlineSnapshot(
|
|
293
|
+
`"The --type option is no longer supported."`
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("should error if `--type javascript` is used", async () => {
|
|
298
|
+
const javascriptValue = await w("init --type javascript");
|
|
299
|
+
expect(javascriptValue.stderr).toMatchInlineSnapshot(
|
|
300
|
+
`"The --type option is no longer supported."`
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("should error if `--type rust` is used", async () => {
|
|
305
|
+
const rustValue = await w("init --type rust");
|
|
306
|
+
expect(rustValue.stderr).toMatchInlineSnapshot(
|
|
307
|
+
`"The --type option is no longer supported."`
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("should error if `--type webpack` is used", async () => {
|
|
312
|
+
const webpackValue = await w("init --type webpack");
|
|
313
|
+
expect(webpackValue.stderr).toMatchInlineSnapshot(`
|
|
314
|
+
"The --type option is no longer supported.
|
|
315
|
+
If you wish to use webpack then you will need to create a custom build."
|
|
316
|
+
`);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
describe("kv:namespace", () => {
|
|
321
|
+
afterEach(() => {
|
|
322
|
+
unsetAllMocks();
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("can create a namespace", async () => {
|
|
326
|
+
const KVNamespaces: { title: string; id: string }[] = [];
|
|
327
|
+
setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
|
|
328
|
+
expect(init.method === "POST");
|
|
329
|
+
expect(uri[0]).toEqual(
|
|
330
|
+
"/accounts/some-account-id/storage/kv/namespaces"
|
|
331
|
+
);
|
|
332
|
+
const { title } = JSON.parse(init.body);
|
|
333
|
+
expect(title).toEqual("worker-UnitTestNamespace");
|
|
334
|
+
KVNamespaces.push({ title, id: "some-namespace-id" });
|
|
335
|
+
return { id: "some-namespace-id" };
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
await w("kv:namespace create UnitTestNamespace");
|
|
339
|
+
|
|
340
|
+
expect(KVNamespaces).toEqual([
|
|
341
|
+
{
|
|
342
|
+
title: "worker-UnitTestNamespace",
|
|
343
|
+
id: "some-namespace-id",
|
|
344
|
+
},
|
|
345
|
+
]);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("can list namespaces", async () => {
|
|
349
|
+
const KVNamespaces: { title: string; id: string }[] = [
|
|
350
|
+
{ title: "title-1", id: "id-1" },
|
|
351
|
+
{ title: "title-2", id: "id-2" },
|
|
352
|
+
];
|
|
353
|
+
setMock(
|
|
354
|
+
"/accounts/:accountId/storage/kv/namespaces\\?:qs",
|
|
355
|
+
(uri, init) => {
|
|
356
|
+
expect(uri[0]).toContain(
|
|
357
|
+
"/accounts/some-account-id/storage/kv/namespaces"
|
|
358
|
+
);
|
|
359
|
+
expect(uri[2]).toContain("per_page=100");
|
|
360
|
+
expect(uri[2]).toContain("order=title");
|
|
361
|
+
expect(uri[2]).toContain("direction=asc");
|
|
362
|
+
expect(uri[2]).toContain("page=1");
|
|
363
|
+
expect(init).toBe(undefined);
|
|
364
|
+
return KVNamespaces;
|
|
365
|
+
}
|
|
366
|
+
);
|
|
367
|
+
const { stdout } = await w("kv:namespace list");
|
|
368
|
+
const namespaces = JSON.parse(stdout) as { id: string; title: string }[];
|
|
369
|
+
expect(namespaces).toEqual(KVNamespaces);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("can delete a namespace", async () => {
|
|
373
|
+
let accountId = "";
|
|
374
|
+
let namespaceId = "";
|
|
375
|
+
setMock(
|
|
376
|
+
"/accounts/:accountId/storage/kv/namespaces/:namespaceId",
|
|
377
|
+
(uri, init) => {
|
|
378
|
+
accountId = uri[1];
|
|
379
|
+
namespaceId = uri[2];
|
|
380
|
+
expect(uri[0]).toEqual(
|
|
381
|
+
"/accounts/some-account-id/storage/kv/namespaces/some-namespace-id"
|
|
382
|
+
);
|
|
383
|
+
expect(init.method).toBe("DELETE");
|
|
384
|
+
}
|
|
385
|
+
);
|
|
386
|
+
await w(`kv:namespace delete --namespace-id some-namespace-id`);
|
|
387
|
+
expect(accountId).toEqual("some-account-id");
|
|
388
|
+
expect(namespaceId).toEqual("some-namespace-id");
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { confirm, prompt } from "../dialogs";
|
|
2
|
+
|
|
3
|
+
jest.mock("../dialogs");
|
|
4
|
+
|
|
5
|
+
// By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
|
|
6
|
+
(confirm as jest.Mock).mockImplementation(() => {
|
|
7
|
+
throw new Error(
|
|
8
|
+
"Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more."
|
|
9
|
+
);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// By default (if not configured by mockPrompt()) calls to `prompt()` should throw.
|
|
13
|
+
(prompt as jest.Mock).mockImplementation(() => {
|
|
14
|
+
throw new Error(
|
|
15
|
+
"Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more."
|
|
16
|
+
);
|
|
17
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// This file mocks ../cfetch.ts
|
|
2
|
+
// so we can insert whatever responses we want from it
|
|
3
|
+
|
|
4
|
+
const { pathToRegexp } = require("path-to-regexp");
|
|
5
|
+
// TODO: add jsdoc style types here
|
|
6
|
+
|
|
7
|
+
// type MockHandler = (resource: string, init?: RequestInit) => any; // TODO: use a generic here
|
|
8
|
+
|
|
9
|
+
let mocks = [];
|
|
10
|
+
|
|
11
|
+
export function mockCfetch(resource, init) {
|
|
12
|
+
for (const { regexp, handler } of mocks) {
|
|
13
|
+
// The `resource` regular expression will extract the labelled groups from the URL.
|
|
14
|
+
// Let's pass these through to the handler, to allow it to do additional checks or behaviour.
|
|
15
|
+
const uri = regexp.exec(resource);
|
|
16
|
+
if (uri !== null) {
|
|
17
|
+
return handler(uri, init); // TODO: should we have some kind of fallthrough system? we'll see.
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
throw new Error(`no mocks found for ${resource}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function setMock(resource, handler) {
|
|
24
|
+
const mock = {
|
|
25
|
+
resource,
|
|
26
|
+
handler,
|
|
27
|
+
regexp: pathToRegexp(resource),
|
|
28
|
+
};
|
|
29
|
+
mocks.push(mock);
|
|
30
|
+
return () => {
|
|
31
|
+
mocks = mocks.filter((x) => x !== mock);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function unsetAllMocks() {
|
|
36
|
+
mocks = [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const CF_API_BASE_URL =
|
|
40
|
+
process.env.CF_API_BASE_URL || "https://api.cloudflare.com/client/v4";
|
|
41
|
+
|
|
42
|
+
export default mockCfetch;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { confirm, prompt } from "../dialogs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The expected values for a confirmation request.
|
|
5
|
+
*/
|
|
6
|
+
export interface ConfirmExpectation {
|
|
7
|
+
/** The text expected to be seen in the confirmation dialog. */
|
|
8
|
+
text: string;
|
|
9
|
+
/** The mock response send back from the confirmation dialog. */
|
|
10
|
+
result: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Mock the implementation of `confirm()` that will respond with configured results
|
|
15
|
+
* for configured confirmation text messages.
|
|
16
|
+
*
|
|
17
|
+
* If there is a call to `confirm()` that does not match any of the expectations
|
|
18
|
+
* then an error is thrown.
|
|
19
|
+
*/
|
|
20
|
+
export function mockConfirm(...expectations: ConfirmExpectation[]) {
|
|
21
|
+
(confirm as jest.Mock).mockImplementation((text: string) => {
|
|
22
|
+
for (const { text: expectedText, result } of expectations) {
|
|
23
|
+
if (text === expectedText) {
|
|
24
|
+
return Promise.resolve(result);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Unexpected confirmation message: ${text}`);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The expected values for a prompt request.
|
|
33
|
+
*/
|
|
34
|
+
export interface PromptExpectation {
|
|
35
|
+
/** The text expected to be seen in the prompt dialog. */
|
|
36
|
+
text: string;
|
|
37
|
+
/** The type of the prompt. */
|
|
38
|
+
type: "text" | "password";
|
|
39
|
+
/** The mock response send back from the prompt dialog. */
|
|
40
|
+
result: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Mock the implementation of `prompt()` that will respond with configured results
|
|
45
|
+
* for configured prompt text messages.
|
|
46
|
+
*
|
|
47
|
+
* If there is a call to `prompt()` that does not match any of the expectations
|
|
48
|
+
* then an error is thrown.
|
|
49
|
+
*/
|
|
50
|
+
export function mockPrompt(...expectations: PromptExpectation[]) {
|
|
51
|
+
(prompt as jest.Mock).mockImplementation(
|
|
52
|
+
(text: string, type: "text" | "password") => {
|
|
53
|
+
for (const {
|
|
54
|
+
text: expectedText,
|
|
55
|
+
type: expectedType,
|
|
56
|
+
result,
|
|
57
|
+
} of expectations) {
|
|
58
|
+
if (text === expectedText && type == expectedType) {
|
|
59
|
+
return Promise.resolve(result);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`Unexpected confirmation message: ${text}`);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CfWorkerInit,
|
|
3
|
+
CfModuleType,
|
|
4
|
+
CfVariable,
|
|
5
|
+
CfModule,
|
|
6
|
+
} from "./worker.js";
|
|
7
|
+
import { FormData, Blob } from "formdata-node";
|
|
8
|
+
|
|
9
|
+
// Credit: https://stackoverflow.com/a/9458996
|
|
10
|
+
function toBase64(source: BufferSource): string {
|
|
11
|
+
let result = "";
|
|
12
|
+
const buffer = source instanceof ArrayBuffer ? source : source.buffer;
|
|
13
|
+
const bytes = new Uint8Array(buffer);
|
|
14
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
15
|
+
result += String.fromCharCode(bytes[i]);
|
|
16
|
+
}
|
|
17
|
+
return btoa(result);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function toBinding(
|
|
21
|
+
name: string,
|
|
22
|
+
variable: CfVariable
|
|
23
|
+
): Record<string, unknown> {
|
|
24
|
+
if (typeof variable === "string") {
|
|
25
|
+
return { name, type: "plain_text", text: variable };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if ("namespaceId" in variable) {
|
|
29
|
+
return {
|
|
30
|
+
name,
|
|
31
|
+
type: "kv_namespace",
|
|
32
|
+
namespace_id: variable.namespaceId,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if ("class_name" in variable) {
|
|
37
|
+
return {
|
|
38
|
+
name,
|
|
39
|
+
type: "durable_object_namespace",
|
|
40
|
+
class_name: variable.class_name,
|
|
41
|
+
...(variable.script_name && {
|
|
42
|
+
script_name: variable.script_name,
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { format, algorithm, usages, data } = variable;
|
|
48
|
+
if (format) {
|
|
49
|
+
let key_base64;
|
|
50
|
+
let key_jwk;
|
|
51
|
+
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
|
52
|
+
key_base64 = toBase64(data);
|
|
53
|
+
} else {
|
|
54
|
+
key_jwk = data;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
name,
|
|
58
|
+
type: "secret_key",
|
|
59
|
+
format,
|
|
60
|
+
algorithm,
|
|
61
|
+
usages,
|
|
62
|
+
key_base64,
|
|
63
|
+
key_jwk,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw new TypeError("Unsupported variable: " + variable);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function toMimeType(type: CfModuleType): string {
|
|
71
|
+
switch (type) {
|
|
72
|
+
case "esm":
|
|
73
|
+
return "application/javascript+module";
|
|
74
|
+
case "commonjs":
|
|
75
|
+
return "application/javascript";
|
|
76
|
+
case "compiled-wasm":
|
|
77
|
+
return "application/wasm";
|
|
78
|
+
case "buffer":
|
|
79
|
+
return "application/octet-stream";
|
|
80
|
+
case "text":
|
|
81
|
+
return "text/plain";
|
|
82
|
+
default:
|
|
83
|
+
throw new TypeError("Unsupported module: " + type);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function toModule(module: CfModule, entryType?: CfModuleType): Blob {
|
|
88
|
+
const { type: moduleType, content } = module;
|
|
89
|
+
const type = toMimeType(moduleType ?? entryType);
|
|
90
|
+
|
|
91
|
+
return new Blob([content], { type });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a `FormData` upload from a `CfWorkerInit`.
|
|
96
|
+
*/
|
|
97
|
+
export function toFormData(worker: CfWorkerInit): FormData {
|
|
98
|
+
const formData = new FormData();
|
|
99
|
+
const {
|
|
100
|
+
main,
|
|
101
|
+
modules,
|
|
102
|
+
variables,
|
|
103
|
+
migrations,
|
|
104
|
+
usage_model,
|
|
105
|
+
compatibility_date,
|
|
106
|
+
compatibility_flags,
|
|
107
|
+
} = worker;
|
|
108
|
+
const { name, type: mainType } = main;
|
|
109
|
+
|
|
110
|
+
const bindings = [];
|
|
111
|
+
for (const [name, variable] of Object.entries(variables ?? {})) {
|
|
112
|
+
const binding = toBinding(name, variable);
|
|
113
|
+
bindings.push(binding);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// TODO: this object should be typed
|
|
117
|
+
const metadata = {
|
|
118
|
+
...(mainType !== "commonjs" ? { main_module: name } : { body_part: name }),
|
|
119
|
+
bindings,
|
|
120
|
+
...(compatibility_date && { compatibility_date }),
|
|
121
|
+
...(compatibility_flags && { compatibility_flags }),
|
|
122
|
+
...(usage_model && { usage_model }),
|
|
123
|
+
...(migrations && { migrations }),
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
formData.set("metadata", JSON.stringify(metadata));
|
|
127
|
+
|
|
128
|
+
if (mainType === "commonjs" && modules && modules.length > 0) {
|
|
129
|
+
throw new TypeError(
|
|
130
|
+
"More than one module can only be specified when type = 'esm'"
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const module of [main].concat(modules || [])) {
|
|
135
|
+
const { name } = module;
|
|
136
|
+
const blob = toModule(module, mainType ?? "esm");
|
|
137
|
+
formData.set(name, blob, name);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return formData;
|
|
141
|
+
}
|