wrangler 2.0.12 → 2.0.16
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.
- package/README.md +7 -1
- package/bin/wrangler.js +111 -57
- package/miniflare-dist/index.mjs +9 -2
- package/package.json +156 -154
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +30 -24
- package/src/__tests__/configuration.test.ts +3935 -3476
- package/src/__tests__/dev.test.tsx +1128 -979
- package/src/__tests__/guess-worker-format.test.ts +68 -68
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/faye-websocket.d.ts +4 -4
- package/src/__tests__/helpers/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-cfetch.ts +92 -92
- package/src/__tests__/helpers/mock-console.ts +49 -39
- package/src/__tests__/helpers/mock-dialogs.ts +94 -71
- package/src/__tests__/helpers/mock-http-server.ts +30 -30
- package/src/__tests__/helpers/mock-istty.ts +65 -18
- package/src/__tests__/helpers/mock-kv.ts +26 -26
- package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
- package/src/__tests__/helpers/mock-process.ts +39 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/run-in-tmp.ts +27 -27
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/write-worker-source.ts +16 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +104 -104
- package/src/__tests__/index.test.ts +239 -234
- package/src/__tests__/init.test.ts +1605 -1250
- package/src/__tests__/jest.setup.ts +63 -33
- package/src/__tests__/kv.test.ts +1128 -1011
- package/src/__tests__/logger.test.ts +100 -74
- package/src/__tests__/package-manager.test.ts +303 -303
- package/src/__tests__/pages.test.ts +1152 -652
- package/src/__tests__/parse.test.ts +252 -252
- package/src/__tests__/publish.test.ts +6371 -5622
- package/src/__tests__/pubsub.test.ts +367 -0
- package/src/__tests__/r2.test.ts +133 -133
- package/src/__tests__/route.test.ts +18 -18
- package/src/__tests__/secret.test.ts +382 -377
- package/src/__tests__/tail.test.ts +530 -530
- package/src/__tests__/user.test.ts +123 -111
- package/src/__tests__/whoami.test.tsx +198 -117
- package/src/__tests__/worker-namespace.test.ts +327 -0
- package/src/abort.d.ts +1 -1
- package/src/api/dev.ts +49 -0
- package/src/api/index.ts +1 -0
- package/src/bundle-reporter.tsx +29 -0
- package/src/bundle.ts +157 -149
- package/src/cfetch/index.ts +80 -80
- package/src/cfetch/internal.ts +90 -83
- package/src/cli.ts +21 -7
- package/src/config/config.ts +204 -195
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +390 -357
- package/src/config/index.ts +206 -193
- package/src/config/validation-helpers.ts +366 -366
- package/src/config/validation.ts +1573 -1376
- package/src/config-cache.ts +79 -41
- package/src/create-worker-preview.ts +206 -136
- package/src/create-worker-upload-form.ts +247 -238
- package/src/dev/dev-vars.ts +13 -13
- package/src/dev/dev.tsx +329 -307
- package/src/dev/local.tsx +304 -275
- package/src/dev/remote.tsx +366 -224
- package/src/dev/use-esbuild.ts +126 -91
- package/src/dev.tsx +538 -0
- package/src/dialogs.tsx +97 -97
- package/src/durable.ts +87 -87
- package/src/entry.ts +234 -228
- package/src/environment-variables.ts +23 -23
- package/src/errors.ts +6 -6
- package/src/generate.ts +33 -0
- package/src/git-client.ts +42 -0
- package/src/https-options.ts +79 -79
- package/src/index.tsx +1775 -2763
- package/src/init.ts +549 -0
- package/src/inspect.ts +593 -593
- package/src/intl-polyfill.d.ts +123 -123
- package/src/is-interactive.ts +12 -0
- package/src/kv.ts +277 -277
- package/src/logger.ts +46 -39
- package/src/miniflare-cli/enum-keys.ts +8 -8
- package/src/miniflare-cli/index.ts +42 -31
- package/src/miniflare-cli/request-context.ts +18 -18
- package/src/module-collection.ts +212 -212
- package/src/open-in-browser.ts +4 -6
- package/src/package-manager.ts +123 -123
- package/src/pages/build.tsx +202 -0
- package/src/pages/constants.ts +7 -0
- package/src/pages/deployments.tsx +101 -0
- package/src/pages/dev.tsx +964 -0
- package/src/pages/functions/buildPlugin.ts +105 -0
- package/src/pages/functions/buildWorker.ts +151 -0
- package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/index.tsx +84 -0
- package/src/pages/projects.tsx +157 -0
- package/src/pages/publish.tsx +335 -0
- package/src/pages/types.ts +40 -0
- package/src/pages/upload.tsx +384 -0
- package/src/pages/utils.ts +12 -0
- package/src/parse.ts +202 -138
- package/src/paths.ts +6 -6
- package/src/preview.ts +31 -0
- package/src/proxy.ts +400 -402
- package/src/publish.ts +667 -621
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.tsx +577 -0
- package/src/r2.ts +19 -19
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.tsx +271 -225
- package/src/tail/filters.ts +108 -108
- package/src/tail/index.ts +217 -217
- package/src/tail/printing.ts +45 -45
- package/src/update-check.ts +11 -11
- package/src/user/choose-account.tsx +60 -0
- package/src/user/env-vars.ts +46 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +3 -0
- package/src/user/user.tsx +1161 -0
- package/src/whoami.tsx +61 -42
- package/src/worker-namespace.ts +190 -0
- package/src/worker.ts +110 -100
- package/src/zones.ts +39 -36
- package/templates/checked-fetch.js +17 -0
- package/templates/new-worker-scheduled.js +3 -3
- package/templates/new-worker-scheduled.ts +15 -15
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +15 -15
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-template-plugin.ts +155 -0
- package/templates/pages-template-worker.ts +161 -0
- package/templates/static-asset-facade.js +31 -31
- package/templates/tsconfig.json +95 -95
- package/wrangler-dist/cli.js +55383 -54138
- package/pages/functions/buildPlugin.ts +0 -105
- package/pages/functions/buildWorker.ts +0 -151
- package/pages/functions/filepath-routing.ts +0 -189
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -156
- package/pages/functions/template-plugin.ts +0 -147
- package/pages/functions/template-worker.ts +0 -143
- package/src/pages.tsx +0 -2093
- package/src/user.tsx +0 -1214
package/src/publish.ts
CHANGED
|
@@ -4,6 +4,7 @@ import path from "node:path";
|
|
|
4
4
|
import { URLSearchParams } from "node:url";
|
|
5
5
|
import tmp from "tmp-promise";
|
|
6
6
|
import { bundleWorker } from "./bundle";
|
|
7
|
+
import { printBundleSize } from "./bundle-reporter";
|
|
7
8
|
import { fetchListResult, fetchResult } from "./cfetch";
|
|
8
9
|
import { printBindings } from "./config";
|
|
9
10
|
import { createWorkerUploadForm } from "./create-worker-upload-form";
|
|
@@ -15,66 +16,67 @@ import { syncAssets } from "./sites";
|
|
|
15
16
|
import { getZoneForRoute } from "./zones";
|
|
16
17
|
import type { Config } from "./config";
|
|
17
18
|
import type {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
Route,
|
|
20
|
+
ZoneIdRoute,
|
|
21
|
+
ZoneNameRoute,
|
|
22
|
+
CustomDomainRoute,
|
|
22
23
|
} from "./config/environment";
|
|
23
24
|
import type { Entry } from "./entry";
|
|
24
25
|
import type { AssetPaths } from "./sites";
|
|
25
26
|
import type { CfWorkerInit } from "./worker";
|
|
26
27
|
|
|
27
28
|
type Props = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
29
|
+
config: Config;
|
|
30
|
+
accountId: string | undefined;
|
|
31
|
+
entry: Entry;
|
|
32
|
+
rules: Config["rules"];
|
|
33
|
+
name: string | undefined;
|
|
34
|
+
env: string | undefined;
|
|
35
|
+
compatibilityDate: string | undefined;
|
|
36
|
+
compatibilityFlags: string[] | undefined;
|
|
37
|
+
assetPaths: AssetPaths | undefined;
|
|
38
|
+
triggers: string[] | undefined;
|
|
39
|
+
routes: string[] | undefined;
|
|
40
|
+
legacyEnv: boolean | undefined;
|
|
41
|
+
jsxFactory: string | undefined;
|
|
42
|
+
jsxFragment: string | undefined;
|
|
43
|
+
tsconfig: string | undefined;
|
|
44
|
+
isWorkersSite: boolean;
|
|
45
|
+
minify: boolean | undefined;
|
|
46
|
+
nodeCompat: boolean | undefined;
|
|
47
|
+
outDir: string | undefined;
|
|
48
|
+
dryRun: boolean | undefined;
|
|
49
|
+
noBundle: boolean | undefined;
|
|
48
50
|
};
|
|
49
51
|
|
|
50
52
|
type RouteObject = ZoneIdRoute | ZoneNameRoute | CustomDomainRoute;
|
|
51
53
|
|
|
52
54
|
function sleep(ms: number) {
|
|
53
|
-
|
|
55
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
function renderRoute(route: Route): string {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
59
|
+
let result = "";
|
|
60
|
+
if (typeof route === "string") {
|
|
61
|
+
result = route;
|
|
62
|
+
} else {
|
|
63
|
+
result = route.pattern;
|
|
64
|
+
const isCustomDomain = Boolean(
|
|
65
|
+
"custom_domain" in route && route.custom_domain
|
|
66
|
+
);
|
|
67
|
+
if (isCustomDomain && "zone_id" in route) {
|
|
68
|
+
result += ` (custom domain - zone id: ${route.zone_id})`;
|
|
69
|
+
} else if (isCustomDomain && "zone_name" in route) {
|
|
70
|
+
result += ` (custom domain - zone name: ${route.zone_name})`;
|
|
71
|
+
} else if (isCustomDomain) {
|
|
72
|
+
result += ` (custom domain)`;
|
|
73
|
+
} else if ("zone_id" in route) {
|
|
74
|
+
result += ` (zone id: ${route.zone_id})`;
|
|
75
|
+
} else if ("zone_name" in route) {
|
|
76
|
+
result += ` (zone name: ${route.zone_name})`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
// this function takes a string with quotes in it
|
|
@@ -87,28 +89,28 @@ function renderRoute(route: Route): string {
|
|
|
87
89
|
// by a string, which we can use to provide helpful
|
|
88
90
|
// messages to a user
|
|
89
91
|
function getQuoteBoundedSubstring(content: string) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
const matches = content.split('"');
|
|
93
|
+
return matches[1] ?? "";
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
function isOriginConflictError(
|
|
95
|
-
|
|
97
|
+
e: unknown
|
|
96
98
|
): e is { code: 100116; message: string; notes: Array<{ text: string }> } {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
return (
|
|
100
|
+
typeof e === "object" &&
|
|
101
|
+
e !== null &&
|
|
102
|
+
(e as { code: number }).code === 100116
|
|
103
|
+
);
|
|
102
104
|
}
|
|
103
105
|
|
|
104
106
|
function isDNSConflictError(
|
|
105
|
-
|
|
107
|
+
e: unknown
|
|
106
108
|
): e is { code: 100117; message: string; notes: Array<{ text: string }> } {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
return (
|
|
110
|
+
typeof e === "object" &&
|
|
111
|
+
e !== null &&
|
|
112
|
+
(e as { code: number }).code === 100117
|
|
113
|
+
);
|
|
112
114
|
}
|
|
113
115
|
|
|
114
116
|
// empty error class to throw and then explicitly catch via `instanceof`
|
|
@@ -128,489 +130,533 @@ class CustomDomainOverrideRejected extends Error {}
|
|
|
128
130
|
// to these custom domains, but continue on through the rest of the
|
|
129
131
|
// publish stage
|
|
130
132
|
function publishCustomDomains(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
+
workerUrl: string,
|
|
134
|
+
domains: Array<RouteObject>
|
|
133
135
|
): Promise<string[]> {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
136
|
+
const config = {
|
|
137
|
+
override_scope: true,
|
|
138
|
+
override_existing_origin: false,
|
|
139
|
+
override_existing_dns_record: false,
|
|
140
|
+
};
|
|
141
|
+
const origins = domains.map((domainRoute) => {
|
|
142
|
+
return {
|
|
143
|
+
hostname: domainRoute.pattern,
|
|
144
|
+
zone_id: "zone_id" in domainRoute ? domainRoute.zone_id : undefined,
|
|
145
|
+
zone_name: "zone_name" in domainRoute ? domainRoute.zone_name : undefined,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
if (!process.stdout.isTTY) {
|
|
150
|
+
// running in non-interactive mode.
|
|
151
|
+
// existing origins / dns records are not indicative of errors,
|
|
152
|
+
// so we aggressively update rather than aggressively fail
|
|
153
|
+
config.override_existing_origin = true;
|
|
154
|
+
config.override_existing_dns_record = true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Mixing promise chains with async/await is funky, but it allows us to keep related
|
|
158
|
+
// logic synchronous-looking (i.e. prompting for confirmation and then re-requesting)
|
|
159
|
+
// while retaining the flexibility of promise chain fall-throughs. We can group error
|
|
160
|
+
// handling logic in dedicated catch calls, and all we have to do is re-throw an
|
|
161
|
+
// error and it will pass down to the next catch call
|
|
162
|
+
return fetchResult(`${workerUrl}/domains`, {
|
|
163
|
+
method: "PUT",
|
|
164
|
+
body: JSON.stringify({ ...config, origins }),
|
|
165
|
+
headers: {
|
|
166
|
+
"Content-Type": "application/json",
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
.catch(async (err) => {
|
|
170
|
+
if (isOriginConflictError(err)) {
|
|
171
|
+
const conflictingOrigins = getQuoteBoundedSubstring(err.notes[0].text);
|
|
172
|
+
const shouldContinue = await confirm(
|
|
173
|
+
`Custom Domains already exist for these domains: "${conflictingOrigins}"\nUpdate them to point to this script instead?`
|
|
174
|
+
);
|
|
175
|
+
if (!shouldContinue) {
|
|
176
|
+
throw new CustomDomainOverrideRejected();
|
|
177
|
+
}
|
|
178
|
+
config.override_existing_origin = true;
|
|
179
|
+
await fetchResult(`${workerUrl}/domains`, {
|
|
180
|
+
method: "PUT",
|
|
181
|
+
body: JSON.stringify({ ...config, origins }),
|
|
182
|
+
headers: {
|
|
183
|
+
"Content-Type": "application/json",
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
} else {
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
.catch(async (err) => {
|
|
191
|
+
if (isDNSConflictError(err)) {
|
|
192
|
+
const conflictingOrigins = getQuoteBoundedSubstring(err.notes[0].text);
|
|
193
|
+
const shouldContinue = await confirm(
|
|
194
|
+
`You already have conflicting DNS records for these domains: "${conflictingOrigins}"\nUpdate them to point to this script instead?`
|
|
195
|
+
);
|
|
196
|
+
if (!shouldContinue) {
|
|
197
|
+
throw new CustomDomainOverrideRejected();
|
|
198
|
+
}
|
|
199
|
+
config.override_existing_dns_record = true;
|
|
200
|
+
await fetchResult(`${workerUrl}/domains`, {
|
|
201
|
+
method: "PUT",
|
|
202
|
+
body: JSON.stringify({ ...config, origins }),
|
|
203
|
+
headers: {
|
|
204
|
+
"Content-Type": "application/json",
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
} else {
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
.then(() => domains.map((domain) => renderRoute(domain)))
|
|
212
|
+
.catch((err) => {
|
|
213
|
+
if (err instanceof CustomDomainOverrideRejected) {
|
|
214
|
+
return [
|
|
215
|
+
domains.length > 1
|
|
216
|
+
? `Publishing to ${domains.length} Custom Domains was skipped, fix conflicts and try again`
|
|
217
|
+
: `Publishing to Custom Domain "${domains[0].pattern}" was skipped, fix conflict and try again`,
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
throw err;
|
|
221
|
+
});
|
|
220
222
|
}
|
|
221
223
|
|
|
222
224
|
export default async function publish(props: Props): Promise<void> {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
225
|
+
// TODO: warn if git/hg has uncommitted changes
|
|
226
|
+
const { config, accountId } = props;
|
|
227
|
+
|
|
228
|
+
if (!(props.compatibilityDate || config.compatibility_date)) {
|
|
229
|
+
const compatibilityDateStr = `${new Date().getFullYear()}-${(
|
|
230
|
+
new Date().getMonth() + ""
|
|
231
|
+
).padStart(2, "0")}-${(new Date().getDate() + "").padStart(2, "0")}`;
|
|
232
|
+
|
|
233
|
+
throw new Error(`A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
|
|
234
|
+
\`\`\`
|
|
235
|
+
compatibility_date = "${compatibilityDateStr}"
|
|
236
|
+
\`\`\`
|
|
237
|
+
Or you could pass it in your terminal as \`--compatibility-date ${compatibilityDateStr}\`
|
|
238
|
+
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const triggers = props.triggers || config.triggers?.crons;
|
|
242
|
+
const routes =
|
|
243
|
+
props.routes ?? config.routes ?? (config.route ? [config.route] : []) ?? [];
|
|
244
|
+
const routesOnly: Array<Route> = [];
|
|
245
|
+
const customDomainsOnly: Array<RouteObject> = [];
|
|
246
|
+
for (const route of routes) {
|
|
247
|
+
if (typeof route !== "string" && route.custom_domain) {
|
|
248
|
+
if (route.pattern.includes("*")) {
|
|
249
|
+
throw new Error(
|
|
250
|
+
`Cannot use "${route.pattern}" as a Custom Domain; wildcard operators (*) are not allowed`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (route.pattern.includes("/")) {
|
|
254
|
+
throw new Error(
|
|
255
|
+
`Cannot use "${route.pattern}" as a Custom Domain; paths are not allowed`
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
customDomainsOnly.push(route);
|
|
259
|
+
} else {
|
|
260
|
+
routesOnly.push(route);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// deployToWorkersDev defaults to true only if there aren't any routes defined
|
|
265
|
+
const deployToWorkersDev = config.workers_dev ?? routes.length === 0;
|
|
266
|
+
|
|
267
|
+
const jsxFactory = props.jsxFactory || config.jsx_factory;
|
|
268
|
+
const jsxFragment = props.jsxFragment || config.jsx_fragment;
|
|
269
|
+
|
|
270
|
+
const minify = props.minify ?? config.minify;
|
|
271
|
+
|
|
272
|
+
const nodeCompat = props.nodeCompat ?? config.node_compat;
|
|
273
|
+
if (nodeCompat) {
|
|
274
|
+
logger.warn(
|
|
275
|
+
"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."
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const scriptName = props.name;
|
|
280
|
+
assert(
|
|
281
|
+
scriptName,
|
|
282
|
+
'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>"`'
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
assert(
|
|
286
|
+
!config.site || config.site.bucket,
|
|
287
|
+
"A [site] definition requires a `bucket` field with a path to the site's assets directory."
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
if (props.outDir) {
|
|
291
|
+
// we're using a custom output directory,
|
|
292
|
+
// so let's first ensure it exists
|
|
293
|
+
mkdirSync(props.outDir, { recursive: true });
|
|
294
|
+
// add a README
|
|
295
|
+
const readmePath = path.join(props.outDir, "README.md");
|
|
296
|
+
writeFileSync(
|
|
297
|
+
readmePath,
|
|
298
|
+
`This folder contains the built output assets for the worker "${scriptName}" generated at ${new Date().toISOString()}.`
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const destination = props.outDir ?? (await tmp.dir({ unsafeCleanup: true }));
|
|
303
|
+
const envName = props.env ?? "production";
|
|
304
|
+
|
|
305
|
+
const start = Date.now();
|
|
306
|
+
const notProd = Boolean(!props.legacyEnv && props.env);
|
|
307
|
+
const workerName = notProd ? `${scriptName} (${envName})` : scriptName;
|
|
308
|
+
const workerUrl = notProd
|
|
309
|
+
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
|
|
310
|
+
: `/accounts/${accountId}/workers/scripts/${scriptName}`;
|
|
311
|
+
|
|
312
|
+
let available_on_subdomain; // we'll set this later
|
|
313
|
+
|
|
314
|
+
const { format } = props.entry;
|
|
315
|
+
|
|
316
|
+
if (
|
|
317
|
+
!props.isWorkersSite &&
|
|
318
|
+
Boolean(props.assetPaths) &&
|
|
319
|
+
format === "service-worker"
|
|
320
|
+
) {
|
|
321
|
+
throw new Error(
|
|
322
|
+
"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/"
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (config.wasm_modules && format === "modules") {
|
|
327
|
+
throw new Error(
|
|
328
|
+
"You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (config.text_blobs && format === "modules") {
|
|
333
|
+
throw new Error(
|
|
334
|
+
"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"
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (config.data_blobs && format === "modules") {
|
|
339
|
+
throw new Error(
|
|
340
|
+
"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"
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
try {
|
|
344
|
+
if (props.noBundle) {
|
|
345
|
+
// if we're not building, let's just copy the entry to the destination directory
|
|
346
|
+
const destinationDir =
|
|
347
|
+
typeof destination === "string" ? destination : destination.path;
|
|
348
|
+
mkdirSync(destinationDir, { recursive: true });
|
|
349
|
+
writeFileSync(
|
|
350
|
+
path.join(destinationDir, path.basename(props.entry.file)),
|
|
351
|
+
readFileSync(props.entry.file, "utf-8")
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const {
|
|
356
|
+
modules,
|
|
357
|
+
resolvedEntryPointPath,
|
|
358
|
+
bundleType,
|
|
359
|
+
}: Awaited<ReturnType<typeof bundleWorker>> = props.noBundle
|
|
360
|
+
? // we can skip the whole bundling step and mock a bundle here
|
|
361
|
+
{
|
|
362
|
+
modules: [],
|
|
363
|
+
resolvedEntryPointPath: props.entry.file,
|
|
364
|
+
bundleType: props.entry.format === "modules" ? "esm" : "commonjs",
|
|
365
|
+
stop: undefined,
|
|
366
|
+
}
|
|
367
|
+
: await bundleWorker(
|
|
368
|
+
props.entry,
|
|
369
|
+
typeof destination === "string" ? destination : destination.path,
|
|
370
|
+
{
|
|
371
|
+
serveAssetsFromWorker:
|
|
372
|
+
!props.isWorkersSite && Boolean(props.assetPaths),
|
|
373
|
+
jsxFactory,
|
|
374
|
+
jsxFragment,
|
|
375
|
+
rules: props.rules,
|
|
376
|
+
tsconfig: props.tsconfig ?? config.tsconfig,
|
|
377
|
+
minify,
|
|
378
|
+
nodeCompat,
|
|
379
|
+
define: config.define,
|
|
380
|
+
checkFetch: false,
|
|
381
|
+
}
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
const content = readFileSync(resolvedEntryPointPath, {
|
|
385
|
+
encoding: "utf-8",
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// durable object migrations
|
|
389
|
+
const migrations = !props.dryRun
|
|
390
|
+
? await getMigrationsToUpload(scriptName, {
|
|
391
|
+
accountId,
|
|
392
|
+
config,
|
|
393
|
+
legacyEnv: props.legacyEnv,
|
|
394
|
+
env: props.env,
|
|
395
|
+
})
|
|
396
|
+
: undefined;
|
|
397
|
+
|
|
398
|
+
const assets = await syncAssets(
|
|
399
|
+
accountId,
|
|
400
|
+
// When we're using the newer service environments, we wouldn't
|
|
401
|
+
// have added the env name on to the script name. However, we must
|
|
402
|
+
// include it in the kv namespace name regardless (since there's no
|
|
403
|
+
// concept of service environments for kv namespaces yet).
|
|
404
|
+
scriptName + (!props.legacyEnv && props.env ? `-${props.env}` : ""),
|
|
405
|
+
props.assetPaths,
|
|
406
|
+
false,
|
|
407
|
+
props.dryRun
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
const bindings: CfWorkerInit["bindings"] = {
|
|
411
|
+
kv_namespaces: (config.kv_namespaces || []).concat(
|
|
412
|
+
assets.namespace
|
|
413
|
+
? { binding: "__STATIC_CONTENT", id: assets.namespace }
|
|
414
|
+
: []
|
|
415
|
+
),
|
|
416
|
+
vars: config.vars,
|
|
417
|
+
wasm_modules: config.wasm_modules,
|
|
418
|
+
text_blobs: {
|
|
419
|
+
...config.text_blobs,
|
|
420
|
+
...(assets.manifest &&
|
|
421
|
+
format === "service-worker" && {
|
|
422
|
+
__STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
|
|
423
|
+
}),
|
|
424
|
+
},
|
|
425
|
+
data_blobs: config.data_blobs,
|
|
426
|
+
durable_objects: config.durable_objects,
|
|
427
|
+
r2_buckets: config.r2_buckets,
|
|
428
|
+
services: config.services,
|
|
429
|
+
worker_namespaces: config.worker_namespaces,
|
|
430
|
+
unsafe: config.unsafe?.bindings,
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
if (assets.manifest) {
|
|
434
|
+
modules.push({
|
|
435
|
+
name: "__STATIC_CONTENT_MANIFEST",
|
|
436
|
+
content: JSON.stringify(assets.manifest),
|
|
437
|
+
type: "text",
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const worker: CfWorkerInit = {
|
|
442
|
+
name: scriptName,
|
|
443
|
+
main: {
|
|
444
|
+
name: path.basename(resolvedEntryPointPath),
|
|
445
|
+
content: content,
|
|
446
|
+
type: bundleType,
|
|
447
|
+
},
|
|
448
|
+
bindings,
|
|
449
|
+
migrations,
|
|
450
|
+
modules,
|
|
451
|
+
compatibility_date: props.compatibilityDate ?? config.compatibility_date,
|
|
452
|
+
compatibility_flags:
|
|
453
|
+
props.compatibilityFlags ?? config.compatibility_flags,
|
|
454
|
+
usage_model: config.usage_model,
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
void printBundleSize(
|
|
458
|
+
{ name: path.basename(resolvedEntryPointPath), content: content },
|
|
459
|
+
modules
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
const withoutStaticAssets = {
|
|
463
|
+
...bindings,
|
|
464
|
+
kv_namespaces: config.kv_namespaces,
|
|
465
|
+
text_blobs: config.text_blobs,
|
|
466
|
+
};
|
|
467
|
+
printBindings(withoutStaticAssets);
|
|
468
|
+
|
|
469
|
+
if (!props.dryRun) {
|
|
470
|
+
// Upload the script so it has time to propagate.
|
|
471
|
+
// We can also now tell whether available_on_subdomain is set
|
|
472
|
+
available_on_subdomain = (
|
|
473
|
+
await fetchResult<{ available_on_subdomain: boolean }>(
|
|
474
|
+
workerUrl,
|
|
475
|
+
{
|
|
476
|
+
method: "PUT",
|
|
477
|
+
body: createWorkerUploadForm(worker),
|
|
478
|
+
},
|
|
479
|
+
new URLSearchParams({
|
|
480
|
+
include_subdomain_availability: "true",
|
|
481
|
+
// pass excludeScript so the whole body of the
|
|
482
|
+
// script doesn't get included in the response
|
|
483
|
+
excludeScript: "true",
|
|
484
|
+
})
|
|
485
|
+
)
|
|
486
|
+
).available_on_subdomain;
|
|
487
|
+
}
|
|
488
|
+
} finally {
|
|
489
|
+
if (typeof destination !== "string") {
|
|
490
|
+
// this means we're using a temp dir,
|
|
491
|
+
// so let's clean up before we proceed
|
|
492
|
+
await destination.cleanup();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (props.dryRun) {
|
|
497
|
+
logger.log(`--dry-run: exiting now.`);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
assert(accountId, "Missing accountId");
|
|
501
|
+
|
|
502
|
+
const uploadMs = Date.now() - start;
|
|
503
|
+
const deployments: Promise<string[]>[] = [];
|
|
504
|
+
|
|
505
|
+
if (deployToWorkersDev) {
|
|
506
|
+
// Deploy to a subdomain of `workers.dev`
|
|
507
|
+
const userSubdomain = await getSubdomain(accountId);
|
|
508
|
+
const scriptURL =
|
|
509
|
+
props.legacyEnv || !props.env
|
|
510
|
+
? `${scriptName}.${userSubdomain}.workers.dev`
|
|
511
|
+
: `${envName}.${scriptName}.${userSubdomain}.workers.dev`;
|
|
512
|
+
if (!available_on_subdomain) {
|
|
513
|
+
// Enable the `workers.dev` subdomain.
|
|
514
|
+
deployments.push(
|
|
515
|
+
fetchResult(`${workerUrl}/subdomain`, {
|
|
516
|
+
method: "POST",
|
|
517
|
+
body: JSON.stringify({ enabled: true }),
|
|
518
|
+
headers: {
|
|
519
|
+
"Content-Type": "application/json",
|
|
520
|
+
},
|
|
521
|
+
})
|
|
522
|
+
.then(() => [scriptURL])
|
|
523
|
+
// Add a delay when the subdomain is first created.
|
|
524
|
+
// This is to prevent an issue where a negative cache-hit
|
|
525
|
+
// causes the subdomain to be unavailable for 30 seconds.
|
|
526
|
+
// This is a temporary measure until we fix this on the edge.
|
|
527
|
+
.then(async (url) => {
|
|
528
|
+
await sleep(3000);
|
|
529
|
+
return url;
|
|
530
|
+
})
|
|
531
|
+
);
|
|
532
|
+
} else {
|
|
533
|
+
deployments.push(Promise.resolve([scriptURL]));
|
|
534
|
+
}
|
|
535
|
+
} else {
|
|
536
|
+
if (available_on_subdomain) {
|
|
537
|
+
// Disable the workers.dev deployment
|
|
538
|
+
await fetchResult(`${workerUrl}/subdomain`, {
|
|
539
|
+
method: "POST",
|
|
540
|
+
body: JSON.stringify({ enabled: false }),
|
|
541
|
+
headers: {
|
|
542
|
+
"Content-Type": "application/json",
|
|
543
|
+
},
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
logger.log("Uploaded", workerName, formatTime(uploadMs));
|
|
549
|
+
|
|
550
|
+
// Update routing table for the script.
|
|
551
|
+
if (routesOnly.length > 0) {
|
|
552
|
+
deployments.push(
|
|
553
|
+
publishRoutes(routesOnly, { workerUrl, scriptName, notProd }).then(() => {
|
|
554
|
+
if (routesOnly.length > 10) {
|
|
555
|
+
return routesOnly
|
|
556
|
+
.slice(0, 9)
|
|
557
|
+
.map((route) => renderRoute(route))
|
|
558
|
+
.concat([`...and ${routesOnly.length - 10} more routes`]);
|
|
559
|
+
}
|
|
560
|
+
return routesOnly.map((route) => renderRoute(route));
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Update custom domains for the script
|
|
566
|
+
if (customDomainsOnly.length > 0) {
|
|
567
|
+
deployments.push(publishCustomDomains(workerUrl, customDomainsOnly));
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Configure any schedules for the script.
|
|
571
|
+
// TODO: rename this to `schedules`?
|
|
572
|
+
if (triggers && triggers.length) {
|
|
573
|
+
deployments.push(
|
|
574
|
+
fetchResult(`${workerUrl}/schedules`, {
|
|
575
|
+
// Note: PUT will override previous schedules on this script.
|
|
576
|
+
method: "PUT",
|
|
577
|
+
body: JSON.stringify(triggers.map((cron) => ({ cron }))),
|
|
578
|
+
headers: {
|
|
579
|
+
"Content-Type": "application/json",
|
|
580
|
+
},
|
|
581
|
+
}).then(() => triggers.map((trigger) => `schedule: ${trigger}`))
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const targets = await Promise.all(deployments);
|
|
586
|
+
const deployMs = Date.now() - start - uploadMs;
|
|
587
|
+
|
|
588
|
+
if (deployments.length > 0) {
|
|
589
|
+
logger.log("Published", workerName, formatTime(deployMs));
|
|
590
|
+
for (const target of targets.flat()) {
|
|
591
|
+
logger.log(" ", target);
|
|
592
|
+
}
|
|
593
|
+
} else {
|
|
594
|
+
logger.log("No publish targets for", workerName, formatTime(deployMs));
|
|
595
|
+
}
|
|
550
596
|
}
|
|
551
597
|
|
|
552
598
|
function formatTime(duration: number) {
|
|
553
|
-
|
|
599
|
+
return `(${(duration / 1000).toFixed(2)} sec)`;
|
|
554
600
|
}
|
|
555
601
|
|
|
556
602
|
async function getSubdomain(accountId: string): Promise<string> {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
603
|
+
try {
|
|
604
|
+
const { subdomain } = await fetchResult(
|
|
605
|
+
`/accounts/${accountId}/workers/subdomain`
|
|
606
|
+
);
|
|
607
|
+
return subdomain;
|
|
608
|
+
} catch (e) {
|
|
609
|
+
const error = e as { code?: number };
|
|
610
|
+
if (typeof error === "object" && !!error && error.code === 10007) {
|
|
611
|
+
// 10007 error code: not found
|
|
612
|
+
// https://api.cloudflare.com/#worker-subdomain-get-subdomain
|
|
613
|
+
|
|
614
|
+
const errorMessage =
|
|
615
|
+
"Error: You need to register a workers.dev subdomain before publishing to workers.dev";
|
|
616
|
+
const solutionMessage =
|
|
617
|
+
"You can either publish your worker to one or more routes by specifying them in wrangler.toml, or register a workers.dev subdomain here:";
|
|
618
|
+
const onboardingLink = `https://dash.cloudflare.com/${accountId}/workers/onboarding`;
|
|
619
|
+
|
|
620
|
+
throw new Error(`${errorMessage}\n${solutionMessage}\n${onboardingLink}`);
|
|
621
|
+
} else {
|
|
622
|
+
throw e;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
579
625
|
}
|
|
580
626
|
|
|
581
627
|
/**
|
|
582
628
|
* Associate the newly deployed Worker with the given routes.
|
|
583
629
|
*/
|
|
584
630
|
async function publishRoutes(
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
631
|
+
routes: Route[],
|
|
632
|
+
{
|
|
633
|
+
workerUrl,
|
|
634
|
+
scriptName,
|
|
635
|
+
notProd,
|
|
636
|
+
}: { workerUrl: string; scriptName: string; notProd: boolean }
|
|
591
637
|
): Promise<string[]> {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
638
|
+
try {
|
|
639
|
+
return await fetchResult(`${workerUrl}/routes`, {
|
|
640
|
+
// Note: PUT will delete previous routes on this script.
|
|
641
|
+
method: "PUT",
|
|
642
|
+
body: JSON.stringify(
|
|
643
|
+
routes.map((route) =>
|
|
644
|
+
typeof route !== "object" ? { pattern: route } : route
|
|
645
|
+
)
|
|
646
|
+
),
|
|
647
|
+
headers: {
|
|
648
|
+
"Content-Type": "application/json",
|
|
649
|
+
},
|
|
650
|
+
});
|
|
651
|
+
} catch (e) {
|
|
652
|
+
if (isAuthenticationError(e)) {
|
|
653
|
+
// An authentication error is probably due to a known issue,
|
|
654
|
+
// where the user is logged in via an API token that does not have "All Zones".
|
|
655
|
+
return await publishRoutesFallback(routes, { scriptName, notProd });
|
|
656
|
+
} else {
|
|
657
|
+
throw e;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
614
660
|
}
|
|
615
661
|
|
|
616
662
|
/**
|
|
@@ -619,103 +665,103 @@ async function publishRoutes(
|
|
|
619
665
|
* Compute match zones to the routes, then for each route attempt to connect it to the Worker via the zone.
|
|
620
666
|
*/
|
|
621
667
|
async function publishRoutesFallback(
|
|
622
|
-
|
|
623
|
-
|
|
668
|
+
routes: Route[],
|
|
669
|
+
{ scriptName, notProd }: { scriptName: string; notProd: boolean }
|
|
624
670
|
) {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
671
|
+
if (notProd) {
|
|
672
|
+
throw new Error(
|
|
673
|
+
"Service environments combined with an API token that doesn't have 'All Zones' permissions is not supported.\n" +
|
|
674
|
+
"Either turn off service environments by setting `legacy_env = true`, creating an API token with 'All Zones' permissions, or logging in via OAuth"
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
logger.warn(
|
|
678
|
+
"The current authentication token does not have 'All Zones' permissions.\n" +
|
|
679
|
+
"Falling back to using the zone-based API endpoint to update each route individually.\n" +
|
|
680
|
+
"Note that there is no access to routes associated with zones that the API token does not have permission for.\n" +
|
|
681
|
+
"Existing routes for this Worker in such zones will not be deleted."
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
const deployedRoutes: string[] = [];
|
|
685
|
+
|
|
686
|
+
// Collect the routes (and their zones) that will be deployed.
|
|
687
|
+
const activeZones = new Map<string, string>();
|
|
688
|
+
const routesToDeploy = new Map<string, string>();
|
|
689
|
+
for (const route of routes) {
|
|
690
|
+
const zone = await getZoneForRoute(route);
|
|
691
|
+
if (zone) {
|
|
692
|
+
activeZones.set(zone.id, zone.host);
|
|
693
|
+
routesToDeploy.set(
|
|
694
|
+
typeof route === "string" ? route : route.pattern,
|
|
695
|
+
zone.id
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
// Collect the routes that are already deployed.
|
|
701
|
+
const allRoutes = new Map<string, string>();
|
|
702
|
+
const alreadyDeployedRoutes = new Set<string>();
|
|
703
|
+
for (const [zone, host] of activeZones) {
|
|
704
|
+
try {
|
|
705
|
+
for (const { pattern, script } of await fetchListResult<{
|
|
706
|
+
pattern: string;
|
|
707
|
+
script: string;
|
|
708
|
+
}>(`/zones/${zone}/workers/routes`)) {
|
|
709
|
+
allRoutes.set(pattern, script);
|
|
710
|
+
if (script === scriptName) {
|
|
711
|
+
alreadyDeployedRoutes.add(pattern);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
} catch (e) {
|
|
715
|
+
if (isAuthenticationError(e)) {
|
|
716
|
+
e.notes.push({
|
|
717
|
+
text: `This could be because the API token being used does not have permission to access the zone "${host}" (${zone}).`,
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
throw e;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// Deploy each route that is not already deployed.
|
|
725
|
+
for (const [routePattern, zoneId] of routesToDeploy.entries()) {
|
|
726
|
+
if (allRoutes.has(routePattern)) {
|
|
727
|
+
const knownScript = allRoutes.get(routePattern);
|
|
728
|
+
if (knownScript === scriptName) {
|
|
729
|
+
// This route is already associated with this worker, so no need to hit the API.
|
|
730
|
+
alreadyDeployedRoutes.delete(routePattern);
|
|
731
|
+
continue;
|
|
732
|
+
} else {
|
|
733
|
+
throw new Error(
|
|
734
|
+
`The route with pattern "${routePattern}" is already associated with another worker called "${knownScript}".`
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
const { pattern } = await fetchResult(`/zones/${zoneId}/workers/routes`, {
|
|
740
|
+
method: "POST",
|
|
741
|
+
body: JSON.stringify({
|
|
742
|
+
pattern: routePattern,
|
|
743
|
+
script: scriptName,
|
|
744
|
+
}),
|
|
745
|
+
headers: {
|
|
746
|
+
"Content-Type": "application/json",
|
|
747
|
+
},
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
deployedRoutes.push(pattern);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
if (alreadyDeployedRoutes.size) {
|
|
754
|
+
logger.warn(
|
|
755
|
+
"Previously deployed routes:\n" +
|
|
756
|
+
"The following routes were already associated with this worker, and have not been deleted:\n" +
|
|
757
|
+
[...alreadyDeployedRoutes.values()].map((route) => ` - "${route}"\n`) +
|
|
758
|
+
"If these routes are not wanted then you can remove them in the dashboard."
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
return deployedRoutes;
|
|
717
763
|
}
|
|
718
764
|
|
|
719
765
|
function isAuthenticationError(e: unknown): e is ParseError {
|
|
720
|
-
|
|
766
|
+
return e instanceof ParseError && (e as { code?: number }).code === 10000;
|
|
721
767
|
}
|