wrangler 2.20.0 → 3.0.1

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 +6 -14
  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 +149 -75
  15. package/wrangler-dist/cli.js +60065 -64339
  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 -4517
  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 -528
  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 -957
  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 -989
  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 -321
  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 -77
  125. package/src/api/pages/index.ts +0 -5
  126. package/src/api/pages/publish.tsx +0 -371
  127. package/src/bundle-reporter.ts +0 -68
  128. package/src/bundle.ts +0 -929
  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 -625
  136. package/src/config/index.ts +0 -403
  137. package/src/config/validation-helpers.ts +0 -597
  138. package/src/config/validation.ts +0 -2369
  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 -363
  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 -605
  177. package/src/dev/get-local-persistence-path.ts +0 -31
  178. package/src/dev/local.tsx +0 -952
  179. package/src/dev/remote.tsx +0 -635
  180. package/src/dev/start-server.ts +0 -545
  181. package/src/dev/use-esbuild.ts +0 -215
  182. package/src/dev/validate-dev-props.ts +0 -40
  183. package/src/dev-registry.ts +0 -202
  184. package/src/dev.tsx +0 -934
  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 -344
  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 -1037
  200. package/src/inspect.ts +0 -883
  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 -423
  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 -734
  230. package/src/pages/errors.ts +0 -67
  231. package/src/pages/functions/buildPlugin.ts +0 -114
  232. package/src/pages/functions/buildWorker.ts +0 -350
  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 -1065
  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 -377
  275. package/src/selfsigned.d.ts +0 -29
  276. package/src/sites.ts +0 -484
  277. package/src/tail/createTail.ts +0 -415
  278. package/src/tail/filters.ts +0 -277
  279. package/src/tail/index.ts +0 -211
  280. package/src/tail/printing.ts +0 -132
  281. package/src/traverse-module-graph.ts +0 -54
  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 -279
  296. package/src/yargs-types.ts +0 -37
  297. package/src/zones.ts +0 -191
@@ -1,1065 +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
- text_blobs: {
542
- ...config.text_blobs,
543
- ...(assets.manifest &&
544
- format === "service-worker" && {
545
- __STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
546
- }),
547
- },
548
- data_blobs: config.data_blobs,
549
- durable_objects: config.durable_objects,
550
- queues: config.queues.producers?.map((producer) => {
551
- return { binding: producer.binding, queue_name: producer.queue };
552
- }),
553
- r2_buckets: config.r2_buckets,
554
- d1_databases: identifyD1BindingsAsBeta(config.d1_databases),
555
- services: config.services,
556
- analytics_engine_datasets: config.analytics_engine_datasets,
557
- dispatch_namespaces: config.dispatch_namespaces,
558
- mtls_certificates: config.mtls_certificates,
559
- logfwdr: config.logfwdr,
560
- unsafe: {
561
- bindings: config.unsafe.bindings,
562
- metadata: config.unsafe.metadata,
563
- },
564
- };
565
-
566
- if (assets.manifest) {
567
- modules.push({
568
- name: "__STATIC_CONTENT_MANIFEST",
569
- content: JSON.stringify(assets.manifest),
570
- type: "text",
571
- });
572
- }
573
-
574
- // The upload API only accepts an empty string or no specified placement for the "off" mode.
575
- const placement: CfPlacement | undefined =
576
- config.placement?.mode === "smart" ? { mode: "smart" } : undefined;
577
-
578
- const worker: CfWorkerInit = {
579
- name: scriptName,
580
- main: {
581
- name: path.basename(resolvedEntryPointPath),
582
- content: content,
583
- type: bundleType,
584
- },
585
- bindings,
586
- migrations,
587
- modules,
588
- compatibility_date: props.compatibilityDate ?? config.compatibility_date,
589
- compatibility_flags: compatibilityFlags,
590
- usage_model: config.usage_model,
591
- keepVars,
592
- logpush: props.logpush !== undefined ? props.logpush : config.logpush,
593
- placement,
594
- };
595
-
596
- // As this is not deterministic for testing, we detect if in a jest environment and run asynchronously
597
- // We do not care about the timing outside of testing
598
- const bundleSizePromise = printBundleSize(
599
- { name: path.basename(resolvedEntryPointPath), content: content },
600
- modules
601
- );
602
- if (process.env.JEST_WORKER_ID !== undefined) await bundleSizePromise;
603
- else void bundleSizePromise;
604
-
605
- const withoutStaticAssets = {
606
- ...bindings,
607
- kv_namespaces: config.kv_namespaces,
608
- text_blobs: config.text_blobs,
609
- };
610
-
611
- // mask anything that was overridden in cli args
612
- // so that we don't log potential secrets into the terminal
613
- const maskedVars = { ...withoutStaticAssets.vars };
614
- for (const key of Object.keys(maskedVars)) {
615
- if (maskedVars[key] !== config.vars[key]) {
616
- // This means it was overridden in cli args
617
- // so let's mask it
618
- maskedVars[key] = "(hidden)";
619
- }
620
- }
621
-
622
- printBindings({ ...withoutStaticAssets, vars: maskedVars });
623
-
624
- if (!props.dryRun) {
625
- await ensureQueuesExist(config);
626
-
627
- // Upload the script so it has time to propagate.
628
- // We can also now tell whether available_on_subdomain is set
629
- try {
630
- const result = await fetchResult<{
631
- available_on_subdomain: boolean;
632
- id: string | null;
633
- etag: string | null;
634
- pipeline_hash: string | null;
635
- deployment_id: string | null;
636
- }>(
637
- workerUrl,
638
- {
639
- method: "PUT",
640
- body: createWorkerUploadForm(worker),
641
- headers: await getMetricsUsageHeaders(config.send_metrics),
642
- },
643
- new URLSearchParams({
644
- include_subdomain_availability: "true",
645
- // pass excludeScript so the whole body of the
646
- // script doesn't get included in the response
647
- excludeScript: "true",
648
- })
649
- );
650
-
651
- available_on_subdomain = result.available_on_subdomain;
652
- deploymentId = addHyphens(result.deployment_id) ?? result.deployment_id;
653
-
654
- if (config.first_party_worker) {
655
- // Print some useful information returned after publishing
656
- // Not all fields will be populated for every worker
657
- // These fields are likely to be scraped by tools, so do not rename
658
- if (result.id) logger.log("Worker ID: ", result.id);
659
- if (result.etag) logger.log("Worker ETag: ", result.etag);
660
- if (result.pipeline_hash)
661
- logger.log("Worker PipelineHash: ", result.pipeline_hash);
662
- }
663
- } catch (err) {
664
- helpIfErrorIsSizeOrScriptStartup(err, dependencies);
665
- throw err;
666
- }
667
- }
668
- } finally {
669
- if (typeof destination !== "string") {
670
- // this means we're using a temp dir,
671
- // so let's clean up before we proceed
672
- await destination.cleanup();
673
- }
674
- }
675
-
676
- if (props.dryRun) {
677
- logger.log(`--dry-run: exiting now.`);
678
- return;
679
- }
680
- assert(accountId, "Missing accountId");
681
-
682
- const uploadMs = Date.now() - start;
683
- const deployments: Promise<string[]>[] = [];
684
-
685
- if (deployToWorkersDev) {
686
- // Deploy to a subdomain of `workers.dev`
687
- const userSubdomain = await getWorkersDevSubdomain(accountId);
688
- const scriptURL =
689
- props.legacyEnv || !props.env
690
- ? `${scriptName}.${userSubdomain}.workers.dev`
691
- : `${envName}.${scriptName}.${userSubdomain}.workers.dev`;
692
- if (!available_on_subdomain) {
693
- // Enable the `workers.dev` subdomain.
694
- deployments.push(
695
- fetchResult(`${workerUrl}/subdomain`, {
696
- method: "POST",
697
- body: JSON.stringify({ enabled: true }),
698
- headers: {
699
- "Content-Type": "application/json",
700
- },
701
- })
702
- .then(() => [scriptURL])
703
- // Add a delay when the subdomain is first created.
704
- // This is to prevent an issue where a negative cache-hit
705
- // causes the subdomain to be unavailable for 30 seconds.
706
- // This is a temporary measure until we fix this on the edge.
707
- .then(async (url) => {
708
- await sleep(3000);
709
- return url;
710
- })
711
- );
712
- } else {
713
- deployments.push(Promise.resolve([scriptURL]));
714
- }
715
- } else {
716
- if (available_on_subdomain) {
717
- // Disable the workers.dev deployment
718
- await fetchResult(`${workerUrl}/subdomain`, {
719
- method: "POST",
720
- body: JSON.stringify({ enabled: false }),
721
- headers: {
722
- "Content-Type": "application/json",
723
- },
724
- });
725
- } else if (routes.length !== 0) {
726
- // if you get to this point it's because
727
- // you're trying to deploy a worker to a custom
728
- // domain that's already bound to another worker.
729
- // so this thing is about finding workers that have
730
- // bindings to the routes you're trying to deploy to.
731
- //
732
- // the logic is kinda similar (read: duplicated) from publishRoutesFallback,
733
- // except here we know we have a good API token or whatever so we don't need
734
- // to bother with all the error handling tomfoolery.
735
- const routesWithOtherBindings: Record<string, string[]> = {};
736
- for (const route of routes) {
737
- const zone = await getZoneForRoute(route);
738
- if (!zone) {
739
- continue;
740
- }
741
-
742
- const routePattern = typeof route === "string" ? route : route.pattern;
743
- const routesInZone = await fetchListResult<{
744
- pattern: string;
745
- script: string;
746
- }>(`/zones/${zone.id}/workers/routes`);
747
-
748
- routesInZone.forEach(({ script, pattern }) => {
749
- if (pattern === routePattern && script !== scriptName) {
750
- if (!(script in routesWithOtherBindings)) {
751
- routesWithOtherBindings[script] = [];
752
- }
753
-
754
- routesWithOtherBindings[script].push(pattern);
755
- }
756
- });
757
- }
758
-
759
- if (Object.keys(routesWithOtherBindings).length > 0) {
760
- let errorMessage =
761
- "Can't publish a worker to routes that are assigned to another worker.\n";
762
-
763
- for (const worker in routesWithOtherBindings) {
764
- const assignedRoutes = routesWithOtherBindings[worker];
765
- errorMessage += `"${worker}" is already assigned to routes:\n${assignedRoutes.map(
766
- (r) => ` - ${chalk.underline(r)}\n`
767
- )}`;
768
- }
769
-
770
- const resolution =
771
- "Unassign other workers from the routes you want to publish to, and then try again.";
772
- const dashLink = `Visit ${chalk.blue(
773
- chalk.underline(
774
- `https://dash.cloudflare.com/${accountId}/workers/overview`
775
- )
776
- )} to unassign a worker from a route.`;
777
-
778
- throw new Error(`${errorMessage}\n${resolution}\n${dashLink}`);
779
- }
780
- }
781
- }
782
-
783
- logger.log("Uploaded", workerName, formatTime(uploadMs));
784
-
785
- // Update routing table for the script.
786
- if (routesOnly.length > 0) {
787
- deployments.push(
788
- publishRoutes(routesOnly, { workerUrl, scriptName, notProd }).then(() => {
789
- if (routesOnly.length > 10) {
790
- return routesOnly
791
- .slice(0, 9)
792
- .map((route) => renderRoute(route))
793
- .concat([`...and ${routesOnly.length - 10} more routes`]);
794
- }
795
- return routesOnly.map((route) => renderRoute(route));
796
- })
797
- );
798
- }
799
-
800
- // Update custom domains for the script
801
- if (customDomainsOnly.length > 0) {
802
- deployments.push(
803
- publishCustomDomains(workerUrl, accountId, customDomainsOnly)
804
- );
805
- }
806
-
807
- // Configure any schedules for the script.
808
- // TODO: rename this to `schedules`?
809
- if (triggers && triggers.length) {
810
- deployments.push(
811
- fetchResult(`${workerUrl}/schedules`, {
812
- // Note: PUT will override previous schedules on this script.
813
- method: "PUT",
814
- body: JSON.stringify(triggers.map((cron) => ({ cron }))),
815
- headers: {
816
- "Content-Type": "application/json",
817
- },
818
- }).then(() => triggers.map((trigger) => `schedule: ${trigger}`))
819
- );
820
- }
821
-
822
- if (config.queues.consumers && config.queues.consumers.length) {
823
- deployments.push(...updateQueueConsumers(config));
824
- }
825
-
826
- const targets = await Promise.all(deployments);
827
- const deployMs = Date.now() - start - uploadMs;
828
-
829
- if (deployments.length > 0) {
830
- logger.log("Published", workerName, formatTime(deployMs));
831
- for (const target of targets.flat()) {
832
- // Append protocol only on workers.dev domains
833
- logger.log(
834
- " ",
835
- (target.endsWith("workers.dev") ? "https://" : "") + target
836
- );
837
- }
838
- } else {
839
- logger.log("No publish targets for", workerName, formatTime(deployMs));
840
- }
841
-
842
- logger.log("Current Deployment ID:", deploymentId);
843
- }
844
-
845
- export function helpIfErrorIsSizeOrScriptStartup(
846
- err: unknown,
847
- dependencies: { [path: string]: { bytesInOutput: number } }
848
- ) {
849
- if (errIsScriptSize(err)) {
850
- printOffendingDependencies(dependencies);
851
- } else if (errIsStartupErr(err)) {
852
- const youFailed =
853
- "Your Worker failed validation because it exceeded startup limits.";
854
- const heresWhy =
855
- "To ensure fast responses, we place constraints on Worker startup -- like how much CPU it can use, or how long it can take.";
856
- const heresTheProblem =
857
- "Your Worker failed validation, which means it hit one of these startup limits.";
858
- const heresTheSolution =
859
- "Try reducing the amount of work done during startup (outside the event handler), either by removing code or relocating it inside the event handler.";
860
- logger.warn(
861
- [youFailed, heresWhy, heresTheProblem, heresTheSolution].join("\n")
862
- );
863
- }
864
- }
865
-
866
- function formatTime(duration: number) {
867
- return `(${(duration / 1000).toFixed(2)} sec)`;
868
- }
869
-
870
- /**
871
- * Associate the newly deployed Worker with the given routes.
872
- */
873
- async function publishRoutes(
874
- routes: Route[],
875
- {
876
- workerUrl,
877
- scriptName,
878
- notProd,
879
- }: { workerUrl: string; scriptName: string; notProd: boolean }
880
- ): Promise<string[]> {
881
- try {
882
- return await fetchResult(`${workerUrl}/routes`, {
883
- // Note: PUT will delete previous routes on this script.
884
- method: "PUT",
885
- body: JSON.stringify(
886
- routes.map((route) =>
887
- typeof route !== "object" ? { pattern: route } : route
888
- )
889
- ),
890
- headers: {
891
- "Content-Type": "application/json",
892
- },
893
- });
894
- } catch (e) {
895
- if (isAuthenticationError(e)) {
896
- // An authentication error is probably due to a known issue,
897
- // where the user is logged in via an API token that does not have "All Zones".
898
- return await publishRoutesFallback(routes, { scriptName, notProd });
899
- } else {
900
- throw e;
901
- }
902
- }
903
- }
904
-
905
- /**
906
- * Try updating routes for the Worker using a less optimal zone-based API.
907
- *
908
- * Compute match zones to the routes, then for each route attempt to connect it to the Worker via the zone.
909
- */
910
- async function publishRoutesFallback(
911
- routes: Route[],
912
- { scriptName, notProd }: { scriptName: string; notProd: boolean }
913
- ) {
914
- if (notProd) {
915
- throw new Error(
916
- "Service environments combined with an API token that doesn't have 'All Zones' permissions is not supported.\n" +
917
- "Either turn off service environments by setting `legacy_env = true`, creating an API token with 'All Zones' permissions, or logging in via OAuth"
918
- );
919
- }
920
- logger.warn(
921
- "The current authentication token does not have 'All Zones' permissions.\n" +
922
- "Falling back to using the zone-based API endpoint to update each route individually.\n" +
923
- "Note that there is no access to routes associated with zones that the API token does not have permission for.\n" +
924
- "Existing routes for this Worker in such zones will not be deleted."
925
- );
926
-
927
- const deployedRoutes: string[] = [];
928
-
929
- // Collect the routes (and their zones) that will be deployed.
930
- const activeZones = new Map<string, string>();
931
- const routesToDeploy = new Map<string, string>();
932
- for (const route of routes) {
933
- const zone = await getZoneForRoute(route);
934
- if (zone) {
935
- activeZones.set(zone.id, zone.host);
936
- routesToDeploy.set(
937
- typeof route === "string" ? route : route.pattern,
938
- zone.id
939
- );
940
- }
941
- }
942
-
943
- // Collect the routes that are already deployed.
944
- const allRoutes = new Map<string, string>();
945
- const alreadyDeployedRoutes = new Set<string>();
946
- for (const [zone, host] of activeZones) {
947
- try {
948
- for (const { pattern, script } of await fetchListResult<{
949
- pattern: string;
950
- script: string;
951
- }>(`/zones/${zone}/workers/routes`)) {
952
- allRoutes.set(pattern, script);
953
- if (script === scriptName) {
954
- alreadyDeployedRoutes.add(pattern);
955
- }
956
- }
957
- } catch (e) {
958
- if (isAuthenticationError(e)) {
959
- e.notes.push({
960
- text: `This could be because the API token being used does not have permission to access the zone "${host}" (${zone}).`,
961
- });
962
- }
963
- throw e;
964
- }
965
- }
966
-
967
- // Deploy each route that is not already deployed.
968
- for (const [routePattern, zoneId] of routesToDeploy.entries()) {
969
- if (allRoutes.has(routePattern)) {
970
- const knownScript = allRoutes.get(routePattern);
971
- if (knownScript === scriptName) {
972
- // This route is already associated with this worker, so no need to hit the API.
973
- alreadyDeployedRoutes.delete(routePattern);
974
- continue;
975
- } else {
976
- throw new Error(
977
- `The route with pattern "${routePattern}" is already associated with another worker called "${knownScript}".`
978
- );
979
- }
980
- }
981
-
982
- const { pattern } = await fetchResult<{ pattern: string }>(
983
- `/zones/${zoneId}/workers/routes`,
984
- {
985
- method: "POST",
986
- body: JSON.stringify({
987
- pattern: routePattern,
988
- script: scriptName,
989
- }),
990
- headers: {
991
- "Content-Type": "application/json",
992
- },
993
- }
994
- );
995
-
996
- deployedRoutes.push(pattern);
997
- }
998
-
999
- if (alreadyDeployedRoutes.size) {
1000
- logger.warn(
1001
- "Previously deployed routes:\n" +
1002
- "The following routes were already associated with this worker, and have not been deleted:\n" +
1003
- [...alreadyDeployedRoutes.values()].map((route) => ` - "${route}"\n`) +
1004
- "If these routes are not wanted then you can remove them in the dashboard."
1005
- );
1006
- }
1007
-
1008
- return deployedRoutes;
1009
- }
1010
-
1011
- function isAuthenticationError(e: unknown): e is ParseError {
1012
- return e instanceof ParseError && (e as { code?: number }).code === 10000;
1013
- }
1014
-
1015
- async function ensureQueuesExist(config: Config) {
1016
- const producers = (config.queues.producers || []).map(
1017
- (producer) => producer.queue
1018
- );
1019
- const consumers = (config.queues.consumers || []).map(
1020
- (consumer) => consumer.queue
1021
- );
1022
-
1023
- const queueNames = producers.concat(consumers);
1024
- for (const queue of queueNames) {
1025
- try {
1026
- await getQueue(config, queue);
1027
- } catch (err) {
1028
- const queueErr = err as FetchError;
1029
- if (queueErr.code === 11000) {
1030
- // queue_not_found
1031
- throw new Error(
1032
- `Queue "${queue}" does not exist. To create it, run: wrangler queues create ${queue}`
1033
- );
1034
- }
1035
- throw err;
1036
- }
1037
- }
1038
- }
1039
-
1040
- function updateQueueConsumers(config: Config): Promise<string[]>[] {
1041
- const consumers = config.queues.consumers || [];
1042
- return consumers.map((consumer) => {
1043
- const body: PutConsumerBody = {
1044
- dead_letter_queue: consumer.dead_letter_queue,
1045
- settings: {
1046
- batch_size: consumer.max_batch_size,
1047
- max_retries: consumer.max_retries,
1048
- max_wait_time_ms: consumer.max_batch_timeout
1049
- ? 1000 * consumer.max_batch_timeout
1050
- : undefined,
1051
- max_concurrency: consumer.max_concurrency,
1052
- },
1053
- };
1054
-
1055
- if (config.name === undefined) {
1056
- // TODO: how can we reliably get the current script name?
1057
- throw new Error("Script name is required to update queue consumers");
1058
- }
1059
- const scriptName = config.name;
1060
- const envName = undefined; // TODO: script environment for wrangler publish?
1061
- return putConsumer(config, consumer.queue, scriptName, envName, body).then(
1062
- () => [`Consumer for ${consumer.queue}`]
1063
- );
1064
- });
1065
- }