wrangler 0.0.0-ece06ea → 0.0.0-eebdb6c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +22 -15
- package/pages/functions/buildWorker.ts +57 -0
- package/pages/functions/filepath-routing.test.ts +32 -0
- package/pages/functions/filepath-routing.ts +225 -0
- package/pages/functions/identifiers.ts +78 -0
- package/pages/functions/routes.ts +158 -0
- package/pages/functions/template-worker.ts +135 -0
- package/src/__tests__/index.test.ts +254 -120
- package/src/__tests__/jest.setup.ts +22 -0
- package/src/__tests__/kv.test.ts +1098 -0
- package/src/__tests__/mock-cfetch.ts +171 -0
- package/src/__tests__/mock-dialogs.ts +65 -0
- package/src/__tests__/run-in-tmp.ts +19 -0
- package/src/__tests__/run-wrangler.ts +32 -0
- package/src/api/form_data.ts +70 -97
- package/src/api/inspect.ts +0 -11
- package/src/api/preview.ts +29 -24
- package/src/api/worker.ts +34 -40
- package/src/cfetch/index.ts +103 -0
- package/src/cfetch/internal.ts +69 -0
- package/src/cli.ts +6 -7
- package/src/config.ts +14 -7
- package/src/dev.tsx +274 -131
- package/src/dialogs.tsx +4 -12
- package/src/index.tsx +304 -245
- package/src/kv.tsx +101 -45
- package/src/module-collection.ts +64 -0
- package/src/pages.tsx +681 -104
- package/src/proxy.ts +103 -0
- package/src/publish.ts +54 -58
- package/src/sites.tsx +5 -6
- package/src/tail.tsx +5 -3
- package/src/user.tsx +9 -16
- package/wrangler-dist/cli.js +36633 -49770
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/cfetch.ts +0 -72
- package/src/util/fetch.ts +0 -74
package/src/kv.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { URLSearchParams } from "node:url";
|
|
1
2
|
import type { Config } from "./config";
|
|
2
|
-
import
|
|
3
|
-
import qs from "node:querystring";
|
|
3
|
+
import { fetchListResult, fetchResult } from "./cfetch";
|
|
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?: string
|
|
36
|
-
limit?: number
|
|
85
|
+
prefix?: string
|
|
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,16 +98,15 @@ export async function putKeyValue(
|
|
|
52
98
|
value: string,
|
|
53
99
|
args?: { expiration?: number; expiration_ttl?: number }
|
|
54
100
|
) {
|
|
55
|
-
return await
|
|
56
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
{ method: "PUT", body: value }
|
|
101
|
+
return await fetchResult(
|
|
102
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
|
|
103
|
+
{ method: "PUT", body: value },
|
|
104
|
+
args
|
|
105
|
+
? new URLSearchParams({
|
|
106
|
+
expiration: args.expiration?.toString(),
|
|
107
|
+
expiration_ttl: args.expiration_ttl?.toString(),
|
|
108
|
+
})
|
|
109
|
+
: undefined
|
|
65
110
|
);
|
|
66
111
|
}
|
|
67
112
|
|
|
@@ -70,7 +115,7 @@ export async function putBulkKeyValue(
|
|
|
70
115
|
namespaceId: string,
|
|
71
116
|
keyvalueStr: string
|
|
72
117
|
) {
|
|
73
|
-
return await
|
|
118
|
+
return await fetchResult(
|
|
74
119
|
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
75
120
|
{
|
|
76
121
|
method: "PUT",
|
|
@@ -85,7 +130,7 @@ export async function deleteBulkKeyValue(
|
|
|
85
130
|
namespaceId: string,
|
|
86
131
|
keyStr: string
|
|
87
132
|
) {
|
|
88
|
-
return await
|
|
133
|
+
return await fetchResult(
|
|
89
134
|
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
90
135
|
{
|
|
91
136
|
method: "DELETE",
|
|
@@ -118,7 +163,7 @@ export function getNamespaceId({
|
|
|
118
163
|
if (!config) {
|
|
119
164
|
throw new Error(
|
|
120
165
|
"Failed to find a config file.\n" +
|
|
121
|
-
"Either use --namespace to upload directly or create a configuration file with a binding."
|
|
166
|
+
"Either use --namespace-id to upload directly or create a configuration file with a binding."
|
|
122
167
|
);
|
|
123
168
|
}
|
|
124
169
|
|
|
@@ -130,7 +175,7 @@ export function getNamespaceId({
|
|
|
130
175
|
);
|
|
131
176
|
}
|
|
132
177
|
|
|
133
|
-
// TODO: either a bespoke arg type for this function to avoid
|
|
178
|
+
// TODO: either a bespoke arg type for this function to avoid `undefined`s or an `EnvOrConfig` type
|
|
134
179
|
return getNamespaceId({
|
|
135
180
|
binding,
|
|
136
181
|
"namespace-id": namespaceId,
|
|
@@ -149,25 +194,27 @@ export function getNamespaceId({
|
|
|
149
194
|
// there's no KV namespaces
|
|
150
195
|
if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
|
|
151
196
|
throw new Error(
|
|
152
|
-
"No KV
|
|
197
|
+
"No KV Namespaces configured! Either use --namespace-id to upload directly or add a KV namespace to your wrangler config file."
|
|
153
198
|
);
|
|
154
199
|
}
|
|
155
200
|
|
|
156
|
-
const namespace = config.kv_namespaces.find(
|
|
157
|
-
(namespace) => namespace.binding === binding
|
|
158
|
-
);
|
|
201
|
+
const namespace = config.kv_namespaces.find((ns) => ns.binding === binding);
|
|
159
202
|
|
|
160
203
|
// we couldn't find a namespace with that binding
|
|
161
204
|
if (!namespace) {
|
|
162
|
-
throw new Error(
|
|
205
|
+
throw new Error(
|
|
206
|
+
`A namespace with binding name "${binding}" was not found in the configured "kv_namespaces".`
|
|
207
|
+
);
|
|
163
208
|
}
|
|
164
209
|
|
|
165
210
|
// end pre-flight checks
|
|
166
211
|
|
|
167
|
-
// we're in preview mode, `--preview true` or `--
|
|
212
|
+
// we're in preview mode, `--preview true` or `--preview` was passed
|
|
168
213
|
if (preview && namespace.preview_id) {
|
|
169
214
|
namespaceId = namespace.preview_id;
|
|
170
|
-
|
|
215
|
+
// We don't want to execute code below if preview is set to true, so we just return. Otherwise we will get errors!
|
|
216
|
+
return namespaceId;
|
|
217
|
+
} else if (preview) {
|
|
171
218
|
throw new Error(
|
|
172
219
|
`No preview ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
173
220
|
);
|
|
@@ -180,7 +227,9 @@ export function getNamespaceId({
|
|
|
180
227
|
// --preview false was passed
|
|
181
228
|
if (previewIsDefined && namespace.id) {
|
|
182
229
|
namespaceId = namespace.id;
|
|
183
|
-
|
|
230
|
+
// We don't want to execute code below if preview is set to true, so we just return. Otherwise we can get error!
|
|
231
|
+
return namespaceId;
|
|
232
|
+
} else if (previewIsDefined) {
|
|
184
233
|
throw new Error(
|
|
185
234
|
`No namespace ID found for ${binding}. Add one to your wrangler config file to use a separate namespace for previewing your worker.`
|
|
186
235
|
);
|
|
@@ -209,3 +258,10 @@ export function getNamespaceId({
|
|
|
209
258
|
|
|
210
259
|
return namespaceId;
|
|
211
260
|
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* KV namespace binding names must be valid JS identifiers.
|
|
264
|
+
*/
|
|
265
|
+
export function isValidNamespaceBinding(binding: string): boolean {
|
|
266
|
+
return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding);
|
|
267
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { CfModule } from "./api/worker";
|
|
2
|
+
import type esbuild from "esbuild";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import crypto from "node:crypto";
|
|
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: /.*\.(pem|txt|html|wasm)$/ },
|
|
34
|
+
async (args: esbuild.OnResolveArgs) => {
|
|
35
|
+
// take the file and massage it to a
|
|
36
|
+
// transportable/manageable format
|
|
37
|
+
const fileExt = path.extname(args.path);
|
|
38
|
+
const filePath = path.join(args.resolveDir, args.path);
|
|
39
|
+
const fileContent = await readFile(filePath);
|
|
40
|
+
const fileHash = crypto
|
|
41
|
+
.createHash("sha1")
|
|
42
|
+
.update(fileContent)
|
|
43
|
+
.digest("hex");
|
|
44
|
+
const fileName = `${fileHash}-${path.basename(args.path)}`;
|
|
45
|
+
|
|
46
|
+
// add the module to the array
|
|
47
|
+
modules.push({
|
|
48
|
+
name: fileName,
|
|
49
|
+
content: fileContent,
|
|
50
|
+
type: fileExt === ".wasm" ? "compiled-wasm" : "text",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
path: fileName, // change the reference to the changed module
|
|
55
|
+
external: true, // mark it as external in the bundle
|
|
56
|
+
namespace: "wrangler-module-collector-ns", // just a tag, this isn't strictly necessary
|
|
57
|
+
watchFiles: [filePath], // we also add the file to esbuild's watch list
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|