wrangler 0.0.0-e14e589 → 0.0.0-e2a26a0
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 +2 -2
- package/package.json +49 -30
- package/pages/functions/buildWorker.ts +62 -0
- package/pages/functions/filepath-routing.test.ts +47 -0
- package/pages/functions/filepath-routing.ts +231 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +160 -0
- package/pages/functions/template-worker.ts +136 -0
- package/src/__tests__/dev.test.tsx +71 -0
- package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
- package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
- package/src/__tests__/helpers/mock-account-id.ts +30 -0
- package/src/__tests__/helpers/mock-bin.ts +33 -0
- package/src/__tests__/helpers/mock-cfetch.ts +207 -0
- package/src/__tests__/helpers/mock-console.ts +56 -0
- package/src/__tests__/helpers/mock-dialogs.ts +65 -0
- package/src/__tests__/helpers/mock-kv.ts +40 -0
- package/src/__tests__/helpers/mock-user.ts +27 -0
- package/src/__tests__/helpers/run-in-tmp.ts +29 -0
- package/src/__tests__/helpers/run-wrangler.ts +14 -0
- package/src/__tests__/index.test.ts +509 -119
- package/src/__tests__/jest.setup.ts +37 -0
- package/src/__tests__/kv.test.ts +1139 -0
- package/src/__tests__/logout.test.ts +50 -0
- package/src/__tests__/package-manager.test.ts +206 -0
- package/src/__tests__/publish.test.ts +1222 -0
- package/src/__tests__/secret.test.ts +210 -0
- package/src/__tests__/whoami.test.tsx +86 -0
- package/src/api/form_data.ts +85 -76
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +50 -35
- package/src/cfetch/index.ts +102 -0
- package/src/cfetch/internal.ts +101 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +630 -85
- package/src/dev.tsx +446 -282
- package/src/dialogs.tsx +6 -14
- package/src/index.tsx +773 -426
- package/src/inspect.ts +596 -0
- package/src/kv.tsx +118 -45
- package/src/module-collection.ts +63 -0
- package/src/package-manager.ts +120 -0
- package/src/pages.tsx +917 -211
- package/src/paths.ts +26 -0
- package/src/proxy.ts +370 -0
- package/src/publish.ts +220 -88
- package/src/sites.tsx +180 -55
- package/src/tail.tsx +6 -4
- package/src/user.tsx +77 -87
- package/src/whoami.tsx +69 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +15 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
- package/wrangler-dist/cli.js +125330 -557
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/inspect.ts +0 -441
- package/src/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
package/src/kv.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { URLSearchParams } from "node:url";
|
|
2
|
+
import { fetchListResult, fetchResult, fetchKVGetValue } from "./cfetch";
|
|
1
3
|
import type { Config } from "./config";
|
|
2
|
-
import cfetch from "./cfetch";
|
|
3
|
-
import qs from "node:querystring";
|
|
4
4
|
|
|
5
5
|
type KvArgs = {
|
|
6
6
|
binding?: string;
|
|
@@ -10,38 +10,84 @@ type KvArgs = {
|
|
|
10
10
|
config?: Config;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Create a new namespace under the given `accountId` with the given `title`.
|
|
15
|
+
*
|
|
16
|
+
* @returns the generated id of the created namespace.
|
|
17
|
+
*/
|
|
18
|
+
export async function createNamespace(
|
|
19
|
+
accountId: string,
|
|
20
|
+
title: string
|
|
21
|
+
): Promise<string> {
|
|
22
|
+
const response = await fetchResult<{ id: string }>(
|
|
23
|
+
`/accounts/${accountId}/storage/kv/namespaces`,
|
|
24
|
+
{
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify({
|
|
30
|
+
title,
|
|
31
|
+
}),
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return response.id;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The information about a namespace that is returned from `listNamespaces()`.
|
|
40
|
+
*/
|
|
41
|
+
export interface KVNamespaceInfo {
|
|
42
|
+
id: string;
|
|
43
|
+
title: string;
|
|
44
|
+
supports_url_encoding?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Fetch a list of all the namespaces under the given `accountId`.
|
|
49
|
+
*/
|
|
50
|
+
export async function listNamespaces(
|
|
51
|
+
accountId: string
|
|
52
|
+
): Promise<KVNamespaceInfo[]> {
|
|
53
|
+
const pageSize = 100;
|
|
54
|
+
let page = 1;
|
|
55
|
+
const results: KVNamespaceInfo[] = [];
|
|
56
|
+
while (results.length % pageSize === 0) {
|
|
57
|
+
const json = await fetchResult<KVNamespaceInfo[]>(
|
|
58
|
+
`/accounts/${accountId}/storage/kv/namespaces`,
|
|
59
|
+
{},
|
|
60
|
+
new URLSearchParams({
|
|
61
|
+
per_page: pageSize.toString(),
|
|
62
|
+
order: "title",
|
|
63
|
+
direction: "asc",
|
|
64
|
+
page: page.toString(),
|
|
65
|
+
})
|
|
22
66
|
);
|
|
23
67
|
page++;
|
|
24
|
-
results
|
|
25
|
-
if (json.length
|
|
26
|
-
|
|
68
|
+
results.push(...json);
|
|
69
|
+
if (json.length < pageSize) {
|
|
70
|
+
break;
|
|
27
71
|
}
|
|
28
72
|
}
|
|
29
73
|
return results;
|
|
30
74
|
}
|
|
31
75
|
|
|
76
|
+
export interface NamespaceKeyInfo {
|
|
77
|
+
name: string;
|
|
78
|
+
expiration?: number;
|
|
79
|
+
metadata?: { [key: string]: unknown };
|
|
80
|
+
}
|
|
81
|
+
|
|
32
82
|
export async function listNamespaceKeys(
|
|
33
83
|
accountId: string,
|
|
34
84
|
namespaceId: string,
|
|
35
|
-
prefix
|
|
36
|
-
limit?: number
|
|
85
|
+
prefix = ""
|
|
37
86
|
) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
{
|
|
41
|
-
|
|
42
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys?${qs.stringify(
|
|
43
|
-
{ prefix, limit }
|
|
44
|
-
)}`
|
|
87
|
+
return await fetchListResult<NamespaceKeyInfo>(
|
|
88
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys`,
|
|
89
|
+
{},
|
|
90
|
+
new URLSearchParams({ prefix })
|
|
45
91
|
);
|
|
46
92
|
}
|
|
47
93
|
|
|
@@ -52,25 +98,37 @@ export async function putKeyValue(
|
|
|
52
98
|
value: string,
|
|
53
99
|
args?: { expiration?: number; expiration_ttl?: number }
|
|
54
100
|
) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
101
|
+
let searchParams: URLSearchParams | undefined;
|
|
102
|
+
if (args) {
|
|
103
|
+
searchParams = new URLSearchParams();
|
|
104
|
+
if (args.expiration) {
|
|
105
|
+
searchParams.set("expiration", `${args.expiration}`);
|
|
106
|
+
}
|
|
107
|
+
if (args.expiration_ttl) {
|
|
108
|
+
searchParams.set("expiration_ttl", `${args.expiration_ttl}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return await fetchResult(
|
|
112
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
|
|
113
|
+
{ method: "PUT", body: value },
|
|
114
|
+
searchParams
|
|
65
115
|
);
|
|
66
116
|
}
|
|
67
117
|
|
|
118
|
+
export async function getKeyValue(
|
|
119
|
+
accountId: string,
|
|
120
|
+
namespaceId: string,
|
|
121
|
+
key: string
|
|
122
|
+
): Promise<string> {
|
|
123
|
+
return await fetchKVGetValue(accountId, namespaceId, key);
|
|
124
|
+
}
|
|
125
|
+
|
|
68
126
|
export async function putBulkKeyValue(
|
|
69
127
|
accountId: string,
|
|
70
128
|
namespaceId: string,
|
|
71
129
|
keyvalueStr: string
|
|
72
130
|
) {
|
|
73
|
-
return await
|
|
131
|
+
return await fetchResult(
|
|
74
132
|
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
75
133
|
{
|
|
76
134
|
method: "PUT",
|
|
@@ -85,7 +143,7 @@ export async function deleteBulkKeyValue(
|
|
|
85
143
|
namespaceId: string,
|
|
86
144
|
keyStr: string
|
|
87
145
|
) {
|
|
88
|
-
return await
|
|
146
|
+
return await fetchResult(
|
|
89
147
|
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
90
148
|
{
|
|
91
149
|
method: "DELETE",
|
|
@@ -118,7 +176,7 @@ export function getNamespaceId({
|
|
|
118
176
|
if (!config) {
|
|
119
177
|
throw new Error(
|
|
120
178
|
"Failed to find a config file.\n" +
|
|
121
|
-
"Either use --namespace to upload directly or create a configuration file with a binding."
|
|
179
|
+
"Either use --namespace-id to upload directly or create a configuration file with a binding."
|
|
122
180
|
);
|
|
123
181
|
}
|
|
124
182
|
|
|
@@ -130,7 +188,7 @@ export function getNamespaceId({
|
|
|
130
188
|
);
|
|
131
189
|
}
|
|
132
190
|
|
|
133
|
-
// TODO: either a bespoke arg type for this function to avoid
|
|
191
|
+
// TODO: either a bespoke arg type for this function to avoid `undefined`s or an `EnvOrConfig` type
|
|
134
192
|
return getNamespaceId({
|
|
135
193
|
binding,
|
|
136
194
|
"namespace-id": namespaceId,
|
|
@@ -149,25 +207,27 @@ export function getNamespaceId({
|
|
|
149
207
|
// there's no KV namespaces
|
|
150
208
|
if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
|
|
151
209
|
throw new Error(
|
|
152
|
-
"No KV
|
|
210
|
+
"No KV Namespaces configured! Either use --namespace-id to upload directly or add a KV namespace to your wrangler config file."
|
|
153
211
|
);
|
|
154
212
|
}
|
|
155
213
|
|
|
156
|
-
const namespace = config.kv_namespaces.find(
|
|
157
|
-
(namespace) => namespace.binding === binding
|
|
158
|
-
);
|
|
214
|
+
const namespace = config.kv_namespaces.find((ns) => ns.binding === binding);
|
|
159
215
|
|
|
160
216
|
// we couldn't find a namespace with that binding
|
|
161
217
|
if (!namespace) {
|
|
162
|
-
throw new Error(
|
|
218
|
+
throw new Error(
|
|
219
|
+
`A namespace with binding name "${binding}" was not found in the configured "kv_namespaces".`
|
|
220
|
+
);
|
|
163
221
|
}
|
|
164
222
|
|
|
165
223
|
// end pre-flight checks
|
|
166
224
|
|
|
167
|
-
// we're in preview mode, `--preview true` or `--
|
|
225
|
+
// we're in preview mode, `--preview true` or `--preview` was passed
|
|
168
226
|
if (preview && namespace.preview_id) {
|
|
169
227
|
namespaceId = namespace.preview_id;
|
|
170
|
-
|
|
228
|
+
// We don't want to execute code below if preview is set to true, so we just return. Otherwise we will get errors!
|
|
229
|
+
return namespaceId;
|
|
230
|
+
} else if (preview) {
|
|
171
231
|
throw new Error(
|
|
172
232
|
`No preview ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
173
233
|
);
|
|
@@ -180,7 +240,9 @@ export function getNamespaceId({
|
|
|
180
240
|
// --preview false was passed
|
|
181
241
|
if (previewIsDefined && namespace.id) {
|
|
182
242
|
namespaceId = namespace.id;
|
|
183
|
-
|
|
243
|
+
// We don't want to execute code below if preview is set to true, so we just return. Otherwise we can get error!
|
|
244
|
+
return namespaceId;
|
|
245
|
+
} else if (previewIsDefined) {
|
|
184
246
|
throw new Error(
|
|
185
247
|
`No namespace ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
186
248
|
);
|
|
@@ -209,3 +271,14 @@ export function getNamespaceId({
|
|
|
209
271
|
|
|
210
272
|
return namespaceId;
|
|
211
273
|
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* KV namespace binding names must be valid JS identifiers.
|
|
277
|
+
*/
|
|
278
|
+
export function isValidNamespaceBinding(
|
|
279
|
+
binding: string | undefined
|
|
280
|
+
): binding is string {
|
|
281
|
+
return (
|
|
282
|
+
typeof binding === "string" && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding)
|
|
283
|
+
);
|
|
284
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { CfModule } from "./api/worker";
|
|
5
|
+
import type esbuild from "esbuild";
|
|
6
|
+
|
|
7
|
+
// This is a combination of an esbuild plugin and a mutable array
|
|
8
|
+
// that we use to collect module references from source code.
|
|
9
|
+
// There will be modules that _shouldn't_ be inlined directly into
|
|
10
|
+
// the bundle. (eg. wasm modules, some text files, etc). We can include
|
|
11
|
+
// those files as modules in the multi part forker form upload. This
|
|
12
|
+
// plugin+array is used to collect references to these modules, reference
|
|
13
|
+
// them correctly in the bundle, and add them to the form upload.
|
|
14
|
+
|
|
15
|
+
export default function makeModuleCollector(): {
|
|
16
|
+
modules: CfModule[];
|
|
17
|
+
plugin: esbuild.Plugin;
|
|
18
|
+
} {
|
|
19
|
+
const modules: CfModule[] = [];
|
|
20
|
+
return {
|
|
21
|
+
modules,
|
|
22
|
+
plugin: {
|
|
23
|
+
name: "wrangler-module-collector",
|
|
24
|
+
setup(build) {
|
|
25
|
+
build.onStart(() => {
|
|
26
|
+
// reset the moduels collection
|
|
27
|
+
modules.splice(0);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
build.onResolve(
|
|
31
|
+
// filter on "known" file types,
|
|
32
|
+
// we can expand this list later
|
|
33
|
+
{ filter: /.*\.(wasm)$/ },
|
|
34
|
+
async (args: esbuild.OnResolveArgs) => {
|
|
35
|
+
// take the file and massage it to a
|
|
36
|
+
// transportable/manageable format
|
|
37
|
+
const filePath = path.join(args.resolveDir, args.path);
|
|
38
|
+
const fileContent = await readFile(filePath);
|
|
39
|
+
const fileHash = crypto
|
|
40
|
+
.createHash("sha1")
|
|
41
|
+
.update(fileContent)
|
|
42
|
+
.digest("hex");
|
|
43
|
+
const fileName = `${fileHash}-${path.basename(args.path)}`;
|
|
44
|
+
|
|
45
|
+
// add the module to the array
|
|
46
|
+
modules.push({
|
|
47
|
+
name: fileName,
|
|
48
|
+
content: fileContent,
|
|
49
|
+
type: "compiled-wasm",
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
path: fileName, // change the reference to the changed module
|
|
54
|
+
external: true, // mark it as external in the bundle
|
|
55
|
+
namespace: "wrangler-module-collector-ns", // just a tag, this isn't strictly necessary
|
|
56
|
+
watchFiles: [filePath], // we also add the file to esbuild's watch list
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { execa, execaCommandSync } from "execa";
|
|
4
|
+
|
|
5
|
+
export interface PackageManager {
|
|
6
|
+
addDevDeps(...packages: string[]): Promise<void>;
|
|
7
|
+
install(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function getPackageManager(root: string): Promise<PackageManager> {
|
|
11
|
+
const [hasYarn, hasNpm] = await Promise.all([supportsYarn(), supportsNpm()]);
|
|
12
|
+
const hasYarnLock = existsSync(join(root, "yarn.lock"));
|
|
13
|
+
const hasNpmLock = existsSync(join(root, "package-lock.json"));
|
|
14
|
+
|
|
15
|
+
if (hasNpmLock) {
|
|
16
|
+
if (hasNpm) {
|
|
17
|
+
console.log(
|
|
18
|
+
"Using npm as package manager, as there is already a package-lock.json file."
|
|
19
|
+
);
|
|
20
|
+
return NpmPackageManager;
|
|
21
|
+
} else if (hasYarn) {
|
|
22
|
+
console.log("Using yarn as package manager.");
|
|
23
|
+
console.warn(
|
|
24
|
+
"There is already a package-lock.json file but could not find npm on the PATH."
|
|
25
|
+
);
|
|
26
|
+
return YarnPackageManager;
|
|
27
|
+
}
|
|
28
|
+
} else if (hasYarnLock) {
|
|
29
|
+
if (hasYarn) {
|
|
30
|
+
console.log(
|
|
31
|
+
"Using yarn as package manager, as there is already a yarn.lock file."
|
|
32
|
+
);
|
|
33
|
+
return YarnPackageManager;
|
|
34
|
+
} else if (hasNpm) {
|
|
35
|
+
console.log("Using npm as package manager.");
|
|
36
|
+
console.warn(
|
|
37
|
+
"There is already a yarn.lock file but could not find yarn on the PATH."
|
|
38
|
+
);
|
|
39
|
+
return NpmPackageManager;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (hasNpm) {
|
|
44
|
+
console.log("Using npm as package manager.");
|
|
45
|
+
return NpmPackageManager;
|
|
46
|
+
} else if (hasYarn) {
|
|
47
|
+
console.log("Using yarn as package manager.");
|
|
48
|
+
return YarnPackageManager;
|
|
49
|
+
} else {
|
|
50
|
+
throw new Error(
|
|
51
|
+
"Unable to find a package manager. Supported managers are: npm and yarn."
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get the name of the given `packageManager`.
|
|
58
|
+
*/
|
|
59
|
+
export function getPackageManagerName(packageManager: unknown): string {
|
|
60
|
+
return packageManager === NpmPackageManager
|
|
61
|
+
? "npm"
|
|
62
|
+
: packageManager === YarnPackageManager
|
|
63
|
+
? "yarn"
|
|
64
|
+
: "unknown";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Manage packages using npm
|
|
69
|
+
*/
|
|
70
|
+
const NpmPackageManager: PackageManager = {
|
|
71
|
+
/** Add and install a new devDependency into the local package.json. */
|
|
72
|
+
async addDevDeps(...packages: string[]): Promise<void> {
|
|
73
|
+
await execa("npm", ["install", ...packages, "--save-dev"], {
|
|
74
|
+
stdio: "inherit",
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/** Install all the dependencies in the local package.json. */
|
|
79
|
+
async install(): Promise<void> {
|
|
80
|
+
await execa("npm", ["install"], {
|
|
81
|
+
stdio: "inherit",
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Manage packages using yarn
|
|
88
|
+
*/
|
|
89
|
+
const YarnPackageManager: PackageManager = {
|
|
90
|
+
/** Add and install a new devDependency into the local package.json. */
|
|
91
|
+
async addDevDeps(...packages: string[]): Promise<void> {
|
|
92
|
+
await execa("yarn", ["add", ...packages, "--dev"], {
|
|
93
|
+
stdio: "inherit",
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
/** Install all the dependencies in the local package.json. */
|
|
98
|
+
async install(): Promise<void> {
|
|
99
|
+
await execa("yarn", ["install"], {
|
|
100
|
+
stdio: "inherit",
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
async function supports(name: string): Promise<boolean> {
|
|
106
|
+
try {
|
|
107
|
+
execaCommandSync(`${name} --version`, { stdio: "ignore" });
|
|
108
|
+
return true;
|
|
109
|
+
} catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function supportsYarn(): Promise<boolean> {
|
|
115
|
+
return supports("yarn");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function supportsNpm(): Promise<boolean> {
|
|
119
|
+
return supports("npm");
|
|
120
|
+
}
|