wrangler 0.0.0-e22f9d7 → 0.0.0-e273e09
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/Cloudflare_CA.pem +18 -0
- package/README.md +27 -14
- package/bin/wrangler.js +48 -4
- package/miniflare-dist/index.mjs +4886 -0
- package/package.json +60 -31
- package/pages/functions/buildPlugin.ts +105 -0
- package/pages/functions/buildWorker.ts +93 -4
- package/pages/functions/filepath-routing.test.ts +223 -28
- package/pages/functions/filepath-routing.ts +100 -132
- package/pages/functions/routes.ts +16 -18
- package/pages/functions/template-plugin.ts +147 -0
- package/pages/functions/template-worker.ts +37 -38
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +3487 -0
- package/src/__tests__/dev.test.tsx +977 -61
- package/src/__tests__/guess-worker-format.test.ts +85 -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/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +48 -0
- package/src/__tests__/helpers/mock-bin.ts +36 -0
- package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +56 -16
- package/src/__tests__/helpers/mock-console.ts +87 -0
- package/src/__tests__/helpers/mock-dialogs.ts +130 -0
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +27 -0
- package/src/__tests__/helpers/mock-kv.ts +33 -0
- package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
- package/src/__tests__/helpers/mock-stdin.ts +103 -0
- package/src/__tests__/helpers/mock-web-socket.ts +29 -0
- package/src/__tests__/helpers/run-in-tmp.ts +39 -0
- package/src/__tests__/helpers/run-wrangler.ts +16 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
- package/src/__tests__/https-options.test.ts +130 -0
- package/src/__tests__/index.test.ts +179 -209
- package/src/__tests__/init.test.ts +1637 -0
- package/src/__tests__/jest.setup.ts +61 -6
- package/src/__tests__/kv.test.ts +842 -472
- package/src/__tests__/logger.test.ts +115 -0
- package/src/__tests__/package-manager.test.ts +353 -0
- package/src/__tests__/pages.test.ts +754 -0
- package/src/__tests__/parse.test.ts +330 -0
- package/src/__tests__/publish.test.ts +5755 -0
- package/src/__tests__/r2.test.ts +233 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +481 -0
- package/src/__tests__/tail.test.ts +690 -0
- package/src/__tests__/user.test.ts +131 -0
- package/src/__tests__/whoami.test.tsx +139 -0
- package/src/abort.d.ts +3 -0
- package/src/bundle.ts +197 -0
- package/src/cfetch/index.ts +40 -21
- package/src/cfetch/internal.ts +102 -29
- package/src/cli.ts +6 -2
- package/src/config/README.md +107 -0
- package/src/config/config.ts +234 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +420 -0
- package/src/config/index.ts +225 -0
- package/src/config/validation-helpers.ts +584 -0
- package/src/config/validation.ts +1548 -0
- package/src/config-cache.ts +45 -0
- package/src/create-worker-preview.ts +185 -0
- package/src/create-worker-upload-form.ts +254 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +401 -0
- package/src/dev/local.tsx +326 -0
- package/src/dev/remote.tsx +237 -0
- package/src/dev/use-esbuild.ts +118 -0
- package/src/dialogs.tsx +66 -8
- package/src/durable.ts +102 -0
- package/src/entry.ts +322 -0
- package/src/environment-variables.ts +35 -0
- package/src/errors.ts +11 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +1848 -976
- package/src/inspect.ts +156 -33
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +199 -67
- package/src/logger.ts +66 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +41 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +229 -35
- package/src/open-in-browser.ts +19 -0
- package/src/package-manager.ts +174 -0
- package/src/pages.tsx +1162 -100
- package/src/parse.ts +203 -0
- package/src/paths.ts +26 -0
- package/src/proxy.ts +227 -51
- package/src/publish.ts +633 -276
- package/src/r2.ts +50 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +270 -62
- package/src/tail/filters.ts +279 -0
- package/src/tail/index.ts +299 -0
- package/src/tail/printing.ts +67 -0
- package/src/update-check.ts +19 -0
- package/src/user.tsx +382 -195
- package/src/whoami.tsx +71 -0
- package/src/{api/worker.ts → worker.ts} +66 -40
- package/src/zones.ts +73 -0
- package/templates/gitignore +171 -0
- package/templates/new-worker-scheduled.js +17 -0
- package/templates/new-worker-scheduled.ts +32 -0
- package/templates/new-worker.js +15 -0
- package/templates/new-worker.ts +30 -0
- package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -5
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +106332 -103741
- package/src/__tests__/mock-dialogs.ts +0 -65
- package/src/__tests__/run-in-tmp.ts +0 -19
- package/src/__tests__/run-wrangler.ts +0 -32
- package/src/api/form_data.ts +0 -131
- package/src/api/preview.ts +0 -128
- package/src/config.ts +0 -487
- package/src/dev.tsx +0 -784
- package/src/tail.tsx +0 -73
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
- package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
- package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
- package/vendor/wrangler-mime/CHANGELOG.md +0 -289
- package/vendor/wrangler-mime/LICENSE +0 -21
- package/vendor/wrangler-mime/Mime.js +0 -97
- package/vendor/wrangler-mime/README.md +0 -187
- package/vendor/wrangler-mime/cli.js +0 -46
- package/vendor/wrangler-mime/index.js +0 -4
- package/vendor/wrangler-mime/lite.js +0 -4
- package/vendor/wrangler-mime/package.json +0 -52
- package/vendor/wrangler-mime/types/other.js +0 -1
- package/vendor/wrangler-mime/types/standard.js +0 -1
- package/wrangler-dist/cli.js.map +0 -7
package/src/{kv.tsx → kv.ts}
RENAMED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import { URLSearchParams } from "node:url";
|
|
2
|
+
import { fetchListResult, fetchResult, fetchKVGetValue } from "./cfetch";
|
|
3
|
+
import { logger } from "./logger";
|
|
2
4
|
import type { Config } from "./config";
|
|
3
|
-
|
|
5
|
+
|
|
6
|
+
/** The largest number of kv items we can pass to the API in a single request. */
|
|
7
|
+
const API_MAX = 10000;
|
|
8
|
+
// The const below are halved from the API's true capacity to help avoid
|
|
9
|
+
// hammering it with large requests.
|
|
10
|
+
const BATCH_KEY_MAX = API_MAX / 2;
|
|
4
11
|
|
|
5
12
|
type KvArgs = {
|
|
6
13
|
binding?: string;
|
|
7
14
|
"namespace-id"?: string;
|
|
8
|
-
env?: string;
|
|
9
15
|
preview?: boolean;
|
|
10
|
-
config?: Config;
|
|
11
16
|
};
|
|
12
17
|
|
|
13
18
|
/**
|
|
@@ -15,7 +20,7 @@ type KvArgs = {
|
|
|
15
20
|
*
|
|
16
21
|
* @returns the generated id of the created namespace.
|
|
17
22
|
*/
|
|
18
|
-
export async function
|
|
23
|
+
export async function createKVNamespace(
|
|
19
24
|
accountId: string,
|
|
20
25
|
title: string
|
|
21
26
|
): Promise<string> {
|
|
@@ -47,7 +52,7 @@ export interface KVNamespaceInfo {
|
|
|
47
52
|
/**
|
|
48
53
|
* Fetch a list of all the namespaces under the given `accountId`.
|
|
49
54
|
*/
|
|
50
|
-
export async function
|
|
55
|
+
export async function listKVNamespaces(
|
|
51
56
|
accountId: string
|
|
52
57
|
): Promise<KVNamespaceInfo[]> {
|
|
53
58
|
const pageSize = 100;
|
|
@@ -79,7 +84,7 @@ export interface NamespaceKeyInfo {
|
|
|
79
84
|
metadata?: { [key: string]: unknown };
|
|
80
85
|
}
|
|
81
86
|
|
|
82
|
-
export async function
|
|
87
|
+
export async function listKVNamespaceKeys(
|
|
83
88
|
accountId: string,
|
|
84
89
|
namespaceId: string,
|
|
85
90
|
prefix = ""
|
|
@@ -91,67 +96,218 @@ export async function listNamespaceKeys(
|
|
|
91
96
|
);
|
|
92
97
|
}
|
|
93
98
|
|
|
94
|
-
export async function
|
|
99
|
+
export async function deleteKVNamespace(
|
|
100
|
+
accountId: string,
|
|
101
|
+
namespaceId: string
|
|
102
|
+
) {
|
|
103
|
+
return await fetchResult<{ id: string }>(
|
|
104
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}`,
|
|
105
|
+
{ method: "DELETE" }
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Information about a key-value pair, including its "metadata" fields.
|
|
111
|
+
*/
|
|
112
|
+
export interface KeyValue {
|
|
113
|
+
key: string;
|
|
114
|
+
value: string;
|
|
115
|
+
expiration?: number;
|
|
116
|
+
expiration_ttl?: number;
|
|
117
|
+
metadata?: object;
|
|
118
|
+
base64?: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const KeyValueKeys = new Set([
|
|
122
|
+
"key",
|
|
123
|
+
"value",
|
|
124
|
+
"expiration",
|
|
125
|
+
"expiration_ttl",
|
|
126
|
+
"metadata",
|
|
127
|
+
"base64",
|
|
128
|
+
]);
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The object has the specified property.
|
|
132
|
+
*/
|
|
133
|
+
function hasProperty<T extends object>(
|
|
134
|
+
obj: object,
|
|
135
|
+
property: keyof T
|
|
136
|
+
): obj is T {
|
|
137
|
+
return property in obj;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The object has a required property of the specified type.
|
|
142
|
+
*/
|
|
143
|
+
function hasTypedProperty<T extends object>(
|
|
144
|
+
obj: object,
|
|
145
|
+
property: keyof T,
|
|
146
|
+
type: string
|
|
147
|
+
): obj is T {
|
|
148
|
+
return hasProperty(obj, property) && typeof obj[property] === type;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The object an optional property, of the specified type.
|
|
153
|
+
*/
|
|
154
|
+
function hasOptionalTypedProperty<T extends object>(
|
|
155
|
+
obj: object,
|
|
156
|
+
property: keyof T,
|
|
157
|
+
type: string
|
|
158
|
+
): obj is Omit<T, typeof property> | T {
|
|
159
|
+
return !hasProperty(obj, property) || typeof obj[property] === type;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Is the given object a valid `KeyValue` type?
|
|
164
|
+
*/
|
|
165
|
+
export function isKVKeyValue(keyValue: unknown): keyValue is KeyValue {
|
|
166
|
+
if (
|
|
167
|
+
keyValue === null ||
|
|
168
|
+
typeof keyValue !== "object" ||
|
|
169
|
+
!hasTypedProperty(keyValue, "key", "string") ||
|
|
170
|
+
!hasTypedProperty(keyValue, "value", "string") ||
|
|
171
|
+
!hasOptionalTypedProperty(keyValue, "expiration", "number") ||
|
|
172
|
+
!hasOptionalTypedProperty(keyValue, "expiration_ttl", "number") ||
|
|
173
|
+
!hasOptionalTypedProperty(keyValue, "base64", "boolean") ||
|
|
174
|
+
!hasOptionalTypedProperty(keyValue, "metadata", "object")
|
|
175
|
+
) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Get all the properties on the `keyValue` that are not expected.
|
|
183
|
+
*/
|
|
184
|
+
export function unexpectedKVKeyValueProps(keyValue: KeyValue): string[] {
|
|
185
|
+
const props = Object.keys(keyValue);
|
|
186
|
+
return props.filter((prop) => !KeyValueKeys.has(prop));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function putKVKeyValue(
|
|
95
190
|
accountId: string,
|
|
96
191
|
namespaceId: string,
|
|
97
|
-
|
|
98
|
-
value: string,
|
|
99
|
-
args?: { expiration?: number; expiration_ttl?: number }
|
|
192
|
+
keyValue: KeyValue
|
|
100
193
|
) {
|
|
101
194
|
let searchParams: URLSearchParams | undefined;
|
|
102
|
-
if (
|
|
195
|
+
if (keyValue.expiration || keyValue.expiration_ttl) {
|
|
103
196
|
searchParams = new URLSearchParams();
|
|
104
|
-
if (
|
|
105
|
-
searchParams.set("expiration", `${
|
|
197
|
+
if (keyValue.expiration) {
|
|
198
|
+
searchParams.set("expiration", `${keyValue.expiration}`);
|
|
106
199
|
}
|
|
107
|
-
if (
|
|
108
|
-
searchParams.set("expiration_ttl", `${
|
|
200
|
+
if (keyValue.expiration_ttl) {
|
|
201
|
+
searchParams.set("expiration_ttl", `${keyValue.expiration_ttl}`);
|
|
109
202
|
}
|
|
110
203
|
}
|
|
111
204
|
return await fetchResult(
|
|
112
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${
|
|
113
|
-
|
|
205
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
|
|
206
|
+
keyValue.key
|
|
207
|
+
)}`,
|
|
208
|
+
{ method: "PUT", body: keyValue.value },
|
|
114
209
|
searchParams
|
|
115
210
|
);
|
|
116
211
|
}
|
|
117
212
|
|
|
118
|
-
export async function
|
|
213
|
+
export async function getKVKeyValue(
|
|
214
|
+
accountId: string,
|
|
215
|
+
namespaceId: string,
|
|
216
|
+
key: string
|
|
217
|
+
): Promise<string> {
|
|
218
|
+
return await fetchKVGetValue(accountId, namespaceId, encodeURIComponent(key));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export async function deleteKVKeyValue(
|
|
119
222
|
accountId: string,
|
|
120
223
|
namespaceId: string,
|
|
121
|
-
|
|
224
|
+
key: string
|
|
122
225
|
) {
|
|
123
226
|
return await fetchResult(
|
|
124
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
227
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(
|
|
228
|
+
key
|
|
229
|
+
)}`,
|
|
230
|
+
{ method: "DELETE" }
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Formatter for converting e.g. 5328 --> 5,328
|
|
236
|
+
*/
|
|
237
|
+
const formatNumber = new Intl.NumberFormat("en-US", {
|
|
238
|
+
notation: "standard",
|
|
239
|
+
}).format;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Helper function for bulk requests, logs ongoing output to console.
|
|
243
|
+
*/
|
|
244
|
+
function logBulkProgress(
|
|
245
|
+
operation: "put" | "delete",
|
|
246
|
+
index: number,
|
|
247
|
+
total: number
|
|
248
|
+
) {
|
|
249
|
+
logger.log(
|
|
250
|
+
`${operation === "put" ? "Uploaded" : "Deleted"} ${Math.floor(
|
|
251
|
+
(100 * index) / total
|
|
252
|
+
)}% (${formatNumber(index)} out of ${formatNumber(total)})`
|
|
130
253
|
);
|
|
131
254
|
}
|
|
132
255
|
|
|
133
|
-
export async function
|
|
256
|
+
export async function putKVBulkKeyValue(
|
|
134
257
|
accountId: string,
|
|
135
258
|
namespaceId: string,
|
|
136
|
-
|
|
259
|
+
keyValues: KeyValue[],
|
|
260
|
+
quiet = false
|
|
137
261
|
) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
method: "DELETE",
|
|
142
|
-
body: keyStr,
|
|
143
|
-
headers: { "Content-Type": "application/json" },
|
|
262
|
+
for (let index = 0; index < keyValues.length; index += BATCH_KEY_MAX) {
|
|
263
|
+
if (!quiet && keyValues.length > BATCH_KEY_MAX) {
|
|
264
|
+
logBulkProgress("put", index, keyValues.length);
|
|
144
265
|
}
|
|
145
|
-
|
|
266
|
+
|
|
267
|
+
await fetchResult(
|
|
268
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
269
|
+
{
|
|
270
|
+
method: "PUT",
|
|
271
|
+
body: JSON.stringify(keyValues.slice(index, index + BATCH_KEY_MAX)),
|
|
272
|
+
headers: { "Content-Type": "application/json" },
|
|
273
|
+
}
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!quiet && keyValues.length > BATCH_KEY_MAX) {
|
|
278
|
+
logBulkProgress("put", keyValues.length, keyValues.length);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export async function deleteKVBulkKeyValue(
|
|
283
|
+
accountId: string,
|
|
284
|
+
namespaceId: string,
|
|
285
|
+
keys: string[],
|
|
286
|
+
quiet = false
|
|
287
|
+
) {
|
|
288
|
+
for (let index = 0; index < keys.length; index += BATCH_KEY_MAX) {
|
|
289
|
+
if (!quiet && keys.length > BATCH_KEY_MAX) {
|
|
290
|
+
logBulkProgress("delete", index, keys.length);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
await fetchResult(
|
|
294
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
295
|
+
{
|
|
296
|
+
method: "DELETE",
|
|
297
|
+
body: JSON.stringify(keys.slice(index, index + BATCH_KEY_MAX)),
|
|
298
|
+
headers: { "Content-Type": "application/json" },
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
if (!quiet && keys.length > BATCH_KEY_MAX) {
|
|
303
|
+
logBulkProgress("delete", keys.length, keys.length);
|
|
304
|
+
}
|
|
146
305
|
}
|
|
147
306
|
|
|
148
|
-
export function
|
|
149
|
-
preview,
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
"namespace-id": namespaceId,
|
|
153
|
-
env,
|
|
154
|
-
}: KvArgs): string {
|
|
307
|
+
export function getKVNamespaceId(
|
|
308
|
+
{ preview, binding, "namespace-id": namespaceId }: KvArgs,
|
|
309
|
+
config: Config
|
|
310
|
+
): string {
|
|
155
311
|
// nice
|
|
156
312
|
if (namespaceId) {
|
|
157
313
|
return namespaceId;
|
|
@@ -159,7 +315,7 @@ export function getNamespaceId({
|
|
|
159
315
|
|
|
160
316
|
// begin pre-flight checks
|
|
161
317
|
|
|
162
|
-
// `--binding` is only valid if there's a wrangler configuration
|
|
318
|
+
// `--binding` is only valid if there's a wrangler configuration.
|
|
163
319
|
if (binding && !config) {
|
|
164
320
|
throw new Error("--binding specified, but no config file was found.");
|
|
165
321
|
}
|
|
@@ -172,30 +328,6 @@ export function getNamespaceId({
|
|
|
172
328
|
);
|
|
173
329
|
}
|
|
174
330
|
|
|
175
|
-
// they want to use an environment, actually
|
|
176
|
-
if (env) {
|
|
177
|
-
if (!config.env || !config.env[env]) {
|
|
178
|
-
throw new Error(
|
|
179
|
-
`Failed to find environment "${env}" in configuration file!`
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// TODO: either a bespoke arg type for this function to avoid `undefined`s or an `EnvOrConfig` type
|
|
184
|
-
return getNamespaceId({
|
|
185
|
-
binding,
|
|
186
|
-
"namespace-id": namespaceId,
|
|
187
|
-
env: undefined,
|
|
188
|
-
preview,
|
|
189
|
-
config: {
|
|
190
|
-
env: undefined,
|
|
191
|
-
build: undefined,
|
|
192
|
-
name: undefined,
|
|
193
|
-
account_id: undefined,
|
|
194
|
-
...config.env[env],
|
|
195
|
-
},
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
331
|
// there's no KV namespaces
|
|
200
332
|
if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
|
|
201
333
|
throw new Error(
|
|
@@ -267,7 +399,7 @@ export function getNamespaceId({
|
|
|
267
399
|
/**
|
|
268
400
|
* KV namespace binding names must be valid JS identifiers.
|
|
269
401
|
*/
|
|
270
|
-
export function
|
|
402
|
+
export function isValidKVNamespaceBinding(
|
|
271
403
|
binding: string | undefined
|
|
272
404
|
): binding is string {
|
|
273
405
|
return (
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { format } from "node:util";
|
|
2
|
+
import { formatMessagesSync } from "esbuild";
|
|
3
|
+
|
|
4
|
+
const LOGGER_LEVELS = {
|
|
5
|
+
error: 0,
|
|
6
|
+
warn: 1,
|
|
7
|
+
log: 2,
|
|
8
|
+
debug: 3,
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
type LoggerLevel = keyof typeof LOGGER_LEVELS;
|
|
12
|
+
|
|
13
|
+
/** A map from LOGGER_LEVEL to the error `kind` needed by `formatMessagesSync()`. */
|
|
14
|
+
const LOGGER_LEVEL_FORMAT_TYPE_MAP = {
|
|
15
|
+
error: "error",
|
|
16
|
+
warn: "warning",
|
|
17
|
+
log: undefined,
|
|
18
|
+
debug: undefined,
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
class Logger {
|
|
22
|
+
constructor(public loggerLevel: LoggerLevel = "log") {}
|
|
23
|
+
|
|
24
|
+
columns = process.stdout.columns;
|
|
25
|
+
|
|
26
|
+
debug = (...args: unknown[]) => this.doLog("debug", args);
|
|
27
|
+
log = (...args: unknown[]) => this.doLog("log", args);
|
|
28
|
+
warn = (...args: unknown[]) => this.doLog("warn", args);
|
|
29
|
+
error = (...args: unknown[]) => this.doLog("error", args);
|
|
30
|
+
|
|
31
|
+
private doLog(messageLevel: LoggerLevel, args: unknown[]) {
|
|
32
|
+
if (LOGGER_LEVELS[this.loggerLevel] >= LOGGER_LEVELS[messageLevel]) {
|
|
33
|
+
console[messageLevel](this.formatMessage(messageLevel, format(...args)));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private formatMessage(level: LoggerLevel, message: string): string {
|
|
38
|
+
const kind = LOGGER_LEVEL_FORMAT_TYPE_MAP[level];
|
|
39
|
+
if (kind) {
|
|
40
|
+
// Format the message using the esbuild formatter.
|
|
41
|
+
// The first line of the message is the main `text`,
|
|
42
|
+
// subsequent lines are put into the `notes`.
|
|
43
|
+
const [firstLine, ...otherLines] = message.split("\n");
|
|
44
|
+
const notes =
|
|
45
|
+
otherLines.length > 0
|
|
46
|
+
? otherLines.map((text) => ({ text }))
|
|
47
|
+
: undefined;
|
|
48
|
+
return formatMessagesSync([{ text: firstLine, notes }], {
|
|
49
|
+
color: true,
|
|
50
|
+
kind,
|
|
51
|
+
terminalWidth: this.columns,
|
|
52
|
+
})[0];
|
|
53
|
+
} else {
|
|
54
|
+
return message;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A drop-in replacement for `console` for outputting logging messages.
|
|
61
|
+
*
|
|
62
|
+
* Errors and Warnings will get additional formatting to highlight them to the user.
|
|
63
|
+
* You can also set a `logger.loggerLevel` value to one of "debug", "log", "warn" or "error",
|
|
64
|
+
* to filter out logging messages.
|
|
65
|
+
*/
|
|
66
|
+
export const logger = new Logger();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Custom Miniflare CLI
|
|
2
|
+
|
|
3
|
+
This directory contains a simple wrapper around the programmatic Miniflare API,
|
|
4
|
+
which Wrangler spawns when running `wrangler dev` in local mode.
|
|
5
|
+
|
|
6
|
+
## Building
|
|
7
|
+
|
|
8
|
+
This CLI is built at the same time as Wrangler by running
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm run -w wrangler build
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The output of the build is `miniflare-dist/index.mjs`.
|
|
15
|
+
|
|
16
|
+
## Running
|
|
17
|
+
|
|
18
|
+
The CLI expects a single command line argument which is the Miniflare options formatted as a string of JSON.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
node --no-warnings ./packages/wrangler/miniflare-dist/index.mjs '{"watch": true, "script": ""}' --log VERBOSE
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The `--log` argument is optional and takes one of Miniflare's LogLevels: "NONE", "ERROR", "WARN", "INFO", "DEBUG", "VERBOSE".
|
|
25
|
+
It defaults to `INFO`.
|
|
26
|
+
|
|
27
|
+
## Debugging
|
|
28
|
+
|
|
29
|
+
Simply place a breakpoint in the code and run the above command in the VS Code "JavaScript Debug Terminal".
|
|
30
|
+
The code will stop at the breakpoint as expected.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type EnumKeys<Enum> = Exclude<keyof Enum, number>;
|
|
2
|
+
|
|
3
|
+
export const enumObject = <Enum extends Record<string, number | string>>(
|
|
4
|
+
e: Enum
|
|
5
|
+
) => {
|
|
6
|
+
const copy = { ...e } as { [K in EnumKeys<Enum>]: Enum[K] };
|
|
7
|
+
Object.values(e).forEach(
|
|
8
|
+
(value) => typeof value === "number" && delete copy[value]
|
|
9
|
+
);
|
|
10
|
+
return copy;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const enumKeys = <Enum extends Record<string, number | string>>(
|
|
14
|
+
e: Enum
|
|
15
|
+
) => {
|
|
16
|
+
return Object.keys(enumObject(e)) as EnumKeys<Enum>[];
|
|
17
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Log, LogLevel, Miniflare } from "miniflare";
|
|
2
|
+
import yargs from "yargs";
|
|
3
|
+
import { hideBin } from "yargs/helpers";
|
|
4
|
+
import { enumKeys } from "./enum-keys";
|
|
5
|
+
import { getRequestContextCheckOptions } from "./request-context";
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
const args = await yargs(hideBin(process.argv))
|
|
9
|
+
.help(false)
|
|
10
|
+
.version(false)
|
|
11
|
+
.option("log", {
|
|
12
|
+
choices: enumKeys(LogLevel),
|
|
13
|
+
}).argv;
|
|
14
|
+
|
|
15
|
+
const logLevel = LogLevel[args.log ?? "INFO"];
|
|
16
|
+
const requestContextCheckOptions = await getRequestContextCheckOptions();
|
|
17
|
+
const config = {
|
|
18
|
+
...JSON.parse((args._[0] as string) ?? "{}"),
|
|
19
|
+
...requestContextCheckOptions,
|
|
20
|
+
log: new Log(logLevel),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (logLevel > LogLevel.INFO) {
|
|
24
|
+
console.log("OPTIONS:\n", JSON.stringify(config, null, 2));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const mf = new Miniflare(config);
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
// Start Miniflare development server
|
|
31
|
+
await mf.startServer();
|
|
32
|
+
await mf.startScheduler();
|
|
33
|
+
} catch (e) {
|
|
34
|
+
mf.log.error(e as Error);
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
// Unmount any mounted workers
|
|
37
|
+
await mf.dispose();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
await main();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { MiniflareOptions } from "miniflare";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Certain runtime APIs are only available to workers during the "request context",
|
|
5
|
+
* which is any code that returns after receiving a request and before returning
|
|
6
|
+
* a response.
|
|
7
|
+
*
|
|
8
|
+
* Miniflare emulates this behavior by using an [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) and
|
|
9
|
+
* [checking at runtime](https://github.com/cloudflare/miniflare/blob/master/packages/shared/src/context.ts#L21-L36)
|
|
10
|
+
* to see if you're using those APIs during the request context.
|
|
11
|
+
*
|
|
12
|
+
* In certain environments `AsyncLocalStorage` is unavailable, such as in a
|
|
13
|
+
* [webcontainer](https://github.com/stackblitz/webcontainer-core).
|
|
14
|
+
* This function figures out if we're able to run those "request context" checks
|
|
15
|
+
* and returns [a set of options](https://miniflare.dev/core/standards#global-functionality-limits)
|
|
16
|
+
* that indicate to miniflare whether to run the checks or not.
|
|
17
|
+
*/
|
|
18
|
+
export const getRequestContextCheckOptions = async (): Promise<
|
|
19
|
+
Pick<MiniflareOptions, "globalAsyncIO" | "globalTimers" | "globalRandom">
|
|
20
|
+
> => {
|
|
21
|
+
// check that there's an implementation of AsyncLocalStorage
|
|
22
|
+
let hasAsyncLocalStorage = true;
|
|
23
|
+
try {
|
|
24
|
+
// ripped from the example here https://nodejs.org/api/async_context.html#class-asynclocalstorage
|
|
25
|
+
const { AsyncLocalStorage } = await import("node:async_hooks");
|
|
26
|
+
const storage = new AsyncLocalStorage<string>();
|
|
27
|
+
|
|
28
|
+
hasAsyncLocalStorage = storage.run("some-value", () => {
|
|
29
|
+
return storage.getStore() === "some-value";
|
|
30
|
+
});
|
|
31
|
+
} catch (e) {
|
|
32
|
+
hasAsyncLocalStorage = false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
globalAsyncIO: hasAsyncLocalStorage,
|
|
37
|
+
globalRandom: hasAsyncLocalStorage,
|
|
38
|
+
globalTimers: hasAsyncLocalStorage,
|
|
39
|
+
};
|
|
40
|
+
};
|