wrangler 2.0.9 → 2.0.14

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 (61) hide show
  1. package/kv-asset-handler.js +1 -0
  2. package/package.json +4 -2
  3. package/src/__tests__/configuration.test.ts +255 -142
  4. package/src/__tests__/dev.test.tsx +27 -0
  5. package/src/__tests__/index.test.ts +2 -1
  6. package/src/__tests__/jest.setup.ts +30 -0
  7. package/src/__tests__/publish.test.ts +393 -160
  8. package/src/__tests__/user.test.ts +1 -0
  9. package/src/bundle.ts +9 -5
  10. package/src/config/environment.ts +1 -1
  11. package/src/config/validation-helpers.ts +10 -1
  12. package/src/config/validation.ts +22 -13
  13. package/src/dev/dev.tsx +29 -45
  14. package/src/dev/local.tsx +10 -7
  15. package/src/dev/remote.tsx +4 -1
  16. package/src/dev/use-esbuild.ts +1 -4
  17. package/src/generate-auth-url.ts +33 -0
  18. package/src/generate-random-state.ts +16 -0
  19. package/src/index.tsx +234 -179
  20. package/src/open-in-browser.ts +1 -3
  21. package/src/pages.tsx +295 -240
  22. package/src/parse.ts +2 -1
  23. package/src/proxy.ts +19 -6
  24. package/src/publish.ts +6 -1
  25. package/src/sites.tsx +49 -18
  26. package/src/user.tsx +12 -24
  27. package/templates/static-asset-facade.js +2 -6
  28. package/wrangler-dist/cli.js +73627 -73462
  29. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  30. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  31. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  32. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  33. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  34. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  35. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  36. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  37. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  38. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  39. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  40. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  41. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  42. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  43. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  44. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  45. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  46. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  47. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  48. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  49. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  50. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  51. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  52. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  53. package/vendor/wrangler-mime/LICENSE +0 -21
  54. package/vendor/wrangler-mime/Mime.js +0 -97
  55. package/vendor/wrangler-mime/README.md +0 -187
  56. package/vendor/wrangler-mime/cli.js +0 -46
  57. package/vendor/wrangler-mime/index.js +0 -4
  58. package/vendor/wrangler-mime/lite.js +0 -4
  59. package/vendor/wrangler-mime/package.json +0 -52
  60. package/vendor/wrangler-mime/types/other.js +0 -1
  61. package/vendor/wrangler-mime/types/standard.js +0 -1
package/src/index.tsx CHANGED
@@ -4,6 +4,7 @@ import path from "node:path";
4
4
  import { setTimeout } from "node:timers/promises";
5
5
  import TOML from "@iarna/toml";
6
6
  import chalk from "chalk";
7
+ import { watch } from "chokidar";
7
8
  import { execa } from "execa";
8
9
  import { findUp } from "find-up";
9
10
  import getPort from "get-port";
@@ -1035,197 +1036,235 @@ function createCLIParser(argv: string[]) {
1035
1036
  });
1036
1037
  },
1037
1038
  async (args) => {
1038
- await printWranglerBanner();
1039
- const configPath =
1040
- (args.config as ConfigPath) ||
1041
- (args.script && findWranglerToml(path.dirname(args.script)));
1042
- const config = readConfig(configPath, args);
1043
- const entry = await getEntry(args, config, "dev");
1044
-
1045
- if (config.services && config.services.length > 0) {
1046
- logger.warn(
1047
- `This worker is bound to live services: ${config.services
1048
- .map(
1049
- (service) =>
1050
- `${service.binding} (${service.service}${
1051
- service.environment ? `@${service.environment}` : ""
1052
- })`
1053
- )
1054
- .join(", ")}`
1055
- );
1056
- }
1057
-
1058
- if (args.inspect) {
1059
- logger.warn(
1060
- "Passing --inspect is unnecessary, now you can always connect to devtools."
1061
- );
1062
- }
1039
+ let watcher: ReturnType<typeof watch> | undefined;
1040
+ try {
1041
+ await printWranglerBanner();
1042
+ const configPath =
1043
+ (args.config as ConfigPath) ||
1044
+ ((args.script &&
1045
+ findWranglerToml(path.dirname(args.script))) as ConfigPath);
1046
+ let config = readConfig(configPath, args);
1047
+
1048
+ if (config.configPath) {
1049
+ watcher = watch(config.configPath, {
1050
+ persistent: true,
1051
+ }).on("change", async (_event) => {
1052
+ // TODO: Do we need to handle different `_event` types differently?
1053
+ // e.g. what if the file is deleted, or added?
1054
+ config = readConfig(configPath, args);
1055
+ if (config.configPath) {
1056
+ logger.log(`${path.basename(config.configPath)} changed...`);
1057
+ rerender(await getDevReactElement(config));
1058
+ }
1059
+ });
1060
+ }
1063
1061
 
1064
- if (args["experimental-public"]) {
1065
- logger.warn(
1066
- "The --experimental-public field is experimental and will change in the future."
1067
- );
1068
- }
1062
+ const entry = await getEntry(args, config, "dev");
1069
1063
 
1070
- if (args.public) {
1071
- throw new Error(
1072
- "The --public field has been renamed to --experimental-public, and will change behaviour in the future."
1073
- );
1074
- }
1064
+ if (config.services && config.services.length > 0) {
1065
+ logger.warn(
1066
+ `This worker is bound to live services: ${config.services
1067
+ .map(
1068
+ (service) =>
1069
+ `${service.binding} (${service.service}${
1070
+ service.environment ? `@${service.environment}` : ""
1071
+ })`
1072
+ )
1073
+ .join(", ")}`
1074
+ );
1075
+ }
1075
1076
 
1076
- const upstreamProtocol =
1077
- args["upstream-protocol"] || config.dev.upstream_protocol;
1078
- if (upstreamProtocol === "http") {
1079
- logger.warn(
1080
- "Setting upstream-protocol to http is not currently implemented.\n" +
1081
- "If this is required in your project, please add your use case to the following issue:\n" +
1082
- "https://github.com/cloudflare/wrangler2/issues/583."
1083
- );
1084
- }
1077
+ if (args.inspect) {
1078
+ logger.warn(
1079
+ "Passing --inspect is unnecessary, now you can always connect to devtools."
1080
+ );
1081
+ }
1085
1082
 
1086
- // TODO: if worker_dev = false and no routes, then error (only for dev)
1083
+ if (args["experimental-public"]) {
1084
+ logger.warn(
1085
+ "The --experimental-public field is experimental and will change in the future."
1086
+ );
1087
+ }
1087
1088
 
1088
- // Compute zone info from the `host` and `route` args and config;
1089
- let host = args.host || config.dev.host;
1090
- let zoneId: string | undefined;
1089
+ if (args["experimental-public"] && (args.site || config.site)) {
1090
+ throw new Error(
1091
+ "Cannot use --experimental-public and a Site configuration together."
1092
+ );
1093
+ }
1091
1094
 
1092
- if (!args.local) {
1093
- if (host) {
1094
- zoneId = await getZoneIdFromHost(host);
1095
+ if (args.public) {
1096
+ throw new Error(
1097
+ "The --public field has been renamed to --experimental-public, and will change behaviour in the future."
1098
+ );
1095
1099
  }
1096
- const routes = args.routes || config.route || config.routes;
1097
- if (!zoneId && routes) {
1098
- const firstRoute = Array.isArray(routes) ? routes[0] : routes;
1099
- const zone = await getZoneForRoute(firstRoute);
1100
- if (zone) {
1101
- zoneId = zone.id;
1102
- host = zone.host;
1103
- }
1100
+
1101
+ const upstreamProtocol =
1102
+ args["upstream-protocol"] || config.dev.upstream_protocol;
1103
+ if (upstreamProtocol === "http") {
1104
+ logger.warn(
1105
+ "Setting upstream-protocol to http is not currently implemented.\n" +
1106
+ "If this is required in your project, please add your use case to the following issue:\n" +
1107
+ "https://github.com/cloudflare/wrangler2/issues/583."
1108
+ );
1104
1109
  }
1105
- }
1106
1110
 
1107
- const nodeCompat = args.nodeCompat ?? config.node_compat;
1108
- if (nodeCompat) {
1109
- logger.warn(
1110
- "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."
1111
- );
1112
- }
1111
+ // TODO: if worker_dev = false and no routes, then error (only for dev)
1113
1112
 
1114
- const bindings = {
1115
- kv_namespaces: config.kv_namespaces?.map(
1116
- ({ binding, preview_id, id: _id }) => {
1117
- // In `dev`, we make folks use a separate kv namespace called
1118
- // `preview_id` instead of `id` so that they don't
1119
- // break production data. So here we check that a `preview_id`
1120
- // has actually been configured.
1121
- // This whole block of code will be obsoleted in the future
1122
- // when we have copy-on-write for previews on edge workers.
1123
- if (!preview_id) {
1124
- // TODO: This error has to be a _lot_ better, ideally just asking
1125
- // to create a preview namespace for the user automatically
1126
- throw new Error(
1127
- `In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "${binding}" in your wrangler.toml`
1128
- ); // Ugh, I really don't like this message very much
1129
- }
1130
- return {
1131
- binding,
1132
- id: preview_id,
1133
- };
1113
+ // Compute zone info from the `host` and `route` args and config;
1114
+ let host = args.host || config.dev.host;
1115
+ let zoneId: string | undefined;
1116
+
1117
+ if (!args.local) {
1118
+ if (host) {
1119
+ zoneId = await getZoneIdFromHost(host);
1134
1120
  }
1135
- ),
1136
- // Use a copy of combinedVars since we're modifying it later
1137
- vars: getVarsForDev(config),
1138
- wasm_modules: config.wasm_modules,
1139
- text_blobs: config.text_blobs,
1140
- data_blobs: config.data_blobs,
1141
- durable_objects: config.durable_objects,
1142
- r2_buckets: config.r2_buckets?.map(
1143
- ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
1144
- // same idea as kv namespace preview id,
1145
- // same copy-on-write TODO
1146
- if (!preview_bucket_name) {
1147
- throw new Error(
1148
- `In development, you should use a separate r2 bucket than the one you'd use in production. Please create a new r2 bucket with "wrangler r2 bucket create <name>" and add its name as preview_bucket_name to the r2_buckets "${binding}" in your wrangler.toml`
1149
- );
1121
+ const routes = args.routes || config.route || config.routes;
1122
+ if (!zoneId && routes) {
1123
+ const firstRoute = Array.isArray(routes) ? routes[0] : routes;
1124
+ const zone = await getZoneForRoute(firstRoute);
1125
+ if (zone) {
1126
+ zoneId = zone.id;
1127
+ host = zone.host;
1150
1128
  }
1151
- return {
1152
- binding,
1153
- bucket_name: preview_bucket_name,
1154
- };
1155
1129
  }
1156
- ),
1157
- services: config.services,
1158
- unsafe: config.unsafe?.bindings,
1159
- };
1130
+ }
1160
1131
 
1161
- // mask anything that was overridden in .dev.vars
1162
- // so that we don't log potential secrets into the terminal
1163
- const maskedVars = { ...bindings.vars };
1164
- for (const key of Object.keys(maskedVars)) {
1165
- if (maskedVars[key] !== config.vars[key]) {
1166
- // This means it was overridden in .dev.vars
1167
- // so let's mask it
1168
- maskedVars[key] = "(hidden)";
1132
+ const nodeCompat = args.nodeCompat ?? config.node_compat;
1133
+ if (nodeCompat) {
1134
+ logger.warn(
1135
+ "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."
1136
+ );
1169
1137
  }
1170
- }
1171
1138
 
1172
- // now log all available bindings into the terminal
1173
- printBindings({
1174
- ...bindings,
1175
- vars: maskedVars,
1176
- });
1139
+ // eslint-disable-next-line no-inner-declarations
1140
+ async function getBindings(configParam: Config) {
1141
+ return {
1142
+ kv_namespaces: configParam.kv_namespaces?.map(
1143
+ ({ binding, preview_id, id: _id }) => {
1144
+ // In `dev`, we make folks use a separate kv namespace called
1145
+ // `preview_id` instead of `id` so that they don't
1146
+ // break production data. So here we check that a `preview_id`
1147
+ // has actually been configured.
1148
+ // This whole block of code will be obsoleted in the future
1149
+ // when we have copy-on-write for previews on edge workers.
1150
+ if (!preview_id) {
1151
+ // TODO: This error has to be a _lot_ better, ideally just asking
1152
+ // to create a preview namespace for the user automatically
1153
+ throw new Error(
1154
+ `In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "${binding}" in your wrangler.toml`
1155
+ ); // Ugh, I really don't like this message very much
1156
+ }
1157
+ return {
1158
+ binding,
1159
+ id: preview_id,
1160
+ };
1161
+ }
1162
+ ),
1163
+ // Use a copy of combinedVars since we're modifying it later
1164
+ vars: getVarsForDev(configParam),
1165
+ wasm_modules: configParam.wasm_modules,
1166
+ text_blobs: configParam.text_blobs,
1167
+ data_blobs: configParam.data_blobs,
1168
+ durable_objects: configParam.durable_objects,
1169
+ r2_buckets: configParam.r2_buckets?.map(
1170
+ ({ binding, preview_bucket_name, bucket_name: _bucket_name }) => {
1171
+ // same idea as kv namespace preview id,
1172
+ // same copy-on-write TODO
1173
+ if (!preview_bucket_name) {
1174
+ throw new Error(
1175
+ `In development, you should use a separate r2 bucket than the one you'd use in production. Please create a new r2 bucket with "wrangler r2 bucket create <name>" and add its name as preview_bucket_name to the r2_buckets "${binding}" in your wrangler.toml`
1176
+ );
1177
+ }
1178
+ return {
1179
+ binding,
1180
+ bucket_name: preview_bucket_name,
1181
+ };
1182
+ }
1183
+ ),
1184
+ services: configParam.services,
1185
+ unsafe: configParam.unsafe?.bindings,
1186
+ };
1187
+ }
1177
1188
 
1178
- const { waitUntilExit } = render(
1179
- <Dev
1180
- name={getScriptName(args, config)}
1181
- entry={entry}
1182
- env={args.env}
1183
- zone={zoneId}
1184
- host={host}
1185
- rules={getRules(config)}
1186
- legacyEnv={isLegacyEnv(config)}
1187
- minify={args.minify ?? config.minify}
1188
- nodeCompat={nodeCompat}
1189
- build={config.build || {}}
1190
- initialMode={args.local ? "local" : "remote"}
1191
- jsxFactory={args["jsx-factory"] || config.jsx_factory}
1192
- jsxFragment={args["jsx-fragment"] || config.jsx_fragment}
1193
- tsconfig={args.tsconfig ?? config.tsconfig}
1194
- upstreamProtocol={upstreamProtocol}
1195
- localProtocol={args["local-protocol"] || config.dev.local_protocol}
1196
- enableLocalPersistence={
1197
- args["experimental-enable-local-persistence"] || false
1198
- }
1199
- accountId={config.account_id}
1200
- assetPaths={getAssetPaths(
1201
- config,
1202
- args.site,
1203
- args.siteInclude,
1204
- args.siteExclude
1205
- )}
1206
- port={
1207
- args.port ||
1208
- config.dev.port ||
1209
- (await getPort({ port: DEFAULT_LOCAL_PORT }))
1210
- }
1211
- ip={args.ip || config.dev.ip}
1212
- inspectorPort={
1213
- args["inspector-port"] ?? (await getPort({ port: 9229 }))
1214
- }
1215
- public={args["experimental-public"]}
1216
- compatibilityDate={getDevCompatibilityDate(
1217
- config,
1218
- args["compatibility-date"]
1219
- )}
1220
- compatibilityFlags={
1221
- args["compatibility-flags"] || config.compatibility_flags
1189
+ const getLocalPort = memoizeGetPort(DEFAULT_LOCAL_PORT);
1190
+ const getInspectorPort = memoizeGetPort(9229);
1191
+
1192
+ // eslint-disable-next-line no-inner-declarations
1193
+ async function getDevReactElement(configParam: Config) {
1194
+ // now log all available bindings into the terminal
1195
+ const bindings = await getBindings(configParam);
1196
+ // mask anything that was overridden in .dev.vars
1197
+ // so that we don't log potential secrets into the terminal
1198
+ const maskedVars = { ...bindings.vars };
1199
+ for (const key of Object.keys(maskedVars)) {
1200
+ if (maskedVars[key] !== configParam.vars[key]) {
1201
+ // This means it was overridden in .dev.vars
1202
+ // so let's mask it
1203
+ maskedVars[key] = "(hidden)";
1204
+ }
1222
1205
  }
1223
- usageModel={config.usage_model}
1224
- bindings={bindings}
1225
- crons={config.triggers.crons}
1226
- />
1227
- );
1228
- await waitUntilExit();
1206
+
1207
+ printBindings({
1208
+ ...bindings,
1209
+ vars: maskedVars,
1210
+ });
1211
+
1212
+ return (
1213
+ <Dev
1214
+ name={getScriptName(args, config)}
1215
+ entry={entry}
1216
+ env={args.env}
1217
+ zone={zoneId}
1218
+ host={host}
1219
+ rules={getRules(config)}
1220
+ legacyEnv={isLegacyEnv(config)}
1221
+ minify={args.minify ?? config.minify}
1222
+ nodeCompat={nodeCompat}
1223
+ build={config.build || {}}
1224
+ initialMode={args.local ? "local" : "remote"}
1225
+ jsxFactory={args["jsx-factory"] || config.jsx_factory}
1226
+ jsxFragment={args["jsx-fragment"] || config.jsx_fragment}
1227
+ tsconfig={args.tsconfig ?? config.tsconfig}
1228
+ upstreamProtocol={upstreamProtocol}
1229
+ localProtocol={
1230
+ args["local-protocol"] || config.dev.local_protocol
1231
+ }
1232
+ enableLocalPersistence={
1233
+ args["experimental-enable-local-persistence"] || false
1234
+ }
1235
+ accountId={config.account_id}
1236
+ assetPaths={getAssetPaths(
1237
+ config,
1238
+ args["experimental-public"] || args.site,
1239
+ args.siteInclude,
1240
+ args.siteExclude
1241
+ )}
1242
+ port={args.port || config.dev.port || (await getLocalPort())}
1243
+ ip={args.ip || config.dev.ip}
1244
+ inspectorPort={
1245
+ args["inspector-port"] ?? (await getInspectorPort())
1246
+ }
1247
+ public={args["experimental-public"]}
1248
+ compatibilityDate={getDevCompatibilityDate(
1249
+ config,
1250
+ args["compatibility-date"]
1251
+ )}
1252
+ compatibilityFlags={
1253
+ args["compatibility-flags"] || config.compatibility_flags
1254
+ }
1255
+ usageModel={config.usage_model}
1256
+ bindings={bindings}
1257
+ crons={config.triggers.crons}
1258
+ />
1259
+ );
1260
+ }
1261
+ const { waitUntilExit, rerender } = render(
1262
+ await getDevReactElement(config)
1263
+ );
1264
+ await waitUntilExit();
1265
+ } finally {
1266
+ await watcher?.close();
1267
+ }
1229
1268
  }
1230
1269
  );
1231
1270
 
@@ -1351,23 +1390,29 @@ function createCLIParser(argv: string[]) {
1351
1390
  },
1352
1391
  async (args) => {
1353
1392
  await printWranglerBanner();
1393
+
1394
+ const configPath =
1395
+ (args.config as ConfigPath) ||
1396
+ (args.script && findWranglerToml(path.dirname(args.script)));
1397
+ const config = readConfig(configPath, args);
1398
+ const entry = await getEntry(args, config, "publish");
1399
+
1354
1400
  if (args["experimental-public"]) {
1355
1401
  logger.warn(
1356
1402
  "The --experimental-public field is experimental and will change in the future."
1357
1403
  );
1358
1404
  }
1405
+ if (args["experimental-public"] && (args.site || config.site)) {
1406
+ throw new Error(
1407
+ "Cannot use --experimental-public and a Site configuration together."
1408
+ );
1409
+ }
1359
1410
  if (args.public) {
1360
1411
  throw new Error(
1361
1412
  "The --public field has been renamed to --experimental-public, and will change behaviour in the future."
1362
1413
  );
1363
1414
  }
1364
1415
 
1365
- const configPath =
1366
- (args.config as ConfigPath) ||
1367
- (args.script && findWranglerToml(path.dirname(args.script)));
1368
- const config = readConfig(configPath, args);
1369
- const entry = await getEntry(args, config, "publish");
1370
-
1371
1416
  if (args.latest) {
1372
1417
  logger.warn(
1373
1418
  "Using the latest version of the Workers runtime. To silence this warning, please choose a specific version of the runtime with --compatibility-date, or add a compatibility_date to your wrangler.toml.\n"
@@ -2801,3 +2846,13 @@ function getDevCompatibilityDate(
2801
2846
  }
2802
2847
  return compatibilityDate ?? currentDate;
2803
2848
  }
2849
+
2850
+ /**
2851
+ * Avoiding calling `getPort()` multiple times by memoizing the first result.
2852
+ */
2853
+ function memoizeGetPort(defaultPort: number) {
2854
+ let portValue: number;
2855
+ return async () => {
2856
+ return portValue || (portValue = await getPort({ port: defaultPort }));
2857
+ };
2858
+ }
@@ -10,10 +10,8 @@ import { logger } from "./logger";
10
10
  * @param url the URL to point the browser at
11
11
  */
12
12
  export default async function openInBrowser(url: string): Promise<void> {
13
- const errorMessage = `Failed to open ${url} in a browser`;
14
-
15
13
  const childProcess = await open(url);
16
14
  childProcess.on("error", () => {
17
- logger.warn(errorMessage);
15
+ logger.warn("Failed to open");
18
16
  });
19
17
  }