wrangler 2.19.0 → 3.0.0

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 (297) hide show
  1. package/README.md +4 -4
  2. package/bin/wrangler.js +9 -75
  3. package/package.json +5 -13
  4. package/templates/__tests__/tsconfig.tsbuildinfo +1 -1
  5. package/templates/checked-fetch.js +1 -1
  6. package/templates/first-party-worker-module-facade.ts +2 -2
  7. package/templates/middleware/common.ts +9 -4
  8. package/templates/middleware/loader-sw.ts +2 -7
  9. package/templates/new-worker-scheduled.ts +1 -1
  10. package/templates/new-worker.ts +1 -1
  11. package/templates/pages-dev-util.ts +4 -1
  12. package/templates/pages-shim.ts +0 -3
  13. package/templates/tsconfig.tsbuildinfo +1 -1
  14. package/wrangler-dist/cli.d.ts +160 -75
  15. package/wrangler-dist/cli.js +61843 -65907
  16. package/import_meta_url.js +0 -3
  17. package/miniflare-config-stubs/.env.empty +0 -0
  18. package/miniflare-config-stubs/package.empty.json +0 -1
  19. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  20. package/miniflare-dist/index.mjs +0 -6442
  21. package/src/__tests__/access.test.ts +0 -25
  22. package/src/__tests__/api-dev.test.ts +0 -238
  23. package/src/__tests__/api-devregistry.test.ts +0 -121
  24. package/src/__tests__/api.test.ts +0 -102
  25. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  26. package/src/__tests__/config-cache.test.ts +0 -42
  27. package/src/__tests__/configuration.test.ts +0 -4509
  28. package/src/__tests__/constellation.test.ts +0 -371
  29. package/src/__tests__/d1/d1.test.ts +0 -82
  30. package/src/__tests__/d1/execute.test.ts +0 -66
  31. package/src/__tests__/d1/migrate.test.ts +0 -257
  32. package/src/__tests__/d1/splitter.test.ts +0 -255
  33. package/src/__tests__/delete.test.ts +0 -272
  34. package/src/__tests__/deployments.test.ts +0 -369
  35. package/src/__tests__/dev.test.tsx +0 -1617
  36. package/src/__tests__/generate.test.ts +0 -237
  37. package/src/__tests__/get-host-from-url.test.ts +0 -16
  38. package/src/__tests__/guess-worker-format.test.ts +0 -120
  39. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  40. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  41. package/src/__tests__/helpers/end-event-loop.ts +0 -6
  42. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  43. package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
  44. package/src/__tests__/helpers/mock-bin.ts +0 -36
  45. package/src/__tests__/helpers/mock-console.ts +0 -112
  46. package/src/__tests__/helpers/mock-dialogs.ts +0 -139
  47. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
  48. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
  49. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  50. package/src/__tests__/helpers/mock-istty.ts +0 -74
  51. package/src/__tests__/helpers/mock-known-routes.ts +0 -12
  52. package/src/__tests__/helpers/mock-kv.ts +0 -46
  53. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
  54. package/src/__tests__/helpers/mock-process.ts +0 -34
  55. package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
  56. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  57. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  58. package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
  59. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  60. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -160
  61. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
  62. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
  63. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
  64. package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
  65. package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
  66. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
  67. package/src/__tests__/helpers/msw/index.ts +0 -52
  68. package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
  69. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  70. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  71. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
  72. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  73. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  74. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  75. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -11
  76. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  77. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  78. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  79. package/src/__tests__/https-options.test.ts +0 -163
  80. package/src/__tests__/index.test.ts +0 -282
  81. package/src/__tests__/init.test.ts +0 -3196
  82. package/src/__tests__/jest.setup.ts +0 -179
  83. package/src/__tests__/kv.test.ts +0 -1799
  84. package/src/__tests__/logger.test.ts +0 -207
  85. package/src/__tests__/logout.test.ts +0 -47
  86. package/src/__tests__/metrics.test.ts +0 -493
  87. package/src/__tests__/middleware.scheduled.test.ts +0 -145
  88. package/src/__tests__/middleware.test.ts +0 -816
  89. package/src/__tests__/mtls-certificates.test.ts +0 -589
  90. package/src/__tests__/package-manager.test.ts +0 -353
  91. package/src/__tests__/pages/deployment-list.test.ts +0 -80
  92. package/src/__tests__/pages/functions-build.test.ts +0 -452
  93. package/src/__tests__/pages/pages.test.ts +0 -81
  94. package/src/__tests__/pages/project-create.test.ts +0 -63
  95. package/src/__tests__/pages/project-list.test.ts +0 -110
  96. package/src/__tests__/pages/project-upload.test.ts +0 -500
  97. package/src/__tests__/pages/publish.test.ts +0 -2864
  98. package/src/__tests__/pages-deployment-tail.test.ts +0 -955
  99. package/src/__tests__/parse.test.ts +0 -436
  100. package/src/__tests__/paths.test.ts +0 -39
  101. package/src/__tests__/publish.test.ts +0 -8849
  102. package/src/__tests__/pubsub.test.ts +0 -496
  103. package/src/__tests__/queues.test.ts +0 -532
  104. package/src/__tests__/r2.test.ts +0 -374
  105. package/src/__tests__/route.test.ts +0 -45
  106. package/src/__tests__/secret.test.ts +0 -693
  107. package/src/__tests__/tail.test.ts +0 -958
  108. package/src/__tests__/test-old-node-version.js +0 -31
  109. package/src/__tests__/traverse-module-graph.test.ts +0 -220
  110. package/src/__tests__/tsconfig-sanity.ts +0 -12
  111. package/src/__tests__/tsconfig.json +0 -8
  112. package/src/__tests__/tsconfig.tsbuildinfo +0 -1
  113. package/src/__tests__/type-generation.test.ts +0 -234
  114. package/src/__tests__/user.test.ts +0 -118
  115. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  116. package/src/__tests__/validate-dev-props.test.ts +0 -56
  117. package/src/__tests__/version.test.ts +0 -35
  118. package/src/__tests__/whoami.test.tsx +0 -172
  119. package/src/__tests__/worker-namespace.test.ts +0 -340
  120. package/src/abort.d.ts +0 -3
  121. package/src/api/dev.ts +0 -316
  122. package/src/api/index.ts +0 -11
  123. package/src/api/mtls-certificate.ts +0 -148
  124. package/src/api/pages/create-worker-bundle-contents.ts +0 -76
  125. package/src/api/pages/index.ts +0 -5
  126. package/src/api/pages/publish.tsx +0 -355
  127. package/src/bundle-reporter.ts +0 -68
  128. package/src/bundle.ts +0 -908
  129. package/src/cfetch/index.ts +0 -158
  130. package/src/cfetch/internal.ts +0 -258
  131. package/src/cli.ts +0 -28
  132. package/src/config/README.md +0 -107
  133. package/src/config/config.ts +0 -282
  134. package/src/config/diagnostics.ts +0 -80
  135. package/src/config/environment.ts +0 -618
  136. package/src/config/index.ts +0 -403
  137. package/src/config/validation-helpers.ts +0 -597
  138. package/src/config/validation.ts +0 -2342
  139. package/src/config-cache.ts +0 -85
  140. package/src/constellation/createProject.tsx +0 -51
  141. package/src/constellation/deleteProject.ts +0 -51
  142. package/src/constellation/deleteProjectModel.ts +0 -68
  143. package/src/constellation/index.ts +0 -75
  144. package/src/constellation/listCatalog.tsx +0 -35
  145. package/src/constellation/listModel.tsx +0 -41
  146. package/src/constellation/listProject.tsx +0 -28
  147. package/src/constellation/listRuntime.tsx +0 -28
  148. package/src/constellation/options.ts +0 -17
  149. package/src/constellation/types.ts +0 -17
  150. package/src/constellation/uploadModel.tsx +0 -64
  151. package/src/constellation/utils.ts +0 -90
  152. package/src/create-worker-preview.ts +0 -293
  153. package/src/create-worker-upload-form.ts +0 -359
  154. package/src/d1/backups.tsx +0 -219
  155. package/src/d1/constants.ts +0 -2
  156. package/src/d1/create.tsx +0 -70
  157. package/src/d1/delete.ts +0 -53
  158. package/src/d1/execute.tsx +0 -357
  159. package/src/d1/formatTimeAgo.ts +0 -14
  160. package/src/d1/index.ts +0 -100
  161. package/src/d1/list.tsx +0 -62
  162. package/src/d1/migrations/apply.tsx +0 -212
  163. package/src/d1/migrations/create.tsx +0 -79
  164. package/src/d1/migrations/helpers.ts +0 -169
  165. package/src/d1/migrations/index.ts +0 -3
  166. package/src/d1/migrations/list.tsx +0 -95
  167. package/src/d1/migrations/options.ts +0 -23
  168. package/src/d1/options.ts +0 -22
  169. package/src/d1/splitter.ts +0 -161
  170. package/src/d1/types.ts +0 -25
  171. package/src/d1/utils.ts +0 -49
  172. package/src/delete.ts +0 -100
  173. package/src/deployments.ts +0 -368
  174. package/src/deprecated/index.ts +0 -144
  175. package/src/dev/dev-vars.ts +0 -39
  176. package/src/dev/dev.tsx +0 -600
  177. package/src/dev/get-local-persistence-path.ts +0 -31
  178. package/src/dev/local.tsx +0 -948
  179. package/src/dev/remote.tsx +0 -632
  180. package/src/dev/start-server.ts +0 -534
  181. package/src/dev/use-esbuild.ts +0 -203
  182. package/src/dev/validate-dev-props.ts +0 -40
  183. package/src/dev-registry.ts +0 -202
  184. package/src/dev.tsx +0 -929
  185. package/src/dialogs.ts +0 -136
  186. package/src/dispatch-namespace.ts +0 -211
  187. package/src/docs/helpers.ts +0 -50
  188. package/src/docs/index.ts +0 -54
  189. package/src/durable.ts +0 -102
  190. package/src/entry.ts +0 -337
  191. package/src/environment-variables/factory.ts +0 -89
  192. package/src/environment-variables/misc-variables.ts +0 -30
  193. package/src/errors.ts +0 -11
  194. package/src/generate/index.ts +0 -298
  195. package/src/git-client.ts +0 -135
  196. package/src/global-wrangler-config-path.ts +0 -26
  197. package/src/https-options.ts +0 -127
  198. package/src/index.ts +0 -768
  199. package/src/init.ts +0 -1032
  200. package/src/inspect.ts +0 -782
  201. package/src/intl-polyfill.d.ts +0 -139
  202. package/src/is-ci.ts +0 -14
  203. package/src/is-interactive.ts +0 -16
  204. package/src/jest.d.ts +0 -4
  205. package/src/kv/helpers.ts +0 -433
  206. package/src/kv/index.ts +0 -594
  207. package/src/logger.ts +0 -123
  208. package/src/metrics/index.ts +0 -5
  209. package/src/metrics/metrics-config.ts +0 -239
  210. package/src/metrics/metrics-dispatcher.ts +0 -96
  211. package/src/metrics/metrics-usage-headers.ts +0 -24
  212. package/src/metrics/send-event.ts +0 -99
  213. package/src/miniflare-cli/README.md +0 -30
  214. package/src/miniflare-cli/assets.ts +0 -251
  215. package/src/miniflare-cli/index.ts +0 -210
  216. package/src/miniflare-cli/request-context.ts +0 -40
  217. package/src/miniflare-cli/tsconfig.json +0 -9
  218. package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
  219. package/src/miniflare-cli/types.ts +0 -11
  220. package/src/module-collection.ts +0 -333
  221. package/src/mtls-certificate/cli.ts +0 -155
  222. package/src/open-in-browser.ts +0 -17
  223. package/src/package-manager.ts +0 -219
  224. package/src/pages/build.ts +0 -410
  225. package/src/pages/buildFunctions.ts +0 -140
  226. package/src/pages/constants.ts +0 -18
  227. package/src/pages/deployment-tails.ts +0 -281
  228. package/src/pages/deployments.tsx +0 -84
  229. package/src/pages/dev.ts +0 -716
  230. package/src/pages/errors.ts +0 -67
  231. package/src/pages/functions/buildPlugin.ts +0 -113
  232. package/src/pages/functions/buildWorker.ts +0 -291
  233. package/src/pages/functions/filepath-routing.test.ts +0 -234
  234. package/src/pages/functions/filepath-routing.ts +0 -189
  235. package/src/pages/functions/identifiers.ts +0 -78
  236. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  237. package/src/pages/functions/routes-consolidation.ts +0 -73
  238. package/src/pages/functions/routes-transformation.test.ts +0 -282
  239. package/src/pages/functions/routes-transformation.ts +0 -115
  240. package/src/pages/functions/routes-validation.test.ts +0 -403
  241. package/src/pages/functions/routes-validation.ts +0 -202
  242. package/src/pages/functions/routes.ts +0 -151
  243. package/src/pages/functions/tsconfig.json +0 -8
  244. package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
  245. package/src/pages/functions.ts +0 -86
  246. package/src/pages/hash.ts +0 -13
  247. package/src/pages/index.ts +0 -102
  248. package/src/pages/projects.tsx +0 -159
  249. package/src/pages/prompt-select-project.tsx +0 -31
  250. package/src/pages/publish.tsx +0 -267
  251. package/src/pages/types.ts +0 -46
  252. package/src/pages/upload.tsx +0 -469
  253. package/src/pages/utils.ts +0 -23
  254. package/src/parse.ts +0 -308
  255. package/src/paths.ts +0 -71
  256. package/src/proxy.ts +0 -694
  257. package/src/publish/index.ts +0 -274
  258. package/src/publish/publish.ts +0 -1060
  259. package/src/pubsub/index.ts +0 -286
  260. package/src/pubsub/pubsub-commands.ts +0 -623
  261. package/src/queues/cli/commands/consumer/add.ts +0 -71
  262. package/src/queues/cli/commands/consumer/index.ts +0 -19
  263. package/src/queues/cli/commands/consumer/remove.ts +0 -31
  264. package/src/queues/cli/commands/create.ts +0 -25
  265. package/src/queues/cli/commands/delete.ts +0 -26
  266. package/src/queues/cli/commands/index.ts +0 -35
  267. package/src/queues/cli/commands/list.ts +0 -25
  268. package/src/queues/client.ts +0 -136
  269. package/src/queues/utils.ts +0 -18
  270. package/src/r2/constants.ts +0 -4
  271. package/src/r2/helpers.ts +0 -132
  272. package/src/r2/index.ts +0 -289
  273. package/src/routes.ts +0 -140
  274. package/src/secret/index.ts +0 -376
  275. package/src/selfsigned.d.ts +0 -29
  276. package/src/sites.ts +0 -484
  277. package/src/tail/createTail.ts +0 -406
  278. package/src/tail/filters.ts +0 -277
  279. package/src/tail/index.ts +0 -211
  280. package/src/tail/printing.ts +0 -122
  281. package/src/traverse-module-graph.ts +0 -53
  282. package/src/tsconfig-sanity.ts +0 -16
  283. package/src/type-generation.ts +0 -181
  284. package/src/update-check.ts +0 -19
  285. package/src/user/access.ts +0 -68
  286. package/src/user/auth-variables.ts +0 -113
  287. package/src/user/choose-account.tsx +0 -39
  288. package/src/user/generate-auth-url.ts +0 -33
  289. package/src/user/generate-random-state.ts +0 -16
  290. package/src/user/index.ts +0 -2
  291. package/src/user/user.ts +0 -1234
  292. package/src/utils/collectKeyValues.ts +0 -14
  293. package/src/utils/render.ts +0 -93
  294. package/src/whoami.ts +0 -135
  295. package/src/worker.ts +0 -274
  296. package/src/yargs-types.ts +0 -37
  297. package/src/zones.ts +0 -191
package/src/inspect.ts DELETED
@@ -1,782 +0,0 @@
1
- import { readFile } from "fs/promises";
2
- import assert from "node:assert";
3
- import { createServer } from "node:http";
4
- import os from "node:os";
5
- import { URL } from "node:url";
6
-
7
- import open from "open";
8
- import { useEffect, useRef, useState } from "react";
9
- import { SourceMapConsumer } from "source-map";
10
- import WebSocket, { WebSocketServer } from "ws";
11
- import { version } from "../package.json";
12
- import { logger } from "./logger";
13
- import { waitForPortToBeAvailable } from "./proxy";
14
- import { getAccessToken } from "./user/access";
15
- import type Protocol from "devtools-protocol";
16
- import type { IncomingMessage, Server, ServerResponse } from "node:http";
17
- import type { MessageEvent } from "ws";
18
-
19
- /**
20
- * `useInspector` is a hook for debugging Workers applications
21
- * when using `wrangler dev`.
22
- *
23
- * When we start a session with `wrangler dev`, the Workers platform
24
- * also exposes a debugging websocket that implements the DevTools
25
- * Protocol. While we could just start up DevTools and connect to this
26
- * URL, that URL changes every time we make a change to the
27
- * worker, or when the session expires. Instead, we start up a proxy
28
- * server locally that acts as a bridge between the remote DevTools
29
- * server and the local DevTools instance. So whenever the URL changes,
30
- * we can can silently connect to it and keep the local DevTools instance
31
- * up to date. Further, we also intercept these messages and selectively
32
- * log them directly to the terminal (namely, calls to `console.<x>`,
33
- * and exceptions)
34
- */
35
-
36
- /**
37
- * TODO:
38
- * - clear devtools whenever we save changes to the worker
39
- * - clear devtools when we switch between local/remote modes
40
- * - handle more methods from console
41
- */
42
-
43
- interface InspectorProps {
44
- /**
45
- * The port that the local proxy server should listen on.
46
- */
47
- port: number;
48
- /**
49
- * The websocket URL exposed by Workers that the inspector should connect to.
50
- */
51
- inspectorUrl: string | undefined;
52
- /**
53
- * Whether console statements and exceptions should be logged to the terminal.
54
- * (We don't log them in local mode because they're already getting
55
- * logged to the terminal by nature of them actually running in node locally.)
56
- */
57
- logToTerminal: boolean;
58
- /**
59
- * Sourcemap path, so that stacktraces can be interpretted
60
- */
61
- sourceMapPath?: string | undefined;
62
-
63
- host?: string;
64
- }
65
-
66
- export default function useInspector(props: InspectorProps) {
67
- /** A unique ID for this session. */
68
- const inspectorIdRef = useRef(randomId());
69
-
70
- /** The websocket from the devtools instance. */
71
- const [localWebSocket, setLocalWebSocket] = useState<WebSocket>();
72
- /** The websocket from the edge */
73
- const [remoteWebSocket, setRemoteWebSocket] = useState<WebSocket>();
74
-
75
- /**
76
- * The local proxy server that acts as the bridge between
77
- * the remote websocket and the local DevTools instance.
78
- */
79
- const serverRef = useRef<Server>();
80
- if (serverRef.current === undefined) {
81
- serverRef.current = createServer(
82
- (req: IncomingMessage, res: ServerResponse) => {
83
- switch (req.url) {
84
- // We implement a couple of well known end points
85
- // that are queried for metadata by chrome://inspect
86
- case "/json/version":
87
- res.setHeader("Content-Type", "application/json");
88
- res.end(
89
- JSON.stringify({
90
- Browser: `wrangler/v${version}`,
91
- // TODO: (someday): The DevTools protocol should match that of Edge Worker.
92
- // This could be exposed by the preview API.
93
- "Protocol-Version": "1.3",
94
- })
95
- );
96
- return;
97
- case "/json":
98
- case "/json/list":
99
- {
100
- res.setHeader("Content-Type", "application/json");
101
- const localHost = `localhost:${props.port}/ws`;
102
- const devtoolsFrontendUrl = `devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=${localHost}`;
103
- const devtoolsFrontendUrlCompat = `devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=${localHost}`;
104
- res.end(
105
- JSON.stringify([
106
- {
107
- id: inspectorIdRef.current,
108
- type: "node",
109
- description: "workers",
110
- webSocketDebuggerUrl: `ws://${localHost}`,
111
- devtoolsFrontendUrl,
112
- devtoolsFrontendUrlCompat,
113
- // Below are fields that are visible in the DevTools UI.
114
- title: "Cloudflare Worker",
115
- faviconUrl: "https://workers.cloudflare.com/favicon.ico",
116
- url:
117
- "https://" +
118
- (remoteWebSocket
119
- ? new URL(remoteWebSocket.url).host
120
- : "workers.dev"),
121
- },
122
- ])
123
- );
124
- }
125
- return;
126
- default:
127
- break;
128
- }
129
- }
130
- );
131
- }
132
- const server = serverRef.current;
133
-
134
- /**
135
- * The websocket server that runs on top of the proxy server.
136
- */
137
- const wsServerRef = useRef<WebSocketServer>();
138
- if (wsServerRef.current === undefined) {
139
- wsServerRef.current = new WebSocketServer({
140
- server,
141
- clientTracking: true,
142
- });
143
- }
144
- const wsServer = wsServerRef.current;
145
-
146
- wsServer.on("connection", (ws: WebSocket) => {
147
- if (wsServer.clients.size > 1) {
148
- /** We only want to have one active Devtools instance at a time. */
149
- logger.error(
150
- "Tried to open a new devtools window when a previous one was already open."
151
- );
152
- ws.close(1013, "Too many clients; only one can be connected at a time");
153
- } else {
154
- // Since Wrangler proxies the inspector, reloading Chrome DevTools won't trigger debugger initialisation events (because it's connecting to an extant session).
155
- // This sends a `Debugger.disable` message to the remote when a new WebSocket connection is initialised,
156
- // with the assumption that the new connection will shortly send a `Debugger.enable` event and trigger re-initialisation.
157
- // The key initialisation messages that are needed are the `Debugger.scriptParsed events`.
158
- remoteWebSocket?.send(
159
- JSON.stringify({
160
- // This number is arbitrary, and is chosen to be high so as not to conflict with messages that DevTools might actually send.
161
- // For completeness, these options don't work: 0, -1, or Number.MAX_SAFE_INTEGER
162
- id: 100_000_000,
163
- method: "Debugger.disable",
164
- })
165
- );
166
- // As promised, save the created websocket in a state hook
167
- setLocalWebSocket(ws);
168
-
169
- ws.addEventListener("close", () => {
170
- // And and cleanup when devtools closes
171
- setLocalWebSocket(undefined);
172
- });
173
- }
174
- });
175
-
176
- /**
177
- * We start and stop the server in an effect to take advantage
178
- * of the component lifecycle. Convenient.
179
- */
180
- useEffect(() => {
181
- const abortController = new AbortController();
182
- async function startInspectorProxy() {
183
- await waitForPortToBeAvailable(props.port, {
184
- retryPeriod: 200,
185
- timeout: 2000,
186
- abortSignal: abortController.signal,
187
- });
188
- server.listen(props.port);
189
- }
190
- startInspectorProxy().catch((err) => {
191
- if ((err as { code: string }).code !== "ABORT_ERR") {
192
- logger.error("Failed to start inspector:", err);
193
- }
194
- });
195
- return () => {
196
- server.close();
197
- // Also disconnect any open websockets/devtools connections
198
- wsServer.clients.forEach((ws) => ws.close());
199
- wsServer.close();
200
- abortController.abort();
201
- };
202
- }, [props.port, server, wsServer]);
203
-
204
- /**
205
- * When connecting to the remote websocket, if we don't start either
206
- * the devtools instance or make an actual request to the worker in time,
207
- * then the connecting process can error out. When this happens, we
208
- * want to simply retry the connection. We use a state hook to trigger retries
209
- * of the effect that connects to the remote websocket.
210
- */
211
- const [
212
- retryRemoteWebSocketConnectionSigil,
213
- setRetryRemoteWebSocketConnectionSigil,
214
- ] = useState<number>(0);
215
- function retryRemoteWebSocketConnection() {
216
- setRetryRemoteWebSocketConnectionSigil((x) => x + 1);
217
- }
218
-
219
- /** A simple incrementing id to attach to messages we send to devtools */
220
- const messageCounterRef = useRef(1);
221
-
222
- const cfAccessRef = useRef<string>();
223
-
224
- useEffect(() => {
225
- const run = async () => {
226
- if (props.host && !cfAccessRef.current) {
227
- const token = await getAccessToken(props.host);
228
- cfAccessRef.current = token;
229
- }
230
- };
231
- if (props.host) void run();
232
- }, [props.host]);
233
-
234
- // This effect tracks the connection to the remote websocket
235
- // (stored in, no surprises here, `remoteWebSocket`)
236
- useEffect(() => {
237
- if (!props.inspectorUrl) {
238
- return;
239
- }
240
-
241
- // The actual websocket instance
242
- const ws = new WebSocket(props.inspectorUrl, {
243
- headers: {
244
- cookie: `CF_Authorization=${cfAccessRef.current}`,
245
- },
246
- });
247
- setRemoteWebSocket(ws);
248
-
249
- /**
250
- * A handle to the interval we run to keep the websocket alive
251
- */
252
- let keepAliveInterval: NodeJS.Timer;
253
-
254
- /**
255
- * Test if the websocket is closed
256
- */
257
- function isClosed() {
258
- return (
259
- ws.readyState === WebSocket.CLOSED ||
260
- ws.readyState === WebSocket.CLOSING
261
- );
262
- }
263
-
264
- /**
265
- * Send a message to the remote websocket
266
- */
267
- function send(event: Record<string, unknown>): void {
268
- if (!isClosed()) {
269
- ws.send(JSON.stringify(event));
270
- }
271
- }
272
-
273
- /**
274
- * Closes the inspector.
275
- */
276
- function close(): void {
277
- if (!isClosed()) {
278
- try {
279
- ws.close();
280
- } catch (err) {
281
- // Closing before the websocket is ready will throw an error.
282
- }
283
- }
284
- }
285
-
286
- /**
287
- * Since we have a handle on the remote websocket, we can tap
288
- * into its events, and log any pertinent ones directly to
289
- * the terminal (which means you have insight into your worker
290
- * without having to open the devtools).
291
- */
292
- if (props.logToTerminal) {
293
- ws.addEventListener("message", async (event: MessageEvent) => {
294
- if (typeof event.data === "string") {
295
- const evt = JSON.parse(event.data);
296
- if (evt.method === "Runtime.exceptionThrown") {
297
- const params = evt.params as Protocol.Runtime.ExceptionThrownEvent;
298
-
299
- // Parse stack trace with source map.
300
- if (props.sourceMapPath) {
301
- // Parse in the sourcemap
302
- const mapContent = JSON.parse(
303
- await readFile(props.sourceMapPath, "utf-8")
304
- );
305
-
306
- // Create the lines for the exception details log
307
- const exceptionLines = [
308
- params.exceptionDetails.exception?.description?.split("\n")[0],
309
- ];
310
-
311
- await SourceMapConsumer.with(
312
- mapContent,
313
- null,
314
- async (consumer) => {
315
- // Pass each of the callframes into the consumer, and format the error
316
- const stack = params.exceptionDetails.stackTrace?.callFrames;
317
-
318
- stack?.forEach(
319
- ({ functionName, lineNumber, columnNumber }, i) => {
320
- try {
321
- if (lineNumber) {
322
- // The line and column numbers in the stackTrace are zero indexed,
323
- // whereas the sourcemap consumer indexes from one.
324
- const pos = consumer.originalPositionFor({
325
- line: lineNumber + 1,
326
- column: columnNumber + 1,
327
- });
328
-
329
- // Print out line which caused error:
330
- if (i === 0 && pos.source && pos.line) {
331
- const fileSource = consumer.sourceContentFor(
332
- pos.source
333
- );
334
- const fileSourceLine =
335
- fileSource?.split("\n")[pos.line - 1] || "";
336
- exceptionLines.push(fileSourceLine.trim());
337
-
338
- // If we have a column, we can mark the position underneath
339
- if (pos.column) {
340
- exceptionLines.push(
341
- `${" ".repeat(
342
- pos.column - fileSourceLine.search(/\S/)
343
- )}^`
344
- );
345
- }
346
- }
347
-
348
- // From the way esbuild implements the "names" field:
349
- // > To save space, the original name is only recorded when it's different from the final name.
350
- // however, source-map consumer does not handle this
351
- if (pos && pos.line != null) {
352
- const convertedFnName =
353
- pos.name || functionName || "";
354
- exceptionLines.push(
355
- ` at ${convertedFnName} (${pos.source}:${pos.line}:${pos.column})`
356
- );
357
- }
358
- }
359
- } catch {
360
- // Line failed to parse through the sourcemap consumer
361
- // We should handle this better
362
- }
363
- }
364
- );
365
- }
366
- );
367
-
368
- // Log the parsed stacktrace
369
- logger.error(
370
- params.exceptionDetails.text,
371
- exceptionLines.join("\n")
372
- );
373
- } else {
374
- // We log the stacktrace to the terminal
375
- logger.error(
376
- params.exceptionDetails.text,
377
- params.exceptionDetails.exception?.description ?? ""
378
- );
379
- }
380
- }
381
- if (evt.method === "Runtime.consoleAPICalled") {
382
- logConsoleMessage(
383
- evt.params as Protocol.Runtime.ConsoleAPICalledEvent
384
- );
385
- }
386
- } else {
387
- // We should never get here, but who know is 2022...
388
- logger.error("Unrecognised devtools event:", event);
389
- }
390
- });
391
- }
392
-
393
- ws.addEventListener("open", () => {
394
- send({ method: "Runtime.enable", id: messageCounterRef.current });
395
- // TODO: This doesn't actually work. Must fix.
396
- send({ method: "Network.enable", id: messageCounterRef.current++ });
397
-
398
- keepAliveInterval = setInterval(() => {
399
- send({
400
- method: "Runtime.getIsolateId",
401
- id: messageCounterRef.current++,
402
- });
403
- }, 10_000);
404
- });
405
-
406
- ws.on("unexpected-response", () => {
407
- logger.log("Waiting for connection...");
408
- /**
409
- * This usually means the worker is not "ready" yet
410
- * so we'll just retry the connection process
411
- */
412
- retryRemoteWebSocketConnection();
413
- });
414
-
415
- ws.addEventListener("close", () => {
416
- clearInterval(keepAliveInterval);
417
- });
418
-
419
- return () => {
420
- // clean up! Let's first stop the heartbeat interval
421
- clearInterval(keepAliveInterval);
422
- // Then we'll send a message to the devtools instance to
423
- // tell it to clear the console.
424
- wsServer.clients.forEach((client) => {
425
- // We could've used `localSocket` here, but
426
- // then we would have had to add it to the effect
427
- // change detection array, which would have made a
428
- // bunch of other stuff complicated. So we'll just
429
- // cycle through all of the server's connected clients
430
- // (in practice, there should only be one or zero) and send
431
- // the Log.clear message.
432
- client.send(
433
- JSON.stringify({
434
- // TODO: This doesn't actually work. Must fix.
435
- method: "Log.clear",
436
- // we can disable the next eslint warning since
437
- // we're referencing a ref that stays alive
438
- // eslint-disable-next-line react-hooks/exhaustive-deps
439
- id: messageCounterRef.current++,
440
- params: {},
441
- })
442
- );
443
- });
444
- // Finally, we'll close the websocket
445
- close();
446
- // And we'll clear `remoteWebsocket`
447
- setRemoteWebSocket(undefined);
448
- };
449
- }, [
450
- props.inspectorUrl,
451
- props.logToTerminal,
452
- props.sourceMapPath,
453
- wsServer,
454
- // We use a state value as a sigil to trigger a retry of the
455
- // remote websocket connection. It's not used inside the effect,
456
- // so react-hooks/exhaustive-deps doesn't complain if it's not
457
- // included in the dependency array. But its presence is critical,
458
- // so do NOT remove it from the dependency list.
459
- retryRemoteWebSocketConnectionSigil,
460
- ]);
461
-
462
- /**
463
- * We want to make sure we don't lose any messages we receive from the
464
- * remote websocket before devtools connects. So we use a ref to buffer
465
- * messages, and flush them whenever devtools connects.
466
- */
467
- const messageBufferRef = useRef<MessageEvent[]>([]);
468
-
469
- // This effect tracks the state changes _between_ the local
470
- // and remote websockets, and handles how messages flow between them.
471
- useEffect(() => {
472
- /**
473
- * This event listener is used for buffering messages from
474
- * the remote websocket, and flushing them
475
- * when the local websocket connects.
476
- */
477
- function bufferMessageFromRemoteSocket(event: MessageEvent) {
478
- messageBufferRef.current.push(event);
479
- // TODO: maybe we should have a max limit on this?
480
- // if so, we should be careful when removing messages
481
- // from the front, because they could be critical for
482
- // devtools (like execution context creation, etc)
483
- }
484
-
485
- if (remoteWebSocket && !localWebSocket) {
486
- // The local websocket hasn't connected yet, so we'll
487
- // buffer messages until it does.
488
- remoteWebSocket.addEventListener(
489
- "message",
490
- bufferMessageFromRemoteSocket
491
- );
492
- }
493
-
494
- /** Send a message from the local websocket to the remote websocket */
495
- function sendMessageToRemoteWebSocket(event: MessageEvent) {
496
- try {
497
- assert(
498
- remoteWebSocket,
499
- "Trying to send a message to an undefined `remoteWebSocket`"
500
- );
501
- remoteWebSocket.send(event.data);
502
- } catch (e) {
503
- if (
504
- (e as Error).message !==
505
- "WebSocket is not open: readyState 0 (CONNECTING)"
506
- ) {
507
- /**
508
- * ^ this just means we haven't opened a websocket yet
509
- * usually happens until there's at least one request
510
- * which is weird, because we may miss something that
511
- * happens on the first request. Maybe we should buffer
512
- * these messages too?
513
- */
514
- logger.error(e);
515
- }
516
- }
517
- }
518
-
519
- /** Send a message from the local websocket to the remote websocket */
520
- function sendMessageToLocalWebSocket(event: MessageEvent) {
521
- assert(
522
- localWebSocket,
523
- "Trying to send a message to an undefined `localWebSocket`"
524
- );
525
- localWebSocket.send(event.data);
526
- }
527
-
528
- if (localWebSocket && remoteWebSocket) {
529
- // Both the remote and local websockets are connected, so let's
530
- // start sending messages between them.
531
- localWebSocket.addEventListener("message", sendMessageToRemoteWebSocket);
532
- remoteWebSocket.addEventListener("message", sendMessageToLocalWebSocket);
533
-
534
- // Also, let's flush any buffered messages
535
- messageBufferRef.current.forEach(sendMessageToLocalWebSocket);
536
- messageBufferRef.current = [];
537
- }
538
-
539
- return () => {
540
- // Cleanup like good citizens
541
- if (remoteWebSocket) {
542
- remoteWebSocket.removeEventListener(
543
- "message",
544
- bufferMessageFromRemoteSocket
545
- );
546
- remoteWebSocket.removeEventListener(
547
- "message",
548
- sendMessageToLocalWebSocket
549
- );
550
- }
551
- if (localWebSocket) {
552
- localWebSocket.removeEventListener(
553
- "message",
554
- sendMessageToRemoteWebSocket
555
- );
556
- }
557
- };
558
- }, [localWebSocket, remoteWebSocket]);
559
- }
560
-
561
- // Credit: https://stackoverflow.com/a/2117523
562
- function randomId(): string {
563
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
564
- const r = (Math.random() * 16) | 0,
565
- v = c == "x" ? r : (r & 0x3) | 0x8;
566
- return v.toString(16);
567
- });
568
- }
569
-
570
- /**
571
- * This function converts a message serialised as a devtools event
572
- * into arguments suitable to be called by a console method, and
573
- * then actually calls the method with those arguments. Effectively,
574
- * we're just doing a little bit of the work of the devtools console,
575
- * directly in the terminal.
576
- */
577
-
578
- export const mapConsoleAPIMessageTypeToConsoleMethod: {
579
- [key in Protocol.Runtime.ConsoleAPICalledEvent["type"]]: Exclude<
580
- keyof Console,
581
- "Console"
582
- >;
583
- } = {
584
- log: "log",
585
- debug: "debug",
586
- info: "info",
587
- warning: "warn",
588
- error: "error",
589
- dir: "dir",
590
- dirxml: "dirxml",
591
- table: "table",
592
- trace: "trace",
593
- clear: "clear",
594
- count: "count",
595
- assert: "assert",
596
- profile: "profile",
597
- profileEnd: "profileEnd",
598
- timeEnd: "timeEnd",
599
- startGroup: "group",
600
- startGroupCollapsed: "groupCollapsed",
601
- endGroup: "groupEnd",
602
- };
603
-
604
- function logConsoleMessage(evt: Protocol.Runtime.ConsoleAPICalledEvent): void {
605
- const args: string[] = [];
606
- for (const ro of evt.args) {
607
- switch (ro.type) {
608
- case "string":
609
- case "number":
610
- case "boolean":
611
- case "undefined":
612
- case "symbol":
613
- case "bigint":
614
- args.push(ro.value);
615
- break;
616
- case "function":
617
- args.push(`[Function: ${ro.description ?? "<no-description>"}]`);
618
- break;
619
- case "object":
620
- if (!ro.preview) {
621
- args.push(
622
- ro.subtype === "null"
623
- ? "null"
624
- : ro.description ?? "<no-description>"
625
- );
626
- } else {
627
- args.push(ro.preview.description ?? "<no-description>");
628
-
629
- switch (ro.preview.subtype) {
630
- case "array":
631
- args.push(
632
- "[ " +
633
- ro.preview.properties
634
- .map(({ value }) => {
635
- return value;
636
- })
637
- .join(", ") +
638
- (ro.preview.overflow ? "..." : "") +
639
- " ]"
640
- );
641
-
642
- break;
643
- case "weakmap":
644
- case "map":
645
- ro.preview.entries === undefined
646
- ? args.push("{}")
647
- : args.push(
648
- "{\n" +
649
- ro.preview.entries
650
- .map(({ key, value }) => {
651
- return ` ${key?.description ?? "<unknown>"} => ${
652
- value.description
653
- }`;
654
- })
655
- .join(",\n") +
656
- (ro.preview.overflow ? "\n ..." : "") +
657
- "\n}"
658
- );
659
-
660
- break;
661
- case "weakset":
662
- case "set":
663
- ro.preview.entries === undefined
664
- ? args.push("{}")
665
- : args.push(
666
- "{ " +
667
- ro.preview.entries
668
- .map(({ value }) => {
669
- return `${value.description}`;
670
- })
671
- .join(", ") +
672
- (ro.preview.overflow ? ", ..." : "") +
673
- " }"
674
- );
675
- break;
676
- case "regexp":
677
- break;
678
- case "date":
679
- break;
680
- case "generator":
681
- args.push(ro.preview.properties[0].value || "");
682
- break;
683
- case "promise":
684
- if (ro.preview.properties[0].value === "pending") {
685
- args.push(`{<${ro.preview.properties[0].value}>}`);
686
- } else {
687
- args.push(
688
- `{<${ro.preview.properties[0].value}>: ${ro.preview.properties[1].value}}`
689
- );
690
- }
691
- break;
692
- case "node":
693
- case "iterator":
694
- case "proxy":
695
- case "typedarray":
696
- case "arraybuffer":
697
- case "dataview":
698
- case "webassemblymemory":
699
- case "wasmvalue":
700
- break;
701
- case "error":
702
- default:
703
- // just a pojo
704
- args.push(
705
- "{\n" +
706
- ro.preview.properties
707
- .map(({ name, value }) => {
708
- return ` ${name}: ${value}`;
709
- })
710
- .join(",\n") +
711
- (ro.preview.overflow ? "\n ..." : "") +
712
- "\n}"
713
- );
714
- }
715
- }
716
- break;
717
- default:
718
- args.push(ro.description || ro.unserializableValue || "🦋");
719
- break;
720
- }
721
- }
722
-
723
- const method = mapConsoleAPIMessageTypeToConsoleMethod[evt.type];
724
-
725
- if (method in console) {
726
- switch (method) {
727
- case "dir":
728
- console.dir(args);
729
- break;
730
- case "table":
731
- console.table(args);
732
- break;
733
- default:
734
- // eslint-disable-next-line prefer-spread
735
- console[method].apply(console, args);
736
- break;
737
- }
738
- } else {
739
- logger.warn(`Unsupported console method: ${method}`);
740
- logger.warn("console event:", evt);
741
- }
742
- }
743
-
744
- /**
745
- * Opens the chrome debugger
746
- */
747
- export const openInspector = async (inspectorPort: number) => {
748
- const url = `https://devtools.devprod.cloudflare.dev/js_app?theme=systemPreferred&ws=localhost:${inspectorPort}/ws`;
749
- const errorMessage =
750
- "Failed to open inspector.\nInspector depends on having a Chromium-based browser installed, maybe you need to install one?";
751
-
752
- // see: https://github.com/sindresorhus/open/issues/177#issue-610016699
753
- let braveBrowser: string;
754
- switch (os.platform()) {
755
- case "darwin":
756
- case "win32":
757
- braveBrowser = "Brave";
758
- break;
759
- default:
760
- braveBrowser = "brave";
761
- }
762
-
763
- const childProcess = await open(url, {
764
- app: [
765
- {
766
- name: open.apps.chrome,
767
- },
768
- {
769
- name: braveBrowser,
770
- },
771
- {
772
- name: open.apps.edge,
773
- },
774
- {
775
- name: open.apps.firefox,
776
- },
777
- ],
778
- });
779
- childProcess.on("error", () => {
780
- logger.warn(errorMessage);
781
- });
782
- };