wrangler 0.0.0-e1e2ee5c → 0.0.0-e1f2576e

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