wrangler 0.0.0-e2a26a0 → 0.0.0-e3278fa

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