wrangler 0.0.0-ece06ea → 0.0.0-ecfbb0c
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 +33 -14
- package/bin/wrangler.js +123 -25
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +4893 -0
- package/package.json +157 -116
- package/src/__tests__/config-cache.test.ts +36 -0
- package/src/__tests__/configuration.test.ts +3829 -0
- package/src/__tests__/dev.test.tsx +1159 -0
- 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__/helpers/mock-cfetch.ts +211 -0
- package/src/__tests__/helpers/mock-console.ts +97 -0
- package/src/__tests__/helpers/mock-dialogs.ts +153 -0
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +74 -0
- package/src/__tests__/helpers/mock-kv.ts +33 -0
- package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
- package/src/__tests__/helpers/mock-process.ts +39 -0
- package/src/__tests__/helpers/mock-stdin.ts +108 -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 +255 -148
- package/src/__tests__/init.test.ts +1992 -0
- package/src/__tests__/jest.setup.ts +107 -0
- package/src/__tests__/kv.test.ts +1619 -0
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/package-manager.test.ts +353 -0
- package/src/__tests__/pages.test.ts +1254 -0
- package/src/__tests__/parse.test.ts +330 -0
- package/src/__tests__/publish.test.ts +6513 -0
- package/src/__tests__/pubsub.test.ts +276 -0
- package/src/__tests__/r2.test.ts +233 -0
- package/src/__tests__/route.test.ts +45 -0
- package/src/__tests__/secret.test.ts +486 -0
- package/src/__tests__/tail.test.ts +690 -0
- package/src/__tests__/user.test.ts +143 -0
- package/src/__tests__/whoami.test.tsx +220 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +39 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +212 -0
- package/src/cfetch/index.ts +121 -0
- package/src/cfetch/internal.ts +149 -0
- package/src/cli.ts +25 -8
- package/src/config/README.md +107 -0
- package/src/config/config.ts +243 -0
- package/src/config/diagnostics.ts +80 -0
- package/src/config/environment.ts +430 -0
- package/src/config/index.ts +225 -0
- package/src/config/validation-helpers.ts +584 -0
- package/src/config/validation.ts +1693 -0
- package/src/config-cache.ts +44 -0
- package/src/create-worker-preview.ts +199 -0
- package/src/create-worker-upload-form.ts +254 -0
- package/src/dev/dev-vars.ts +36 -0
- package/src/dev/dev.tsx +417 -0
- package/src/dev/local.tsx +350 -0
- package/src/dev/remote.tsx +294 -0
- package/src/dev/use-esbuild.ts +150 -0
- package/src/dev.tsx +517 -727
- package/src/dialogs.tsx +116 -66
- package/src/durable.ts +102 -0
- package/src/entry.ts +328 -0
- package/src/environment-variables.ts +35 -0
- package/src/errors.ts +11 -0
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/https-options.ts +123 -0
- package/src/index.tsx +1845 -1828
- package/src/init.ts +549 -0
- package/src/inspect.ts +662 -0
- package/src/intl-polyfill.d.ts +139 -0
- package/src/kv.ts +408 -0
- package/src/logger.ts +73 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/enum-keys.ts +17 -0
- package/src/miniflare-cli/index.ts +52 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/module-collection.ts +258 -0
- package/src/open-in-browser.ts +17 -0
- package/src/package-manager.ts +174 -0
- package/src/pages/build.tsx +202 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +101 -0
- package/src/pages/dev.tsx +964 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -0
- package/src/pages/functions/filepath-routing.test.ts +234 -0
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/index.tsx +84 -0
- package/src/pages/projects.tsx +157 -0
- package/src/pages/publish.tsx +335 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +384 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +286 -0
- package/src/paths.ts +26 -0
- package/src/preview.ts +31 -0
- package/src/proxy.ts +476 -0
- package/src/publish.ts +755 -343
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +594 -0
- package/src/r2.ts +50 -0
- package/src/selfsigned.d.ts +29 -0
- package/src/sites.tsx +357 -100
- 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/choose-account.tsx +60 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1130 -0
- package/src/whoami.tsx +90 -0
- package/src/worker.ts +185 -0
- package/src/zones.ts +76 -0
- package/templates/checked-fetch.js +17 -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/templates/no-op-worker.js +10 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +43 -0
- package/templates/tsconfig.json +105 -0
- package/wrangler-dist/cli.js +104118 -113381
- package/src/__tests__/fixtures/init/.gitkeep +0 -0
- package/src/__tests__/mock-cfetch.js +0 -46
- package/src/api/form_data.ts +0 -158
- package/src/api/inspect.ts +0 -441
- package/src/api/preview.ts +0 -123
- package/src/api/worker.ts +0 -161
- package/src/cfetch.ts +0 -72
- package/src/config.ts +0 -124
- package/src/kv.tsx +0 -211
- package/src/pages.tsx +0 -344
- package/src/tail.tsx +0 -71
- package/src/user.tsx +0 -1029
- package/src/util/fetch.ts +0 -74
- package/static-asset-facade.js +0 -47
- 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/dev.tsx
CHANGED
|
@@ -1,736 +1,526 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
import
|
|
13
|
-
import {
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
import
|
|
25
|
-
import
|
|
26
|
-
import
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
jsxFragment: props.jsxFragment,
|
|
64
|
-
polyfillNode: props.polyfillNode,
|
|
65
|
-
});
|
|
66
|
-
if (bundle && bundle.type === "commonjs" && !props.format && props.public) {
|
|
67
|
-
throw new Error(
|
|
68
|
-
"You cannot use the service worker format with a `public` directory."
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// @ts-expect-error whack
|
|
73
|
-
useDevtoolsRefresh(bundle?.id ?? 0);
|
|
74
|
-
|
|
75
|
-
const toggles = useHotkeys(
|
|
76
|
-
{
|
|
77
|
-
local: props.initialMode === "local",
|
|
78
|
-
tunnel: false,
|
|
79
|
-
},
|
|
80
|
-
port
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
useTunnel(toggles.tunnel);
|
|
84
|
-
|
|
85
|
-
return (
|
|
86
|
-
<>
|
|
87
|
-
{toggles.local ? (
|
|
88
|
-
<Local
|
|
89
|
-
name={props.name}
|
|
90
|
-
bundle={bundle}
|
|
91
|
-
format={props.format}
|
|
92
|
-
variables={props.variables}
|
|
93
|
-
site={props.site}
|
|
94
|
-
public={props.public}
|
|
95
|
-
port={props.port}
|
|
96
|
-
/>
|
|
97
|
-
) : (
|
|
98
|
-
<Remote
|
|
99
|
-
name={props.name}
|
|
100
|
-
bundle={bundle}
|
|
101
|
-
format={props.format}
|
|
102
|
-
accountId={props.accountId}
|
|
103
|
-
apiToken={apiToken}
|
|
104
|
-
variables={props.variables}
|
|
105
|
-
site={props.site}
|
|
106
|
-
public={props.public}
|
|
107
|
-
port={props.port}
|
|
108
|
-
compatibilityDate={props.compatibilityDate}
|
|
109
|
-
compatibilityFlags={props.compatibilityFlags}
|
|
110
|
-
usageModel={props.usageModel}
|
|
111
|
-
/>
|
|
112
|
-
)}
|
|
113
|
-
<Box borderStyle="round" paddingLeft={1} paddingRight={1}>
|
|
114
|
-
<Text>
|
|
115
|
-
{`B to open a browser, D to open Devtools, S to ${
|
|
116
|
-
toggles.tunnel ? "turn off" : "turn on"
|
|
117
|
-
} (experimental) sharing, L to ${
|
|
118
|
-
toggles.local ? "turn off" : "turn on"
|
|
119
|
-
} local mode, X to exit`}
|
|
120
|
-
</Text>
|
|
121
|
-
</Box>
|
|
122
|
-
</>
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function useDevtoolsRefresh(bundleId: number) {
|
|
127
|
-
// TODO: this is a hack while we figure out
|
|
128
|
-
// a better cleaner solution to get devtools to reconnect
|
|
129
|
-
// without having to do a full refresh
|
|
130
|
-
const ref = useRef();
|
|
131
|
-
// @ts-expect-error whack
|
|
132
|
-
ref.current = bundleId;
|
|
133
|
-
|
|
134
|
-
useEffect(() => {
|
|
135
|
-
const server = http.createServer((req, res) => {
|
|
136
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
137
|
-
res.setHeader("Access-Control-Request-Method", "*");
|
|
138
|
-
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
|
|
139
|
-
res.setHeader("Access-Control-Allow-Headers", "*");
|
|
140
|
-
if (req.method === "OPTIONS") {
|
|
141
|
-
res.writeHead(200);
|
|
142
|
-
res.end();
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
146
|
-
res.end(JSON.stringify({ value: ref.current }));
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
server.listen(3142);
|
|
150
|
-
return () => {
|
|
151
|
-
server.close();
|
|
152
|
-
};
|
|
153
|
-
}, []);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function Remote(props: {
|
|
157
|
-
name: void | string;
|
|
158
|
-
bundle: EsbuildBundle | void;
|
|
159
|
-
format: CfScriptFormat;
|
|
160
|
-
public: void | string;
|
|
161
|
-
site: void | string;
|
|
162
|
-
port: number;
|
|
163
|
-
accountId: void | string;
|
|
164
|
-
apiToken: void | string;
|
|
165
|
-
variables: { [name: string]: CfVariable };
|
|
166
|
-
compatibilityDate: string | void;
|
|
167
|
-
compatibilityFlags: void | string[];
|
|
168
|
-
usageModel: void | "bundled" | "unbound";
|
|
169
|
-
}) {
|
|
170
|
-
assert(props.accountId, "accountId is required");
|
|
171
|
-
assert(props.apiToken, "apiToken is required");
|
|
172
|
-
const token = useWorker({
|
|
173
|
-
name: props.name,
|
|
174
|
-
bundle: props.bundle,
|
|
175
|
-
format: props.format,
|
|
176
|
-
modules: [],
|
|
177
|
-
accountId: props.accountId,
|
|
178
|
-
apiToken: props.apiToken,
|
|
179
|
-
variables: props.variables,
|
|
180
|
-
sitesFolder: props.site,
|
|
181
|
-
port: props.port,
|
|
182
|
-
compatibilityDate: props.compatibilityDate,
|
|
183
|
-
compatibilityFlags: props.compatibilityFlags,
|
|
184
|
-
usageModel: props.usageModel,
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
useProxy({ token, publicRoot: props.public, port: props.port });
|
|
188
|
-
|
|
189
|
-
useInspector(token ? token.inspectorUrl.href : undefined);
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
function Local(props: {
|
|
193
|
-
name: void | string;
|
|
194
|
-
bundle: EsbuildBundle | void;
|
|
195
|
-
format: CfScriptFormat;
|
|
196
|
-
variables: { [name: string]: CfVariable };
|
|
197
|
-
public: void | string;
|
|
198
|
-
site: void | string;
|
|
199
|
-
port: number;
|
|
200
|
-
}) {
|
|
201
|
-
const { inspectorUrl } = useLocalWorker({
|
|
202
|
-
name: props.name,
|
|
203
|
-
bundle: props.bundle,
|
|
204
|
-
format: props.format,
|
|
205
|
-
variables: props.variables,
|
|
206
|
-
port: props.port,
|
|
207
|
-
});
|
|
208
|
-
useInspector(inspectorUrl);
|
|
209
|
-
return null;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function useLocalWorker(props: {
|
|
213
|
-
name: void | string;
|
|
214
|
-
bundle: EsbuildBundle | void;
|
|
215
|
-
format: CfScriptFormat;
|
|
216
|
-
variables: { [name: string]: CfVariable };
|
|
217
|
-
port: number;
|
|
218
|
-
}) {
|
|
219
|
-
// TODO: pass vars via command line
|
|
220
|
-
const { bundle, format, variables, port } = props;
|
|
221
|
-
const local = useRef<ReturnType<typeof spawn>>();
|
|
222
|
-
const removeSignalExitListener = useRef<() => void>();
|
|
223
|
-
const [inspectorUrl, setInspectorUrl] = useState<string | void>();
|
|
224
|
-
useEffect(() => {
|
|
225
|
-
async function startLocalWorker() {
|
|
226
|
-
if (!bundle) return;
|
|
227
|
-
if (format === "modules" && bundle.type === "commonjs") {
|
|
228
|
-
console.error("⎔ Cannot use modules with a commonjs bundle.");
|
|
229
|
-
// TODO: a much better error message here, with what to do next
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
if (format === "service-worker" && bundle.type !== "esm") {
|
|
233
|
-
console.error("⎔ Cannot use service-worker with a esm bundle.");
|
|
234
|
-
// TODO: a much better error message here, with what to do next
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
console.log("⎔ Starting a local server...");
|
|
239
|
-
local.current = spawn("node", [
|
|
240
|
-
"--experimental-vm-modules",
|
|
241
|
-
"--inspect",
|
|
242
|
-
require.resolve("miniflare/cli"),
|
|
243
|
-
bundle.path,
|
|
244
|
-
"--watch",
|
|
245
|
-
"--wrangler-config",
|
|
246
|
-
path.join(__dirname, "../miniflare-config-stubs/wrangler.empty.toml"),
|
|
247
|
-
"--env",
|
|
248
|
-
path.join(__dirname, "../miniflare-config-stubs/.env.empty"),
|
|
249
|
-
"--package",
|
|
250
|
-
path.join(__dirname, "../miniflare-config-stubs/package.empty.json"),
|
|
251
|
-
"--port",
|
|
252
|
-
port.toString(),
|
|
253
|
-
"--kv-persist",
|
|
254
|
-
"--cache-persist",
|
|
255
|
-
"--do-persist",
|
|
256
|
-
...Object.entries(variables)
|
|
257
|
-
.map(([varKey, varVal]) => {
|
|
258
|
-
if (typeof varVal === "string") {
|
|
259
|
-
return `--binding ${varKey}=${varVal}`;
|
|
260
|
-
} else if (
|
|
261
|
-
"namespaceId" in varVal &&
|
|
262
|
-
typeof varVal.namespaceId === "string"
|
|
263
|
-
) {
|
|
264
|
-
return `--kv ${varKey}`;
|
|
265
|
-
} else if ("class_name" in varVal) {
|
|
266
|
-
return `--do ${varKey}=${varVal.class_name}`;
|
|
267
|
-
}
|
|
268
|
-
})
|
|
269
|
-
.filter(Boolean),
|
|
270
|
-
"--modules",
|
|
271
|
-
format ||
|
|
272
|
-
(bundle.type === "esm" ? "modules" : "service-worker") === "modules"
|
|
273
|
-
? "true"
|
|
274
|
-
: "false",
|
|
275
|
-
]);
|
|
276
|
-
console.log(`⬣ Listening at http://localhost:${port}`);
|
|
277
|
-
|
|
278
|
-
local.current.on("close", (code) => {
|
|
279
|
-
if (code !== null) {
|
|
280
|
-
console.log(`miniflare process exited with code ${code}`);
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
local.current.stdout.on("data", (_data: string) => {
|
|
285
|
-
// console.log(`stdout: ${data}`);
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
local.current.stderr.on("data", (data: string) => {
|
|
289
|
-
// console.error(`stderr: ${data}`);
|
|
290
|
-
const matches =
|
|
291
|
-
/Debugger listening on (ws:\/\/127\.0\.0\.1:9229\/[A-Za-z0-9-]+)/.exec(
|
|
292
|
-
data
|
|
293
|
-
);
|
|
294
|
-
if (matches) {
|
|
295
|
-
setInspectorUrl(matches[1]);
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
removeSignalExitListener.current = onExit((_code, _signal) => {
|
|
300
|
-
console.log("⎔ Shutting down local server.");
|
|
301
|
-
local.current?.kill();
|
|
302
|
-
local.current = undefined;
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
startLocalWorker();
|
|
307
|
-
|
|
308
|
-
return () => {
|
|
309
|
-
if (local.current) {
|
|
310
|
-
console.log("⎔ Shutting down local server.");
|
|
311
|
-
local.current?.kill();
|
|
312
|
-
local.current = undefined;
|
|
313
|
-
removeSignalExitListener.current && removeSignalExitListener.current();
|
|
314
|
-
removeSignalExitListener.current = undefined;
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
}, [bundle, format, port]);
|
|
318
|
-
return { inspectorUrl };
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { watch } from "chokidar";
|
|
3
|
+
import getPort from "get-port";
|
|
4
|
+
import { render } from "ink";
|
|
5
|
+
import React from "react";
|
|
6
|
+
import { findWranglerToml, printBindings, readConfig } from "./config";
|
|
7
|
+
import Dev from "./dev/dev";
|
|
8
|
+
import { getVarsForDev } from "./dev/dev-vars";
|
|
9
|
+
|
|
10
|
+
import { getEntry } from "./entry";
|
|
11
|
+
import { logger } from "./logger";
|
|
12
|
+
import { getAssetPaths, getSiteAssetPaths } from "./sites";
|
|
13
|
+
import { getZoneIdFromHost, getZoneForRoute, getHostFromRoute } from "./zones";
|
|
14
|
+
import {
|
|
15
|
+
printWranglerBanner,
|
|
16
|
+
DEFAULT_LOCAL_PORT,
|
|
17
|
+
type ConfigPath,
|
|
18
|
+
getScriptName,
|
|
19
|
+
getDevCompatibilityDate,
|
|
20
|
+
getRules,
|
|
21
|
+
isLegacyEnv,
|
|
22
|
+
} from "./index";
|
|
23
|
+
|
|
24
|
+
import type { Config } from "./config";
|
|
25
|
+
import type { Route } from "./config/environment";
|
|
26
|
+
import type { Argv, ArgumentsCamelCase } from "yargs";
|
|
27
|
+
|
|
28
|
+
interface DevArgs {
|
|
29
|
+
config?: string;
|
|
30
|
+
script?: string;
|
|
31
|
+
name?: string;
|
|
32
|
+
build?: boolean;
|
|
33
|
+
format?: string;
|
|
34
|
+
env?: string;
|
|
35
|
+
"compatibility-date"?: string;
|
|
36
|
+
"compatibility-flags"?: string[];
|
|
37
|
+
latest?: boolean;
|
|
38
|
+
ip?: string;
|
|
39
|
+
inspect?: boolean;
|
|
40
|
+
port?: number;
|
|
41
|
+
"inspector-port"?: number;
|
|
42
|
+
routes?: string[];
|
|
43
|
+
host?: string;
|
|
44
|
+
"local-protocol"?: "http" | "https";
|
|
45
|
+
"local-upstream"?: string | undefined;
|
|
46
|
+
"experimental-public"?: string;
|
|
47
|
+
public?: string;
|
|
48
|
+
assets?: string;
|
|
49
|
+
site?: string;
|
|
50
|
+
"site-include"?: string[];
|
|
51
|
+
"site-exclude"?: string[];
|
|
52
|
+
"upstream-protocol"?: "http" | "https";
|
|
53
|
+
"jsx-factory"?: string;
|
|
54
|
+
"jsx-fragment"?: string;
|
|
55
|
+
tsconfig?: string;
|
|
56
|
+
local?: boolean;
|
|
57
|
+
minify?: boolean;
|
|
58
|
+
"node-compat"?: boolean;
|
|
59
|
+
"experimental-enable-local-persistence"?: boolean;
|
|
60
|
+
onReady?: () => void;
|
|
61
|
+
logLevel?: "none" | "error" | "log" | "warn" | "debug";
|
|
62
|
+
showInteractiveDevSession?: boolean;
|
|
319
63
|
}
|
|
320
64
|
|
|
321
|
-
function
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
65
|
+
export function devOptions(yargs: Argv): Argv<DevArgs> {
|
|
66
|
+
return (
|
|
67
|
+
yargs
|
|
68
|
+
.positional("script", {
|
|
69
|
+
describe: "The path to an entry point for your worker",
|
|
70
|
+
type: "string",
|
|
71
|
+
})
|
|
72
|
+
.option("name", {
|
|
73
|
+
describe: "Name of the worker",
|
|
74
|
+
type: "string",
|
|
75
|
+
requiresArg: true,
|
|
76
|
+
})
|
|
77
|
+
// We want to have a --no-build flag, but yargs requires that
|
|
78
|
+
// we also have a --build flag (that it adds the --no to by itself)
|
|
79
|
+
// So we make a --build flag, but hide it, and then add a --no-build flag
|
|
80
|
+
// that's visible to the user but doesn't "do" anything.
|
|
81
|
+
.option("build", {
|
|
82
|
+
describe: "Run wrangler's compilation step before publishing",
|
|
83
|
+
type: "boolean",
|
|
84
|
+
default: true,
|
|
85
|
+
hidden: true,
|
|
86
|
+
})
|
|
87
|
+
.option("no-build", {
|
|
88
|
+
describe: "Skip internal build steps and directly publish script",
|
|
89
|
+
type: "boolean",
|
|
90
|
+
default: false,
|
|
91
|
+
})
|
|
92
|
+
.option("format", {
|
|
93
|
+
choices: ["modules", "service-worker"] as const,
|
|
94
|
+
describe: "Choose an entry type",
|
|
95
|
+
deprecated: true,
|
|
96
|
+
})
|
|
97
|
+
.option("env", {
|
|
98
|
+
describe: "Perform on a specific environment",
|
|
99
|
+
type: "string",
|
|
100
|
+
requiresArg: true,
|
|
101
|
+
alias: "e",
|
|
102
|
+
})
|
|
103
|
+
.option("compatibility-date", {
|
|
104
|
+
describe: "Date to use for compatibility checks",
|
|
105
|
+
type: "string",
|
|
106
|
+
requiresArg: true,
|
|
107
|
+
})
|
|
108
|
+
.option("compatibility-flags", {
|
|
109
|
+
describe: "Flags to use for compatibility checks",
|
|
110
|
+
alias: "compatibility-flag",
|
|
111
|
+
type: "string",
|
|
112
|
+
requiresArg: true,
|
|
113
|
+
array: true,
|
|
114
|
+
})
|
|
115
|
+
.option("latest", {
|
|
116
|
+
describe: "Use the latest version of the worker runtime",
|
|
117
|
+
type: "boolean",
|
|
118
|
+
default: true,
|
|
119
|
+
})
|
|
120
|
+
.option("ip", {
|
|
121
|
+
describe: "IP address to listen on, defaults to `localhost`",
|
|
122
|
+
type: "string",
|
|
123
|
+
requiresArg: true,
|
|
124
|
+
})
|
|
125
|
+
.option("port", {
|
|
126
|
+
describe: "Port to listen on",
|
|
127
|
+
type: "number",
|
|
128
|
+
})
|
|
129
|
+
.option("inspector-port", {
|
|
130
|
+
describe: "Port for devtools to connect to",
|
|
131
|
+
type: "number",
|
|
132
|
+
})
|
|
133
|
+
.option("routes", {
|
|
134
|
+
describe: "Routes to upload",
|
|
135
|
+
alias: "route",
|
|
136
|
+
type: "string",
|
|
137
|
+
requiresArg: true,
|
|
138
|
+
array: true,
|
|
139
|
+
})
|
|
140
|
+
.option("host", {
|
|
141
|
+
type: "string",
|
|
142
|
+
requiresArg: true,
|
|
143
|
+
describe:
|
|
144
|
+
"Host to forward requests to, defaults to the zone of project",
|
|
145
|
+
})
|
|
146
|
+
.option("local-protocol", {
|
|
147
|
+
describe: "Protocol to listen to requests on, defaults to http.",
|
|
148
|
+
choices: ["http", "https"] as const,
|
|
149
|
+
})
|
|
150
|
+
.options("local-upstream", {
|
|
151
|
+
type: "string",
|
|
152
|
+
describe:
|
|
153
|
+
"Host to act as origin in local mode, defaults to dev.host or route",
|
|
154
|
+
})
|
|
155
|
+
.option("experimental-public", {
|
|
156
|
+
describe: "Static assets to be served",
|
|
157
|
+
type: "string",
|
|
158
|
+
requiresArg: true,
|
|
159
|
+
deprecated: true,
|
|
160
|
+
hidden: true,
|
|
161
|
+
})
|
|
162
|
+
.option("assets", {
|
|
163
|
+
describe: "Static assets to be served",
|
|
164
|
+
type: "string",
|
|
165
|
+
requiresArg: true,
|
|
166
|
+
})
|
|
167
|
+
.option("site", {
|
|
168
|
+
describe: "Root folder of static assets for Workers Sites",
|
|
169
|
+
type: "string",
|
|
170
|
+
requiresArg: true,
|
|
171
|
+
})
|
|
172
|
+
.option("site-include", {
|
|
173
|
+
describe:
|
|
174
|
+
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.",
|
|
175
|
+
type: "string",
|
|
176
|
+
requiresArg: true,
|
|
177
|
+
array: true,
|
|
178
|
+
})
|
|
179
|
+
.option("site-exclude", {
|
|
180
|
+
describe:
|
|
181
|
+
"Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.",
|
|
182
|
+
type: "string",
|
|
183
|
+
requiresArg: true,
|
|
184
|
+
array: true,
|
|
185
|
+
})
|
|
186
|
+
.option("upstream-protocol", {
|
|
187
|
+
describe: "Protocol to forward requests to host on, defaults to https.",
|
|
188
|
+
choices: ["http", "https"] as const,
|
|
189
|
+
})
|
|
190
|
+
.option("jsx-factory", {
|
|
191
|
+
describe: "The function that is called for each JSX element",
|
|
192
|
+
type: "string",
|
|
193
|
+
requiresArg: true,
|
|
194
|
+
})
|
|
195
|
+
.option("jsx-fragment", {
|
|
196
|
+
describe: "The function that is called for each JSX fragment",
|
|
197
|
+
type: "string",
|
|
198
|
+
requiresArg: true,
|
|
199
|
+
})
|
|
200
|
+
.option("tsconfig", {
|
|
201
|
+
describe: "Path to a custom tsconfig.json file",
|
|
202
|
+
type: "string",
|
|
203
|
+
requiresArg: true,
|
|
204
|
+
})
|
|
205
|
+
.option("local", {
|
|
206
|
+
alias: "l",
|
|
207
|
+
describe: "Run on my machine",
|
|
208
|
+
type: "boolean",
|
|
209
|
+
default: false, // I bet this will a point of contention. We'll revisit it.
|
|
210
|
+
})
|
|
211
|
+
.option("minify", {
|
|
212
|
+
describe: "Minify the script",
|
|
213
|
+
type: "boolean",
|
|
214
|
+
})
|
|
215
|
+
.option("node-compat", {
|
|
216
|
+
describe: "Enable node.js compatibility",
|
|
217
|
+
type: "boolean",
|
|
218
|
+
})
|
|
219
|
+
.option("experimental-enable-local-persistence", {
|
|
220
|
+
describe: "Enable persistence for this session (only for local mode)",
|
|
221
|
+
type: "boolean",
|
|
222
|
+
})
|
|
223
|
+
.option("inspect", {
|
|
224
|
+
describe: "Enable dev tools",
|
|
225
|
+
type: "boolean",
|
|
226
|
+
deprecated: true,
|
|
227
|
+
})
|
|
228
|
+
.option("legacy-env", {
|
|
229
|
+
type: "boolean",
|
|
230
|
+
describe: "Use legacy environments",
|
|
231
|
+
hidden: true,
|
|
232
|
+
})
|
|
233
|
+
);
|
|
342
234
|
}
|
|
343
235
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
destination: string | void;
|
|
355
|
-
staticRoot: void | string;
|
|
356
|
-
jsxFactory: string | void;
|
|
357
|
-
jsxFragment: string | void;
|
|
358
|
-
polyfillNode: boolean | void;
|
|
359
|
-
}): EsbuildBundle | void {
|
|
360
|
-
const {
|
|
361
|
-
entry,
|
|
362
|
-
destination,
|
|
363
|
-
staticRoot,
|
|
364
|
-
jsxFactory,
|
|
365
|
-
jsxFragment,
|
|
366
|
-
polyfillNode,
|
|
367
|
-
} = props;
|
|
368
|
-
const [bundle, setBundle] = useState<EsbuildBundle>();
|
|
369
|
-
useEffect(() => {
|
|
370
|
-
let result: esbuild.BuildResult;
|
|
371
|
-
async function build() {
|
|
372
|
-
if (!destination) return;
|
|
373
|
-
result = await esbuild.build({
|
|
374
|
-
entryPoints: [entry],
|
|
375
|
-
define: {
|
|
376
|
-
...(polyfillNode && { global: "globalThis" }),
|
|
377
|
-
},
|
|
378
|
-
bundle: true,
|
|
379
|
-
outdir: destination,
|
|
380
|
-
metafile: true,
|
|
381
|
-
format: "esm",
|
|
382
|
-
sourcemap: true,
|
|
383
|
-
loader: {
|
|
384
|
-
".js": "jsx",
|
|
385
|
-
},
|
|
386
|
-
plugins: polyfillNode
|
|
387
|
-
? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
|
|
388
|
-
: undefined,
|
|
389
|
-
...(jsxFactory && { jsxFactory }),
|
|
390
|
-
...(jsxFragment && { jsxFragment }),
|
|
391
|
-
external: ["__STATIC_CONTENT_MANIFEST"],
|
|
392
|
-
// TODO: import.meta.url
|
|
393
|
-
watch: {
|
|
394
|
-
async onRebuild(error) {
|
|
395
|
-
if (error) console.error("watch build failed:", error);
|
|
396
|
-
else {
|
|
397
|
-
// nothing really changes here, so let's increment the id
|
|
398
|
-
// to change the return object's identity
|
|
399
|
-
setBundle((bundle) => ({ ...bundle, id: bundle.id + 1 }));
|
|
400
|
-
}
|
|
401
|
-
},
|
|
402
|
-
},
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
const chunks = Object.entries(result.metafile.outputs).find(
|
|
406
|
-
([_path, { entryPoint }]) => entryPoint === entry
|
|
407
|
-
); // assumedly only one entry point
|
|
408
|
-
|
|
409
|
-
setBundle({
|
|
410
|
-
id: 0,
|
|
411
|
-
entry,
|
|
412
|
-
path: chunks[0],
|
|
413
|
-
type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
|
|
414
|
-
exports: chunks[1].exports,
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
build();
|
|
418
|
-
return () => {
|
|
419
|
-
result?.stop();
|
|
420
|
-
};
|
|
421
|
-
}, [entry, destination, staticRoot, jsxFactory, jsxFragment, polyfillNode]);
|
|
422
|
-
return bundle;
|
|
236
|
+
export async function devHandler(args: ArgumentsCamelCase<DevArgs>) {
|
|
237
|
+
let watcher;
|
|
238
|
+
try {
|
|
239
|
+
const devInstance = await startDev(args);
|
|
240
|
+
watcher = devInstance.watcher;
|
|
241
|
+
const { waitUntilExit } = devInstance.devReactElement;
|
|
242
|
+
await waitUntilExit();
|
|
243
|
+
} finally {
|
|
244
|
+
await watcher?.close();
|
|
245
|
+
}
|
|
423
246
|
}
|
|
424
247
|
|
|
425
|
-
function
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
//
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
248
|
+
export async function startDev(args: ArgumentsCamelCase<DevArgs>) {
|
|
249
|
+
let watcher: ReturnType<typeof watch> | undefined;
|
|
250
|
+
let rerender: (node: React.ReactNode) => void | undefined;
|
|
251
|
+
try {
|
|
252
|
+
if (args.logLevel) {
|
|
253
|
+
// we don't define a "none" logLevel, so "error" will do for now.
|
|
254
|
+
logger.loggerLevel = args.logLevel === "none" ? "error" : args.logLevel;
|
|
255
|
+
}
|
|
256
|
+
await printWranglerBanner();
|
|
257
|
+
|
|
258
|
+
const configPath =
|
|
259
|
+
(args.config as ConfigPath) ||
|
|
260
|
+
((args.script &&
|
|
261
|
+
findWranglerToml(path.dirname(args.script))) as ConfigPath);
|
|
262
|
+
let config = readConfig(configPath, args);
|
|
263
|
+
|
|
264
|
+
if (config.configPath) {
|
|
265
|
+
watcher = watch(config.configPath, {
|
|
266
|
+
persistent: true,
|
|
267
|
+
}).on("change", async (_event) => {
|
|
268
|
+
// TODO: Do we need to handle different `_event` types differently?
|
|
269
|
+
// e.g. what if the file is deleted, or added?
|
|
270
|
+
config = readConfig(configPath, args);
|
|
271
|
+
if (config.configPath) {
|
|
272
|
+
logger.log(`${path.basename(config.configPath)} changed...`);
|
|
273
|
+
rerender(await getDevReactElement(config));
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const entry = await getEntry(
|
|
279
|
+
{ assets: args.assets, script: args.script },
|
|
280
|
+
config,
|
|
281
|
+
"dev"
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
if (config.services && config.services.length > 0) {
|
|
285
|
+
logger.warn(
|
|
286
|
+
`This worker is bound to live services: ${config.services
|
|
287
|
+
.map(
|
|
288
|
+
(service) =>
|
|
289
|
+
`${service.binding} (${service.service}${
|
|
290
|
+
service.environment ? `@${service.environment}` : ""
|
|
291
|
+
})`
|
|
292
|
+
)
|
|
293
|
+
.join(", ")}`
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (args.inspect) {
|
|
298
|
+
//devtools are enabled by default, but we still need to disable them if the caller doesn't want them
|
|
299
|
+
logger.warn(
|
|
300
|
+
"Passing --inspect is unnecessary, now you can always connect to devtools."
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (args["experimental-public"]) {
|
|
305
|
+
throw new Error(
|
|
306
|
+
"The --experimental-public field has been renamed to --assets"
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (args.public) {
|
|
311
|
+
throw new Error("The --public field has been renamed to --assets");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if ((args.assets || config.assets) && (args.site || config.site)) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
"Cannot use Assets and Workers Sites in the same Worker."
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (args.assets) {
|
|
321
|
+
logger.warn(
|
|
322
|
+
"The --assets argument is experimental and may change or break at any time"
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const upstreamProtocol =
|
|
327
|
+
args["upstream-protocol"] || config.dev.upstream_protocol;
|
|
328
|
+
if (upstreamProtocol === "http") {
|
|
329
|
+
logger.warn(
|
|
330
|
+
"Setting upstream-protocol to http is not currently implemented.\n" +
|
|
331
|
+
"If this is required in your project, please add your use case to the following issue:\n" +
|
|
332
|
+
"https://github.com/cloudflare/wrangler2/issues/583."
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// TODO: if worker_dev = false and no routes, then error (only for dev)
|
|
337
|
+
|
|
338
|
+
// Compute zone info from the `host` and `route` args and config;
|
|
339
|
+
let host = args.host || config.dev.host;
|
|
340
|
+
let zoneId: string | undefined;
|
|
341
|
+
const routes: Route[] | undefined =
|
|
342
|
+
args.routes || (config.route && [config.route]) || config.routes;
|
|
343
|
+
|
|
344
|
+
if (!args.local) {
|
|
345
|
+
if (host) {
|
|
346
|
+
zoneId = await getZoneIdFromHost(host);
|
|
347
|
+
}
|
|
348
|
+
if (!zoneId && routes) {
|
|
349
|
+
const firstRoute = routes[0];
|
|
350
|
+
const zone = await getZoneForRoute(firstRoute);
|
|
351
|
+
if (zone) {
|
|
352
|
+
zoneId = zone.id;
|
|
353
|
+
host = zone.host;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
} else if (!host) {
|
|
357
|
+
if (routes) {
|
|
358
|
+
const firstRoute = routes[0];
|
|
359
|
+
host = getHostFromRoute(firstRoute);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const nodeCompat = args["node-compat"] ?? config.node_compat;
|
|
364
|
+
if (nodeCompat) {
|
|
365
|
+
logger.warn(
|
|
366
|
+
"Enabling node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// eslint-disable-next-line no-inner-declarations
|
|
371
|
+
async function getBindings(configParam: Config) {
|
|
372
|
+
return {
|
|
373
|
+
kv_namespaces: configParam.kv_namespaces?.map(
|
|
374
|
+
({ binding, preview_id, id: _id }) => {
|
|
375
|
+
// In `dev`, we make folks use a separate kv namespace called
|
|
376
|
+
// `preview_id` instead of `id` so that they don't
|
|
377
|
+
// break production data. So here we check that a `preview_id`
|
|
378
|
+
// has actually been configured.
|
|
379
|
+
// This whole block of code will be obsoleted in the future
|
|
380
|
+
// when we have copy-on-write for previews on edge workers.
|
|
381
|
+
if (!preview_id) {
|
|
382
|
+
// TODO: This error has to be a _lot_ better, ideally just asking
|
|
383
|
+
// to create a preview namespace for the user automatically
|
|
384
|
+
throw new Error(
|
|
385
|
+
`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`
|
|
386
|
+
); // Ugh, I really don't like this message very much
|
|
387
|
+
}
|
|
388
|
+
return {
|
|
389
|
+
binding,
|
|
390
|
+
id: preview_id,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
),
|
|
394
|
+
// Use a copy of combinedVars since we're modifying it later
|
|
395
|
+
vars: getVarsForDev(configParam),
|
|
396
|
+
wasm_modules: configParam.wasm_modules,
|
|
397
|
+
text_blobs: configParam.text_blobs,
|
|
398
|
+
data_blobs: configParam.data_blobs,
|
|
399
|
+
durable_objects: configParam.durable_objects,
|
|
400
|
+
r2_buckets: configParam.r2_buckets?.map(
|
|
401
|
+
({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
|
|
402
|
+
// same idea as kv namespace preview id,
|
|
403
|
+
// same copy-on-write TODO
|
|
404
|
+
if (!preview_bucket_name) {
|
|
405
|
+
throw new Error(
|
|
406
|
+
`In development, you should use a separate r2 bucket than the one you'd use in production. Please create a new r2 bucket with "wrangler r2 bucket create <name>" and add its name as preview_bucket_name to the r2_buckets "${binding}" in your wrangler.toml`
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
return {
|
|
410
|
+
binding,
|
|
411
|
+
bucket_name: preview_bucket_name,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
),
|
|
415
|
+
services: configParam.services,
|
|
416
|
+
unsafe: configParam.unsafe?.bindings,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
|
|
421
|
+
const getInspectorPort = memoizeGetPort(9229);
|
|
422
|
+
|
|
423
|
+
// eslint-disable-next-line no-inner-declarations
|
|
424
|
+
async function getDevReactElement(configParam: Config) {
|
|
425
|
+
// now log all available bindings into the terminal
|
|
426
|
+
const bindings = await getBindings(configParam);
|
|
427
|
+
// mask anything that was overridden in .dev.vars
|
|
428
|
+
// so that we don't log potential secrets into the terminal
|
|
429
|
+
const maskedVars = { ...bindings.vars };
|
|
430
|
+
for (const key of Object.keys(maskedVars)) {
|
|
431
|
+
if (maskedVars[key] !== configParam.vars[key]) {
|
|
432
|
+
// This means it was overridden in .dev.vars
|
|
433
|
+
// so let's mask it
|
|
434
|
+
maskedVars[key] = "(hidden)";
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
printBindings({
|
|
439
|
+
...bindings,
|
|
440
|
+
vars: maskedVars,
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const assetPaths =
|
|
444
|
+
args.assets || config.assets
|
|
445
|
+
? getAssetPaths(config, args.assets)
|
|
446
|
+
: getSiteAssetPaths(
|
|
447
|
+
config,
|
|
448
|
+
args.site,
|
|
449
|
+
args.siteInclude,
|
|
450
|
+
args.siteExclude
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
return (
|
|
454
|
+
<Dev
|
|
455
|
+
name={getScriptName({ name: args.name, env: args.env }, config)}
|
|
456
|
+
noBuild={!args.build}
|
|
457
|
+
entry={entry}
|
|
458
|
+
env={args.env}
|
|
459
|
+
zone={zoneId}
|
|
460
|
+
host={host}
|
|
461
|
+
routes={routes}
|
|
462
|
+
rules={getRules(config)}
|
|
463
|
+
legacyEnv={isLegacyEnv(config)}
|
|
464
|
+
minify={args.minify ?? config.minify}
|
|
465
|
+
nodeCompat={nodeCompat}
|
|
466
|
+
build={config.build || {}}
|
|
467
|
+
define={config.define}
|
|
468
|
+
initialMode={args.local ? "local" : "remote"}
|
|
469
|
+
jsxFactory={args["jsx-factory"] || config.jsx_factory}
|
|
470
|
+
jsxFragment={args["jsx-fragment"] || config.jsx_fragment}
|
|
471
|
+
tsconfig={args.tsconfig ?? config.tsconfig}
|
|
472
|
+
upstreamProtocol={upstreamProtocol}
|
|
473
|
+
localProtocol={args["local-protocol"] || config.dev.local_protocol}
|
|
474
|
+
localUpstream={args["local-upstream"] || host}
|
|
475
|
+
enableLocalPersistence={
|
|
476
|
+
args["experimental-enable-local-persistence"] || false
|
|
477
|
+
}
|
|
478
|
+
accountId={config.account_id}
|
|
479
|
+
assetPaths={assetPaths}
|
|
480
|
+
port={args.port || config.dev.port || (await getLocalPort())}
|
|
481
|
+
ip={args.ip || config.dev.ip}
|
|
482
|
+
inspectorPort={args["inspector-port"] ?? (await getInspectorPort())}
|
|
483
|
+
isWorkersSite={Boolean(args.site || config.site)}
|
|
484
|
+
compatibilityDate={getDevCompatibilityDate(
|
|
485
|
+
config,
|
|
486
|
+
args["compatibility-date"]
|
|
487
|
+
)}
|
|
488
|
+
compatibilityFlags={
|
|
489
|
+
args["compatibility-flags"] || config.compatibility_flags
|
|
490
|
+
}
|
|
491
|
+
usageModel={config.usage_model}
|
|
492
|
+
bindings={bindings}
|
|
493
|
+
crons={config.triggers.crons}
|
|
494
|
+
logLevel={args.logLevel}
|
|
495
|
+
onReady={args.onReady}
|
|
496
|
+
inspect={args.inspect}
|
|
497
|
+
showInteractiveDevSession={args.showInteractiveDevSession}
|
|
498
|
+
/>
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
const devReactElement = render(await getDevReactElement(config));
|
|
502
|
+
rerender = devReactElement.rerender;
|
|
503
|
+
return {
|
|
504
|
+
devReactElement,
|
|
505
|
+
watcher,
|
|
506
|
+
stop: async () => {
|
|
507
|
+
devReactElement.unmount();
|
|
508
|
+
await devReactElement.waitUntilExit();
|
|
509
|
+
await watcher?.close();
|
|
510
|
+
process.exit(0);
|
|
511
|
+
},
|
|
512
|
+
};
|
|
513
|
+
} finally {
|
|
514
|
+
await watcher?.close();
|
|
515
|
+
}
|
|
682
516
|
}
|
|
683
517
|
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
(
|
|
693
|
-
input,
|
|
694
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
695
|
-
key
|
|
696
|
-
) => {
|
|
697
|
-
switch (input) {
|
|
698
|
-
case "b": // open browser
|
|
699
|
-
open(
|
|
700
|
-
`http://localhost:${port}/`
|
|
701
|
-
// {
|
|
702
|
-
// app: {
|
|
703
|
-
// name: open.apps.chrome, // TODO: fallback on other browsers
|
|
704
|
-
// },
|
|
705
|
-
// }
|
|
706
|
-
);
|
|
707
|
-
break;
|
|
708
|
-
case "d": // toggle inspector
|
|
709
|
-
open(
|
|
710
|
-
`https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:9229/ws`
|
|
711
|
-
// {
|
|
712
|
-
// app: {
|
|
713
|
-
// name: open.apps.chrome,
|
|
714
|
-
// // todo - add firefox and edge fallbacks
|
|
715
|
-
// },
|
|
716
|
-
// }
|
|
717
|
-
);
|
|
718
|
-
break;
|
|
719
|
-
case "s": // toggle tunnel
|
|
720
|
-
setToggles((toggles) => ({ ...toggles, tunnel: !toggles.tunnel }));
|
|
721
|
-
break;
|
|
722
|
-
case "l": // toggle local
|
|
723
|
-
setToggles((toggles) => ({ ...toggles, local: !toggles.local }));
|
|
724
|
-
break;
|
|
725
|
-
case "q": // shut down
|
|
726
|
-
case "x": // shut down
|
|
727
|
-
process.exit(0);
|
|
728
|
-
break;
|
|
729
|
-
default:
|
|
730
|
-
// nothing?
|
|
731
|
-
break;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
);
|
|
735
|
-
return toggles;
|
|
518
|
+
/**
|
|
519
|
+
* Avoiding calling `getPort()` multiple times by memoizing the first result.
|
|
520
|
+
*/
|
|
521
|
+
function memoizeGetPort(defaultPort: number) {
|
|
522
|
+
let portValue: number;
|
|
523
|
+
return async () => {
|
|
524
|
+
return portValue || (portValue = await getPort({ port: defaultPort }));
|
|
525
|
+
};
|
|
736
526
|
}
|