astro 4.1.2 → 4.2.0

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 (104) hide show
  1. package/components/ViewTransitions.astro +6 -1
  2. package/dist/@types/astro.d.ts +127 -25
  3. package/dist/assets/internal.js +3 -2
  4. package/dist/assets/services/noop.js +1 -4
  5. package/dist/assets/types.d.ts +1 -1
  6. package/dist/assets/utils/emitAsset.js +1 -1
  7. package/dist/assets/utils/proxy.d.ts +2 -1
  8. package/dist/assets/utils/proxy.js +5 -1
  9. package/dist/assets/vite-plugin-assets.js +21 -14
  10. package/dist/content/runtime-assets.d.ts +1 -1
  11. package/dist/content/runtime-assets.js +1 -1
  12. package/dist/content/types-generator.js +0 -26
  13. package/dist/content/utils.d.ts +1 -1
  14. package/dist/content/utils.js +3 -10
  15. package/dist/content/vite-plugin-content-imports.js +1 -0
  16. package/dist/core/app/createOutgoingHttpHeaders.d.ts +9 -0
  17. package/dist/core/app/createOutgoingHttpHeaders.js +19 -0
  18. package/dist/core/app/index.d.ts +38 -1
  19. package/dist/core/app/index.js +38 -8
  20. package/dist/core/app/node.d.ts +42 -10
  21. package/dist/core/app/node.js +87 -46
  22. package/dist/core/app/types.d.ts +2 -1
  23. package/dist/core/config/schema.d.ts +146 -66
  24. package/dist/core/config/schema.js +24 -6
  25. package/dist/core/config/vite-load.js +2 -2
  26. package/dist/core/constants.js +1 -1
  27. package/dist/core/create-vite.js +3 -3
  28. package/dist/core/dev/dev.js +1 -1
  29. package/dist/core/endpoint/index.d.ts +2 -1
  30. package/dist/core/errors/dev/vite.js +5 -11
  31. package/dist/core/errors/errors-data.d.ts +5 -0
  32. package/dist/core/errors/errors-data.js +7 -0
  33. package/dist/core/errors/overlay.js +2 -2
  34. package/dist/core/logger/vite.d.ts +0 -1
  35. package/dist/core/logger/vite.js +1 -2
  36. package/dist/core/messages.js +2 -2
  37. package/dist/core/render/context.d.ts +3 -2
  38. package/dist/core/render/context.js +1 -1
  39. package/dist/core/render/result.d.ts +2 -1
  40. package/dist/core/routing/manifest/create.d.ts +1 -1
  41. package/dist/core/routing/manifest/create.js +175 -117
  42. package/dist/core/sync/index.js +2 -2
  43. package/dist/i18n/index.d.ts +3 -2
  44. package/dist/i18n/index.js +4 -4
  45. package/dist/i18n/middleware.js +36 -19
  46. package/dist/prefetch/index.js +17 -1
  47. package/dist/prefetch/vite-plugin-prefetch.js +4 -1
  48. package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/astro.js +2 -2
  49. package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/audit/a11y.js +52 -4
  50. package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/audit/index.js +3 -3
  51. package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/settings.js +7 -7
  52. package/dist/runtime/client/dev-toolbar/apps/utils/highlight.d.ts +8 -0
  53. package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/utils/window.d.ts +1 -1
  54. package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/xray.js +4 -4
  55. package/dist/runtime/client/{dev-overlay → dev-toolbar}/entrypoint.js +64 -68
  56. package/dist/runtime/client/{dev-overlay → dev-toolbar}/settings.d.ts +2 -2
  57. package/dist/runtime/client/{dev-overlay → dev-toolbar}/settings.js +10 -5
  58. package/dist/runtime/client/dev-toolbar/toolbar.d.ts +45 -0
  59. package/dist/runtime/client/{dev-overlay/overlay.js → dev-toolbar/toolbar.js} +99 -103
  60. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/badge.d.ts +1 -1
  61. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/badge.js +2 -2
  62. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/button.d.ts +1 -1
  63. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/button.js +3 -2
  64. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/card.d.ts +1 -1
  65. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/card.js +2 -2
  66. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/highlight.d.ts +1 -1
  67. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/highlight.js +2 -2
  68. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/icon.d.ts +1 -1
  69. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/icon.js +2 -2
  70. package/dist/runtime/client/dev-toolbar/ui-library/index.d.ts +8 -0
  71. package/dist/runtime/client/dev-toolbar/ui-library/index.js +18 -0
  72. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/toggle.d.ts +1 -1
  73. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/toggle.js +2 -2
  74. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/tooltip.d.ts +3 -3
  75. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/tooltip.js +2 -2
  76. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/window.d.ts +1 -1
  77. package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/window.js +3 -3
  78. package/dist/vite-plugin-astro/compile.js +0 -4
  79. package/dist/vite-plugin-astro/hmr.d.ts +3 -2
  80. package/dist/vite-plugin-astro/hmr.js +20 -41
  81. package/dist/vite-plugin-astro/index.js +36 -3
  82. package/dist/vite-plugin-astro/query.d.ts +0 -1
  83. package/dist/vite-plugin-astro/query.js +0 -5
  84. package/dist/vite-plugin-astro-server/route.js +3 -3
  85. package/dist/{vite-plugin-dev-overlay/vite-plugin-dev-overlay.d.ts → vite-plugin-dev-toolbar/vite-plugin-dev-toolbar.d.ts} +1 -1
  86. package/dist/{vite-plugin-dev-overlay/vite-plugin-dev-overlay.js → vite-plugin-dev-toolbar/vite-plugin-dev-toolbar.js} +5 -5
  87. package/dist/vite-plugin-markdown/images.js +46 -19
  88. package/package.json +4 -3
  89. package/dist/runtime/client/dev-overlay/overlay.d.ts +0 -45
  90. package/dist/runtime/client/dev-overlay/plugins/utils/highlight.d.ts +0 -8
  91. package/dist/runtime/client/dev-overlay/ui-library/index.d.ts +0 -8
  92. package/dist/runtime/client/dev-overlay/ui-library/index.js +0 -18
  93. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/astro.d.ts +0 -0
  94. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/audit/a11y.d.ts +0 -0
  95. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/audit/index.d.ts +0 -0
  96. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/settings.d.ts +0 -0
  97. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/utils/highlight.js +0 -0
  98. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/utils/icons.d.ts +0 -0
  99. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/utils/icons.js +0 -0
  100. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/utils/window.js +0 -0
  101. /package/dist/runtime/client/{dev-overlay/plugins → dev-toolbar/apps}/xray.d.ts +0 -0
  102. /package/dist/runtime/client/{dev-overlay → dev-toolbar}/entrypoint.d.ts +0 -0
  103. /package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/icons.d.ts +0 -0
  104. /package/dist/runtime/client/{dev-overlay → dev-toolbar}/ui-library/icons.js +0 -0
@@ -19,9 +19,10 @@ import {
19
19
  import { matchRoute } from "../routing/match.js";
20
20
  import { EndpointNotFoundError, SSRRoutePipeline } from "./ssrPipeline.js";
21
21
  import { deserializeManifest } from "./common.js";
22
- const clientLocalsSymbol = Symbol.for("astro.locals");
22
+ const localsSymbol = Symbol.for("astro.locals");
23
+ const clientAddressSymbol = Symbol.for("astro.clientAddress");
23
24
  const responseSentSymbol = Symbol.for("astro.responseSent");
24
- const STATUS_CODES = /* @__PURE__ */ new Set([404, 500]);
25
+ const REROUTABLE_STATUS_CODES = /* @__PURE__ */ new Set([404, 500]);
25
26
  class App {
26
27
  /**
27
28
  * The current environment of the application
@@ -115,7 +116,15 @@ class App {
115
116
  async render(request, routeDataOrOptions, maybeLocals) {
116
117
  let routeData;
117
118
  let locals;
118
- if (routeDataOrOptions && ("routeData" in routeDataOrOptions || "locals" in routeDataOrOptions)) {
119
+ let clientAddress;
120
+ let addCookieHeader;
121
+ if (routeDataOrOptions && ("addCookieHeader" in routeDataOrOptions || "clientAddress" in routeDataOrOptions || "locals" in routeDataOrOptions || "routeData" in routeDataOrOptions)) {
122
+ if ("addCookieHeader" in routeDataOrOptions) {
123
+ addCookieHeader = routeDataOrOptions.addCookieHeader;
124
+ }
125
+ if ("clientAddress" in routeDataOrOptions) {
126
+ clientAddress = routeDataOrOptions.clientAddress;
127
+ }
119
128
  if ("routeData" in routeDataOrOptions) {
120
129
  routeData = routeDataOrOptions.routeData;
121
130
  }
@@ -129,6 +138,12 @@ class App {
129
138
  this.#logRenderOptionsDeprecationWarning();
130
139
  }
131
140
  }
141
+ if (locals) {
142
+ Reflect.set(request, localsSymbol, locals);
143
+ }
144
+ if (clientAddress) {
145
+ Reflect.set(request, clientAddressSymbol, clientAddress);
146
+ }
132
147
  if (request.url !== collapseDuplicateSlashes(request.url)) {
133
148
  request = new Request(collapseDuplicateSlashes(request.url), request);
134
149
  }
@@ -138,7 +153,6 @@ class App {
138
153
  if (!routeData) {
139
154
  return this.#renderError(request, { status: 404 });
140
155
  }
141
- Reflect.set(request, clientLocalsSymbol, locals ?? {});
142
156
  const pathname = this.#getPathnameFromRequest(request);
143
157
  const defaultStatus = this.#getDefaultStatusCode(routeData, pathname);
144
158
  const mod = await this.#getModuleForRoute(routeData);
@@ -153,7 +167,7 @@ class App {
153
167
  );
154
168
  let response;
155
169
  try {
156
- let i18nMiddleware = createI18nMiddleware(
170
+ const i18nMiddleware = createI18nMiddleware(
157
171
  this.#manifest.i18n,
158
172
  this.#manifest.base,
159
173
  this.#manifest.trailingSlash
@@ -180,15 +194,19 @@ class App {
180
194
  }
181
195
  }
182
196
  if (routeData.type === "page" || routeData.type === "redirect") {
183
- if (STATUS_CODES.has(response.status)) {
197
+ if (REROUTABLE_STATUS_CODES.has(response.status)) {
184
198
  return this.#renderError(request, {
185
199
  response,
186
200
  status: response.status
187
201
  });
188
202
  }
189
- Reflect.set(response, responseSentSymbol, true);
190
- return response;
191
203
  }
204
+ if (addCookieHeader) {
205
+ for (const setCookieHeaderValue of App.getSetCookieFromResponse(response)) {
206
+ response.headers.append("set-cookie", setCookieHeaderValue);
207
+ }
208
+ }
209
+ Reflect.set(response, responseSentSymbol, true);
192
210
  return response;
193
211
  }
194
212
  #logRenderOptionsDeprecationWarning() {
@@ -203,6 +221,18 @@ class App {
203
221
  setCookieHeaders(response) {
204
222
  return getSetCookiesFromResponse(response);
205
223
  }
224
+ /**
225
+ * Reads all the cookies written by `Astro.cookie.set()` onto the passed response.
226
+ * For example,
227
+ * ```ts
228
+ * for (const cookie_ of App.getSetCookieFromResponse(response)) {
229
+ * const cookie: string = cookie_
230
+ * }
231
+ * ```
232
+ * @param response The response to read cookies from.
233
+ * @returns An iterator that yields key-value pairs as equal-sign-separated strings.
234
+ */
235
+ static getSetCookieFromResponse = getSetCookiesFromResponse;
206
236
  /**
207
237
  * Creates the render context of the current route
208
238
  */
@@ -1,25 +1,57 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
+ import { App } from './index.js';
3
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
4
  import type { RouteData } from '../../@types/astro.js';
3
5
  import type { RenderOptions } from './index.js';
4
6
  import type { SSRManifest } from './types.js';
5
- import { IncomingMessage } from 'node:http';
6
- import { App } from './index.js';
7
7
  export { apply as applyPolyfills } from '../polyfill.js';
8
- declare class NodeIncomingMessage extends IncomingMessage {
9
- /**
10
- * Allow the request body to be explicitly overridden. For example, this
11
- * is used by the Express JSON middleware.
12
- */
8
+ /**
9
+ * Allow the request body to be explicitly overridden. For example, this
10
+ * is used by the Express JSON middleware.
11
+ */
12
+ interface NodeRequest extends IncomingMessage {
13
13
  body?: unknown;
14
14
  }
15
15
  export declare class NodeApp extends App {
16
- match(req: NodeIncomingMessage | Request): RouteData | undefined;
17
- render(request: NodeIncomingMessage | Request, options?: RenderOptions): Promise<Response>;
16
+ match(req: NodeRequest | Request): RouteData | undefined;
17
+ render(request: NodeRequest | Request, options?: RenderOptions): Promise<Response>;
18
18
  /**
19
19
  * @deprecated Instead of passing `RouteData` and locals individually, pass an object with `routeData` and `locals` properties.
20
20
  * See https://github.com/withastro/astro/pull/9199 for more information.
21
21
  */
22
- render(request: NodeIncomingMessage | Request, routeData?: RouteData, locals?: object): Promise<Response>;
22
+ render(request: NodeRequest | Request, routeData?: RouteData, locals?: object): Promise<Response>;
23
+ /**
24
+ * Converts a NodeJS IncomingMessage into a web standard Request.
25
+ * ```js
26
+ * import { NodeApp } from 'astro/app/node';
27
+ * import { createServer } from 'node:http';
28
+ *
29
+ * const server = createServer(async (req, res) => {
30
+ * const request = NodeApp.createRequest(req);
31
+ * const response = await app.render(request);
32
+ * await NodeApp.writeResponse(response, res);
33
+ * })
34
+ * ```
35
+ */
36
+ static createRequest(req: NodeRequest, { skipBody }?: {
37
+ skipBody?: boolean | undefined;
38
+ }): Request;
39
+ /**
40
+ * Streams a web-standard Response into a NodeJS Server Response.
41
+ * ```js
42
+ * import { NodeApp } from 'astro/app/node';
43
+ * import { createServer } from 'node:http';
44
+ *
45
+ * const server = createServer(async (req, res) => {
46
+ * const request = NodeApp.createRequest(req);
47
+ * const response = await app.render(request);
48
+ * await NodeApp.writeResponse(response, res);
49
+ * })
50
+ * ```
51
+ * @param source WhatWG Response
52
+ * @param destination NodeJS ServerResponse
53
+ */
54
+ static writeResponse(source: Response, destination: ServerResponse): Promise<void>;
23
55
  }
24
56
  export declare function loadManifest(rootFolder: URL): Promise<SSRManifest>;
25
57
  export declare function loadApp(rootFolder: URL): Promise<NodeApp>;
@@ -1,30 +1,95 @@
1
- import * as fs from "node:fs";
2
- import { IncomingMessage } from "node:http";
3
- import { TLSSocket } from "node:tls";
4
- import { deserializeManifest } from "./common.js";
1
+ import fs from "node:fs";
5
2
  import { App } from "./index.js";
3
+ import { deserializeManifest } from "./common.js";
4
+ import { createOutgoingHttpHeaders } from "./createOutgoingHttpHeaders.js";
6
5
  import { apply } from "../polyfill.js";
7
6
  const clientAddressSymbol = Symbol.for("astro.clientAddress");
8
- function createRequestFromNodeRequest(req, options) {
9
- const protocol = req.socket instanceof TLSSocket || req.headers["x-forwarded-proto"] === "https" ? "https" : "http";
10
- const hostname = req.headers.host || req.headers[":authority"];
11
- const url = `${protocol}://${hostname}${req.url}`;
12
- const headers = makeRequestHeaders(req);
13
- const method = req.method || "GET";
14
- let bodyProps = {};
15
- const bodyAllowed = method !== "HEAD" && method !== "GET" && !options?.emptyBody;
16
- if (bodyAllowed) {
17
- bodyProps = makeRequestBody(req);
7
+ class NodeApp extends App {
8
+ match(req) {
9
+ if (!(req instanceof Request)) {
10
+ req = NodeApp.createRequest(req, {
11
+ skipBody: true
12
+ });
13
+ }
14
+ return super.match(req);
15
+ }
16
+ render(req, routeDataOrOptions, maybeLocals) {
17
+ if (!(req instanceof Request)) {
18
+ req = NodeApp.createRequest(req);
19
+ }
20
+ return super.render(req, routeDataOrOptions, maybeLocals);
18
21
  }
19
- const request = new Request(url, {
20
- method,
21
- headers,
22
- ...bodyProps
23
- });
24
- if (req.socket?.remoteAddress) {
25
- Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
22
+ /**
23
+ * Converts a NodeJS IncomingMessage into a web standard Request.
24
+ * ```js
25
+ * import { NodeApp } from 'astro/app/node';
26
+ * import { createServer } from 'node:http';
27
+ *
28
+ * const server = createServer(async (req, res) => {
29
+ * const request = NodeApp.createRequest(req);
30
+ * const response = await app.render(request);
31
+ * await NodeApp.writeResponse(response, res);
32
+ * })
33
+ * ```
34
+ */
35
+ static createRequest(req, { skipBody = false } = {}) {
36
+ const protocol = req.headers["x-forwarded-proto"] ?? ("encrypted" in req.socket && req.socket.encrypted ? "https" : "http");
37
+ const hostname = req.headers.host || req.headers[":authority"];
38
+ const url = `${protocol}://${hostname}${req.url}`;
39
+ const options = {
40
+ method: req.method || "GET",
41
+ headers: makeRequestHeaders(req)
42
+ };
43
+ const bodyAllowed = options.method !== "HEAD" && options.method !== "GET" && skipBody === false;
44
+ if (bodyAllowed) {
45
+ Object.assign(options, makeRequestBody(req));
46
+ }
47
+ const request = new Request(url, options);
48
+ if (req.socket?.remoteAddress) {
49
+ Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress);
50
+ }
51
+ return request;
52
+ }
53
+ /**
54
+ * Streams a web-standard Response into a NodeJS Server Response.
55
+ * ```js
56
+ * import { NodeApp } from 'astro/app/node';
57
+ * import { createServer } from 'node:http';
58
+ *
59
+ * const server = createServer(async (req, res) => {
60
+ * const request = NodeApp.createRequest(req);
61
+ * const response = await app.render(request);
62
+ * await NodeApp.writeResponse(response, res);
63
+ * })
64
+ * ```
65
+ * @param source WhatWG Response
66
+ * @param destination NodeJS ServerResponse
67
+ */
68
+ static async writeResponse(source, destination) {
69
+ const { status, headers, body } = source;
70
+ destination.writeHead(status, createOutgoingHttpHeaders(headers));
71
+ if (body) {
72
+ try {
73
+ const reader = body.getReader();
74
+ destination.on("close", () => {
75
+ reader.cancel().catch((err) => {
76
+ console.error(
77
+ `There was an uncaught error in the middle of the stream while rendering ${destination.req.url}.`,
78
+ err
79
+ );
80
+ });
81
+ });
82
+ let result = await reader.read();
83
+ while (!result.done) {
84
+ destination.write(result.value);
85
+ result = await reader.read();
86
+ }
87
+ } catch {
88
+ destination.write("Internal server error");
89
+ }
90
+ }
91
+ destination.end();
26
92
  }
27
- return request;
28
93
  }
29
94
  function makeRequestHeaders(req) {
30
95
  const headers = new Headers();
@@ -65,33 +130,9 @@ function asyncIterableToBodyProps(iterable) {
65
130
  // The duplex property is required when using a ReadableStream or async
66
131
  // iterable for the body. The type definitions do not include the duplex
67
132
  // property because they are not up-to-date.
68
- // @ts-expect-error
69
133
  duplex: "half"
70
134
  };
71
135
  }
72
- class NodeIncomingMessage extends IncomingMessage {
73
- /**
74
- * Allow the request body to be explicitly overridden. For example, this
75
- * is used by the Express JSON middleware.
76
- */
77
- body;
78
- }
79
- class NodeApp extends App {
80
- match(req) {
81
- if (!(req instanceof Request)) {
82
- req = createRequestFromNodeRequest(req, {
83
- emptyBody: true
84
- });
85
- }
86
- return super.match(req);
87
- }
88
- render(req, routeDataOrOptions, maybeLocals) {
89
- if (!(req instanceof Request)) {
90
- req = createRequestFromNodeRequest(req);
91
- }
92
- return super.render(req, routeDataOrOptions, maybeLocals);
93
- }
94
- }
95
136
  async function loadManifest(rootFolder) {
96
137
  const manifestFile = new URL("./manifest.json", rootFolder);
97
138
  const rawManifest = await fs.promises.readFile(manifestFile, "utf-8");
@@ -1,5 +1,6 @@
1
1
  import type { Locales, RouteData, SerializedRouteData, SSRComponentMetadata, SSRLoadedRenderer, SSRResult } from '../../@types/astro.js';
2
2
  import type { SinglePageBuiltModule } from '../build/types.js';
3
+ import type { RoutingStrategies } from '../config/schema.js';
3
4
  export type ComponentPath = string;
4
5
  export type StylesheetAsset = {
5
6
  type: 'inline';
@@ -47,7 +48,7 @@ export type SSRManifest = {
47
48
  };
48
49
  export type SSRManifestI18n = {
49
50
  fallback?: Record<string, string>;
50
- routing?: 'prefix-always' | 'prefix-other-locales';
51
+ routing?: RoutingStrategies;
51
52
  locales: Locales;
52
53
  defaultLocale: string;
53
54
  };