wrangler 0.0.0-e351afcf → 0.0.0-e3791f7d8

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 (331) hide show
  1. package/README.md +29 -43
  2. package/bin/wrangler.js +13 -102
  3. package/config-schema.json +3300 -0
  4. package/package.json +183 -199
  5. package/templates/__tests__/tsconfig.json +1 -1
  6. package/templates/checked-fetch.js +11 -4
  7. package/templates/facade.d.ts +7 -2
  8. package/templates/gitignore +2 -4
  9. package/templates/init-tests/test-vitest-new-worker.js +1 -1
  10. package/templates/init-tests/test-vitest-new-worker.ts +3 -3
  11. package/templates/middleware/common.ts +9 -4
  12. package/templates/middleware/loader-modules.ts +94 -44
  13. package/templates/middleware/loader-sw.ts +6 -10
  14. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  15. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  16. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  17. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  18. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  19. package/templates/middleware/middleware-scheduled.ts +15 -1
  20. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  21. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  22. package/templates/modules-watch-stub.js +4 -0
  23. package/templates/new-worker-scheduled.ts +1 -1
  24. package/templates/new-worker.ts +1 -1
  25. package/templates/pages-dev-pipeline.ts +2 -1
  26. package/templates/pages-dev-util.ts +4 -1
  27. package/templates/pages-shim.ts +0 -3
  28. package/templates/pages-template-plugin.ts +14 -4
  29. package/templates/pages-template-worker.ts +12 -3
  30. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  31. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  32. package/templates/tsconfig.init.json +4 -87
  33. package/templates/tsconfig.json +1 -1
  34. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  35. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  36. package/wrangler-dist/ProxyWorker.js +241 -0
  37. package/wrangler-dist/ProxyWorker.js.map +6 -0
  38. package/wrangler-dist/cli.d.ts +25340 -281
  39. package/wrangler-dist/cli.js +142304 -115105
  40. package/Cloudflare_CA.pem +0 -18
  41. package/import_meta_url.js +0 -3
  42. package/miniflare-config-stubs/.env.empty +0 -0
  43. package/miniflare-config-stubs/package.empty.json +0 -1
  44. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  45. package/miniflare-dist/index.mjs +0 -6442
  46. package/src/__tests__/access.test.ts +0 -25
  47. package/src/__tests__/api-dev.test.ts +0 -238
  48. package/src/__tests__/api-devregistry.test.ts +0 -121
  49. package/src/__tests__/api.test.ts +0 -102
  50. package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
  51. package/src/__tests__/config-cache.test.ts +0 -42
  52. package/src/__tests__/configuration.test.ts +0 -4509
  53. package/src/__tests__/constellation.test.ts +0 -371
  54. package/src/__tests__/d1/d1.test.ts +0 -82
  55. package/src/__tests__/d1/execute.test.ts +0 -66
  56. package/src/__tests__/d1/migrate.test.ts +0 -257
  57. package/src/__tests__/d1/splitter.test.ts +0 -255
  58. package/src/__tests__/delete.test.ts +0 -272
  59. package/src/__tests__/deployments.test.ts +0 -369
  60. package/src/__tests__/dev.test.tsx +0 -1617
  61. package/src/__tests__/generate.test.ts +0 -237
  62. package/src/__tests__/get-host-from-url.test.ts +0 -16
  63. package/src/__tests__/guess-worker-format.test.ts +0 -120
  64. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  65. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  66. package/src/__tests__/helpers/end-event-loop.ts +0 -6
  67. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  68. package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
  69. package/src/__tests__/helpers/mock-bin.ts +0 -36
  70. package/src/__tests__/helpers/mock-console.ts +0 -112
  71. package/src/__tests__/helpers/mock-dialogs.ts +0 -139
  72. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
  73. package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
  74. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  75. package/src/__tests__/helpers/mock-istty.ts +0 -74
  76. package/src/__tests__/helpers/mock-known-routes.ts +0 -12
  77. package/src/__tests__/helpers/mock-kv.ts +0 -46
  78. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
  79. package/src/__tests__/helpers/mock-process.ts +0 -34
  80. package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
  81. package/src/__tests__/helpers/mock-stdin.ts +0 -108
  82. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  83. package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
  84. package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
  85. package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -160
  86. package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
  87. package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
  88. package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
  89. package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
  90. package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
  91. package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
  92. package/src/__tests__/helpers/msw/index.ts +0 -52
  93. package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
  94. package/src/__tests__/helpers/run-in-tmp.ts +0 -38
  95. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  96. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
  97. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
  98. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
  99. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
  100. package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -11
  101. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
  102. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  103. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  104. package/src/__tests__/https-options.test.ts +0 -163
  105. package/src/__tests__/index.test.ts +0 -282
  106. package/src/__tests__/init.test.ts +0 -3196
  107. package/src/__tests__/jest.setup.ts +0 -179
  108. package/src/__tests__/kv.test.ts +0 -1799
  109. package/src/__tests__/logger.test.ts +0 -207
  110. package/src/__tests__/logout.test.ts +0 -47
  111. package/src/__tests__/metrics.test.ts +0 -493
  112. package/src/__tests__/middleware.scheduled.test.ts +0 -145
  113. package/src/__tests__/middleware.test.ts +0 -816
  114. package/src/__tests__/mtls-certificates.test.ts +0 -589
  115. package/src/__tests__/package-manager.test.ts +0 -353
  116. package/src/__tests__/pages/deployment-list.test.ts +0 -80
  117. package/src/__tests__/pages/functions-build.test.ts +0 -522
  118. package/src/__tests__/pages/pages.test.ts +0 -81
  119. package/src/__tests__/pages/project-create.test.ts +0 -63
  120. package/src/__tests__/pages/project-list.test.ts +0 -110
  121. package/src/__tests__/pages/project-upload.test.ts +0 -500
  122. package/src/__tests__/pages/publish.test.ts +0 -2864
  123. package/src/__tests__/pages-deployment-tail.test.ts +0 -955
  124. package/src/__tests__/parse.test.ts +0 -436
  125. package/src/__tests__/paths.test.ts +0 -39
  126. package/src/__tests__/publish.test.ts +0 -8849
  127. package/src/__tests__/pubsub.test.ts +0 -496
  128. package/src/__tests__/queues.test.ts +0 -532
  129. package/src/__tests__/r2.test.ts +0 -374
  130. package/src/__tests__/route.test.ts +0 -45
  131. package/src/__tests__/secret.test.ts +0 -693
  132. package/src/__tests__/tail.test.ts +0 -958
  133. package/src/__tests__/test-old-node-version.js +0 -31
  134. package/src/__tests__/traverse-module-graph.test.ts +0 -220
  135. package/src/__tests__/tsconfig-sanity.ts +0 -12
  136. package/src/__tests__/tsconfig.json +0 -8
  137. package/src/__tests__/tsconfig.tsbuildinfo +0 -1
  138. package/src/__tests__/type-generation.test.ts +0 -234
  139. package/src/__tests__/user.test.ts +0 -118
  140. package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
  141. package/src/__tests__/validate-dev-props.test.ts +0 -56
  142. package/src/__tests__/version.test.ts +0 -35
  143. package/src/__tests__/whoami.test.tsx +0 -172
  144. package/src/__tests__/worker-namespace.test.ts +0 -340
  145. package/src/abort.d.ts +0 -3
  146. package/src/api/dev.ts +0 -321
  147. package/src/api/index.ts +0 -11
  148. package/src/api/mtls-certificate.ts +0 -148
  149. package/src/api/pages/create-worker-bundle-contents.ts +0 -76
  150. package/src/api/pages/index.ts +0 -5
  151. package/src/api/pages/publish.tsx +0 -371
  152. package/src/bundle-reporter.ts +0 -68
  153. package/src/bundle.ts +0 -911
  154. package/src/cfetch/index.ts +0 -158
  155. package/src/cfetch/internal.ts +0 -258
  156. package/src/cli.ts +0 -28
  157. package/src/config/README.md +0 -107
  158. package/src/config/config.ts +0 -282
  159. package/src/config/diagnostics.ts +0 -80
  160. package/src/config/environment.ts +0 -618
  161. package/src/config/index.ts +0 -403
  162. package/src/config/validation-helpers.ts +0 -597
  163. package/src/config/validation.ts +0 -2342
  164. package/src/config-cache.ts +0 -85
  165. package/src/constellation/createProject.tsx +0 -51
  166. package/src/constellation/deleteProject.ts +0 -51
  167. package/src/constellation/deleteProjectModel.ts +0 -68
  168. package/src/constellation/index.ts +0 -75
  169. package/src/constellation/listCatalog.tsx +0 -35
  170. package/src/constellation/listModel.tsx +0 -41
  171. package/src/constellation/listProject.tsx +0 -28
  172. package/src/constellation/listRuntime.tsx +0 -28
  173. package/src/constellation/options.ts +0 -17
  174. package/src/constellation/types.ts +0 -17
  175. package/src/constellation/uploadModel.tsx +0 -64
  176. package/src/constellation/utils.ts +0 -90
  177. package/src/create-worker-preview.ts +0 -293
  178. package/src/create-worker-upload-form.ts +0 -359
  179. package/src/d1/backups.tsx +0 -219
  180. package/src/d1/constants.ts +0 -2
  181. package/src/d1/create.tsx +0 -70
  182. package/src/d1/delete.ts +0 -53
  183. package/src/d1/execute.tsx +0 -357
  184. package/src/d1/formatTimeAgo.ts +0 -14
  185. package/src/d1/index.ts +0 -100
  186. package/src/d1/list.tsx +0 -62
  187. package/src/d1/migrations/apply.tsx +0 -212
  188. package/src/d1/migrations/create.tsx +0 -79
  189. package/src/d1/migrations/helpers.ts +0 -169
  190. package/src/d1/migrations/index.ts +0 -3
  191. package/src/d1/migrations/list.tsx +0 -95
  192. package/src/d1/migrations/options.ts +0 -23
  193. package/src/d1/options.ts +0 -22
  194. package/src/d1/splitter.ts +0 -161
  195. package/src/d1/types.ts +0 -25
  196. package/src/d1/utils.ts +0 -49
  197. package/src/delete.ts +0 -100
  198. package/src/deployments.ts +0 -368
  199. package/src/deprecated/index.ts +0 -144
  200. package/src/dev/dev-vars.ts +0 -39
  201. package/src/dev/dev.tsx +0 -602
  202. package/src/dev/get-local-persistence-path.ts +0 -31
  203. package/src/dev/local.tsx +0 -948
  204. package/src/dev/remote.tsx +0 -632
  205. package/src/dev/start-server.ts +0 -543
  206. package/src/dev/use-esbuild.ts +0 -212
  207. package/src/dev/validate-dev-props.ts +0 -40
  208. package/src/dev-registry.ts +0 -202
  209. package/src/dev.tsx +0 -934
  210. package/src/dialogs.ts +0 -136
  211. package/src/dispatch-namespace.ts +0 -211
  212. package/src/docs/helpers.ts +0 -50
  213. package/src/docs/index.ts +0 -54
  214. package/src/durable.ts +0 -102
  215. package/src/entry.ts +0 -338
  216. package/src/environment-variables/factory.ts +0 -89
  217. package/src/environment-variables/misc-variables.ts +0 -30
  218. package/src/errors.ts +0 -11
  219. package/src/generate/index.ts +0 -298
  220. package/src/git-client.ts +0 -135
  221. package/src/global-wrangler-config-path.ts +0 -26
  222. package/src/https-options.ts +0 -127
  223. package/src/index.ts +0 -768
  224. package/src/init.ts +0 -1032
  225. package/src/inspect.ts +0 -782
  226. package/src/intl-polyfill.d.ts +0 -139
  227. package/src/is-ci.ts +0 -14
  228. package/src/is-interactive.ts +0 -16
  229. package/src/jest.d.ts +0 -4
  230. package/src/kv/helpers.ts +0 -433
  231. package/src/kv/index.ts +0 -594
  232. package/src/logger.ts +0 -123
  233. package/src/metrics/index.ts +0 -5
  234. package/src/metrics/metrics-config.ts +0 -239
  235. package/src/metrics/metrics-dispatcher.ts +0 -96
  236. package/src/metrics/metrics-usage-headers.ts +0 -24
  237. package/src/metrics/send-event.ts +0 -99
  238. package/src/miniflare-cli/README.md +0 -30
  239. package/src/miniflare-cli/assets.ts +0 -251
  240. package/src/miniflare-cli/index.ts +0 -210
  241. package/src/miniflare-cli/request-context.ts +0 -40
  242. package/src/miniflare-cli/tsconfig.json +0 -9
  243. package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
  244. package/src/miniflare-cli/types.ts +0 -11
  245. package/src/module-collection.ts +0 -333
  246. package/src/mtls-certificate/cli.ts +0 -155
  247. package/src/open-in-browser.ts +0 -17
  248. package/src/package-manager.ts +0 -219
  249. package/src/pages/build.ts +0 -422
  250. package/src/pages/buildFunctions.ts +0 -140
  251. package/src/pages/constants.ts +0 -18
  252. package/src/pages/deployment-tails.ts +0 -281
  253. package/src/pages/deployments.tsx +0 -84
  254. package/src/pages/dev.ts +0 -734
  255. package/src/pages/errors.ts +0 -67
  256. package/src/pages/functions/buildPlugin.ts +0 -113
  257. package/src/pages/functions/buildWorker.ts +0 -348
  258. package/src/pages/functions/filepath-routing.test.ts +0 -234
  259. package/src/pages/functions/filepath-routing.ts +0 -189
  260. package/src/pages/functions/identifiers.ts +0 -78
  261. package/src/pages/functions/routes-consolidation.test.ts +0 -250
  262. package/src/pages/functions/routes-consolidation.ts +0 -73
  263. package/src/pages/functions/routes-transformation.test.ts +0 -282
  264. package/src/pages/functions/routes-transformation.ts +0 -115
  265. package/src/pages/functions/routes-validation.test.ts +0 -403
  266. package/src/pages/functions/routes-validation.ts +0 -202
  267. package/src/pages/functions/routes.ts +0 -151
  268. package/src/pages/functions/tsconfig.json +0 -8
  269. package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
  270. package/src/pages/functions.ts +0 -86
  271. package/src/pages/hash.ts +0 -13
  272. package/src/pages/index.ts +0 -102
  273. package/src/pages/projects.tsx +0 -159
  274. package/src/pages/prompt-select-project.tsx +0 -31
  275. package/src/pages/publish.tsx +0 -267
  276. package/src/pages/types.ts +0 -46
  277. package/src/pages/upload.tsx +0 -469
  278. package/src/pages/utils.ts +0 -23
  279. package/src/parse.ts +0 -308
  280. package/src/paths.ts +0 -71
  281. package/src/proxy.ts +0 -694
  282. package/src/publish/index.ts +0 -274
  283. package/src/publish/publish.ts +0 -1060
  284. package/src/pubsub/index.ts +0 -286
  285. package/src/pubsub/pubsub-commands.ts +0 -623
  286. package/src/queues/cli/commands/consumer/add.ts +0 -71
  287. package/src/queues/cli/commands/consumer/index.ts +0 -19
  288. package/src/queues/cli/commands/consumer/remove.ts +0 -31
  289. package/src/queues/cli/commands/create.ts +0 -25
  290. package/src/queues/cli/commands/delete.ts +0 -26
  291. package/src/queues/cli/commands/index.ts +0 -35
  292. package/src/queues/cli/commands/list.ts +0 -25
  293. package/src/queues/client.ts +0 -136
  294. package/src/queues/utils.ts +0 -18
  295. package/src/r2/constants.ts +0 -4
  296. package/src/r2/helpers.ts +0 -132
  297. package/src/r2/index.ts +0 -289
  298. package/src/routes.ts +0 -140
  299. package/src/secret/index.ts +0 -376
  300. package/src/selfsigned.d.ts +0 -29
  301. package/src/sites.ts +0 -484
  302. package/src/tail/createTail.ts +0 -406
  303. package/src/tail/filters.ts +0 -277
  304. package/src/tail/index.ts +0 -211
  305. package/src/tail/printing.ts +0 -122
  306. package/src/traverse-module-graph.ts +0 -53
  307. package/src/tsconfig-sanity.ts +0 -16
  308. package/src/type-generation.ts +0 -181
  309. package/src/update-check.ts +0 -19
  310. package/src/user/access.ts +0 -68
  311. package/src/user/auth-variables.ts +0 -113
  312. package/src/user/choose-account.tsx +0 -39
  313. package/src/user/generate-auth-url.ts +0 -33
  314. package/src/user/generate-random-state.ts +0 -16
  315. package/src/user/index.ts +0 -2
  316. package/src/user/user.ts +0 -1234
  317. package/src/utils/collectKeyValues.ts +0 -14
  318. package/src/utils/render.ts +0 -93
  319. package/src/whoami.ts +0 -135
  320. package/src/worker.ts +0 -274
  321. package/src/yargs-types.ts +0 -37
  322. package/src/zones.ts +0 -191
  323. package/templates/__tests__/tsconfig.tsbuildinfo +0 -1
  324. package/templates/d1-beta-facade.js +0 -244
  325. package/templates/first-party-worker-module-facade.ts +0 -19
  326. package/templates/format-dev-errors.ts +0 -34
  327. package/templates/serve-static-assets.ts +0 -66
  328. package/templates/service-bindings-module-facade.js +0 -55
  329. package/templates/service-bindings-sw-facade.js +0 -42
  330. package/templates/tsconfig.tsbuildinfo +0 -1
  331. package/wrangler-dist/wasm-sync.wasm +0 -0
@@ -1,1060 +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 } 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
- const worker: CfWorkerInit = {
575
- name: scriptName,
576
- main: {
577
- name: path.basename(resolvedEntryPointPath),
578
- content: content,
579
- type: bundleType,
580
- },
581
- bindings,
582
- migrations,
583
- modules,
584
- compatibility_date: props.compatibilityDate ?? config.compatibility_date,
585
- compatibility_flags: compatibilityFlags,
586
- usage_model: config.usage_model,
587
- keepVars,
588
- logpush: props.logpush !== undefined ? props.logpush : config.logpush,
589
- };
590
-
591
- // As this is not deterministic for testing, we detect if in a jest environment and run asynchronously
592
- // We do not care about the timing outside of testing
593
- const bundleSizePromise = printBundleSize(
594
- { name: path.basename(resolvedEntryPointPath), content: content },
595
- modules
596
- );
597
- if (process.env.JEST_WORKER_ID !== undefined) await bundleSizePromise;
598
- else void bundleSizePromise;
599
-
600
- const withoutStaticAssets = {
601
- ...bindings,
602
- kv_namespaces: config.kv_namespaces,
603
- text_blobs: config.text_blobs,
604
- };
605
-
606
- // mask anything that was overridden in cli args
607
- // so that we don't log potential secrets into the terminal
608
- const maskedVars = { ...withoutStaticAssets.vars };
609
- for (const key of Object.keys(maskedVars)) {
610
- if (maskedVars[key] !== config.vars[key]) {
611
- // This means it was overridden in cli args
612
- // so let's mask it
613
- maskedVars[key] = "(hidden)";
614
- }
615
- }
616
-
617
- printBindings({ ...withoutStaticAssets, vars: maskedVars });
618
-
619
- if (!props.dryRun) {
620
- await ensureQueuesExist(config);
621
-
622
- // Upload the script so it has time to propagate.
623
- // We can also now tell whether available_on_subdomain is set
624
- try {
625
- const result = await fetchResult<{
626
- available_on_subdomain: boolean;
627
- id: string | null;
628
- etag: string | null;
629
- pipeline_hash: string | null;
630
- deployment_id: string | null;
631
- }>(
632
- workerUrl,
633
- {
634
- method: "PUT",
635
- body: createWorkerUploadForm(worker),
636
- headers: await getMetricsUsageHeaders(config.send_metrics),
637
- },
638
- new URLSearchParams({
639
- include_subdomain_availability: "true",
640
- // pass excludeScript so the whole body of the
641
- // script doesn't get included in the response
642
- excludeScript: "true",
643
- })
644
- );
645
-
646
- available_on_subdomain = result.available_on_subdomain;
647
- deploymentId = addHyphens(result.deployment_id) ?? result.deployment_id;
648
-
649
- if (config.first_party_worker) {
650
- // Print some useful information returned after publishing
651
- // Not all fields will be populated for every worker
652
- // These fields are likely to be scraped by tools, so do not rename
653
- if (result.id) logger.log("Worker ID: ", result.id);
654
- if (result.etag) logger.log("Worker ETag: ", result.etag);
655
- if (result.pipeline_hash)
656
- logger.log("Worker PipelineHash: ", result.pipeline_hash);
657
- }
658
- } catch (err) {
659
- helpIfErrorIsSizeOrScriptStartup(err, dependencies);
660
- throw err;
661
- }
662
- }
663
- } finally {
664
- if (typeof destination !== "string") {
665
- // this means we're using a temp dir,
666
- // so let's clean up before we proceed
667
- await destination.cleanup();
668
- }
669
- }
670
-
671
- if (props.dryRun) {
672
- logger.log(`--dry-run: exiting now.`);
673
- return;
674
- }
675
- assert(accountId, "Missing accountId");
676
-
677
- const uploadMs = Date.now() - start;
678
- const deployments: Promise<string[]>[] = [];
679
-
680
- if (deployToWorkersDev) {
681
- // Deploy to a subdomain of `workers.dev`
682
- const userSubdomain = await getWorkersDevSubdomain(accountId);
683
- const scriptURL =
684
- props.legacyEnv || !props.env
685
- ? `${scriptName}.${userSubdomain}.workers.dev`
686
- : `${envName}.${scriptName}.${userSubdomain}.workers.dev`;
687
- if (!available_on_subdomain) {
688
- // Enable the `workers.dev` subdomain.
689
- deployments.push(
690
- fetchResult(`${workerUrl}/subdomain`, {
691
- method: "POST",
692
- body: JSON.stringify({ enabled: true }),
693
- headers: {
694
- "Content-Type": "application/json",
695
- },
696
- })
697
- .then(() => [scriptURL])
698
- // Add a delay when the subdomain is first created.
699
- // This is to prevent an issue where a negative cache-hit
700
- // causes the subdomain to be unavailable for 30 seconds.
701
- // This is a temporary measure until we fix this on the edge.
702
- .then(async (url) => {
703
- await sleep(3000);
704
- return url;
705
- })
706
- );
707
- } else {
708
- deployments.push(Promise.resolve([scriptURL]));
709
- }
710
- } else {
711
- if (available_on_subdomain) {
712
- // Disable the workers.dev deployment
713
- await fetchResult(`${workerUrl}/subdomain`, {
714
- method: "POST",
715
- body: JSON.stringify({ enabled: false }),
716
- headers: {
717
- "Content-Type": "application/json",
718
- },
719
- });
720
- } else if (routes.length !== 0) {
721
- // if you get to this point it's because
722
- // you're trying to deploy a worker to a custom
723
- // domain that's already bound to another worker.
724
- // so this thing is about finding workers that have
725
- // bindings to the routes you're trying to deploy to.
726
- //
727
- // the logic is kinda similar (read: duplicated) from publishRoutesFallback,
728
- // except here we know we have a good API token or whatever so we don't need
729
- // to bother with all the error handling tomfoolery.
730
- const routesWithOtherBindings: Record<string, string[]> = {};
731
- for (const route of routes) {
732
- const zone = await getZoneForRoute(route);
733
- if (!zone) {
734
- continue;
735
- }
736
-
737
- const routePattern = typeof route === "string" ? route : route.pattern;
738
- const routesInZone = await fetchListResult<{
739
- pattern: string;
740
- script: string;
741
- }>(`/zones/${zone.id}/workers/routes`);
742
-
743
- routesInZone.forEach(({ script, pattern }) => {
744
- if (pattern === routePattern && script !== scriptName) {
745
- if (!(script in routesWithOtherBindings)) {
746
- routesWithOtherBindings[script] = [];
747
- }
748
-
749
- routesWithOtherBindings[script].push(pattern);
750
- }
751
- });
752
- }
753
-
754
- if (Object.keys(routesWithOtherBindings).length > 0) {
755
- let errorMessage =
756
- "Can't publish a worker to routes that are assigned to another worker.\n";
757
-
758
- for (const worker in routesWithOtherBindings) {
759
- const assignedRoutes = routesWithOtherBindings[worker];
760
- errorMessage += `"${worker}" is already assigned to routes:\n${assignedRoutes.map(
761
- (r) => ` - ${chalk.underline(r)}\n`
762
- )}`;
763
- }
764
-
765
- const resolution =
766
- "Unassign other workers from the routes you want to publish to, and then try again.";
767
- const dashLink = `Visit ${chalk.blue(
768
- chalk.underline(
769
- `https://dash.cloudflare.com/${accountId}/workers/overview`
770
- )
771
- )} to unassign a worker from a route.`;
772
-
773
- throw new Error(`${errorMessage}\n${resolution}\n${dashLink}`);
774
- }
775
- }
776
- }
777
-
778
- logger.log("Uploaded", workerName, formatTime(uploadMs));
779
-
780
- // Update routing table for the script.
781
- if (routesOnly.length > 0) {
782
- deployments.push(
783
- publishRoutes(routesOnly, { workerUrl, scriptName, notProd }).then(() => {
784
- if (routesOnly.length > 10) {
785
- return routesOnly
786
- .slice(0, 9)
787
- .map((route) => renderRoute(route))
788
- .concat([`...and ${routesOnly.length - 10} more routes`]);
789
- }
790
- return routesOnly.map((route) => renderRoute(route));
791
- })
792
- );
793
- }
794
-
795
- // Update custom domains for the script
796
- if (customDomainsOnly.length > 0) {
797
- deployments.push(
798
- publishCustomDomains(workerUrl, accountId, customDomainsOnly)
799
- );
800
- }
801
-
802
- // Configure any schedules for the script.
803
- // TODO: rename this to `schedules`?
804
- if (triggers && triggers.length) {
805
- deployments.push(
806
- fetchResult(`${workerUrl}/schedules`, {
807
- // Note: PUT will override previous schedules on this script.
808
- method: "PUT",
809
- body: JSON.stringify(triggers.map((cron) => ({ cron }))),
810
- headers: {
811
- "Content-Type": "application/json",
812
- },
813
- }).then(() => triggers.map((trigger) => `schedule: ${trigger}`))
814
- );
815
- }
816
-
817
- if (config.queues.consumers && config.queues.consumers.length) {
818
- deployments.push(...updateQueueConsumers(config));
819
- }
820
-
821
- const targets = await Promise.all(deployments);
822
- const deployMs = Date.now() - start - uploadMs;
823
-
824
- if (deployments.length > 0) {
825
- logger.log("Published", workerName, formatTime(deployMs));
826
- for (const target of targets.flat()) {
827
- // Append protocol only on workers.dev domains
828
- logger.log(
829
- " ",
830
- (target.endsWith("workers.dev") ? "https://" : "") + target
831
- );
832
- }
833
- } else {
834
- logger.log("No publish targets for", workerName, formatTime(deployMs));
835
- }
836
-
837
- logger.log("Current Deployment ID:", deploymentId);
838
- }
839
-
840
- export function helpIfErrorIsSizeOrScriptStartup(
841
- err: unknown,
842
- dependencies: { [path: string]: { bytesInOutput: number } }
843
- ) {
844
- if (errIsScriptSize(err)) {
845
- printOffendingDependencies(dependencies);
846
- } else if (errIsStartupErr(err)) {
847
- const youFailed =
848
- "Your Worker failed validation because it exceeded startup limits.";
849
- const heresWhy =
850
- "To ensure fast responses, we place constraints on Worker startup -- like how much CPU it can use, or how long it can take.";
851
- const heresTheProblem =
852
- "Your Worker failed validation, which means it hit one of these startup limits.";
853
- const heresTheSolution =
854
- "Try reducing the amount of work done during startup (outside the event handler), either by removing code or relocating it inside the event handler.";
855
- logger.warn(
856
- [youFailed, heresWhy, heresTheProblem, heresTheSolution].join("\n")
857
- );
858
- }
859
- }
860
-
861
- function formatTime(duration: number) {
862
- return `(${(duration / 1000).toFixed(2)} sec)`;
863
- }
864
-
865
- /**
866
- * Associate the newly deployed Worker with the given routes.
867
- */
868
- async function publishRoutes(
869
- routes: Route[],
870
- {
871
- workerUrl,
872
- scriptName,
873
- notProd,
874
- }: { workerUrl: string; scriptName: string; notProd: boolean }
875
- ): Promise<string[]> {
876
- try {
877
- return await fetchResult(`${workerUrl}/routes`, {
878
- // Note: PUT will delete previous routes on this script.
879
- method: "PUT",
880
- body: JSON.stringify(
881
- routes.map((route) =>
882
- typeof route !== "object" ? { pattern: route } : route
883
- )
884
- ),
885
- headers: {
886
- "Content-Type": "application/json",
887
- },
888
- });
889
- } catch (e) {
890
- if (isAuthenticationError(e)) {
891
- // An authentication error is probably due to a known issue,
892
- // where the user is logged in via an API token that does not have "All Zones".
893
- return await publishRoutesFallback(routes, { scriptName, notProd });
894
- } else {
895
- throw e;
896
- }
897
- }
898
- }
899
-
900
- /**
901
- * Try updating routes for the Worker using a less optimal zone-based API.
902
- *
903
- * Compute match zones to the routes, then for each route attempt to connect it to the Worker via the zone.
904
- */
905
- async function publishRoutesFallback(
906
- routes: Route[],
907
- { scriptName, notProd }: { scriptName: string; notProd: boolean }
908
- ) {
909
- if (notProd) {
910
- throw new Error(
911
- "Service environments combined with an API token that doesn't have 'All Zones' permissions is not supported.\n" +
912
- "Either turn off service environments by setting `legacy_env = true`, creating an API token with 'All Zones' permissions, or logging in via OAuth"
913
- );
914
- }
915
- logger.warn(
916
- "The current authentication token does not have 'All Zones' permissions.\n" +
917
- "Falling back to using the zone-based API endpoint to update each route individually.\n" +
918
- "Note that there is no access to routes associated with zones that the API token does not have permission for.\n" +
919
- "Existing routes for this Worker in such zones will not be deleted."
920
- );
921
-
922
- const deployedRoutes: string[] = [];
923
-
924
- // Collect the routes (and their zones) that will be deployed.
925
- const activeZones = new Map<string, string>();
926
- const routesToDeploy = new Map<string, string>();
927
- for (const route of routes) {
928
- const zone = await getZoneForRoute(route);
929
- if (zone) {
930
- activeZones.set(zone.id, zone.host);
931
- routesToDeploy.set(
932
- typeof route === "string" ? route : route.pattern,
933
- zone.id
934
- );
935
- }
936
- }
937
-
938
- // Collect the routes that are already deployed.
939
- const allRoutes = new Map<string, string>();
940
- const alreadyDeployedRoutes = new Set<string>();
941
- for (const [zone, host] of activeZones) {
942
- try {
943
- for (const { pattern, script } of await fetchListResult<{
944
- pattern: string;
945
- script: string;
946
- }>(`/zones/${zone}/workers/routes`)) {
947
- allRoutes.set(pattern, script);
948
- if (script === scriptName) {
949
- alreadyDeployedRoutes.add(pattern);
950
- }
951
- }
952
- } catch (e) {
953
- if (isAuthenticationError(e)) {
954
- e.notes.push({
955
- text: `This could be because the API token being used does not have permission to access the zone "${host}" (${zone}).`,
956
- });
957
- }
958
- throw e;
959
- }
960
- }
961
-
962
- // Deploy each route that is not already deployed.
963
- for (const [routePattern, zoneId] of routesToDeploy.entries()) {
964
- if (allRoutes.has(routePattern)) {
965
- const knownScript = allRoutes.get(routePattern);
966
- if (knownScript === scriptName) {
967
- // This route is already associated with this worker, so no need to hit the API.
968
- alreadyDeployedRoutes.delete(routePattern);
969
- continue;
970
- } else {
971
- throw new Error(
972
- `The route with pattern "${routePattern}" is already associated with another worker called "${knownScript}".`
973
- );
974
- }
975
- }
976
-
977
- const { pattern } = await fetchResult<{ pattern: string }>(
978
- `/zones/${zoneId}/workers/routes`,
979
- {
980
- method: "POST",
981
- body: JSON.stringify({
982
- pattern: routePattern,
983
- script: scriptName,
984
- }),
985
- headers: {
986
- "Content-Type": "application/json",
987
- },
988
- }
989
- );
990
-
991
- deployedRoutes.push(pattern);
992
- }
993
-
994
- if (alreadyDeployedRoutes.size) {
995
- logger.warn(
996
- "Previously deployed routes:\n" +
997
- "The following routes were already associated with this worker, and have not been deleted:\n" +
998
- [...alreadyDeployedRoutes.values()].map((route) => ` - "${route}"\n`) +
999
- "If these routes are not wanted then you can remove them in the dashboard."
1000
- );
1001
- }
1002
-
1003
- return deployedRoutes;
1004
- }
1005
-
1006
- function isAuthenticationError(e: unknown): e is ParseError {
1007
- return e instanceof ParseError && (e as { code?: number }).code === 10000;
1008
- }
1009
-
1010
- async function ensureQueuesExist(config: Config) {
1011
- const producers = (config.queues.producers || []).map(
1012
- (producer) => producer.queue
1013
- );
1014
- const consumers = (config.queues.consumers || []).map(
1015
- (consumer) => consumer.queue
1016
- );
1017
-
1018
- const queueNames = producers.concat(consumers);
1019
- for (const queue of queueNames) {
1020
- try {
1021
- await getQueue(config, queue);
1022
- } catch (err) {
1023
- const queueErr = err as FetchError;
1024
- if (queueErr.code === 11000) {
1025
- // queue_not_found
1026
- throw new Error(
1027
- `Queue "${queue}" does not exist. To create it, run: wrangler queues create ${queue}`
1028
- );
1029
- }
1030
- throw err;
1031
- }
1032
- }
1033
- }
1034
-
1035
- function updateQueueConsumers(config: Config): Promise<string[]>[] {
1036
- const consumers = config.queues.consumers || [];
1037
- return consumers.map((consumer) => {
1038
- const body: PutConsumerBody = {
1039
- dead_letter_queue: consumer.dead_letter_queue,
1040
- settings: {
1041
- batch_size: consumer.max_batch_size,
1042
- max_retries: consumer.max_retries,
1043
- max_wait_time_ms: consumer.max_batch_timeout
1044
- ? 1000 * consumer.max_batch_timeout
1045
- : undefined,
1046
- max_concurrency: consumer.max_concurrency,
1047
- },
1048
- };
1049
-
1050
- if (config.name === undefined) {
1051
- // TODO: how can we reliably get the current script name?
1052
- throw new Error("Script name is required to update queue consumers");
1053
- }
1054
- const scriptName = config.name;
1055
- const envName = undefined; // TODO: script environment for wrangler publish?
1056
- return putConsumer(config, consumer.queue, scriptName, envName, body).then(
1057
- () => [`Consumer for ${consumer.queue}`]
1058
- );
1059
- });
1060
- }