wrangler 0.0.0-e2a26a0 → 0.0.0-e3e3243
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 +1 -1
- package/bin/wrangler.js +10 -1
- package/miniflare-dist/index.mjs +79 -0
- package/miniflare-dist/index.mjs.map +7 -0
- package/package.json +24 -12
- package/pages/functions/filepath-routing.test.ts +212 -36
- package/pages/functions/filepath-routing.ts +69 -119
- package/pages/functions/routes.ts +8 -17
- package/pages/functions/template-worker.ts +3 -9
- package/src/__tests__/configuration.test.ts +2820 -0
- package/src/__tests__/dev.test.tsx +535 -65
- package/src/__tests__/guess-worker-format.test.ts +68 -0
- package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +26 -8
- package/src/__tests__/helpers/mock-bin.ts +4 -1
- package/src/__tests__/helpers/mock-cfetch.ts +5 -5
- package/src/__tests__/helpers/mock-console.ts +21 -7
- package/src/__tests__/helpers/mock-kv.ts +9 -16
- 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 +3 -0
- package/src/__tests__/helpers/run-wrangler.ts +5 -3
- 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 +467 -27
- package/src/__tests__/jest.setup.ts +37 -5
- package/src/__tests__/kv.test.ts +442 -201
- package/src/__tests__/logout.test.ts +15 -13
- package/src/__tests__/package-manager.test.ts +2 -2
- package/src/__tests__/pages.test.ts +82 -0
- package/src/__tests__/parse.test.ts +322 -0
- package/src/__tests__/publish.test.ts +2717 -430
- package/src/__tests__/r2.test.ts +198 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +301 -86
- package/src/__tests__/sentry.test.ts +146 -0
- package/src/__tests__/tail.test.ts +562 -0
- package/src/__tests__/whoami.test.tsx +21 -7
- package/src/bundle.ts +155 -0
- package/src/cfetch/index.ts +8 -6
- package/src/cfetch/internal.ts +60 -22
- package/src/cli.ts +10 -0
- package/src/config/README.md +107 -0
- package/src/config/config.ts +218 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +371 -0
- package/src/config/index.ts +62 -0
- package/src/config/validation-helpers.ts +554 -0
- package/src/config/validation.ts +1232 -0
- package/src/{api/preview.ts → create-worker-preview.ts} +59 -21
- package/src/create-worker-upload-form.ts +217 -0
- package/src/dev/dev.tsx +392 -0
- package/src/dev/local.tsx +232 -0
- package/src/dev/remote.tsx +203 -0
- package/src/dev/use-esbuild.ts +104 -0
- package/src/entry.ts +240 -0
- package/src/environment-variables.ts +33 -0
- package/src/https-options.ts +122 -0
- package/src/index.tsx +1067 -779
- package/src/inspect.ts +25 -4
- package/src/intl-polyfill.d.ts +139 -0
- package/src/{kv.tsx → kv.ts} +95 -60
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +37 -0
- package/src/module-collection.ts +226 -31
- package/src/open-in-browser.ts +25 -0
- package/src/package-manager.ts +21 -15
- package/src/pages.tsx +35 -20
- package/src/parse.ts +183 -0
- package/src/proxy.ts +115 -45
- package/src/publish.ts +268 -328
- package/src/r2.ts +50 -0
- package/src/reporting.ts +131 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +83 -43
- 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 +234 -126
- package/src/{api/worker.ts → worker.ts} +44 -36
- package/templates/new-worker.js +1 -1
- package/templates/new-worker.ts +1 -1
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +131201 -114242
- package/wrangler-dist/cli.js.map +3 -3
- package/src/__tests__/helpers/mock-user.ts +0 -27
- package/src/api/form_data.ts +0 -134
- package/src/config.ts +0 -641
- package/src/dev.tsx +0 -834
- package/src/tail.tsx +0 -73
package/src/inspect.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { URL } from "node:url";
|
|
|
5
5
|
import { useEffect, useRef, useState } from "react";
|
|
6
6
|
import WebSocket, { WebSocketServer } from "ws";
|
|
7
7
|
import { version } from "../package.json";
|
|
8
|
+
import { waitForPortToBeAvailable } from "./proxy";
|
|
8
9
|
import type Protocol from "devtools-protocol";
|
|
9
10
|
import type { IncomingMessage, Server, ServerResponse } from "node:http";
|
|
10
11
|
import type { MessageEvent } from "ws";
|
|
@@ -153,7 +154,14 @@ export default function useInspector(props: InspectorProps) {
|
|
|
153
154
|
* of the component lifecycle. Convenient.
|
|
154
155
|
*/
|
|
155
156
|
useEffect(() => {
|
|
156
|
-
|
|
157
|
+
async function startInspectorProxy() {
|
|
158
|
+
await waitForPortToBeAvailable(props.port, {
|
|
159
|
+
retryPeriod: 200,
|
|
160
|
+
timeout: 2000,
|
|
161
|
+
});
|
|
162
|
+
server.listen(props.port);
|
|
163
|
+
}
|
|
164
|
+
startInspectorProxy().catch((err) => console.error(err));
|
|
157
165
|
return () => {
|
|
158
166
|
server.close();
|
|
159
167
|
// Also disconnect any open websockets/devtools connections
|
|
@@ -367,7 +375,10 @@ export default function useInspector(props: InspectorProps) {
|
|
|
367
375
|
);
|
|
368
376
|
remoteWebSocket.send(event.data);
|
|
369
377
|
} catch (e) {
|
|
370
|
-
if (
|
|
378
|
+
if (
|
|
379
|
+
(e as Error).message !==
|
|
380
|
+
"WebSocket is not open: readyState 0 (CONNECTING)"
|
|
381
|
+
) {
|
|
371
382
|
/**
|
|
372
383
|
* ^ this just means we haven't opened a websocket yet
|
|
373
384
|
* usually happens until there's at least one request
|
|
@@ -587,8 +598,18 @@ function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
|
|
|
587
598
|
const method = mapConsoleAPIMessageTypeToConsoleMethod[evt.type];
|
|
588
599
|
|
|
589
600
|
if (method in console) {
|
|
590
|
-
|
|
591
|
-
|
|
601
|
+
switch (method) {
|
|
602
|
+
case "dir":
|
|
603
|
+
console.dir(args);
|
|
604
|
+
break;
|
|
605
|
+
case "table":
|
|
606
|
+
console.table(args);
|
|
607
|
+
break;
|
|
608
|
+
default:
|
|
609
|
+
// eslint-disable-next-line prefer-spread
|
|
610
|
+
console[method].apply(console, args);
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
592
613
|
} else {
|
|
593
614
|
console.warn(`Unsupported console method: ${method}`);
|
|
594
615
|
console.log("console event:", evt);
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TODO Remove once https://github.com/microsoft/TypeScript/pull/47254 lands
|
|
3
|
+
*/
|
|
4
|
+
declare namespace Intl {
|
|
5
|
+
interface DateTimeFormatOptions {
|
|
6
|
+
formatMatcher?: "basic" | "best fit" | "best fit" | undefined;
|
|
7
|
+
dateStyle?: "full" | "long" | "medium" | "short" | undefined;
|
|
8
|
+
timeStyle?: "full" | "long" | "medium" | "short" | undefined;
|
|
9
|
+
dayPeriod?: "narrow" | "short" | "long" | undefined;
|
|
10
|
+
fractionalSecondDigits?: 0 | 1 | 2 | 3 | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ResolvedDateTimeFormatOptions {
|
|
14
|
+
formatMatcher?: "basic" | "best fit" | "best fit";
|
|
15
|
+
dateStyle?: "full" | "long" | "medium" | "short";
|
|
16
|
+
timeStyle?: "full" | "long" | "medium" | "short";
|
|
17
|
+
hourCycle?: "h11" | "h12" | "h23" | "h24";
|
|
18
|
+
dayPeriod?: "narrow" | "short" | "long";
|
|
19
|
+
fractionalSecondDigits?: 0 | 1 | 2 | 3;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface NumberFormat {
|
|
23
|
+
formatRange(startDate: number | bigint, endDate: number | bigint): string;
|
|
24
|
+
formatRangeToParts(
|
|
25
|
+
startDate: number | bigint,
|
|
26
|
+
endDate: number | bigint
|
|
27
|
+
): NumberFormatPart[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The locale matching algorithm to use.
|
|
32
|
+
*
|
|
33
|
+
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation).
|
|
34
|
+
*/
|
|
35
|
+
type ListFormatLocaleMatcher = "lookup" | "best fit";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The format of output message.
|
|
39
|
+
*
|
|
40
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat#parameters).
|
|
41
|
+
*/
|
|
42
|
+
type ListFormatType = "conjunction" | "disjunction" | "unit";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The length of the formatted message.
|
|
46
|
+
*
|
|
47
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat#parameters).
|
|
48
|
+
*/
|
|
49
|
+
type ListFormatStyle = "long" | "short" | "narrow";
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* An object with some or all properties of the `Intl.ListFormat` constructor `options` parameter.
|
|
53
|
+
*
|
|
54
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat#parameters).
|
|
55
|
+
*/
|
|
56
|
+
interface ListFormatOptions {
|
|
57
|
+
/** The locale matching algorithm to use. For information about this option, see [Intl page](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation). */
|
|
58
|
+
localeMatcher?: ListFormatLocaleMatcher;
|
|
59
|
+
/** The format of output message. */
|
|
60
|
+
type?: ListFormatType;
|
|
61
|
+
/** The length of the internationalized message. */
|
|
62
|
+
style?: ListFormatStyle;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface ListFormat {
|
|
66
|
+
/**
|
|
67
|
+
* Returns a string with a language-specific representation of the list.
|
|
68
|
+
*
|
|
69
|
+
* @param list - An iterable object, such as an [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
|
|
70
|
+
*
|
|
71
|
+
* @throws `TypeError` if `list` includes something other than the possible values.
|
|
72
|
+
*
|
|
73
|
+
* @returns {string} A language-specific formatted string representing the elements of the list.
|
|
74
|
+
*
|
|
75
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/format).
|
|
76
|
+
*/
|
|
77
|
+
format(list: Iterable<string>): string;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Returns an Array of objects representing the different components that can be used to format a list of values in a locale-aware fashion.
|
|
81
|
+
*
|
|
82
|
+
* @param list - An iterable object, such as an [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), to be formatted according to a locale.
|
|
83
|
+
*
|
|
84
|
+
* @throws `TypeError` if `list` includes something other than the possible values.
|
|
85
|
+
*
|
|
86
|
+
* @returns {{ type: "element" | "literal", value: string; }[]} An Array of components which contains the formatted parts from the list.
|
|
87
|
+
*
|
|
88
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/formatToParts).
|
|
89
|
+
*/
|
|
90
|
+
formatToParts(
|
|
91
|
+
list: Iterable<string>
|
|
92
|
+
): { type: "element" | "literal"; value: string }[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const ListFormat: {
|
|
96
|
+
prototype: ListFormat;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Creates [Intl.ListFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat) objects that
|
|
100
|
+
* enable language-sensitive list formatting.
|
|
101
|
+
*
|
|
102
|
+
* @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
|
|
103
|
+
* For the general form and interpretation of the `locales` argument,
|
|
104
|
+
* see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
|
|
105
|
+
*
|
|
106
|
+
* @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat#parameters)
|
|
107
|
+
* with some or all options of `ListFormatOptions`.
|
|
108
|
+
*
|
|
109
|
+
* @returns [Intl.ListFormatOptions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat) object.
|
|
110
|
+
*
|
|
111
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat).
|
|
112
|
+
*/
|
|
113
|
+
new (
|
|
114
|
+
locales?: BCP47LanguageTag | BCP47LanguageTag[],
|
|
115
|
+
options?: ListFormatOptions
|
|
116
|
+
): ListFormat;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Returns an array containing those of the provided locales that are
|
|
120
|
+
* supported in list formatting without having to fall back to the runtime's default locale.
|
|
121
|
+
*
|
|
122
|
+
* @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
|
|
123
|
+
* For the general form and interpretation of the `locales` argument,
|
|
124
|
+
* see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
|
|
125
|
+
*
|
|
126
|
+
* @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/supportedLocalesOf#parameters).
|
|
127
|
+
* with some or all possible options.
|
|
128
|
+
*
|
|
129
|
+
* @returns An array of strings representing a subset of the given locale tags that are supported in list
|
|
130
|
+
* formatting without having to fall back to the runtime's default locale.
|
|
131
|
+
*
|
|
132
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/supportedLocalesOf).
|
|
133
|
+
*/
|
|
134
|
+
supportedLocalesOf(
|
|
135
|
+
locales: BCP47LanguageTag | BCP47LanguageTag[],
|
|
136
|
+
options?: Pick<ListFormatOptions, "localeMatcher">
|
|
137
|
+
): BCP47LanguageTag[];
|
|
138
|
+
};
|
|
139
|
+
}
|
package/src/{kv.tsx → kv.ts}
RENAMED
|
@@ -2,12 +2,16 @@ import { URLSearchParams } from "node:url";
|
|
|
2
2
|
import { fetchListResult, fetchResult, fetchKVGetValue } from "./cfetch";
|
|
3
3
|
import type { Config } from "./config";
|
|
4
4
|
|
|
5
|
+
/** The largest number of kv items we can pass to the API in a single request. */
|
|
6
|
+
const API_MAX = 10000;
|
|
7
|
+
// The const below are halved from the API's true capacity to help avoid
|
|
8
|
+
// hammering it with large requests.
|
|
9
|
+
const BATCH_KEY_MAX = API_MAX / 2;
|
|
10
|
+
|
|
5
11
|
type KvArgs = {
|
|
6
12
|
binding?: string;
|
|
7
13
|
"namespace-id"?: string;
|
|
8
|
-
env?: string;
|
|
9
14
|
preview?: boolean;
|
|
10
|
-
config?: Config;
|
|
11
15
|
};
|
|
12
16
|
|
|
13
17
|
/**
|
|
@@ -91,26 +95,64 @@ export async function listNamespaceKeys(
|
|
|
91
95
|
);
|
|
92
96
|
}
|
|
93
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Information about a key-value pair, including its "metadata" fields.
|
|
100
|
+
*/
|
|
101
|
+
export interface KeyValue {
|
|
102
|
+
key: string;
|
|
103
|
+
value: string;
|
|
104
|
+
expiration?: number;
|
|
105
|
+
expiration_ttl?: number;
|
|
106
|
+
metadata?: object;
|
|
107
|
+
base64?: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const KeyValueKeys = new Set([
|
|
111
|
+
"key",
|
|
112
|
+
"value",
|
|
113
|
+
"expiration",
|
|
114
|
+
"expiration_ttl",
|
|
115
|
+
"metadata",
|
|
116
|
+
"base64",
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Is the given object a valid `KeyValue` type?
|
|
121
|
+
*/
|
|
122
|
+
export function isKeyValue(keyValue: object): keyValue is KeyValue {
|
|
123
|
+
const props = Object.keys(keyValue);
|
|
124
|
+
if (!props.includes("key") || !props.includes("value")) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get all the properties on the `keyValue` that are not expected.
|
|
132
|
+
*/
|
|
133
|
+
export function unexpectedKeyValueProps(keyValue: KeyValue): string[] {
|
|
134
|
+
const props = Object.keys(keyValue);
|
|
135
|
+
return props.filter((prop) => !KeyValueKeys.has(prop));
|
|
136
|
+
}
|
|
137
|
+
|
|
94
138
|
export async function putKeyValue(
|
|
95
139
|
accountId: string,
|
|
96
140
|
namespaceId: string,
|
|
97
|
-
|
|
98
|
-
value: string,
|
|
99
|
-
args?: { expiration?: number; expiration_ttl?: number }
|
|
141
|
+
keyValue: KeyValue
|
|
100
142
|
) {
|
|
101
143
|
let searchParams: URLSearchParams | undefined;
|
|
102
|
-
if (
|
|
144
|
+
if (keyValue.expiration || keyValue.expiration_ttl) {
|
|
103
145
|
searchParams = new URLSearchParams();
|
|
104
|
-
if (
|
|
105
|
-
searchParams.set("expiration", `${
|
|
146
|
+
if (keyValue.expiration) {
|
|
147
|
+
searchParams.set("expiration", `${keyValue.expiration}`);
|
|
106
148
|
}
|
|
107
|
-
if (
|
|
108
|
-
searchParams.set("expiration_ttl", `${
|
|
149
|
+
if (keyValue.expiration_ttl) {
|
|
150
|
+
searchParams.set("expiration_ttl", `${keyValue.expiration_ttl}`);
|
|
109
151
|
}
|
|
110
152
|
}
|
|
111
153
|
return await fetchResult(
|
|
112
|
-
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
|
|
113
|
-
{ method: "PUT", body: value },
|
|
154
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${keyValue.key}`,
|
|
155
|
+
{ method: "PUT", body: keyValue.value },
|
|
114
156
|
searchParams
|
|
115
157
|
);
|
|
116
158
|
}
|
|
@@ -126,40 +168,57 @@ export async function getKeyValue(
|
|
|
126
168
|
export async function putBulkKeyValue(
|
|
127
169
|
accountId: string,
|
|
128
170
|
namespaceId: string,
|
|
129
|
-
|
|
171
|
+
keyValues: KeyValue[],
|
|
172
|
+
progressCallback: (index: number, total: number) => void
|
|
130
173
|
) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
method: "PUT",
|
|
135
|
-
body: keyvalueStr,
|
|
136
|
-
headers: { "Content-Type": "application/json" },
|
|
174
|
+
for (let index = 0; index < keyValues.length; index += BATCH_KEY_MAX) {
|
|
175
|
+
if (progressCallback && keyValues.length > BATCH_KEY_MAX) {
|
|
176
|
+
progressCallback(index, keyValues.length);
|
|
137
177
|
}
|
|
138
|
-
|
|
178
|
+
|
|
179
|
+
await fetchResult(
|
|
180
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
181
|
+
{
|
|
182
|
+
method: "PUT",
|
|
183
|
+
body: JSON.stringify(keyValues.slice(index, index + BATCH_KEY_MAX)),
|
|
184
|
+
headers: { "Content-Type": "application/json" },
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
if (progressCallback && keyValues.length > BATCH_KEY_MAX) {
|
|
189
|
+
progressCallback(keyValues.length, keyValues.length);
|
|
190
|
+
}
|
|
139
191
|
}
|
|
140
192
|
|
|
141
193
|
export async function deleteBulkKeyValue(
|
|
142
194
|
accountId: string,
|
|
143
195
|
namespaceId: string,
|
|
144
|
-
|
|
196
|
+
keys: string[],
|
|
197
|
+
progressCallback: (index: number, total: number) => void
|
|
145
198
|
) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
method: "DELETE",
|
|
150
|
-
body: keyStr,
|
|
151
|
-
headers: { "Content-Type": "application/json" },
|
|
199
|
+
for (let index = 0; index < keys.length; index += BATCH_KEY_MAX) {
|
|
200
|
+
if (progressCallback && keys.length > BATCH_KEY_MAX) {
|
|
201
|
+
progressCallback(index, keys.length);
|
|
152
202
|
}
|
|
153
|
-
|
|
203
|
+
|
|
204
|
+
await fetchResult(
|
|
205
|
+
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`,
|
|
206
|
+
{
|
|
207
|
+
method: "DELETE",
|
|
208
|
+
body: JSON.stringify(keys.slice(index, index + BATCH_KEY_MAX)),
|
|
209
|
+
headers: { "Content-Type": "application/json" },
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
if (progressCallback && keys.length > BATCH_KEY_MAX) {
|
|
214
|
+
progressCallback(keys.length, keys.length);
|
|
215
|
+
}
|
|
154
216
|
}
|
|
155
217
|
|
|
156
|
-
export function getNamespaceId(
|
|
157
|
-
preview,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
"namespace-id": namespaceId,
|
|
161
|
-
env,
|
|
162
|
-
}: KvArgs): string {
|
|
218
|
+
export function getNamespaceId(
|
|
219
|
+
{ preview, binding, "namespace-id": namespaceId }: KvArgs,
|
|
220
|
+
config: Config
|
|
221
|
+
): string {
|
|
163
222
|
// nice
|
|
164
223
|
if (namespaceId) {
|
|
165
224
|
return namespaceId;
|
|
@@ -167,7 +226,7 @@ export function getNamespaceId({
|
|
|
167
226
|
|
|
168
227
|
// begin pre-flight checks
|
|
169
228
|
|
|
170
|
-
// `--binding` is only valid if there's a wrangler configuration
|
|
229
|
+
// `--binding` is only valid if there's a wrangler configuration.
|
|
171
230
|
if (binding && !config) {
|
|
172
231
|
throw new Error("--binding specified, but no config file was found.");
|
|
173
232
|
}
|
|
@@ -180,30 +239,6 @@ export function getNamespaceId({
|
|
|
180
239
|
);
|
|
181
240
|
}
|
|
182
241
|
|
|
183
|
-
// they want to use an environment, actually
|
|
184
|
-
if (env) {
|
|
185
|
-
if (!config.env || !config.env[env]) {
|
|
186
|
-
throw new Error(
|
|
187
|
-
`Failed to find environment "${env}" in configuration file!`
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// TODO: either a bespoke arg type for this function to avoid `undefined`s or an `EnvOrConfig` type
|
|
192
|
-
return getNamespaceId({
|
|
193
|
-
binding,
|
|
194
|
-
"namespace-id": namespaceId,
|
|
195
|
-
env: undefined,
|
|
196
|
-
preview,
|
|
197
|
-
config: {
|
|
198
|
-
env: undefined,
|
|
199
|
-
build: undefined,
|
|
200
|
-
name: undefined,
|
|
201
|
-
account_id: undefined,
|
|
202
|
-
...config.env[env],
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
242
|
// there's no KV namespaces
|
|
208
243
|
if (!config.kv_namespaces || config.kv_namespaces.length === 0) {
|
|
209
244
|
throw new Error(
|
|
@@ -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,37 @@
|
|
|
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
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const args = await yargs(hideBin(process.argv))
|
|
8
|
+
.help(false)
|
|
9
|
+
.version(false)
|
|
10
|
+
.option("log", {
|
|
11
|
+
choices: enumKeys(LogLevel),
|
|
12
|
+
}).argv;
|
|
13
|
+
|
|
14
|
+
const logLevel = LogLevel[args.log ?? "INFO"];
|
|
15
|
+
const config = {
|
|
16
|
+
...JSON.parse((args._[0] as string) ?? "{}"),
|
|
17
|
+
log: new Log(logLevel),
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
if (logLevel > LogLevel.INFO) {
|
|
21
|
+
console.log("OPTIONS:\n", JSON.stringify(config, null, 2));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const mf = new Miniflare(config);
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
// Start Miniflare development server
|
|
28
|
+
await mf.startServer();
|
|
29
|
+
} catch (e) {
|
|
30
|
+
mf.log.error(e as Error);
|
|
31
|
+
process.exitCode = 1;
|
|
32
|
+
// Unmount any mounted workers
|
|
33
|
+
await mf.dispose();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
await main();
|