wrangler 0.0.0-e87ad068 → 0.0.0-e89ee2a5

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 (327) hide show
  1. package/README.md +10 -12
  2. package/bin/wrangler.js +13 -74
  3. package/package.json +226 -199
  4. package/templates/__tests__/tsconfig.json +1 -1
  5. package/templates/checked-fetch.js +10 -3
  6. package/templates/facade.d.ts +4 -1
  7. package/templates/gitignore +0 -5
  8. package/templates/init-tests/test-vitest-new-worker.js +1 -1
  9. package/templates/init-tests/test-vitest-new-worker.ts +1 -1
  10. package/templates/middleware/common.ts +9 -4
  11. package/templates/middleware/loader-modules.ts +67 -15
  12. package/templates/middleware/loader-sw.ts +6 -10
  13. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  14. package/templates/middleware/middleware-ensure-req-body-drained.ts +19 -0
  15. package/templates/middleware/middleware-miniflare3-json-error.ts +1 -0
  16. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  17. package/templates/middleware/middleware-multiworker-dev.ts +68 -0
  18. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  19. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  20. package/templates/modules-watch-stub.js +4 -0
  21. package/templates/new-worker-scheduled.ts +1 -1
  22. package/templates/new-worker.ts +1 -1
  23. package/templates/pages-dev-pipeline.ts +2 -1
  24. package/templates/pages-shim.ts +0 -3
  25. package/templates/pages-template-plugin.ts +12 -2
  26. package/templates/pages-template-worker.ts +11 -2
  27. package/templates/startDevWorker/InspectorProxyWorker.ts +661 -0
  28. package/templates/startDevWorker/ProxyWorker.ts +326 -0
  29. package/templates/tsconfig.json +1 -1
  30. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  31. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  32. package/wrangler-dist/ProxyWorker.js +237 -0
  33. package/wrangler-dist/ProxyWorker.js.map +6 -0
  34. package/wrangler-dist/cli.d.ts +22559 -328
  35. package/wrangler-dist/cli.js +129487 -111124
  36. package/wrangler-dist/wasm-sync.wasm +0 -0
  37. package/import_meta_url.js +0 -3
  38. package/miniflare-config-stubs/.env.empty +0 -0
  39. package/miniflare-config-stubs/package.empty.json +0 -1
  40. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  41. package/miniflare-dist/index.mjs +0 -6442
  42. package/src/__tests__/access.test.ts +0 -25
  43. package/src/__tests__/api-dev.test.ts +0 -238
  44. package/src/__tests__/api-devregistry.test.ts +0 -121
  45. package/src/__tests__/api.test.ts +0 -102
  46. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  47. package/src/__tests__/config-cache.test.ts +0 -42
  48. package/src/__tests__/configuration.test.ts +0 -4727
  49. package/src/__tests__/constellation.test.ts +0 -371
  50. package/src/__tests__/d1/d1.test.ts +0 -82
  51. package/src/__tests__/d1/execute.test.ts +0 -66
  52. package/src/__tests__/d1/migrate.test.ts +0 -257
  53. package/src/__tests__/d1/splitter.test.ts +0 -255
  54. package/src/__tests__/delete.test.ts +0 -272
  55. package/src/__tests__/deployments.test.ts +0 -369
  56. package/src/__tests__/dev.test.tsx +0 -1617
  57. package/src/__tests__/generate.test.ts +0 -237
  58. package/src/__tests__/get-host-from-url.test.ts +0 -16
  59. package/src/__tests__/guess-worker-format.test.ts +0 -120
  60. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  61. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  62. package/src/__tests__/helpers/end-event-loop.ts +0 -6
  63. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  64. package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
  65. package/src/__tests__/helpers/mock-bin.ts +0 -36
  66. package/src/__tests__/helpers/mock-console.ts +0 -112
  67. package/src/__tests__/helpers/mock-dialogs.ts +0 -139
  68. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
  69. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
  70. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  71. package/src/__tests__/helpers/mock-istty.ts +0 -74
  72. package/src/__tests__/helpers/mock-known-routes.ts +0 -12
  73. package/src/__tests__/helpers/mock-kv.ts +0 -46
  74. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
  75. package/src/__tests__/helpers/mock-process.ts +0 -34
  76. package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
  77. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  78. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  79. package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
  80. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  81. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -160
  82. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
  83. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
  84. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
  85. package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
  86. package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
  87. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
  88. package/src/__tests__/helpers/msw/index.ts +0 -52
  89. package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
  90. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  91. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  92. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
  93. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  94. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  95. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  96. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -11
  97. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  98. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  99. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  100. package/src/__tests__/https-options.test.ts +0 -163
  101. package/src/__tests__/index.test.ts +0 -282
  102. package/src/__tests__/init.test.ts +0 -3302
  103. package/src/__tests__/jest.setup.ts +0 -179
  104. package/src/__tests__/kv.test.ts +0 -1799
  105. package/src/__tests__/logger.test.ts +0 -207
  106. package/src/__tests__/logout.test.ts +0 -47
  107. package/src/__tests__/metrics.test.ts +0 -493
  108. package/src/__tests__/middleware.scheduled.test.ts +0 -145
  109. package/src/__tests__/middleware.test.ts +0 -816
  110. package/src/__tests__/mtls-certificates.test.ts +0 -589
  111. package/src/__tests__/package-manager.test.ts +0 -353
  112. package/src/__tests__/pages/deployment-list.test.ts +0 -80
  113. package/src/__tests__/pages/functions-build.test.ts +0 -529
  114. package/src/__tests__/pages/pages.test.ts +0 -81
  115. package/src/__tests__/pages/project-create.test.ts +0 -63
  116. package/src/__tests__/pages/project-list.test.ts +0 -110
  117. package/src/__tests__/pages/project-upload.test.ts +0 -500
  118. package/src/__tests__/pages/publish.test.ts +0 -2864
  119. package/src/__tests__/pages-deployment-tail.test.ts +0 -957
  120. package/src/__tests__/parse.test.ts +0 -436
  121. package/src/__tests__/paths.test.ts +0 -39
  122. package/src/__tests__/publish.test.ts +0 -8882
  123. package/src/__tests__/pubsub.test.ts +0 -496
  124. package/src/__tests__/queues.test.ts +0 -532
  125. package/src/__tests__/r2.test.ts +0 -374
  126. package/src/__tests__/route.test.ts +0 -45
  127. package/src/__tests__/secret.test.ts +0 -693
  128. package/src/__tests__/tail.test.ts +0 -1000
  129. package/src/__tests__/test-old-node-version.js +0 -31
  130. package/src/__tests__/traverse-module-graph.test.ts +0 -220
  131. package/src/__tests__/tsconfig-sanity.ts +0 -12
  132. package/src/__tests__/tsconfig.json +0 -8
  133. package/src/__tests__/tsconfig.tsbuildinfo +0 -1
  134. package/src/__tests__/type-generation.test.ts +0 -234
  135. package/src/__tests__/unstableDev.test.ts +0 -61
  136. package/src/__tests__/user.test.ts +0 -118
  137. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  138. package/src/__tests__/validate-dev-props.test.ts +0 -56
  139. package/src/__tests__/version.test.ts +0 -35
  140. package/src/__tests__/whoami.test.tsx +0 -172
  141. package/src/__tests__/worker-namespace.test.ts +0 -340
  142. package/src/abort.d.ts +0 -3
  143. package/src/api/dev.ts +0 -328
  144. package/src/api/index.ts +0 -11
  145. package/src/api/mtls-certificate.ts +0 -148
  146. package/src/api/pages/create-worker-bundle-contents.ts +0 -80
  147. package/src/api/pages/index.ts +0 -5
  148. package/src/api/pages/publish.tsx +0 -371
  149. package/src/bundle-reporter.ts +0 -68
  150. package/src/bundle.ts +0 -952
  151. package/src/cfetch/index.ts +0 -158
  152. package/src/cfetch/internal.ts +0 -263
  153. package/src/cli.ts +0 -28
  154. package/src/config/README.md +0 -107
  155. package/src/config/config.ts +0 -282
  156. package/src/config/diagnostics.ts +0 -80
  157. package/src/config/environment.ts +0 -659
  158. package/src/config/index.ts +0 -424
  159. package/src/config/validation-helpers.ts +0 -597
  160. package/src/config/validation.ts +0 -2524
  161. package/src/config-cache.ts +0 -85
  162. package/src/constellation/createProject.tsx +0 -51
  163. package/src/constellation/deleteProject.ts +0 -51
  164. package/src/constellation/deleteProjectModel.ts +0 -76
  165. package/src/constellation/index.ts +0 -75
  166. package/src/constellation/listCatalog.tsx +0 -35
  167. package/src/constellation/listModel.tsx +0 -49
  168. package/src/constellation/listProject.tsx +0 -28
  169. package/src/constellation/listRuntime.tsx +0 -28
  170. package/src/constellation/options.ts +0 -17
  171. package/src/constellation/types.ts +0 -17
  172. package/src/constellation/uploadModel.tsx +0 -84
  173. package/src/constellation/utils.ts +0 -90
  174. package/src/create-worker-preview.ts +0 -293
  175. package/src/create-worker-upload-form.ts +0 -384
  176. package/src/d1/backups.tsx +0 -219
  177. package/src/d1/constants.ts +0 -5
  178. package/src/d1/create.tsx +0 -102
  179. package/src/d1/delete.ts +0 -53
  180. package/src/d1/execute.tsx +0 -371
  181. package/src/d1/formatTimeAgo.ts +0 -14
  182. package/src/d1/index.ts +0 -100
  183. package/src/d1/list.tsx +0 -62
  184. package/src/d1/migrations/apply.tsx +0 -235
  185. package/src/d1/migrations/create.tsx +0 -79
  186. package/src/d1/migrations/helpers.ts +0 -171
  187. package/src/d1/migrations/index.ts +0 -3
  188. package/src/d1/migrations/list.tsx +0 -95
  189. package/src/d1/migrations/options.ts +0 -23
  190. package/src/d1/options.ts +0 -22
  191. package/src/d1/splitter.ts +0 -161
  192. package/src/d1/types.ts +0 -32
  193. package/src/d1/utils.ts +0 -49
  194. package/src/delete.ts +0 -100
  195. package/src/deployments.ts +0 -368
  196. package/src/deprecated/index.ts +0 -144
  197. package/src/dev/dev-vars.ts +0 -39
  198. package/src/dev/dev.tsx +0 -607
  199. package/src/dev/get-local-persistence-path.ts +0 -31
  200. package/src/dev/local.tsx +0 -952
  201. package/src/dev/remote.tsx +0 -636
  202. package/src/dev/start-server.ts +0 -558
  203. package/src/dev/use-esbuild.ts +0 -230
  204. package/src/dev/validate-dev-props.ts +0 -40
  205. package/src/dev-registry.ts +0 -213
  206. package/src/dev.tsx +0 -946
  207. package/src/dialogs.ts +0 -136
  208. package/src/dispatch-namespace.ts +0 -211
  209. package/src/docs/helpers.ts +0 -50
  210. package/src/docs/index.ts +0 -54
  211. package/src/durable.ts +0 -102
  212. package/src/entry.ts +0 -344
  213. package/src/environment-variables/factory.ts +0 -89
  214. package/src/environment-variables/misc-variables.ts +0 -30
  215. package/src/errors.ts +0 -11
  216. package/src/generate/index.ts +0 -303
  217. package/src/git-client.ts +0 -135
  218. package/src/global-wrangler-config-path.ts +0 -26
  219. package/src/https-options.ts +0 -127
  220. package/src/index.ts +0 -769
  221. package/src/init.ts +0 -1074
  222. package/src/inspect.ts +0 -883
  223. package/src/intl-polyfill.d.ts +0 -139
  224. package/src/is-ci.ts +0 -14
  225. package/src/is-interactive.ts +0 -16
  226. package/src/jest.d.ts +0 -4
  227. package/src/kv/helpers.ts +0 -433
  228. package/src/kv/index.ts +0 -594
  229. package/src/logger.ts +0 -123
  230. package/src/metrics/index.ts +0 -5
  231. package/src/metrics/metrics-config.ts +0 -239
  232. package/src/metrics/metrics-dispatcher.ts +0 -96
  233. package/src/metrics/metrics-usage-headers.ts +0 -24
  234. package/src/metrics/send-event.ts +0 -99
  235. package/src/miniflare-cli/README.md +0 -30
  236. package/src/miniflare-cli/assets.ts +0 -251
  237. package/src/miniflare-cli/index.ts +0 -210
  238. package/src/miniflare-cli/request-context.ts +0 -40
  239. package/src/miniflare-cli/tsconfig.json +0 -9
  240. package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
  241. package/src/miniflare-cli/types.ts +0 -11
  242. package/src/module-collection.ts +0 -333
  243. package/src/mtls-certificate/cli.ts +0 -155
  244. package/src/open-in-browser.ts +0 -17
  245. package/src/package-manager.ts +0 -219
  246. package/src/pages/build.ts +0 -423
  247. package/src/pages/buildFunctions.ts +0 -140
  248. package/src/pages/constants.ts +0 -18
  249. package/src/pages/deployment-tails.ts +0 -281
  250. package/src/pages/deployments.tsx +0 -84
  251. package/src/pages/dev.ts +0 -759
  252. package/src/pages/errors.ts +0 -67
  253. package/src/pages/functions/buildPlugin.ts +0 -114
  254. package/src/pages/functions/buildWorker.ts +0 -381
  255. package/src/pages/functions/filepath-routing.test.ts +0 -234
  256. package/src/pages/functions/filepath-routing.ts +0 -189
  257. package/src/pages/functions/identifiers.ts +0 -78
  258. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  259. package/src/pages/functions/routes-consolidation.ts +0 -73
  260. package/src/pages/functions/routes-transformation.test.ts +0 -282
  261. package/src/pages/functions/routes-transformation.ts +0 -115
  262. package/src/pages/functions/routes-validation.test.ts +0 -403
  263. package/src/pages/functions/routes-validation.ts +0 -202
  264. package/src/pages/functions/routes.ts +0 -151
  265. package/src/pages/functions/tsconfig.json +0 -8
  266. package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
  267. package/src/pages/functions.ts +0 -86
  268. package/src/pages/hash.ts +0 -13
  269. package/src/pages/index.ts +0 -102
  270. package/src/pages/projects.tsx +0 -159
  271. package/src/pages/prompt-select-project.tsx +0 -31
  272. package/src/pages/publish.tsx +0 -267
  273. package/src/pages/types.ts +0 -46
  274. package/src/pages/upload.tsx +0 -469
  275. package/src/pages/utils.ts +0 -23
  276. package/src/parse.ts +0 -308
  277. package/src/paths.ts +0 -71
  278. package/src/proxy.ts +0 -694
  279. package/src/publish/index.ts +0 -274
  280. package/src/publish/publish.ts +0 -1068
  281. package/src/pubsub/index.ts +0 -286
  282. package/src/pubsub/pubsub-commands.ts +0 -623
  283. package/src/queues/cli/commands/consumer/add.ts +0 -71
  284. package/src/queues/cli/commands/consumer/index.ts +0 -19
  285. package/src/queues/cli/commands/consumer/remove.ts +0 -31
  286. package/src/queues/cli/commands/create.ts +0 -25
  287. package/src/queues/cli/commands/delete.ts +0 -26
  288. package/src/queues/cli/commands/index.ts +0 -35
  289. package/src/queues/cli/commands/list.ts +0 -25
  290. package/src/queues/client.ts +0 -136
  291. package/src/queues/utils.ts +0 -18
  292. package/src/r2/constants.ts +0 -4
  293. package/src/r2/helpers.ts +0 -132
  294. package/src/r2/index.ts +0 -289
  295. package/src/routes.ts +0 -140
  296. package/src/secret/index.ts +0 -380
  297. package/src/selfsigned.d.ts +0 -29
  298. package/src/sites.ts +0 -484
  299. package/src/tail/createTail.ts +0 -415
  300. package/src/tail/filters.ts +0 -277
  301. package/src/tail/index.ts +0 -211
  302. package/src/tail/printing.ts +0 -134
  303. package/src/traverse-module-graph.ts +0 -54
  304. package/src/tsconfig-sanity.ts +0 -16
  305. package/src/type-generation.ts +0 -187
  306. package/src/update-check.ts +0 -19
  307. package/src/user/access.ts +0 -68
  308. package/src/user/auth-variables.ts +0 -113
  309. package/src/user/choose-account.tsx +0 -39
  310. package/src/user/generate-auth-url.ts +0 -33
  311. package/src/user/generate-random-state.ts +0 -16
  312. package/src/user/index.ts +0 -2
  313. package/src/user/user.ts +0 -1234
  314. package/src/utils/collectKeyValues.ts +0 -14
  315. package/src/utils/render.ts +0 -93
  316. package/src/whoami.ts +0 -135
  317. package/src/worker.ts +0 -300
  318. package/src/yargs-types.ts +0 -37
  319. package/src/zones.ts +0 -191
  320. package/templates/__tests__/tsconfig.tsbuildinfo +0 -1
  321. package/templates/d1-beta-facade.js +0 -244
  322. package/templates/first-party-worker-module-facade.ts +0 -19
  323. package/templates/format-dev-errors.ts +0 -34
  324. package/templates/serve-static-assets.ts +0 -66
  325. package/templates/service-bindings-module-facade.js +0 -55
  326. package/templates/service-bindings-sw-facade.js +0 -42
  327. package/templates/tsconfig.tsbuildinfo +0 -1
@@ -1,1068 +0,0 @@
1
- import assert from "node:assert";
2
- import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
- import path from "node:path";
4
- import { URLSearchParams } from "node:url";
5
- import chalk from "chalk";
6
- import tmp from "tmp-promise";
7
- import { bundleWorker } from "../bundle";
8
- import {
9
- printBundleSize,
10
- printOffendingDependencies,
11
- } from "../bundle-reporter";
12
- import { fetchListResult, fetchResult } from "../cfetch";
13
- import { printBindings } from "../config";
14
- import { createWorkerUploadForm } from "../create-worker-upload-form";
15
- import { addHyphens } from "../deployments";
16
- import { confirm } from "../dialogs";
17
- import { getMigrationsToUpload } from "../durable";
18
- import { logger } from "../logger";
19
- import { getMetricsUsageHeaders } from "../metrics";
20
- import { ParseError } from "../parse";
21
- import { getQueue, putConsumer } from "../queues/client";
22
- import { getWorkersDevSubdomain } from "../routes";
23
- import { syncAssets } from "../sites";
24
- import traverseModuleGraph from "../traverse-module-graph";
25
- import { identifyD1BindingsAsBeta } from "../worker";
26
- import { getZoneForRoute } from "../zones";
27
- import type { FetchError } from "../cfetch";
28
- import type { Config } from "../config";
29
- import type {
30
- Route,
31
- ZoneIdRoute,
32
- ZoneNameRoute,
33
- CustomDomainRoute,
34
- } from "../config/environment";
35
- import type { Entry } from "../entry";
36
- import type { PutConsumerBody } from "../queues/client";
37
- import type { AssetPaths } from "../sites";
38
- import type { CfWorkerInit, CfPlacement } from "../worker";
39
-
40
- type Props = {
41
- config: Config;
42
- accountId: string | undefined;
43
- entry: Entry;
44
- rules: Config["rules"];
45
- name: string | undefined;
46
- env: string | undefined;
47
- compatibilityDate: string | undefined;
48
- compatibilityFlags: string[] | undefined;
49
- assetPaths: AssetPaths | undefined;
50
- vars: Record<string, string> | undefined;
51
- defines: Record<string, string> | undefined;
52
- triggers: string[] | undefined;
53
- routes: string[] | undefined;
54
- legacyEnv: boolean | undefined;
55
- jsxFactory: string | undefined;
56
- jsxFragment: string | undefined;
57
- tsconfig: string | undefined;
58
- isWorkersSite: boolean;
59
- minify: boolean | undefined;
60
- nodeCompat: boolean | undefined;
61
- outDir: string | undefined;
62
- dryRun: boolean | undefined;
63
- noBundle: boolean | undefined;
64
- keepVars: boolean | undefined;
65
- logpush: boolean | undefined;
66
- };
67
-
68
- type RouteObject = ZoneIdRoute | ZoneNameRoute | CustomDomainRoute;
69
-
70
- export type CustomDomain = {
71
- id: string;
72
- zone_id: string;
73
- zone_name: string;
74
- hostname: string;
75
- service: string;
76
- environment: string;
77
- };
78
- type UpdatedCustomDomain = CustomDomain & { modified: boolean };
79
- type ConflictingCustomDomain = CustomDomain & {
80
- external_dns_record_id?: string;
81
- external_cert_id?: string;
82
- };
83
-
84
- export type CustomDomainChangeset = {
85
- added: CustomDomain[];
86
- removed: CustomDomain[];
87
- updated: UpdatedCustomDomain[];
88
- conflicting: ConflictingCustomDomain[];
89
- };
90
-
91
- function sleep(ms: number) {
92
- return new Promise((resolve) => setTimeout(resolve, ms));
93
- }
94
-
95
- const scriptStartupErrorRegex = /startup/i;
96
-
97
- function errIsScriptSize(err: unknown): err is { code: 10027 } {
98
- if (!err) return false;
99
-
100
- // 10027 = workers.api.error.script_too_large
101
- if ((err as { code: number }).code === 10027) {
102
- return true;
103
- }
104
-
105
- return false;
106
- }
107
-
108
- function errIsStartupErr(err: unknown): err is ParseError & { code: 10021 } {
109
- if (!err) return false;
110
-
111
- // 10021 = validation error
112
- // no explicit error code for more granular errors than "invalid script"
113
- // but the error will contain a string error message directly from the
114
- // validator.
115
- // the error always SHOULD look like "Script startup exceeded CPU limit."
116
- // (or the less likely "Script startup exceeded memory limits.")
117
- if (
118
- (err as { code: number }).code === 10021 &&
119
- err instanceof ParseError &&
120
- scriptStartupErrorRegex.test(err.notes[0]?.text)
121
- ) {
122
- return true;
123
- }
124
-
125
- return false;
126
- }
127
-
128
- function renderRoute(route: Route): string {
129
- let result = "";
130
- if (typeof route === "string") {
131
- result = route;
132
- } else {
133
- result = route.pattern;
134
- const isCustomDomain = Boolean(
135
- "custom_domain" in route && route.custom_domain
136
- );
137
- if (isCustomDomain && "zone_id" in route) {
138
- result += ` (custom domain - zone id: ${route.zone_id})`;
139
- } else if (isCustomDomain && "zone_name" in route) {
140
- result += ` (custom domain - zone name: ${route.zone_name})`;
141
- } else if (isCustomDomain) {
142
- result += ` (custom domain)`;
143
- } else if ("zone_id" in route) {
144
- result += ` (zone id: ${route.zone_id})`;
145
- } else if ("zone_name" in route) {
146
- result += ` (zone name: ${route.zone_name})`;
147
- }
148
- }
149
- return result;
150
- }
151
-
152
- // publishing to custom domains involves a few more steps than just updating
153
- // the routing table, and thus the api implementing it is fairly defensive -
154
- // it will error eagerly on conflicts against existing domains or existing
155
- // managed DNS records
156
-
157
- // however, you can pass params to override the errors. to know if we should
158
- // override the current state, we generate a "changeset" of required actions
159
- // to get to the state we want (specified by the list of custom domains). the
160
- // changeset returns an "updated" collection (existing custom domains
161
- // connected to other scripts) and a "conflicting" collection (the requested
162
- // custom domains that have a managed, conflicting DNS record preventing the
163
- // host's use as a custom domain). with this information, we can prompt to
164
- // the user what will occur if we create the custom domains requested, and
165
- // add the override param if they confirm the action
166
- //
167
- // if a user does not confirm that they want to override, we skip publishing
168
- // to these custom domains, but continue on through the rest of the
169
- // publish stage
170
- async function publishCustomDomains(
171
- workerUrl: string,
172
- accountId: string,
173
- domains: Array<RouteObject>
174
- ): Promise<string[]> {
175
- const config = {
176
- override_scope: true,
177
- override_existing_origin: false,
178
- override_existing_dns_record: false,
179
- };
180
- const origins = domains.map((domainRoute) => {
181
- return {
182
- hostname: domainRoute.pattern,
183
- zone_id: "zone_id" in domainRoute ? domainRoute.zone_id : undefined,
184
- zone_name: "zone_name" in domainRoute ? domainRoute.zone_name : undefined,
185
- };
186
- });
187
-
188
- const fail = () => {
189
- return [
190
- domains.length > 1
191
- ? `Publishing to ${domains.length} Custom Domains was skipped, fix conflicts and try again`
192
- : `Publishing to Custom Domain "${domains[0].pattern}" was skipped, fix conflict and try again`,
193
- ];
194
- };
195
-
196
- if (!process.stdout.isTTY) {
197
- // running in non-interactive mode.
198
- // existing origins / dns records are not indicative of errors,
199
- // so we aggressively update rather than aggressively fail
200
- config.override_existing_origin = true;
201
- config.override_existing_dns_record = true;
202
- } else {
203
- // get a changeset for operations required to achieve a state with the requested domains
204
- const changeset = await fetchResult<CustomDomainChangeset>(
205
- `${workerUrl}/domains/changeset?replace_state=true`,
206
- {
207
- method: "POST",
208
- body: JSON.stringify(origins),
209
- headers: {
210
- "Content-Type": "application/json",
211
- },
212
- }
213
- );
214
-
215
- const updatesRequired = changeset.updated.filter(
216
- (domain) => domain.modified
217
- );
218
- if (updatesRequired.length > 0) {
219
- // find out which scripts the conflict domains are already attached to
220
- // so we can provide that in the confirmation prompt
221
- const existing = await Promise.all(
222
- updatesRequired.map((domain) =>
223
- fetchResult<CustomDomain>(
224
- `/accounts/${accountId}/workers/domains/records/${domain.id}`
225
- )
226
- )
227
- );
228
- const existingRendered = existing
229
- .map(
230
- (domain) =>
231
- `\t• ${domain.hostname} (used as a domain for "${domain.service}")`
232
- )
233
- .join("\n");
234
- const message = `Custom Domains already exist for these domains:
235
- ${existingRendered}
236
- Update them to point to this script instead?`;
237
- if (!(await confirm(message))) return fail();
238
- config.override_existing_origin = true;
239
- }
240
-
241
- if (changeset.conflicting.length > 0) {
242
- const conflicitingRendered = changeset.conflicting
243
- .map((domain) => `\t• ${domain.hostname}`)
244
- .join("\n");
245
- const message = `You already have DNS records that conflict for these Custom Domains:
246
- ${conflicitingRendered}
247
- Update them to point to this script instead?`;
248
- if (!(await confirm(message))) return fail();
249
- config.override_existing_dns_record = true;
250
- }
251
- }
252
-
253
- // publish to domains
254
- await fetchResult(`${workerUrl}/domains/records`, {
255
- method: "PUT",
256
- body: JSON.stringify({ ...config, origins }),
257
- headers: {
258
- "Content-Type": "application/json",
259
- },
260
- });
261
-
262
- return domains.map((domain) => renderRoute(domain));
263
- }
264
-
265
- export default async function publish(props: Props): Promise<void> {
266
- // TODO: warn if git/hg has uncommitted changes
267
- const { config, accountId, name } = props;
268
- if (accountId && name) {
269
- try {
270
- const serviceMetaData = await fetchResult(
271
- `/accounts/${accountId}/workers/services/${name}`
272
- );
273
- const { default_environment } = serviceMetaData as {
274
- default_environment: {
275
- script: { last_deployed_from: "dash" | "wrangler" | "api" };
276
- };
277
- };
278
-
279
- if (default_environment.script.last_deployed_from === "dash") {
280
- logger.warn(
281
- `You are about to publish a Workers Service that was last published via the Cloudflare Dashboard.\nEdits that have been made via the dashboard will be overridden by your local code and config.`
282
- );
283
- if (!(await confirm("Would you like to continue?"))) {
284
- return;
285
- }
286
- }
287
- } catch (e) {
288
- // code: 10090, message: workers.api.error.service_not_found
289
- // is thrown from the above fetchResult on the first publish of a Worker
290
- if ((e as { code?: number }).code !== 10090) {
291
- logger.error(e);
292
- }
293
- }
294
- }
295
-
296
- if (!(props.compatibilityDate || config.compatibility_date)) {
297
- const compatibilityDateStr = `${new Date().getFullYear()}-${(
298
- new Date().getMonth() +
299
- 1 +
300
- ""
301
- ).padStart(2, "0")}-${(new Date().getDate() + "").padStart(2, "0")}`;
302
-
303
- throw new Error(`A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
304
- \`\`\`
305
- compatibility_date = "${compatibilityDateStr}"
306
- \`\`\`
307
- Or you could pass it in your terminal as \`--compatibility-date ${compatibilityDateStr}\`
308
- See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.`);
309
- }
310
-
311
- const triggers = props.triggers || config.triggers?.crons;
312
- const routes =
313
- props.routes ?? config.routes ?? (config.route ? [config.route] : []) ?? [];
314
- const routesOnly: Array<Route> = [];
315
- const customDomainsOnly: Array<RouteObject> = [];
316
- for (const route of routes) {
317
- if (typeof route !== "string" && route.custom_domain) {
318
- if (route.pattern.includes("*")) {
319
- throw new Error(
320
- `Cannot use "${route.pattern}" as a Custom Domain; wildcard operators (*) are not allowed`
321
- );
322
- }
323
- if (route.pattern.includes("/")) {
324
- throw new Error(
325
- `Cannot use "${route.pattern}" as a Custom Domain; paths are not allowed`
326
- );
327
- }
328
- customDomainsOnly.push(route);
329
- } else {
330
- routesOnly.push(route);
331
- }
332
- }
333
-
334
- // deployToWorkersDev defaults to true only if there aren't any routes defined
335
- const deployToWorkersDev = config.workers_dev ?? routes.length === 0;
336
-
337
- const jsxFactory = props.jsxFactory || config.jsx_factory;
338
- const jsxFragment = props.jsxFragment || config.jsx_fragment;
339
- const keepVars = props.keepVars || config.keep_vars;
340
-
341
- const minify = props.minify ?? config.minify;
342
-
343
- const legacyNodeCompat = props.nodeCompat ?? config.node_compat;
344
- if (legacyNodeCompat) {
345
- logger.warn(
346
- "Enabling Node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
347
- );
348
- }
349
-
350
- const compatibilityFlags =
351
- props.compatibilityFlags ?? config.compatibility_flags;
352
- const nodejsCompat = compatibilityFlags.includes("nodejs_compat");
353
- assert(
354
- !(legacyNodeCompat && nodejsCompat),
355
- "The `nodejs_compat` compatibility flag cannot be used in conjunction with the legacy `--node-compat` flag. If you want to use the Workers runtime Node.js compatibility features, please remove the `--node-compat` argument from your CLI command or `node_compat = true` from your config file."
356
- );
357
-
358
- // Warn if user tries minify or node-compat with no-bundle
359
- if (props.noBundle && minify) {
360
- logger.warn(
361
- "`--minify` and `--no-bundle` can't be used together. If you want to minify your Worker and disable Wrangler's bundling, please minify as part of your own bundling process."
362
- );
363
- }
364
-
365
- if (props.noBundle && legacyNodeCompat) {
366
- logger.warn(
367
- "`--node-compat` and `--no-bundle` can't be used together. If you want to polyfill Node.js built-ins and disable Wrangler's bundling, please polyfill as part of your own bundling process."
368
- );
369
- }
370
-
371
- const scriptName = props.name;
372
- assert(
373
- scriptName,
374
- 'You need to provide a name when publishing a worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
375
- );
376
-
377
- assert(
378
- !config.site || config.site.bucket,
379
- "A [site] definition requires a `bucket` field with a path to the site's assets directory."
380
- );
381
-
382
- if (props.outDir) {
383
- // we're using a custom output directory,
384
- // so let's first ensure it exists
385
- mkdirSync(props.outDir, { recursive: true });
386
- // add a README
387
- const readmePath = path.join(props.outDir, "README.md");
388
- writeFileSync(
389
- readmePath,
390
- `This folder contains the built output assets for the worker "${scriptName}" generated at ${new Date().toISOString()}.`
391
- );
392
- }
393
-
394
- const destination = props.outDir ?? (await tmp.dir({ unsafeCleanup: true }));
395
- const envName = props.env ?? "production";
396
-
397
- const start = Date.now();
398
- const notProd = Boolean(!props.legacyEnv && props.env);
399
- const workerName = notProd ? `${scriptName} (${envName})` : scriptName;
400
- const workerUrl = notProd
401
- ? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
402
- : `/accounts/${accountId}/workers/scripts/${scriptName}`;
403
-
404
- let available_on_subdomain: boolean | undefined = undefined; // we'll set this later
405
- let deploymentId: string | null = null;
406
-
407
- const { format } = props.entry;
408
-
409
- if (
410
- !props.isWorkersSite &&
411
- Boolean(props.assetPaths) &&
412
- format === "service-worker"
413
- ) {
414
- throw new Error(
415
- "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/"
416
- );
417
- }
418
-
419
- if (config.wasm_modules && format === "modules") {
420
- throw new Error(
421
- "You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"
422
- );
423
- }
424
-
425
- if (config.text_blobs && format === "modules") {
426
- throw new Error(
427
- "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"
428
- );
429
- }
430
-
431
- if (config.data_blobs && format === "modules") {
432
- throw new Error(
433
- "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"
434
- );
435
- }
436
- try {
437
- if (props.noBundle) {
438
- // if we're not building, let's just copy the entry to the destination directory
439
- const destinationDir =
440
- typeof destination === "string" ? destination : destination.path;
441
- mkdirSync(destinationDir, { recursive: true });
442
- writeFileSync(
443
- path.join(destinationDir, path.basename(props.entry.file)),
444
- readFileSync(props.entry.file, "utf-8")
445
- );
446
- }
447
-
448
- // If we are using d1 bindings, and are not bundling the worker
449
- // we should error here as the d1 shim won't be added
450
- const betaD1Shims = identifyD1BindingsAsBeta(config.d1_databases);
451
- if (
452
- Array.isArray(betaD1Shims) &&
453
- betaD1Shims.length > 0 &&
454
- props.noBundle
455
- ) {
456
- throw new Error(
457
- "While in beta, you cannot use D1 bindings without bundling your worker. Please remove `no_bundle` from your wrangler.toml file or remove the `--no-bundle` flag to access D1 bindings."
458
- );
459
- }
460
-
461
- const {
462
- modules,
463
- dependencies,
464
- resolvedEntryPointPath,
465
- bundleType,
466
- }: Awaited<ReturnType<typeof bundleWorker>> = props.noBundle
467
- ? await traverseModuleGraph(props.entry, props.rules)
468
- : await bundleWorker(
469
- props.entry,
470
- typeof destination === "string" ? destination : destination.path,
471
- {
472
- serveAssetsFromWorker:
473
- !props.isWorkersSite && Boolean(props.assetPaths),
474
- betaD1Shims: identifyD1BindingsAsBeta(config.d1_databases)?.map(
475
- (db) => db.binding
476
- ),
477
- doBindings: config.durable_objects.bindings,
478
- jsxFactory,
479
- jsxFragment,
480
- rules: props.rules,
481
- tsconfig: props.tsconfig ?? config.tsconfig,
482
- minify,
483
- legacyNodeCompat,
484
- nodejsCompat,
485
- define: { ...config.define, ...props.defines },
486
- checkFetch: false,
487
- assets: config.assets && {
488
- ...config.assets,
489
- // enable the cache when publishing
490
- bypassCache: false,
491
- },
492
- services: config.services,
493
- // We don't set workerDefinitions here,
494
- // because we don't want to apply the dev-time
495
- // facades on top of it
496
- workerDefinitions: undefined,
497
- firstPartyWorkerDevFacade: false,
498
- // We want to know if the build is for development or publishing
499
- // This could potentially cause issues as we no longer have identical behaviour between dev and publish?
500
- targetConsumer: "publish",
501
- local: false,
502
- experimentalLocal: false,
503
- }
504
- );
505
-
506
- const content = readFileSync(resolvedEntryPointPath, {
507
- encoding: "utf-8",
508
- });
509
-
510
- // durable object migrations
511
- const migrations = !props.dryRun
512
- ? await getMigrationsToUpload(scriptName, {
513
- accountId,
514
- config,
515
- legacyEnv: props.legacyEnv,
516
- env: props.env,
517
- })
518
- : undefined;
519
-
520
- const assets = await syncAssets(
521
- accountId,
522
- // When we're using the newer service environments, we wouldn't
523
- // have added the env name on to the script name. However, we must
524
- // include it in the kv namespace name regardless (since there's no
525
- // concept of service environments for kv namespaces yet).
526
- scriptName + (!props.legacyEnv && props.env ? `-${props.env}` : ""),
527
- props.assetPaths,
528
- false,
529
- props.dryRun
530
- );
531
-
532
- const bindings: CfWorkerInit["bindings"] = {
533
- kv_namespaces: (config.kv_namespaces || []).concat(
534
- assets.namespace
535
- ? { binding: "__STATIC_CONTENT", id: assets.namespace }
536
- : []
537
- ),
538
- send_email: config.send_email,
539
- vars: { ...config.vars, ...props.vars },
540
- wasm_modules: config.wasm_modules,
541
- browser: config.browser,
542
- text_blobs: {
543
- ...config.text_blobs,
544
- ...(assets.manifest &&
545
- format === "service-worker" && {
546
- __STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
547
- }),
548
- },
549
- data_blobs: config.data_blobs,
550
- durable_objects: config.durable_objects,
551
- queues: config.queues.producers?.map((producer) => {
552
- return { binding: producer.binding, queue_name: producer.queue };
553
- }),
554
- r2_buckets: config.r2_buckets,
555
- d1_databases: identifyD1BindingsAsBeta(config.d1_databases),
556
- constellation: config.constellation,
557
- services: config.services,
558
- analytics_engine_datasets: config.analytics_engine_datasets,
559
- dispatch_namespaces: config.dispatch_namespaces,
560
- mtls_certificates: config.mtls_certificates,
561
- logfwdr: config.logfwdr,
562
- unsafe: {
563
- bindings: config.unsafe.bindings,
564
- metadata: config.unsafe.metadata,
565
- },
566
- };
567
-
568
- if (assets.manifest) {
569
- modules.push({
570
- name: "__STATIC_CONTENT_MANIFEST",
571
- content: JSON.stringify(assets.manifest),
572
- type: "text",
573
- });
574
- }
575
-
576
- // The upload API only accepts an empty string or no specified placement for the "off" mode.
577
- const placement: CfPlacement | undefined =
578
- config.placement?.mode === "smart" ? { mode: "smart" } : undefined;
579
-
580
- const worker: CfWorkerInit = {
581
- name: scriptName,
582
- main: {
583
- name: path.basename(resolvedEntryPointPath),
584
- content: content,
585
- type: bundleType,
586
- },
587
- bindings,
588
- migrations,
589
- modules,
590
- compatibility_date: props.compatibilityDate ?? config.compatibility_date,
591
- compatibility_flags: compatibilityFlags,
592
- usage_model: config.usage_model,
593
- keepVars,
594
- logpush: props.logpush !== undefined ? props.logpush : config.logpush,
595
- placement,
596
- tail_consumers: config.tail_consumers,
597
- };
598
-
599
- // As this is not deterministic for testing, we detect if in a jest environment and run asynchronously
600
- // We do not care about the timing outside of testing
601
- const bundleSizePromise = printBundleSize(
602
- { name: path.basename(resolvedEntryPointPath), content: content },
603
- modules
604
- );
605
- if (process.env.JEST_WORKER_ID !== undefined) await bundleSizePromise;
606
- else void bundleSizePromise;
607
-
608
- const withoutStaticAssets = {
609
- ...bindings,
610
- kv_namespaces: config.kv_namespaces,
611
- text_blobs: config.text_blobs,
612
- };
613
-
614
- // mask anything that was overridden in cli args
615
- // so that we don't log potential secrets into the terminal
616
- const maskedVars = { ...withoutStaticAssets.vars };
617
- for (const key of Object.keys(maskedVars)) {
618
- if (maskedVars[key] !== config.vars[key]) {
619
- // This means it was overridden in cli args
620
- // so let's mask it
621
- maskedVars[key] = "(hidden)";
622
- }
623
- }
624
-
625
- printBindings({ ...withoutStaticAssets, vars: maskedVars });
626
-
627
- if (!props.dryRun) {
628
- await ensureQueuesExist(config);
629
-
630
- // Upload the script so it has time to propagate.
631
- // We can also now tell whether available_on_subdomain is set
632
- try {
633
- const result = await fetchResult<{
634
- available_on_subdomain: boolean;
635
- id: string | null;
636
- etag: string | null;
637
- pipeline_hash: string | null;
638
- deployment_id: string | null;
639
- }>(
640
- workerUrl,
641
- {
642
- method: "PUT",
643
- body: createWorkerUploadForm(worker),
644
- headers: await getMetricsUsageHeaders(config.send_metrics),
645
- },
646
- new URLSearchParams({
647
- include_subdomain_availability: "true",
648
- // pass excludeScript so the whole body of the
649
- // script doesn't get included in the response
650
- excludeScript: "true",
651
- })
652
- );
653
-
654
- available_on_subdomain = result.available_on_subdomain;
655
- deploymentId = addHyphens(result.deployment_id) ?? result.deployment_id;
656
-
657
- if (config.first_party_worker) {
658
- // Print some useful information returned after publishing
659
- // Not all fields will be populated for every worker
660
- // These fields are likely to be scraped by tools, so do not rename
661
- if (result.id) logger.log("Worker ID: ", result.id);
662
- if (result.etag) logger.log("Worker ETag: ", result.etag);
663
- if (result.pipeline_hash)
664
- logger.log("Worker PipelineHash: ", result.pipeline_hash);
665
- }
666
- } catch (err) {
667
- helpIfErrorIsSizeOrScriptStartup(err, dependencies);
668
- throw err;
669
- }
670
- }
671
- } finally {
672
- if (typeof destination !== "string") {
673
- // this means we're using a temp dir,
674
- // so let's clean up before we proceed
675
- await destination.cleanup();
676
- }
677
- }
678
-
679
- if (props.dryRun) {
680
- logger.log(`--dry-run: exiting now.`);
681
- return;
682
- }
683
- assert(accountId, "Missing accountId");
684
-
685
- const uploadMs = Date.now() - start;
686
- const deployments: Promise<string[]>[] = [];
687
-
688
- if (deployToWorkersDev) {
689
- // Deploy to a subdomain of `workers.dev`
690
- const userSubdomain = await getWorkersDevSubdomain(accountId);
691
- const scriptURL =
692
- props.legacyEnv || !props.env
693
- ? `${scriptName}.${userSubdomain}.workers.dev`
694
- : `${envName}.${scriptName}.${userSubdomain}.workers.dev`;
695
- if (!available_on_subdomain) {
696
- // Enable the `workers.dev` subdomain.
697
- deployments.push(
698
- fetchResult(`${workerUrl}/subdomain`, {
699
- method: "POST",
700
- body: JSON.stringify({ enabled: true }),
701
- headers: {
702
- "Content-Type": "application/json",
703
- },
704
- })
705
- .then(() => [scriptURL])
706
- // Add a delay when the subdomain is first created.
707
- // This is to prevent an issue where a negative cache-hit
708
- // causes the subdomain to be unavailable for 30 seconds.
709
- // This is a temporary measure until we fix this on the edge.
710
- .then(async (url) => {
711
- await sleep(3000);
712
- return url;
713
- })
714
- );
715
- } else {
716
- deployments.push(Promise.resolve([scriptURL]));
717
- }
718
- } else {
719
- if (available_on_subdomain) {
720
- // Disable the workers.dev deployment
721
- await fetchResult(`${workerUrl}/subdomain`, {
722
- method: "POST",
723
- body: JSON.stringify({ enabled: false }),
724
- headers: {
725
- "Content-Type": "application/json",
726
- },
727
- });
728
- } else if (routes.length !== 0) {
729
- // if you get to this point it's because
730
- // you're trying to deploy a worker to a custom
731
- // domain that's already bound to another worker.
732
- // so this thing is about finding workers that have
733
- // bindings to the routes you're trying to deploy to.
734
- //
735
- // the logic is kinda similar (read: duplicated) from publishRoutesFallback,
736
- // except here we know we have a good API token or whatever so we don't need
737
- // to bother with all the error handling tomfoolery.
738
- const routesWithOtherBindings: Record<string, string[]> = {};
739
- for (const route of routes) {
740
- const zone = await getZoneForRoute(route);
741
- if (!zone) {
742
- continue;
743
- }
744
-
745
- const routePattern = typeof route === "string" ? route : route.pattern;
746
- const routesInZone = await fetchListResult<{
747
- pattern: string;
748
- script: string;
749
- }>(`/zones/${zone.id}/workers/routes`);
750
-
751
- routesInZone.forEach(({ script, pattern }) => {
752
- if (pattern === routePattern && script !== scriptName) {
753
- if (!(script in routesWithOtherBindings)) {
754
- routesWithOtherBindings[script] = [];
755
- }
756
-
757
- routesWithOtherBindings[script].push(pattern);
758
- }
759
- });
760
- }
761
-
762
- if (Object.keys(routesWithOtherBindings).length > 0) {
763
- let errorMessage =
764
- "Can't publish a worker to routes that are assigned to another worker.\n";
765
-
766
- for (const worker in routesWithOtherBindings) {
767
- const assignedRoutes = routesWithOtherBindings[worker];
768
- errorMessage += `"${worker}" is already assigned to routes:\n${assignedRoutes.map(
769
- (r) => ` - ${chalk.underline(r)}\n`
770
- )}`;
771
- }
772
-
773
- const resolution =
774
- "Unassign other workers from the routes you want to publish to, and then try again.";
775
- const dashLink = `Visit ${chalk.blue(
776
- chalk.underline(
777
- `https://dash.cloudflare.com/${accountId}/workers/overview`
778
- )
779
- )} to unassign a worker from a route.`;
780
-
781
- throw new Error(`${errorMessage}\n${resolution}\n${dashLink}`);
782
- }
783
- }
784
- }
785
-
786
- logger.log("Uploaded", workerName, formatTime(uploadMs));
787
-
788
- // Update routing table for the script.
789
- if (routesOnly.length > 0) {
790
- deployments.push(
791
- publishRoutes(routesOnly, { workerUrl, scriptName, notProd }).then(() => {
792
- if (routesOnly.length > 10) {
793
- return routesOnly
794
- .slice(0, 9)
795
- .map((route) => renderRoute(route))
796
- .concat([`...and ${routesOnly.length - 10} more routes`]);
797
- }
798
- return routesOnly.map((route) => renderRoute(route));
799
- })
800
- );
801
- }
802
-
803
- // Update custom domains for the script
804
- if (customDomainsOnly.length > 0) {
805
- deployments.push(
806
- publishCustomDomains(workerUrl, accountId, customDomainsOnly)
807
- );
808
- }
809
-
810
- // Configure any schedules for the script.
811
- // TODO: rename this to `schedules`?
812
- if (triggers && triggers.length) {
813
- deployments.push(
814
- fetchResult(`${workerUrl}/schedules`, {
815
- // Note: PUT will override previous schedules on this script.
816
- method: "PUT",
817
- body: JSON.stringify(triggers.map((cron) => ({ cron }))),
818
- headers: {
819
- "Content-Type": "application/json",
820
- },
821
- }).then(() => triggers.map((trigger) => `schedule: ${trigger}`))
822
- );
823
- }
824
-
825
- if (config.queues.consumers && config.queues.consumers.length) {
826
- deployments.push(...updateQueueConsumers(config));
827
- }
828
-
829
- const targets = await Promise.all(deployments);
830
- const deployMs = Date.now() - start - uploadMs;
831
-
832
- if (deployments.length > 0) {
833
- logger.log("Published", workerName, formatTime(deployMs));
834
- for (const target of targets.flat()) {
835
- // Append protocol only on workers.dev domains
836
- logger.log(
837
- " ",
838
- (target.endsWith("workers.dev") ? "https://" : "") + target
839
- );
840
- }
841
- } else {
842
- logger.log("No publish targets for", workerName, formatTime(deployMs));
843
- }
844
-
845
- logger.log("Current Deployment ID:", deploymentId);
846
- }
847
-
848
- export function helpIfErrorIsSizeOrScriptStartup(
849
- err: unknown,
850
- dependencies: { [path: string]: { bytesInOutput: number } }
851
- ) {
852
- if (errIsScriptSize(err)) {
853
- printOffendingDependencies(dependencies);
854
- } else if (errIsStartupErr(err)) {
855
- const youFailed =
856
- "Your Worker failed validation because it exceeded startup limits.";
857
- const heresWhy =
858
- "To ensure fast responses, we place constraints on Worker startup -- like how much CPU it can use, or how long it can take.";
859
- const heresTheProblem =
860
- "Your Worker failed validation, which means it hit one of these startup limits.";
861
- const heresTheSolution =
862
- "Try reducing the amount of work done during startup (outside the event handler), either by removing code or relocating it inside the event handler.";
863
- logger.warn(
864
- [youFailed, heresWhy, heresTheProblem, heresTheSolution].join("\n")
865
- );
866
- }
867
- }
868
-
869
- function formatTime(duration: number) {
870
- return `(${(duration / 1000).toFixed(2)} sec)`;
871
- }
872
-
873
- /**
874
- * Associate the newly deployed Worker with the given routes.
875
- */
876
- async function publishRoutes(
877
- routes: Route[],
878
- {
879
- workerUrl,
880
- scriptName,
881
- notProd,
882
- }: { workerUrl: string; scriptName: string; notProd: boolean }
883
- ): Promise<string[]> {
884
- try {
885
- return await fetchResult(`${workerUrl}/routes`, {
886
- // Note: PUT will delete previous routes on this script.
887
- method: "PUT",
888
- body: JSON.stringify(
889
- routes.map((route) =>
890
- typeof route !== "object" ? { pattern: route } : route
891
- )
892
- ),
893
- headers: {
894
- "Content-Type": "application/json",
895
- },
896
- });
897
- } catch (e) {
898
- if (isAuthenticationError(e)) {
899
- // An authentication error is probably due to a known issue,
900
- // where the user is logged in via an API token that does not have "All Zones".
901
- return await publishRoutesFallback(routes, { scriptName, notProd });
902
- } else {
903
- throw e;
904
- }
905
- }
906
- }
907
-
908
- /**
909
- * Try updating routes for the Worker using a less optimal zone-based API.
910
- *
911
- * Compute match zones to the routes, then for each route attempt to connect it to the Worker via the zone.
912
- */
913
- async function publishRoutesFallback(
914
- routes: Route[],
915
- { scriptName, notProd }: { scriptName: string; notProd: boolean }
916
- ) {
917
- if (notProd) {
918
- throw new Error(
919
- "Service environments combined with an API token that doesn't have 'All Zones' permissions is not supported.\n" +
920
- "Either turn off service environments by setting `legacy_env = true`, creating an API token with 'All Zones' permissions, or logging in via OAuth"
921
- );
922
- }
923
- logger.warn(
924
- "The current authentication token does not have 'All Zones' permissions.\n" +
925
- "Falling back to using the zone-based API endpoint to update each route individually.\n" +
926
- "Note that there is no access to routes associated with zones that the API token does not have permission for.\n" +
927
- "Existing routes for this Worker in such zones will not be deleted."
928
- );
929
-
930
- const deployedRoutes: string[] = [];
931
-
932
- // Collect the routes (and their zones) that will be deployed.
933
- const activeZones = new Map<string, string>();
934
- const routesToDeploy = new Map<string, string>();
935
- for (const route of routes) {
936
- const zone = await getZoneForRoute(route);
937
- if (zone) {
938
- activeZones.set(zone.id, zone.host);
939
- routesToDeploy.set(
940
- typeof route === "string" ? route : route.pattern,
941
- zone.id
942
- );
943
- }
944
- }
945
-
946
- // Collect the routes that are already deployed.
947
- const allRoutes = new Map<string, string>();
948
- const alreadyDeployedRoutes = new Set<string>();
949
- for (const [zone, host] of activeZones) {
950
- try {
951
- for (const { pattern, script } of await fetchListResult<{
952
- pattern: string;
953
- script: string;
954
- }>(`/zones/${zone}/workers/routes`)) {
955
- allRoutes.set(pattern, script);
956
- if (script === scriptName) {
957
- alreadyDeployedRoutes.add(pattern);
958
- }
959
- }
960
- } catch (e) {
961
- if (isAuthenticationError(e)) {
962
- e.notes.push({
963
- text: `This could be because the API token being used does not have permission to access the zone "${host}" (${zone}).`,
964
- });
965
- }
966
- throw e;
967
- }
968
- }
969
-
970
- // Deploy each route that is not already deployed.
971
- for (const [routePattern, zoneId] of routesToDeploy.entries()) {
972
- if (allRoutes.has(routePattern)) {
973
- const knownScript = allRoutes.get(routePattern);
974
- if (knownScript === scriptName) {
975
- // This route is already associated with this worker, so no need to hit the API.
976
- alreadyDeployedRoutes.delete(routePattern);
977
- continue;
978
- } else {
979
- throw new Error(
980
- `The route with pattern "${routePattern}" is already associated with another worker called "${knownScript}".`
981
- );
982
- }
983
- }
984
-
985
- const { pattern } = await fetchResult<{ pattern: string }>(
986
- `/zones/${zoneId}/workers/routes`,
987
- {
988
- method: "POST",
989
- body: JSON.stringify({
990
- pattern: routePattern,
991
- script: scriptName,
992
- }),
993
- headers: {
994
- "Content-Type": "application/json",
995
- },
996
- }
997
- );
998
-
999
- deployedRoutes.push(pattern);
1000
- }
1001
-
1002
- if (alreadyDeployedRoutes.size) {
1003
- logger.warn(
1004
- "Previously deployed routes:\n" +
1005
- "The following routes were already associated with this worker, and have not been deleted:\n" +
1006
- [...alreadyDeployedRoutes.values()].map((route) => ` - "${route}"\n`) +
1007
- "If these routes are not wanted then you can remove them in the dashboard."
1008
- );
1009
- }
1010
-
1011
- return deployedRoutes;
1012
- }
1013
-
1014
- function isAuthenticationError(e: unknown): e is ParseError {
1015
- return e instanceof ParseError && (e as { code?: number }).code === 10000;
1016
- }
1017
-
1018
- async function ensureQueuesExist(config: Config) {
1019
- const producers = (config.queues.producers || []).map(
1020
- (producer) => producer.queue
1021
- );
1022
- const consumers = (config.queues.consumers || []).map(
1023
- (consumer) => consumer.queue
1024
- );
1025
-
1026
- const queueNames = producers.concat(consumers);
1027
- for (const queue of queueNames) {
1028
- try {
1029
- await getQueue(config, queue);
1030
- } catch (err) {
1031
- const queueErr = err as FetchError;
1032
- if (queueErr.code === 11000) {
1033
- // queue_not_found
1034
- throw new Error(
1035
- `Queue "${queue}" does not exist. To create it, run: wrangler queues create ${queue}`
1036
- );
1037
- }
1038
- throw err;
1039
- }
1040
- }
1041
- }
1042
-
1043
- function updateQueueConsumers(config: Config): Promise<string[]>[] {
1044
- const consumers = config.queues.consumers || [];
1045
- return consumers.map((consumer) => {
1046
- const body: PutConsumerBody = {
1047
- dead_letter_queue: consumer.dead_letter_queue,
1048
- settings: {
1049
- batch_size: consumer.max_batch_size,
1050
- max_retries: consumer.max_retries,
1051
- max_wait_time_ms: consumer.max_batch_timeout
1052
- ? 1000 * consumer.max_batch_timeout
1053
- : undefined,
1054
- max_concurrency: consumer.max_concurrency,
1055
- },
1056
- };
1057
-
1058
- if (config.name === undefined) {
1059
- // TODO: how can we reliably get the current script name?
1060
- throw new Error("Script name is required to update queue consumers");
1061
- }
1062
- const scriptName = config.name;
1063
- const envName = undefined; // TODO: script environment for wrangler publish?
1064
- return putConsumer(config, consumer.queue, scriptName, envName, body).then(
1065
- () => [`Consumer for ${consumer.queue}`]
1066
- );
1067
- });
1068
- }