wrangler 0.0.0-e61907d → 0.0.0-e95bea2
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/bin/wrangler.js +36 -2
- package/import_meta_url.js +3 -0
- package/package.json +79 -21
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +123 -0
- package/pages/functions/filepath-routing.ts +178 -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__/configuration.test.ts +2217 -0
- package/src/__tests__/dev.test.tsx +405 -0
- package/src/__tests__/dev2.test.tsx +152 -0
- package/src/__tests__/guess-worker-format.test.ts +58 -0
- package/src/__tests__/helpers/clipboardy-mock.js +4 -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 +44 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +70 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -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__/https-options.test.ts +127 -0
- package/src/__tests__/index.test.ts +991 -0
- package/src/__tests__/jest.setup.ts +69 -0
- package/src/__tests__/kv.test.ts +1408 -0
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/pages.test.ts +44 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +2453 -0
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +425 -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/bundle.ts +142 -0
- package/src/cfetch/index.ts +104 -0
- package/src/cfetch/internal.ts +135 -0
- package/src/cli.ts +19 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +279 -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 +388 -0
- package/src/config/validation.ts +1027 -0
- package/src/create-worker-preview.ts +166 -0
- package/src/create-worker-upload-form.ts +217 -0
- package/src/dev/dev.tsx +357 -0
- package/src/dev/local.tsx +241 -0
- package/src/dev/remote.tsx +201 -0
- package/src/dev/use-esbuild.ts +100 -0
- package/src/dialogs.tsx +77 -0
- package/src/entry.ts +159 -0
- package/src/environment-variables.ts +33 -0
- package/src/https-options.ts +122 -0
- package/src/index.tsx +2329 -0
- package/src/inspect.ts +617 -0
- package/src/kv/index.tsx +339 -0
- package/src/module-collection.ts +253 -0
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +126 -0
- package/src/pages.tsx +1045 -0
- package/src/parse.ts +183 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +438 -0
- package/src/publish.ts +354 -0
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +238 -0
- 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 +1065 -0
- package/src/whoami.tsx +69 -0
- package/src/worker.ts +163 -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/{index_node.js → cli.js} +105532 -67432
- package/wrangler-dist/cli.js.map +7 -0
- package/wrangler-dist/index_node.js.map +0 -7
package/src/index.tsx
ADDED
|
@@ -0,0 +1,2329 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { writeFile, mkdir } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { setTimeout } from "node:timers/promises";
|
|
5
|
+
import TOML from "@iarna/toml";
|
|
6
|
+
import { findUp } from "find-up";
|
|
7
|
+
import getPort from "get-port";
|
|
8
|
+
import { render } from "ink";
|
|
9
|
+
import React from "react";
|
|
10
|
+
import onExit from "signal-exit";
|
|
11
|
+
import makeCLI from "yargs";
|
|
12
|
+
import { version as wranglerVersion } from "../package.json";
|
|
13
|
+
import { fetchResult } from "./cfetch";
|
|
14
|
+
import { findWranglerToml, readConfig } from "./config";
|
|
15
|
+
import { createWorkerUploadForm } from "./create-worker-upload-form";
|
|
16
|
+
import Dev from "./dev/dev";
|
|
17
|
+
import { confirm, prompt } from "./dialogs";
|
|
18
|
+
import { getEntry } from "./entry";
|
|
19
|
+
import {
|
|
20
|
+
getNamespaceId,
|
|
21
|
+
listNamespaces,
|
|
22
|
+
listNamespaceKeys,
|
|
23
|
+
putKeyValue,
|
|
24
|
+
putBulkKeyValue,
|
|
25
|
+
deleteBulkKeyValue,
|
|
26
|
+
createNamespace,
|
|
27
|
+
isValidNamespaceBinding,
|
|
28
|
+
getKeyValue,
|
|
29
|
+
isKeyValue,
|
|
30
|
+
unexpectedKeyValueProps,
|
|
31
|
+
} from "./kv";
|
|
32
|
+
import { getPackageManager } from "./package-manager";
|
|
33
|
+
import { pages } from "./pages";
|
|
34
|
+
import { formatMessage, ParseError, parseJSON, readFileSync } from "./parse";
|
|
35
|
+
import publish from "./publish";
|
|
36
|
+
import { createR2Bucket, deleteR2Bucket, listR2Buckets } from "./r2";
|
|
37
|
+
import { getAssetPaths } from "./sites";
|
|
38
|
+
import {
|
|
39
|
+
createTail,
|
|
40
|
+
jsonPrintLogs,
|
|
41
|
+
prettyPrintLogs,
|
|
42
|
+
translateCLICommandToFilterMessage,
|
|
43
|
+
} from "./tail";
|
|
44
|
+
import {
|
|
45
|
+
login,
|
|
46
|
+
logout,
|
|
47
|
+
listScopes,
|
|
48
|
+
initialise as initialiseUserConfig,
|
|
49
|
+
loginOrRefreshIfRequired,
|
|
50
|
+
getAccountId,
|
|
51
|
+
validateScopeKeys,
|
|
52
|
+
} from "./user";
|
|
53
|
+
import { whoami } from "./whoami";
|
|
54
|
+
|
|
55
|
+
import type { Config } from "./config";
|
|
56
|
+
import type { TailCLIFilters } from "./tail";
|
|
57
|
+
import type { RawData } from "ws";
|
|
58
|
+
import type { CommandModule } from "yargs";
|
|
59
|
+
import type Yargs from "yargs";
|
|
60
|
+
|
|
61
|
+
type ConfigPath = string | undefined;
|
|
62
|
+
|
|
63
|
+
const resetColor = "\x1b[0m";
|
|
64
|
+
const fgGreenColor = "\x1b[32m";
|
|
65
|
+
|
|
66
|
+
function getRules(config: Config): Config["rules"] {
|
|
67
|
+
const rules = config.rules ?? config.build?.upload?.rules ?? [];
|
|
68
|
+
|
|
69
|
+
if (config.rules && config.build?.upload?.rules) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`You cannot configure both [rules] and [build.upload.rules] in your wrangler.toml. Delete the \`build.upload\` section.`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (config.build?.upload?.rules) {
|
|
76
|
+
console.warn(
|
|
77
|
+
`Deprecation notice: The \`build.upload.rules\` config field is no longer used, the rules should be specified via the \`rules\` config field. Delete the \`build.upload\` field from the configuration file, and add this:
|
|
78
|
+
|
|
79
|
+
${TOML.stringify({ rules: config.build.upload.rules })}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return rules;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isLegacyEnv(args: unknown, config: Config): boolean {
|
|
86
|
+
return (
|
|
87
|
+
(args as { "legacy-env": boolean | undefined })["legacy-env"] ??
|
|
88
|
+
config.legacy_env
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function getScriptName(
|
|
93
|
+
args: { name: string | undefined; env: string | undefined },
|
|
94
|
+
config: Config
|
|
95
|
+
): string | undefined {
|
|
96
|
+
const shortScriptName = args.name ?? config.name;
|
|
97
|
+
if (!shortScriptName) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return isLegacyEnv(args, config)
|
|
102
|
+
? `${shortScriptName}${args.env ? `-${args.env}` : ""}`
|
|
103
|
+
: shortScriptName;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Ensure that a user is logged in, and a valid account_id is available.
|
|
108
|
+
*/
|
|
109
|
+
async function requireAuth(
|
|
110
|
+
config: Config,
|
|
111
|
+
isInteractive = true
|
|
112
|
+
): Promise<string> {
|
|
113
|
+
const loggedIn = await loginOrRefreshIfRequired(isInteractive);
|
|
114
|
+
if (!loggedIn) {
|
|
115
|
+
// didn't login, let's just quit
|
|
116
|
+
throw new Error("Did not login, quitting...");
|
|
117
|
+
}
|
|
118
|
+
const accountId = config.account_id || (await getAccountId(isInteractive));
|
|
119
|
+
if (!accountId) {
|
|
120
|
+
throw new Error("No account id found, quitting...");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return accountId;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get a promise to the streamed input from stdin.
|
|
128
|
+
*
|
|
129
|
+
* This function can be used to grab the incoming stream of data from, say,
|
|
130
|
+
* piping the output of another process into the wrangler process.
|
|
131
|
+
*/
|
|
132
|
+
function readFromStdin(): Promise<string> {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
const stdin = process.stdin;
|
|
135
|
+
const chunks: string[] = [];
|
|
136
|
+
|
|
137
|
+
// When there is data ready to be read, the `readable` event will be triggered.
|
|
138
|
+
// In the handler for `readable` we call `read()` over and over until all the available data has been read.
|
|
139
|
+
stdin.on("readable", () => {
|
|
140
|
+
let chunk;
|
|
141
|
+
while (null !== (chunk = stdin.read())) {
|
|
142
|
+
chunks.push(chunk);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// When the streamed data is complete the `end` event will be triggered.
|
|
147
|
+
// In the handler for `end` we join the chunks together and resolve the promise.
|
|
148
|
+
stdin.on("end", () => {
|
|
149
|
+
resolve(chunks.join(""));
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// If there is an `error` event then the handler will reject the promise.
|
|
153
|
+
stdin.on("error", (err) => {
|
|
154
|
+
reject(err);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// a helper to demand one of a set of options
|
|
160
|
+
// via https://github.com/yargs/yargs/issues/1093#issuecomment-491299261
|
|
161
|
+
function demandOneOfOption(...options: string[]) {
|
|
162
|
+
return function (argv: Yargs.Arguments) {
|
|
163
|
+
const count = options.filter((option) => argv[option]).length;
|
|
164
|
+
const lastOption = options.pop();
|
|
165
|
+
|
|
166
|
+
if (count === 0) {
|
|
167
|
+
throw new CommandLineArgsError(
|
|
168
|
+
`Exactly one of the arguments ${options.join(
|
|
169
|
+
", "
|
|
170
|
+
)} and ${lastOption} is required`
|
|
171
|
+
);
|
|
172
|
+
} else if (count > 1) {
|
|
173
|
+
throw new CommandLineArgsError(
|
|
174
|
+
`Arguments ${options.join(
|
|
175
|
+
", "
|
|
176
|
+
)} and ${lastOption} are mutually exclusive`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return true;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
class CommandLineArgsError extends Error {}
|
|
185
|
+
class DeprecationError extends Error {
|
|
186
|
+
constructor(message: string) {
|
|
187
|
+
super(`DEPRECATION WARNING:\n${message}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function main(argv: string[]): Promise<void> {
|
|
192
|
+
const wrangler = makeCLI(argv)
|
|
193
|
+
// We handle errors ourselves in a try-catch around `yargs.parse`.
|
|
194
|
+
// If you want the "help info" to be displayed then throw an instance of `CommandLineArgsError`.
|
|
195
|
+
// Otherwise we just log the error that was thrown without any "help info".
|
|
196
|
+
.showHelpOnFail(false)
|
|
197
|
+
.fail((msg, error) => {
|
|
198
|
+
if (!error) {
|
|
199
|
+
// If there is only a `msg` then this came from yargs own validation, so wrap in a `CommandLineArgsError`.
|
|
200
|
+
error = new CommandLineArgsError(msg);
|
|
201
|
+
}
|
|
202
|
+
throw error;
|
|
203
|
+
})
|
|
204
|
+
.scriptName("wrangler")
|
|
205
|
+
.wrap(null);
|
|
206
|
+
|
|
207
|
+
// Default help command that supports the subcommands
|
|
208
|
+
const subHelp: CommandModule = {
|
|
209
|
+
command: ["*"],
|
|
210
|
+
handler: async (args) => {
|
|
211
|
+
setImmediate(() =>
|
|
212
|
+
wrangler.parse([...args._.map((a) => `${a}`), "--help"])
|
|
213
|
+
);
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
wrangler.command(
|
|
217
|
+
["*"],
|
|
218
|
+
false,
|
|
219
|
+
() => {},
|
|
220
|
+
(args) => {
|
|
221
|
+
if (args._.length > 0) {
|
|
222
|
+
throw new CommandLineArgsError(`Unknown command: ${args._}.`);
|
|
223
|
+
} else {
|
|
224
|
+
wrangler.showHelp("log");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
// You will note that we use the form for all commands where we use the builder function
|
|
230
|
+
// to define options and subcommands.
|
|
231
|
+
// Further we return the result of this builder even though it's not completely necessary.
|
|
232
|
+
// The reason is that it's required for type inference of the args in the handle function.
|
|
233
|
+
// I wish we could enforce this pattern, but this comment will have to do for now.
|
|
234
|
+
// (It's also annoying that choices[] doesn't get inferred as an enum. 🤷♂.)
|
|
235
|
+
|
|
236
|
+
// [DEPRECATED] generate
|
|
237
|
+
wrangler.command(
|
|
238
|
+
// we definitely want to move away from us cloning github templates
|
|
239
|
+
// we can do something better here, let's see
|
|
240
|
+
"generate [name] [template]",
|
|
241
|
+
false,
|
|
242
|
+
(yargs) => {
|
|
243
|
+
return yargs
|
|
244
|
+
.positional("name", {
|
|
245
|
+
describe: "Name of the Workers project",
|
|
246
|
+
default: "worker",
|
|
247
|
+
})
|
|
248
|
+
.positional("template", {
|
|
249
|
+
describe: "a link to a GitHub template",
|
|
250
|
+
default: "https://github.com/cloudflare/worker-template",
|
|
251
|
+
});
|
|
252
|
+
},
|
|
253
|
+
() => {
|
|
254
|
+
// "👯 [DEPRECATED]. Scaffold a Cloudflare Workers project from a public GitHub repository.",
|
|
255
|
+
throw new DeprecationError(
|
|
256
|
+
"`wrangler generate` has been deprecated, please refer to https://github.com/cloudflare/wrangler2/blob/main/docs/deprecations.md#generate for alternatives"
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
// init
|
|
262
|
+
wrangler.command(
|
|
263
|
+
"init [name]",
|
|
264
|
+
"📥 Create a wrangler.toml configuration file",
|
|
265
|
+
(yargs) => {
|
|
266
|
+
return yargs
|
|
267
|
+
.positional("name", {
|
|
268
|
+
describe: "The name of your worker",
|
|
269
|
+
type: "string",
|
|
270
|
+
})
|
|
271
|
+
.option("yes", {
|
|
272
|
+
describe: 'Answer "yes" to any prompts for new projects',
|
|
273
|
+
type: "boolean",
|
|
274
|
+
alias: "y",
|
|
275
|
+
});
|
|
276
|
+
},
|
|
277
|
+
async (args) => {
|
|
278
|
+
if ("type" in args) {
|
|
279
|
+
let message = "The --type option is no longer supported.";
|
|
280
|
+
if (args.type === "webpack") {
|
|
281
|
+
message +=
|
|
282
|
+
"\nIf you wish to use webpack then you will need to create a custom build.";
|
|
283
|
+
// TODO: Add a link to docs
|
|
284
|
+
}
|
|
285
|
+
throw new CommandLineArgsError(message);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// TODO: make sure args.name is a valid identifier for a worker name
|
|
289
|
+
|
|
290
|
+
const creationDirectory = path.join(process.cwd(), args.name ?? "");
|
|
291
|
+
|
|
292
|
+
const packageManager = await getPackageManager(creationDirectory);
|
|
293
|
+
|
|
294
|
+
// TODO: ask which directory to make the worker in (defaults to args.name)
|
|
295
|
+
// TODO: if args.name isn't provided, ask what to name the worker
|
|
296
|
+
|
|
297
|
+
const wranglerTomlDestination = path.join(
|
|
298
|
+
creationDirectory,
|
|
299
|
+
"./wrangler.toml"
|
|
300
|
+
);
|
|
301
|
+
let justCreatedWranglerToml = false;
|
|
302
|
+
const workerName =
|
|
303
|
+
args.name || path.basename(path.resolve(process.cwd()));
|
|
304
|
+
if (fs.existsSync(wranglerTomlDestination)) {
|
|
305
|
+
console.warn(`${wranglerTomlDestination} file already exists!`);
|
|
306
|
+
const shouldContinue = await confirm(
|
|
307
|
+
"Do you want to continue initializing this project?"
|
|
308
|
+
);
|
|
309
|
+
if (!shouldContinue) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
} else {
|
|
313
|
+
await mkdir(creationDirectory, { recursive: true });
|
|
314
|
+
const compatibilityDate = new Date().toISOString().substring(0, 10);
|
|
315
|
+
try {
|
|
316
|
+
await writeFile(
|
|
317
|
+
wranglerTomlDestination,
|
|
318
|
+
TOML.stringify({
|
|
319
|
+
name: workerName,
|
|
320
|
+
compatibility_date: compatibilityDate,
|
|
321
|
+
}) + "\n"
|
|
322
|
+
);
|
|
323
|
+
console.log(`✨ Successfully created wrangler.toml`);
|
|
324
|
+
justCreatedWranglerToml = true;
|
|
325
|
+
} catch (err) {
|
|
326
|
+
throw new Error(
|
|
327
|
+
`Failed to create wrangler.toml.\n${(err as Error).message ?? err}`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let pathToPackageJson = await findUp("package.json");
|
|
333
|
+
let shouldCreatePackageJson = false;
|
|
334
|
+
const yesFlag = args.yes ?? false;
|
|
335
|
+
if (!pathToPackageJson) {
|
|
336
|
+
// If no package.json exists, ask to create one
|
|
337
|
+
shouldCreatePackageJson =
|
|
338
|
+
yesFlag ||
|
|
339
|
+
(await confirm(
|
|
340
|
+
"No package.json found. Would you like to create one?"
|
|
341
|
+
));
|
|
342
|
+
|
|
343
|
+
if (shouldCreatePackageJson) {
|
|
344
|
+
await writeFile(
|
|
345
|
+
path.join(creationDirectory, "./package.json"),
|
|
346
|
+
JSON.stringify(
|
|
347
|
+
{
|
|
348
|
+
name: workerName,
|
|
349
|
+
version: "0.0.0",
|
|
350
|
+
devDependencies: {
|
|
351
|
+
wrangler: wranglerVersion,
|
|
352
|
+
},
|
|
353
|
+
private: true,
|
|
354
|
+
},
|
|
355
|
+
null,
|
|
356
|
+
" "
|
|
357
|
+
) + "\n"
|
|
358
|
+
);
|
|
359
|
+
await packageManager.install();
|
|
360
|
+
console.log(`✨ Created package.json`);
|
|
361
|
+
pathToPackageJson = path.join(creationDirectory, "package.json");
|
|
362
|
+
} else {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
// If package.json exists and wrangler isn't installed,
|
|
367
|
+
// then ask to add wrangler to devDependencies
|
|
368
|
+
const packageJson = parseJSON(
|
|
369
|
+
readFileSync(pathToPackageJson),
|
|
370
|
+
pathToPackageJson
|
|
371
|
+
);
|
|
372
|
+
if (
|
|
373
|
+
!(
|
|
374
|
+
packageJson.devDependencies?.wrangler ||
|
|
375
|
+
packageJson.dependencies?.wrangler
|
|
376
|
+
)
|
|
377
|
+
) {
|
|
378
|
+
const shouldInstall =
|
|
379
|
+
yesFlag ||
|
|
380
|
+
(await confirm(
|
|
381
|
+
"Would you like to install wrangler into your package.json?"
|
|
382
|
+
));
|
|
383
|
+
if (shouldInstall) {
|
|
384
|
+
await packageManager.addDevDeps(`wrangler@${wranglerVersion}`);
|
|
385
|
+
console.log(`✨ Installed wrangler`);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
let isTypescriptProject = false;
|
|
391
|
+
let pathToTSConfig = await findUp("tsconfig.json");
|
|
392
|
+
if (!pathToTSConfig) {
|
|
393
|
+
// If there's no tsconfig, offer to create one
|
|
394
|
+
// and install @cloudflare/workers-types
|
|
395
|
+
if (yesFlag || (await confirm("Would you like to use TypeScript?"))) {
|
|
396
|
+
isTypescriptProject = true;
|
|
397
|
+
await writeFile(
|
|
398
|
+
path.join(creationDirectory, "./tsconfig.json"),
|
|
399
|
+
readFileSync(path.join(__dirname, "../templates/tsconfig.json"))
|
|
400
|
+
);
|
|
401
|
+
await packageManager.addDevDeps(
|
|
402
|
+
"@cloudflare/workers-types",
|
|
403
|
+
"typescript"
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
console.log(
|
|
407
|
+
`✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies`
|
|
408
|
+
);
|
|
409
|
+
pathToTSConfig = path.join(creationDirectory, "tsconfig.json");
|
|
410
|
+
}
|
|
411
|
+
} else {
|
|
412
|
+
isTypescriptProject = true;
|
|
413
|
+
// If there's a tsconfig, check if @cloudflare/workers-types
|
|
414
|
+
// is already installed, and offer to install it if not
|
|
415
|
+
const packageJson = parseJSON(
|
|
416
|
+
readFileSync(pathToPackageJson),
|
|
417
|
+
pathToPackageJson
|
|
418
|
+
);
|
|
419
|
+
if (
|
|
420
|
+
!(
|
|
421
|
+
packageJson.devDependencies?.["@cloudflare/workers-types"] ||
|
|
422
|
+
packageJson.dependencies?.["@cloudflare/workers-types"]
|
|
423
|
+
)
|
|
424
|
+
) {
|
|
425
|
+
const shouldInstall = await confirm(
|
|
426
|
+
"Would you like to install the type definitions for Workers into your package.json?"
|
|
427
|
+
);
|
|
428
|
+
if (shouldInstall) {
|
|
429
|
+
await packageManager.addDevDeps("@cloudflare/workers-types");
|
|
430
|
+
// We don't update the tsconfig.json because
|
|
431
|
+
// it could be complicated in existing projects
|
|
432
|
+
// and we don't want to break them. Instead, we simply
|
|
433
|
+
// tell the user that they need to update their tsconfig.json
|
|
434
|
+
console.log(
|
|
435
|
+
`✨ Installed @cloudflare/workers-types.\nPlease add "@cloudflare/workers-types" to compilerOptions.types in your tsconfig.json`
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const packageJsonContent = parseJSON(
|
|
442
|
+
readFileSync(pathToPackageJson),
|
|
443
|
+
pathToPackageJson
|
|
444
|
+
);
|
|
445
|
+
const shouldWritePackageJsonScripts =
|
|
446
|
+
!packageJsonContent.scripts?.start &&
|
|
447
|
+
!packageJsonContent.scripts?.publish &&
|
|
448
|
+
shouldCreatePackageJson;
|
|
449
|
+
|
|
450
|
+
async function writePackageJsonScriptsAndUpdateWranglerToml(
|
|
451
|
+
isWritingScripts: boolean,
|
|
452
|
+
isCreatingWranglerToml: boolean,
|
|
453
|
+
packagePath: string,
|
|
454
|
+
scriptPath: string
|
|
455
|
+
) {
|
|
456
|
+
if (isCreatingWranglerToml) {
|
|
457
|
+
// rewrite wrangler.toml with main = "path/to/script"
|
|
458
|
+
const parsedWranglerToml = TOML.parse(
|
|
459
|
+
readFileSync(wranglerTomlDestination)
|
|
460
|
+
);
|
|
461
|
+
fs.writeFileSync(
|
|
462
|
+
wranglerTomlDestination,
|
|
463
|
+
TOML.stringify({
|
|
464
|
+
name: parsedWranglerToml.name,
|
|
465
|
+
main: scriptPath,
|
|
466
|
+
compatibility_date: parsedWranglerToml.compatibility_date,
|
|
467
|
+
})
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
const isNamedWorker =
|
|
471
|
+
isCreatingWranglerToml && path.dirname(packagePath) !== process.cwd();
|
|
472
|
+
|
|
473
|
+
if (isWritingScripts) {
|
|
474
|
+
await writeFile(
|
|
475
|
+
packagePath,
|
|
476
|
+
JSON.stringify(
|
|
477
|
+
{
|
|
478
|
+
...packageJsonContent,
|
|
479
|
+
scripts: {
|
|
480
|
+
...packageJsonContent.scripts,
|
|
481
|
+
start: isCreatingWranglerToml
|
|
482
|
+
? `wrangler dev`
|
|
483
|
+
: `wrangler dev ${scriptPath}`,
|
|
484
|
+
publish: isCreatingWranglerToml
|
|
485
|
+
? `wrangler publish`
|
|
486
|
+
: `wrangler publish ${scriptPath}`,
|
|
487
|
+
},
|
|
488
|
+
},
|
|
489
|
+
null,
|
|
490
|
+
2
|
|
491
|
+
) + "\n"
|
|
492
|
+
);
|
|
493
|
+
console.log(
|
|
494
|
+
`\nTo start developing your Worker, run \`${
|
|
495
|
+
isNamedWorker ? `cd ${args.name} && ` : ""
|
|
496
|
+
}npm start\``
|
|
497
|
+
);
|
|
498
|
+
console.log(
|
|
499
|
+
`To publish your Worker to the Internet, run \`npm run publish\``
|
|
500
|
+
);
|
|
501
|
+
} else {
|
|
502
|
+
console.log(
|
|
503
|
+
`\nTo start developing your Worker, run \`npx wrangler dev\`${
|
|
504
|
+
isCreatingWranglerToml ? "" : ` ${scriptPath}`
|
|
505
|
+
}`
|
|
506
|
+
);
|
|
507
|
+
console.log(
|
|
508
|
+
`To publish your Worker to the Internet, run \`npx wrangler publish\`${
|
|
509
|
+
isCreatingWranglerToml ? "" : ` ${scriptPath}`
|
|
510
|
+
}`
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (isTypescriptProject) {
|
|
516
|
+
if (!fs.existsSync(path.join(creationDirectory, "./src/index.ts"))) {
|
|
517
|
+
let shouldCreateSource = false;
|
|
518
|
+
|
|
519
|
+
shouldCreateSource =
|
|
520
|
+
yesFlag ||
|
|
521
|
+
(await confirm(
|
|
522
|
+
`Would you like to create a Worker at src/index.ts?`
|
|
523
|
+
));
|
|
524
|
+
|
|
525
|
+
if (shouldCreateSource) {
|
|
526
|
+
await mkdir(path.join(creationDirectory, "./src"), {
|
|
527
|
+
recursive: true,
|
|
528
|
+
});
|
|
529
|
+
await writeFile(
|
|
530
|
+
path.join(creationDirectory, "./src/index.ts"),
|
|
531
|
+
readFileSync(path.join(__dirname, "../templates/new-worker.ts"))
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
console.log(`✨ Created src/index.ts`);
|
|
535
|
+
|
|
536
|
+
await writePackageJsonScriptsAndUpdateWranglerToml(
|
|
537
|
+
shouldWritePackageJsonScripts,
|
|
538
|
+
justCreatedWranglerToml,
|
|
539
|
+
pathToPackageJson,
|
|
540
|
+
"src/index.ts"
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
} else {
|
|
545
|
+
if (!fs.existsSync("./src/index.js")) {
|
|
546
|
+
const shouldCreateSource = await confirm(
|
|
547
|
+
`Would you like to create a Worker at src/index.js?`
|
|
548
|
+
);
|
|
549
|
+
if (shouldCreateSource) {
|
|
550
|
+
await mkdir(path.join(creationDirectory, "./src"), {
|
|
551
|
+
recursive: true,
|
|
552
|
+
});
|
|
553
|
+
await writeFile(
|
|
554
|
+
path.join(path.join(creationDirectory, "./src/index.js")),
|
|
555
|
+
readFileSync(path.join(__dirname, "../templates/new-worker.js"))
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
console.log(`✨ Created src/index.js`);
|
|
559
|
+
|
|
560
|
+
await writePackageJsonScriptsAndUpdateWranglerToml(
|
|
561
|
+
shouldWritePackageJsonScripts,
|
|
562
|
+
justCreatedWranglerToml,
|
|
563
|
+
pathToPackageJson,
|
|
564
|
+
"src/index.js"
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
);
|
|
571
|
+
|
|
572
|
+
// build
|
|
573
|
+
wrangler.command(
|
|
574
|
+
"build",
|
|
575
|
+
false,
|
|
576
|
+
(yargs) => {
|
|
577
|
+
return yargs.option("env", {
|
|
578
|
+
describe: "Perform on a specific environment",
|
|
579
|
+
});
|
|
580
|
+
},
|
|
581
|
+
() => {
|
|
582
|
+
// "[DEPRECATED] 🦀 Build your project (if applicable)",
|
|
583
|
+
throw new DeprecationError(
|
|
584
|
+
"`wrangler build` has been deprecated, please refer to https://github.com/cloudflare/wrangler2/blob/main/docs/deprecations.md#build for alternatives"
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
// login
|
|
590
|
+
wrangler.command(
|
|
591
|
+
// this needs scopes as an option?
|
|
592
|
+
"login",
|
|
593
|
+
false, // we don't need to show this in the menu
|
|
594
|
+
// "🔓 Login to Cloudflare",
|
|
595
|
+
(yargs) => {
|
|
596
|
+
// TODO: This needs some copy editing
|
|
597
|
+
// I mean, this entire app does, but this too.
|
|
598
|
+
return yargs
|
|
599
|
+
.option("scopes-list", {
|
|
600
|
+
describe: "List all the available OAuth scopes with descriptions",
|
|
601
|
+
})
|
|
602
|
+
.option("scopes", {
|
|
603
|
+
describe: "Pick the set of applicable OAuth scopes when logging in",
|
|
604
|
+
array: true,
|
|
605
|
+
type: "string",
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
// TODO: scopes
|
|
609
|
+
},
|
|
610
|
+
async (args) => {
|
|
611
|
+
if (args["scopes-list"]) {
|
|
612
|
+
listScopes();
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
if (args.scopes) {
|
|
616
|
+
if (args.scopes.length === 0) {
|
|
617
|
+
// don't allow no scopes to be passed, that would be weird
|
|
618
|
+
listScopes();
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
if (!validateScopeKeys(args.scopes)) {
|
|
622
|
+
throw new CommandLineArgsError(
|
|
623
|
+
`One of ${args.scopes} is not a valid authentication scope. Run "wrangler login --list-scopes" to see the valid scopes.`
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
await login({ scopes: args.scopes });
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
await login();
|
|
630
|
+
|
|
631
|
+
// TODO: would be nice if it optionally saved login
|
|
632
|
+
// credentials inside node_modules/.cache or something
|
|
633
|
+
// this way you could have multiple users on a single machine
|
|
634
|
+
}
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
// logout
|
|
638
|
+
wrangler.command(
|
|
639
|
+
// this needs scopes as an option?
|
|
640
|
+
"logout",
|
|
641
|
+
false, // we don't need to show this in the menu
|
|
642
|
+
// "🚪 Logout from Cloudflare",
|
|
643
|
+
() => {},
|
|
644
|
+
async () => {
|
|
645
|
+
await logout();
|
|
646
|
+
}
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
// whoami
|
|
650
|
+
wrangler.command(
|
|
651
|
+
"whoami",
|
|
652
|
+
"🕵️ Retrieve your user info and test your auth config",
|
|
653
|
+
() => {},
|
|
654
|
+
async () => {
|
|
655
|
+
await whoami();
|
|
656
|
+
}
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
// config
|
|
660
|
+
wrangler.command(
|
|
661
|
+
"config",
|
|
662
|
+
false,
|
|
663
|
+
() => {},
|
|
664
|
+
() => {
|
|
665
|
+
// "🕵️ Authenticate Wrangler with a Cloudflare API Token",
|
|
666
|
+
throw new DeprecationError(
|
|
667
|
+
"`wrangler config` has been deprecated, please refer to TODO://some/path for alternatives"
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
);
|
|
671
|
+
|
|
672
|
+
// dev
|
|
673
|
+
wrangler.command(
|
|
674
|
+
"dev [script]",
|
|
675
|
+
"👂 Start a local server for developing your worker",
|
|
676
|
+
(yargs) => {
|
|
677
|
+
return yargs
|
|
678
|
+
.positional("script", {
|
|
679
|
+
describe: "The path to an entry point for your worker",
|
|
680
|
+
type: "string",
|
|
681
|
+
})
|
|
682
|
+
.option("name", {
|
|
683
|
+
describe: "Name of the worker",
|
|
684
|
+
type: "string",
|
|
685
|
+
})
|
|
686
|
+
.option("format", {
|
|
687
|
+
choices: ["modules", "service-worker"] as const,
|
|
688
|
+
describe: "Choose an entry type",
|
|
689
|
+
})
|
|
690
|
+
.option("env", {
|
|
691
|
+
describe: "Perform on a specific environment",
|
|
692
|
+
type: "string",
|
|
693
|
+
})
|
|
694
|
+
.option("compatibility-date", {
|
|
695
|
+
describe: "Date to use for compatibility checks",
|
|
696
|
+
type: "string",
|
|
697
|
+
})
|
|
698
|
+
.option("compatibility-flags", {
|
|
699
|
+
describe: "Flags to use for compatibility checks",
|
|
700
|
+
type: "array",
|
|
701
|
+
alias: "compatibility-flag",
|
|
702
|
+
})
|
|
703
|
+
.option("latest", {
|
|
704
|
+
describe: "Use the latest version of the worker runtime",
|
|
705
|
+
type: "boolean",
|
|
706
|
+
default: true,
|
|
707
|
+
})
|
|
708
|
+
.option("ip", {
|
|
709
|
+
describe: "IP address to listen on",
|
|
710
|
+
type: "string",
|
|
711
|
+
default: "127.0.0.1",
|
|
712
|
+
})
|
|
713
|
+
.option("port", {
|
|
714
|
+
describe: "Port to listen on",
|
|
715
|
+
type: "number",
|
|
716
|
+
})
|
|
717
|
+
.option("inspector-port", {
|
|
718
|
+
describe: "Port for devtools to connect to",
|
|
719
|
+
type: "number",
|
|
720
|
+
})
|
|
721
|
+
.option("routes", {
|
|
722
|
+
describe: "Routes to upload",
|
|
723
|
+
alias: "route",
|
|
724
|
+
type: "array",
|
|
725
|
+
})
|
|
726
|
+
.option("host", {
|
|
727
|
+
type: "string",
|
|
728
|
+
describe:
|
|
729
|
+
"Host to forward requests to, defaults to the zone of project",
|
|
730
|
+
})
|
|
731
|
+
.option("local-protocol", {
|
|
732
|
+
describe: "Protocol to listen to requests on, defaults to http.",
|
|
733
|
+
choices: ["http", "https"] as const,
|
|
734
|
+
})
|
|
735
|
+
.option("experimental-public", {
|
|
736
|
+
describe: "Static assets to be served",
|
|
737
|
+
type: "string",
|
|
738
|
+
})
|
|
739
|
+
.option("site", {
|
|
740
|
+
describe: "Root folder of static assets for Workers Sites",
|
|
741
|
+
type: "string",
|
|
742
|
+
})
|
|
743
|
+
.option("site-include", {
|
|
744
|
+
describe:
|
|
745
|
+
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
|
|
746
|
+
type: "string",
|
|
747
|
+
array: true,
|
|
748
|
+
})
|
|
749
|
+
.option("site-exclude", {
|
|
750
|
+
describe:
|
|
751
|
+
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
|
|
752
|
+
type: "string",
|
|
753
|
+
array: true,
|
|
754
|
+
})
|
|
755
|
+
.option("upstream-protocol", {
|
|
756
|
+
describe:
|
|
757
|
+
"Protocol to forward requests to host on, defaults to https.",
|
|
758
|
+
choices: ["http", "https"] as const,
|
|
759
|
+
})
|
|
760
|
+
.option("jsx-factory", {
|
|
761
|
+
describe: "The function that is called for each JSX element",
|
|
762
|
+
type: "string",
|
|
763
|
+
})
|
|
764
|
+
.option("jsx-fragment", {
|
|
765
|
+
describe: "The function that is called for each JSX fragment",
|
|
766
|
+
type: "string",
|
|
767
|
+
})
|
|
768
|
+
.option("local", {
|
|
769
|
+
alias: "l",
|
|
770
|
+
describe: "Run on my machine",
|
|
771
|
+
type: "boolean",
|
|
772
|
+
default: false, // I bet this will a point of contention. We'll revisit it.
|
|
773
|
+
})
|
|
774
|
+
.option("experimental-enable-local-persistence", {
|
|
775
|
+
describe: "Enable persistence for this session (only for local mode)",
|
|
776
|
+
type: "boolean",
|
|
777
|
+
});
|
|
778
|
+
},
|
|
779
|
+
async (args) => {
|
|
780
|
+
const config = readConfig(
|
|
781
|
+
(args.config as ConfigPath) ||
|
|
782
|
+
(args.script && findWranglerToml(path.dirname(args.script)))
|
|
783
|
+
);
|
|
784
|
+
const entry = await getEntry(args, config, "dev");
|
|
785
|
+
|
|
786
|
+
if (args["experimental-public"]) {
|
|
787
|
+
console.warn(
|
|
788
|
+
"🚨 The --experimental-public field is experimental and will change in the future."
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (args.public) {
|
|
793
|
+
throw new Error(
|
|
794
|
+
"🚨 The --public field has been renamed to --experimental-public, and will change behaviour in the future."
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const upstreamProtocol =
|
|
799
|
+
(args["upstream-protocol"] as "http" | "https") ||
|
|
800
|
+
config.dev.upstream_protocol;
|
|
801
|
+
if (upstreamProtocol === "http") {
|
|
802
|
+
console.warn(
|
|
803
|
+
"Setting upstream-protocol to http is not currently implemented.\n" +
|
|
804
|
+
"If this is required in your project, please add your use case to the following issue:\n" +
|
|
805
|
+
"https://github.com/cloudflare/wrangler2/issues/583."
|
|
806
|
+
);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const accountId = !args.local ? await requireAuth(config) : undefined;
|
|
810
|
+
|
|
811
|
+
// TODO: if worker_dev = false and no routes, then error (only for dev)
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Given something that resembles a URL,
|
|
815
|
+
* try to extract a host from it
|
|
816
|
+
*/
|
|
817
|
+
function getHost(urlLike: string): string | undefined {
|
|
818
|
+
if (
|
|
819
|
+
!(urlLike.startsWith("http://") || urlLike.startsWith("https://"))
|
|
820
|
+
) {
|
|
821
|
+
urlLike = "http://" + urlLike;
|
|
822
|
+
}
|
|
823
|
+
return new URL(urlLike).host;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Given something that resembles a host,
|
|
828
|
+
* try to infer a zone id from it
|
|
829
|
+
*/
|
|
830
|
+
async function getZoneId(host: string): Promise<string | undefined> {
|
|
831
|
+
const zones = await fetchResult<{ id: string }[]>(
|
|
832
|
+
`/zones`,
|
|
833
|
+
{},
|
|
834
|
+
new URLSearchParams({ name: host })
|
|
835
|
+
);
|
|
836
|
+
return zones[0]?.id;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const environments = config.env ?? {};
|
|
840
|
+
const envRootObj = args.env ? environments[args.env] || {} : config;
|
|
841
|
+
|
|
842
|
+
const hostLike =
|
|
843
|
+
args.host ||
|
|
844
|
+
config.dev.host ||
|
|
845
|
+
(args.routes && args.routes[0]) ||
|
|
846
|
+
envRootObj.route ||
|
|
847
|
+
(envRootObj.routes && envRootObj.routes[0]);
|
|
848
|
+
|
|
849
|
+
// When we're given a host (in one of the above ways), we do 2 things:
|
|
850
|
+
// - We try to extract a host from it
|
|
851
|
+
// - We try to get a zone id from the host
|
|
852
|
+
//
|
|
853
|
+
// So it turns out it's particularly hard to get a 'valid' domain
|
|
854
|
+
// from a string, so we don't even try to validate TLDs, etc.
|
|
855
|
+
// Once we get something that looks like w.x.y.z-ish, we then try to
|
|
856
|
+
// get a zone id for it, by lopping off subdomains until we get a hit
|
|
857
|
+
// from the API. That's it!
|
|
858
|
+
|
|
859
|
+
const host = typeof hostLike === "string" ? getHost(hostLike) : undefined;
|
|
860
|
+
|
|
861
|
+
let zoneId: string | undefined;
|
|
862
|
+
const hostPieces = typeof host === "string" ? host.split(".") : undefined;
|
|
863
|
+
|
|
864
|
+
while (hostPieces && hostPieces.length > 1) {
|
|
865
|
+
zoneId = await getZoneId(hostPieces.join("."));
|
|
866
|
+
if (zoneId) break;
|
|
867
|
+
hostPieces.shift();
|
|
868
|
+
}
|
|
869
|
+
if (host && !zoneId) {
|
|
870
|
+
throw new Error(`Could not find zone for ${hostLike}`);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const zone =
|
|
874
|
+
typeof zoneId === "string" && typeof host === "string"
|
|
875
|
+
? {
|
|
876
|
+
host,
|
|
877
|
+
id: zoneId,
|
|
878
|
+
}
|
|
879
|
+
: undefined;
|
|
880
|
+
|
|
881
|
+
const { waitUntilExit } = render(
|
|
882
|
+
<Dev
|
|
883
|
+
name={getScriptName(args, config)}
|
|
884
|
+
entry={entry}
|
|
885
|
+
env={args.env}
|
|
886
|
+
zone={zone}
|
|
887
|
+
rules={getRules(config)}
|
|
888
|
+
legacyEnv={isLegacyEnv(args, config)}
|
|
889
|
+
buildCommand={config.build || {}}
|
|
890
|
+
initialMode={args.local ? "local" : "remote"}
|
|
891
|
+
jsxFactory={args["jsx-factory"] || envRootObj?.jsx_factory}
|
|
892
|
+
jsxFragment={args["jsx-fragment"] || envRootObj?.jsx_fragment}
|
|
893
|
+
upstreamProtocol={upstreamProtocol}
|
|
894
|
+
localProtocol={
|
|
895
|
+
// The typings are not quite clever enough to handle element accesses, only property accesses,
|
|
896
|
+
// so we need to cast here.
|
|
897
|
+
(args["local-protocol"] as "http" | "https") ||
|
|
898
|
+
config.dev.local_protocol
|
|
899
|
+
}
|
|
900
|
+
enableLocalPersistence={
|
|
901
|
+
args["experimental-enable-local-persistence"] || false
|
|
902
|
+
}
|
|
903
|
+
accountId={accountId}
|
|
904
|
+
assetPaths={getAssetPaths(
|
|
905
|
+
config,
|
|
906
|
+
args.site,
|
|
907
|
+
args.siteInclude,
|
|
908
|
+
args.siteExclude
|
|
909
|
+
)}
|
|
910
|
+
port={
|
|
911
|
+
args.port || config.dev?.port || (await getPort({ port: 8787 }))
|
|
912
|
+
}
|
|
913
|
+
inspectorPort={
|
|
914
|
+
args["inspector-port"] ?? (await getPort({ port: 9229 }))
|
|
915
|
+
}
|
|
916
|
+
public={args["experimental-public"]}
|
|
917
|
+
compatibilityDate={
|
|
918
|
+
args["compatibility-date"] ||
|
|
919
|
+
config.compatibility_date ||
|
|
920
|
+
new Date().toISOString().substring(0, 10)
|
|
921
|
+
}
|
|
922
|
+
compatibilityFlags={
|
|
923
|
+
(args["compatibility-flags"] as string[]) ||
|
|
924
|
+
config.compatibility_flags
|
|
925
|
+
}
|
|
926
|
+
usageModel={envRootObj.usage_model}
|
|
927
|
+
bindings={{
|
|
928
|
+
kv_namespaces: envRootObj.kv_namespaces?.map(
|
|
929
|
+
({ binding, preview_id, id: _id }) => {
|
|
930
|
+
// In `dev`, we make folks use a separate kv namespace called
|
|
931
|
+
// `preview_id` instead of `id` so that they don't
|
|
932
|
+
// break production data. So here we check that a `preview_id`
|
|
933
|
+
// has actually been configured.
|
|
934
|
+
// This whole block of code will be obsoleted in the future
|
|
935
|
+
// when we have copy-on-write for previews on edge workers.
|
|
936
|
+
if (!preview_id) {
|
|
937
|
+
// TODO: This error has to be a _lot_ better, ideally just asking
|
|
938
|
+
// to create a preview namespace for the user automatically
|
|
939
|
+
throw new Error(
|
|
940
|
+
`In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "${binding}" in your wrangler.toml`
|
|
941
|
+
); // Ugh, I really don't like this message very much
|
|
942
|
+
}
|
|
943
|
+
return {
|
|
944
|
+
binding,
|
|
945
|
+
id: preview_id,
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
),
|
|
949
|
+
vars: envRootObj.vars,
|
|
950
|
+
wasm_modules: config.wasm_modules,
|
|
951
|
+
text_blobs: config.text_blobs,
|
|
952
|
+
durable_objects: envRootObj.durable_objects,
|
|
953
|
+
r2_buckets: envRootObj.r2_buckets,
|
|
954
|
+
unsafe: envRootObj.unsafe?.bindings,
|
|
955
|
+
}}
|
|
956
|
+
/>
|
|
957
|
+
);
|
|
958
|
+
await waitUntilExit();
|
|
959
|
+
}
|
|
960
|
+
);
|
|
961
|
+
|
|
962
|
+
// publish
|
|
963
|
+
wrangler.command(
|
|
964
|
+
"publish [script]",
|
|
965
|
+
"🆙 Publish your Worker to Cloudflare.",
|
|
966
|
+
(yargs) => {
|
|
967
|
+
return yargs
|
|
968
|
+
.option("env", {
|
|
969
|
+
type: "string",
|
|
970
|
+
describe: "Perform on a specific environment",
|
|
971
|
+
})
|
|
972
|
+
.positional("script", {
|
|
973
|
+
describe: "The path to an entry point for your worker",
|
|
974
|
+
type: "string",
|
|
975
|
+
})
|
|
976
|
+
.option("name", {
|
|
977
|
+
describe: "Name of the worker",
|
|
978
|
+
type: "string",
|
|
979
|
+
})
|
|
980
|
+
.option("format", {
|
|
981
|
+
choices: ["modules", "service-worker"] as const,
|
|
982
|
+
describe: "Choose an entry type",
|
|
983
|
+
})
|
|
984
|
+
.option("compatibility-date", {
|
|
985
|
+
describe: "Date to use for compatibility checks",
|
|
986
|
+
type: "string",
|
|
987
|
+
})
|
|
988
|
+
.option("compatibility-flags", {
|
|
989
|
+
describe: "Flags to use for compatibility checks",
|
|
990
|
+
type: "array",
|
|
991
|
+
alias: "compatibility-flag",
|
|
992
|
+
})
|
|
993
|
+
.option("latest", {
|
|
994
|
+
describe: "Use the latest version of the worker runtime",
|
|
995
|
+
type: "boolean",
|
|
996
|
+
default: false,
|
|
997
|
+
})
|
|
998
|
+
.option("experimental-public", {
|
|
999
|
+
describe: "Static assets to be served",
|
|
1000
|
+
type: "string",
|
|
1001
|
+
})
|
|
1002
|
+
.option("site", {
|
|
1003
|
+
describe: "Root folder of static assets for Workers Sites",
|
|
1004
|
+
type: "string",
|
|
1005
|
+
})
|
|
1006
|
+
.option("site-include", {
|
|
1007
|
+
describe:
|
|
1008
|
+
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
|
|
1009
|
+
type: "string",
|
|
1010
|
+
array: true,
|
|
1011
|
+
})
|
|
1012
|
+
.option("site-exclude", {
|
|
1013
|
+
describe:
|
|
1014
|
+
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
|
|
1015
|
+
type: "string",
|
|
1016
|
+
array: true,
|
|
1017
|
+
})
|
|
1018
|
+
.option("triggers", {
|
|
1019
|
+
describe: "cron schedules to attach",
|
|
1020
|
+
alias: ["schedule", "schedules"],
|
|
1021
|
+
type: "array",
|
|
1022
|
+
})
|
|
1023
|
+
.option("routes", {
|
|
1024
|
+
describe: "Routes to upload",
|
|
1025
|
+
alias: "route",
|
|
1026
|
+
type: "array",
|
|
1027
|
+
})
|
|
1028
|
+
.option("jsx-factory", {
|
|
1029
|
+
describe: "The function that is called for each JSX element",
|
|
1030
|
+
type: "string",
|
|
1031
|
+
})
|
|
1032
|
+
.option("jsx-fragment", {
|
|
1033
|
+
describe: "The function that is called for each JSX fragment",
|
|
1034
|
+
type: "string",
|
|
1035
|
+
});
|
|
1036
|
+
},
|
|
1037
|
+
async (args) => {
|
|
1038
|
+
if (args["experimental-public"]) {
|
|
1039
|
+
console.warn(
|
|
1040
|
+
"🚨 The --experimental-public field is experimental and will change in the future."
|
|
1041
|
+
);
|
|
1042
|
+
}
|
|
1043
|
+
if (args.public) {
|
|
1044
|
+
throw new Error(
|
|
1045
|
+
"🚨 The --public field has been renamed to --experimental-public, and will change behaviour in the future."
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
const config = readConfig(
|
|
1050
|
+
(args.config as ConfigPath) ||
|
|
1051
|
+
(args.script && findWranglerToml(path.dirname(args.script)))
|
|
1052
|
+
);
|
|
1053
|
+
const entry = await getEntry(args, config, "publish");
|
|
1054
|
+
|
|
1055
|
+
if (args.latest) {
|
|
1056
|
+
console.warn(
|
|
1057
|
+
"⚠️ Using the latest version of the Workers runtime. To silence this warning, please choose a specific version of the runtime with --compatibility-date, or add a compatibility_date to your wrangler.toml.\n"
|
|
1058
|
+
);
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
const accountId = await requireAuth(config);
|
|
1062
|
+
|
|
1063
|
+
const assetPaths = getAssetPaths(
|
|
1064
|
+
config,
|
|
1065
|
+
args["experimental-public"] || args.site,
|
|
1066
|
+
args.siteInclude,
|
|
1067
|
+
args.siteExclude
|
|
1068
|
+
);
|
|
1069
|
+
await publish({
|
|
1070
|
+
config,
|
|
1071
|
+
accountId,
|
|
1072
|
+
name: getScriptName(args, config),
|
|
1073
|
+
rules: getRules(config),
|
|
1074
|
+
entry,
|
|
1075
|
+
env: args.env,
|
|
1076
|
+
compatibilityDate: args.latest
|
|
1077
|
+
? new Date().toISOString().substring(0, 10)
|
|
1078
|
+
: args["compatibility-date"],
|
|
1079
|
+
compatibilityFlags: args["compatibility-flags"] as string[],
|
|
1080
|
+
triggers: args.triggers,
|
|
1081
|
+
jsxFactory: args["jsx-factory"],
|
|
1082
|
+
jsxFragment: args["jsx-fragment"],
|
|
1083
|
+
routes: args.routes,
|
|
1084
|
+
assetPaths,
|
|
1085
|
+
legacyEnv: isLegacyEnv(args, config),
|
|
1086
|
+
experimentalPublic: args["experimental-public"] !== undefined,
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
);
|
|
1090
|
+
|
|
1091
|
+
// tail
|
|
1092
|
+
wrangler.command(
|
|
1093
|
+
"tail [name]",
|
|
1094
|
+
"🦚 Starts a log tailing session for a published Worker.",
|
|
1095
|
+
(yargs) => {
|
|
1096
|
+
return (
|
|
1097
|
+
yargs
|
|
1098
|
+
.positional("name", {
|
|
1099
|
+
describe: "Name of the worker",
|
|
1100
|
+
type: "string",
|
|
1101
|
+
})
|
|
1102
|
+
// TODO: auto-detect if this should be json or pretty based on atty
|
|
1103
|
+
.option("format", {
|
|
1104
|
+
default: "json",
|
|
1105
|
+
choices: ["json", "pretty"],
|
|
1106
|
+
describe: "The format of log entries",
|
|
1107
|
+
})
|
|
1108
|
+
.option("status", {
|
|
1109
|
+
choices: ["ok", "error", "canceled"],
|
|
1110
|
+
describe: "Filter by invocation status",
|
|
1111
|
+
array: true,
|
|
1112
|
+
})
|
|
1113
|
+
.option("header", {
|
|
1114
|
+
type: "string",
|
|
1115
|
+
describe: "Filter by HTTP header",
|
|
1116
|
+
})
|
|
1117
|
+
.option("method", {
|
|
1118
|
+
type: "string",
|
|
1119
|
+
describe: "Filter by HTTP method",
|
|
1120
|
+
array: true,
|
|
1121
|
+
})
|
|
1122
|
+
.option("sampling-rate", {
|
|
1123
|
+
type: "number",
|
|
1124
|
+
describe: "Adds a percentage of requests to log sampling rate",
|
|
1125
|
+
})
|
|
1126
|
+
.option("search", {
|
|
1127
|
+
type: "string",
|
|
1128
|
+
describe: "Filter by a text match in console.log messages",
|
|
1129
|
+
})
|
|
1130
|
+
.option("ip", {
|
|
1131
|
+
type: "string",
|
|
1132
|
+
describe:
|
|
1133
|
+
'Filter by the IP address the request originates from. Use "self" to filter for your own IP',
|
|
1134
|
+
array: true,
|
|
1135
|
+
})
|
|
1136
|
+
.option("env", {
|
|
1137
|
+
type: "string",
|
|
1138
|
+
describe: "Perform on a specific environment",
|
|
1139
|
+
})
|
|
1140
|
+
.option("debug", {
|
|
1141
|
+
type: "boolean",
|
|
1142
|
+
hidden: true,
|
|
1143
|
+
default: false,
|
|
1144
|
+
describe:
|
|
1145
|
+
"If a log would have been filtered out, send it through anyway alongside the filter which would have blocked it.",
|
|
1146
|
+
})
|
|
1147
|
+
);
|
|
1148
|
+
},
|
|
1149
|
+
async (args) => {
|
|
1150
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1151
|
+
|
|
1152
|
+
const scriptName = getScriptName(args, config);
|
|
1153
|
+
|
|
1154
|
+
if (!scriptName) {
|
|
1155
|
+
throw new Error("Missing script name");
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
const accountId = await requireAuth(config);
|
|
1159
|
+
|
|
1160
|
+
const cliFilters: TailCLIFilters = {
|
|
1161
|
+
status: args.status as ("ok" | "error" | "canceled")[],
|
|
1162
|
+
header: args.header,
|
|
1163
|
+
method: args.method,
|
|
1164
|
+
samplingRate: args["sampling-rate"],
|
|
1165
|
+
search: args.search,
|
|
1166
|
+
clientIp: args.ip,
|
|
1167
|
+
};
|
|
1168
|
+
|
|
1169
|
+
const filters = translateCLICommandToFilterMessage(
|
|
1170
|
+
cliFilters,
|
|
1171
|
+
args.debug
|
|
1172
|
+
);
|
|
1173
|
+
|
|
1174
|
+
const { tail, expiration, deleteTail } = await createTail(
|
|
1175
|
+
accountId,
|
|
1176
|
+
scriptName,
|
|
1177
|
+
filters,
|
|
1178
|
+
!isLegacyEnv(args, config) ? args.env : undefined
|
|
1179
|
+
);
|
|
1180
|
+
|
|
1181
|
+
const scriptDisplayName = `${scriptName}${
|
|
1182
|
+
args.env && !isLegacyEnv(args, config) ? ` (${args.env})` : ""
|
|
1183
|
+
}`;
|
|
1184
|
+
|
|
1185
|
+
console.log(
|
|
1186
|
+
`successfully created tail, expires at ${expiration.toLocaleString()}`
|
|
1187
|
+
);
|
|
1188
|
+
|
|
1189
|
+
onExit(async () => {
|
|
1190
|
+
tail.terminate();
|
|
1191
|
+
await deleteTail();
|
|
1192
|
+
});
|
|
1193
|
+
|
|
1194
|
+
const printLog: (data: RawData) => void =
|
|
1195
|
+
args.format === "pretty" ? prettyPrintLogs : jsonPrintLogs;
|
|
1196
|
+
|
|
1197
|
+
tail.on("message", printLog);
|
|
1198
|
+
|
|
1199
|
+
while (tail.readyState !== tail.OPEN) {
|
|
1200
|
+
switch (tail.readyState) {
|
|
1201
|
+
case tail.CONNECTING:
|
|
1202
|
+
await setTimeout(100);
|
|
1203
|
+
break;
|
|
1204
|
+
case tail.CLOSING:
|
|
1205
|
+
await setTimeout(100);
|
|
1206
|
+
break;
|
|
1207
|
+
case tail.CLOSED:
|
|
1208
|
+
throw new Error(
|
|
1209
|
+
`Connection to ${scriptDisplayName} closed unexpectedly.`
|
|
1210
|
+
);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
console.log(`Connected to ${scriptDisplayName}, waiting for logs...`);
|
|
1215
|
+
|
|
1216
|
+
tail.on("close", async () => {
|
|
1217
|
+
tail.terminate();
|
|
1218
|
+
await deleteTail();
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
);
|
|
1222
|
+
|
|
1223
|
+
// preview
|
|
1224
|
+
wrangler.command(
|
|
1225
|
+
"preview [method] [body]",
|
|
1226
|
+
false,
|
|
1227
|
+
(yargs) => {
|
|
1228
|
+
return yargs
|
|
1229
|
+
.positional("method", {
|
|
1230
|
+
type: "string",
|
|
1231
|
+
describe: "Type of request to preview your worker",
|
|
1232
|
+
})
|
|
1233
|
+
.positional("body", {
|
|
1234
|
+
type: "string",
|
|
1235
|
+
describe: "Body string to post to your preview worker request.",
|
|
1236
|
+
})
|
|
1237
|
+
.option("env", {
|
|
1238
|
+
type: "string",
|
|
1239
|
+
describe: "Perform on a specific environment",
|
|
1240
|
+
})
|
|
1241
|
+
.option("watch", {
|
|
1242
|
+
default: true,
|
|
1243
|
+
describe: "Enable live preview",
|
|
1244
|
+
type: "boolean",
|
|
1245
|
+
});
|
|
1246
|
+
},
|
|
1247
|
+
async (args) => {
|
|
1248
|
+
if (args.method || args.body) {
|
|
1249
|
+
throw new DeprecationError(
|
|
1250
|
+
"The `wrangler preview` command has been deprecated.\n" +
|
|
1251
|
+
"Try using `wrangler dev` to to try out a worker during development.\n"
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
// Delegate to `wrangler dev`
|
|
1256
|
+
console.warn(
|
|
1257
|
+
"***************************************************\n" +
|
|
1258
|
+
"The `wrangler preview` command has been deprecated.\n" +
|
|
1259
|
+
"Attempting to run `wrangler dev` instead.\n" +
|
|
1260
|
+
"***************************************************\n"
|
|
1261
|
+
);
|
|
1262
|
+
|
|
1263
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1264
|
+
const entry = await getEntry({}, config, "dev");
|
|
1265
|
+
|
|
1266
|
+
const accountId = await requireAuth(config);
|
|
1267
|
+
|
|
1268
|
+
const environments = config.env ?? {};
|
|
1269
|
+
const envRootObj = args.env ? environments[args.env] || {} : config;
|
|
1270
|
+
|
|
1271
|
+
const { waitUntilExit } = render(
|
|
1272
|
+
<Dev
|
|
1273
|
+
name={config.name}
|
|
1274
|
+
entry={entry}
|
|
1275
|
+
rules={getRules(config)}
|
|
1276
|
+
env={args.env}
|
|
1277
|
+
zone={undefined}
|
|
1278
|
+
legacyEnv={isLegacyEnv(args, config)}
|
|
1279
|
+
buildCommand={config.build || {}}
|
|
1280
|
+
initialMode={args.local ? "local" : "remote"}
|
|
1281
|
+
jsxFactory={envRootObj?.jsx_factory}
|
|
1282
|
+
jsxFragment={envRootObj?.jsx_fragment}
|
|
1283
|
+
upstreamProtocol={config.dev.upstream_protocol}
|
|
1284
|
+
localProtocol={config.dev.local_protocol}
|
|
1285
|
+
enableLocalPersistence={false}
|
|
1286
|
+
accountId={accountId}
|
|
1287
|
+
assetPaths={undefined}
|
|
1288
|
+
port={config.dev?.port}
|
|
1289
|
+
public={undefined}
|
|
1290
|
+
compatibilityDate={
|
|
1291
|
+
config.compatibility_date ||
|
|
1292
|
+
new Date().toISOString().substring(0, 10)
|
|
1293
|
+
}
|
|
1294
|
+
compatibilityFlags={
|
|
1295
|
+
(args["compatibility-flags"] as string[]) ||
|
|
1296
|
+
config.compatibility_flags
|
|
1297
|
+
}
|
|
1298
|
+
usageModel={config.usage_model}
|
|
1299
|
+
bindings={{
|
|
1300
|
+
kv_namespaces: envRootObj.kv_namespaces?.map(
|
|
1301
|
+
({ binding, preview_id, id: _id }) => {
|
|
1302
|
+
// In `dev`, we make folks use a separate kv namespace called
|
|
1303
|
+
// `preview_id` instead of `id` so that they don't
|
|
1304
|
+
// break production data. So here we check that a `preview_id`
|
|
1305
|
+
// has actually been configured.
|
|
1306
|
+
// This whole block of code will be obsoleted in the future
|
|
1307
|
+
// when we have copy-on-write for previews on edge workers.
|
|
1308
|
+
if (!preview_id) {
|
|
1309
|
+
// TODO: This error has to be a _lot_ better, ideally just asking
|
|
1310
|
+
// to create a preview namespace for the user automatically
|
|
1311
|
+
throw new Error(
|
|
1312
|
+
`In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "${binding}" in your wrangler.toml`
|
|
1313
|
+
); // Ugh, I really don't like this message very much
|
|
1314
|
+
}
|
|
1315
|
+
return {
|
|
1316
|
+
binding,
|
|
1317
|
+
id: preview_id,
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
),
|
|
1321
|
+
vars: envRootObj.vars,
|
|
1322
|
+
wasm_modules: config.wasm_modules,
|
|
1323
|
+
text_blobs: config.text_blobs,
|
|
1324
|
+
durable_objects: envRootObj.durable_objects,
|
|
1325
|
+
r2_buckets: envRootObj.r2_buckets,
|
|
1326
|
+
unsafe: envRootObj.unsafe?.bindings,
|
|
1327
|
+
}}
|
|
1328
|
+
inspectorPort={await getPort({ port: 9229 })}
|
|
1329
|
+
/>
|
|
1330
|
+
);
|
|
1331
|
+
await waitUntilExit();
|
|
1332
|
+
}
|
|
1333
|
+
);
|
|
1334
|
+
|
|
1335
|
+
// [DEPRECATED] route
|
|
1336
|
+
wrangler.command(
|
|
1337
|
+
"route",
|
|
1338
|
+
false, // I think we want to hide this command
|
|
1339
|
+
// "➡️ List or delete worker routes",
|
|
1340
|
+
(routeYargs) => {
|
|
1341
|
+
return routeYargs
|
|
1342
|
+
.command(
|
|
1343
|
+
"list",
|
|
1344
|
+
"List the routes associated with a zone",
|
|
1345
|
+
(yargs) => {
|
|
1346
|
+
return yargs
|
|
1347
|
+
.option("env", {
|
|
1348
|
+
type: "string",
|
|
1349
|
+
describe: "Perform on a specific environment",
|
|
1350
|
+
})
|
|
1351
|
+
.option("zone", {
|
|
1352
|
+
type: "string",
|
|
1353
|
+
describe: "Zone id",
|
|
1354
|
+
})
|
|
1355
|
+
.positional("zone", {
|
|
1356
|
+
describe: "Zone id",
|
|
1357
|
+
type: "string",
|
|
1358
|
+
});
|
|
1359
|
+
},
|
|
1360
|
+
() => {
|
|
1361
|
+
// "👯 [DEPRECATED]. Use wrangler.toml to manage routes.
|
|
1362
|
+
const deprecationNotice =
|
|
1363
|
+
"`wrangler route list` has been deprecated.";
|
|
1364
|
+
const futureRoutes =
|
|
1365
|
+
"Refer to wrangler.toml for a list of routes the worker will be deployed to upon publishing.";
|
|
1366
|
+
const presentRoutes =
|
|
1367
|
+
"Refer to the Cloudflare Dashboard to see the routes this worker is currently running on.";
|
|
1368
|
+
throw new DeprecationError(
|
|
1369
|
+
`${deprecationNotice}\n${futureRoutes}\n${presentRoutes}`
|
|
1370
|
+
);
|
|
1371
|
+
}
|
|
1372
|
+
)
|
|
1373
|
+
.command(
|
|
1374
|
+
"delete",
|
|
1375
|
+
"Delete a route associated with a zone",
|
|
1376
|
+
(yargs) => {
|
|
1377
|
+
return yargs
|
|
1378
|
+
.positional("id", {
|
|
1379
|
+
describe: "The hash of the route ID to delete.",
|
|
1380
|
+
type: "string",
|
|
1381
|
+
})
|
|
1382
|
+
.option("zone", {
|
|
1383
|
+
type: "string",
|
|
1384
|
+
describe: "zone id",
|
|
1385
|
+
})
|
|
1386
|
+
.option("env", {
|
|
1387
|
+
type: "string",
|
|
1388
|
+
describe: "Perform on a specific environment",
|
|
1389
|
+
});
|
|
1390
|
+
},
|
|
1391
|
+
() => {
|
|
1392
|
+
// "👯 [DEPRECATED]. Use wrangler.toml to manage routes.
|
|
1393
|
+
const deprecationNotice =
|
|
1394
|
+
"`wrangler route delete` has been deprecated.";
|
|
1395
|
+
const shouldDo =
|
|
1396
|
+
"Remove the unwanted route(s) from wrangler.toml and run `wrangler publish` to remove your worker from those routes.";
|
|
1397
|
+
throw new DeprecationError(`${deprecationNotice}\n${shouldDo}`);
|
|
1398
|
+
}
|
|
1399
|
+
);
|
|
1400
|
+
},
|
|
1401
|
+
() => {
|
|
1402
|
+
// "👯 [DEPRECATED]. Use wrangler.toml to manage routes.
|
|
1403
|
+
const deprecationNotice = "`wrangler route` has been deprecated.";
|
|
1404
|
+
const shouldDo =
|
|
1405
|
+
"Please use wrangler.toml and/or `wrangler publish --routes` to modify routes";
|
|
1406
|
+
throw new DeprecationError(`${deprecationNotice}\n${shouldDo}`);
|
|
1407
|
+
}
|
|
1408
|
+
);
|
|
1409
|
+
|
|
1410
|
+
// subdomain
|
|
1411
|
+
wrangler.command(
|
|
1412
|
+
"subdomain [name]",
|
|
1413
|
+
false,
|
|
1414
|
+
// "👷 Create or change your workers.dev subdomain.",
|
|
1415
|
+
(yargs) => {
|
|
1416
|
+
return yargs.positional("name", { type: "string" });
|
|
1417
|
+
},
|
|
1418
|
+
() => {
|
|
1419
|
+
throw new DeprecationError(
|
|
1420
|
+
"`wrangler subdomain` has been deprecated, please refer to TODO://some/path for alternatives"
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
);
|
|
1424
|
+
|
|
1425
|
+
// secret
|
|
1426
|
+
wrangler.command(
|
|
1427
|
+
"secret",
|
|
1428
|
+
"🤫 Generate a secret that can be referenced in the worker script",
|
|
1429
|
+
(secretYargs) => {
|
|
1430
|
+
return secretYargs
|
|
1431
|
+
.command(subHelp)
|
|
1432
|
+
.command(
|
|
1433
|
+
"put <key>",
|
|
1434
|
+
"Create or update a secret variable for a script",
|
|
1435
|
+
(yargs) => {
|
|
1436
|
+
return yargs
|
|
1437
|
+
.positional("key", {
|
|
1438
|
+
describe: "The variable name to be accessible in the script",
|
|
1439
|
+
type: "string",
|
|
1440
|
+
})
|
|
1441
|
+
.option("name", {
|
|
1442
|
+
describe: "Name of the worker",
|
|
1443
|
+
type: "string",
|
|
1444
|
+
})
|
|
1445
|
+
.option("env", {
|
|
1446
|
+
type: "string",
|
|
1447
|
+
describe:
|
|
1448
|
+
"Binds the secret to the Worker of the specific environment",
|
|
1449
|
+
});
|
|
1450
|
+
},
|
|
1451
|
+
async (args) => {
|
|
1452
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1453
|
+
|
|
1454
|
+
const scriptName = getScriptName(args, config);
|
|
1455
|
+
if (!scriptName) {
|
|
1456
|
+
throw new Error("Missing script name");
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
const isInteractive = process.stdin.isTTY;
|
|
1460
|
+
const accountId = await requireAuth(config, isInteractive);
|
|
1461
|
+
|
|
1462
|
+
const secretValue = isInteractive
|
|
1463
|
+
? await prompt("Enter a secret value:", "password")
|
|
1464
|
+
: await readFromStdin();
|
|
1465
|
+
|
|
1466
|
+
console.log(
|
|
1467
|
+
`🌀 Creating the secret for script ${scriptName} ${
|
|
1468
|
+
args.env && !isLegacyEnv(args, config) ? `(${args.env})` : ""
|
|
1469
|
+
}`
|
|
1470
|
+
);
|
|
1471
|
+
|
|
1472
|
+
async function submitSecret() {
|
|
1473
|
+
const url =
|
|
1474
|
+
!args.env || isLegacyEnv(args, config)
|
|
1475
|
+
? `/accounts/${accountId}/workers/scripts/${scriptName}/secrets`
|
|
1476
|
+
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;
|
|
1477
|
+
|
|
1478
|
+
return await fetchResult(url, {
|
|
1479
|
+
method: "PUT",
|
|
1480
|
+
headers: { "Content-Type": "application/json" },
|
|
1481
|
+
body: JSON.stringify({
|
|
1482
|
+
name: args.key,
|
|
1483
|
+
text: secretValue,
|
|
1484
|
+
type: "secret_text",
|
|
1485
|
+
}),
|
|
1486
|
+
});
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
const createDraftWorker = async () => {
|
|
1490
|
+
// TODO: log a warning
|
|
1491
|
+
await fetchResult(
|
|
1492
|
+
!args["legacy-env"] && args.env
|
|
1493
|
+
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}`
|
|
1494
|
+
: `/accounts/${accountId}/workers/scripts/${scriptName}`,
|
|
1495
|
+
{
|
|
1496
|
+
method: "PUT",
|
|
1497
|
+
body: createWorkerUploadForm({
|
|
1498
|
+
name: scriptName,
|
|
1499
|
+
main: {
|
|
1500
|
+
name: scriptName,
|
|
1501
|
+
content: `export default { fetch() {} }`,
|
|
1502
|
+
type: "esm",
|
|
1503
|
+
},
|
|
1504
|
+
bindings: {
|
|
1505
|
+
kv_namespaces: [],
|
|
1506
|
+
vars: {},
|
|
1507
|
+
durable_objects: { bindings: [] },
|
|
1508
|
+
r2_buckets: [],
|
|
1509
|
+
wasm_modules: {},
|
|
1510
|
+
text_blobs: {},
|
|
1511
|
+
unsafe: [],
|
|
1512
|
+
},
|
|
1513
|
+
modules: [],
|
|
1514
|
+
migrations: undefined,
|
|
1515
|
+
compatibility_date: undefined,
|
|
1516
|
+
compatibility_flags: undefined,
|
|
1517
|
+
usage_model: undefined,
|
|
1518
|
+
}),
|
|
1519
|
+
}
|
|
1520
|
+
);
|
|
1521
|
+
};
|
|
1522
|
+
|
|
1523
|
+
function isMissingWorkerError(e: unknown): e is { code: 10007 } {
|
|
1524
|
+
return (
|
|
1525
|
+
typeof e === "object" &&
|
|
1526
|
+
e !== null &&
|
|
1527
|
+
(e as { code: 10007 }).code === 10007
|
|
1528
|
+
);
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
try {
|
|
1532
|
+
await submitSecret();
|
|
1533
|
+
} catch (e) {
|
|
1534
|
+
if (isMissingWorkerError(e)) {
|
|
1535
|
+
// create a draft worker and try again
|
|
1536
|
+
await createDraftWorker();
|
|
1537
|
+
await submitSecret();
|
|
1538
|
+
// TODO: delete the draft worker if this failed too?
|
|
1539
|
+
} else {
|
|
1540
|
+
throw e;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
console.log(`✨ Success! Uploaded secret ${args.key}`);
|
|
1545
|
+
}
|
|
1546
|
+
)
|
|
1547
|
+
.command(
|
|
1548
|
+
"delete <key>",
|
|
1549
|
+
"Delete a secret variable from a script",
|
|
1550
|
+
(yargs) => {
|
|
1551
|
+
return yargs
|
|
1552
|
+
.positional("key", {
|
|
1553
|
+
describe: "The variable name to be accessible in the script",
|
|
1554
|
+
type: "string",
|
|
1555
|
+
})
|
|
1556
|
+
.option("name", {
|
|
1557
|
+
describe: "Name of the worker",
|
|
1558
|
+
type: "string",
|
|
1559
|
+
})
|
|
1560
|
+
.option("env", {
|
|
1561
|
+
type: "string",
|
|
1562
|
+
describe:
|
|
1563
|
+
"Binds the secret to the Worker of the specific environment",
|
|
1564
|
+
});
|
|
1565
|
+
},
|
|
1566
|
+
async (args) => {
|
|
1567
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1568
|
+
|
|
1569
|
+
const scriptName = getScriptName(args, config);
|
|
1570
|
+
if (!scriptName) {
|
|
1571
|
+
throw new Error("Missing script name");
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
const accountId = await requireAuth(config);
|
|
1575
|
+
|
|
1576
|
+
if (
|
|
1577
|
+
await confirm(
|
|
1578
|
+
`Are you sure you want to permanently delete the variable ${
|
|
1579
|
+
args.key
|
|
1580
|
+
} on the script ${scriptName}${
|
|
1581
|
+
args.env && !isLegacyEnv(args, config) ? ` (${args.env})` : ""
|
|
1582
|
+
}?`
|
|
1583
|
+
)
|
|
1584
|
+
) {
|
|
1585
|
+
console.log(
|
|
1586
|
+
`🌀 Deleting the secret ${args.key} on script ${scriptName}${
|
|
1587
|
+
args.env && !isLegacyEnv(args, config) ? ` (${args.env})` : ""
|
|
1588
|
+
}`
|
|
1589
|
+
);
|
|
1590
|
+
|
|
1591
|
+
const url =
|
|
1592
|
+
!args.env || isLegacyEnv(args, config)
|
|
1593
|
+
? `/accounts/${accountId}/workers/scripts/${scriptName}/secrets`
|
|
1594
|
+
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;
|
|
1595
|
+
|
|
1596
|
+
await fetchResult(`${url}/${args.key}`, { method: "DELETE" });
|
|
1597
|
+
console.log(`✨ Success! Deleted secret ${args.key}`);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
)
|
|
1601
|
+
.command(
|
|
1602
|
+
"list",
|
|
1603
|
+
"List all secrets for a script",
|
|
1604
|
+
(yargs) => {
|
|
1605
|
+
return yargs
|
|
1606
|
+
.option("name", {
|
|
1607
|
+
describe: "Name of the worker",
|
|
1608
|
+
type: "string",
|
|
1609
|
+
})
|
|
1610
|
+
.option("env", {
|
|
1611
|
+
type: "string",
|
|
1612
|
+
describe:
|
|
1613
|
+
"Binds the secret to the Worker of the specific environment.",
|
|
1614
|
+
});
|
|
1615
|
+
},
|
|
1616
|
+
async (args) => {
|
|
1617
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1618
|
+
|
|
1619
|
+
const scriptName = getScriptName(args, config);
|
|
1620
|
+
if (!scriptName) {
|
|
1621
|
+
throw new Error("Missing script name");
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
const accountId = await requireAuth(config);
|
|
1625
|
+
|
|
1626
|
+
const url =
|
|
1627
|
+
!args.env || isLegacyEnv(args, config)
|
|
1628
|
+
? `/accounts/${accountId}/workers/scripts/${scriptName}/secrets`
|
|
1629
|
+
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;
|
|
1630
|
+
|
|
1631
|
+
console.log(JSON.stringify(await fetchResult(url), null, " "));
|
|
1632
|
+
}
|
|
1633
|
+
);
|
|
1634
|
+
}
|
|
1635
|
+
);
|
|
1636
|
+
|
|
1637
|
+
// kv
|
|
1638
|
+
// :namespace
|
|
1639
|
+
wrangler.command(
|
|
1640
|
+
"kv:namespace",
|
|
1641
|
+
"🗂️ Interact with your Workers KV Namespaces",
|
|
1642
|
+
(namespaceYargs) => {
|
|
1643
|
+
return namespaceYargs
|
|
1644
|
+
.command(subHelp)
|
|
1645
|
+
.command(
|
|
1646
|
+
"create <namespace>",
|
|
1647
|
+
"Create a new namespace",
|
|
1648
|
+
(yargs) => {
|
|
1649
|
+
return yargs
|
|
1650
|
+
.positional("namespace", {
|
|
1651
|
+
describe: "The name of the new namespace",
|
|
1652
|
+
type: "string",
|
|
1653
|
+
demandOption: true,
|
|
1654
|
+
})
|
|
1655
|
+
.option("env", {
|
|
1656
|
+
type: "string",
|
|
1657
|
+
describe: "Perform on a specific environment",
|
|
1658
|
+
})
|
|
1659
|
+
.option("preview", {
|
|
1660
|
+
type: "boolean",
|
|
1661
|
+
describe: "Interact with a preview namespace",
|
|
1662
|
+
});
|
|
1663
|
+
},
|
|
1664
|
+
async (args) => {
|
|
1665
|
+
if (args._.length > 2) {
|
|
1666
|
+
const extraArgs = args._.slice(2).join(" ");
|
|
1667
|
+
throw new CommandLineArgsError(
|
|
1668
|
+
`Unexpected additional positional arguments "${extraArgs}".`
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
if (!isValidNamespaceBinding(args.namespace)) {
|
|
1673
|
+
throw new CommandLineArgsError(
|
|
1674
|
+
`The namespace binding name "${args.namespace}" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.`
|
|
1675
|
+
);
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1679
|
+
if (!config.name) {
|
|
1680
|
+
console.warn(
|
|
1681
|
+
"No configured name present, using `worker` as a prefix for the title"
|
|
1682
|
+
);
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
const name = config.name || "worker";
|
|
1686
|
+
const environment = args.env ? `-${args.env}` : "";
|
|
1687
|
+
const preview = args.preview ? "_preview" : "";
|
|
1688
|
+
const title = `${name}${environment}-${args.namespace}${preview}`;
|
|
1689
|
+
|
|
1690
|
+
const accountId = await requireAuth(config);
|
|
1691
|
+
|
|
1692
|
+
// TODO: generate a binding name stripping non alphanumeric chars
|
|
1693
|
+
|
|
1694
|
+
console.log(`🌀 Creating namespace with title "${title}"`);
|
|
1695
|
+
const namespaceId = await createNamespace(accountId, title);
|
|
1696
|
+
|
|
1697
|
+
console.log("✨ Success!");
|
|
1698
|
+
const envString = args.env ? ` under [env.${args.env}]` : "";
|
|
1699
|
+
const previewString = args.preview ? "preview_" : "";
|
|
1700
|
+
console.log(
|
|
1701
|
+
`Add the following to your configuration file in your kv_namespaces array${envString}:`
|
|
1702
|
+
);
|
|
1703
|
+
console.log(
|
|
1704
|
+
`{ binding = "${args.namespace}", ${previewString}id = "${namespaceId}" }`
|
|
1705
|
+
);
|
|
1706
|
+
|
|
1707
|
+
// TODO: automatically write this block to the wrangler.toml config file??
|
|
1708
|
+
}
|
|
1709
|
+
)
|
|
1710
|
+
.command(
|
|
1711
|
+
"list",
|
|
1712
|
+
"Outputs a list of all KV namespaces associated with your account id.",
|
|
1713
|
+
{},
|
|
1714
|
+
async (args) => {
|
|
1715
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1716
|
+
|
|
1717
|
+
const accountId = await requireAuth(config);
|
|
1718
|
+
|
|
1719
|
+
// TODO: we should show bindings if they exist for given ids
|
|
1720
|
+
|
|
1721
|
+
console.log(
|
|
1722
|
+
JSON.stringify(await listNamespaces(accountId), null, " ")
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
)
|
|
1726
|
+
.command(
|
|
1727
|
+
"delete",
|
|
1728
|
+
"Deletes a given namespace.",
|
|
1729
|
+
(yargs) => {
|
|
1730
|
+
return yargs
|
|
1731
|
+
.option("binding", {
|
|
1732
|
+
type: "string",
|
|
1733
|
+
describe: "The name of the namespace to delete",
|
|
1734
|
+
})
|
|
1735
|
+
.option("namespace-id", {
|
|
1736
|
+
type: "string",
|
|
1737
|
+
describe: "The id of the namespace to delete",
|
|
1738
|
+
})
|
|
1739
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
1740
|
+
.option("env", {
|
|
1741
|
+
type: "string",
|
|
1742
|
+
describe: "Perform on a specific environment",
|
|
1743
|
+
})
|
|
1744
|
+
.option("preview", {
|
|
1745
|
+
type: "boolean",
|
|
1746
|
+
describe: "Interact with a preview namespace",
|
|
1747
|
+
});
|
|
1748
|
+
},
|
|
1749
|
+
async (args) => {
|
|
1750
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1751
|
+
|
|
1752
|
+
let id;
|
|
1753
|
+
try {
|
|
1754
|
+
id = getNamespaceId(args, config);
|
|
1755
|
+
} catch (e) {
|
|
1756
|
+
throw new CommandLineArgsError(
|
|
1757
|
+
"Not able to delete namespace.\n" + ((e as Error).message ?? e)
|
|
1758
|
+
);
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
const accountId = await requireAuth(config);
|
|
1762
|
+
|
|
1763
|
+
await fetchResult<{ id: string }>(
|
|
1764
|
+
`/accounts/${accountId}/storage/kv/namespaces/${id}`,
|
|
1765
|
+
{ method: "DELETE" }
|
|
1766
|
+
);
|
|
1767
|
+
|
|
1768
|
+
// TODO: recommend they remove it from wrangler.toml
|
|
1769
|
+
|
|
1770
|
+
// test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
|
|
1771
|
+
// Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
|
|
1772
|
+
// n
|
|
1773
|
+
// 💁 Not deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
|
|
1774
|
+
// ➜ test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
|
|
1775
|
+
// Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
|
|
1776
|
+
// y
|
|
1777
|
+
// 🌀 Deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
|
|
1778
|
+
// ✨ Success
|
|
1779
|
+
// ⚠️ Make sure to remove this "kv-namespace" entry from your configuration file!
|
|
1780
|
+
// ➜ test-mf
|
|
1781
|
+
|
|
1782
|
+
// TODO: do it automatically
|
|
1783
|
+
|
|
1784
|
+
// TODO: delete the preview namespace as well?
|
|
1785
|
+
}
|
|
1786
|
+
);
|
|
1787
|
+
}
|
|
1788
|
+
);
|
|
1789
|
+
|
|
1790
|
+
// :key
|
|
1791
|
+
wrangler.command(
|
|
1792
|
+
"kv:key",
|
|
1793
|
+
"🔑 Individually manage Workers KV key-value pairs",
|
|
1794
|
+
(kvKeyYargs) => {
|
|
1795
|
+
return kvKeyYargs
|
|
1796
|
+
.command(subHelp)
|
|
1797
|
+
.command(
|
|
1798
|
+
"put <key> [value]",
|
|
1799
|
+
"Writes a single key/value pair to the given namespace.",
|
|
1800
|
+
(yargs) => {
|
|
1801
|
+
return yargs
|
|
1802
|
+
.positional("key", {
|
|
1803
|
+
type: "string",
|
|
1804
|
+
describe: "The key to write to",
|
|
1805
|
+
demandOption: true,
|
|
1806
|
+
})
|
|
1807
|
+
.positional("value", {
|
|
1808
|
+
type: "string",
|
|
1809
|
+
describe: "The value to write",
|
|
1810
|
+
})
|
|
1811
|
+
.option("binding", {
|
|
1812
|
+
type: "string",
|
|
1813
|
+
describe: "The binding of the namespace to write to",
|
|
1814
|
+
})
|
|
1815
|
+
.option("namespace-id", {
|
|
1816
|
+
type: "string",
|
|
1817
|
+
describe: "The id of the namespace to write to",
|
|
1818
|
+
})
|
|
1819
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
1820
|
+
.option("env", {
|
|
1821
|
+
type: "string",
|
|
1822
|
+
describe: "Perform on a specific environment",
|
|
1823
|
+
})
|
|
1824
|
+
.option("preview", {
|
|
1825
|
+
type: "boolean",
|
|
1826
|
+
describe: "Interact with a preview namespace",
|
|
1827
|
+
})
|
|
1828
|
+
.option("ttl", {
|
|
1829
|
+
type: "number",
|
|
1830
|
+
describe: "Time for which the entries should be visible",
|
|
1831
|
+
})
|
|
1832
|
+
.option("expiration", {
|
|
1833
|
+
type: "number",
|
|
1834
|
+
describe:
|
|
1835
|
+
"Time since the UNIX epoch after which the entry expires",
|
|
1836
|
+
})
|
|
1837
|
+
.option("path", {
|
|
1838
|
+
type: "string",
|
|
1839
|
+
describe: "Read value from the file at a given path",
|
|
1840
|
+
})
|
|
1841
|
+
.check(demandOneOfOption("value", "path"));
|
|
1842
|
+
},
|
|
1843
|
+
async ({ key, ttl, expiration, ...args }) => {
|
|
1844
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1845
|
+
const namespaceId = getNamespaceId(args, config);
|
|
1846
|
+
// One of `args.path` and `args.value` must be defined
|
|
1847
|
+
const value = args.path
|
|
1848
|
+
? readFileSync(args.path)
|
|
1849
|
+
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1850
|
+
args.value!;
|
|
1851
|
+
|
|
1852
|
+
if (args.path) {
|
|
1853
|
+
console.log(
|
|
1854
|
+
`writing the contents of ${args.path} to the key "${key}" on namespace ${namespaceId}`
|
|
1855
|
+
);
|
|
1856
|
+
} else {
|
|
1857
|
+
console.log(
|
|
1858
|
+
`writing the value "${value}" to key "${key}" on namespace ${namespaceId}`
|
|
1859
|
+
);
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
const accountId = await requireAuth(config);
|
|
1863
|
+
|
|
1864
|
+
await putKeyValue(accountId, namespaceId, {
|
|
1865
|
+
key,
|
|
1866
|
+
value,
|
|
1867
|
+
expiration,
|
|
1868
|
+
expiration_ttl: ttl,
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
)
|
|
1872
|
+
.command(
|
|
1873
|
+
"list",
|
|
1874
|
+
"Outputs a list of all keys in a given namespace.",
|
|
1875
|
+
(yargs) => {
|
|
1876
|
+
return yargs
|
|
1877
|
+
.option("binding", {
|
|
1878
|
+
type: "string",
|
|
1879
|
+
describe: "The name of the namespace to list",
|
|
1880
|
+
})
|
|
1881
|
+
.option("namespace-id", {
|
|
1882
|
+
type: "string",
|
|
1883
|
+
describe: "The id of the namespace to list",
|
|
1884
|
+
})
|
|
1885
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
1886
|
+
.option("env", {
|
|
1887
|
+
type: "string",
|
|
1888
|
+
describe: "Perform on a specific environment",
|
|
1889
|
+
})
|
|
1890
|
+
.option("preview", {
|
|
1891
|
+
type: "boolean",
|
|
1892
|
+
// In the case of listing keys we will default to non-preview mode
|
|
1893
|
+
default: false,
|
|
1894
|
+
describe: "Interact with a preview namespace",
|
|
1895
|
+
})
|
|
1896
|
+
.option("prefix", {
|
|
1897
|
+
type: "string",
|
|
1898
|
+
describe: "A prefix to filter listed keys",
|
|
1899
|
+
});
|
|
1900
|
+
},
|
|
1901
|
+
async ({ prefix, ...args }) => {
|
|
1902
|
+
// TODO: support for limit+cursor (pagination)
|
|
1903
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1904
|
+
const namespaceId = getNamespaceId(args, config);
|
|
1905
|
+
|
|
1906
|
+
const accountId = await requireAuth(config);
|
|
1907
|
+
|
|
1908
|
+
const results = await listNamespaceKeys(
|
|
1909
|
+
accountId,
|
|
1910
|
+
namespaceId,
|
|
1911
|
+
prefix
|
|
1912
|
+
);
|
|
1913
|
+
console.log(JSON.stringify(results, undefined, 2));
|
|
1914
|
+
}
|
|
1915
|
+
)
|
|
1916
|
+
.command(
|
|
1917
|
+
"get <key>",
|
|
1918
|
+
"Reads a single value by key from the given namespace.",
|
|
1919
|
+
(yargs) => {
|
|
1920
|
+
return yargs
|
|
1921
|
+
.positional("key", {
|
|
1922
|
+
describe: "The key value to get.",
|
|
1923
|
+
type: "string",
|
|
1924
|
+
demandOption: true,
|
|
1925
|
+
})
|
|
1926
|
+
.option("binding", {
|
|
1927
|
+
type: "string",
|
|
1928
|
+
describe: "The name of the namespace to get from",
|
|
1929
|
+
})
|
|
1930
|
+
.option("namespace-id", {
|
|
1931
|
+
type: "string",
|
|
1932
|
+
describe: "The id of the namespace to get from",
|
|
1933
|
+
})
|
|
1934
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
1935
|
+
.option("env", {
|
|
1936
|
+
type: "string",
|
|
1937
|
+
describe: "Perform on a specific environment",
|
|
1938
|
+
})
|
|
1939
|
+
.option("preview", {
|
|
1940
|
+
type: "boolean",
|
|
1941
|
+
describe: "Interact with a preview namespace",
|
|
1942
|
+
})
|
|
1943
|
+
.option("preview", {
|
|
1944
|
+
type: "boolean",
|
|
1945
|
+
// In the case of getting key values we will default to non-preview mode
|
|
1946
|
+
default: false,
|
|
1947
|
+
describe: "Interact with a preview namespace",
|
|
1948
|
+
});
|
|
1949
|
+
},
|
|
1950
|
+
async ({ key, ...args }) => {
|
|
1951
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1952
|
+
const namespaceId = getNamespaceId(args, config);
|
|
1953
|
+
|
|
1954
|
+
const accountId = await requireAuth(config);
|
|
1955
|
+
|
|
1956
|
+
console.log(await getKeyValue(accountId, namespaceId, key));
|
|
1957
|
+
}
|
|
1958
|
+
)
|
|
1959
|
+
.command(
|
|
1960
|
+
"delete <key>",
|
|
1961
|
+
"Removes a single key value pair from the given namespace.",
|
|
1962
|
+
(yargs) => {
|
|
1963
|
+
return yargs
|
|
1964
|
+
.positional("key", {
|
|
1965
|
+
describe: "The key value to delete",
|
|
1966
|
+
type: "string",
|
|
1967
|
+
demandOption: true,
|
|
1968
|
+
})
|
|
1969
|
+
.option("binding", {
|
|
1970
|
+
type: "string",
|
|
1971
|
+
describe: "The name of the namespace to delete from",
|
|
1972
|
+
})
|
|
1973
|
+
.option("namespace-id", {
|
|
1974
|
+
type: "string",
|
|
1975
|
+
describe: "The id of the namespace to delete from",
|
|
1976
|
+
})
|
|
1977
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
1978
|
+
.option("env", {
|
|
1979
|
+
type: "string",
|
|
1980
|
+
describe: "Perform on a specific environment",
|
|
1981
|
+
})
|
|
1982
|
+
.option("preview", {
|
|
1983
|
+
type: "boolean",
|
|
1984
|
+
describe: "Interact with a preview namespace",
|
|
1985
|
+
});
|
|
1986
|
+
},
|
|
1987
|
+
async ({ key, ...args }) => {
|
|
1988
|
+
const config = readConfig(args.config as ConfigPath);
|
|
1989
|
+
const namespaceId = getNamespaceId(args, config);
|
|
1990
|
+
|
|
1991
|
+
console.log(
|
|
1992
|
+
`deleting the key "${key}" on namespace ${namespaceId}`
|
|
1993
|
+
);
|
|
1994
|
+
|
|
1995
|
+
const accountId = await requireAuth(config);
|
|
1996
|
+
|
|
1997
|
+
await fetchResult(
|
|
1998
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
|
|
1999
|
+
{ method: "DELETE" }
|
|
2000
|
+
);
|
|
2001
|
+
}
|
|
2002
|
+
);
|
|
2003
|
+
}
|
|
2004
|
+
);
|
|
2005
|
+
|
|
2006
|
+
// :bulk
|
|
2007
|
+
wrangler.command(
|
|
2008
|
+
"kv:bulk",
|
|
2009
|
+
"💪 Interact with multiple Workers KV key-value pairs at once",
|
|
2010
|
+
(kvBulkYargs) => {
|
|
2011
|
+
return kvBulkYargs
|
|
2012
|
+
.command(subHelp)
|
|
2013
|
+
.command(
|
|
2014
|
+
"put <filename>",
|
|
2015
|
+
"Upload multiple key-value pairs to a namespace",
|
|
2016
|
+
(yargs) => {
|
|
2017
|
+
return yargs
|
|
2018
|
+
.positional("filename", {
|
|
2019
|
+
describe: `The JSON file of key-value pairs to upload, in form [{"key":..., "value":...}"...]`,
|
|
2020
|
+
type: "string",
|
|
2021
|
+
demandOption: true,
|
|
2022
|
+
})
|
|
2023
|
+
.option("binding", {
|
|
2024
|
+
type: "string",
|
|
2025
|
+
describe: "The name of the namespace to insert values into",
|
|
2026
|
+
})
|
|
2027
|
+
.option("namespace-id", {
|
|
2028
|
+
type: "string",
|
|
2029
|
+
describe: "The id of the namespace to insert values into",
|
|
2030
|
+
})
|
|
2031
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
2032
|
+
.option("env", {
|
|
2033
|
+
type: "string",
|
|
2034
|
+
describe: "Perform on a specific environment",
|
|
2035
|
+
})
|
|
2036
|
+
.option("preview", {
|
|
2037
|
+
type: "boolean",
|
|
2038
|
+
describe: "Interact with a preview namespace",
|
|
2039
|
+
});
|
|
2040
|
+
},
|
|
2041
|
+
async ({ filename, ...args }) => {
|
|
2042
|
+
// The simplest implementation I could think of.
|
|
2043
|
+
// This could be made more efficient with a streaming parser/uploader
|
|
2044
|
+
// but we'll do that in the future if needed.
|
|
2045
|
+
|
|
2046
|
+
const config = readConfig(args.config as ConfigPath);
|
|
2047
|
+
const namespaceId = getNamespaceId(args, config);
|
|
2048
|
+
const content = parseJSON(readFileSync(filename), filename);
|
|
2049
|
+
|
|
2050
|
+
if (!Array.isArray(content)) {
|
|
2051
|
+
throw new Error(
|
|
2052
|
+
`Unexpected JSON input from "${filename}".\n` +
|
|
2053
|
+
`Expected an array of key-value objects but got type "${typeof content}".`
|
|
2054
|
+
);
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
const errors: string[] = [];
|
|
2058
|
+
const warnings: string[] = [];
|
|
2059
|
+
for (let i = 0; i < content.length; i++) {
|
|
2060
|
+
const keyValue = content[i];
|
|
2061
|
+
if (typeof keyValue !== "object") {
|
|
2062
|
+
errors.push(
|
|
2063
|
+
`The item at index ${i} is type: "${typeof keyValue}" - ${JSON.stringify(
|
|
2064
|
+
keyValue
|
|
2065
|
+
)}`
|
|
2066
|
+
);
|
|
2067
|
+
} else if (!isKeyValue(keyValue)) {
|
|
2068
|
+
errors.push(
|
|
2069
|
+
`The item at index ${i} is ${JSON.stringify(keyValue)}`
|
|
2070
|
+
);
|
|
2071
|
+
} else {
|
|
2072
|
+
const props = unexpectedKeyValueProps(keyValue);
|
|
2073
|
+
if (props.length > 0) {
|
|
2074
|
+
warnings.push(
|
|
2075
|
+
`The item at index ${i} contains unexpected properties: ${JSON.stringify(
|
|
2076
|
+
props
|
|
2077
|
+
)}.`
|
|
2078
|
+
);
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
if (warnings.length > 0) {
|
|
2083
|
+
console.warn(
|
|
2084
|
+
`Unexpected key-value properties in "${filename}".\n` +
|
|
2085
|
+
warnings.join("\n")
|
|
2086
|
+
);
|
|
2087
|
+
}
|
|
2088
|
+
if (errors.length > 0) {
|
|
2089
|
+
throw new Error(
|
|
2090
|
+
`Unexpected JSON input from "${filename}".\n` +
|
|
2091
|
+
`Each item in the array should be an object that matches:\n\n` +
|
|
2092
|
+
`interface KeyValue {\n` +
|
|
2093
|
+
` key: string;\n` +
|
|
2094
|
+
` value: string;\n` +
|
|
2095
|
+
` expiration?: number;\n` +
|
|
2096
|
+
` expiration_ttl?: number;\n` +
|
|
2097
|
+
` metadata?: object;\n` +
|
|
2098
|
+
` base64?: boolean;\n` +
|
|
2099
|
+
`}\n\n` +
|
|
2100
|
+
errors.join("\n")
|
|
2101
|
+
);
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
const accountId = await requireAuth(config);
|
|
2105
|
+
await putBulkKeyValue(
|
|
2106
|
+
accountId,
|
|
2107
|
+
namespaceId,
|
|
2108
|
+
content,
|
|
2109
|
+
(index, total) => {
|
|
2110
|
+
console.log(`Uploaded ${index} of ${total}.`);
|
|
2111
|
+
}
|
|
2112
|
+
);
|
|
2113
|
+
|
|
2114
|
+
console.log("Success!");
|
|
2115
|
+
}
|
|
2116
|
+
)
|
|
2117
|
+
.command(
|
|
2118
|
+
"delete <filename>",
|
|
2119
|
+
"Delete multiple key-value pairs from a namespace",
|
|
2120
|
+
(yargs) => {
|
|
2121
|
+
return yargs
|
|
2122
|
+
.positional("filename", {
|
|
2123
|
+
describe: `The JSON file of keys to delete, in the form ["key1", "key2", ...]`,
|
|
2124
|
+
type: "string",
|
|
2125
|
+
demandOption: true,
|
|
2126
|
+
})
|
|
2127
|
+
.option("binding", {
|
|
2128
|
+
type: "string",
|
|
2129
|
+
describe: "The name of the namespace to delete from",
|
|
2130
|
+
})
|
|
2131
|
+
.option("namespace-id", {
|
|
2132
|
+
type: "string",
|
|
2133
|
+
describe: "The id of the namespace to delete from",
|
|
2134
|
+
})
|
|
2135
|
+
.check(demandOneOfOption("binding", "namespace-id"))
|
|
2136
|
+
.option("env", {
|
|
2137
|
+
type: "string",
|
|
2138
|
+
describe: "Perform on a specific environment",
|
|
2139
|
+
})
|
|
2140
|
+
.option("preview", {
|
|
2141
|
+
type: "boolean",
|
|
2142
|
+
describe: "Interact with a preview namespace",
|
|
2143
|
+
})
|
|
2144
|
+
.option("force", {
|
|
2145
|
+
type: "boolean",
|
|
2146
|
+
alias: "f",
|
|
2147
|
+
describe: "Do not ask for confirmation before deleting",
|
|
2148
|
+
});
|
|
2149
|
+
},
|
|
2150
|
+
async ({ filename, ...args }) => {
|
|
2151
|
+
const config = readConfig(args.config as ConfigPath);
|
|
2152
|
+
const namespaceId = getNamespaceId(args, config);
|
|
2153
|
+
|
|
2154
|
+
if (!args.force) {
|
|
2155
|
+
const result = await confirm(
|
|
2156
|
+
`Are you sure you want to delete all the keys read from "${filename}" from kv-namespace with id "${namespaceId}"?`
|
|
2157
|
+
);
|
|
2158
|
+
if (!result) {
|
|
2159
|
+
console.log(`Not deleting keys read from "${filename}".`);
|
|
2160
|
+
return;
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
const content = parseJSON(
|
|
2165
|
+
readFileSync(filename),
|
|
2166
|
+
filename
|
|
2167
|
+
) as string[];
|
|
2168
|
+
|
|
2169
|
+
if (!Array.isArray(content)) {
|
|
2170
|
+
throw new Error(
|
|
2171
|
+
`Unexpected JSON input from "${filename}".\n` +
|
|
2172
|
+
`Expected an array of strings but got:\n${content}`
|
|
2173
|
+
);
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
const errors: string[] = [];
|
|
2177
|
+
for (let i = 0; i < content.length; i++) {
|
|
2178
|
+
const key = content[i];
|
|
2179
|
+
if (typeof key !== "string") {
|
|
2180
|
+
errors.push(
|
|
2181
|
+
`The item at index ${i} is type: "${typeof key}" - ${JSON.stringify(
|
|
2182
|
+
key
|
|
2183
|
+
)}`
|
|
2184
|
+
);
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
if (errors.length > 0) {
|
|
2188
|
+
throw new Error(
|
|
2189
|
+
`Unexpected JSON input from "${filename}".\n` +
|
|
2190
|
+
`Expected an array of strings.\n` +
|
|
2191
|
+
errors.join("\n")
|
|
2192
|
+
);
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
const accountId = await requireAuth(config);
|
|
2196
|
+
|
|
2197
|
+
await deleteBulkKeyValue(
|
|
2198
|
+
accountId,
|
|
2199
|
+
namespaceId,
|
|
2200
|
+
content,
|
|
2201
|
+
(index, total) => {
|
|
2202
|
+
console.log(`Deleted ${index} of ${total}.`);
|
|
2203
|
+
}
|
|
2204
|
+
);
|
|
2205
|
+
|
|
2206
|
+
console.log("Success!");
|
|
2207
|
+
}
|
|
2208
|
+
);
|
|
2209
|
+
}
|
|
2210
|
+
);
|
|
2211
|
+
|
|
2212
|
+
wrangler.command("pages", "⚡️ Configure Cloudflare Pages", (pagesYargs) =>
|
|
2213
|
+
pages(pagesYargs.command(subHelp))
|
|
2214
|
+
);
|
|
2215
|
+
|
|
2216
|
+
wrangler.command("r2", "📦 Interact with an R2 store", (r2Yargs) => {
|
|
2217
|
+
return r2Yargs
|
|
2218
|
+
.command(subHelp)
|
|
2219
|
+
.command("bucket", "Manage R2 buckets", (r2BucketYargs) => {
|
|
2220
|
+
r2BucketYargs.command(
|
|
2221
|
+
"create <name>",
|
|
2222
|
+
"Create a new R2 bucket",
|
|
2223
|
+
(yargs) => {
|
|
2224
|
+
return yargs.positional("name", {
|
|
2225
|
+
describe: "The name of the new bucket",
|
|
2226
|
+
type: "string",
|
|
2227
|
+
demandOption: true,
|
|
2228
|
+
});
|
|
2229
|
+
},
|
|
2230
|
+
async (args) => {
|
|
2231
|
+
// We expect three values in `_`: `r2`, `bucket`, `create`.
|
|
2232
|
+
if (args._.length > 3) {
|
|
2233
|
+
const extraArgs = args._.slice(3).join(" ");
|
|
2234
|
+
throw new CommandLineArgsError(
|
|
2235
|
+
`Unexpected additional positional arguments "${extraArgs}".`
|
|
2236
|
+
);
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
const config = readConfig(args.config as ConfigPath);
|
|
2240
|
+
|
|
2241
|
+
const accountId = await requireAuth(config);
|
|
2242
|
+
|
|
2243
|
+
console.log(`Creating bucket ${args.name}.`);
|
|
2244
|
+
await createR2Bucket(accountId, args.name);
|
|
2245
|
+
console.log(`Created bucket ${args.name}.`);
|
|
2246
|
+
}
|
|
2247
|
+
);
|
|
2248
|
+
|
|
2249
|
+
r2BucketYargs.command("list", "List R2 buckets", {}, async (args) => {
|
|
2250
|
+
const config = readConfig(args.config as ConfigPath);
|
|
2251
|
+
|
|
2252
|
+
const accountId = await requireAuth(config);
|
|
2253
|
+
|
|
2254
|
+
console.log(JSON.stringify(await listR2Buckets(accountId), null, 2));
|
|
2255
|
+
});
|
|
2256
|
+
|
|
2257
|
+
r2BucketYargs.command(
|
|
2258
|
+
"delete <name>",
|
|
2259
|
+
"Delete an R2 bucket",
|
|
2260
|
+
(yargs) => {
|
|
2261
|
+
return yargs.positional("name", {
|
|
2262
|
+
describe: "The name of the bucket to delete",
|
|
2263
|
+
type: "string",
|
|
2264
|
+
demandOption: true,
|
|
2265
|
+
});
|
|
2266
|
+
},
|
|
2267
|
+
async (args) => {
|
|
2268
|
+
// We expect three values in `_`: `r2`, `bucket`, `delete`.
|
|
2269
|
+
if (args._.length > 3) {
|
|
2270
|
+
const extraArgs = args._.slice(3).join(" ");
|
|
2271
|
+
throw new CommandLineArgsError(
|
|
2272
|
+
`Unexpected additional positional arguments "${extraArgs}".`
|
|
2273
|
+
);
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
const config = readConfig(args.config as ConfigPath);
|
|
2277
|
+
|
|
2278
|
+
const accountId = await requireAuth(config);
|
|
2279
|
+
|
|
2280
|
+
console.log(`Deleting bucket ${args.name}.`);
|
|
2281
|
+
await deleteR2Bucket(accountId, args.name);
|
|
2282
|
+
console.log(`Deleted bucket ${args.name}.`);
|
|
2283
|
+
}
|
|
2284
|
+
);
|
|
2285
|
+
return r2BucketYargs;
|
|
2286
|
+
});
|
|
2287
|
+
});
|
|
2288
|
+
|
|
2289
|
+
wrangler
|
|
2290
|
+
.option("legacy-env", {
|
|
2291
|
+
type: "boolean",
|
|
2292
|
+
describe: "Use legacy environments",
|
|
2293
|
+
})
|
|
2294
|
+
.option("config", {
|
|
2295
|
+
alias: "c",
|
|
2296
|
+
describe: "Path to .toml configuration file",
|
|
2297
|
+
type: "string",
|
|
2298
|
+
});
|
|
2299
|
+
|
|
2300
|
+
wrangler.group(["config", "help", "version", "legacy-env"], "Flags:");
|
|
2301
|
+
wrangler.help().alias("h", "help");
|
|
2302
|
+
wrangler.version(wranglerVersion).alias("v", "version");
|
|
2303
|
+
wrangler.exitProcess(false);
|
|
2304
|
+
|
|
2305
|
+
try {
|
|
2306
|
+
await initialiseUserConfig();
|
|
2307
|
+
await wrangler.parse();
|
|
2308
|
+
} catch (e) {
|
|
2309
|
+
if (e instanceof CommandLineArgsError) {
|
|
2310
|
+
wrangler.showHelp("error");
|
|
2311
|
+
console.error(""); // Just adds a bit of space
|
|
2312
|
+
console.error(e.message);
|
|
2313
|
+
} else if (e instanceof ParseError) {
|
|
2314
|
+
console.error("");
|
|
2315
|
+
e.notes.push({
|
|
2316
|
+
text: "\nIf you think this is a bug, please open an issue at: https://github.com/cloudflare/wrangler2/issues/new",
|
|
2317
|
+
});
|
|
2318
|
+
console.error(formatMessage(e));
|
|
2319
|
+
} else {
|
|
2320
|
+
console.error(e instanceof Error ? e.message : e);
|
|
2321
|
+
console.error(""); // Just adds a bit of space
|
|
2322
|
+
console.error(
|
|
2323
|
+
`${fgGreenColor}%s${resetColor}`,
|
|
2324
|
+
"If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
|
|
2325
|
+
);
|
|
2326
|
+
}
|
|
2327
|
+
throw e;
|
|
2328
|
+
}
|
|
2329
|
+
}
|