astro 4.10.0 → 4.10.2

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 (43) hide show
  1. package/client.d.ts +0 -4
  2. package/dist/@types/astro.d.ts +1 -1
  3. package/dist/config/index.d.ts +2 -2
  4. package/dist/config/index.js +4 -4
  5. package/dist/container/pipeline.d.ts +1 -1
  6. package/dist/container/pipeline.js +17 -18
  7. package/dist/content/runtime.js +2 -2
  8. package/dist/content/types-generator.js +2 -4
  9. package/dist/core/app/index.js +0 -4
  10. package/dist/core/app/pipeline.d.ts +1 -1
  11. package/dist/core/app/pipeline.js +4 -4
  12. package/dist/core/base-pipeline.d.ts +1 -1
  13. package/dist/core/build/generate.js +2 -1
  14. package/dist/core/build/pipeline.d.ts +1 -1
  15. package/dist/core/build/pipeline.js +4 -4
  16. package/dist/core/config/schema.d.ts +498 -2
  17. package/dist/core/constants.js +1 -1
  18. package/dist/core/dev/dev.js +1 -1
  19. package/dist/core/errors/errors-data.d.ts +11 -0
  20. package/dist/core/errors/errors-data.js +6 -0
  21. package/dist/core/messages.js +2 -2
  22. package/dist/core/render-context.d.ts +1 -1
  23. package/dist/core/render-context.js +25 -22
  24. package/dist/core/routing/params.js +1 -1
  25. package/dist/env/config.d.ts +2 -1
  26. package/dist/env/config.js +4 -0
  27. package/dist/env/constants.d.ts +0 -1
  28. package/dist/env/constants.js +1 -3
  29. package/dist/env/schema.d.ts +264 -210
  30. package/dist/env/schema.js +47 -63
  31. package/dist/env/setup.d.ts +1 -0
  32. package/dist/env/setup.js +4 -0
  33. package/dist/env/validators.js +73 -10
  34. package/dist/env/vite-plugin-env.js +6 -5
  35. package/dist/i18n/index.d.ts +1 -1
  36. package/dist/i18n/index.js +4 -11
  37. package/dist/runtime/server/render/astro/render.js +8 -2
  38. package/dist/vite-plugin-astro-server/pipeline.d.ts +1 -1
  39. package/dist/vite-plugin-astro-server/pipeline.js +4 -4
  40. package/dist/vite-plugin-astro-server/request.js +2 -2
  41. package/package.json +13 -12
  42. package/dist/virtual-modules/env-setup.d.ts +0 -1
  43. package/dist/virtual-modules/env-setup.js +0 -4
@@ -3,19 +3,56 @@ import { PUBLIC_PREFIX } from "./constants.js";
3
3
  const StringSchema = z.object({
4
4
  type: z.literal("string"),
5
5
  optional: z.boolean().optional(),
6
- default: z.string().optional()
6
+ default: z.string().optional(),
7
+ max: z.number().optional(),
8
+ min: z.number().min(0).optional(),
9
+ length: z.number().optional(),
10
+ url: z.boolean().optional(),
11
+ includes: z.string().optional(),
12
+ startsWith: z.string().optional(),
13
+ endsWith: z.string().optional()
7
14
  });
8
15
  const NumberSchema = z.object({
9
16
  type: z.literal("number"),
10
17
  optional: z.boolean().optional(),
11
- default: z.number().optional()
18
+ default: z.number().optional(),
19
+ gt: z.number().optional(),
20
+ min: z.number().optional(),
21
+ lt: z.number().optional(),
22
+ max: z.number().optional(),
23
+ int: z.boolean().optional()
12
24
  });
13
25
  const BooleanSchema = z.object({
14
26
  type: z.literal("boolean"),
15
27
  optional: z.boolean().optional(),
16
28
  default: z.boolean().optional()
17
29
  });
18
- const EnvFieldType = z.discriminatedUnion("type", [StringSchema, NumberSchema, BooleanSchema]);
30
+ const EnumSchema = z.object({
31
+ type: z.literal("enum"),
32
+ values: z.array(
33
+ // We use "'" for codegen so it can't be passed here
34
+ z.string().refine((v) => !v.includes("'"), {
35
+ message: `The "'" character can't be used as an enum value`
36
+ })
37
+ ),
38
+ optional: z.boolean().optional(),
39
+ default: z.string().optional()
40
+ });
41
+ const EnvFieldType = z.union([
42
+ StringSchema,
43
+ NumberSchema,
44
+ BooleanSchema,
45
+ EnumSchema.superRefine((schema, ctx) => {
46
+ if (schema.default) {
47
+ if (!schema.values.includes(schema.default)) {
48
+ ctx.addIssue({
49
+ code: z.ZodIssueCode.custom,
50
+ message: `The default value "${schema.default}" must be one of the specified values: ${schema.values.join(", ")}.`
51
+ });
52
+ }
53
+ }
54
+ })
55
+ ]);
19
56
  const PublicClientEnvFieldMetadata = z.object({
20
57
  context: z.literal("client"),
21
58
  access: z.literal("public")
@@ -28,19 +65,17 @@ const SecretServerEnvFieldMetadata = z.object({
28
65
  context: z.literal("server"),
29
66
  access: z.literal("secret")
30
67
  });
68
+ const EnvFieldMetadata = z.union([
69
+ PublicClientEnvFieldMetadata,
70
+ PublicServerEnvFieldMetadata,
71
+ SecretServerEnvFieldMetadata
72
+ ]);
31
73
  const KEY_REGEX = /^[A-Z_]+$/;
32
74
  const EnvSchema = z.record(
33
75
  z.string().regex(KEY_REGEX, {
34
76
  message: "A valid variable name can only contain uppercase letters and underscores."
35
77
  }),
36
- z.intersection(
37
- z.union([
38
- PublicClientEnvFieldMetadata,
39
- PublicServerEnvFieldMetadata,
40
- SecretServerEnvFieldMetadata
41
- ]),
42
- EnvFieldType
43
- )
78
+ z.intersection(EnvFieldMetadata, EnvFieldType)
44
79
  ).superRefine((schema, ctx) => {
45
80
  for (const [key, value] of Object.entries(schema)) {
46
81
  if (key.startsWith(PUBLIC_PREFIX) && value.access !== "public") {
@@ -57,57 +92,6 @@ const EnvSchema = z.record(
57
92
  }
58
93
  }
59
94
  });
60
- const StringField = z.intersection(
61
- z.union([
62
- PublicClientEnvFieldMetadata,
63
- PublicServerEnvFieldMetadata,
64
- SecretServerEnvFieldMetadata
65
- ]),
66
- StringSchema
67
- );
68
- const StringFieldInput = z.intersection(
69
- z.union([
70
- PublicClientEnvFieldMetadata,
71
- PublicServerEnvFieldMetadata,
72
- SecretServerEnvFieldMetadata
73
- ]),
74
- StringSchema.omit({ type: true })
75
- );
76
- const NumberField = z.intersection(
77
- z.union([
78
- PublicClientEnvFieldMetadata,
79
- PublicServerEnvFieldMetadata,
80
- SecretServerEnvFieldMetadata
81
- ]),
82
- NumberSchema
83
- );
84
- const NumberFieldInput = z.intersection(
85
- z.union([
86
- PublicClientEnvFieldMetadata,
87
- PublicServerEnvFieldMetadata,
88
- SecretServerEnvFieldMetadata
89
- ]),
90
- NumberSchema.omit({ type: true })
91
- );
92
- const BooleanField = z.intersection(
93
- z.union([
94
- PublicClientEnvFieldMetadata,
95
- PublicServerEnvFieldMetadata,
96
- SecretServerEnvFieldMetadata
97
- ]),
98
- BooleanSchema
99
- );
100
- const BooleanFieldInput = z.intersection(
101
- z.union([
102
- PublicClientEnvFieldMetadata,
103
- PublicServerEnvFieldMetadata,
104
- SecretServerEnvFieldMetadata
105
- ]),
106
- BooleanSchema.omit({ type: true })
107
- );
108
95
  export {
109
- BooleanFieldInput,
110
- EnvSchema,
111
- NumberFieldInput,
112
- StringFieldInput
96
+ EnvSchema
113
97
  };
@@ -0,0 +1 @@
1
+ export { setGetEnv, type GetEnv } from './runtime.js';
@@ -0,0 +1,4 @@
1
+ import { setGetEnv } from "./runtime.js";
2
+ export {
3
+ setGetEnv
4
+ };
@@ -1,17 +1,66 @@
1
1
  function getEnvFieldType(options) {
2
2
  const optional = options.optional ? options.default !== void 0 ? false : true : false;
3
- return `${options.type}${optional ? " | undefined" : ""}`;
3
+ let type;
4
+ if (options.type === "enum") {
5
+ type = options.values.map((v) => `'${v}'`).join(" | ");
6
+ } else {
7
+ type = options.type;
8
+ }
9
+ return `${type}${optional ? " | undefined" : ""}`;
4
10
  }
5
- const stringValidator = (input) => {
11
+ const stringValidator = ({ max, min, length, url, includes, startsWith, endsWith }) => (input) => {
12
+ let valid = typeof input === "string";
13
+ if (valid && max !== void 0) {
14
+ valid = input.length <= max;
15
+ }
16
+ if (valid && min !== void 0) {
17
+ valid = input.length >= min;
18
+ }
19
+ if (valid && length !== void 0) {
20
+ valid = input.length === length;
21
+ }
22
+ if (valid && url !== void 0) {
23
+ try {
24
+ new URL(input);
25
+ } catch (_) {
26
+ valid = false;
27
+ }
28
+ }
29
+ if (valid && includes !== void 0) {
30
+ valid = input.includes(includes);
31
+ }
32
+ if (valid && startsWith !== void 0) {
33
+ valid = input.startsWith(startsWith);
34
+ }
35
+ if (valid && endsWith !== void 0) {
36
+ valid = input.endsWith(endsWith);
37
+ }
6
38
  return {
7
- valid: typeof input === "string",
39
+ valid,
8
40
  parsed: input
9
41
  };
10
42
  };
11
- const numberValidator = (input) => {
43
+ const numberValidator = ({ gt, min, lt, max, int }) => (input) => {
12
44
  const num = parseFloat(input ?? "");
45
+ let valid = !isNaN(num);
46
+ if (valid && gt !== void 0) {
47
+ valid = num > gt;
48
+ }
49
+ if (valid && min !== void 0) {
50
+ valid = num >= min;
51
+ }
52
+ if (valid && lt !== void 0) {
53
+ valid = num < lt;
54
+ }
55
+ if (valid && max !== void 0) {
56
+ valid = num <= max;
57
+ }
58
+ if (valid && int !== void 0) {
59
+ const isInt = Number.isInteger(num);
60
+ valid = int ? isInt : !isInt;
61
+ }
13
62
  return {
14
- valid: !isNaN(num),
63
+ valid,
15
64
  parsed: num
16
65
  };
17
66
  };
@@ -22,12 +71,26 @@ const booleanValidator = (input) => {
22
71
  parsed: bool
23
72
  };
24
73
  };
74
+ const enumValidator = ({ values }) => (input) => {
75
+ return {
76
+ valid: typeof input === "string" ? values.includes(input) : false,
77
+ parsed: input
78
+ };
79
+ };
80
+ function selectValidator(options) {
81
+ switch (options.type) {
82
+ case "string":
83
+ return stringValidator(options);
84
+ case "number":
85
+ return numberValidator(options);
86
+ case "boolean":
87
+ return booleanValidator;
88
+ case "enum":
89
+ return enumValidator(options);
90
+ }
91
+ }
25
92
  function validateEnvVariable(value, options) {
26
- const validator = {
27
- string: stringValidator,
28
- number: numberValidator,
29
- boolean: booleanValidator
30
- }[options.type];
93
+ const validator = selectValidator(options);
31
94
  const type = getEnvFieldType(options);
32
95
  if (options.optional || options.default !== void 0) {
33
96
  if (value === void 0) {
@@ -6,8 +6,7 @@ import {
6
6
  MODULE_TEMPLATE_URL,
7
7
  TYPES_TEMPLATE_URL,
8
8
  VIRTUAL_MODULES_IDS,
9
- VIRTUAL_MODULES_IDS_VALUES,
10
- VIRTUAL_MODULE_SETUP_ID
9
+ VIRTUAL_MODULES_IDS_VALUES
11
10
  } from "./constants.js";
12
11
  import { getEnvFieldType, validateEnvVariable } from "./validators.js";
13
12
  function astroEnv({
@@ -29,6 +28,11 @@ function astroEnv({
29
28
  fileURLToPath(settings.config.root),
30
29
  ""
31
30
  );
31
+ for (const [key, value] of Object.entries(loadedEnv)) {
32
+ if (value !== void 0) {
33
+ process.env[key] = value;
34
+ }
35
+ }
32
36
  const validatedVariables = validatePublicVariables({ schema, loadedEnv });
33
37
  const clientTemplates = getClientTemplates({ validatedVariables });
34
38
  const serverTemplates = getServerTemplates({ validatedVariables, schema, fs });
@@ -55,9 +59,6 @@ function astroEnv({
55
59
  if (VIRTUAL_MODULES_IDS_VALUES.has(id)) {
56
60
  return resolveVirtualModuleId(id);
57
61
  }
58
- if (id === VIRTUAL_MODULE_SETUP_ID) {
59
- return this.resolve("astro/virtual-modules/env-setup.js");
60
- }
61
62
  },
62
63
  load(id, options) {
63
64
  if (id === resolveVirtualModuleId(VIRTUAL_MODULES_IDS.client)) {
@@ -60,7 +60,7 @@ export declare function getLocaleAbsoluteUrlList(params: GetLocalesAbsoluteUrlLi
60
60
  */
61
61
  export declare function getPathByLocale(locale: string, locales: Locales): string;
62
62
  /**
63
- * An utility function that retrieves the preferred locale that correspond to a path.
63
+ * A utility function that retrieves the preferred locale that correspond to a path.
64
64
  *
65
65
  * @param path
66
66
  * @param locales
@@ -1,7 +1,7 @@
1
1
  import { appendForwardSlash, joinPaths } from "@astrojs/internal-helpers/path";
2
2
  import { shouldAppendForwardSlash } from "../core/build/util.js";
3
3
  import { REROUTE_DIRECTIVE_HEADER } from "../core/constants.js";
4
- import { MissingLocale } from "../core/errors/errors-data.js";
4
+ import { MissingLocale, i18nNoLocaleFoundInPath } from "../core/errors/errors-data.js";
5
5
  import { AstroError } from "../core/errors/index.js";
6
6
  import { createI18nMiddleware } from "./middleware.js";
7
7
  function requestHasLocale(locales) {
@@ -110,21 +110,21 @@ function getPathByLocale(locale, locales) {
110
110
  }
111
111
  }
112
112
  }
113
- throw new Unreachable();
113
+ throw new AstroError(i18nNoLocaleFoundInPath);
114
114
  }
115
115
  function getLocaleByPath(path, locales) {
116
116
  for (const locale of locales) {
117
117
  if (typeof locale !== "string") {
118
118
  if (locale.path === path) {
119
119
  const code = locale.codes.at(0);
120
- if (code === void 0) throw new Unreachable();
120
+ if (code === void 0) throw new AstroError(i18nNoLocaleFoundInPath);
121
121
  return code;
122
122
  }
123
123
  } else if (locale === path) {
124
124
  return locale;
125
125
  }
126
126
  }
127
- throw new Unreachable();
127
+ throw new AstroError(i18nNoLocaleFoundInPath);
128
128
  }
129
129
  function normalizeTheLocale(locale) {
130
130
  return locale.replaceAll("_", "-").toLowerCase();
@@ -163,13 +163,6 @@ function peekCodePathToUse(locales, locale) {
163
163
  }
164
164
  return void 0;
165
165
  }
166
- class Unreachable extends Error {
167
- constructor() {
168
- super(
169
- "Astro encountered an unexpected line of code.\nIn most cases, this is not your fault, but a bug in astro code.\nIf there isn't one already, please create an issue.\nhttps://astro.build/issues"
170
- );
171
- }
172
- }
173
166
  function redirectToDefaultLocale({
174
167
  trailingSlash,
175
168
  format,
@@ -150,6 +150,9 @@ async function renderToAsyncIterable(result, componentFactory, props, children,
150
150
  if (result.cancelled) return { done: true, value: void 0 };
151
151
  if (next !== null) {
152
152
  await next.promise;
153
+ } else if (!renderingComplete && !buffer.length) {
154
+ next = promiseWithResolvers();
155
+ await next.promise;
153
156
  }
154
157
  if (!renderingComplete) {
155
158
  next = promiseWithResolvers();
@@ -170,8 +173,9 @@ async function renderToAsyncIterable(result, componentFactory, props, children,
170
173
  }
171
174
  buffer.length = 0;
172
175
  const returnValue = {
173
- // The iterator is done if there are no chunks to return.
174
- done: length === 0,
176
+ // The iterator is done when rendering has finished
177
+ // and there are no more chunks to return.
178
+ done: length === 0 && renderingComplete,
175
179
  value: mergedArray
176
180
  };
177
181
  return returnValue;
@@ -197,6 +201,8 @@ async function renderToAsyncIterable(result, componentFactory, props, children,
197
201
  if (bytes.length > 0) {
198
202
  buffer.push(bytes);
199
203
  next?.resolve();
204
+ } else if (buffer.length > 0) {
205
+ next?.resolve();
200
206
  }
201
207
  }
202
208
  };
@@ -19,7 +19,7 @@ export declare class DevPipeline extends Pipeline {
19
19
  preload(routeData: RouteData, filePath: URL): Promise<ComponentInstance>;
20
20
  clearRouteCache(): void;
21
21
  getComponentByRoute(routeData: RouteData): Promise<ComponentInstance>;
22
- tryRewrite(payload: RewritePayload, request: Request, sourceRoute: RouteData): Promise<[RouteData, ComponentInstance]>;
22
+ tryRewrite(payload: RewritePayload, request: Request, sourceRoute: RouteData): Promise<[RouteData, ComponentInstance, URL]>;
23
23
  setManifestData(manifestData: ManifestData): void;
24
24
  rewriteKnownRoute(route: string, sourceRoute: RouteData): ComponentInstance;
25
25
  }
@@ -138,8 +138,8 @@ class DevPipeline extends Pipeline {
138
138
  if (!this.manifestData) {
139
139
  throw new Error("Missing manifest data. This is an internal error, please file an issue.");
140
140
  }
141
+ let finalUrl = void 0;
141
142
  for (const route of this.manifestData.routes) {
142
- let finalUrl = void 0;
143
143
  if (payload instanceof URL) {
144
144
  finalUrl = payload;
145
145
  } else if (payload instanceof Request) {
@@ -155,13 +155,13 @@ class DevPipeline extends Pipeline {
155
155
  break;
156
156
  }
157
157
  }
158
- if (foundRoute) {
158
+ if (foundRoute && finalUrl) {
159
159
  if (foundRoute.pathname === "/404") {
160
160
  const componentInstance = this.rewriteKnownRoute(foundRoute.pathname, sourceRoute);
161
- return [foundRoute, componentInstance];
161
+ return [foundRoute, componentInstance, finalUrl];
162
162
  } else {
163
163
  const componentInstance = await this.getComponentByRoute(foundRoute);
164
- return [foundRoute, componentInstance];
164
+ return [foundRoute, componentInstance, finalUrl];
165
165
  }
166
166
  } else {
167
167
  throw new AstroError({
@@ -1,4 +1,4 @@
1
- import { collapseDuplicateSlashes, removeTrailingForwardSlash } from "../core/path.js";
1
+ import { removeTrailingForwardSlash } from "../core/path.js";
2
2
  import { runWithErrorHandling } from "./controller.js";
3
3
  import { recordServerError } from "./error.js";
4
4
  import { handle500Response } from "./response.js";
@@ -12,7 +12,7 @@ async function handleRequest({
12
12
  }) {
13
13
  const { config, loader } = pipeline;
14
14
  const origin = `${loader.isHttps() ? "https" : "http"}://${incomingRequest.headers.host}`;
15
- const url = new URL(collapseDuplicateSlashes(origin + incomingRequest.url));
15
+ const url = new URL(origin + incomingRequest.url);
16
16
  let pathname;
17
17
  if (config.trailingSlash === "never" && !incomingRequest.url) {
18
18
  pathname = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro",
3
- "version": "4.10.0",
3
+ "version": "4.10.2",
4
4
  "description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.",
5
5
  "type": "module",
6
6
  "author": "withastro",
@@ -33,6 +33,7 @@
33
33
  },
34
34
  "./env": "./env.d.ts",
35
35
  "./env/runtime": "./dist/env/runtime.js",
36
+ "./env/setup": "./dist/env/setup.js",
36
37
  "./types": "./types.d.ts",
37
38
  "./client": "./client.d.ts",
38
39
  "./astro-jsx": "./astro-jsx.d.ts",
@@ -109,12 +110,12 @@
109
110
  ],
110
111
  "dependencies": {
111
112
  "@astrojs/compiler": "^2.8.0",
112
- "@babel/core": "^7.24.6",
113
- "@babel/generator": "^7.24.6",
114
- "@babel/parser": "^7.24.6",
115
- "@babel/plugin-transform-react-jsx": "^7.24.6",
116
- "@babel/traverse": "^7.24.6",
117
- "@babel/types": "^7.24.6",
113
+ "@babel/core": "^7.24.7",
114
+ "@babel/generator": "^7.24.7",
115
+ "@babel/parser": "^7.24.7",
116
+ "@babel/plugin-transform-react-jsx": "^7.24.7",
117
+ "@babel/traverse": "^7.24.7",
118
+ "@babel/types": "^7.24.7",
118
119
  "@types/babel__core": "^7.20.5",
119
120
  "@types/cookie": "^0.6.0",
120
121
  "acorn": "^8.11.3",
@@ -127,14 +128,14 @@
127
128
  "common-ancestor-path": "^1.0.1",
128
129
  "cookie": "^0.6.0",
129
130
  "cssesc": "^3.0.0",
130
- "debug": "^4.3.4",
131
+ "debug": "^4.3.5",
131
132
  "deterministic-object-hash": "^2.0.2",
132
133
  "devalue": "^5.0.0",
133
134
  "diff": "^5.2.0",
134
135
  "dlv": "^1.1.3",
135
136
  "dset": "^3.1.3",
136
137
  "es-module-lexer": "^1.5.3",
137
- "esbuild": "^0.21.4",
138
+ "esbuild": "^0.21.5",
138
139
  "estree-walker": "^3.0.3",
139
140
  "execa": "^8.0.1",
140
141
  "fast-glob": "^3.3.2",
@@ -156,13 +157,13 @@
156
157
  "rehype": "^13.0.1",
157
158
  "resolve": "^1.22.8",
158
159
  "semver": "^7.6.2",
159
- "shiki": "^1.6.1",
160
+ "shiki": "^1.6.3",
160
161
  "string-width": "^7.1.0",
161
162
  "strip-ansi": "^7.1.0",
162
163
  "tsconfck": "^3.1.0",
163
164
  "unist-util-visit": "^5.0.0",
164
165
  "vfile": "^6.0.1",
165
- "vite": "^5.2.12",
166
+ "vite": "^5.2.13",
166
167
  "vitefu": "^0.2.5",
167
168
  "which-pm": "^2.2.0",
168
169
  "yargs-parser": "^21.1.1",
@@ -211,7 +212,7 @@
211
212
  "rehype-toc": "^3.0.2",
212
213
  "remark-code-titles": "^0.1.2",
213
214
  "rollup": "^4.18.0",
214
- "sass": "^1.77.3",
215
+ "sass": "^1.77.4",
215
216
  "srcset-parse": "^1.1.0",
216
217
  "unified": "^11.0.4",
217
218
  "astro-scripts": "0.0.14"
@@ -1 +0,0 @@
1
- export { setGetEnv, type GetEnv } from '../env/runtime.js';
@@ -1,4 +0,0 @@
1
- import { setGetEnv } from "../env/runtime.js";
2
- export {
3
- setGetEnv
4
- };