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