wrangler 0.0.0-e2a26a0 → 0.0.0-e322475

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.
Files changed (157) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +49 -5
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +4886 -0
  6. package/package.json +54 -27
  7. package/pages/functions/buildPlugin.ts +105 -0
  8. package/pages/functions/buildWorker.ts +93 -4
  9. package/pages/functions/filepath-routing.test.ts +223 -36
  10. package/pages/functions/filepath-routing.ts +78 -120
  11. package/pages/functions/routes.ts +13 -17
  12. package/pages/functions/template-plugin.ts +147 -0
  13. package/pages/functions/template-worker.ts +24 -17
  14. package/src/__tests__/config-cache.test.ts +36 -0
  15. package/src/__tests__/configuration.test.ts +3548 -0
  16. package/src/__tests__/dev.test.tsx +1049 -65
  17. package/src/__tests__/guess-worker-format.test.ts +85 -0
  18. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  19. package/src/__tests__/helpers/mock-account-id.ts +26 -8
  20. package/src/__tests__/helpers/mock-bin.ts +4 -1
  21. package/src/__tests__/helpers/mock-cfetch.ts +12 -8
  22. package/src/__tests__/helpers/mock-console.ts +49 -8
  23. package/src/__tests__/helpers/mock-dialogs.ts +99 -11
  24. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  25. package/src/__tests__/helpers/mock-istty.ts +27 -0
  26. package/src/__tests__/helpers/mock-kv.ts +9 -16
  27. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  28. package/src/__tests__/helpers/mock-process.ts +24 -0
  29. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  30. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  31. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  32. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  33. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  34. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  35. package/src/__tests__/https-options.test.ts +130 -0
  36. package/src/__tests__/index.test.ts +150 -435
  37. package/src/__tests__/init.test.ts +1992 -0
  38. package/src/__tests__/jest.setup.ts +75 -5
  39. package/src/__tests__/kv.test.ts +634 -253
  40. package/src/__tests__/logger.test.ts +141 -0
  41. package/src/__tests__/package-manager.test.ts +156 -9
  42. package/src/__tests__/pages.test.ts +754 -0
  43. package/src/__tests__/parse.test.ts +330 -0
  44. package/src/__tests__/publish.test.ts +5895 -961
  45. package/src/__tests__/r2.test.ts +233 -0
  46. package/src/__tests__/route.test.ts +45 -0
  47. package/src/__tests__/secret.test.ts +362 -91
  48. package/src/__tests__/tail.test.ts +690 -0
  49. package/src/__tests__/user.test.ts +132 -0
  50. package/src/__tests__/whoami.test.tsx +60 -7
  51. package/src/abort.d.ts +3 -0
  52. package/src/bundle-reporter.tsx +29 -0
  53. package/src/bundle.ts +204 -0
  54. package/src/cfetch/index.ts +27 -8
  55. package/src/cfetch/internal.ts +75 -34
  56. package/src/cli.ts +6 -2
  57. package/src/config/README.md +107 -0
  58. package/src/config/config.ts +243 -0
  59. package/src/config/diagnostics.ts +80 -0
  60. package/src/config/environment.ts +420 -0
  61. package/src/config/index.ts +225 -0
  62. package/src/config/validation-helpers.ts +584 -0
  63. package/src/config/validation.ts +1580 -0
  64. package/src/config-cache.ts +45 -0
  65. package/src/create-worker-preview.ts +185 -0
  66. package/src/create-worker-upload-form.ts +254 -0
  67. package/src/dev/dev-vars.ts +36 -0
  68. package/src/dev/dev.tsx +399 -0
  69. package/src/dev/local.tsx +321 -0
  70. package/src/dev/remote.tsx +249 -0
  71. package/src/dev/use-esbuild.ts +112 -0
  72. package/src/dialogs.tsx +64 -6
  73. package/src/durable.ts +102 -0
  74. package/src/entry.ts +328 -0
  75. package/src/environment-variables.ts +35 -0
  76. package/src/errors.ts +11 -0
  77. package/src/generate-auth-url.ts +33 -0
  78. package/src/generate-random-state.ts +16 -0
  79. package/src/git-client.ts +42 -0
  80. package/src/https-options.ts +123 -0
  81. package/src/index.tsx +1804 -1009
  82. package/src/inspect.ts +80 -14
  83. package/src/intl-polyfill.d.ts +139 -0
  84. package/src/{kv.tsx → kv.ts} +193 -69
  85. package/src/logger.ts +73 -0
  86. package/src/miniflare-cli/README.md +30 -0
  87. package/src/miniflare-cli/enum-keys.ts +17 -0
  88. package/src/miniflare-cli/index.ts +41 -0
  89. package/src/miniflare-cli/request-context.ts +40 -0
  90. package/src/module-collection.ts +226 -31
  91. package/src/open-in-browser.ts +17 -0
  92. package/src/package-manager.ts +79 -25
  93. package/src/pages.tsx +1131 -84
  94. package/src/parse.ts +222 -0
  95. package/src/proxy.ts +176 -70
  96. package/src/publish.ts +637 -308
  97. package/src/r2.ts +50 -0
  98. package/src/selfsigned.d.ts +29 -0
  99. package/src/sites.tsx +185 -57
  100. package/src/tail/filters.ts +279 -0
  101. package/src/tail/index.ts +299 -0
  102. package/src/tail/printing.ts +67 -0
  103. package/src/update-check.ts +19 -0
  104. package/src/user.tsx +349 -166
  105. package/src/whoami.tsx +5 -3
  106. package/src/{api/worker.ts → worker.ts} +65 -39
  107. package/src/zones.ts +73 -0
  108. package/templates/gitignore +171 -0
  109. package/templates/new-worker-scheduled.js +17 -0
  110. package/templates/new-worker-scheduled.ts +32 -0
  111. package/templates/new-worker.js +1 -1
  112. package/templates/new-worker.ts +17 -2
  113. package/templates/no-op-worker.js +10 -0
  114. package/templates/static-asset-facade.js +3 -7
  115. package/templates/tsconfig.json +105 -0
  116. package/wrangler-dist/cli.js +77463 -75999
  117. package/src/__tests__/helpers/mock-user.ts +0 -27
  118. package/src/__tests__/logout.test.ts +0 -50
  119. package/src/api/form_data.ts +0 -134
  120. package/src/api/preview.ts +0 -128
  121. package/src/config.ts +0 -641
  122. package/src/dev.tsx +0 -834
  123. package/src/tail.tsx +0 -73
  124. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  125. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  126. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  127. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  128. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  129. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  130. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  131. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  132. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  133. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  134. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  135. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  136. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  137. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  138. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  139. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  140. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  141. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  142. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  143. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  144. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  145. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  146. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  147. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  148. package/vendor/wrangler-mime/LICENSE +0 -21
  149. package/vendor/wrangler-mime/Mime.js +0 -97
  150. package/vendor/wrangler-mime/README.md +0 -187
  151. package/vendor/wrangler-mime/cli.js +0 -46
  152. package/vendor/wrangler-mime/index.js +0 -4
  153. package/vendor/wrangler-mime/lite.js +0 -4
  154. package/vendor/wrangler-mime/package.json +0 -52
  155. package/vendor/wrangler-mime/types/other.js +0 -1
  156. package/vendor/wrangler-mime/types/standard.js +0 -1
  157. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,399 @@
1
+ import { spawn } from "node:child_process";
2
+ import * as path from "node:path";
3
+ import { watch } from "chokidar";
4
+ import clipboardy from "clipboardy";
5
+ import commandExists from "command-exists";
6
+ import { Box, Text, useApp, useInput, useStdin } from "ink";
7
+ import React, { useState, useEffect, useRef } from "react";
8
+ import { withErrorBoundary, useErrorHandler } from "react-error-boundary";
9
+ import onExit from "signal-exit";
10
+ import tmp from "tmp-promise";
11
+ import { fetch } from "undici";
12
+ import { runCustomBuild } from "../entry";
13
+ import { openInspector } from "../inspect";
14
+ import { logger } from "../logger";
15
+ import openInBrowser from "../open-in-browser";
16
+ import { Local } from "./local";
17
+ import { Remote } from "./remote";
18
+ import { useEsbuild } from "./use-esbuild";
19
+ import type { Config } from "../config";
20
+ import type { Entry } from "../entry";
21
+ import type { AssetPaths } from "../sites";
22
+ import type { CfWorkerInit } from "../worker";
23
+
24
+ export type DevProps = {
25
+ name?: string;
26
+ entry: Entry;
27
+ port: number;
28
+ ip: string;
29
+ inspectorPort: number;
30
+ rules: Config["rules"];
31
+ accountId: string | undefined;
32
+ initialMode: "local" | "remote";
33
+ jsxFactory: string | undefined;
34
+ jsxFragment: string | undefined;
35
+ tsconfig: string | undefined;
36
+ upstreamProtocol: "https" | "http";
37
+ localProtocol: "https" | "http";
38
+ enableLocalPersistence: boolean;
39
+ bindings: CfWorkerInit["bindings"];
40
+ crons: Config["triggers"]["crons"];
41
+ isWorkersSite: boolean;
42
+ assetPaths: AssetPaths | undefined;
43
+ compatibilityDate: string;
44
+ compatibilityFlags: string[] | undefined;
45
+ usageModel: "bundled" | "unbound" | undefined;
46
+ minify: boolean | undefined;
47
+ nodeCompat: boolean | undefined;
48
+ build: Config["build"];
49
+ env: string | undefined;
50
+ legacyEnv: boolean;
51
+ zone: string | undefined;
52
+ host: string | undefined;
53
+ };
54
+
55
+ export function DevImplementation(props: DevProps): JSX.Element {
56
+ if (
57
+ !props.isWorkersSite &&
58
+ props.assetPaths &&
59
+ props.entry.format === "service-worker"
60
+ ) {
61
+ throw new Error(
62
+ "You cannot use the service-worker format with an `assets` directory yet. For information on how to migrate to the module-worker format, see: https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
63
+ );
64
+ }
65
+
66
+ if (props.bindings.wasm_modules && props.entry.format === "modules") {
67
+ throw new Error(
68
+ "You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"
69
+ );
70
+ }
71
+
72
+ if (props.bindings.text_blobs && props.entry.format === "modules") {
73
+ throw new Error(
74
+ "You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
75
+ );
76
+ }
77
+
78
+ if (props.bindings.data_blobs && props.entry.format === "modules") {
79
+ throw new Error(
80
+ "You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure `[rules]` in your wrangler.toml"
81
+ );
82
+ }
83
+
84
+ // only load the UI if we're running in a supported environment
85
+ const { isRawModeSupported } = useStdin();
86
+ return isRawModeSupported ? (
87
+ <InteractiveDevSession {...props} />
88
+ ) : (
89
+ <DevSession {...props} local={props.initialMode === "local"} />
90
+ );
91
+ }
92
+
93
+ function InteractiveDevSession(props: DevProps) {
94
+ const toggles = useHotkeys(
95
+ {
96
+ local: props.initialMode === "local",
97
+ tunnel: false,
98
+ },
99
+ props.port,
100
+ props.ip,
101
+ props.inspectorPort,
102
+ props.localProtocol
103
+ );
104
+
105
+ useTunnel(toggles.tunnel);
106
+
107
+ return (
108
+ <>
109
+ <DevSession {...props} local={toggles.local} />
110
+ <Box borderStyle="round" paddingLeft={1} paddingRight={1}>
111
+ <Text bold={true}>[b]</Text>
112
+ <Text> open a browser, </Text>
113
+ <Text bold={true}>[d]</Text>
114
+ <Text> open Devtools, </Text>
115
+ <Text bold={true}>[l]</Text>
116
+ <Text> {toggles.local ? "turn off" : "turn on"} local mode, </Text>
117
+ <Text bold={true}>[c]</Text>
118
+ <Text> clear console, </Text>
119
+ <Text bold={true}>[x]</Text>
120
+ <Text> to exit</Text>
121
+ </Box>
122
+ </>
123
+ );
124
+ }
125
+
126
+ type DevSessionProps = DevProps & {
127
+ local: boolean;
128
+ };
129
+
130
+ function DevSession(props: DevSessionProps) {
131
+ useCustomBuild(props.entry, props.build);
132
+
133
+ const directory = useTmpDir();
134
+
135
+ const bundle = useEsbuild({
136
+ entry: props.entry,
137
+ destination: directory,
138
+ jsxFactory: props.jsxFactory,
139
+ rules: props.rules,
140
+ jsxFragment: props.jsxFragment,
141
+ serveAssetsFromWorker: Boolean(
142
+ props.assetPaths && !props.isWorkersSite && props.local
143
+ ),
144
+ tsconfig: props.tsconfig,
145
+ minify: props.minify,
146
+ nodeCompat: props.nodeCompat,
147
+ });
148
+
149
+ return props.local ? (
150
+ <Local
151
+ name={props.name}
152
+ bundle={bundle}
153
+ format={props.entry.format}
154
+ compatibilityDate={props.compatibilityDate}
155
+ localProtocol={props.localProtocol}
156
+ compatibilityFlags={props.compatibilityFlags}
157
+ bindings={props.bindings}
158
+ assetPaths={props.assetPaths}
159
+ isWorkersSite={props.isWorkersSite}
160
+ port={props.port}
161
+ ip={props.ip}
162
+ rules={props.rules}
163
+ inspectorPort={props.inspectorPort}
164
+ enableLocalPersistence={props.enableLocalPersistence}
165
+ crons={props.crons}
166
+ />
167
+ ) : (
168
+ <Remote
169
+ name={props.name}
170
+ bundle={bundle}
171
+ format={props.entry.format}
172
+ accountId={props.accountId}
173
+ bindings={props.bindings}
174
+ assetPaths={props.assetPaths}
175
+ isWorkersSite={props.isWorkersSite}
176
+ port={props.port}
177
+ ip={props.ip}
178
+ localProtocol={props.localProtocol}
179
+ inspectorPort={props.inspectorPort}
180
+ compatibilityDate={props.compatibilityDate}
181
+ compatibilityFlags={props.compatibilityFlags}
182
+ usageModel={props.usageModel}
183
+ env={props.env}
184
+ legacyEnv={props.legacyEnv}
185
+ zone={props.zone}
186
+ host={props.host}
187
+ />
188
+ );
189
+ }
190
+
191
+ export interface DirectorySyncResult {
192
+ name: string;
193
+ removeCallback: () => void;
194
+ }
195
+
196
+ function useTmpDir(): string | undefined {
197
+ const [directory, setDirectory] = useState<DirectorySyncResult>();
198
+ const handleError = useErrorHandler();
199
+ useEffect(() => {
200
+ let dir: DirectorySyncResult | undefined;
201
+ try {
202
+ dir = tmp.dirSync({ unsafeCleanup: true });
203
+ setDirectory(dir);
204
+ return;
205
+ } catch (err) {
206
+ logger.error(
207
+ "Failed to create temporary directory to store built files."
208
+ );
209
+ handleError(err);
210
+ }
211
+ return () => {
212
+ dir?.removeCallback();
213
+ };
214
+ }, [handleError]);
215
+ return directory?.name;
216
+ }
217
+
218
+ function useCustomBuild(expectedEntry: Entry, build: Config["build"]): void {
219
+ useEffect(() => {
220
+ if (!build.command) return;
221
+ let watcher: ReturnType<typeof watch> | undefined;
222
+ if (build.watch_dir) {
223
+ watcher = watch(build.watch_dir, {
224
+ persistent: true,
225
+ ignoreInitial: true,
226
+ }).on("all", (_event, filePath) => {
227
+ const relativeFile =
228
+ path.relative(expectedEntry.directory, expectedEntry.file) || ".";
229
+ //TODO: we should buffer requests to the proxy until this completes
230
+ logger.log(`The file ${filePath} changed, restarting build...`);
231
+ runCustomBuild(expectedEntry.file, relativeFile, build).catch((err) => {
232
+ logger.error("Custom build failed:", err);
233
+ });
234
+ });
235
+ }
236
+
237
+ return () => {
238
+ watcher?.close();
239
+ };
240
+ }, [build, expectedEntry]);
241
+ }
242
+
243
+ function sleep(period: number) {
244
+ return new Promise((resolve) => setTimeout(resolve, period));
245
+ }
246
+ const SLEEP_DURATION = 2000;
247
+ // really need a first class api for this
248
+ const hostNameRegex = /userHostname="(.*)"/g;
249
+ async function findTunnelHostname() {
250
+ let hostName: string | undefined;
251
+ while (!hostName) {
252
+ try {
253
+ const resp = await fetch("http://localhost:8789/metrics");
254
+ const data = await resp.text();
255
+ const matches = Array.from(data.matchAll(hostNameRegex));
256
+ hostName = matches[0][1];
257
+ } catch (err) {
258
+ await sleep(SLEEP_DURATION);
259
+ }
260
+ }
261
+ return hostName;
262
+ }
263
+
264
+ /**
265
+ * Create a tunnel to the remote worker.
266
+ * We've disabled this for now until we figure out a better user experience.
267
+ */
268
+ function useTunnel(toggle: boolean) {
269
+ const tunnel = useRef<ReturnType<typeof spawn>>();
270
+ const removeSignalExitListener = useRef<() => void>();
271
+ // TODO: test if cloudflared is available, if not
272
+ // point them to a url where they can get docs to install it
273
+ useEffect(() => {
274
+ async function startTunnel() {
275
+ if (toggle) {
276
+ try {
277
+ await commandExists("cloudflared");
278
+ } catch (e) {
279
+ logger.warn(
280
+ "To share your worker on the Internet, please install `cloudflared` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation"
281
+ );
282
+ return;
283
+ }
284
+ logger.log("⎔ Starting a tunnel...");
285
+ tunnel.current = spawn("cloudflared", [
286
+ "tunnel",
287
+ "--url",
288
+ "http://localhost:8787",
289
+ "--metrics",
290
+ "localhost:8789",
291
+ ]);
292
+
293
+ tunnel.current.on("close", (code) => {
294
+ if (code) {
295
+ logger.log(`Tunnel process exited with code ${code}`);
296
+ }
297
+ });
298
+
299
+ removeSignalExitListener.current = onExit((_code, _signal) => {
300
+ logger.log("⎔ Shutting down local tunnel.");
301
+ tunnel.current?.kill();
302
+ tunnel.current = undefined;
303
+ });
304
+
305
+ const hostName = await findTunnelHostname();
306
+ await clipboardy.write(hostName);
307
+ logger.log(`⬣ Sharing at ${hostName}, copied to clipboard.`);
308
+ }
309
+ }
310
+
311
+ startTunnel().catch(async (err) => {
312
+ logger.error("tunnel:", err);
313
+ });
314
+
315
+ return () => {
316
+ if (tunnel.current) {
317
+ logger.log("⎔ Shutting down tunnel.");
318
+ tunnel.current?.kill();
319
+ tunnel.current = undefined;
320
+ removeSignalExitListener.current && removeSignalExitListener.current();
321
+ removeSignalExitListener.current = undefined;
322
+ }
323
+ };
324
+ }, [toggle]);
325
+ }
326
+
327
+ type useHotkeysInitialState = {
328
+ local: boolean;
329
+ tunnel: boolean;
330
+ };
331
+ function useHotkeys(
332
+ initial: useHotkeysInitialState,
333
+ port: number,
334
+ ip: string,
335
+ inspectorPort: number,
336
+ localProtocol: "http" | "https"
337
+ ) {
338
+ // UGH, we should put port in context instead
339
+ const [toggles, setToggles] = useState(initial);
340
+ const { exit } = useApp();
341
+ useInput(
342
+ async (
343
+ input,
344
+ // eslint-disable-next-line unused-imports/no-unused-vars
345
+ key
346
+ ) => {
347
+ switch (input.toLowerCase()) {
348
+ // clear console
349
+ case "c":
350
+ console.clear();
351
+ // This console.log causes Ink to re-render the `DevSession` component.
352
+ // Couldn't find a better way to tell it to do so...
353
+ console.log();
354
+ break;
355
+ // open browser
356
+ case "b": {
357
+ await openInBrowser(`${localProtocol}://${ip}:${port}`);
358
+ break;
359
+ }
360
+ // toggle inspector
361
+ case "d": {
362
+ await openInspector(inspectorPort);
363
+ break;
364
+ }
365
+ // toggle local
366
+ case "l":
367
+ setToggles((previousToggles) => ({
368
+ ...previousToggles,
369
+ local: !previousToggles.local,
370
+ }));
371
+ break;
372
+ // shut down
373
+ case "q":
374
+ case "x":
375
+ exit();
376
+ break;
377
+ default:
378
+ // nothing?
379
+ break;
380
+ }
381
+ }
382
+ );
383
+ return toggles;
384
+ }
385
+
386
+ function ErrorFallback(props: { error: Error }) {
387
+ const { exit } = useApp();
388
+ useEffect(() => exit(props.error));
389
+ return (
390
+ <>
391
+ <Text>Something went wrong:</Text>
392
+ <Text>{props.error.stack}</Text>
393
+ </>
394
+ );
395
+ }
396
+
397
+ export default withErrorBoundary(DevImplementation, {
398
+ FallbackComponent: ErrorFallback,
399
+ });